-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrammar.ne
157 lines (128 loc) · 12.3 KB
/
grammar.ne
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
@preprocessor typescript
@{%
import moo from "moo";
const lexer = moo.compile({
returnSymbol: /return/,
nullSymbol: /null/,
thisSymbol: /this/,
ifSymbol: /if/,
thenSymbol: /then/,
elseSymbol: /else/,
switchSymbol: /switch/,
caseSymbol: /case/,
arrow: /->/,
longArrow: /-->/,
openBracket: /\(/,
closedBracket: /\)/,
openCurlyBracket: /{/,
closedCurlyBracket: /}/,
point: /\./,
comma: /,/,
colon: /:/,
smallerEqual: /<=/,
greaterEqual: />=/,
smaller: /</,
greater: />/,
doubleEqual: /==/,
equal: /=/,
unequal: /!=/,
and: /&&/,
or: /\|\|/,
not: /!/,
parallel: /\|/,
int: /0[Xx][\da-fA-F]+|0[bB][01]+/,
number: /-?\d+(?:\.\d+)?/,
string: /"[^"]*"/,
boolean: /true|false/,
plus: /\+/,
minus: /-/,
multiply: /\*/,
percent: /%/,
divide: /\//,
identifier: /[a-zA-Z_$@']+\w*/,
ws: { match: /\s+/, lineBreaks: true },
});
%}
@lexer lexer
GrammarDefinition -> ws RuleDefinition ws {% ([,rule]) => [rule] %}
| ws RuleDefinitions:+ RuleDefinition ws {% ([,rules,rule]) => { if(rules.find(({ name }: { name: string }) => name === rule.name) != null) { throw new Error(`rule "${identifier}" is already defined`) } else { return [...rules, rule] } } %}
| ws {% () => [] %}
RuleDefinitions -> RuleDefinition %ws {% ([rule]) => rule %}
RuleDefinition -> %identifier ws %longArrow ws Steps {% ([{ value },,,,step]) => ({ name: value, step }) %}
Steps -> ParallelSteps {% ([steps]) => steps %}
ParallelSteps -> ParallelStep:+ SequentialSteps {% ([sequentials,sequential]) => ({ type: "parallel", children: [...sequentials, sequential] }) %}
| SequentialSteps {% ([sequential]) => sequential %}
ParallelStep -> SequentialSteps ws %parallel ws {% ([sequential]) => sequential %}
SequentialSteps -> SequentialStep:+ OrOperation {% ([primaries,primary]) => ({ type: "sequential", children: [...primaries, primary] }) %}
| OrOperation {% ([primary]) => primary %}
SequentialStep -> OrOperation ws %arrow ws {% ([primary]) => primary %}
OrOperation -> OrOperation ws %or ws AndOperation {% ([op1,,,,op2]) => ({ type: "or", children: [op1, op2] }) %}
| AndOperation {% ([value]) => value %}
AndOperation -> AndOperation ws %and ws NegateOperation {% ([op1,,,,op2]) => ({ type: "and", children: [op1, op2] }) %}
| NegateOperation {% ([value]) => value %}
NegateOperation -> %not ws NegateOperation {% ([,,op1]) => ({ type: "not", children: [op1] }) %}
| ComparisonOperation {% ([value]) => value %}
ComparisonOperation -> EquityOperation {% ([value]) => value %}
EquityOperation -> EqualOperation {% ([value]) => value %}
| UnequalOperation {% ([value]) => value %}
| RelationalOperation {% ([value]) => value %}
EqualOperation -> EquityOperation ws %doubleEqual ws RelationalOperation {% ([op1,,,,op2]) => ({ type: "equal", children: [op1, op2] }) %}
UnequalOperation -> EquityOperation ws %unequal ws RelationalOperation {% ([op1,,,,op2]) => ({ type: "unequal", children: [op1, op2] }) %}
RelationalOperation -> SmallerOperation {% ([value]) => value %}
| SmallerEqualOperation {% ([value]) => value %}
| GreaterOperation {% ([value]) => value %}
| GreaterEqualOperation {% ([value]) => value %}
| ArithmeticOperation {% ([value]) => value %}
SmallerOperation -> RelationalOperation ws %smaller ws ArithmeticOperation {% ([op1,,,,op2]) => ({ type: "smaller", children: [op1, op2] }) %}
SmallerEqualOperation -> RelationalOperation ws %smallerEqual ws ArithmeticOperation {% ([op1,,,,op2]) => ({ type: "smallerEqual", children: [op1, op2] }) %}
GreaterOperation -> RelationalOperation ws %greater ws ArithmeticOperation {% ([op1,,,,op2]) => ({ type: "greater", children: [op1, op2] }) %}
GreaterEqualOperation -> RelationalOperation ws %greaterEqual ws ArithmeticOperation {% ([op1,,,,op2]) => ({ type: "greaterEqual", children: [op1, op2] }) %}
ArithmeticOperation -> LineOperation {% ([value]) => value %}
LineOperation -> AddOperation {% ([value]) => value %}
| SubtractOperation {% ([value]) => value %}
| PointOperation {% ([value]) => value %}
AddOperation -> LineOperation ws %plus ws PointOperation {% ([op1,,,,op2]) => ({ type: "add", children: [op1, op2] }) %}
SubtractOperation -> LineOperation ws %minus ws PointOperation {% ([op1,,,,op2]) => ({ type: "subtract", children: [op1, op2] }) %}
PointOperation -> MultiplyOperation {% ([value]) => value %}
| DivideOperation {% ([value]) => value %}
| ModuloOperation {% ([value]) => value %}
| InvertOperation {% ([value]) => value %}
DivideOperation -> PointOperation ws %divide ws InvertOperation {% ([op1,,,,op2]) => ({ type: "divide", children: [op1, op2] }) %}
MultiplyOperation -> PointOperation ws %multiply ws InvertOperation {% ([op1,,,,op2]) => ({ type: "multiply", children: [op1, op2] }) %}
ModuloOperation -> PointOperation ws %percent ws InvertOperation {% ([op1,,,,op2]) => ({ type: "modulo", children: [op1, op2] }) %}
InvertOperation -> %minus ws InvertOperation {% ([,,op1]) => ({ type: "invert", children: [op1] }) %}
| Step {% ([value]) => value %}
Step -> Operation {% ([operation]) => operation %}
| Symbol {% ([symbol]) => symbol %}
| %thisSymbol {% () => ({ type: "this" }) %}
| GetVariable {% ([getVariable]) => getVariable %}
| SetVariable {% ([setVariable]) => setVariable %}
| Constant {% ([value]) => ({ type: "raw", value }) %}
| Conditional {% ([operation]) => operation %}
| %returnSymbol {% () => ({ type: "return" }) %}
| %nullSymbol {% () => ({ type: "null" }) %}
| %openBracket ws Steps ws %closedBracket {% ([,,steps]) => steps %}
| Random {% ([random]) => random %}
Random -> %openCurlyBracket RandomStep:* ws %closedCurlyBracket {% ([,steps]) => ({ type: "random", probabilities: steps.map(({ probability }: any) => probability), children: steps.map(({ steps }: any) => steps) }) %}
RandomStep -> ws %number %percent ws %colon ws Steps {% ([,{ value },,,,, steps]) => ({ probability: Number.parseFloat(value) / 100, steps }) %}
Operation -> %identifier %openBracket EmptyParameters ws %closedBracket {% ([{ value },,children]) => ({ type: "operation", children, identifier: value }) %}
EmptyParameters -> ws Parameters {% ([,parameters]) => parameters%}
| null {% () => [] %}
Parameters -> Parameter:* Steps {% ([stepsList, steps]) => [...stepsList, steps] %}
Parameter -> Steps ws %comma ws {% ([steps]) => steps %}
Symbol -> %identifier {% ([{ value }]) => ({ type: "symbol", identifier: value }) %}
ws -> %ws | null
Constant -> %boolean {% ([{ value }]) => value === "true" %}
| %string {% ([{ value }]) => value.slice(1, -1) %}
| %number {% ([{ value }]) => Number.parseFloat(value) %}
| %int {% ([{ value }]) => Number.parseInt(value) %}
GetVariable -> %thisSymbol %point %identifier {% ([,,{ value: identifier }]) => ({ type: "getVariable", identifier }) %}
SetVariable -> %thisSymbol %point %identifier ws %equal ws Step {% ([,,{ value: identifier },,,,value]) => ({ type: "setVariable", identifier, children: [value] }) %}
Conditional -> IfThenElse {% ([value]) => value %}
| Switch {% ([value]) => value %}
IfThenElse -> %ifSymbol %ws Steps %ws Then ws Else {% ([,,condition,,ifStep,,elseStep]) => ({ type: "if", children: [condition, ifStep, elseStep] }) %}
Then -> %thenSymbol ws %openCurlyBracket ws Steps ws %closedCurlyBracket {% ([,,,,steps]) => steps %}
Else -> %elseSymbol ws %openCurlyBracket ws Steps ws %closedCurlyBracket {% ([,,,,steps]) => steps %}
Switch -> %switchSymbol %ws Steps ws %openCurlyBracket SwitchCases:* ws %closedCurlyBracket {% ([,,value,,,cases]) => ({ type: "switch", cases: cases.map(({ caseValues }: any) => caseValues), children: [value, ...cases.map(({ steps }: any) => steps)] }) %}
SwitchCases -> ws SwitchCase:+ Steps {% ([,caseValues,steps]) => ({ caseValues, steps }) %}
SwitchCase -> %caseSymbol %ws Constant %colon ws {% ([,,caseValue]) => caseValue %}