<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="199633" data-post-id="199633">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mattmower" data-post="9" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/48/21353_2.png" class="avatar"> mattmower:</div>
<blockquote>
<p>I understand the words but cannot put them into action. Do you have an example I could learn from?</p>
</blockquote>
</aside>
<p>It is mostly an implementation concern that you should generally not worry about but I am glad to provide more information:</p>
<p>You can think that every combinator in NimbleParsec compiles to a function. Therefore, if you had something like this:</p>
<pre><code>ascii_char([?a..?z]) |&gt; ascii_char([?0..?9])
</code></pre>
<p>We would compile it down to this (simplified):</p>
<pre><code>defp node_1(&lt;&lt;x, rest::binary&gt;&gt;) when x in ?a..?z, do: node_2(rest)
defp node_1(_), do: :error

defp node_2(&lt;&lt;x, rest::binary&gt;&gt;) when x in ?0..?9, do: node_2(rest)
defp node_2(_), do: :error

defp node_3(rest), do: {:ok, rest}
</code></pre>
<p>However this approach would be expensive both in terms of compilation and at runtime. So we compile it to this:</p>
<pre><code>defp node_1(&lt;&lt;x, y, rest::binary&gt;&gt;) when x in ?a..?z and y in ?0..?9, do: node_3(rest)
defp node_1(_), do: :error

defp node_3(rest), do: {:ok, rest}
</code></pre>
<p>You can see “node_1” and “node_2” were fused into the same pattern. The downside of this approach is that if you fail on “node_2”, because they were fused, the pattern reports it failed at “node_1”.</p>
<p>But this should not affect the parsing behaviour, it only affects the reported lines in case of errors.</p>
<aside class="quote no-group" data-username="mattmower" data-post="9" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/48/21353_2.png" class="avatar"> mattmower:</div>
<blockquote>
<p>I am also not sure why the error information reports <code>{…{line,offset},offset,…}</code>. The two offsets are always close but not exact.</p>
</blockquote>
</aside>
<p>The first offset is the offset on the current line. The second is the offset since the beginning of the binary. The offset will be the column if you are parsing ascii. However, if you are parsing something with UTF-8 support, then in case of errors you will have to build the column using the line and offset. The algorithm would go like this:</p>
<ol>
<li>Get the original input</li>
<li>Find the nth line given by <code>line</code></li>
<li>Now get the offset bytes for that line and call <code>String.length(binary_part(line_str, 0, offset))</code></li>
</ol>
<p>We can probably add a helper function to do this in NimbleParsec.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199633" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-199633" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199633"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="199634" data-post-id="199634">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="12" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>The first offset is the offset on the current line. The second is the offset since the beginning of the binary.</p>
</blockquote>
</aside>
<p>This doesn’t seem to be what I’m seeing. According to <code>file</code> I am parsing a file that is <code>ASCII text</code> but the “line offset” is clearly not referring to a column of that line (e.g. getting a value of 60 something in a line with 10 characters in it). I’ll have to come up with a concrete example later but, I don’t think I am seeing columns.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199634" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-199634" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199634"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="199637" data-post-id="199637">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sorry, I was speaking from memory and it seems I misspoke.</p>
<p>Please try this interpretation: the offset in <code>{line, offset}</code> is the offset the current line starts. And the other <code>offset</code> is either the offset to the current line or the offset in comparison to the whole binary. If the latter, you can get the line offset by doing <code>binary_offset - line_offset</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199637" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-199637" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199637"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="199687" data-post-id="199687">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m going to need a little more time to digest your comments Sasa but I wanted to say thanks for the link to your talk which I just watched and which really helped me to understand what was going on with the combinator approach. It’s not a technique I had used before and I kind of skipped over the underlying concepts in my hurry. Thank you.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199687" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-199687" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199687"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="199833" data-post-id="199833">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>initialize your parser context as a struct ([<a href="https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L68" rel="noopener nofollow ugc">https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L68</a> ]</li>
</ul>
</blockquote>
</aside>
<p>That’s the context returned in:</p>
<p><code>{:ok, [token], rest, context, position, byte_offset}</code> or<br>
<code>{:error, reason, rest, context, line, byte_offset}</code></p>
<p>?</p>
<p>At this point, I am not sure how to interact with the context but I generally concur with the idea of replacing maps with structs when you know what the structure should look like.</p>
<p>Is this the key to implementing better error handling? That you adorn the context with more information?</p>
<aside class="quote no-group" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>avoid using anything besides structs in your context</li>
</ul>
</blockquote>
</aside>
<p>I’m not quite sure what you mean. Are you restating point 1 or do you mean that your context should also only contain structs? That doesn’t seem to be the case in the ZigParser. Can you clarify?</p>
<aside class="quote no-group" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>each combinator should be a series of textual matching, only followed by single (optional post_traverse),<br>
that analyzes all text that you have kept around. The only other place I use post_traverse is for internal validations, but I make sure that these traversers don’t alter context state, they only raise on syntax error.</li>
</ul>
</blockquote>
</aside>
<p>I’m really not sure what guidance you are giving here. All of the combinators are textual matching… I think you are talking about something more subtle? I’ve not used <code>post_traverse</code> so far only <code>map</code> and <code>reduce</code> for converting to lists and maps. Do you have an example of what you are saying not to do?</p>
<aside class="quote no-group" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>I make a conscious decision to either throw away all of the text always or, only do textual transformations. You <em>can</em> put arbitrary terms in your outputs, but you probably shouldn’t. It’s better to stash them in the context and pull them out at the end. Thus in most cases I avoid label, for example. Here is an example of <code>clear</code> helper function I wrote to make sure that I’m in a sane state after any block. <a href="https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L410" rel="noopener nofollow ugc">https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L410 </a>.</li>
</ul>
</blockquote>
</aside>
<p>I confess that I am really not following this at all.</p>
<aside class="quote no-group" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>create tests for your more complex intermediate combinators: <a href="https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L151" rel="noopener nofollow ugc">https://github.com/ityonemo/zigler/blob/master/lib/zig/parser.ex#L151</a> that you can verify in unit testing. You don’t have to keep them around in dev/prod.</li>
</ul>
</blockquote>
</aside>
<p>That’s good advice. I am switching from defining helpers as inline variables to functions in a helper module which will, I think, help with that.</p>
<aside class="quote no-group" data-username="ityonemo" data-post="4" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<ul>
<li>don’t be afraid to raise. I typically use SyntaxError. If you’re new to elixir, when you’re testing, <code>assert_raise SyntaxError, fn &lt;erroring code&gt; end</code> is a thing.</li>
</ul>
</blockquote>
</aside>
<p>Looking at where you are doing this I think it relates to my confusion in the points above. I get the idea that you would raise SyntaxErrors (or possibly “SemanticErrors”) but there is something here about the combinator/context/post_traverse interaction that you are assuming as good practice and I have not grasped yet.</p>
<p>Ah well, beginners mind. If you care to expand on any of your points that would be great but you have, in any case, given me a lot to think about. Thank you.</p>
<p>Matt</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199833" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-199833" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199833"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="199834" data-post-id="199834">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sasajuric" data-post="10" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/48/991_2.png" class="avatar"> sasajuric:</div>
<blockquote>
<p>It was possible to do this with combine, but I don’t think you can make it work with nimble, because nimble always expects a binary input, and if you’re doing two passes, your input in the 2nd pass is the list of terms, not a string of chars. Therefore, my impression is that nimble is better suited for the cases where precise error reporting is not required, or the cases where the grammar is not too complicated. Otherwise, I’d either roll my own combinator (I did a talk on this, you can find it <a href="https://www.youtube.com/watch?v=xNzoerDljjo" rel="noopener nofollow ugc">here </a>), or hand-write a recursive-descent parser. The latter will give you maximum flexibility and speed, but the code will be significantly noisier.</p>
</blockquote>
</aside>
<p>I’m writing a game definition parser (not a natural language parser) as part of an interactive fiction game engine. So I will be parsing primarily human authored text.</p>
<p>In the first case that text will be written by me and I have a low tolerance for being frustrated by error messages (one of my constant bug bears with Clojure). So error handling &amp; reporting are important to me.</p>
<p>Certainly if I imagine anyone else might use it I think it’s essential that an error message be helpful about the position and likely nature of what is wrong.</p>
<p>My understanding is that Jose has done a lot of work on the resulting parsers to make sure they are very performant. While speed is always welcome I’m not writing a parser for HTTP requests where every microsecond counts.</p>
<p>Is the hand-rolled combinator long-term feasible? What would I regret by forgoing NimbleParsec and hand-rolling as you outlined in your video. It seems pretty easy to get started with, but I wonder what I am missing. I guess there is a way to find out…</p>
<p>Thanks.</p>
<p>Matt</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199834" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-199834" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199834"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="199835" data-post-id="199835">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That makes sense, thanks, Jose. I’m not sure if I’ve done it right but I <em>think</em> I’ve submitted a PR to clarify the docs.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199835" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-199835" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199835"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="199855" data-post-id="199855">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If precise error handling is required, I personally wouldn’t use Nimble, except maybe for tokenization.</p>
<p>Hand-rolling a parser for complex languages is definitely feasible. For example see <a href="https://github.com/gleam-lang/gleam/pull/891" rel="noopener nofollow ugc">this PR in Gleam</a> where switching to a hand-rolled parser improved error reporting and parsing speed. Also, I recall reading that at some point GCC switched to a hand-rolled recursive-descent parser to improve error reporting.</p>
<p>In cases where the grammar is more involved, I’d probably start with a hand-rolled parser combinator. Being in control of the combinator should provide maximum flexibility, while the parser code should remain readable.</p>
<p>If maximum performance is required, parts of the parser could be converted to manual recursive descent. For example of this technique, take a look at <a href="https://github.com/sasa1977/aoc/blob/master/lib/2018/day20.ex#L94-L143" rel="noopener nofollow ugc">this parser</a> which parses regex-like inputs (input example: <code>"^ENWWW(NEEE|SSE(EE|N))$"</code>, full problem description is <a href="https://adventofcode.com/2018/day/20" rel="noopener nofollow ugc">here</a>).</p>
<p>To better understand this, I advise trying these techniques on a small toy grammar. An arithmetic expression parser which supports parentheses and operator precedence is IMO a great example. You could develop it incrementally, e.g.:</p>
<ol>
<li>Support flat expression with just one operator (e.g. 1+2+34).</li>
<li>Add support for ignoring whitespaces</li>
<li>Introduce the <code>*</code> operator, and handle operator precedence (e.g. parsing <code>1 * 2 + 3 * 4</code> should return something like <code>{+, [{*, [1, 2]}, {*, [3, 4]}}</code>).</li>
<li>Add support for parentheses</li>
<li>Improve quality of reported errors</li>
</ol>
<p>Start by implementing this with the recursive descent technique. Then see how could it be done with the combinators. This should help you understand the differences between these two approaches.</p>
<p>As a bonus exercise, try implementing two-pass compiling. This can be done by using e.g. Nimble to convert the original input into a list of tokens (e.g. convert <code>1 * (2 + 3)</code> into <code>[{:integer, 1}, {:operator, "*"}, {:operator, "("}, ...]</code>), and then converting such list into the final ast. IMO, when grammar is more complex, explicit tokenization pass can do wonders for code readability.</p>
<p>Finally, you can also consider using <a href="https://erlang.org/doc/man/yecc.html" rel="nofollow">yecc</a>. IIRC, Elixir parser is based on yecc. Also, absinthe does two-phase parsing, <a href="https://github.com/absinthe-graphql/absinthe/blob/master/lib/absinthe/lexer.ex" rel="noopener nofollow ugc">using nimble for the first pass</a>, and <a href="https://github.com/absinthe-graphql/absinthe/blob/9cb09001d32334debaf2b3d8ed9ebcd3f61fbb99/src/absinthe_parser.yrl" rel="noopener nofollow ugc">yecc for the second</a>.</p>
<p>I understand this may all seem overwhelming, and I barely scratched the surface of the complex topic of parsing <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> So as a final parting advice, I think that starting with a hand-rolled combinator is probably a sensible choice which will give you a lot of control and flexibility, while the code should still be readable. As soon as the grammar becomes more complex, consider doing two-phase parsing, with the first pass powered by e.g. nimble.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199855" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-199855" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199855"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="199950" data-post-id="199950">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mattmower" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/120/21353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mattmower
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Given I have the luxury of time I have decided to try and roll my own system of parser combinators along the lines you describe in your video but starting from the principle of providing good error reporting. I’ve made good progress on the simple stuff and will try and separate it from the project and upload to Github so others can comment. If nothing else I hope to come back to Nimble with a better perspective.</p>
<p>One thing I have done, for better or worse, is define a “parser context” struct type that contains among other things the input, position, and term data and that parsers are written in terms of a context rather than a binary input. This slightly complicates the beginning in that you have to wrap the initial input in a context but I think makes it easier to do the right thing later. Please someone tell me if I am making a ghastly mistake here.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199950" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-199950" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199950"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="199958" data-post-id="199958">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mattmower" data-post="20" data-topic="36637">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattmower/48/21353_2.png" class="avatar"> mattmower:</div>
<blockquote>
<p>One thing I have done, for better or worse, is define a “parser context” struct type that contains among other things the input, position, and term data and that parsers are written in terms of a context rather than a binary input. This slightly complicates the beginning in that you have to wrap the initial input in a context but I think makes it easier to do the right thing later. Please someone tell me if I am making a ghastly mistake here.</p>
</blockquote>
</aside>
<p>This is fine, and it’s in fact one reason why I recommend rolling your own combinator, rather than using some of the existing ones. With your own combinator you’re in full control, and you can shape the context to your own particular needs.</p>
<p>My talk is envisioned as a “gentle” introduction, so I oversimplified many things. Don’t treat it as a definitive reference, and feel free to diverge from it wherever it makes sense. For example, as I said in my initial post, parsers such as <code>choice</code>, <code>optional</code>, and <code>many</code>, will lead to uninformative reported errors, so you’ll probably need to invent some alternatives based on a lookahead technique (decide what to do next based on what the next term is).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="199958" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/can-you-help-me-understand-how-to-implement-nimbleparsec-error-handling/36637/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-199958" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="199958"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/36637/load_more?page=3">Load more posts (1 remaining)</a>
</div></template></turbo-stream>