summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/RGrammarSet.java
blob: c797b8c637173df4e8b824fa6cc28f6508e3afa2 (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
package bjc.rgens.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;
import java.util.Set;

/**
 * Represents a set of grammars that can share rules via exports.
 *
 * @author EVE
 */
public class RGrammarSet {
	/* Contains all the grammars in this set. */
	private Map<String, RGrammar> grammars;

	/* Contains all the exported rules from grammars. */
	private Map<String, RGrammar> exportedRules;

	/* Contains which export came from which grammar. */
	private Map<String, String> exportFrom;

	/* Contains which file a grammar was loaded from. */
	private Map<String, String> loadedFrom;

	/** Create a new set of randomized grammars. */
	public RGrammarSet() {
		grammars = new HashMap<>();

		exportedRules = new HashMap<>();

		exportFrom = new TreeMap<>();
		loadedFrom = new HashMap<>();
	}

	/**
	 * Add a grammar to this grammar set.
	 *
	 * @param grammarName
	 * 	The name of the grammar to add.
	 *
	 * @param gram
	 * 	The grammar to add.
	 *
	 * @throws IllegalArgumentException
	 * 	If the grammar name is invalid.
	 */
	public void addGrammar(String grammarName, RGrammar gram) {
		/* Make sure a grammar is valid. */
		if (grammarName == null) {
			throw new NullPointerException("Grammar name must not be null");
		} else if (gram == null) {
			throw new NullPointerException("Grammar must not be null");
		} else if (grammarName.equals("")) {
			throw new IllegalArgumentException("The empty string is not a valid grammar name");
		}

		grammars.put(grammarName, gram);

		/* Process exports from the grammar. */
		for (Rule export : gram.getExportedRules()) {
			exportedRules.put(export.name, gram);

			exportFrom.put(export.name, grammarName);
		}

		/* Add exports to grammar. */
		gram.setImportedRules(exportedRules);
	}

	/**
	 * Get a grammar from this grammar set.
	 *
	 * @param grammarName
	 * 	The name of the grammar to get.
	 *
	 * @return
	 * 	The grammar with that name.
	 *
	 * @throws IllegalArgumentException
	 * 	If the grammar name is invalid or not present in this set.
	 */
	public RGrammar getGrammar(String grammarName) {
		/* Check arguments. */
		if (grammarName == null) {
			throw new NullPointerException("Grammar name must not be null");
		} else if (grammarName.equals("")) {
			throw new IllegalArgumentException("The empty string is not a valid grammar name");
		} else if (!grammars.containsKey(grammarName)) {
			String msg = String.format("No grammar with name '%s' found", grammarName);

			throw new IllegalArgumentException(msg);
		}

		return grammars.get(grammarName);
	}

	/**
	 * Get the grammar a rule was exported from.
	 *
	 * @param exportName
	 * 	The name of the exported rule.
	 *
	 * @return
	 * 	The grammar the exported rule came from.
	 *
	 * @throws IllegalArgumentException
	 * 	If the export name is invalid or not present in this set.
	 */
	public RGrammar getExportSource(String exportName) {
		/* Check arguments. */
		if (exportName == null) {
			throw new NullPointerException("Export name must not be null");
		} else if (exportName.equals("")) {
			throw new IllegalArgumentException("The empty string is not a valid rule name");
		} else if (!exportedRules.containsKey(exportName)) {
			String msg = String.format("No export with name '%s' defined", exportName);
			throw new IllegalArgumentException(msg);
		}

		return exportedRules.get(exportName);
	}

	/**
	 * Get the source of an exported rule.
	 *
	 * This will often be a grammar name, but is not required to be one.
	 *
	 * @param exportName
	 * 	The name of the exported rule.
	 *
	 * @return 
	 * 	The source of an exported rule.
	 *
	 * @throws IllegalArgumentException
	 * 	If the exported rule is invalid or not present in this set.
	 */
	public String exportedFrom(String exportName) {
		/* Check arguments. */
		if (exportName == null) {
			throw new NullPointerException("Export name must not be null");
		} else if (exportName.equals("")) {
			throw new IllegalArgumentException("The empty string is not a valid rule name");
		} else if (!exportedRules.containsKey(exportName)) {
			String msg = String.format("No export with name '%s' defined", exportName);

			throw new IllegalArgumentException(msg);
		}

		return exportFrom.getOrDefault(exportName, "Unknown");
	}

	/**
	 * Get the source of an grammar
	 *
	 * This will often be a file name, but is not required to be one.
	 *
	 * @param grammarName
	 * 	The name of the exported grammar.
	 *
	 * @return
	 * 	The source of an exported grammar.
	 *
	 * @throws IllegalArgumentException
	 * 	If the exported grammar is invalid or not present in this set.
	 */
	public String loadedFrom(String grammarName) {
		/* Check arguments. */
		if (grammarName == null) {
			throw new NullPointerException("Grammar name must not be null");
		} else if (grammarName.equals("")) {
			throw new IllegalArgumentException("The empty string is not a valid grammar name");
		} else if (grammarName.equals("unknown")) {
			return grammarName;
		} else if (!grammars.containsKey(grammarName)) {
			String msg = String.format("No grammar with name '%s' defined", grammarName);
			throw new IllegalArgumentException(msg);
		}

		return loadedFrom.getOrDefault(grammarName, "Unknown");
	}

	/**
	 * Get the names of all the grammars in this set.
	 *
	 * @return
	 * 	The names of all the grammars in this set.
	 */
	public Set<String> getGrammars() {
		return grammars.keySet();
	}

	/**
	 * Get the names of all the exported rules in this set.
	 *
	 * @return
	 * 	The names of all the exported rules in this set.
	 */
	public Set<String> getExportedRules() {
		return exportedRules.keySet();
	}

	/**
	 * Load a grammar set from a configuration file.
	 *
	 * @param cfgFile
	 * 	The configuration file to load from.
	 *
	 * @return
	 * 	The grammar set created by the configuration file.
	 *
	 * @throws IOException
	 * 	If something goes wrong during configuration loading.
	 */
	public static RGrammarSet fromConfigFile(Path cfgFile) throws IOException {
		/* The grammar set to hand back. */
		RGrammarSet set = new RGrammarSet();

		/* Get the directory that contains the config file. */
		Path cfgParent = cfgFile.getParent();

		try(Scanner scn = new Scanner(cfgFile)) {
			/* Execute lines from the configuration file. */
			while (scn.hasNextLine()) {
				String ln = scn.nextLine().trim();

				/* Ignore blank/comment lines. */
				if (ln.equals("")) continue;

				if (ln.startsWith("#")) continue;

				/* Handle mixed whitespace. */
				ln = ln.replaceAll("\\s+", " ");

				/* 
				 * Get the place where the name of the grammar
				 * ends. 
				 */
				int nameIdx = ln.indexOf(" ");
				if (nameIdx == -1) {
					throw new GrammarException("Must specify a name for a loaded grammar");
				}

				/* Name and path of grammar. */
				String name = ln.substring(0, nameIdx);
				Path path   = Paths.get(ln.substring(nameIdx).trim());

				/*
				 * Convert from configuration relative path to
				 * absolute path.
				 */
				Path convPath = cfgParent.resolve(path.toString());

				if(Files.isDirectory(convPath)) {
					/* @TODO implement subset grammars */
					throw new GrammarException("Sub-grammar sets aren't implemented yet");
				} else if (convPath.getFileName().toString().endsWith(".gram")) {
					/* Load grammar file. */
					try {

						BufferedReader fis = Files.newBufferedReader(convPath);
						RGrammar gram = RGrammarParser.readGrammar(fis);

						fis.close();

						/* Add grammar to the set. */
						set.addGrammar(name, gram);

						/*
						 * Mark where the grammar came
						 * from. 
						 */
						set.loadedFrom.put(name, path.toString());
					} catch (GrammarException gex) {
						String msg = String.format("Error loading file '%s'", path);
						throw new GrammarException(msg, gex);
					}
				} else {
					String msg = String.format("Unrecognized file type '%s'", convPath.getFileName());
					throw new GrammarException(msg);
				}
			}
		}

		return set;
	}
}