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
|
package com.ashardalon.pratt.commands.impls;
import static com.ashardalon.pratt.blocks.ParseBlocks.repeating;
import static com.ashardalon.pratt.blocks.ParseBlocks.simple;
import static com.ashardalon.pratt.blocks.ParseBlocks.trigger;
import java.util.function.UnaryOperator;
import com.ashardalon.pratt.blocks.ParseBlock;
import com.ashardalon.pratt.commands.*;
import com.ashardalon.pratt.tokens.Token;
import bjc.data.Tree;
import bjc.functypes.MapBuilder;
/**
* * Contains factory methods for producing common implementations of
* {@link InitialCommand}
*
* @author EVE
*
*/
public class InitialCommands {
/**
* Create a new unary operator.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param precedence
* The precedence of the operator.
*
* @return A command implementing that operator.
*/
public static <K, V, C> InitialCommand<K, V, C> unary(final int precedence) {
return new UnaryCommand<>(precedence);
}
/**
* Create a new grouping operator.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param precedence
* The precedence of the expression in the operator.
*
* @param term
* The type that closes the group.
*
* @param mark
* The token for the AST node of the group.
*
* @return A command implementing the operator.
*/
public static <K, V, C> InitialCommand<K, V, C> grouping(final int precedence, final K term,
final Token<K, V> mark) {
final ParseBlock<K, V, C> innerBlock = simple(precedence, term, null);
return new GroupingCommand<>(innerBlock, mark);
}
/**
* Create a new leaf operator.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @return A command implementing the operator.
*/
public static <K, V, C> InitialCommand<K, V, C> leaf() {
return new LeafCommand<>();
}
/**
* Create a new pre-ternary operator, like an if-then-else statement.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param cond1
* The priority of the first block.
*
* @param block1
* The priority of the second block.
*
* @param block2
* The priority of the third block.
*
* @param mark1
* The marker that ends the first block.
*
* @param mark2
* The marker that ends the second block.
*
* @param term
* The token for the AST node of the group.
*
* @return A command implementing the operator.
*/
public static <K, V, C> InitialCommand<K, V, C> preTernary(final int cond1, final int block1, final int block2,
final K mark1, final K mark2, final Token<K, V> term) {
final ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null);
final ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null);
final ParseBlock<K, V, C> opblock2 = simple(block2, null, null);
return new PreTernaryCommand<>(condBlock, opblock1, opblock2, term);
}
/**
* Create a new named constant.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param val
* The value of the constant.
*
* @return A command implementing the constant.
*/
public static <K, V, C> InitialCommand<K, V, C> constant(final Tree<Token<K, V>> val) {
return new ConstantCommand<>(val);
}
/**
* Create a new delimited command. This is for block-like constructs.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param inner
* The precedence of the inner blocks.
*
* @param delim
* The marker between sub-blocks.
*
* @param mark
* The block terminator.
*
* @param term
* The token for the AST node of the group.
*
* @param onEnter
* The function to apply to the state on entering the block.
*
* @param onDelim
* The function to apply to the state on finishing a sub-block.
*
* @param onExit
* The function to apply to the state on exiting the block.
*
* @param statement
* Whether or not the sub-blocks are statements or expressions.
*
* @return A command implementing the operator.
*/
public static <K, V, C> InitialCommand<K, V, C> delimited(final int inner, final K delim, final K mark,
final Token<K, V> term, final UnaryOperator<C> onEnter, final UnaryOperator<C> onDelim,
final UnaryOperator<C> onExit, final boolean statement) {
final ParseBlock<K, V, C> innerBlock = simple(inner, null, null);
final ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim);
final ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit);
final GroupingCommand<K, V, C> command = new GroupingCommand<>(scopedBlock, term);
/*
* Remove the wrapper layer from grouping-command on top of
* RepeatingParseBlock.
*/
return denest(command);
}
/**
* Create a new denesting command.
*
* This removes one tree-level, and is useful when combining complex
* parse blocks with commands.
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param comm
* The command to denest.
*
* @return A command that denests the result of the provided command.
*/
public static <K, V, C> InitialCommand<K, V, C> denest(final InitialCommand<K, V, C> comm) {
return new DenestingCommand<>(comm);
}
/**
* Create a new 'panfix' command.
*
* Form is <term> <expr> <term> <expr> <term>
*
* @param <K> The key type for the tokens.
* @param <V> The value type for the tokens.
* @param <C> The context type for the tokens.
*
* @param precedence The precedence for this operator
* @param term The indicator for the operator
* @param marker The token to mark this tree
*
* @return A command that implements a panfix operator
*/
public static <K, V, C> InitialCommand<K, V, C> panfix(final int precedence, final K term, final Token<K, V> marker) {
return new PanfixCommand<>(marker, term, precedence);
}
/**
* Create a command that unconditionally returns a failure result.
*
* @param <K> Token key type
* @param <V> Token value type
* @param <C> Context type
*
* @return A command that unconditionally fails
*/
public static <K, V, C> InitialCommand<K, V, C> fail() {
return (operator, ctx) -> CommandResult.fail();
}
/**
* Create a new builder for branching/sub-command style commands.
*
* @param <K> Token key type
* @param <V> Value key type
* @param <C> Context type
*
* @return A builder for branching/sub-command style commands
*/
public static <K, V, C> MapBuilder<K, InitialCommand<K, V, C>, InitialCommand<K, V, C>> branch() {
return MapBuilder.from(BranchInitialCommand::new);
}
}
|