-
Notifications
You must be signed in to change notification settings - Fork 4
/
pegex.pgx
248 lines (196 loc) · 5.29 KB
/
pegex.pgx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# This is the Pegex grammar for Pegex grammars!
%grammar pegex
%version 0.2.0
%include pegex-atoms
grammar:
meta-section
rule-section
( doc-ending | ERROR-rule-definition )
meta-section: ( meta-definition | + | ERROR-meta-definition )*
rule-section: ( rule-definition | + )*
meta-definition: / '%' meta-name BLANK+ meta-value /
rule-definition: rule-start rule-group ending
rule-start: / ( rule-name ) BLANK* ':' -/
rule-group: any-group
any-group: /- '|'? -/ all-group ( /- '|' -/ all-group )*
all-group: rule-part (- rule-part)*
rule-part: (rule-item)1-2 % /+ ( '%'{1,2} ) +/
rule-item:
| bracketed-group
| whitespace-token
| rule-reference
| quoted-regex
| regular-expression
| error-message
rule-reference:
/
( rule-modifier? ) # [=!.-+]
(: # foo | <foo>
( rule-name ) |
(: '<' ( rule-name ) '>' )
)
( rule-quantifier? ) # [?*+] 2+ 2-3
(! BLANK* ':' ) # Avoid parsing 'foo:'
/ # as a rule reference.
quoted-regex:
/ TICK ( [^ TICK ]* ) TICK /
regular-expression:
/( group-modifier? )/
'/'
whitespace-start?
(
| whitespace-must
| whitespace-maybe
| quoted-regex
| regex-rule-reference
| +
| regex-raw
)*
'/'
whitespace-start: / ([ PLUS DASH]) (! [ DASH TILDE ]) /
whitespace-must: /+ (: PLUS | DASH DASH ) (= [ SPACE SLASH CR NL ]) /
whitespace-maybe: /- DASH (= [ SPACE SLASH CR NL ]) /
regex-rule-reference:
/
(:
+ ( rule-name ) |
(: '<' ( rule-name ) '>' )
)
(! BLANK* ':' )
/
regex-raw:
/
(
'(?'? '<' |
(?:[^ WS SLASH TICK LANGLE ])+
)
/
bracketed-group:
/ ( group-modifier? ) '(' -/
rule-group
/- ')' ( rule-quantifier? ) /
whitespace-token:
/ ( (: PLUS | DASH | DASH DASH | TILDE | TILDE TILDE ) ) (= + )/
error-message:
/ '`' ( [^ '`' DOS ]* ) '`' /
rule-modifier: / [ BANG EQUAL PLUS DASH DOT ] /
group-modifier: / [ DASH DOT ] /
rule-quantifier:
/ (:
[ STAR PLUS QMARK ] |
DIGIT+ (: DASH DIGIT+ | PLUS)?
) /
meta-name:
/ ( 'grammar' | 'extends' | 'include' | 'version' ) /
meta-value:
/
BLANK*
( [^ SEMI BREAK ]*? )
BLANK*
ending
/
rule_name: /
(:
ALPHA ALNUM* (:[ DASH UNDER ] ALNUM+)* |
DASH+ |
UNDER+
)
(= [^ WORD DASH ])
/
ending: /
-
(:
BREAK - SEMI? - |
comment - SEMI? - |
SEMI - |
EOS
)
/
ws: / (: WS | comment ) /
comment: / '#' ANY* (: BREAK | EOS ) /
###
# Pegex common error recognition and reporting:
###
doc-ending: /- EOS /
illegal-non-modifier-char: / [^
WORD LPAREN RPAREN LANGLE SLASH TILDE PIPE GRAVE WS
] /
illegal-non-quantifier-char: / [^
WORD LPAREN RPAREN LANGLE SLASH TILDE PIPE GRAVE WS
STAR PLUS QMARK BANG EQUAL PLUS DASH DOT COLON SEMI
] /
ERROR-meta-definition:
/(= PERCENT WORD+ )/
`Illegal meta rule`
# Much of this is essentially a duplicate of the above rules, except with added
# error checking
ERROR-rule-definition: ERROR-rule-start ERROR-rule-group ( ending | `Rule ending syntax error` )
ERROR-rule-group: ERROR-any-group | ERROR-all-group
ERROR-all-group: ERROR-rule-part+ % -
ERROR-any-group: (ERROR-all-group)2+ % /- PIPE -/
ERROR-rule-part: (ERROR-rule-item)1-2 % /+ ( PERCENT{1,2} ) +/
ERROR-rule-start: / ( rule-name ) BLANK* COLON -/ | `Rule header syntax error`
ERROR-rule-item:
rule-item |
ERROR-rule-reference |
ERROR-regular-expression |
ERROR-bracketed-group |
ERROR-error-message
# Errors - rule-reference
ERROR-rule-reference:
/(= rule-modifier? LANGLE rule-name (! RANGLE ) )/
`Missing > in rule reference`
|
/(= rule-modifier? rule-name RANGLE )/
`Missing < in rule reference`
|
/(=
rule-modifier? (: rule-name | LANGLE rule-name RANGLE )
illegal-non-quantifier-char
)/
`Illegal character in rule quantifier`
|
/(= rule-modifier? rule-name DASH )/
`Unprotected rule name with numeric quantifier; please use <rule>#-# syntax!`
|
!rule-modifier
/(=
illegal-non-modifier-char
(: rule-name | LANGLE rule-name RANGLE )
rule-quantifier? # [?*+] 2+ 2-3
(! BLANK* COLON ) # Avoid parsing 'foo:'
)/ # as a rule reference.
`Illegal rule modifier (must be [=!.-+]?)`
# Errors - regular-expression
ERROR-regular-expression:
/(= SLASH ( [^ SLASH ]* ) doc-ending )/
`Runaway regular expression; no ending slash at EOF`
# Errors - bracketed-group
ERROR-bracketed-group:
/(! group-modifier) (= illegal-non-modifier-char LPAREN )/
`Illegal group rule modifier (can only use .)`
|
/ ( group-modifier? ) LPAREN -/
rule-group
(
=doc-ending
`Runaway rule group; no ending parens at EOF`
|
/ (= - RPAREN illegal-non-quantifier-char ) /
`Illegal character in group rule quantifier`
)
# Errors - error-message
ERROR-error-message:
/(= GRAVE [^ GRAVE DOS ]* [ DOS ] [^ GRAVE ]* GRAVE )/
`Multi-line error messages not allowed!`
|
/(= GRAVE [^ GRAVE ]* doc-ending )/
`Runaway error message; no ending grave at EOF`
# Errors - separation
ERROR-separation:
/(= - PERCENT{3} )/
`Leading separator form (BOK) no longer supported`
|
/(= - PERCENT{1,2} [^ WS ] )/
`Illegal characters in separator indicator`
# vim: set lisp: