diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-22 19:43:24 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-22 19:43:24 -0400 |
| commit | 46ec514ec487c10aa61a2c36be30b5370a5a0189 (patch) | |
| tree | feb0837717e363a60eb73ff310232b23c3e80beb /RGens/src/main/java | |
| parent | c22e1fb365c5d434fb57d92fd0269bc603296240 (diff) | |
Fix assorted bugs
Diffstat (limited to 'RGens/src/main/java')
4 files changed, 114 insertions, 14 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java index 362100f..af9ac1b 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java +++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java @@ -38,7 +38,7 @@ public class CaseElement { EXPVARDEF; } - private static final String SPECIAL_CASELEM = "\\{[^}]\\}"; + private static final String SPECIAL_CASELEM = "\\{[^}]+\\}"; private static final String REFER_CASELEM = "\\[[^\\]]+\\]"; private static final String RANGE_CASELM = "\\[\\d+\\.\\.\\d+\\]"; diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java index 0fcff68..b7253db 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java @@ -112,7 +112,7 @@ public class RGrammar { * @return A possible string from the grammar. */ public String generate(String startRule) { - return generate(startRule, new Random()); + return generate(startRule, new Random(), new HashMap<>()); } /** @@ -126,9 +126,12 @@ public class RGrammar { * @param rnd * The random number generator to use. * + * @param vars + * The set of variables to use. + * * @return A possible string from the grammar. */ - public String generate(String startRule, Random rnd) { + public String generate(String startRule, Random rnd, Map<String, String> vars) { String fromRule = startRule; if(startRule == null) { @@ -148,9 +151,7 @@ public class RGrammar { StringBuilder contents = new StringBuilder(); - HashMap<String, String> scope = new HashMap<>(); - - generateCase(start, new GenerationState(contents, rnd, scope)); + generateCase(start, new GenerationState(contents, rnd, vars)); return contents.toString(); } @@ -223,9 +224,9 @@ public class RGrammar { generateCase(destCase, newState); } else if(importRules.containsKey(defn)) { RGrammar destGrammar = importRules.get(defn); - RuleCase destCase = destGrammar.rules.get(defn).getCase(); + String res = destGrammar.generate(defn, state.rnd, state.vars); - destGrammar.generateCase(destCase, newState); + newState.contents.append(res); } else { throw new GrammarException(String.format("No rule '%s' defined", defn)); } @@ -285,7 +286,7 @@ public class RGrammar { nameMatcher.appendTail(nameBuffer); - refersTo = nameBuffer.toString(); + refersTo = "[" + nameBuffer.toString() + "]"; } else { /* * Handle string references. @@ -301,9 +302,9 @@ public class RGrammar { } state.contents.append(state.vars.get(key)); - } - refersTo = refBody; + return; + } } if(rules.containsKey(refersTo)) { diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java index fbcd8f7..1cc8f92 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java @@ -17,10 +17,26 @@ import java.util.Set; * */ 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. */ @@ -28,6 +44,9 @@ public class RGrammarSet { grammars = new HashMap<>(); exportedRules = new HashMap<>(); + + exportFrom = new HashMap<>(); + loadedFrom = new HashMap<>(); } /** @@ -55,6 +74,7 @@ public class RGrammarSet { for(Rule export : gram.getExportedRules()) { exportedRules.put(export.ruleName, gram); + exportFrom.put(export.ruleName, grammarName); } gram.setImportedRules(exportedRules); @@ -77,7 +97,7 @@ public class RGrammarSet { 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("")) { + } else if(!grammars.containsKey(grammarName)) { throw new IllegalArgumentException( String.format("No grammar with name '%s' found", grammarName)); } @@ -103,13 +123,71 @@ public class RGrammarSet { } else if(exportName.equals("")) { throw new IllegalArgumentException("The empty string is not a valid rule name"); } else if(!exportedRules.containsKey(exportName)) { - throw new IllegalArgumentException(String.format("No export with name '%s' found", exportName)); + throw new IllegalArgumentException( + String.format("No export with name '%s' defined", exportName)); } 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) { + 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)) { + throw new IllegalArgumentException( + String.format("No export with name '%s' defined", exportName)); + } + + 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) { + 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)) { + + throw new IllegalArgumentException( + String.format("No grammar with name '%s' defined", grammarName)); + } + + 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. @@ -194,6 +272,8 @@ public class RGrammarSet { try { RGrammar gram = parser.readGrammar(new FileInputStream(fle)); set.addGrammar(name, gram); + + set.loadedFrom.put(name, path.toString()); } catch(GrammarException gex) { throw new GrammarException( String.format("Error loading file '%s'", path), gex); diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java index 14b29e7..7d8ed9c 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java @@ -24,10 +24,29 @@ public class RGrammarTest { try { RGrammarSet gramSet = RGrammarSet.fromConfigFile(Paths.get(rsc.toURI())); + for(String gramName : gramSet.getGrammars()) { + gramSet.getGrammar(gramName).generateSuggestions(); + } + for(String exportName : gramSet.getExportedRules()) { RGrammar grammar = gramSet.getExportSource(exportName); - grammar.generate(exportName); + for(int i = 0; i < 10; i++) { + try { + grammar.generate(exportName); + } catch(GrammarException gex) { + System.out.println("Error in exported rule " + exportName + + " (loaded from " + + gramSet.loadedFrom(gramSet.exportedFrom(exportName))); + + System.out.println(); + + gex.printStackTrace(); + + System.out.println(); + System.out.println(); + } + } } } catch(IOException ioex) { ioex.printStackTrace(); |
