summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java
blob: 280afd05979baefb663fb4dcbccd5069c0a1a935 (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
package bjc.utils.cli.objects;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.UnaryOperator;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import static bjc.utils.cli.objects.Command.CommandStatus;
import static bjc.utils.cli.objects.Command.CommandStatus.*;

/*
 * @TODO 10/09/17 :DefineCLIFinish
 * 	This got left off about halfway through due to getting distracted
 * 	implementing CL-style format strings. It needs to be finished.
 */
/**
 * Command-line interface for building defines.
 *
 * @author Ben Culkin
 */
public class DefineCLI {
	private final Logger LOGGER = Logger.getLogger(DefineCLI.class.getName());

	public static class DefineState {
		public final Map<String, UnaryOperator<String>> defines;

		public final Map<String, String> strings;
		public final Map<String, String> formats;

		public final Map<String, Pattern> patterns;

		public DefineState() {
			this(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
		}

		public DefineState(Map<String, UnaryOperator<String>> defines, Map<String, String> strings,
				Map<String, String> formats, Map<String, Pattern> patterns) {
			this.defines = defines;

			this.strings = strings;
			this.formats = formats;

			this.patterns = patterns;
		}
	}

	private DefineState stat;

	public DefineCLI() {
		stat = new DefineState();
	}

	public static void main(String[] args) {
		DefineCLI defin = new DefineCLI();

		try (Scanner scn = new Scanner(System.in)) {
			defin.run(scn, "console", true);
		}
	}

	/**
	 * Run the CLI on an input source.
	 *
	 * @param input
	 *                The place to read input from.
	 * @param ioSource
	 *                The name of the place to read input from.
	 * @param interactive
	 *                Whether or not the source is interactive
	 */
	public void run(Scanner input, String ioSource, boolean interactive) {
		int lno = 0;
		while (input.hasNextLine()) {
			if (interactive)
				System.out.printf("define-conf(%d)>", lno);

			String ln = input.nextLine();

			lno += 1;

			Command com = Command.fromString(ln, lno, ioSource);
			if (com == null)
				continue;

			handleCommand(com, interactive);
		}

		input.close();
	}

	public void handleCommand(Command com, boolean interactive) {
		switch (com.nameCommand) {
		case "def-string":
		default:
			LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand));
			break;
		}
	}

	private CommandStatus defString(Command com) {
		String remn = com.remnCommand;

		int idx = remn.indexOf(' ');
		if (idx == -1) {
			LOGGER.warning(com.warn("Binding empty string to name '%s'\n", remn));
			idx = remn.length();
		}
		String name = remn.substring(0, idx);
		String strang = remn.substring(idx);

		if (stat.strings.containsKey(name)) {
			LOGGER.warning(com.warn("Shadowing string '%s'\n", name));
		}

		stat.strings.put(name, strang);

		return SUCCESS;
	}

	private CommandStatus defFormat(Command com) {
		String remn = com.remnCommand;

		int idx = remn.indexOf(' ');
		if (idx == -1) {
			LOGGER.warning(com.warn("Binding empty format to name '%s'\n", remn));
			idx = remn.length();
		}
		String name = remn.substring(0, idx);
		String fmt = remn.substring(idx);

		if (stat.formats.containsKey(name)) {
			LOGGER.warning(com.warn("Shadowing format '%s'\n", name));
		}

		stat.formats.put(name, fmt);

		return SUCCESS;
	}

	private CommandStatus bindFormat(Command com) {
		String[] parts = com.remnCommand.split(" ");

		return SUCCESS;
	}
}