summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/PrattParser.java
blob: e08a67c9f2f655bd2c25edc331cc80eca7697f1d (plain)
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
package bjc.pratt;

import bjc.pratt.commands.DefaultInitialCommand;
import bjc.pratt.commands.DefaultNonInitialCommand;
import bjc.utils.data.ITree;
import bjc.utils.funcutils.NumberUtils;
import bjc.utils.parserutils.ParserException;

import java.util.HashMap;
import java.util.Map;

/**
 * A configurable Pratt parser for expressions.
 * 
 * @author EVE
 * 
 * @param <K>
 *                The key type for the tokens.
 * 
 * @param <V>
 *                The value type for the tokens.
 * 
 * @param <C>
 *                The state type of the parser.
 * 
 *
 */
public class PrattParser<K, V, C> {
	/*
	 * Default commands that error when used.
	 */
	private final NonInitialCommand<K, V, C>	DEFAULT_LEFT_COMMAND	= new DefaultNonInitialCommand<>();
	private final InitialCommand<K, V, C>		DEFAULT_NULL_COMMAND	= new DefaultInitialCommand<>();

	/*
	 * Left-commands that depend on what the null command was.
	 */
	private Map<K, Map<K, NonInitialCommand<K, V, C>>> dependantLeftCommands;

	/*
	 * The left commands.
	 */
	private Map<K, NonInitialCommand<K, V, C>> leftCommands;
	/*
	 * The initial commands.
	 */
	private Map<K, InitialCommand<K, V, C>> nullCommands;
	/*
	 * Initial commands only checked for statements.
	 */
	private Map<K, InitialCommand<K, V, C>> statementCommands;

	/**
	 * Create a new Pratt parser.
	 * 
	 */
	public PrattParser() {
		dependantLeftCommands = new HashMap<>();

		leftCommands = new HashMap<>();
		nullCommands = new HashMap<>();
		statementCommands = new HashMap<>();
	}

	/**
	 * Parse an expression.
	 * 
	 * @param precedence
	 *                The initial precedence for the expression.
	 * 
	 * @param tokens
	 *                The tokens for the expression.
	 * 
	 * @param state
	 *                The state of the parser.
	 * 
	 * @param isStatement
	 *                Whether or not to parse statements.
	 * 
	 * @return The expression as an AST.
	 * 
	 * @throws ParserException
	 *                 If something goes wrong during parsing.
	 */
	public ITree<Token<K, V>> parseExpression(int precedence, TokenStream<K, V> tokens, C state,
			boolean isStatement) throws ParserException {
		if (precedence < 0) {
			throw new IllegalArgumentException("Precedence must be greater than zero");
		}

		Token<K, V> initToken = tokens.current();
		tokens.next();

		K initKey = initToken.getKey();

		ITree<Token<K, V>> ast;

		if (isStatement && statementCommands.containsKey(initKey)) {
			ast = statementCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken,
					new ParserContext<>(tokens, this, state));
		} else {
			ast = nullCommands.getOrDefault(initKey, DEFAULT_NULL_COMMAND).denote(initToken,
					new ParserContext<>(tokens, this, state));
		}

		int rightPrec = Integer.MAX_VALUE;

		while (true) {
			Token<K, V> tok = tokens.current();

			K key = tok.getKey();

			NonInitialCommand<K, V, C> command = leftCommands.getOrDefault(key, DEFAULT_LEFT_COMMAND);

			if (dependantLeftCommands.containsKey(initKey)) {
				command = dependantLeftCommands.get(initKey).getOrDefault(key, command);
			}

			int leftBind = command.leftBinding();

			if (NumberUtils.between(precedence, rightPrec, leftBind)) {
				tokens.next();

				ast = command.denote(ast, tok, new ParserContext<>(tokens, this, state));
				rightPrec = command.nextBinding();
			} else {
				break;
			}
		}

		return ast;
	}

	/**
	 * Add a non-initial command to this parser.
	 * 
	 * @param marker
	 *                The key that marks the command.
	 * 
	 * @param comm
	 *                The command.
	 */
	public void addNonInitialCommand(K marker, NonInitialCommand<K, V, C> comm) {
		leftCommands.put(marker, comm);
	}

	/**
	 * Add a initial command to this parser.
	 * 
	 * @param marker
	 *                The key that marks the command.
	 * 
	 * @param comm
	 *                The command.
	 */
	public void addInitialCommand(K marker, InitialCommand<K, V, C> comm) {
		nullCommands.put(marker, comm);
	}

	/**
	 * Add a statement command to this parser.
	 * 
	 * The difference between statements and initial commands is that
	 * statements can only appear at the start of the expression.
	 * 
	 * @param marker
	 *                The key that marks the command.
	 * 
	 * @param comm
	 *                The command.
	 */
	public void addStatementCommand(K marker, InitialCommand<K, V, C> comm) {
		statementCommands.put(marker, comm);
	}

	/**
	 * Add a dependant non-initial command to this parser.
	 */
	public void addDependantCommand(K dependant, K marker, NonInitialCommand<K, V, C> comm) {
		if (dependantLeftCommands.containsKey(dependant)) {
			dependantLeftCommands.get(dependant).put(marker, comm);
		} else {
			Map<K, NonInitialCommand<K, V, C>> comms = new HashMap<>();

			comms.put(marker, comm);

			dependantLeftCommands.put(dependant, comms);
		}
	}
}