-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdLola_grammar.peg
376 lines (296 loc) · 11.8 KB
/
dLola_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{
package dLola
import(
"strconv"
)
}
Input <- _? t:Topo m:(MonitorDeclaration*) EOF {
return NewTopoMonitorDecls(t,m), nil
}
Topo <- CLIQUE {return Identifier{string(c.text)}, nil}
/ LINE {return Identifier{string(c.text)}, nil}
/ RINGSHORT {return Identifier{string(c.text)}, nil}
/ RING {return Identifier{string(c.text)}, nil}
/ STAR {return Identifier{string(c.text)}, nil}
MonitorDeclaration <- _? AT n:NumLiteral "{" d:Decls "}" _? {return NewMonitorDecl(n,d),nil}
Decls <- _? d:(Declaration*) {
// returns a []interface{} where all elements are
// Const, Input, Output, Ticks or Definitions
return d,nil
}
Declaration <- i:InputStreamDecl { return i,nil }
/ k:Const { return k,nil }
// / o:OutputStreamDecl { return o,nil }
/ d:OutputDefinition { return d,nil }
// com:Comment {return com, nil }
Const <- CONST t:Type n:Name ASSIGN e:Expr {
// if !checkGround(e) { return nil,err }
return NewConstDecl(n,t,e,c.pos),nil //c.pos is a struct defined in parser.go (once generated) containing line, col and offset
}
InputStreamDecl <- INPUT t:Type n:Name le:LazyEval? {
return NewInputDecl(n,t, le, c.pos),nil
}
OutputDefinition <- do:DefOut t:Type n:Name le:LazyEval? ASSIGN e:Block {
//fmt.Printf("do: %v\n", do)
return NewOutputDefinition(true,n,t,le, e, c.pos),nil
}
DefOut <- DEFINE / OUTPUT{
//return string(c.text) == "output" ,nil TODO: make this work, so that DEFINE returns false and OUTPUT true
return true,nil
}
LazyEval <- LAZY { return false, nil }
/ EVAL { return true, nil }
/*OutputStreamDecl <- OUTPUT t:Type n:Name {
return NewOutputDecl(n,t),nil
}*/
Block <- LET n:Name p:(Name*) ASSIGN e:Expr IN b:Block { return NewLetExpr(n,p,e,b),nil }
/ e:Expr { return e,nil }
Expr <- IF p:BooleanExpr THEN a:Expr ELSE b:Expr { return NewIfThenElseExpr(p,a,b),nil }
/ b:BooleanExpr { return b,nil }
/ n:NumExpr{ return n, nil }
/ s:StrExpr{return s, nil}
// BooleanExpressions
// Big OR of ANDS -> ands will be evaluated first and then ors
BooleanExpr <- a:Ands b:(OrExpr*) {
preds := ToSlice(b)
if len(preds)==0 {
//fmt.Printf("boolean expr without or\n")
v, err := getBoolExpr(a)
//fmt.Printf("Error %s", err)
if err == nil{
//fmt.Printf("boolean NO error: %s", v.Sprint())
return BoolExprToExpr(v),nil //old: a,nil lm:BoolExprToExpr(a), nil
} else {
//fmt.Printf("boolean error")
return nil, errors.New("BoolExpr: Ands expression is not boolean")
}
}
//fmt.Printf("boolean expr with ors")
return BoolExprToExpr(NewOrPredicate(a,b)),nil
}
OrExpr <- OR p:Ands { return p,nil }
Ands <- a:NegExpr b:(AndExpr*) {
preds := ToSlice(b)
if len(preds)==0 { return a,nil }
return BoolExprToExpr(NewAndPredicate(a,b)),nil
}
AndExpr <- AND p:NegExpr { return p,nil }
NegExpr <- NOT p:NegExpr { return NewBooleanExpr(NewNotPredicate(p)),nil }
/ p:Primary { return p,nil }
Primary <- '(' _? p:BooleanExpr ')' _? { return p,nil }
/ n:NumComparison { /*fmt.Printf("NumComparison \n");*/return NewNumComparisonPredicate(n),nil } //old: BooleanExprToExpr(NewNumComparisonPredicate(n)),nil
/ s:StrComparison {return NewStrComparisonPredicate(s),nil} //old:BooleanExprToExpr(NewStr...)
/ a:AtomicBool {return a, nil} // old:return a,nil testing:getBoolExpr(a)
NumComparison <- a:NumExpr LESSEQ b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumLessEq(na,nb),nil }
/ a:NumExpr LESS b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumLess(na,nb),nil }
/ a:NumExpr EQ b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumEq(na,nb),nil }
/ a:NumExpr GREATEQ b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumGreaterEq(na,nb),nil }
/ a:NumExpr GREATER b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumGreater(na,nb),nil }
/ a:NumExpr NEQ b:NumExpr { na,_:= getNumExpr(a)
nb,_:= getNumExpr(b)
return NewNumNotEq(na,nb),nil }
StrComparison <- a:StrExpr SEQ b:StrExpr { na,_:= getStrExpr(a)
nb,_:= getStrExpr(b)
return NewStrEqExpr(na,nb),nil }
/*Numeric expressions*/
NumExpr <- a:Mults b:(Plus*) {
//fmt.Printf("aa")
exprs := ToSlice(b) // common.ToSlice(b)
if len(exprs)==0 {
//fmt.Printf("NumExpr of an Atom: %s\n", a)
v,err := getNumExpr(a)
if err == nil {
return NumExprToExpr(v),nil //old: NumExprToExpr(a.(NumExpr)),nil lm: NumExprToExpr(getNumExpr(a))
} else {
return nil, errors.New("NumExpr: Mults expression is not numeric\n")
}
} else {
return Flatten(a,b),nil // common.Flatten(a,b)
}
}
Plus <- PLUS e:Mults { return NewRightPlusExpr(e),nil }
/ MINUS e:Mults { return NewRightMinusExpr(e),nil }
Mults <- a:BasicNumExpr b:(MoreMults*) {
exprs := ToSlice(b) // common.ToSlice(b)
if len(exprs)==0 { return a,nil }
return Flatten(a,b),nil // common.Flatten(a,b)
}
MoreMults <- MULT e:BasicNumExpr { v, err := getNumExpr(e)
if err == nil{
return NewRightMultExpr(v),nil // common. NewRightMultExpr(e)
} else {
return nil, errors.New("MoreMults: BasicNumExpr is not numeric")
}}
/ DIV e:BasicNumExpr { v, err := getNumExpr(e)
if err == nil{
return NewRightDivExpr(v),nil // common. NewRightMultExpr(e)
} else {
return nil, errors.New("MoreMults: BasicNumExpr is not numeric")
}}
BasicNumExpr <- a:AtomicNum { return a,nil } //return a, nil lm:added getNumExpr(a)
/ '(' _? n:NumExpr ')' _? { return n,nil } //old: n lm: getNumExpr(n)
StrExpr <- a:BasicStrExpr b:(MoreStr*){
exprs := ToSlice(b) // common.ToSlice(b)
if len(exprs)==0 {
v,err := getStrExpr(a)
if err == nil {
return StrExprToExpr(v),nil //old: NumExprToExpr(a.(NumExpr)),nil lm: NumExprToExpr(getNumExpr(a))
} else {
return nil, errors.New("StrExpr: Str expression is not string\n")
}
} else {
return FlattenStr(a,b),nil // common.Flatten(a,b)
}
}
MoreStr <- STRCONCAT e:BasicStrExpr { v, err := getStrExpr(e)
if err == nil{
return NewRightStrConcatExpr(v),nil
} else {
return nil, errors.New("MoreStr: BasicStrExpr is not string")
}}
BasicStrExpr <- '(' _? s:StrExpr ')' _? { return s,nil }
/ a:AtomicStr { return a,nil }
/*Atoms used for each theory(type)*/
AtomicBool <- co:ConstBool { return co, nil}
/ e:StreamExpr !OtherOp { return NewStreamOffsetExpr(e),nil } //IMPORTANT: !NumOp will make sure that this expression is only matched when not followed by a Numeric Operation, in order to correctly parse numeric expressions through NumExpr
/ f:FuncApp {return f, nil}
AtomicNum <- e:StreamExpr { return NewStreamOffsetExpr(e),nil }
/ co:ConstNum { return co, nil}
/ f:FuncApp {return f, nil}
AtomicStr <- e:StreamExpr { return NewStreamOffsetExpr(e),nil }
/ co:ConstStr { return co, nil}
/ f:FuncApp {return f, nil}
/*Streams are generic*/
StreamExpr <- n:Name "[" _? t:OffsetExpr "|" _? co:Constant "]" _? { return NewStreamFetchExpr(n,t,co, c.pos),nil }
/ n:Name { return NewStreamFetchExpr(n,NewIntLiteralExpr(0, c.pos), nil, c.pos), nil }
OffsetExpr <- n:NumLiteral _? {return n, nil}
/*Every constant of every type, only accesible from StreamExpr as a default value!!!*/
Constant <- b:ConstBool {return b,nil}
/ n:ConstNum {return n,nil}
/ s:ConstStr {return s,nil}
// / k:ConstExpr { return NewConstExpr(k),nil } // constant uses are treated syntactically as stream names
/*Constants for each type*/
ConstBool <- TRUE _? { return NewTruePredicate(c.pos),nil } //old:TrueExpr{c.pos},nil lm:
/ FALSE _? { return NewFalsePredicate(c.pos),nil }
ConstNum <- l:NumLiteral _? { return l,nil } //old:NewNumericExpr(l),nil this returns an NumericExpr which is an Expr, but in the general case the Numliteral will be in a NumExpr
ConstStr <- l:QuotedString _? { return l,nil }
ConstExpr <- n:Name { return n,nil }
FuncApp <- n:Name "(" p:(ActualParams) ")"
ActualParams <- e:Expr "," a:ActualParams
/ e:Expr
NumLiteral <- p:(('-')?[0-9]+('.'[0-9]+)?) {
if strings.Contains(string(c.text), ".") {
// x,err := strconv.ParseFloat(p.(string),32)
x,err := strconv.ParseFloat(string(c.text),32)
return NewFloatLiteralExpr(x, c.pos), err
} else {
x,err := strconv.ParseInt(string(c.text),10,32)
return NewIntLiteralExpr(int(x), c.pos), err
}
}
Type <- NUM { return NumT, nil }
/ BOOL { return BoolT, nil }
/ STRING { return StringT,nil}
Name <- n:Identifier _? { return n.(Identifier),nil }
QuotedString <- ('"'(([^'"''\\']*('\\'.[^'"''\\']*)*))'"') {
str := string(c.text)
inner := str[1:len(str)-1]
return NewStringLiteralExpr(inner, c.pos),nil //old:NewQuotedString lm:
}
JSONPath <- ([a-zA-Z0-9]+('.'[a-zA-Z0-9]+)*) {
return NewPathName(string(c.text)),nil
}
Identifier <- ([a-zA-Z]['_''-''@'a-zA-Z0-9]*) {
// IMPORTANT: cannot have '.' to distinguish from "e.path" "e.strcmp"
return Identifier{string(c.text)}, nil
}
String <- ([a-zA-Z0-9'#''_''-''@']+)
Alphanum <- ([a-zA-Z0-9]+) { return Alphanum{string(c.text)},nil }
Tag "tag" <- ( '#'[a-zA-Z](['_''-''.'a-zA-Z0-9])* ) { return Tag{string(c.text)},nil }
_ "whitespace" <- [ \t\n\r]+ _?
/ Comment+ _?
//EOL <- _? Comment? ("\r\n" / "\n\r" / "\r" / "\n" / EOF)
EOL <- ("\r\n" / "\n\r" / "\r" / "\n")
Comment <- "/*" [^*/]* "*/" { return "", nil}
/ "//" [ a-zA-Z0-9'#''_''-''@'',']* [\n\r] {return "",nil }
//IMPORTANT: these operations are meant to be used exclusively in !X expressions so the rule is only matched if not followed by X(see AtomicBool).
OtherOp <- NumOp
/ StrOp
NumOp <- PLUS
/ MINUS
/ MULT
/ DIV
StrOp <- STRCONCAT
EOF <- !.
//
// Striver Keywords
//
//DOT_TICKS <- ".ticks" _?
//DELAY <- "delay" _?
OUTPUT <- "output" _?
INPUT <- "input" _?
//TICKS <- "ticks" _?
CONST <- "const" _?
DEFINE <- "define" _?
LET <- "let" _?
IN <- "in" _?
//UNION <- "U" _?
CONST <- "const" _
//OUTSIDE <- "outside" _?
//NOTICK <- "notick" _?
//PREV <- "<<" _?
//PREVEQ <- "<~" _?
//SUCC <- ">>" _?
//SUCCEQ <- "~>" _?
INT <- "int" _
NUM <- "num" _
BOOL <- "bool" _
STRING <- "string" _
IF <- "if" _
THEN <- "then" _
ELSE <- "else" _
ASSIGN <- "=" _?
TRUE <- "true" _?
FALSE <- "false" _?
AND <- "and" _? //"/\\" _?
OR <- "or" _? //"\\/" _?
NOT <- "not" _? //"~" _?
PLUS <- '+' _?
MINUS <- '-' _?
MULT <- '*' _?
DIV <- '/' _?
LESSEQ <- "<=" _?
LESS <- "<" _?
GREATEQ <- ">=" _?
GREATER <- ">" _?
EQ <- "==" _?
NEQ <- "!=" _?
SEQ <- "sEq" _?
LAZY <- "lazy" _?
EVAL <- "eval" _?
STRCONCAT <- "sConcat" _?
AT <- "@"
CLIQUE <- "clique"
LINE <- "line"
RINGSHORT <- "ringshort"
RING <- "ring"
STAR <- "star"
/*Function <- n:Name '(' a:Args ')' { //TODO: return proper data
return n, nil
}
Args <- e:Expr "," a:Args {
v := a.([]interface{})
return append(v,e),nil
}
/ e:Expr {return []interface{}{e}, nil}
*/