diff options
Diffstat (limited to 'RGens/src')
6 files changed, 102 insertions, 95 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java index 8a5454b..210686e 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java +++ b/RGens/src/main/java/bjc/rgens/newparser/CaseElement.java @@ -38,6 +38,9 @@ public class CaseElement { EXPVARDEF; } + /* + * Regexps for marking rule types. + */ private static final String SPECIAL_CASELEM = "\\{[^}]+\\}"; private static final String REFER_CASELEM = "\\[[^\\]]+\\]"; private static final String RANGE_CASELM = "\\[\\d+\\.\\.\\d+\\]"; @@ -450,4 +453,4 @@ public class CaseElement { return new CaseElement(LITERAL, csepart); } } -}
\ No newline at end of file +} diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java index 2c389a5..3606d40 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java @@ -80,11 +80,11 @@ public class RGrammar { * Imported rules are checked for rule definitions after local * definitions are checked. * - * @param exportedRules + * @param importedRules * The set of imported rules to use. */ - public void setImportedRules(Map<String, RGrammar> exportedRules) { - importRules = exportedRules; + public void setImportedRules(Map<String, RGrammar> importedRules) { + importRules = importedRules; } /** @@ -95,7 +95,6 @@ public class RGrammar { MutableBkTree<String> ruleSuggester = new MutableBkTree<>(new LevenshteinMetric()); ruleSuggester.addAll(rules.keySet()); - ruleSuggester.addAll(importRules.keySet()); ruleSearcher = new BkTreeSearcher<>(ruleSuggester); @@ -170,10 +169,12 @@ public class RGrammar { break; default: - throw new GrammarException(String.format("Unknown case type '%s'", start.type)); + String msg = String.format("Unknown case type '%s'", start.type); + throw new GrammarException(msg); } } catch (GrammarException gex) { - throw new GrammarException(String.format("Error in generating case (%s)", start), gex); + String msg = String.format("Error in generating case (%s)", start); + throw new GrammarException(msg, gex); } } @@ -212,11 +213,12 @@ public class RGrammar { break; default: - throw new GrammarException(String.format("Unknown element type '%s'", elm.type)); + String msg = String.format("Unknown element type '%s'", elm.type); + throw new GrammarException(msg); } } catch (GrammarException gex) { - throw new GrammarException(String.format("Error in generating case element (%s)", elm), - gex); + String msg = String.format("Error in generating case element (%s)", elm); + throw new GrammarException(msg, gex); } } @@ -237,7 +239,8 @@ public class RGrammar { newState.contents.append(res); } else { - throw new GrammarException(String.format("No rule '%s' defined", defn)); + String msg = String.format("No rule '%s' defined", defn); + throw new GrammarException(msg); } state.vars.put(name, newState.contents.toString()); @@ -277,8 +280,8 @@ public class RGrammar { String var = nameMatcher.group(1); if (!state.vars.containsKey(var)) { - throw new GrammarException( - String.format("No variable '%s' defined", var)); + String msg = String.format("No variable '%s' defined", var); + throw new GrammarException(); } String name = state.vars.get(var); @@ -308,7 +311,8 @@ public class RGrammar { String key = refBody.substring(1); if (!state.vars.containsKey(key)) { - throw new GrammarException(String.format("No variable '%s' defined", key)); + String msg = String.format("No variable '%s' defined", key); + throw new GrammarException(); } state.contents.append(state.vars.get(key)); @@ -332,11 +336,14 @@ public class RGrammar { String[] resArray = results.stream().map((mat) -> mat.getMatch()) .toArray((i) -> new String[i]); - throw new GrammarException(String.format("No rule '%s' defined (perhaps you meant %s?)", - refersTo, StringUtils.toEnglishList(resArray, false))); + String msg = String.format("No rule '%s' defined (perhaps you meant %s?)", + refersTo, StringUtils.toEnglishList(resArray, false)); + + throw new GrammarException(); } - throw new GrammarException(String.format("No rule '%s' defined", refersTo)); + String msg = String.format("No rule '%s' defined", refersTo); + throw new GrammarException(); } if (refersTo.contains("+")) { @@ -377,8 +384,8 @@ public class RGrammar { if (initRule.equals("")) { throw new GrammarException("The empty string is not a valid rule name"); } else if (!rules.containsKey(initRule)) { - throw new GrammarException( - String.format("No rule '%s' local to this grammar defined.", initRule)); + String msg = String.format("No rule '%s' local to this grammar defined.", initRule); + throw new GrammarException(); } initialRule = initRule; @@ -396,8 +403,9 @@ public class RGrammar { for (String rname : exportRules) { if (!rules.containsKey(rname)) { - throw new GrammarException(String.format("No rule '%s' local to this grammar defined", - initialRule)); + String msg = String.format("No rule '%s' local to this grammar defined", + initialRule); + throw new GrammarException(); } res.add(rules.get(rname)); diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java index 9717eee..22e0ca7 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java @@ -20,9 +20,9 @@ public class RGrammarParser { /* * Templates for level-dependent delimiters. */ - private static final String TMPL_PRAGMA_BLOCK_DELIM = "\\R\\t{%d}(?!\\t)"; + private static final String TMPL_PRAGMA_BLOCK_DELIM = "\\R\\t{%d}(?!\\t)"; private static final String TMPL_RULEDECL_BLOCK_DELIM = "\\R\\t\\t{%d}"; - private static final String TMPL_WHERE_BLOCK_DELIM = "\\R\\t{%d}(?:in|end)\\R"; + private static final String TMPL_WHERE_BLOCK_DELIM = "\\R\\t{%d}(?:in|end)\\R"; private static final String TMPL_TOPLEVEL_BLOCK_DELIM = "\\R\\t{%d}\\.?\\R"; /* @@ -40,7 +40,8 @@ public class RGrammarParser { int sep = body.indexOf(' '); if (sep != -1) { - throw new GrammarException("Initial-rule pragma takes only one argument, the name of the initial rule"); + String msg = "Initial-rule pragma takes only one argument, the name of the initial rule"; + throw new GrammarException(msg); } build.setInitialRule(body); @@ -58,36 +59,22 @@ public class RGrammarParser { String[] parts = body.trim().split(" "); 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"); + String msg = "Suffix-with pragma takes two arguments, the name of the rule to suffix, then what to suffix it with"; + throw new GrammarException(msg); } - String name = parts[0]; - String suffix = parts[1]; - - if (name.equals("")) { - throw new GrammarException("The empty string is not a valid rule name"); - } - - build.suffixWith(name, suffix); + build.suffixWith(parts[0], parts[1]); }); pragmas.put("prefix-with", (body, build, level) -> { String[] parts = body.trim().split(" "); 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"); - } - - String name = parts[0]; - String prefix = parts[1]; - - if (name.equals("")) { - throw new GrammarException("The empty string is not a valid rule name"); + String msg = "Prefix-with pragma takes two arguments, the name of the rule to prefix, then what to prefix it with"; + throw new GrammarException(msg); } - build.prefixWith(name, prefix); + build.prefixWith(parts[0], parts[1]); }); } @@ -103,8 +90,9 @@ public class RGrammarParser { * Thrown if the grammar has a syntax error. */ public static RGrammar readGrammar(Reader is) throws GrammarException { - try (BlockReader reader = new SimpleBlockReader(String.format(TMPL_TOPLEVEL_BLOCK_DELIM, - 0), is)) { + String dlm = String.format(TMPL_TOPLEVEL_BLOCK_DELIM, 0); + + try (BlockReader reader = new SimpleBlockReader(dlm, is)) { if (!reader.hasNextBlock()) { throw new GrammarException("At least one top-level block must be present"); } @@ -118,10 +106,11 @@ public class RGrammarParser { return build.toRGrammar(); } catch (GrammarException gex) { - throw new GrammarException(String.format("Error in block (%s)", reader.getBlock()), gex); + String msg = String.format("Error in block (%s)", reader.getBlock()); + throw new GrammarException(msg, gex); } } catch (Exception ex) { - throw new GrammarException(String.format("Unknown error handling block"), ex); + throw new GrammarException("Unknown error handling block", ex); } } @@ -135,22 +124,16 @@ public class RGrammarParser { private static void handleBlock(RGrammarBuilder build, String block, int level) throws GrammarException { /* - * Discard empty blocks + * Discard empty blocks. */ - if (block.equals("")) - return; - - if (block.equals("\n")) - return; - - if (block.equals("\r\n")) + if (block.equals("") || block.matches("\\R")) return; int typeSep = block.indexOf(' '); if (typeSep == -1) { throw new GrammarException( - "A block must start with a type, followed by a space, then the rest of the block"); + "A block must start with a introducer, followed by a space, then the rest of the block"); } String blockType = block.substring(0, typeSep); @@ -164,10 +147,14 @@ public class RGrammarParser { } else if (blockType.equalsIgnoreCase("#")) { /* * Comment block. + * + * @Incomplete Attach these to the grammar builder so + * that they can be re-output during formatting. */ return; } else { - throw new GrammarException(String.format("Unknown block type: '%s'", blockType)); + String msg = String.format("Unknown block type: '%s'", blockType); + throw new GrammarException(msg); } } @@ -176,9 +163,8 @@ public class RGrammarParser { */ private static void handlePragmaBlock(String block, RGrammarBuilder build, int level) throws GrammarException { - try (BlockReader pragmaReader = new SimpleBlockReader(String.format( - TMPL_PRAGMA_BLOCK_DELIM, level), - new StringReader(block))) { + String dlm = String.format(TMPL_PRAGMA_BLOCK_DELIM, level); + try (BlockReader pragmaReader = new SimpleBlockReader(dlm, new StringReader(block))) { try { pragmaReader.forEachBlock((pragma) -> { String pragmaContents = pragma.contents; @@ -186,16 +172,16 @@ public class RGrammarParser { int pragmaSep = pragmaContents.indexOf(' '); 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"); + String msg = "A pragma invocation must consist of the word pragma, followed by a space, then the body of the pragma"; + throw new GrammarException(msg); } 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)); + String msg = String.format("Illegal line leader in pragma block: '%s'", pragmaLeader); + throw new GrammarException(msg); } handlePragma(pragmaBody, build, level); @@ -203,14 +189,15 @@ public class RGrammarParser { } catch (GrammarException gex) { Block pragma = pragmaReader.getBlock(); - throw new GrammarException(String.format("Error in pragma: (%s)", pragma), gex); + String msg = String.format("Error in pragma: (%s)", pragma); + throw new GrammarException(msg, gex); } } catch (Exception ex) { throw new GrammarException("Unknown error handling pragma block", ex); } } - /*msg + /* * Handle an individual pragma in a block. */ private static void handlePragma(String pragma, RGrammarBuilder build, @@ -227,10 +214,12 @@ public class RGrammarParser { try { pragmas.get(pragmaName).accept(pragmaBody, build, level); } catch (GrammarException gex) { - throw new GrammarException(String.format("Error in '%s' pragma", pragmaName), gex); + String msg = String.format("Error in pragma '%s'", pragmaName); + throw new GrammarException(msg, gex); } } else { - throw new GrammarException(String.format("Unknown pragma named '%s'", pragmaName)); + String msg = String.format("Unknown pragma '%s'", pragmaName) + throw new GrammarException(msg); } } @@ -239,9 +228,8 @@ public class RGrammarParser { */ private static void handleRuleBlock(String ruleBlock, RGrammarBuilder build, int level) throws GrammarException { - try (BlockReader ruleReader = new SimpleBlockReader(String.format( - TMPL_RULEDECL_BLOCK_DELIM, level), - new StringReader(ruleBlock))) { + String msg = String.format(TMPL_RULEDECL_BLOCK_DELIM, level); + try (BlockReader ruleReader = new SimpleBlockReaderformat(msg, new StringReader(ruleBlock)) { try { if (ruleReader.hasNextBlock()) { /* @@ -267,8 +255,8 @@ public class RGrammarParser { build.finishRule(); } } catch (GrammarException gex) { - throw new GrammarException(String.format("Error in rule case (%s)", - ruleReader.getBlock()), gex); + String msg = String.format("Error in rule case (%s)", ruleReader.getBlock()); + throw new GrammarException(msg, gex); } } catch (Exception ex) { throw new GrammarException("Unknown error handling rule 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 e1b9823..d1cd1c2 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java @@ -98,8 +98,9 @@ public class RGrammarSet { } else if (grammarName.equals("")) { throw new IllegalArgumentException("The empty string is not a valid grammar name"); } else if (!grammars.containsKey(grammarName)) { - throw new IllegalArgumentException( - String.format("No grammar with name '%s' found", grammarName)); + String msg = String.format("No grammar with name '%s' found", grammarName); + + throw new IllegalArgumentException(msg); } return grammars.get(grammarName); @@ -123,8 +124,8 @@ 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' defined", exportName)); + String msg = String.format("No export with name '%s' defined", exportName) + throw new IllegalArgumentException(msg); } return exportedRules.get(exportName); @@ -150,11 +151,11 @@ 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' defined", exportName)); + String msg = String.format("No export with name '%s' defined", exportName) + throw new IllegalArgumentException(msg); } - return exportFrom.getOrDefault(exportName, "unknown"); + return exportFrom.getOrDefault(exportName, "Unknown"); } /** @@ -179,12 +180,11 @@ public class RGrammarSet { } else if (grammarName.equals("unknown")) { return grammarName; } else if (!grammars.containsKey(grammarName)) { - - throw new IllegalArgumentException( - String.format("No grammar with name '%s' defined", grammarName)); + String msg = String.format("No grammar with name '%s' defined", grammarName); + throw new IllegalArgumentException(); } - return loadedFrom.getOrDefault(grammarName, "unknown"); + return loadedFrom.getOrDefault(grammarName, "Unknown"); } /** @@ -236,7 +236,7 @@ public class RGrammarSet { if (ln.startsWith("#")) continue; /* - * Handle mixed whitespace + * Handle mixed whitespace. */ ln = ln.replaceAll("\\s+", " "); @@ -277,11 +277,12 @@ public class RGrammarSet { set.loadedFrom.put(name, path.toString()); } catch (GrammarException gex) { - throw new GrammarException( - String.format("Error loading file '%s'", path), gex); + String msg = String.format("Error loading file '%s'", path); + throw new GrammarException(msg, gex); } } else { - throw new GrammarException(String.format("Unrecognized file '%s'")); + String msg = String.format("Unrecognized file '%s'"); + throw new GrammarException(msg); } } } diff --git a/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java b/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java index 3f971c3..c22aa98 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java @@ -94,4 +94,4 @@ public class RuleCase { public IList<CaseElement> getElements() { return elementList; } -}
\ No newline at end of file +} diff --git a/RGens/src/main/java/bjc/rgens/text/markov/Markov.java b/RGens/src/main/java/bjc/rgens/text/markov/Markov.java index df8b06c..5b74b8c 100644 --- a/RGens/src/main/java/bjc/rgens/text/markov/Markov.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/Markov.java @@ -1,11 +1,12 @@ -package bjc.rgens.text.markov; +p*ackage bjc.rgens.text.markov; import java.util.Map.Entry; import java.util.*; /** - * Represents a k-character substring. Can give a pseudo-random suffix character - * based on probability. + * Represents a k-character substring. + * + * Can give a pseudo-random suffix character based on probability. * * @author Daniel Friedman (Fall 2011) * @@ -32,7 +33,9 @@ public class Markov { /** * Constructs a Markov object from a given substring and suffix - * character. Suffix characters are stored in a TreeMap. + * character. + * + * Suffix characters are stored in a TreeMap. * * @param substr * the specified substring. @@ -136,7 +139,9 @@ public class Markov { /** * Using probability, returns a pseudo-random character to follow the - * substring. Character possibilities are added to an ArrayList + * substring. + * + * Character possibilities are added to an ArrayList * (duplicates allowed), and a random number from 0 to the last index in * the ArrayList is picked. Since more common suffixes occupy more * indices in the ArrayList, the probability of getting a more common @@ -163,8 +168,10 @@ public class Markov { } Random rand = new Random(); + int retIndex = rand.nextInt(suffixes.size()); ret = suffixes.get(retIndex); + return ret; } |
