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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
package bjc.utils.parserutils.pratt;
import bjc.utils.data.ITree;
import bjc.utils.data.Tree;
import bjc.utils.parserutils.ParserException;
import java.util.Set;
/**
* Contains factory methods for producing common implementations of
* {@link LeftCommand}
*
* @author EVE
*
*/
public class LeftCommands {
/*
* A command with constant binding power.
*/
private static abstract class BinaryPostCommand<K, V, C> extends LeftCommand<K, V, C> {
private final int leftPower;
public BinaryPostCommand(int power) {
leftPower = power;
}
@Override
public int leftBinding() {
return leftPower;
}
}
private static abstract class BinaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
public BinaryCommand(int leftPower) {
super(leftPower);
}
protected abstract int rightBinding();
@Override
public ITree<Token<K, V>> leftDenote(ITree<Token<K, V>> operand, Token<K, V> operator,
ParserContext<K, V, C> ctx) throws ParserException {
ITree<Token<K, V>> opr = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false);
return new Tree<>(operator, operand, opr);
}
}
private static class LeftBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
public LeftBinaryCommand(int leftPower) {
super(leftPower);
}
@Override
protected int rightBinding() {
return 1 + leftBinding();
}
}
private static class RightBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
public RightBinaryCommand(int leftPower) {
super(leftPower);
}
@Override
protected int rightBinding() {
return leftBinding();
}
}
private static class NonBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
public NonBinaryCommand(int leftPower) {
super(leftPower);
}
@Override
protected int rightBinding() {
return 1 + leftBinding();
}
@Override
public int nextBinding() {
return leftBinding() - 1;
}
}
private static class PostCircumfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
private int insidePrec;
private K term;
private Token<K, V> mark;
public PostCircumfixCommand(int leftPower, int insidePower, K terminator, Token<K, V> marker) {
super(leftPower);
insidePrec = insidePower;
term = terminator;
mark = marker;
}
@SuppressWarnings("unchecked")
@Override
public ITree<Token<K, V>> leftDenote(ITree<Token<K, V>> operand, Token<K, V> operator,
ParserContext<K, V, C> ctx) throws ParserException {
ITree<Token<K, V>> inside = ctx.parse.parseExpression(insidePrec, ctx.tokens, ctx.state, false);
ctx.tokens.expect(term);
return new Tree<>(mark, operand, inside);
}
}
private static class PostfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
public PostfixCommand(int leftPower) {
super(leftPower);
}
@Override
public ITree<Token<K, V>> leftDenote(ITree<Token<K, V>> operand, Token<K, V> operator,
ParserContext<K, V, C> ctx) throws ParserException {
return new Tree<>(operator, operand);
}
}
private static class TernaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
private K term;
private int innerExp;
private Token<K, V> mark;
private boolean nonassoc;
public TernaryCommand(int leftPower, K terminator, Token<K, V> marker, boolean isNonassoc) {
super(leftPower);
term = terminator;
mark = marker;
nonassoc = isNonassoc;
}
@SuppressWarnings("unchecked")
@Override
public ITree<Token<K, V>> leftDenote(ITree<Token<K, V>> operand, Token<K, V> operator,
ParserContext<K, V, C> ctx) throws ParserException {
ITree<Token<K, V>> inner = ctx.parse.parseExpression(innerExp, ctx.tokens, ctx.state, false);
ctx.tokens.expect(term);
ITree<Token<K, V>> outer = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false);
return new Tree<>(mark, inner, operand, outer);
}
@Override
public int nextBinding() {
if (nonassoc) {
return leftBinding() - 1;
} else {
return leftBinding();
}
}
}
private static class ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
private Set<K> chainWith;
private Token<K, V> chain;
public ChainCommand(int leftPower, Set<K> chainSet, Token<K, V> chainMarker) {
super(leftPower);
chainWith = chainSet;
chain = chainMarker;
}
@Override
public ITree<Token<K, V>> leftDenote(ITree<Token<K, V>> operand, Token<K, V> operator,
ParserContext<K, V, C> ctx) throws ParserException {
ITree<Token<K, V>> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false);
ITree<Token<K, V>> res = new Tree<>(operator, operand, tree);
if (chainWith.contains(ctx.tokens.current().getKey())) {
Token<K, V> tok = ctx.tokens.current();
ctx.tokens.next();
ITree<Token<K, V>> other = leftDenote(tree, tok,
new ParserContext<>(ctx.tokens, ctx.parse, ctx.state));
return new Tree<>(chain, res, other);
} else {
return res;
}
}
@Override
public int nextBinding() {
return leftBinding() - 1;
}
}
/**
* Create a left-associative infix operator.
*
* @param precedence
* The precedence of the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> infixLeft(int precedence) {
return new LeftBinaryCommand<>(precedence);
}
/**
* Create a right-associative infix operator.
*
* @param precedence
* The precedence of the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> infixRight(int precedence) {
return new RightBinaryCommand<>(precedence);
}
/**
* Create a non-associative infix operator.
*
* @param precedence
* The precedence of the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> infixNon(int precedence) {
return new NonBinaryCommand<>(precedence);
}
/**
* Create a chained operator.
*
* @param precedence
* The precedence of the operator.
*
* @param chainSet
* The operators it forms a chain with.
*
* @param marker
* The token to use as the AST node for the chained
* operators.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> chain(int precedence, Set<K> chainSet, Token<K, V> marker) {
return new ChainCommand<>(precedence, chainSet, marker);
}
/**
* Create a postfix operator.
*
* @param precedence
* The precedence of the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> postfix(int precedence) {
return new PostfixCommand<>(precedence);
}
/**
* Create a post-circumfix operator.
*
* This is an operator in form similar to array indexing.
*
* @param precedence
* The precedence of this operator
*
* @param insidePrecedence
* The precedence of the expression inside the operator
*
* @param closer
* The token that closes the circumfix.
*
* @param marker
* The token to use as the AST node for the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> LeftCommand<K, V, C> postCircumfix(int precedence, int insidePrecedence, K closer,
Token<K, V> marker) {
return new PostCircumfixCommand<>(precedence, insidePrecedence, closer, marker);
}
/**
* Create a ternary operator.
*
* This is like C's ?: operator.
*
* @param precedence
* The precedence of the operator.
*
* @param insidePrecedence
* The precedence of the inner section of the operator.
*
* @param closer
* The token that marks the end of the inner section.
*
* @param marker
* The token to use as the AST node for the operator.
*
* @param nonassoc
* True if the command is non-associative, false
* otherwise.
*
* @return A command implementing this operator.
*/
public static <K, V, C> LeftCommand<K, V, C> ternary(int precedence, int insidePrecedence, K closer,
Token<K, V> marker, boolean nonassoc) {
return new TernaryCommand<>(insidePrecedence, closer, marker, nonassoc);
}
}
|