-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuildParser.js
163 lines (155 loc) · 5.31 KB
/
buildParser.js
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
const { Parser } = require("jison");
const binaryExpression =
"$$ = {type: 'BINARY_EXPRESSION', left: $1, right: $3, operator: $2, loc: @$}";
const unaryExpression =
"$$ = {type: 'UNARY_EXPRESSION', value: $2, operator: $1, loc: @$}";
const grammar = {
comment: "EEL Parser",
lex: {
rules: [
["\\s+", "/* skip whitespace */"],
["[0-9]+", "return 'DIGITS_TOKEN'"],
["(==|!=|<=|>=|<|>)", "return 'COMPARISON_TOKEN'"],
["[+\\-*/%]?=", "return 'ASSIGNMENT_OPERATOR_TOKEN'"],
["(\\&\\&)|\\|\\|", "return 'LOGICAL_OPERATOR_TOKEN'"],
// https://github.com/justinfrankel/WDL/blob/63943fbac273b847b733aceecdb16703679967b9/WDL/eel2/eel2.l#L93
["[a-zA-Z_][a-zA-Z0-9._]*", "return 'IDENTIFIER_TOKEN'"],
["$", "return 'EOF'"],
[".", "return yytext[0]"],
],
},
operators: [
// List of operators ordered by precedence. First value is the operators
// associativity. Operators of the same precedence should be on the same
// line.
// https://www.gnu.org/software/bison/manual/bison.html#Precedence
// TODO: Confirm these orders
["right", "ASSIGNMENT_OPERATOR_TOKEN"],
["right", "COMPARISON_TOKEN"],
["left", "+", "-", "!"],
["left", "*", "/"],
// Unlike many other languages, `^` _is_ left associative in Eel.
["left", "%", "^"],
["left", "&", "|" /* "~"" will go here as well */],
["left", "LOGICAL_OPERATOR_TOKEN"],
// TODO: Theoretically it should be possible to make `--1` a parse error.
],
bnf: {
SCRIPT: [
["expression EOF", "return {type: 'SCRIPT', body: [$1], loc: @$}"],
[
"expressionsOptionalTrailingSemi EOF",
"return {type: 'SCRIPT', body: $1, loc: @$}",
],
["EOF", "return {type: 'SCRIPT', body: [], loc: @$}"],
],
separator: [";", "separator ;"],
expressions: [
["expression separator", "$$ = [$1]"],
["expressions expression separator", "$$ = $1.concat([$2])"],
],
// This feels like a hack, but I haven't managed to find another way to
// express optional semicolons while still keeping the grammar unambiguous.
// Notably this does not allow single expressions to omit the trailing semi.
expressionsOptionalTrailingSemi: [
["expressions", "$$ = $1"],
["expressions expression", "$$ = $1.concat([$2])"],
],
EXPRESSION_BLOCK: [
[
"expressionsOptionalTrailingSemi",
"$$ = {type: 'EXPRESSION_BLOCK', body: $1, loc: @$}",
],
],
IDENTIFIER: [
[
"IDENTIFIER_TOKEN",
"$$ = {type: 'IDENTIFIER', value: $1.toLowerCase(), loc: @$};",
],
],
argument: ["expression", "EXPRESSION_BLOCK"],
arguments: [
["argument", "$$ = [$1]"],
["arguments , argument", "$$ = $1.concat([$3])"],
],
FUNCTION_CALL: [
[
"IDENTIFIER ( )",
"$$ = {type: 'CALL_EXPRESSION', callee: $1, arguments: [], loc: @$}",
],
[
"IDENTIFIER ( arguments )",
"$$ = {type: 'CALL_EXPRESSION', callee: $1, arguments: $3, loc: @$}",
],
],
LOGICAL_EXPRESSION: [
[
"expression LOGICAL_OPERATOR_TOKEN expression",
"$$ = {type: 'LOGICAL_EXPRESSION', left: $1, right: $3, operator: $2, loc: @$}",
],
],
ASSIGNMENT: [
[
"IDENTIFIER ASSIGNMENT_OPERATOR_TOKEN expression",
"$$ = {type: 'ASSIGNMENT_EXPRESSION', left: $1, operator: $2, right: $3, loc: @$}",
],
[
"FUNCTION_CALL ASSIGNMENT_OPERATOR_TOKEN expression",
"$$ = {type: 'ASSIGNMENT_EXPRESSION', left: $1, operator: $2, right: $3, loc: @$}",
],
],
number: [
["DIGITS_TOKEN", "$$ = Number($1)"],
["DIGITS_TOKEN .", "$$ = Number($1)"],
["DIGITS_TOKEN . DIGITS_TOKEN", "$$ = Number($1 + $2 + $3)"],
[". DIGITS_TOKEN", "$$ = Number('0' + $1 + $2)"],
[".", "$$ = 0"],
],
NUMBER_LITERAL: [
["number", "$$ = {type: 'NUMBER_LITERAL', value: $1, loc: @$}"],
],
UNARY_EXPRESSION: [
["- expression", unaryExpression],
["+ expression", unaryExpression],
["! expression", unaryExpression],
],
BINARY_EXPRESSION: [
["expression + expression", binaryExpression],
["expression - expression", binaryExpression],
["expression * expression", binaryExpression],
["expression / expression", binaryExpression],
["expression % expression", binaryExpression],
["expression & expression", binaryExpression],
["expression | expression", binaryExpression],
["expression ^ expression", binaryExpression],
["expression COMPARISON_TOKEN expression", binaryExpression],
],
expression: [
"BINARY_EXPRESSION",
"UNARY_EXPRESSION",
["( expression )", "$$ = $2"],
"NUMBER_LITERAL",
"ASSIGNMENT",
"FUNCTION_CALL",
"IDENTIFIER",
"LOGICAL_EXPRESSION",
["( EXPRESSION_BLOCK )", "$$ = $2"],
],
},
};
var parser = new Parser(grammar);
// If called from CLI, we should output source.
if (require.main === module) {
const settings = {
moduleType: "commonjs",
// Without this Jison defaults to including a commandline interface to the generated module.
moduleMain: () => {},
};
console.log(parser.generate(settings));
}
// you can also use the parser directly from memory
module.exports = {
parse: program => {
return parser.parse(program);
},
};