Attach position information to errors

This commit is contained in:
Marijn Haverbeke 2013-01-16 12:19:13 +01:00
parent 3c2bd590da
commit c2dd92fec0
2 changed files with 20 additions and 16 deletions

View File

@ -164,15 +164,17 @@
var inFunction, labels, strict;
// This function is used to raise exceptions on parse errors. It
// takes either a `{line, column}` object or an offset integer (into
// the current `input`) as `pos` argument. It attaches the position
// to the end of the error message, and then raises a `SyntaxError`
// with that message.
// takes an offset integer (into the current `input`) to indicate
// the location of the error, attaches the position to the end
// of the error message, and then raises a `SyntaxError` with that
// message.
function raise(pos, message) {
if (typeof pos == "number") pos = getLineInfo(input, pos);
message += " (" + pos.line + ":" + pos.column + ")";
throw new SyntaxError(message);
var loc = getLineInfo(input, pos);
message += " (" + loc.line + ":" + loc.column + ")";
var err = new SyntaxError(message);
err.pos = pos; err.loc = loc;
throw err;
}
// ## Token types
@ -626,7 +628,7 @@
var tok = getTokenFromCode(code);
if(tok === false) {
if (tok === false) {
// If we are here, we either found a non-ASCII identifier
// character, or something that's entirely disallowed.
var ch = String.fromCharCode(code);

View File

@ -91,13 +91,15 @@ when finishing a node and assigning its <code>end</code> position.</p>
<code>return</code> statements outside of functions, <code>labels</code> to verify that
<code>break</code> and <code>continue</code> have somewhere to jump to, and <code>strict</code>
indicates whether strict mode is on.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">inFunction</span><span class="p">,</span> <span class="nx">labels</span><span class="p">,</span> <span class="nx">strict</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>This function is used to raise exceptions on parse errors. It
takes either a <code>{line, column}</code> object or an offset integer (into
the current <code>input</code>) as <code>pos</code> argument. It attaches the position
to the end of the error message, and then raises a <code>SyntaxError</code>
with that message.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">raise</span><span class="p">(</span><span class="nx">pos</span><span class="p">,</span> <span class="nx">message</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">pos</span> <span class="o">==</span> <span class="s2">&quot;number&quot;</span><span class="p">)</span> <span class="nx">pos</span> <span class="o">=</span> <span class="nx">getLineInfo</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="nx">pos</span><span class="p">);</span>
<span class="nx">message</span> <span class="o">+=</span> <span class="s2">&quot; (&quot;</span> <span class="o">+</span> <span class="nx">pos</span><span class="p">.</span><span class="nx">line</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span> <span class="o">+</span> <span class="nx">pos</span><span class="p">.</span><span class="nx">column</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span><span class="p">;</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
takes an offset integer (into the current <code>input</code>) to indicate
the location of the error, attaches the position to the end
of the error message, and then raises a <code>SyntaxError</code> with that
message.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">raise</span><span class="p">(</span><span class="nx">pos</span><span class="p">,</span> <span class="nx">message</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">loc</span> <span class="o">=</span> <span class="nx">getLineInfo</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="nx">pos</span><span class="p">);</span>
<span class="nx">message</span> <span class="o">+=</span> <span class="s2">&quot; (&quot;</span> <span class="o">+</span> <span class="nx">loc</span><span class="p">.</span><span class="nx">line</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span> <span class="o">+</span> <span class="nx">loc</span><span class="p">.</span><span class="nx">column</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">err</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
<span class="nx">err</span><span class="p">.</span><span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span><span class="p">;</span> <span class="nx">err</span><span class="p">.</span><span class="nx">loc</span> <span class="o">=</span> <span class="nx">loc</span><span class="p">;</span>
<span class="k">throw</span> <span class="nx">err</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <h2>Token types</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>The assignment of fine-grained, information-carrying type objects
allows the tokenizer to store the information it has about a
token in a way that is very cheap for the parser to look up.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>All token type variables start with an underscore, to make them
@ -420,7 +422,7 @@ identifiers, so '\' also dispatches to that.</p> </td> <
<span class="kd">var</span> <span class="nx">tok</span> <span class="o">=</span> <span class="nx">getTokenFromCode</span><span class="p">(</span><span class="nx">code</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="nx">tok</span> <span class="o">===</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>If we are here, we either found a non-ASCII identifier
<span class="k">if</span> <span class="p">(</span><span class="nx">tok</span> <span class="o">===</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>If we are here, we either found a non-ASCII identifier
character, or something that's entirely disallowed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">ch</span> <span class="o">=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">code</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">ch</span> <span class="o">===</span> <span class="s2">&quot;\\&quot;</span> <span class="o">||</span> <span class="nx">nonASCIIidentifierStart</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">ch</span><span class="p">))</span> <span class="k">return</span> <span class="nx">readWord</span><span class="p">();</span>
<span class="nx">raise</span><span class="p">(</span><span class="nx">tokPos</span><span class="p">,</span> <span class="s2">&quot;Unexpected character &#39;&quot;</span> <span class="o">+</span> <span class="nx">ch</span> <span class="o">+</span> <span class="s2">&quot;&#39;&quot;</span><span class="p">);</span>