diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 14:06:13 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 14:06:13 -0400 |
| commit | 3cb61c2aede328e2c302f8cfd030478727fca66c (patch) | |
| tree | 11465e2264325338cfc65ccdc09e142c60324d2a /RGens/src/main/java/bjc/rgens/newparser | |
| parent | 2f84d6ae03819960e6adeb6f8d1638470a4f3452 (diff) | |
General cleanup
Removes the old grammar stuff.
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/newparser')
4 files changed, 126 insertions, 125 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java index 3214ea1..5f798aa 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java +++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java @@ -420,9 +420,9 @@ public class CaseElement { int secondNum = Integer.parseInt(rawRange.substring(rawRange.lastIndexOf('.') + 1)); return new CaseElement(RANGE, firstNum, secondNum); - } else { - return new CaseElement(RULEREF, csepart); } + + return new CaseElement(RULEREF, csepart); } else { return new CaseElement(LITERAL, csepart); } diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java index b7253db..5f1c3aa 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java @@ -33,6 +33,9 @@ public class RGrammar { DIST = LevenshteinDistance.getDefaultInstance(); } + public LevenshteinMetric() { + } + @Override public int distance(String x, String y) { return DIST.apply(x, y); @@ -45,33 +48,30 @@ public class RGrammar { public Map<String, String> vars; - public GenerationState(StringBuilder contents, Random rnd, Map<String, String> vs) { - this.contents = contents; - this.rnd = rnd; - this.vars = vs; + public GenerationState(StringBuilder cont, Random rand, Map<String, String> vs) { + contents = cont; + rnd = rand; + vars = vs; } } private static Pattern NAMEVAR_PATTERN = Pattern.compile("\\$(\\w+)"); - private Map<String, Rule> rules; - - private Map<String, RGrammar> importRules; - - private Set<String> exportRules; - - private String initialRule; + private Map<String, Rule> rules; + private Map<String, RGrammar> importRules; + private Set<String> exportRules; + private String initialRule; private BkTreeSearcher<String> ruleSearcher; /** * Create a new randomized grammar using the specified set of rules. * - * @param rules + * @param ruls * The rules to use. */ - public RGrammar(Map<String, Rule> rules) { - this.rules = rules; + public RGrammar(Map<String, Rule> ruls) { + rules = ruls; } /** @@ -134,15 +134,15 @@ public class RGrammar { public String generate(String startRule, Random rnd, Map<String, String> vars) { String fromRule = startRule; - if(startRule == null) { - if(initialRule == null) { + if (startRule == null) { + if (initialRule == null) { throw new GrammarException( "Must specify a start rule for grammars with no initial rule"); - } else { - fromRule = initialRule; } + + fromRule = initialRule; } else { - if(startRule.equals("")) { + if (startRule.equals("")) { throw new GrammarException("The empty string is not a valid rule name"); } } @@ -161,16 +161,16 @@ public class RGrammar { */ private void generateCase(RuleCase start, GenerationState state) { try { - switch(start.type) { + switch (start.type) { case NORMAL: - for(CaseElement elm : start.getElements()) { + for (CaseElement elm : start.getElements()) { generateElement(elm, state); } break; default: throw new GrammarException(String.format("Unknown case type '%s'", start.type)); } - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException(String.format("Error in generating case (%s)", start), gex); } } @@ -180,7 +180,7 @@ public class RGrammar { */ private void generateElement(CaseElement elm, GenerationState state) { try { - switch(elm.type) { + switch (elm.type) { case LITERAL: state.contents.append(elm.getLiteral()); state.contents.append(" "); @@ -207,7 +207,7 @@ public class RGrammar { default: throw new GrammarException(String.format("Unknown element type '%s'", elm.type)); } - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException(String.format("Error in generating case element (%s)", elm), gex); } } @@ -218,11 +218,11 @@ public class RGrammar { private void generateExpVarDef(String name, String defn, GenerationState state) { GenerationState newState = new GenerationState(new StringBuilder(), state.rnd, state.vars); - if(rules.containsKey(defn)) { + if (rules.containsKey(defn)) { RuleCase destCase = rules.get(defn).getCase(); generateCase(destCase, newState); - } else if(importRules.containsKey(defn)) { + } else if (importRules.containsKey(defn)) { RGrammar destGrammar = importRules.get(defn); String res = destGrammar.generate(defn, state.rnd, state.vars); @@ -237,7 +237,7 @@ public class RGrammar { /* * Generate a variable definition. */ - private void generateVarDef(String name, String defn, GenerationState state) { + private static void generateVarDef(String name, String defn, GenerationState state) { state.vars.put(name, defn); } @@ -249,34 +249,34 @@ public class RGrammar { GenerationState newState = new GenerationState(new StringBuilder(), state.rnd, state.vars); - if(refersTo.contains("$")) { + if (refersTo.contains("$")) { /* * Parse variables */ String refBody = refersTo.substring(1, refersTo.length() - 1); - if(refBody.contains("-")) { + if (refBody.contains("-")) { /* - * Handle dependant rule names. + * Handle dependent rule names. */ StringBuffer nameBuffer = new StringBuffer(); Matcher nameMatcher = NAMEVAR_PATTERN.matcher(refBody); - while(nameMatcher.find()) { + while (nameMatcher.find()) { String var = nameMatcher.group(1); - if(!state.vars.containsKey(var)) { + if (!state.vars.containsKey(var)) { throw new GrammarException( String.format("No variable '%s' defined", var)); } String name = state.vars.get(var); - if(name.contains(" ")) { + if (name.contains(" ")) { throw new GrammarException( "Variables substituted into names cannot contain spaces"); - } else if(name.equals("")) { + } else if (name.equals("")) { throw new GrammarException( "Variables substituted into names cannot be empty"); } @@ -291,13 +291,13 @@ public class RGrammar { /* * Handle string references. */ - if(refBody.equals("$")) { + if (refBody.equals("$")) { throw new GrammarException("Cannot refer to unnamed variables"); } String key = refBody.substring(1); - if(!state.vars.containsKey(key)) { + if (!state.vars.containsKey(key)) { throw new GrammarException(String.format("No variable '%s' defined", key)); } @@ -307,16 +307,16 @@ public class RGrammar { } } - if(rules.containsKey(refersTo)) { + if (rules.containsKey(refersTo)) { RuleCase cse = rules.get(refersTo).getCase(state.rnd); generateCase(cse, newState); - } else if(importRules.containsKey(refersTo)) { + } else if (importRules.containsKey(refersTo)) { RGrammar dst = importRules.get(refersTo); newState.contents.append(dst.generate(refersTo, state.rnd, state.vars)); } else { - if(ruleSearcher != null) { + if (ruleSearcher != null) { Set<Match<? extends String>> results = ruleSearcher.search(refersTo, MAX_DISTANCE); String[] resArray = results.stream().map((mat) -> mat.getMatch()) @@ -324,12 +324,12 @@ public class RGrammar { throw new GrammarException(String.format("No rule '%s' defined (perhaps you meant %s?)", refersTo, StringUtils.toEnglishList(resArray, false))); - } else { - throw new GrammarException(String.format("No rule '%s' defined", refersTo)); } + + throw new GrammarException(String.format("No rule '%s' defined", refersTo)); } - if(refersTo.contains("+")) { + if (refersTo.contains("+")) { /* * Rule names with pluses in them get space-flattened */ @@ -351,27 +351,27 @@ public class RGrammar { /** * Set the initial rule of this grammar. * - * @param initialRule + * @param initRule * The initial rule of this grammar, or null to say there * is no initial rule. */ - public void setInitialRule(String initialRule) { + public void setInitialRule(String initRule) { /* * Passing null nulls our initial rule. */ - if(initialRule == null) { + if (initRule == null) { this.initialRule = null; return; } - if(initialRule.equals("")) { + if (initRule.equals("")) { throw new GrammarException("The empty string is not a valid rule name"); - } else if(!rules.containsKey(initialRule)) { + } else if (!rules.containsKey(initRule)) { throw new GrammarException( - String.format("No rule '%s' local to this grammar defined.", initialRule)); + String.format("No rule '%s' local to this grammar defined.", initRule)); } - this.initialRule = initialRule; + initialRule = initRule; } /** @@ -384,8 +384,8 @@ public class RGrammar { public Set<Rule> getExportedRules() { Set<Rule> res = new HashSet<>(); - for(String rname : exportRules) { - if(!rules.containsKey(rname)) { + for (String rname : exportRules) { + if (!rules.containsKey(rname)) { throw new GrammarException(String.format("No rule '%s' local to this grammar defined", initialRule)); } @@ -393,7 +393,7 @@ public class RGrammar { res.add(rules.get(rname)); } - if(initialRule != null) { + if (initialRule != null) { res.add(rules.get(initialRule)); } @@ -403,11 +403,11 @@ public class RGrammar { /** * Set the rules exported by this grammar. * - * @param exportRules + * @param exportedRules * The rules exported by this grammar. */ - public void setExportedRules(Set<String> exportRules) { - this.exportRules = exportRules; + public void setExportedRules(Set<String> exportedRules) { + exportRules = exportedRules; } /** diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java index 80e1c2f..cb76946 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java @@ -42,7 +42,7 @@ public class RGrammarParser { pragmas.put("initial-rule", (body, build, level) -> { int sep = body.indexOf(' '); - if(sep != -1) { + if (sep != -1) { throw new GrammarException( "Initial-rule pragma takes only one argument, the name of the initial rule"); } @@ -53,7 +53,7 @@ public class RGrammarParser { pragmas.put("export-rule", (body, build, level) -> { String[] exports = body.split(" "); - for(String export : exports) { + for (String export : exports) { build.addExport(export); } }); @@ -61,37 +61,37 @@ public class RGrammarParser { pragmas.put("suffix-with", (body, build, level) -> { String[] parts = body.trim().split(" "); - if(parts.length != 2) { + if (parts.length != 2) { throw new GrammarException("Suffix-with pragma takes two arguments," + " the name of the rule to suffix, then what to suffix it with"); - } else { - String name = parts[0]; - String suffix = parts[1]; + } - if(name.equals("")) { - throw new GrammarException("The empty string is not a valid rule name"); - } + String name = parts[0]; + String suffix = parts[1]; - build.suffixWith(name, suffix); + if (name.equals("")) { + throw new GrammarException("The empty string is not a valid rule name"); } + + build.suffixWith(name, suffix); }); - + pragmas.put("prefix-with", (body, build, level) -> { String[] parts = body.trim().split(" "); - if(parts.length != 2) { + if (parts.length != 2) { throw new GrammarException("Prefix-with pragma takes two arguments," + " the name of the rule to prefix, then what to prefix it with"); - } else { - String name = parts[0]; - String prefix = parts[1]; + } - if(name.equals("")) { - throw new GrammarException("The empty string is not a valid rule name"); - } + String name = parts[0]; + String prefix = parts[1]; - build.prefixWith(name, prefix); + if (name.equals("")) { + throw new GrammarException("The empty string is not a valid rule name"); } + + build.prefixWith(name, prefix); }); } @@ -106,9 +106,9 @@ public class RGrammarParser { * @throws GrammarException * Thrown if the grammar has a syntax error. */ - public RGrammar readGrammar(InputStream is) throws GrammarException { - try(BlockReader reader = new BlockReader(TOPLEVEL_BLOCK_DELIM, new InputStreamReader(is))) { - if(!reader.hasNextBlock()) { + public static RGrammar readGrammar(InputStream is) throws GrammarException { + try (BlockReader reader = new BlockReader(TOPLEVEL_BLOCK_DELIM, new InputStreamReader(is))) { + if (!reader.hasNextBlock()) { throw new GrammarException("At least one top-level block must be present"); } @@ -120,11 +120,11 @@ public class RGrammarParser { }); return build.toRGrammar(); - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException(String.format("Error in block (%s)", reader.getBlock()), gex); } - } catch(Exception ex) { + } catch (Exception ex) { throw new GrammarException(String.format("Unknown error handling block"), ex); } } @@ -137,30 +137,30 @@ public class RGrammarParser { /* * Handles an arbitrary block. */ - private void handleBlock(RGrammarBuilder build, String block, int level) throws GrammarException { + private static void handleBlock(RGrammarBuilder build, String block, int level) throws GrammarException { /* * Discard empty blocks */ - if(block.equals("")) return; - if(block.equals("\n")) return; - if(block.equals("\r\n")) return; + if (block.equals("")) return; + if (block.equals("\n")) return; + if (block.equals("\r\n")) return; int typeSep = block.indexOf(' '); - if(typeSep == -1) { + if (typeSep == -1) { throw new GrammarException( "A block must start with a type, followed by a space, then the rest of the block"); } String blockType = block.substring(0, typeSep); - if(blockType.equalsIgnoreCase("pragma")) { + if (blockType.equalsIgnoreCase("pragma")) { handlePragmaBlock(block, build, level); - } else if(blockType.startsWith("[")) { + } else if (blockType.startsWith("[")) { handleRuleBlock(block, build, level); - } else if(blockType.equalsIgnoreCase("where")) { + } else if (blockType.equalsIgnoreCase("where")) { handleWhereBlock(block, build, level); - } else if(blockType.equalsIgnoreCase("#")) { + } else if (blockType.equalsIgnoreCase("#")) { /* * Comment block. */ @@ -173,8 +173,8 @@ public class RGrammarParser { /* * Handle reading a block of pragmas. */ - private void handlePragmaBlock(String block, RGrammarBuilder build, int level) throws GrammarException { - try(BlockReader pragmaReader = new BlockReader(String.format(TMPL_PRAGMA_BLOCK_DELIM, level), + private static void handlePragmaBlock(String block, RGrammarBuilder build, int level) throws GrammarException { + try (BlockReader pragmaReader = new BlockReader(String.format(TMPL_PRAGMA_BLOCK_DELIM, level), new StringReader(block))) { try { pragmaReader.forEachBlock((pragma) -> { @@ -182,7 +182,7 @@ public class RGrammarParser { int pragmaSep = pragmaContents.indexOf(' '); - if(pragmaSep == -1) { + if (pragmaSep == -1) { throw new GrammarException( "A pragma invocation must consist of the word pragma," + " followed by a space, then the body of the pragma"); @@ -191,20 +191,20 @@ public class RGrammarParser { String pragmaLeader = pragmaContents.substring(0, pragmaSep); String pragmaBody = pragmaContents.substring(pragmaSep + 1); - if(!pragmaLeader.equalsIgnoreCase("pragma")) { - throw new GrammarException( - String.format("Illegal line leader in pragma block: '%s'", - pragmaLeader)); - } else { - handlePragma(pragmaBody, build, level); + if (!pragmaLeader.equalsIgnoreCase("pragma")) { + throw new GrammarException(String.format( + "Illegal line leader in pragma block: '%s'", + pragmaLeader)); } + + handlePragma(pragmaBody, build, level); }); - } catch(GrammarException gex) { + } catch (GrammarException gex) { Block pragma = pragmaReader.getBlock(); throw new GrammarException(String.format("Error in pragma: (%s)", pragma), gex); } - } catch(Exception ex) { + } catch (Exception ex) { throw new GrammarException("Unknown error handling pragma block", ex); } } @@ -212,18 +212,18 @@ public class RGrammarParser { /* * Handle an individual pragma in a block. */ - private void handlePragma(String pragma, RGrammarBuilder build, int level) throws GrammarException { + private static void handlePragma(String pragma, RGrammarBuilder build, int level) throws GrammarException { int bodySep = pragma.indexOf(' '); - if(bodySep == -1) bodySep = pragma.length(); + if (bodySep == -1) bodySep = pragma.length(); String pragmaName = pragma.substring(0, bodySep); String pragmaBody = pragma.substring(bodySep + 1); - if(pragmas.containsKey(pragmaName)) { + if (pragmas.containsKey(pragmaName)) { try { pragmas.get(pragmaName).accept(pragmaBody, build, level); - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException(String.format("Error in '%s' pragma", pragmaName), gex); } } else { @@ -234,11 +234,12 @@ public class RGrammarParser { /* * Handle a block of a rule declaration and one or more cases. */ - private void handleRuleBlock(String ruleBlock, RGrammarBuilder build, int level) throws GrammarException { - try(BlockReader ruleReader = new BlockReader(String.format(TMPL_RULEDECL_BLOCK_DELIM, level), + private static void handleRuleBlock(String ruleBlock, RGrammarBuilder build, int level) + throws GrammarException { + try (BlockReader ruleReader = new BlockReader(String.format(TMPL_RULEDECL_BLOCK_DELIM, level), new StringReader(ruleBlock))) { try { - if(ruleReader.hasNextBlock()) { + if (ruleReader.hasNextBlock()) { /* * Rule with a declaration followed by * multiple cases. @@ -263,11 +264,11 @@ public class RGrammarParser { build.finishRule(); } - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException( String.format("Error in rule case (%s)", ruleReader.getBlock()), gex); } - } catch(Exception ex) { + } catch (Exception ex) { throw new GrammarException("Unknown error handling rule block", ex); } } @@ -275,17 +276,17 @@ public class RGrammarParser { /* * Handle a rule declaration and its initial case. */ - private void handleRuleDecl(RGrammarBuilder build, String declContents) { + private static void handleRuleDecl(RGrammarBuilder build, String declContents) { int declSep = declContents.indexOf("\u2192"); - if(declSep == -1) { + if (declSep == -1) { /* * TODO remove support for the old syntax when all of * the files are converted. */ declSep = declContents.indexOf(' '); - if(declSep == -1) { + if (declSep == -1) { throw new GrammarException( "A rule must be given at least one case in its declaration, and" + "seperated from that case by \u2192"); @@ -298,7 +299,7 @@ public class RGrammarParser { String ruleName = declContents.substring(0, declSep).trim(); String ruleBody = declContents.substring(declSep + 1).trim(); - if(ruleName.equals("")) { + if (ruleName.equals("")) { throw new GrammarException("The empty string is not a valid rule name"); } @@ -310,16 +311,16 @@ public class RGrammarParser { /* * Handle a single case of a rule. */ - private void handleRuleCase(String cse, RGrammarBuilder build) { + private static void handleRuleCase(String cse, RGrammarBuilder build) { build.beginCase(); - for(String csepart : cse.split(" ")) { + for (String csepart : cse.split(" ")) { String partToAdd = csepart.trim(); /* * Ignore empty parts */ - if(partToAdd.equals("")) continue; + if (partToAdd.equals("")) continue; build.addCasePart(partToAdd); } @@ -330,19 +331,18 @@ public class RGrammarParser { /* * Handle a where block (a block with local rules). */ - @SuppressWarnings("unused") - private void handleWhereBlock(String block, RGrammarBuilder build, int level) throws GrammarException { - try(BlockReader whereReader = new BlockReader("", new StringReader(block))) { + private static void handleWhereBlock(String block, RGrammarBuilder build, int level) throws GrammarException { + try (BlockReader whereReader = new BlockReader("", new StringReader(block))) { try { /* * TODO decide syntax for where blocks. */ - } catch(GrammarException gex) { + } catch (GrammarException gex) { throw new GrammarException( String.format("Error in where block (%s)", whereReader.getBlock()), gex); } - } catch(Exception ex) { + } catch (Exception ex) { throw new GrammarException("Unknown error in where block", ex); } } diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java index 1cc8f92..d86b18c 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java @@ -219,8 +219,6 @@ public class RGrammarSet { public static RGrammarSet fromConfigFile(Path cfgFile) throws IOException { RGrammarSet set = new RGrammarSet(); - RGrammarParser parser = new RGrammarParser(); - Path cfgParent = cfgFile.getParent(); try(Scanner scn = new Scanner(cfgFile)) { @@ -270,7 +268,10 @@ public class RGrammarSet { * Load grammar files. */ try { - RGrammar gram = parser.readGrammar(new FileInputStream(fle)); + FileInputStream fis = new FileInputStream(fle); + RGrammar gram = RGrammarParser.readGrammar(fis); + fis.close(); + set.addGrammar(name, gram); set.loadedFrom.put(name, path.toString()); |
