Allow multiple lexer modes to be specified for a lexer pattern - close #35

This commit is contained in:
josh committed 2025-03-11 20:40:10 -04:00
1 parent 54bb3307cd
commit eb9d9026fc
11 files changed
+166 -30

No files matched your search

+22
View File
@@ -536,6 +536,28 @@ It also returns the `str` token now that the token is complete.
Note that the token name `str` above could have been `string` instead - the
namespace for token names is distinct from the namespace for lexer modes.
Multiple modes can be specified for a token or pattern or drop statement.
For example, if the grammar wanted to only recognize an identifier following
a `.` token and not other keywords, it could switch to an `identonly` mode
when matching a `.`
The `ident` token pattern will be matched in either the `default` or
`identonly` mode.
```
ptype char;
token abc;
token def;
default, identonly: token ident /[a-z]+/ <<
$$ = match[0];
$mode(default);
return $token(ident);
>>
token dot /\\./ <<
$mode(identonly);
>>
default, identonly: drop /\\s+/;
```
##> Specifying parser value types - the `ptype` statement
The `ptype` statement is used to define parser value type(s).