-
Notifications
You must be signed in to change notification settings - Fork 4
/
logics.par
103 lines (84 loc) · 4.06 KB
/
logics.par
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
// Logics: A Python-like evaluation language
// This grammar requires unicc >= 1.9 to compile, use the Makefile for building.
%!mode scanner;
%prefix "Logics";
%whitespaces @whitespace;
@whitespace ' \r\n\t'+
| "#" !'\n'* '\n'
;
@Identifier 'A-Za-z_' 'A-Za-z0-9_'* = Identifier;
@Number '0-9'+ '.' '0-9'* | '0-9'* '.' '0-9'+ | '0-9'+ = Number;
@String '"' ( '\\' . | !'\\"' )* '"'
| '\'' ( '\\' . | !'\\\'' )* '\'' = String;
expression$ : or "if" expression "else" expression = if
| or
;
or : or "or" and = or
| and
;
and : and "and" not = and
| not
;
not : "not" not = not
| cmp
;
cmp : add_sub (
"==" add_sub = eq
| ">" add_sub = gt
| ">=" add_sub = gteq
| "<" add_sub = lt
| "<=" add_sub = lteq
| ("!=" | "<>") add_sub = neq
| "in" add_sub = in
| "not" "in" add_sub = outer
)+ = cmp
| add_sub
;
add_sub : add_sub '+' mul_div = add
| add_sub '-' mul_div = sub
| mul_div
;
mul_div : mul_div '*' unary = mul
| mul_div '/' unary = div
| mul_div "//" unary = idiv
| mul_div '%' unary = mod
| pow;
pow : pow "**" unary = pow
| unary
;
unary : '+' unary = pos
| '-' unary = neg
| '~' unary = invert
| factor
;
factor : atom
| atom trailer+ = entity
| @Identifier '(' list? ')' = call
;
opt_expression : expression
| = None
;
trailer : '[' expression ']' = index
| '[' opt_expression ':' opt_expression ']' = slice
| '.' @Identifier = attr
;
atom : ""True""
| ""False""
| ""None""
| "$" = vars
| @Number
| @Identifier = load
| @String
| @String+ = strings
| '[' expression "for" @Identifier "in"
or ( "if" expression )? ']' = comprehension
| '[' list ']'
| '(' expression ',' ')' = list
| '(' expression ',' internal_list ','? ')' = list
| '(' expression ')'
;
internal_list : expression
| internal_list ',' expression
;
list := internal_list ','?
;