summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/objects/DelimSplitterCLI.java
blob: 59822e4c7428e9ba65e91ef8c2070cf2a88639ab (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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package bjc.utils.cli.objects;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import bjc.data.ITree;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.IList;
import bjc.utils.funcutils.StringUtils;
import bjc.utils.ioutils.MirrorDB;
import bjc.utils.parserutils.delims.DelimiterException;
import bjc.utils.parserutils.delims.DelimiterGroup;
import bjc.utils.parserutils.delims.RegexCloser;
import bjc.utils.parserutils.delims.RegexOpener;
import bjc.utils.parserutils.delims.SequenceDelimiter;
import bjc.utils.parserutils.delims.StringDelimiter;
import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter;

/**
 * Test for {@link SequenceDelimiter} as well as
 * {@link ConfigurableTokenSplitter}
 *
 * @author EVE
 *
 */
public class DelimSplitterCLI {
	private ConfigurableTokenSplitter split;

	private StringDelimiter dlm;

	private MirrorDB mirrored;

	private Map<String, DelimiterGroup<String>> groups;

	boolean verbose;

	/*
	 * Create a new tester.
	 */
	private DelimSplitterCLI() {
		mirrored = new MirrorDB();

		groups = new HashMap<>();

		split = new ConfigurableTokenSplitter(true);

		dlm = new StringDelimiter();

		verbose = true;
	}

	/*
	 * Run the tester interface.
	 */
	private void runLoop() {
		final Scanner scn = new Scanner(System.in);

		System.out.print("Enter a command (blank line to quit): ");
		String inp = scn.nextLine().trim();
		System.out.println();

		while (!inp.equals("")) {
			handleCommand(inp, scn, true);

			System.out.println();

			System.out.print("Enter a command (blank line to quit): ");
			inp = scn.nextLine();

			System.out.println();
		}

		scn.close();
	}

	/*
	 * Handle a input command.
	 */
	private void handleCommand(final String inp, final Scanner scn,
			final boolean isInteractive) {
		if (inp.equals(""))
			return;

		int idx = inp.indexOf(' ');

		if (idx == -1) {
			idx = inp.length();
		}

		final String command = inp.substring(0, idx);

		final String args = inp.substring(idx).trim();
		final String[] argArray = args.split(" ");

		switch (command) {
		case "test":
			handleTest(args, false);
			break;
		case "test-ws":
			handleTest(args, true);
			break;
		case "splitter-split":
			handleSplit(argArray);
			break;
		case "splitter-compile":
			split.compile();
			if (verbose) {
				System.out.println("Compiled splitter");
			}
			break;
		case "splitter-add":
			split.addSimpleDelimiters(argArray);
			if (verbose) {
				System.out.println(
						"Added delimiters " + StringUtils.toEnglishList(argArray, true));
			}
			break;
		case "splitter-addmulti":
			split.addMultiDelimiters(argArray);
			if (verbose) {
				System.out.println("Added multi-delimiters "
						+ StringUtils.toEnglishList(argArray, true));
			}
			break;
		case "splitter-addmatch":
			for (final String arg : argArray) {
				split.addSimpleDelimiters(arg, mirrored.mirror(arg));
			}
			if (verbose) {
				System.out.println("Added matched delimiters "
						+ StringUtils.toEnglishList(argArray, true));
			}
			break;
		case "splitter-debug":
			System.out.println(split.toString());
			break;
		case "splitter-reset":
			split = new ConfigurableTokenSplitter(true);
			if (verbose) {
				System.out.println("Reset splitter");
			}
			break;

		case "delims-addgroup":
			for (final String arg : argArray) {
				dlm.addGroup(groups.get(arg));
			}
			if (verbose) {
				System.out.println(
						"Added groups " + StringUtils.toEnglishList(argArray, true));
			}
			break;
		case "delims-setinitial":
			dlm.setInitialGroup(groups.get(argArray[0]));
			if (verbose) {
				System.out.println("Set initial group");
			}
			break;
		case "delims-debug":
			System.out.println(dlm.toString());
			break;
		case "delims-test":
			handleDelim(args);
			break;
		case "delims-reset":
			dlm = new StringDelimiter();
			if (verbose) {
				System.out.println("Reset delimiter");
			}
			break;
		case "delimgroups-new":
			for (final String arg : argArray) {
				groups.put(arg, new DelimiterGroup<>(arg));
			}
			if (verbose) {
				System.out.println(
						"Created groups " + StringUtils.toEnglishList(argArray, true));
			}
			break;
		case "delimgroups-edit":
			for (final String arg : argArray) {
				handleEditGroup(arg, scn, isInteractive);
			}
			break;
		case "delimgroups-debug":
			for (final DelimiterGroup<String> group : groups.values()) {
				System.out.println(group.toString());
			}
			break;
		case "delimgroups-reset":
			dlm = new StringDelimiter();
			groups = new HashMap<>();
			if (verbose) {
				System.out.println("Reset delimiter groups + delimiter");
			}
			break;
		case "load-file":
			handleLoadFile(args);
			break;
		default:
			System.out.println("Unknown command ");
		}

	}

	/*
	 * Load script commands from a file.
	 */
	private void handleLoadFile(final String args) {
		String pth = args;

		if (args.startsWith("\"")) {
			pth = args.substring(1, args.length() - 1);
		}

		try (FileInputStream fis = new FileInputStream(pth)) {
			final Scanner scn = new Scanner(fis);

			while (scn.hasNextLine()) {
				final String ln = scn.nextLine().trim();

				if (ln.equals("")) {
					continue;
				}
				if (ln.startsWith("#")) {
					continue;
				}

				if (verbose) {
					System.out.println("\nRead command '" + ln + "' from file\n");
				}
				handleCommand(ln, scn, false);
			}

			scn.close();
		} catch (final FileNotFoundException fnfex) {
			System.out.println("Couldn't find file '" + args + "'");
		} catch (final IOException ioex) {
			System.out.println(
					"I/O error with file '" + args + "'\nCause: " + ioex.getMessage());
		}
	}

	/*
	 * Handle editing a group.
	 */
	private void handleEditGroup(final String arg, final Scanner scn,
			final boolean isInteractive) {
		if (!groups.containsKey(arg)) {
			System.out.println("No group named '" + arg + "'");
			return;
		}

		final DelimiterGroup<String> group = groups.get(arg);

		if (verbose) {
			System.out.println("Editing group '" + arg + "'");
		}
		if (isInteractive) {
			System.out.println("Enter command (blank line to stop editing): ");
		}

		String ln = scn.nextLine().trim();

		while (!ln.equals("")) {
			int idx = ln.indexOf(' ');

			if (idx == -1) {
				idx = ln.length();
			}

			final String command = ln.substring(0, idx);

			final String args = ln.substring(idx).trim();
			final String[] argArray = args.split(" ");

			switch (command) {
			case "add-closing":
				group.addClosing(argArray);
				if (verbose) {
					System.out.println(
							"Added closers " + StringUtils.toEnglishList(argArray, true));
				}
				break;
			case "add-tlexclude":
				group.addTopLevelForbid(argArray);
				if (verbose) {
					System.out.println("Added top-level exclusions "
							+ StringUtils.toEnglishList(argArray, true));
				}
				break;
			case "add-exclude":
				group.addTopLevelForbid(argArray);
				if (verbose) {
					System.out.println("Added nested exclusions "
							+ StringUtils.toEnglishList(argArray, true));
				}
				break;
			case "add-subgroup":
				group.addSubgroup(argArray[0], Integer.parseInt(argArray[1]));
				if (verbose) {
					System.out.printf("Added subgroup %s with priority %s\n", argArray[0],
							argArray[1]);
				}
				break;
			case "add-implied-subgroup":
				group.implySubgroup(argArray[0], argArray[1]);
				if (verbose) {
					System.out.printf("Made closer '%s' imply a '%s' subgroup\n",
							argArray[0], argArray[1]);
				}
				break;
			case "add-opener":
				group.addOpener(argArray[0], argArray[1]);
				if (verbose) {
					System.out.printf("Added opener '%s' for group '%s'\n", argArray[0],
							argArray[1]);
				}
				break;
			case "add-reopener":
				group.addPredOpener(new RegexOpener(argArray[0], argArray[1]));
				if (verbose) {
					System.out.printf("Added regex '%s' as opener for '%s'\n",
							argArray[1], argArray[0]);
				}
				break;
			case "add-recloser":
				group.addPredCloser(new RegexCloser(argArray[0]));
				if (verbose) {
					System.out.printf("Added parameterized string '%s' as closer\n",
							argArray[0]);
				}
				break;
			case "debug":
				System.out.println(group.toString());
				break;
			default:
				System.out.println("Unknown command " + command);
			}

			if (isInteractive) {
				System.out.println("Enter command (blank line to stop editing): ");
			}

			ln = scn.nextLine().trim();
		}

		if (verbose) {
			System.out.println("Finished editing group '" + arg + "'");
		}
	}

	private void handleDelim(final String args) {
		try {
			final ITree<String> res = dlm.delimitSequence(args.split(" "));

			printDelimSeq(res);
		} catch (final DelimiterException dex) {
			System.out.println("Expression '" + args
					+ "' isn't properly delimited.\n\tCause: " + dex.getMessage());
		}
	}

	private void handleSplit(final String[] argArray) {
		for (int i = 0; i < argArray.length; i++) {
			final String arg = argArray[i];

			final IList<String> strangs = split.split(arg);

			System.out.printf("%d '%s' %s\n", i, arg, strangs);
		}
	}

	private void handleTest(final String inp, final boolean splitWS) {
		IList<String> strings;

		try {
			strings = split.split(inp);
		} catch (final IllegalStateException isex) {
			System.out.println("Splitter must be compiled at least once before use.");
			return;
		}

		System.out.println("Split tokens: " + strings);

		if (splitWS) {
			final List<String> tks = new LinkedList<>();

			for (final String strang : strings) {
				tks.addAll(Arrays.asList(strang.split(" ")));
			}

			strings = new FunctionalList<>(tks);
		}
		try {
			final ITree<String> delim
					= dlm.delimitSequence(strings.toArray(new String[0]));

			printDelimSeq(delim);
		} catch (final DelimiterException dex) {
			System.out.println("Expression isn't properly delimited.");
			System.out.println("Cause: " + dex.getMessage());
		}
	}

	private void printDelimSeq(final ITree<String> delim) {
		System.out.println("Delimited tokens:\n" + delim.getChild(1).toString());
		System.out.print("Delimited expr: ");
		printDelimTree(delim);
		System.out.println();
		System.out.println();

		System.out.println();
	}

	private void printDelimTree(final ITree<String> tree) {
		final StringBuilder sb = new StringBuilder();

		intPrintDelimTree(tree.getChild(1), sb);

		System.out.println(sb.toString().replaceAll("\\s+", " "));
	}

	private void intPrintDelimTree(final ITree<String> tree, final StringBuilder sb) {
		tree.doForChildren(child -> {
			intPrintDelimNode(child, sb);
		});
	}

	private void intPrintDelimNode(final ITree<String> tree, final StringBuilder sb) {
		if (tree.getHead().equals("contents")) {
			intPrintDelimTree(tree, sb);
			return;
		}

		switch (tree.getChildrenCount()) {
		case 0:
			sb.append(tree.getHead());
			sb.append(" ");

			break;
		case 1:
			intPrintDelimTree(tree.getChild(0), sb);

			break;
		case 2:
			intPrintDelimTree(tree.getChild(0).getChild(0), sb);
			intPrintDelimNode(tree.getChild(1), sb);

			break;
		case 3:
			intPrintDelimNode(tree.getChild(0), sb);

			final ITree<String> contents = tree.getChild(1);

			intPrintDelimTree(contents.getChild(0), sb);
			intPrintDelimNode(tree.getChild(2), sb);

			break;
		}
	}

	/**
	 * Main method
	 *
	 * @param args
	 *             Unused CLI args.
	 */
	public static void main(final String[] args) {
		final DelimSplitterCLI tst = new DelimSplitterCLI();

		tst.runLoop();
	}
}