-
Notifications
You must be signed in to change notification settings - Fork 11
/
expr.es6
132 lines (98 loc) · 2.28 KB
/
expr.es6
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
// Cheddar Expression Parser
import O from '../literals/op';
import P from './property';
import L from './any';
import F from './function';
import CheddarLexer from '../tok/lex';
import {OP, UOP, EXPR_OPEN, EXPR_CLOSE} from '../consts/ops';
// Special Exceptions
import B from '../literals/boolean';
/*
For Reference, the orginal grammar is:
E -> O E
E O E?
( E )
B
P
L
Made right-recursive:
E -> O E α
( E ) α
α <-- This is where any custom grammars go
B α
P α
L α
α -> O E α
O α
ε
Combined into one expression:
E -> (OE|(E)|P|L|B)[OE|O]
where groups are only nested to a depth of one
=== Forget the above ===
With thanks to [@orlp](http://chat.stackexchange.com/transcript/message/29080854#29080854),
errors in the above have been fixed and now the following grammar should work
Obfuscated because I'm an idiot
expr -> α β
start -> ( E ) // parenthesis
L // number
B // boolean
P // identifier
prefix E // prefix
end -> infix E // infix
postfix // postfix
ε
even worse:
E -> α β
ε
α -> ( E )
L
B
P
O E
β -> O E?
ε
*/
/*A -> '(' expr ')'
A -> op expr
A -> (indentifier | bool | literal)
B -> op expr?
expr -> A B*/
class CheddarExpressionTokenAlpha extends CheddarLexer {
exec() {
this.open(false);
this.jumpWhite();
const E = CheddarExpressionToken;
return this.grammar(true,
['(', E, ')'],
[O, E], // Prefix
[L],
[B],
[P]
);
}
get isExpression() { return true; }
}
class CheddarExpressionTokenBeta extends CheddarLexer {
exec() {
this.open(false);
this.jumpWhite();
const E = CheddarExpressionToken;
return this.grammar(true,
[O, E], //infix
[O],
[] // ε
);
}
get isExpression() { return true; }
}
export default class CheddarExpressionToken extends CheddarLexer {
exec() {
this.open(false);
this.jumpWhite();
return this.grammar(true,
[CheddarExpressionTokenAlpha, CheddarExpressionTokenBeta],
[] // ε
);
}
get isExpression() { return true; }
}