language icon indicating copy to clipboard operation
language copied to clipboard

Line number in "rule is not defined" error

Open shamrin opened this issue 13 years ago • 3 comments
trafficstars

When referencing undefined symbol in the grammar rule, the following is throwed:


It's nice, but line number would be even better.

I tried implementing this, but got stuck at traverseRulesAnchoredAtName. At that point information about rule syntax nodes is lost. The function also seems to be highly optimized: uses its own stack instead of recursion.

Any suggestions on how to implement it?

shamrin avatar Apr 12 '12 13:04 shamrin

If you look at SyntaxNode.prototype.message you will see a way to calculate a lineNumber for a given SyntaxNode. This should probably be factored out into something like SyntaxNode.prototype.lineNumber.

After this you would want to add the defining node to the rule object in buildRulesFromSyntaxTree. This would actually go into the individual rule builders (e.g. https://github.com/tolmasky/language/blob/master/lib/compiledgrammar.js#L251 ). You would just add node:aNode to that object creation line.

Now finally in traverseRulesAnchoredAtName when you throw you can just use rule.node.lineNumber() to get the line number in question.

tolmasky avatar Apr 12 '12 13:04 tolmasky

Thanks! The problem with your suggestion is that rule is undefined at this point... And no easy way to find out what rule produced the hash...

I decided to do it the other way, with firstUsageNodes object:

https://github.com/tolmasky/language/pull/20

Here's how the message looks like:

multiplicative = primary ([*/] miltiplicative)?
                               ^^^^^^^^^^^^^^
ERROR line 6: Rule "miltiplicative" is not defined.

What do you think?

shamrin avatar Apr 12 '12 15:04 shamrin

Francisco, could you please comment on my patch? Is idea good or bad? I can completely rewrite my patch if needed.

shamrin avatar Apr 25 '12 18:04 shamrin