README.md
1peglint
2-------
3
4The lint utility for PEG.
5
6```
7usage: grammar_file_path [source_file_path]
8
9 options:
10 --ast: show AST tree
11 --packrat: enable packrat memoise
12 --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction
13 --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction
14 --source: source text
15 --trace: show trace messages
16```
17
18### Build peglint
19
20```
21> cd lint
22> mkdir build
23> cd build
24> cmake ..
25> make
26```
27
28### Lint grammar
29
30```
31> cat a.peg
32A <- 'hello' ^ 'world'
33
34> peglint a.peg
35a.peg:1:16: syntax error
36```
37
38```
39> cat a.peg
40A <- B
41
42> peglint a.peg
43a.peg:1:6: 'B' is not defined.
44```
45
46```
47> cat a.peg
48A <- B / C
49B <- 'b'
50C <- A
51
52> peglint a.peg
53a.peg:1:10: 'C' is left recursive.
54a.peg:3:6: 'A' is left recursive.
55```
56
57### Lint source text
58
59```
60> cat a.peg
61Additive <- Multitive '+' Additive / Multitive
62Multitive <- Primary '*' Multitive / Primary
63Primary <- '(' Additive ')' / Number
64Number <- < [0-9]+ >
65%whitespace <- [ \t\r\n]*
66
67> peglint --source "1 + a * 3" a.peg
68[commendline]:1:3: syntax error
69```
70
71### AST
72
73```
74> cat a.txt
751 + 2 * 3
76
77> peglint --ast a.peg a.txt
78+ Additive
79 + Multitive
80 + Primary
81 - Number (1)
82 + Additive
83 + Multitive
84 + Primary
85 - Number (2)
86 + Multitive
87 + Primary
88 - Number (3)
89```
90
91### AST optimazation
92
93```
94> peglint --ast --opt --source "1 + 2 * 3" a.peg
95+ Additive
96 - Multitive[Number] (1)
97 + Additive[Multitive]
98 - Primary[Number] (2)
99 - Multitive[Number] (3)
100```
101
102### Adjust AST optimazation with `no_ast_opt` instruction
103
104```
105> cat a.peg
106Additive <- Multitive '+' Additive / Multitive
107Multitive <- Primary '*' Multitive / Primary
108Primary <- '(' Additive ')' / Number { no_ast_opt }
109Number <- < [0-9]+ >
110%whitespace <- [ \t\r\n]*
111
112> peglint --ast --opt --source "1 + 2 * 3" a.peg
113+ Additive/0
114 + Multitive/1[Primary]
115 - Number (1)
116 + Additive/1[Multitive]
117 + Primary/1
118 - Number (2)
119 + Multitive/1[Primary]
120 - Number (3)
121
122> peglint --ast --opt-only --source "1 + 2 * 3" a.peg
123+ Additive/0
124 + Multitive/1
125 - Primary/1[Number] (1)
126 + Additive/1
127 + Multitive/0
128 - Primary/1[Number] (2)
129 + Multitive/1
130 - Primary/1[Number] (3)
131```
132