-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statement.g
99 lines (87 loc) · 3.04 KB
/
Statement.g
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
grammar Statement;
options {
language = Ruby;
output = AST;
}
@token::members {
def value
return nil if text == 'null'
return text.to_f unless text =~ /^(\'|\")/
text[1..-2]
end
}
program returns [value]
: s=statement { $value = $s.value }
;
statement returns [value]
: 'if' e=multi_expression 'then' v=literal s=next_statement?
{
if $e.value
$value = $v.value
else
$value = $s.value
end
}
;
next_statement returns [value]
: 'elsif' e=multi_expression 'then' v=literal s=next_statement?
{
if $e.value
$value = $v.value
else
$value = $s.value
end
}
| 'else' v=literal
{
$value = $v.value
}
;
multi_expression returns [value]
: 'and(' l=single_expression ',' r=multi_expression ')' { $value = $l.value && $r.value }
| 'or(' l=single_expression ',' r=multi_expression ')' { $value = $l.value || $r.value }
| e=single_expression { $value = $e.value }
;
single_expression returns [value]
: 'equal(' l=literal ',' r=literal ')' { $value = $l.value == $r.value }
| 'greaterThan(' l=literal ',' r=literal ')' { $value = $l.value.nil? == false && $r.value.nil? == false && $l.value.to_f > $r.value.to_f }
| 'greaterThanEqual('l=literal ',' r=literal ')' { $value = $l.value.nil? == false && $r.value.nil? == false && $l.value.to_f >= $r.value.to_f }
| 'lessThan(' l=literal ',' r=literal ')' { $value = $l.value.nil? == false && $r.value.nil? == false && $l.value.to_f < $r.value.to_f }
| 'lessThanEqual(' l=literal ',' r=literal ')' { $value = $l.value.nil? == false && $r.value.nil? == false && $l.value.to_f <= $r.value.to_f }
| 'between(' l=literal ',' min=literal ',' max=literal ')' { $value = $l.value.nil? == false && $min.value.nil? == false && $max.value.nil? == false && $l.value.to_f >= $min.value.to_f && $l.value.to_f <= $max.value.to_f }
| 'not(' e=single_expression ')'
{
if $e.value
$value = false
else
$value = true
end
}
| 'in(' v=literal ',' l=list ')'
{
$value = $l.value.include?($v.value)
}
| e=literal { $value = $e.value }
;
list returns [value]
: '[' l=literals']' { $value = $l.value }
;
literals returns [value]
: l=literal ',' rest=literals { $value = $rest.value.push($l.value) }
| l=literal { $value = [$l.value] }
;
literal returns [value]
: s=STRING { $value = $s.value }
| v=VARIABLE { $value = \$argument_mapper.get_value($v.text) }
| n=NUMBER { $value = $n.value }
| i=INT { $value = $i.value }
;
NEWLINE : '\n' { $channel = HIDDEN } ;
SPACE : ' '+ { $channel = HIDDEN } ;
STRING : '\'' ~('\'')* '\''
| '\"' ~('\"')* '\"'
;
NUMBER : '-'? DIGIT+ '.' DIGIT+ ;
INT : '-'? DIGIT+ ;
VARIABLE : '$' DIGIT+ ;
fragment DIGIT : ('0' .. '9') ;