Closed ex3ndr closed 6 years ago
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
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]
};
}
;
The jison lexer provides a few extra APIs which can be used to simplify working with location tracking info objects ('YYLLOC' objects):
deriveLocationInfo()
prettyPrintRange()
describeYYLLOC()
- useful when debugging lex/parse run-time activityThe 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.
When in doubt, check the code: these are the lexer and parser kernel codes for the latest npm release:
HTH
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).
Thank you for that good answer!
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?