forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.peggy
170 lines (142 loc) · 3.37 KB
/
grammar.peggy
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
// tinymath parsing grammar
{
function simpleLocation (location) {
// Returns an object representing the position of the function within the expression,
// demarcated by the position of its first character and last character. We calculate these values
// using the offset because the expression could span multiple lines, and we don't want to deal
// with column and line values.
return {
min: location.start.offset,
max: location.end.offset
}
}
}
start
= Expression
// characters
_ "whitespace"
= [ \t\n\r]*
Space
= [ ]
Quote
= [\"\']
StartChar
= [A-Za-z_@.\[\]-]
ValidChar
= [0-9A-Za-z._@\[\]-]
// literals and variables
Literal "literal"
= _ literal:(Number / Variable) _ {
return literal;
}
// Quoted variables are interpreted as strings
// but unquoted variables are more restrictive
Variable
= _ [\'] chars:(ValidChar / Space / [\"])* [\'] _ {
return {
type: 'variable',
value: chars.join(''),
location: simpleLocation(location()),
text: text()
};
}
/ _ [\"] chars:(ValidChar / Space / [\'])* [\"] _ {
return {
type: 'variable',
value: chars.join(''),
location: simpleLocation(location()),
text: text()
};
}
/ _ rest:ValidChar+ _ {
return {
type: 'variable',
value: rest.join(''),
location: simpleLocation(location()),
text: text()
};
}
// expressions
Expression
= AddSubtract
AddSubtract
= _ left:MultiplyDivide rest:(('+' / '-') MultiplyDivide)+ _ {
const topLevel = rest.reduce((acc, curr) => ({
type: 'function',
name: curr[0] === '+' ? 'add' : 'subtract',
args: [acc, curr[1]],
}), left);
if (typeof topLevel === 'object') {
topLevel.location = simpleLocation(location());
topLevel.text = text();
}
return topLevel;
}
/ MultiplyDivide
MultiplyDivide
= _ left:Factor rest:(('*' / '/') Factor)* _ {
const topLevel = rest.reduce((acc, curr) => ({
type: 'function',
name: curr[0] === '*' ? 'multiply' : 'divide',
args: [acc, curr[1]],
}), left);
if (typeof topLevel === 'object') {
topLevel.location = simpleLocation(location());
topLevel.text = text();
}
return topLevel;
}
/ Factor
Factor
= Group
/ Function
/ Literal
Group
= _ '(' _ expr:Expression _ ')' _ {
return expr
}
Argument_List "arguments"
= first:Argument rest:(_ ',' _ arg:Argument {return arg})* _ ','? {
return [first].concat(rest);
}
String
= '"' chars:("\\\"" { return "\""; } / [^"])* '"' { return chars.join(''); }
/ "'" chars:("\\\'" { return "\'"; } / [^'])* "'" { return chars.join(''); }
/ chars:(ValidChar)+ { return chars.join(''); }
Argument
= name:[a-zA-Z_]+ _ '=' _ value:(Number / String) _ {
return {
type: 'namedArgument',
name: name.join(''),
value: value,
location: simpleLocation(location()),
text: text()
};
}
/ arg:Expression
Function "function"
= _ name:[a-zA-Z_-]+ '(' _ args:Argument_List? _ ')' _ {
return {
type: 'function',
name: name.join(''),
args: args || [],
location: simpleLocation(location()),
text: text()
};
}
// Numbers. Lol.
Number "number"
= '-'? Integer Fraction? Exp? {
return parseFloat(text());
}
E
= [eE]
Exp "exponent"
= E '-'? Digit+
Fraction
= '.' Digit+
Integer
= '0'
/ ([1-9] Digit*)
Digit
= [0-9]