1
0
mirror of https://github.com/fluencelabs/lalrpop synced 2025-03-31 23:41:03 +00:00

doc: Document lexer skipping

This commit is contained in:
Markus Westerlind 2020-03-02 15:44:04 +01:00
parent ee2f7060e9
commit 4447616283
2 changed files with 34 additions and 5 deletions
doc
calculator/src
src/lexer_tutorial

@ -2,6 +2,21 @@ use std::str::FromStr;
grammar; grammar;
match {
"+",
"-",
"*",
"/",
"(",
")",
r"[0-9]+",
// Skip whitespace and comments
r"\s*" => { },
r"//[^\n\r]*[\n\r]*" => { }, // `// comment`
r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { }, // `/* comment */`
}
pub Expr: i32 = { pub Expr: i32 = {
<l:Expr> "+" <r:Factor> => l + r, <l:Expr> "+" <r:Factor> => l + r,
<l:Expr> "-" <r:Factor> => l - r, <l:Expr> "-" <r:Factor> => l - r,

@ -321,6 +321,20 @@ match {
And now any reference in your grammar to `"BEGIN"` will actually match And now any reference in your grammar to `"BEGIN"` will actually match
any capitalization. any capitalization.
#### Customizing skipping between tokens
If we want to support comments we will need to skip more than just whitespace in our lexer.
To this end `ignore patterns` can be specified.
```
match {
r"\s*" => { }, // The default whitespace skipping is disabled an `ignore pattern` is specified
r"//[^\n\r]*[\n\r]*" => { }, // Skip `// comments`
r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { }, // Skip `/* comments */`
}
```
[lexer tutorial]: index.md [lexer tutorial]: index.md
[calculator2b]: ../../calculator/src/calculator2b.lalrpop [calculator2b]: ../../calculator/src/calculator2b.lalrpop
[calculator3]: ../../calculator/src/calculator3.lalrpop [calculator3]: ../../calculator/src/calculator3.lalrpop