GerHobbelt / jison

bison / YACC / LEX in JavaScript (LALR(1), SLR(1), etc. lexer/parser generator)
https://gerhobbelt.github.io/jison/
MIT License
118 stars 20 forks source link

Error handling? #41

Closed ex3ndr closed 6 years ago

ex3ndr commented 6 years ago

Is there any guide or reference documentation about how i can find exact place where error is occurred? I have found a way to find lexer error position, but for parser there are only line number and there are no offset in the document (or in line). Am i missing something?

GerHobbelt commented 6 years ago

Lexer

Location tracking is available via the lex/flex style yylloc variable (which is expanded under the hood by jison to this.yylloc; I advise to use the yylloc variable name in your lexer action code blocks and let jison do the code rewriting.)

Next to start and end line and column fields, you can also have the jison lexer produce a input index range array when you add this option:

%options ranges

Parser

The lexer-produced yylloc location tracking info is available in your grammar rules through the @token variables/references, similar to the $token variables/references providing access to the lexed value of the given token (lexer: yytext).

Example:

grammarRule: TOKEN_A subrule  {
    $$ = {
      value: $TOKEN_A + $subrule,
      locations: [@TOKEN_A, @subrule]
    };
  }
;

Extras (advanced jison coding)

The jison lexer provides a few extra APIs which can be used to simplify working with location tracking info objects ('YYLLOC' objects):

The parser kernel includes an (internal use) additional API: yyMergeLocationInfo(), which can be used to mix multiple YYLLOC objects into a single spanning one.

WARNING: these 'extras' are specific to jison-gho (i.e. GerHobbelt/jison) and have appeared in the more recent jison-gho releases.

RTFC-To-The-Rescue

When in doubt, check the code: these are the lexer and parser kernel codes for the latest npm release:

HTH

GerHobbelt commented 6 years ago

Code Generation - Optimizations (A Side Note)

Note: depending on the features you use some sections of these kernels will be removed while generating your specific parser.

This means any use of a @xxx variable reference in any of your grammar rules' action code blocks will cause jison to detect 'location tracking' as being in use.

Currently the lexer code generator is rather dumb and always spits out location tracking coding as the detection logic in the lexer is currently a no-op, but that will change in future jison-gho releases as lexers can execute much faster when there's no location tracking required by the userland code (particularly line tracking, i.e. CR/LF monitoring in the lexer).

ex3ndr commented 6 years ago

Thank you for that good answer!