maciejhirsz / logos

Create ridiculously fast Lexers
https://logos.maciej.codes
Apache License 2.0
2.9k stars 118 forks source link

Concept of a lexer mode #419

Open ccleve opened 1 month ago

ccleve commented 1 month ago

Is it possible for the lexer to run in different modes, where in one mode something might be recognized, and it in another it would not?

For example, when lexing

red AND blue

A lexer running in "query" mode might recognize AND as a operator, and it "text" mode it would treat it as a regular token.

Other lexers that I've used, including JFlex and Antlr, have this feature and it's extremely useful.

jeertmans commented 1 month ago

Well, yes and not. You cannot change the Lexer at runtime, as it is constructed during compilation (which makes it fast). However, you can still use callbacks, extras, and other techniques to interpret the tokens differently, depending on the context. At runtime, in a callback, you can also use Lexer.morph to change the lexer to produce other tokens.