forked from pegex-parser/pegex-pgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pegex.pgx
193 lines (148 loc) · 4.95 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
# This is the Pegex grammar for Pegex grammars!
%grammar pegex
%version 0.2.0
grammar: meta_section rule_section (
doc_ending |
# We definitely have an error at this point; analyze it!
ERROR_rule_definition
)
meta_section: ( meta_definition | ~~ | ERROR_meta_definition )*
rule_section: ( rule_definition | ~~ )*
meta_definition: / <PERCENT> <meta_name> <BLANK>+ <meta_value> /
rule_definition: rule_start rule_group ending
rule_start: / ( <rule_name> ) <BLANK>* <COLON> ~ /
rule_group: any_group
any_group: all_group ( / ~ <PIPE> ~ / all_group )*
all_group: rule_part (~ rule_part)*
rule_part: <rule_item>1-2 % / ~~ ( <PERCENT>{1,2} ) ~~ /
rule_item:
rule_reference |
regular_expression |
bracketed_group |
whitespace_token |
error_message
rule_reference:
/
( <rule_modifier>? ) # [=!.-+]
(: # foo | <foo>
( <rule_name> ) |
(: <LANGLE> ( <rule_name> ) <RANGLE> )
)
( <rule_quantifier>? ) # [?*+] 2+ 2-3
(! <BLANK>* <COLON> ) # Avoid parsing 'foo:'
/ # as a rule reference.
regular_expression:
/ <SLASH> ( [^ <SLASH> ]* ) <SLASH> /
bracketed_group:
/ ( <group_modifier>? ) <LPAREN> ~ /
rule_group
/ ~ <RPAREN> ( <rule_quantifier>? ) /
whitespace_token:
/ ( <TILDE>+ ) /
error_message:
/ <GRAVE> ( [^ <GRAVE> <DOS> ]* ) <GRAVE> /
rule_modifier: / [ <BANG> <EQUAL> <PLUS> <DASH> <DOT> ] /
group_modifier: / <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> <WORD>* \b /
ending: / ~? (: <BREAK> ~ <SEMI>? ~ | <comment> ~ <SEMI>? ~ | <SEMI> ~ | <EOS> ) /
ws: / (: <WS> | <comment> ) /
comment: / <HASH> <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`