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
|
package com.ashardalon.pratt.commands.impls;
import java.util.Set;
import com.ashardalon.pratt.blocks.ParseBlock;
import com.ashardalon.pratt.blocks.SimpleParseBlock;
import com.ashardalon.pratt.commands.NonInitialCommand;
import com.ashardalon.pratt.tokens.Token;
/**
* Contains factory methods for producing common implementations of
* {@link NonInitialCommand}
*
* @author EVE
*
*/
public class NonInitialCommands {
/**
* Create a left-associative infix 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> NonInitialCommand<K, V, C> infixLeft(final int precedence) {
return new LeftBinaryCommand<>(precedence);
}
/**
* Create a right-associative infix 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> NonInitialCommand<K, V, C> infixRight(final int precedence) {
return new RightBinaryCommand<>(precedence);
}
/**
* Create a non-associative infix 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> NonInitialCommand<K, V, C> infixNon(final int precedence) {
return new NonBinaryCommand<>(precedence);
}
/**
* Create a chained 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.
*
* @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> NonInitialCommand<K, V, C> chain(final int precedence, final Set<K> chainSet,
final Token<K, V> marker) {
return new ChainCommand<>(precedence, chainSet, marker);
}
/**
* Create a postfix 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> NonInitialCommand<K, V, C> postfix(final int precedence) {
return new PostfixCommand<>(precedence);
}
/**
* Create a post-circumfix operator.
*
* This is an operator in form similar to array indexing.
*
* @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 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> NonInitialCommand<K, V, C> postCircumfix(final int precedence,
final int insidePrecedence, final K closer, final Token<K, V> marker) {
final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
return new PostCircumfixCommand<>(precedence, innerBlock, marker);
}
/**
* Create a ternary operator.
*
* This is like C's ?: 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.
*
* @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> NonInitialCommand<K, V, C> ternary(final int precedence, final int insidePrecedence,
final K closer, final Token<K, V> marker, final boolean nonassoc) {
final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc);
}
}
|