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

import bjc.utils.esodata.MapSet;
import bjc.utils.data.IPair;
import bjc.utils.data.Pair;
import bjc.utils.ioutils.ReportWriter;

import java.io.IOException;
import java.io.StringWriter;

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

import static bjc.rgens.parser.RGrammarLogging.*;

/**
 * The current state during generation.
 *
 * @author Ben Culkin
 */
public class GenerationState {
	/* The current output. */
	private ReportWriter contents;
	/** The RNG. */
	public Random rnd;

	/** The current grammar. */
	public RGrammar gram;

	/* The rules of the grammar. */
	private Map<String, Rule> rules;
	/* The rules imported from other grammars. */
	private Map<String, Rule> importRules;

	/** The current set of variables. */
	private MapSet<String, String> vars;
	private MapSet<String, Rule> rlVars;

	/* The base random number generator. */
	private static final Random BASE = new Random();

	/**
	 * Create a new generation state.
	 * 
	 * @param rw
	 * 	The place to write output to.
	 *
	 * @param rand
	 * 	The random number generator to use.
	 *
	 * @param vs
	 * 	The normal variables to use.
	 * 
	 * @param rvs
	 * 	The rule variables to use.
	 *
	 * @param gram
	 * 	The grammar we are generating from.
	 */
	public GenerationState(ReportWriter rw, Random rand, Map<String, String> vs,
			Map<String, Rule> rvs, RGrammar gram) {
		vars   = new MapSet<>();
		rlVars = new MapSet<>();

		contents = rw;
		rnd      = rand;

		vars.setPutMap(gram.name, vs);
		rlVars.setPutMap(gram.name, rvs);

		this.gram = gram;
	
		this.rules = gram.getRules();
		this.importRules = gram.getImportRules();
	}

	/**
	 * Create a new generation state, from a given grammar.
	 *
	 * @param gram
	 * 	The grammar to generate from.
	 *
	 * @return
	 * 	A new generation state, with the provided parameters.
	 */
	public static GenerationState fromGrammar(RGrammar gram) {
		return fromGrammar(BASE, gram);
	}

	/**
	 * Create a new generation state, using a provided random generator and
	 * grammar.
	 *
	 * @param rand
	 * 	The random number generator to use.
	 *
	 * @param gram
	 * 	The grammar to generate from.
	 *
	 * @return
	 * 	A new generation state, with the provided parameters.
	 */
	public static GenerationState fromGrammar(Random rand, RGrammar gram) {
		ReportWriter rw = new ReportWriter(new StringWriter());

		return new GenerationState(rw, rand, new HashMap<>(), new HashMap<>(), gram);
	}

	/**
	 * Swap the grammar for the state.
	 *
	 * @param gram
	 * 	The grammar to swap to.
	 */
	public void swapGrammar(RGrammar gram) {
		if(this.gram == gram) return;

		this.gram = gram;

		rules = gram.getRules();
		
		importRules = gram.getImportRules();

		vars.setCreateMap(gram.name);
		rlVars.setCreateMap(gram.name);
	}

	/**
	 * Create a copy of this generation state, writing into a fresh buffer.
	 *
	 * @return A generation state that is a copy of this one, but writes into a
	 * fresh buffer.
	 */
	public GenerationState newBuf() {
		// @NOTE 9/5/18
		//
		// Not sure if this is the right thing to do or not.
		//
		// I suppose it'll only matter once we actually start using the
		// features of ReportWriter, instead of just using the basic
		// Writer functionality
		ReportWriter rw = contents.duplicate(new StringWriter());

		return new GenerationState(rw, rnd, vars, rlVars, gram);
	}

	/*
	 * @TODO 6/5/18 Ben Culkin :ImportRefactor
	 * 
	 * Change this so that imports in almost all cases have to specify where
	 * they are importing the rule from, so as to make it clear which rules
	 * are imported, and which aren't
	 */
	/**
	 * Find an instance of a rule.
	 *
	 * @param ruleName
	 * 	The name of the rule to look for.
	 *
	 * @param allowImports
	 * 	Whether or not to look for imported rules.
	 *
	 * @return The rule instance you were looking for, or null if none by that
	 * name happen to exist.
	 */
	public Rule findRule(String ruleName, boolean allowImports) {
		if(rules.containsKey(ruleName)) {
			return rules.get(ruleName);
		}

		if(allowImports) return findImport(ruleName);

		return null;
	}

	/**
	 * Find an instance of an imported rule.
	 *
	 * @param ruleName
	 * 	The name of the rule to look for.
	 *
	 * @return The rule instance you were looking for, or null if none by that
	 * name happen to exist.
	 */
	public Rule findImport(String ruleName) {
		if(importRules.containsKey(ruleName)) {
			return  importRules.get(ruleName);
		}

		return null;
	}

	/**
	 * Define a normal variable.
	 *
	 * @param name
	 * 	The name to give the variable.
	 *
	 * @param val
	 * 	The value to give the variable.
	 */
	public void defineVar(String name, String val) {
		if(vars.containsKey(name)) 
			warn("Shadowing variable %s with value %s (old value %s)",
					name, val, vars.get(name));
		else if (gram.autoVars.containsKey(name))
			warn("Shadowing autovariable %s with value %s (defn. %s)",
					name, val, gram.autoVars.get(name));

		vars.put(name, val);
	}

	/**
	 * Define a rule variable.
	 *
	 * @param name
	 * 	The name to give the variable.
	 *
	 * @param rle
	 * 	The value to give the variable.
	 */
	public void defineRuleVar(String name, Rule rle) {
		if(rlVars.containsKey(name))
			warn("Shadowing rule variable %s with value %s (old value %s)",
					name, rlVars.get(name), rle);
		else if (gram.autoRlVars.containsKey(name))
			warn("Shadowing rule autovariable %s with value %s (defn. %s)",
					name, rle, gram.autoRlVars.get(name));

		rlVars.put(name, rle);
	}

	/**
	 * Find a variable.
	 *
	 * @param name
	 * 	The variable to look for.
	 *
	 * @return The value of the variable.
	 *
	 * @throws GrammarException If the variable isn't found, or if it was an
	 * auto-variable that failed to generate succesfully.
	 */
	public String findVar(String name) {
		if(!vars.containsKey(name)) 
			if(gram.autoVars.containsKey(name)) {
				gram.autoVars.get(name).generate(this);
			} else {
				throw new GrammarException(String.format("Variable %s not defined", name));
			}

		return vars.get(name);
	}

	/**
	 * Find a rule variable.
	 *
	 * @param name
	 * 	The variable to look for.
	 *
	 * @return The value of the variable.
	 *
	 * @throws GrammarException If the variable isn't found, or if it was an
	 * auto-variable that failed to generate succesfully.
	 */
	public Rule findRuleVar(String name) {
		if(!rlVars.containsKey(name))
			if(gram.autoRlVars.containsKey(name)) {
				gram.autoRlVars.get(name).generate(this);
			} else {
				throw new GrammarException(String.format("Rule variable %s not defined", name));
			}

		return rlVars.get(name);
	}

	/**
	 * Append the given string to our output.
	 */
	public void appendContents(String strang) {
		try {
			contents.write(strang);
		} catch (IOException ioex) {
			throw new GrammarException("I/O Error", ioex);
		}
	}

	/**
	 * Replace the current contents of our output with the given string.
	 *
	 * @param strang
	 * 	The string to replace the output with.
	 */
	public void setContents(String strang) {
		// @NOTE 9/5/18
		//
		// This raises some interesting questions as to what the
		// appropriate behavior is.
		//
		// For now, I'm simply going to say to go with a StringWriter
		// and then write the contents to that, but I am not sure that
		// that is the right way to go about it.
		contents = contents.duplicate(new StringWriter());

		try {
			contents.write(strang);
		} catch (IOException ioex) {
			throw new GrammarException("I/O Error", ioex);
		}
	}

	/**
	 * Get the report writer that we use for our input.
	 *
	 * @return The report writer that we write stuff &amp; things to.
	 */
	public ReportWriter getWriter() {
		return contents;
	}

	/**
	 * Get our output as a string.
	 */
	public String getContents() {
		return contents.toString();
	}

	/**
	 * Execute a find/replace on our output.
	 *
	 * @param find
	 * 	The pattern to look for
	 *
	 * @param replace
	 * 	The string to replace occurances of 'find' with.
	 */
	public void findReplaceContents(String find, String replace) {
		setContents(getContents().replaceAll(find, replace));
	}

	/**
	 * Clear out our contents.
	 */
	public void clearContents() {
		contents = contents.duplicate(new StringWriter());
	}
}