-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrammar.peg
229 lines (200 loc) · 11.9 KB
/
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
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
start
= f:- { JsonItem(oJson, -1) = Empty
Set $f = oJson }
(n:stmt { Set oJson = $f
JsonItem(oJson, -1) = $n }
)+ { Set $$ = $f }
stmt
= definition SEMI?
| extern SEMI?
| expr SEMI?
| SEMI
definition
= K_DEF ns p:prototype e:expr { JsonItem(oJson, "Tag") = "Def"
JsonItem(oJson, "Proto") = $p
JsonItem(oJson, "Body") = $e
Set $$ = oJson }
extern
= K_EXTERN ns p:prototype { JsonItem(oJson, "Tag") = "Extern"
JsonItem(oJson, "Proto") = $p
Set $$ = oJson }
prototype
= binopproto
| K_UNARY ns n:UNOP LPAREN p:param_list RPAREN { JsonItem(oJson, "Tag") = "UnOpProto"
JsonItem(oJson, "Oper") = $n
JsonItem(oJson, "Params") = $p
Set $$ = oJson }
| !KEYWORD n:IDENT LPAREN p:param_list RPAREN { JsonItem(oJson, "Tag") = "FuncProto"
JsonItem(oJson, "Name") = $n
JsonItem(oJson, "Params") = $p
Set $$ = oJson }
binopproto
= &{ Dim sBinOp As String
Dim lOpPrec As Long
Dim lOpAssoc As Long }
K_BINARY ns n:BINOP &{ '-- collect local binop info for use on success
sBinOp = $$text }
pr:NUMBER &{ lOpPrec = C_Lng($$text)
lOpAssoc = 1 }
LPAREN p:param_list RPAREN &{ '--- on match immediately update parser binop info
SetBinopInfo sBinOp, lOpPrec, lOpAssoc }
{ JsonItem(oJson, "Tag") = "BinOpProto"
JsonItem(oJson, "Oper") = $n
JsonItem(oJson, "Precedence") = $pr
JsonItem(oJson, "Params") = $p
Set $$ = oJson }
expr
= &{ '--- clear min precedence
m_lBinopMinPrec = 0 }
binexpr
binexpr
= &{ Dim lMinPrec As Long
Dim vOpInfo As Variant
lMinPrec = m_lBinopMinPrec }
l:unexpr (op:BINOP &{ '--- get current binop prec & assoc
vOpInfo = GetBinopInfo($$text) }
&{ vOpInfo(0) >= lMinPrec }
&{ '--- set next min precedence based on binop prec & assoc
m_lBinopMinPrec = vOpInfo(0) + vOpInfo(1) }
r:binexpr { JsonItem(oJson, "Tag") = "BinOp"
JsonItem(oJson, "Left") = $l
JsonItem(oJson, "Oper") = $op
JsonItem(oJson, "Right") = $r
JsonItem(oJson, "BinopInfo") = GetBinopInfo(C_Str($op))
Set $l = oJson }
)* { AssignVariant $$, $l }
unexpr
= op:UNOP e:unexpr { JsonItem(oJson, "Tag") = "UnOp"
JsonItem(oJson, "Oper") = $op
JsonItem(oJson, "Expr") = $e
Set $$ = oJson }
| primary
primary
= DOUBLE
| LPAREN expr RPAREN
| invokeexpr
| ifexpr
| forexpr
| varexpr
| IDENT
invokeexpr
= n:IDENT LPAREN a:arg_list RPAREN { JsonItem(oJson, "Tag") = "InvokeExpr"
JsonItem(oJson, "Func") = $n
JsonItem(oJson, "Args") = $a
Set $$ = oJson }
ifexpr
= K_IF ns c:expr K_THEN ns t:expr (K_ELSE ns e:expr)? { JsonItem(oJson, "Tag") = "IfExpr"
JsonItem(oJson, "Cond") = $c
JsonItem(oJson, "Then") = $t
JsonItem(oJson, "Else") = $e
Set $$ = oJson }
forexpr
= K_FOR ns n:IDENT EQ i:expr COMMA l:expr (COMMA s:expr)?
K_IN ns e:expr { JsonItem(oJson, "Tag") = "ForExpr"
JsonItem(oJson, "Var") = $n
JsonItem(oJson, "Init") = $i
JsonItem(oJson, "Last") = $l
JsonItem(oJson, "Step") = $s
JsonItem(oJson, "Body") = $e
Set $$ = oJson }
varexpr
= K_VAR ns v:var_list K_IN ns e:expr { JsonItem(oJson, "Tag") = "VarExpr"
JsonItem(oJson, "Vars") = $v
JsonItem(oJson, "Body") = $e
Set $$ = oJson }
param_list
= f:IDENT { JsonItem(oJson, -1) = $f
Set $f = oJson }
(COMMA? n:IDENT { Set oJson = $f
JsonItem(oJson, -1) = $n }
)* { Set $$ = $f }
arg_list
= f:expr { JsonItem(oJson, -1) = $f
Set $f = oJson }
(COMMA? n:expr { Set oJson = $f
JsonItem(oJson, -1) = $n }
)* { Set $$ = $f }
var_list
= f:var_decl { JsonItem(oJson, -1) = $f
Set $f = oJson }
(COMMA? n:var_decl { Set oJson = $f
JsonItem(oJson, -1) = $n }
)* { Set $$ = $f }
var_decl
= n:IDENT (EQ e:expr | e:EMPTY) { JsonItem(oJson, "Tag") = "VarDecl"
JsonItem(oJson, "Name") = $n
JsonItem(oJson, "Expr") = $e }
KEYWORD
= ( K_DEF
| K_EXTERN
| K_IF
| K_THEN
| K_ELSE
| K_UNARY
| K_FOR
| K_BINARY
| K_IN
| K_VAR
) !ALPHANUM
# Lexical rules
LPAREN = '(' -
RPAREN = ')' -
COMMA = ',' -
EQ = '=' -
SEMI = ';' -
IDENT = < [a-zA-Z_] ALPHANUM* > - { $$ = $$text }
BINOP = < [-+*/<>=:|&] [=]? > - { $$ = $$text }
UNOP = < [-+!~] > { $$ = $$text }
NUMBER = < [0-9]+ > - { $$ = C_Lng($$text) }
DOUBLE = < ([0-9]+ '.'? [0-9]* | [0-9]* '.' [0-9]+) > - { $$ = C_Dbl($$text) }
K_DEF = 'def'
K_EXTERN = 'extern'
K_IF = 'if'
K_THEN = 'then'
K_ELSE = 'else'
K_BINARY = 'binary'
K_UNARY = 'unary'
K_FOR = 'for'
K_IN = 'in'
K_VAR = 'var'
ALPHANUM = [a-zA-Z_0-9]
EMPTY = < '' >
# Whitespace
- = (LineComment | [ \t\r\n])*
ns = !ALPHANUM -
LineComment = '#' [^\r\n]*
@declares {
Private m_oBinopInfo As Scripting.Dictionary
Private m_lBinopMinPrec As Long
}
@prolog {
Dim oJson As Object
With ctx
}
@epilog {
End With
}
@{
Public Function GetBinopInfo(sOp As String) As Variant
If m_oBinopInfo Is Nothing Then
Set m_oBinopInfo = New Scripting.Dictionary
m_oBinopInfo.Item("<") = Array(10, 1)
m_oBinopInfo.Item("+") = Array(20, 1)
m_oBinopInfo.Item("-") = Array(20, 1)
m_oBinopInfo.Item("*") = Array(40, 1)
' m_oBinopInfo.Item("/") = Array(40, 1)
' m_oBinopInfo.Item("^") = Array(50, 0)
End If
If m_oBinopInfo.Exists(sOp) Then
GetBinopInfo = m_oBinopInfo.Item(sOp)
Else
GetBinopInfo = Array(0, 0)
End If
End Function
Public Function SetBinopInfo(sOp As String, ByVal lPrec As Long, ByVal lAssoc As Long) As Variant
If m_oBinopInfo Is Nothing Then
GetBinopInfo vbNullString
End If
m_oBinopInfo.Item(sOp) = Array(lPrec, lAssoc, "Override")
End Function
}