-
Notifications
You must be signed in to change notification settings - Fork 54
/
meta_grammar.peg
109 lines (64 loc) · 3.35 KB
/
meta_grammar.peg
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
grammar Canopy.MetaGrammar
# ==============================================================================
grammar <- _* grammar_name (_* rule)+ _* %grammar
grammar_name <- `grammar` ":"? _+ id:object_identifier
rule <- identifier assignment parsing_expression %rule
assignment <- _+ "<-" _+
# ==============================================================================
_ <- [ \t\n\r] / comment
comment <- "#" [^\n]*
object_identifier <- identifier ("." identifier)*
identifier <- [a-zA-Z_] [a-zA-Z0-9_]*
integer <- [1-9] [0-9]*
# ==============================================================================
parsing_expression <- choice
/ choice_part
choice_part <- action_expression
/ typed_expression
/ sequence
/ sequence_element
sequence_element <- predicated_atom
/ repeated_atom
/ maybe_atom
/ atom
atom <- reference
/ terminal
/ paren_expression
terminal <- literal_string
/ ci_string
/ char_class
/ any_char
# ==============================================================================
action_expression <- actionable _+ action_tag %action
actionable <- sequence
/ repeated_atom
/ maybe_atom
/ terminal
/ "(" _* actionable _* ")" %paren_expr
action_tag <- "%" id:identifier
# ==============================================================================
typed_expression <- typable _+ type_tag %extension
typable <- sequence
/ sequence_element
type_tag <- "<" id:object_identifier ">"
# ==============================================================================
choice <- choice_part (_* "/" _* expr:choice_part)+ %choice
# ==============================================================================
sequence <- sequence_part (_+ expr:sequence_part)+ %sequence
sequence_part <- mute? label? sequence_element %sequence_part
mute <- "@"
label <- id:identifier ":"
# ==============================================================================
repeated_atom <- atom _* quantifier %repeat
quantifier <- "*" / "+" / "{" _* numeric_quantifier _* "}"
numeric_quantifier <- min:integer max:(_* "," _* n:integer?)?
# ==============================================================================
paren_expression <- "(" _* parsing_expression _* ")" %paren_expr
predicated_atom <- ("&" / "!") _* atom %predicate
maybe_atom <- atom _* "?" %maybe
reference <- identifier !assignment %reference
literal_string <- '"' ("\\" . / [^"])* '"' %string
/ "'" ("\\" . / [^'])* "'" %string
ci_string <- "`" ("\\" . / [^`])* "`" %ci_string
char_class <- "[" "^"? ("\\" . / [^\]])+ "]" %char_class
any_char <- "." %any_char