Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust_parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h2 id="lexing">Lexing</h2>
Num(<span class="dt">u64</span>),
}</code></pre></div>
<p>I could have used more enum values to distinguish between <code>+</code> and <code>*</code> and the different types of parentheses, but instead I just store the character. It probably would have been a good idea to add another integer to each <code>LexItem</code> that stores the location in the input at which the token starts. That would make error reporting more useful. Instead I will just use the position in the token stream for my errors. Since I’m the only user of this program, I will only be angry at myself, so it’s okay.</p>
<p>The language I want to parse is very simple to lex. Except numbers, all tokens are just a single character long. So instead of complicated things with regular expressions. Instead I iterate over the characters of my input <code>String</code> and use a <code>match</code> do create a <code>LexItem</code>. The <code>match</code> statement is really handy here, since I can specify multiple alternatives for the same case with <code>|</code> and ranges of characters are also supported.</p>
<p>The language I want to parse is very simple to lex. Except numbers, all tokens are just a single character long. So instead of doing complicated things with regular expressions, I iterate over the characters of my input <code>String</code> and use a <code>match</code> do create a <code>LexItem</code>. The <code>match</code> statement is really handy here, since I can specify multiple alternatives for the same case with <code>|</code> and ranges of characters are also supported.</p>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> lex(input: &amp;<span class="dt">String</span>) -&gt; <span class="dt">Result</span>&lt;<span class="dt">Vec</span>&lt;LexItem&gt;, <span class="dt">String</span>&gt; {
<span class="kw">let</span> <span class="kw">mut</span> result = <span class="dt">Vec</span>::new();

Expand Down