summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/CLTokenizer.java
blob: 9fd56f5541b0b08bd878be4d595eb3e6b998c377 (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
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
package bjc.utils.ioutils.format;

import java.util.*;
import java.util.regex.*;

/**
 * Tokenizer for creating @{link Decree}s from strings.
 *
 * @author bjculkin
 *
 */
public class CLTokenizer implements Iterator<SimpleDecree> {
	/**
	 * Whether or not the tokenizer is in debug mode or not.
	 */
	public static boolean DEBUG = false;

	/*
	 * Internal class for a tokenizer that returns a specific set of tokens.
	 */
	private static class SetCLTokenizer extends CLTokenizer {
		private Iterator<SimpleDecree> body;

		public SetCLTokenizer(Iterator<SimpleDecree> bod) {
			body = bod;
		}

		public SetCLTokenizer(Iterable<SimpleDecree> bod) {
			body = bod.iterator();
		}

		@Override
		public boolean hasNext() {
			return body.hasNext();
		}

		@Override
		public SimpleDecree next() {
			return body.next();
		}
	}

	private Matcher mat;

	private SimpleDecree dir;

	/**
	 * Empty constructor that should only be invoked if you are a subclass who
	 * overrides hasNext()/next().
	 */
	protected CLTokenizer() {

	}

	/**
	 * Create a new tokenizer, tokenizing from a given string.
	 *
	 * @param strang
	 *               The string to tokenize from.
	 */
	public CLTokenizer(String strang) {
		this.mat = CLPattern.getDirectiveMatcher(strang);
	}

	/**
	 * Create a CLTokenizer yielding a given set of decrees.
	 *
	 * @param bod
	 *            The decrees to yield.
	 *
	 * @return A tokenizer yielding the given set of decrees.
	 */
	public static CLTokenizer fromTokens(Iterator<SimpleDecree> bod) {
		return new SetCLTokenizer(bod);
	}

	/**
	 * Create a CLTokenizer yielding a given set of decrees.
	 *
	 * @param bod
	 *            The decrees to yield.
	 *
	 * @return A tokenizer yielding the given set of decrees.
	 */
	public static CLTokenizer fromTokens(Iterable<SimpleDecree> bod) {
		return new SetCLTokenizer(bod);
	}

	@Override
	public boolean hasNext() {
		return !mat.hitEnd();
	}

	@Override
	public SimpleDecree next() {
		return getNext();
	}

	private SimpleDecree getNext() {
		if (!hasNext()) throw new NoSuchElementException("No possible decrees remaining");

		if (dir != null) {
			SimpleDecree tmp = dir;

			dir = null;

			return tmp;
		}

		StringBuffer sb = new StringBuffer();

		while (mat.find()) {
			mat.appendReplacement(sb, "");

			String tmp = sb.toString();

			{
				String directiveName = mat.group("name");
				String directiveFunction = mat.group("funcname");
				String directiveModifierString = mat.group("modifiers");
				String directiveParameterString = mat.group("params");

				if (directiveModifierString  == null)  directiveModifierString  = "";
				if (directiveParameterString == null)  directiveParameterString = "";

				boolean isUser = directiveName == null && directiveFunction != null;

				dir = new SimpleDecree(directiveName, isUser,
						CLParameters.fromDirective(directiveParameterString),
						CLModifiers.fromString(directiveModifierString));
				
				dir.setPosition(mat.start(), mat.end());
			}

			if (tmp.equals("")) {
				SimpleDecree dcr = dir;

				dir = null;

				return dcr;
			}

			return new SimpleDecree(sb.toString(), mat.start(), mat.end());
		}

		mat.appendTail(sb);

		return new SimpleDecree(sb.toString());
	}

	/**
	 * Read in a group decree.
	 *
	 * @param openedWith
	 *                       The decree that started the group.
	 *
	 * @param desiredClosing
	 *                       The name of the decree that will close the group.
	 *
	 * @return A group decree with the given properties.
	 */
	public GroupDecree nextGroup(SimpleDecree openedWith, String desiredClosing) {
		return nextGroup(openedWith, desiredClosing, null);
	}

	/**
	 * Read in a group decree.
	 *
	 * @param openedWith
	 *                       The decree that started the group.
	 *
	 * @param desiredClosing
	 *                       The name of the decree that will close the group.
	 *
	 * @param clauseSep
	 *                       The name of the decree that will separate clauses in
	 *                       the group. Pass 'null' if this group doesn't have
	 *                       separate clauses.
	 *
	 * @return A group decree with the given properties.
	 */
	public GroupDecree nextGroup(SimpleDecree openedWith, String desiredClosing,
			String clauseSep) {
		GroupDecree newGroup = new GroupDecree();
		newGroup.opening = openedWith;

		if (!hasNext()) {
			String fmt = "No decrees available for group starting with %s at %#s";
			throw new NoSuchElementException(String.format(fmt, openedWith.name, openedWith.position));
		}

		ClauseDecree curClause = new ClauseDecree();

		int nestingLevel = 1;

		SimpleDecree curDecree;

		do {
			curDecree = next();

			if (curDecree.isLiteral) {
				curClause.addChild(curDecree);
			} else if (nestingLevel == 1) {
				// Clauses can only be ended at neutral nesting
				if (curDecree.isNamed(desiredClosing)) {
					newGroup.addChild(curClause);
					newGroup.closing = curDecree;

					if (DEBUG) {
						System.err.printf("[TRACE] Closing with %s\n", curDecree);
					}

					break;
				} else if (clauseSep != null && curDecree.isNamed(clauseSep)) {
					if (DEBUG)
						System.err.printf("[TRACE] Clause separator %s\n", curDecree);
					curClause.terminator = curDecree;
					newGroup.addChild(curClause);

					curClause = new ClauseDecree();
				} else {
					curClause.addChild(curDecree);
				}
			} else if (curDecree.isNamed(openedWith.name)) {
				// Nest one level deeper
				nestingLevel += 1;

				curClause.addChild(curDecree);
			} else if (curDecree.isNamed(desiredClosing)) {
				// Unnest
				nestingLevel -= 1;

				curClause.addChild(curDecree);
			} else {
				curClause.addChild(curDecree);
			}
		} while (hasNext());

		if (newGroup.closing == null) {
			String msg = String.format(
					"Did not find closing directive for group (wanted \"%s\", last decree was \"%s\" at %#s)",
					desiredClosing, curDecree.name, curDecree.position);

			throw new NoSuchElementException(msg);
		}

		return newGroup;
	}
}