cpp-peglib/lint/README.md

134 lines
2.4 KiB
Markdown
Raw Normal View History

2015-11-30 04:16:51 +00:00
peglint
-------
The lint utility for PEG.
```
2020-05-26 02:48:42 +00:00
usage: grammar_file_path [source_file_path]
options:
--ast: show AST tree
2021-01-24 03:58:23 +00:00
--packrat: enable packrat memoise
--opt, --opt-all: optimize all AST nodes except nodes selected with `no_ast_opt` instruction
--opt-only: optimize only AST nodes selected with `no_ast_opt` instruction
2020-05-26 02:48:42 +00:00
--source: source text
2022-05-27 04:06:27 +00:00
--trace: show concise trace messages
2022-06-03 02:15:09 +00:00
--profile: show profile report
2022-06-03 22:42:44 +00:00
--verbose: verbose output for trace and profile
2020-05-26 02:48:42 +00:00
```
### Build peglint
```
> cd lint
> mkdir build
> cd build
> cmake ..
> make
```
### Lint grammar
```
> cat a.peg
A <- 'hello' ^ 'world'
> peglint a.peg
2021-01-16 22:34:09 +00:00
a.peg:1:16: syntax error
2020-05-26 02:48:42 +00:00
```
```
> cat a.peg
A <- B
> peglint a.peg
a.peg:1:6: 'B' is not defined.
```
```
> cat a.peg
A <- B / C
B <- 'b'
C <- A
> peglint a.peg
a.peg:1:10: 'C' is left recursive.
a.peg:3:6: 'A' is left recursive.
```
### Lint source text
```
> cat a.peg
Additive <- Multiplicative '+' Additive / Multiplicative
Multiplicative <- Primary '*' Multiplicative / Primary
2020-05-26 02:48:42 +00:00
Primary <- '(' Additive ')' / Number
Number <- < [0-9]+ >
%whitespace <- [ \t\r\n]*
> peglint --source "1 + a * 3" a.peg
[commandline]:1:3: syntax error
2020-05-26 02:48:42 +00:00
```
2021-01-22 02:16:47 +00:00
### AST
2020-05-26 02:48:42 +00:00
```
> cat a.txt
1 + 2 * 3
> peglint --ast a.peg a.txt
+ Additive
+ Multiplicative
2020-05-26 02:48:42 +00:00
+ Primary
- Number (1)
+ Additive
+ Multiplicative
2020-05-26 02:48:42 +00:00
+ Primary
- Number (2)
+ Multiplicative
2020-05-26 02:48:42 +00:00
+ Primary
- Number (3)
```
### AST optimization
2021-01-22 02:16:47 +00:00
2020-05-26 02:48:42 +00:00
```
> peglint --ast --opt --source "1 + 2 * 3" a.peg
+ Additive
- Multiplicative[Number] (1)
+ Additive[Multiplicative]
2020-05-26 02:48:42 +00:00
- Primary[Number] (2)
- Multiplicative[Number] (3)
2020-05-26 02:48:42 +00:00
```
### Adjust AST optimization with `no_ast_opt` instruction
2021-01-22 02:16:47 +00:00
2020-05-26 02:48:42 +00:00
```
2021-01-22 02:16:47 +00:00
> cat a.peg
Additive <- Multiplicative '+' Additive / Multiplicative
Multiplicative <- Primary '*' Multiplicative / Primary
2021-01-22 02:16:47 +00:00
Primary <- '(' Additive ')' / Number { no_ast_opt }
Number <- < [0-9]+ >
%whitespace <- [ \t\r\n]*
> peglint --ast --opt --source "1 + 2 * 3" a.peg
2020-05-26 02:48:42 +00:00
+ Additive/0
+ Multiplicative/1[Primary]
2020-05-26 02:48:42 +00:00
- Number (1)
+ Additive/1[Multiplicative]
2020-05-26 02:48:42 +00:00
+ Primary/1
- Number (2)
+ Multiplicative/1[Primary]
2020-05-26 02:48:42 +00:00
- Number (3)
2021-01-22 02:16:47 +00:00
> peglint --ast --opt-only --source "1 + 2 * 3" a.peg
2020-05-26 02:48:42 +00:00
+ Additive/0
+ Multiplicative/1
2020-05-26 02:48:42 +00:00
- Primary/1[Number] (1)
+ Additive/1
+ Multiplicative/0
2020-05-26 02:48:42 +00:00
- Primary/1[Number] (2)
+ Multiplicative/1
2020-05-26 02:48:42 +00:00
- Primary/1[Number] (3)
2015-11-30 04:16:51 +00:00
```