From 3cb61c2aede328e2c302f8cfd030478727fca66c Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 25 Mar 2017 14:06:13 -0400 Subject: General cleanup Removes the old grammar stuff. --- .../src/main/java/bjc/rgens/gui/GrammarStudio.java | 26 -- .../java/bjc/rgens/gui/GrammarStudioFrame.java | 30 -- .../main/java/bjc/rgens/newparser/CaseElement.java | 4 +- .../main/java/bjc/rgens/newparser/RGrammar.java | 110 +++---- .../java/bjc/rgens/newparser/RGrammarParser.java | 130 ++++---- .../main/java/bjc/rgens/newparser/RGrammarSet.java | 7 +- RGens/src/main/java/bjc/rgens/parser/.DS_Store | Bin 6148 -> 0 bytes .../java/bjc/rgens/parser/GrammarReaderApp.java | 93 ------ .../java/bjc/rgens/parser/GrammarReaderCLI.java | 61 ---- .../bjc/rgens/parser/PragmaErrorException.java | 21 -- .../java/bjc/rgens/parser/RBGrammarReader.java | 319 ------------------- .../main/java/bjc/rgens/parser/ReaderState.java | 306 ------------------ .../main/java/bjc/rgens/server/CLIArgsParser.java | 141 --------- .../main/java/bjc/rgens/server/GrammarServer.java | 346 --------------------- .../java/bjc/rgens/server/GrammarServerEngine.java | 300 ------------------ .../main/java/bjc/rgens/server/ReaderState.java | 181 ----------- .../java/bjc/rgens/server/ServerGrammarReader.java | 326 ------------------- .../bjc/rgens/text/markov/StandaloneMarkov.java | 10 +- 18 files changed, 130 insertions(+), 2281 deletions(-) delete mode 100644 RGens/src/main/java/bjc/rgens/gui/GrammarStudio.java delete mode 100644 RGens/src/main/java/bjc/rgens/gui/GrammarStudioFrame.java delete mode 100644 RGens/src/main/java/bjc/rgens/parser/.DS_Store delete mode 100644 RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java delete mode 100644 RGens/src/main/java/bjc/rgens/parser/GrammarReaderCLI.java delete mode 100644 RGens/src/main/java/bjc/rgens/parser/PragmaErrorException.java delete mode 100644 RGens/src/main/java/bjc/rgens/parser/RBGrammarReader.java delete mode 100644 RGens/src/main/java/bjc/rgens/parser/ReaderState.java delete mode 100644 RGens/src/main/java/bjc/rgens/server/CLIArgsParser.java delete mode 100644 RGens/src/main/java/bjc/rgens/server/GrammarServer.java delete mode 100644 RGens/src/main/java/bjc/rgens/server/GrammarServerEngine.java delete mode 100644 RGens/src/main/java/bjc/rgens/server/ReaderState.java delete mode 100644 RGens/src/main/java/bjc/rgens/server/ServerGrammarReader.java (limited to 'RGens/src/main') diff --git a/RGens/src/main/java/bjc/rgens/gui/GrammarStudio.java b/RGens/src/main/java/bjc/rgens/gui/GrammarStudio.java deleted file mode 100644 index 46f0d6e..0000000 --- a/RGens/src/main/java/bjc/rgens/gui/GrammarStudio.java +++ /dev/null @@ -1,26 +0,0 @@ -package bjc.rgens.gui; - -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; - -public class GrammarStudio { - public static void main(String[] args) { - try { - UIManager.setLookAndFeel( - UIManager.getSystemLookAndFeelClassName()); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException - | UnsupportedLookAndFeelException ex) { - System.out.println( - "WARNING: Could not use system look and feel"); - ex.printStackTrace(); - } - - GrammarStudioFrame mainFrame = new GrammarStudioFrame(); - - mainFrame.setDefaultCloseOperation( - GrammarStudioFrame.EXIT_ON_CLOSE); - - mainFrame.setVisible(true); - } -} diff --git a/RGens/src/main/java/bjc/rgens/gui/GrammarStudioFrame.java b/RGens/src/main/java/bjc/rgens/gui/GrammarStudioFrame.java deleted file mode 100644 index 17021fc..0000000 --- a/RGens/src/main/java/bjc/rgens/gui/GrammarStudioFrame.java +++ /dev/null @@ -1,30 +0,0 @@ -package bjc.rgens.gui; - -import javax.swing.JDesktopPane; -import javax.swing.JFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; - -import bjc.utils.gui.layout.AutosizeLayout; - -public class GrammarStudioFrame extends JFrame { - private static final long serialVersionUID = 1476431892446002428L; - - public GrammarStudioFrame() { - super("Grammar Studio"); - - setLayout(new AutosizeLayout()); - - JDesktopPane mainPane = new JDesktopPane(); - - JMenuBar menuBar = new JMenuBar(); - - JMenu fileMenu = new JMenu("File"); - fileMenu.setMnemonic('m'); - - menuBar.add(fileMenu); - - setJMenuBar(menuBar); - add(mainPane); - } -} 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 vars; - public GenerationState(StringBuilder contents, Random rnd, Map vs) { - this.contents = contents; - this.rnd = rnd; - this.vars = vs; + public GenerationState(StringBuilder cont, Random rand, Map vs) { + contents = cont; + rnd = rand; + vars = vs; } } private static Pattern NAMEVAR_PATTERN = Pattern.compile("\\$(\\w+)"); - private Map rules; - - private Map importRules; - - private Set exportRules; - - private String initialRule; + private Map rules; + private Map importRules; + private Set exportRules; + private String initialRule; private BkTreeSearcher ruleSearcher; /** * Create a new randomized grammar using the specified set of rules. * - * @param rules + * @param ruls * The rules to use. */ - public RGrammar(Map rules) { - this.rules = rules; + public RGrammar(Map ruls) { + rules = ruls; } /** @@ -134,15 +134,15 @@ public class RGrammar { public String generate(String startRule, Random rnd, Map 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> 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 getExportedRules() { Set 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 exportRules) { - this.exportRules = exportRules; + public void setExportedRules(Set 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()); diff --git a/RGens/src/main/java/bjc/rgens/parser/.DS_Store b/RGens/src/main/java/bjc/rgens/parser/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/RGens/src/main/java/bjc/rgens/parser/.DS_Store and /dev/null differ diff --git a/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java b/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java deleted file mode 100644 index b3beb81..0000000 --- a/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java +++ /dev/null @@ -1,93 +0,0 @@ -package bjc.rgens.parser; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.PrintStream; - -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; - -import bjc.utils.funcutils.ListUtils; -import bjc.utils.gen.WeightedGrammar; -import bjc.utils.gui.SimpleDialogs; -import bjc.utils.gui.awt.SimpleFileDialog; - -/** - * App that reads a grammar from a file and generates results - * - * @author ben - * - */ -public class GrammarReaderApp { - /** - * Main method of class - * - * @param args - * CLI args - */ - public static void main(String[] args) { - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (ClassNotFoundException - | InstantiationException - | IllegalAccessException - | UnsupportedLookAndFeelException e) { - e.printStackTrace(); - System.exit(1); - } - - doSingleFile(); - } - - private static void doSingleFile() { - File gramFile = SimpleFileDialog.getOpenFile(null, "Choose Grammar File", ".gram"); - - WeightedGrammar grammar = null; - - try { - grammar = RBGrammarReader.fromPath(gramFile.toPath()); - } catch (IOException ioex) { - ioex.printStackTrace(); - - System.exit(1); - } - - String initRule = ""; - - if (!grammar.hasInitialRule()) { - grammar.getRuleNames().sort((leftString, rightString) -> { - return leftString.compareTo(rightString); - }); - - initRule = SimpleDialogs.getChoice(null, - "Pick a initial rule", - "Pick a initial rule to generate choices from", - grammar.getRuleNames().toArray(new String[0])); - } else { - initRule = grammar.getInitialRule(); - } - - int count = SimpleDialogs.getWhole(null, - "Enter number of repetitions", - "Enter the number of items to generate from the rule"); - - File outputFile = SimpleFileDialog.getSaveFile(null, "Choose Grammar File"); - - PrintStream outputStream = null; - - try { - outputStream = new PrintStream(outputFile); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - - for (int i = 0; i < count; i++) { - String ruleResult = ListUtils.collapseTokens(grammar.generateListValues(initRule, " ")); - - outputStream.println(ruleResult.replaceAll("\\s+", " ")); - } - - outputStream.close(); - } -} diff --git a/RGens/src/main/java/bjc/rgens/parser/GrammarReaderCLI.java b/RGens/src/main/java/bjc/rgens/parser/GrammarReaderCLI.java deleted file mode 100644 index b8eac35..0000000 --- a/RGens/src/main/java/bjc/rgens/parser/GrammarReaderCLI.java +++ /dev/null @@ -1,61 +0,0 @@ -package bjc.rgens.parser; - -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Paths; - -import bjc.utils.funcutils.ListUtils; -import bjc.utils.gen.WeightedGrammar; - -/** - * App that reads a grammar from a file and generates results - * - * @author ben - * - */ -public class GrammarReaderCLI { - private static WeightedGrammar grammar = null; - - /** - * Main application method - * - * @param args - * CLI args - */ - public static void main(String[] args) { - if (args.length == 0) { - GrammarReaderApp.main(args); - } else { - String fName = args[0]; - - if (fName.equalsIgnoreCase("--help")) { - System.out.println( - "Usage: java -jar GrammarReader.jar "); - System.exit(0); - } - - String ruleName = args[1]; - - try (FileInputStream fStream = new FileInputStream(fName)) { - grammar = RBGrammarReader.fromPath(Paths.get(fName, "")); - } catch (IOException e) { - e.printStackTrace(); - } - - if (ruleName.equalsIgnoreCase("--list-rules")) { - grammar.getRuleNames().forEach(System.out::println); - - System.exit(0); - } - - int rCount = Integer.parseInt(args[2]); - - for (int i = 0; i < rCount; i++) { - String ruleResult = ListUtils.collapseTokens( - grammar.generateListValues(ruleName, " ")); - - System.out.println(ruleResult.replaceAll("\\s+", " ")); - } - } - } -} diff --git a/RGens/src/main/java/bjc/rgens/parser/PragmaErrorException.java b/RGens/src/main/java/bjc/rgens/parser/PragmaErrorException.java deleted file mode 100644 index 05b71d3..0000000 --- a/RGens/src/main/java/bjc/rgens/parser/PragmaErrorException.java +++ /dev/null @@ -1,21 +0,0 @@ -package bjc.rgens.parser; - -/** - * Exception for error executing a pragma - * - * @author ben - * - */ -public class PragmaErrorException extends RuntimeException { - private static final long serialVersionUID = 7245421182038076899L; - - /** - * Create a new exception with the given message - * - * @param message - * The message of the exception - */ - public PragmaErrorException(String message) { - super(message); - } -} diff --git a/RGens/src/main/java/bjc/rgens/parser/RBGrammarReader.java b/RGens/src/main/java/bjc/rgens/parser/RBGrammarReader.java deleted file mode 100644 index 7753d25..0000000 --- a/RGens/src/main/java/bjc/rgens/parser/RBGrammarReader.java +++ /dev/null @@ -1,319 +0,0 @@ -package bjc.rgens.parser; - -import com.mifmif.common.regex.Generex; - -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Path; -import java.util.Random; -import java.util.function.BiFunction; -import java.util.function.Predicate; - -import bjc.utils.funcdata.FunctionalStringTokenizer; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; -import bjc.utils.funcutils.ListUtils; -import bjc.utils.gen.WeightedGrammar; -import bjc.utils.parserutils.RuleBasedConfigReader; - -/** - * Read a grammar from a stream - * - * @author ben - * - */ -public class RBGrammarReader { - private static RuleBasedConfigReader reader; - - private static Random numgen = new Random(); - - static { - setupReader(); - - initPragmas(); - } - - private static void addSubgrammarPragmas() { - reader.addPragma("new-sub-grammar", (tokenizer, state) -> { - state.startNewSubgrammar(); - }); - - reader.addPragma("load-sub-grammar", RBGrammarReader::loadSubGrammar); - reader.addPragma("save-sub-grammar", RBGrammarReader::saveGrammar); - reader.addPragma("edit-sub-grammar", RBGrammarReader::editSubGrammar); - reader.addPragma("remove-sub-grammar", RBGrammarReader::removeSubGrammar); - - reader.addPragma("edit-parent", (tokenizer, state) -> { - state.editParent(); - }); - - reader.addPragma("promote", RBGrammarReader::promoteGrammar); - reader.addPragma("subordinate", RBGrammarReader::subordinateGrammar); - - } - - private static void debugGrammar(ReaderState state) { - System.out.println("Printing rule names: "); - - for (String currentRule : state.getRuleNames().toIterable()) { - System.out.println("\t" + currentRule); - } - - System.out.println(); - } - - private static void doCase(FunctionalStringTokenizer tokenizer, ReaderState state) { - int ruleProbability = readOptionalProbability(tokenizer, state); - - state.addCase(ruleProbability, tokenizer.toList()); - } - - private static void editSubGrammar(FunctionalStringTokenizer tokenizer, ReaderState state) { - String subgrammarName = tokenizer.nextToken(); - - state.editSubgrammar(subgrammarName); - } - - /** - * Read a grammar from a path - * - * @param inputPath - * The path to load the grammar from - * - * @return A grammar read from the stream - * - * @throws IOException - * If something goes wrong during file reading - * - */ - public static WeightedGrammar fromPath(Path inputPath) throws IOException { - ReaderState initialState = new ReaderState(inputPath); - - try (FileInputStream inputStream = new FileInputStream(inputPath.toFile())) { - WeightedGrammar gram = reader.fromStream(inputStream, initialState).getGrammar(); - - IMap> vars = new FunctionalMap<>(); - - Predicate specialPredicate = (strang) -> { - if(strang.matches("\\{\\S+\\}") || strang.matches("\\[\\S+\\}")) { - return true; - } - - return false; - }; - - BiFunction, IList> - specialAction = (strang, gramm) -> { - IList retList = new FunctionalList<>(); - - if(strang.matches("\\{\\S+\\}")) { - if(strang.matches("\\{\\S+:=\\S+\\}")) { - String[] varParts = strang.split(":="); - - String varName = varParts[0].substring(1); - String ruleName = varParts[1].substring(0, varParts[1].length()); - - IList varValue = gramm.generateGenericValues( - ruleName, (s) -> s, " "); - - vars.put(varName, varValue); - } else if(strang.matches("\\{\\S+=\\S+\\}")) { - String[] varParts = strang.split("="); - - String varName = varParts[0].substring(1); - String varValue = varParts[1].substring(0, varParts[1].length()); - - vars.put(varName, new FunctionalList<>(varValue)); - } else { - // @FIXME notify the user they did something wrong - retList.add(strang); - } - } else { - if(strang.matches("\\[\\$\\S+\\]")) { - String varName = strang.substring(2, strang.length()); - - retList = vars.get(varName); - } else if(strang.matches("\\[\\$\\S+\\-\\S+\\]")) { - String[] varParts = strang.substring(1, strang.length()).split("-"); - - StringBuilder actualName = new StringBuilder("["); - - for(String varPart : varParts) { - if(varPart.startsWith("$")) { - IList varName = vars.get(varPart.substring(1)); - - if(varName.getSize() != 1) { - // @FIXME notify the user they did something wrong - } - - actualName.append(varName.first() + "-"); - } else { - actualName.append(varPart + "-"); - } - } - - // Trim trailing - - actualName.deleteCharAt(actualName.length() - 1); - actualName.append("]"); - - retList = gramm.generateGenericValues(actualName.toString(), (s) -> s, " "); - } else { - // @FIXME notify the user they did something wrong - retList.add(strang); - } - } - - return retList; - }; - - gram.configureSpecial(specialPredicate, specialAction); - - return gram; - } catch (IOException ioex) { - throw ioex; - } - } - - private static void importRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - String subgrammarName = tokenizer.nextToken(); - - state.addGrammarAlias(subgrammarName, ruleName); - } - - private static void initialRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String initialRuleName = tokenizer.nextToken(); - - state.setInitialRule(initialRuleName); - } - - private static void initPragmas() { - addSubgrammarPragmas(); - - reader.addPragma("debug", (tokenizer, state) -> { - debugGrammar(state); - }); - - reader.addPragma("uniform", (tokenizer, state) -> { - state.toggleUniformity(); - }); - - reader.addPragma("initial-rule", RBGrammarReader::initialRule); - - reader.addPragma("import-rule", RBGrammarReader::importRule); - - reader.addPragma("remove-rule", RBGrammarReader::removeRule); - - reader.addPragma("prefix-with", RBGrammarReader::prefixRule); - reader.addPragma("suffix-with", RBGrammarReader::suffixRule); - - reader.addPragma("regex-rule", (tokenizer, state) -> { - String ruleName = tokenizer.nextToken(); - - IList regx = tokenizer.toList(); - Generex regex = new Generex(ListUtils.collapseTokens(regx)); - - state.addSpecialRule(ruleName, () -> { - return new FunctionalList<>(regex.random().split(" ")); - }); - }); - - reader.addPragma("range-rule", (tokenizer, state) -> { - String ruleName = tokenizer.nextToken(); - - int start = Integer.parseInt(tokenizer.nextToken()); - int end = Integer.parseInt(tokenizer.nextToken()); - - state.addSpecialRule(ruleName, () -> { - return new FunctionalList<>(Integer.toString( - numgen.nextInt((end - start) + 1) + start)); - }); - }); - } - - private static void loadSubGrammar(FunctionalStringTokenizer stk, ReaderState rs) { - String subgrammarName = stk.nextToken(); - String subgrammarPath = stk.nextToken(); - - rs.loadSubgrammar(subgrammarName, subgrammarPath); - } - - private static void prefixRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - String prefixToken = tokenizer.nextToken(); - - int additionalProbability = readOptionalProbability(tokenizer, state); - - state.prefixRule(ruleName, prefixToken, additionalProbability); - } - - private static void promoteGrammar(FunctionalStringTokenizer tokenizer, ReaderState state) { - String subgrammarName = tokenizer.nextToken(); - String subordinateName = tokenizer.nextToken(); - - state.promoteGrammar(subgrammarName, subordinateName); - } - - private static int readOptionalProbability(FunctionalStringTokenizer tokenizer, ReaderState state) { - if (state.isUniform()) { - return 1; - } - - return Integer.parseInt(tokenizer.nextToken()); - } - - private static void removeRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - - state.deleteRule(ruleName); - } - - private static void removeSubGrammar(FunctionalStringTokenizer tokenizer, ReaderState state) { - String subgrammarName = tokenizer.nextToken(); - - state.deleteSubgrammar(subgrammarName); - } - - private static void saveGrammar(FunctionalStringTokenizer tokenizer, ReaderState state) { - String subgrammarName = tokenizer.nextToken(); - - state.saveSubgrammar(subgrammarName); - } - - private static void setupReader() { - reader = new RuleBasedConfigReader<>(null, null, null); - - reader.setStartRule((tokenizer, stateTokenPair) -> { - stateTokenPair.doWith((initToken, state) -> { - state.startNewRule(initToken); - - doCase(tokenizer, state); - }); - }); - - reader.setContinueRule((tokenizer, state) -> { - doCase(tokenizer, state); - }); - - reader.setEndRule((tokenizer) -> { - tokenizer.setCurrentRule(null); - }); - } - - private static void subordinateGrammar(FunctionalStringTokenizer tokenizer, ReaderState state) { - String grammarName = tokenizer.nextToken(); - - state.subordinateGrammar(grammarName); - } - - private static void suffixRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - String suffixToken = tokenizer.nextToken(); - - int additionalProbability = readOptionalProbability(tokenizer, state); - - state.suffixRule(ruleName, suffixToken, additionalProbability); - } -} diff --git a/RGens/src/main/java/bjc/rgens/parser/ReaderState.java b/RGens/src/main/java/bjc/rgens/parser/ReaderState.java deleted file mode 100644 index 89fde25..0000000 --- a/RGens/src/main/java/bjc/rgens/parser/ReaderState.java +++ /dev/null @@ -1,306 +0,0 @@ -package bjc.rgens.parser; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.Stack; -import java.util.function.Supplier; - -import bjc.utils.funcdata.IList; -import bjc.utils.gen.WeightedGrammar; - -/** - * Represents the internal state of reader - * - * @author ben - * - */ -public class ReaderState { - private WeightedGrammar currentGrammar; - - private Stack> grammarStack; - - private String currentRule; - - private boolean isUniform; - - private Path currentDirectory; - - /** - * Create a new reader state - * - * @param inputPath - * The path to this grammar - */ - public ReaderState(Path inputPath) { - grammarStack = new Stack<>(); - - currentGrammar = new WeightedGrammar<>(); - - // Grammars start out uniform - isUniform = true; - - currentDirectory = inputPath.getParent(); - } - - /** - * Get the rule names for the current grammar - * - * @return The rule names for the current grammar - */ - public IList getRuleNames() { - return currentGrammar.getRuleNames(); - } - - /** - * Check if this reader is currently in uniform mode - * - * @return Whether this grammar is in uniform mode - */ - public boolean isUniform() { - return isUniform; - } - - /** - * Set the current grammar to be the specified one - * - * @param newWorkingGrammar - * The new grammar to use - */ - public void setCurrentGrammar(WeightedGrammar newWorkingGrammar) { - currentGrammar = newWorkingGrammar; - } - - /** - * Set the rule currently being worked on - * - * @param ruleName - * The rule currently being worked on - */ - public void setCurrentRule(String ruleName) { - currentRule = ruleName; - } - - /** - * Set the initial rule of this grammar - * - * @param ruleName - * The initial rule of this grammar - */ - public void setInitialRule(String ruleName) { - currentGrammar.setInitialRule(ruleName); - } - - /** - * Toggle this uniformity setting for this grammar - */ - public void toggleUniformity() { - isUniform = !isUniform; - } - - /** - * Start work on a new sub-grammar of the previous grammar - */ - public void startNewSubgrammar() { - grammarStack.push(currentGrammar); - - currentGrammar = new WeightedGrammar<>(); - } - - /** - * Move to editing the grammar that is the parent of this current one - */ - public void editParent() { - currentGrammar = grammarStack.pop(); - } - - /** - * Add a case to the current grammar - * - * @param ruleProbability - * The probability for this case to occur - * @param ruleParts - * The parts that make up this case - */ - public void addCase(int ruleProbability, IList ruleParts) { - currentGrammar.addCase(currentRule, ruleProbability, ruleParts); - } - - public void addSpecialRule(String ruleName, Supplier> cse) { - currentGrammar.addSpecialRule(ruleName, cse); - } - /** - * Edit a subgrammar of the current grammar - * - * @param subgrammarName - * The name of the subgrammar to edit - */ - public void editSubgrammar(String subgrammarName) { - WeightedGrammar subgrammar = currentGrammar.getSubgrammar(subgrammarName); - - grammarStack.push(currentGrammar); - - currentGrammar = subgrammar; - } - - /** - * Start editing a new rule in the current grammar - * - * @param ruleName - * The name of the new rule to edit - */ - public void startNewRule(String ruleName) { - currentGrammar.addRule(ruleName); - - currentRule = ruleName; - } - - /** - * Convert this package of state into a weighted grammar - * - * @return The grammar represented by this state - */ - public WeightedGrammar getGrammar() { - return currentGrammar; - } - - /** - * Alias a current subgrammar to a new name - * - * @param subgrammarName - * The name of the subgrammar to alias - * @param subgrammarAlias - * The name of the alias for the subgrammar - */ - public void addGrammarAlias(String subgrammarName, String subgrammarAlias) { - currentGrammar.addGrammarAlias(subgrammarName, subgrammarAlias); - } - - /** - * Load a subgrammar into this one. - * - * @param subgrammarName - * The name to assign to the subgrammar - * @param subgrammarPath - * The path to load the subgrammar from - */ - public void loadSubgrammar(String subgrammarName, - String subgrammarPath) { - Path loadPath = currentDirectory.resolve(subgrammarPath); - - try { - WeightedGrammar subgrammar = RBGrammarReader - .fromPath(loadPath); - - currentGrammar.addSubgrammar(subgrammarName, subgrammar); - } catch (IOException ioex) { - PragmaErrorException peex = new PragmaErrorException( - "Couldn't load subgrammar " + subgrammarName + " from " - + subgrammarPath); - - peex.initCause(ioex); - - throw peex; - } - } - - /** - * Prefix a token onto all of the cases for the specified rule - * - * @param ruleName - * The rule to do prefixing on - * @param prefixToken - * The token to prefix onto each case - * @param additionalProbability - * The probability modification of the prefixed cases - */ - public void prefixRule(String ruleName, String prefixToken, - int additionalProbability) { - currentGrammar.prefixRule(ruleName, prefixToken, - additionalProbability); - } - - /** - * Promote the specified subgrammar above the current grammar - * - * @param subgrammarName - * The name of the subgrammar to promote - * @param subordinateName - * The name to bind this grammar to the subgrammar under - */ - public void promoteGrammar(String subgrammarName, - String subordinateName) { - WeightedGrammar subgrammar = currentGrammar - .getSubgrammar(subgrammarName); - - currentGrammar.deleteSubgrammar(subgrammarName); - - subgrammar.addSubgrammar(subordinateName, currentGrammar); - - currentGrammar = subgrammar; - } - - /** - * Delete a rule from the current subgrammar - * - * @param ruleName - * The name of the rule to delete - */ - public void deleteRule(String ruleName) { - currentGrammar.deleteRule(ruleName); - } - - /** - * Delete a subgrammar from the current grammar - * - * @param subgrammarName - * The name of the subgrammar to delete - */ - public void deleteSubgrammar(String subgrammarName) { - currentGrammar.deleteSubgrammar(subgrammarName); - } - - /** - * Save the current grammar as a subgrammar of the previous one - * - * @param subgrammarName - * The name of the subgrammar to save this under - */ - public void saveSubgrammar(String subgrammarName) { - WeightedGrammar newWorkingGrammar = grammarStack.pop(); - - newWorkingGrammar.addSubgrammar(subgrammarName, currentGrammar); - - currentGrammar = newWorkingGrammar; - } - - /** - * Subordinate this grammar as a subgrammar to a new grammar - * - * @param grammarName - * The name for the subgrammar to bind the current grammar - * to - */ - public void subordinateGrammar(String grammarName) { - WeightedGrammar newWorkingGrammar = new WeightedGrammar<>(); - - newWorkingGrammar.addSubgrammar(grammarName, currentGrammar); - - currentGrammar = newWorkingGrammar; - } - - /** - * Suffix a token onto all of the cases for the specified rule - * - * @param ruleName - * The rule to do suffixing on - * @param suffixToken - * The token to suffix onto each case - * @param additionalProbability - * The probability modification of the suffixed cases - */ - public void suffixRule(String ruleName, String suffixToken, - int additionalProbability) { - currentGrammar.suffixRule(ruleName, suffixToken, - additionalProbability); - } -} diff --git a/RGens/src/main/java/bjc/rgens/server/CLIArgsParser.java b/RGens/src/main/java/bjc/rgens/server/CLIArgsParser.java deleted file mode 100644 index fa0d399..0000000 --- a/RGens/src/main/java/bjc/rgens/server/CLIArgsParser.java +++ /dev/null @@ -1,141 +0,0 @@ -package bjc.rgens.server; - -import java.io.File; - -public class CLIArgsParser { - public static void parseArgs(String[] args, GrammarServerEngine eng) { - boolean didTerminalOp = false; - boolean forceInteractive = false; - - // @TODO report error status - boolean didError = false; - - if(args.length < 0) return; - - if(args.length == 1 && args[0].equals("--help")) { - // @TODO show help - } else { - for(int i = 0; i < args.length; i++) { - String arg = args[i]; - - switch(arg) { - case "-lc": - case "--load-config-file": - String configFileName = args[++i]; - - eng.doLoadConfig(configFileName); - break; - case "-lg": - case "--load-grammar-file": - String grammarFileName = args[++i]; - - File grammarFile = new File(grammarFileName); - - String ruleName = grammarFile.getName(); - ruleName = ruleName.substring(0, ruleName.lastIndexOf('.')); - - if(!args[i+1].startsWith("-")) { - ruleName = args[++i]; - } - - eng.doLoadGrammar(ruleName, grammarFileName); - - break; - case "-ll": - case "--load-long-file": - String longRuleFileName = args[++i]; - - File longRuleFile = new File(longRuleFileName); - - String longRuleName = longRuleFile.getName(); - longRuleName = longRuleName.substring(0, longRuleName.lastIndexOf('.')); - - if(!args[i+1].startsWith("-")) { - longRuleName = args[++i]; - } - - eng.doLoadLongRule(longRuleName, longRuleFileName); - - break; - case "-ge": - case "--generate-exported-rule": - String exportedRuleName = args[++i]; - - if(eng.hasExportedRule(exportedRuleName)) { - eng.doGenerateExportedRule(exportedRuleName); - didTerminalOp = true; - } else { - System.out.printf("Error: No exported rule named %s\n", exportedRuleName); - } - break; - case "-gg": - case "--generate-grammar-rule": - String grammarName = args[++i]; - - if(!eng.hasLoadedGrammar(grammarName)) { - System.out.printf("Error: No grammar named %s\n", grammarName); - } else { - String ruleToGenerate = ""; - - if(!args[i+1].startsWith("-")) { - ruleToGenerate = args[++i]; - } else if(eng.hasInitialRule(grammarName)) { - ruleToGenerate = eng.getInitialRule(grammarName); - } else { - System.out.printf("Error: Grammar %s has no initial rule. A " - + "rule must be provided.",grammarName); - } - - eng.doGenerateGrammar(grammarName, ruleToGenerate); - didTerminalOp = true; - } - break; - case "-st": - case "--stress-test": - String thingToTest = args[++i]; - - // @TODO support testing rules from grammars - // as well as a specified number of times - if(thingToTest.equals("*")) { - eng.doStressTest(10000); - } else { - eng.doStressTest(thingToTest, 1000); - } - - didTerminalOp = true; - - break; - case "-d": - case "--debug": - if(eng.debugMode) { - System.out.println("Warning: debug mode is already on. Use -nd or --no-debug" - + " to turn it off"); - } else { - eng.debugMode = true; - } - break; - case "-nd": - case "--no-debug": - if(!eng.debugMode) { - System.out.println("Warning: debug mode is already off. Use -d or --debug" - + " to turn it on"); - } else { - eng.debugMode = false; - } - break; - case "-i": - case "--interactive": - forceInteractive = true; - break; - default: - System.out.println("Error: Unrecognized argument " + arg); - break; - } - } - } - - if(!forceInteractive && didTerminalOp) { - System.exit(didError ? 1 : 0); - } - } -} diff --git a/RGens/src/main/java/bjc/rgens/server/GrammarServer.java b/RGens/src/main/java/bjc/rgens/server/GrammarServer.java deleted file mode 100644 index 1a47de2..0000000 --- a/RGens/src/main/java/bjc/rgens/server/GrammarServer.java +++ /dev/null @@ -1,346 +0,0 @@ -package bjc.rgens.server; - -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IMap; -import bjc.utils.gen.WeightedGrammar; - -import java.io.File; - -import java.util.Scanner; - -public class GrammarServer { - private Scanner scn; - - private IMap> loadedGrammars; - private IMap> exportedRules; - - private GrammarServerEngine eng; - - public GrammarServer(Scanner scn) { - this.scn = scn; - - this.loadedGrammars = new FunctionalMap<>(); - this.exportedRules = new FunctionalMap<>(); - - eng = new GrammarServerEngine(loadedGrammars, exportedRules); - - ServerGrammarReader.setExportedRules(exportedRules); - } - - public static void main(String[] args) { - System.out.println("GrammarServer 1.0"); - - Scanner scn = new Scanner(System.in); - - GrammarServer serv = new GrammarServer(scn); - - CLIArgsParser.parseArgs(args, serv.eng); - - System.out.print("Enter a command (m for help): "); - - char comm = scn.nextLine().charAt(0); - - while(comm != 'e') { - switch(comm) { - case 'm': - System.out.println("GrammarServer Commands:"); - System.out.println("\tm: Print command help"); - System.out.println("\te: Exit GrammarServer"); - System.out.println("\tl: Load from file"); - System.out.println("\ts: Show information"); - System.out.println("\tg: Generate text"); - break; - case 'g': - serv.generateMode(); - break; - case 's': - serv.showMode(); - break; - case 'l': - serv.loadMode(); - break; - default: - System.out.println("? Unrecognized Command"); - } - - System.out.print("Enter a command (m for help): "); - - comm = scn.nextLine().charAt(0); - } - - System.out.println("GrammarServer exiting"); - } - - - - private void loadMode() { - System.out.println("Entering Load Mode"); - - System.out.print("(Load Mode) Enter a command (m for help): "); - char comm = scn.nextLine().charAt(0); - - while(comm != 'e') { - switch(comm) { - case 'm': - System.out.println("GrammarServer Load Mode Commands"); - System.out.println("\tm: Show command help"); - System.out.println("\te: Exit Load Mode"); - System.out.println("\tg: Load grammar from a file"); - System.out.println("\tl: Load long rule from a file"); - System.out.println("\tc: Load configuration from a file"); - break; - case 'g': - loadGrammar(); - break; - case 'c': - loadConfig(); - break; - case 'l': - loadLongRule(); - break; - default: - System.out.println("? Unrecognized Command"); - } - - System.out.print("(Load Mode) Enter a command (m for help): "); - comm = scn.nextLine().charAt(0); - } - - System.out.println("Exiting Load Mode"); - } - - private void loadGrammar() { - System.out.print("Enter path to load grammar from: "); - - String grammarPath = scn.nextLine().trim(); - - File grammarFile = new File(grammarPath); - - String grammarName = grammarFile.getName().trim(); - - grammarName = grammarName.substring(0, grammarName.lastIndexOf(".")); - - System.out.printf("Enter grammar name or press enter for" - + " the default (%s): ", grammarName); - - String inputName = scn.nextLine(); - - if(!inputName.equals("")) { - grammarName = inputName; - } - - eng.doLoadGrammar(grammarName, grammarPath); - - return; - } - - private void loadLongRule() { - System.out.print("Enter the file to load a long rule from: "); - - String fileName = scn.nextLine().trim(); - File ruleFile = new File(fileName); - - String tempName = ruleFile.getName(); - tempName = tempName.substring(0, tempName.lastIndexOf('.')); - - System.out.printf("Enter the name for the long rule (default %s): ", tempName); - - String ruleName = scn.nextLine().trim(); - - if(ruleName.equals("")) { - ruleName = tempName; - } - - eng.doLoadLongRule(ruleName, fileName); - } - - private void loadConfig() { - System.out.print("Enter the file to load configuration from: "); - - String fileName = scn.nextLine().trim(); - - eng.doLoadConfig(fileName); - } - - private void showMode() { - System.out.println("Entering Show Mode"); - - System.out.print("(Show Mode) Enter a command (m for help): "); - - char comm = scn.nextLine().charAt(0); - - while(comm != 'e') { - switch(comm) { - case 'm': - System.out.println("GrammarServer Show Mode Commands: "); - System.out.println("\tm: Show command help"); - System.out.println("\tl: Show loaded grammars"); - System.out.println("\tr: Show rules from a grammar"); - System.out.println("\tx: Show exported rules"); - System.out.println("\te: Exit Show Mode"); - break; - case 'r': - showGrammarRules(); - break; - case 'x': - eng.doShowExportedRules(); - break; - case 'l': - eng.doShowLoadedGrammars(); - break; - default: - System.out.println("? Unrecognized command"); - break; - } - - System.out.print("(Show Mode) Enter a command (m for help): "); - - comm = scn.nextLine().charAt(0); - } - - - - System.out.println("Exiting Show Mode"); - } - - private void generateMode() { - System.out.println("Entering Generate Mode"); - - System.out.print("(Generate Mode) Enter a command (m for help): "); - - char comm = scn.nextLine().charAt(0); - - while(comm != 'e') { - switch(comm) { - case 'm': - System.out.println("GrammarServer Generate Mode Commands: "); - System.out.println("\tm: Show command help"); - System.out.println("\tx: Generate from exported rules"); - System.out.println("\tr: Generate from a grammar"); - System.out.println("\te: Exit Generate Mode"); - break; - case 'x': - generateExportedRule(); - break; - case 'r': - generateGrammar(); - break; - default: - System.out.println("? Unrecognized command"); - } - - System.out.print("(Generate Mode) Enter a command (m for help): "); - - comm = scn.nextLine().charAt(0); - } - - System.out.println("Exiting Generate Mode"); - } - - private void generateExportedRule() { - System.out.print("Enter the name of the rule to generate" - + " (l to list, enter to cancel): "); - - String ruleName = scn.nextLine().trim(); - - while(true) { - if(ruleName.equals("")) break; - - if(ruleName.equals("l")) { - eng.doShowExportedRules(); - } else if (exportedRules.containsKey(ruleName)) { - eng.doGenerateExportedRule(ruleName); - - System.out.print("Generate again from this rule? (yes/no) (yes by default): "); - - String resp = scn.nextLine().trim(); - - if(resp.equalsIgnoreCase("yes") || resp.equals("")) { - continue; - } - } else { - System.out.println("? Unrecognized external rule"); - } - - System.out.print("Enter the name of the rule to generate" - + " (l to list, enter to cancel): "); - - ruleName = scn.nextLine().trim(); - } - } - - private void generateGrammar() { - System.out.print("Enter the name of the grammar to generate from (l to list, enter to cancel): "); - - String grammarName = scn.nextLine().trim(); - - while(true) { - if(grammarName.equals("")) break; - - if(grammarName.equals("l")) { - eng.doShowLoadedGrammars(); - } else if(loadedGrammars.containsKey(grammarName)) { - WeightedGrammar currentGram = loadedGrammars.get(grammarName); - - System.out.print("Enter the name of the rule to generate" - + " (l to list, enter to cancel): "); - - String ruleName = scn.nextLine().trim(); - - while(true) { - if(ruleName.equals("")) break; - - if(ruleName.equals("l")) { - eng.doShowGrammarRules(grammarName); - } else if (currentGram.hasRule(ruleName)) { - eng.doGenerateGrammar(currentGram, ruleName); - - System.out.print("Generate again from this rule? (yes/no) (yes by default): "); - - String resp = scn.nextLine().trim(); - - if(resp.equalsIgnoreCase("yes") || resp.equals("")) { - continue; - } - } else { - System.out.println("? Unrecognized grammar rule"); - } - - System.out.print("Enter the name of the rule to generate" - + " (l to list, enter to cancel): "); - - ruleName = scn.nextLine().trim(); - } - - } else { - System.out.println("? Unrecognized grammar name"); - } - - System.out.print("Enter the name of the grammar to generate from " - + "(l to list, enter to cancel): "); - - grammarName = scn.nextLine().trim(); - } - } - - private void showGrammarRules() { - System.out.print("Enter the name of the grammar (l to list): "); - String gramName = scn.nextLine().trim(); - - do { - if(gramName.equals("")) break; - - if(gramName.equals("l")) { - eng.doShowLoadedGrammars(); - } else if (loadedGrammars.containsKey(gramName)) { - eng.doShowGrammarRules(gramName); - break; - } else { - System.out.println("? Unrecognized grammar name"); - } - - System.out.print("Enter the name of the grammar (l to list): "); - gramName = scn.nextLine().trim(); - } while(true); - } -} diff --git a/RGens/src/main/java/bjc/rgens/server/GrammarServerEngine.java b/RGens/src/main/java/bjc/rgens/server/GrammarServerEngine.java deleted file mode 100644 index 2f608ec..0000000 --- a/RGens/src/main/java/bjc/rgens/server/GrammarServerEngine.java +++ /dev/null @@ -1,300 +0,0 @@ -package bjc.rgens.server; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.LinkedList; -import java.util.Queue; -import java.util.Scanner; - -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; -import bjc.utils.funcutils.ListUtils; -import bjc.utils.gen.WeightedGrammar; - -public class GrammarServerEngine { - private IMap> loadedGrammars; - private IMap> exportedRules; - - public static boolean debugMode = false; - - public GrammarServerEngine(IMap> loadedGrammars, - IMap> exportedRules) { - this.loadedGrammars = loadedGrammars; - this.exportedRules = exportedRules; - } - - public String getInitialRule(String grammarName) { - return loadedGrammars.get(grammarName).getInitialRule(); - } - - public boolean hasInitialRule(String grammarName) { - return loadedGrammars.get(grammarName).hasInitialRule(); - } - - public boolean hasExportedRule(String ruleName) { - return exportedRules.containsKey(ruleName); - } - - public boolean hasLoadedGrammar(String grammarName) { - return loadedGrammars.containsKey(grammarName); - } - - public void doLoadConfig(String fileName) { - File inputFile = new File(fileName); - - try(FileInputStream inputStream = new FileInputStream(inputFile)) { - try(Scanner fle = new Scanner(inputStream)) { - while(fle.hasNextLine()) { - String line = fle.nextLine().trim(); - - // Handle comments - if(line.equals("") || - line.startsWith("#") || - line.startsWith("//")) { - continue; - } - - // Handle mixed whitespace in input - line = line.replaceAll("\\s+", " "); - - String path; - String name; - - if(line.lastIndexOf(' ') != -1) { - path = line.substring(0, line.lastIndexOf(' ')); - name = line.substring(line.lastIndexOf(' ') + 1, line.length()); - } else { - path = line; - - File pathFile = new File(path); - String pathName = pathFile.getName(); - - if(pathFile.isDirectory()) { - // Load all the files in the directory recursively - Queue entries = new LinkedList<>(); - - for (File entry : pathFile.listFiles()) { - entries.add(entry); - } - - while(!entries.isEmpty()) { - File entry = entries.poll(); - - String entryPath = entry.getName(); - - if(entry.isHidden()) continue; - if(entry.isDirectory()) { - for (File newEntry : entry.listFiles()) { - entries.add(newEntry); - } - - continue; - } - - name = entryPath.substring(0, entryPath.lastIndexOf('.')); - - doLoadGrammarEntry(entry.toString(), name); - } - - continue; - } - - name = pathName.substring(0, pathName.lastIndexOf('.')); - } - - doLoadGrammarEntry(path, name); - } - } - } catch(IOException ioex) { - System.out.printf("? Error reading configuration from file" - + " (reason: %s)\n", ioex.getMessage()); - } - } - - private void doLoadGrammarEntry(String path, String name) { - if(path.endsWith(".gram")) { - doLoadGrammar(name, path); - } else if(path.endsWith(".template")) { - System.out.println("Error: Templates are not supported yet"); - } else if(path.endsWith(".long")) { - doLoadLongRule(name, path); - } else { - System.out.println("Error: Unknown filetype " + - path.substring(path.lastIndexOf("."), path.length())); - } - } - - public void doLoadLongRule(String ruleName, String ruleFile) { - ruleName = "[" + ruleName + "]"; - - if(debugMode) { - System.out.printf("Loading long rule (named %s) from path %s\n", - ruleName, ruleFile); - } - - try (FileInputStream inputStream = new FileInputStream(ruleFile)) { - try (Scanner fle = new Scanner(inputStream)) { - IList> ruleParts = new FunctionalList<>(); - - while(fle.hasNextLine()) { - ruleParts.add(new FunctionalList<>(fle.nextLine().trim().split(" "))); - } - - WeightedGrammar longGram = new WeightedGrammar<>(); - - longGram.addSpecialRule(ruleName, () -> ruleParts.randItem()); - longGram.setInitialRule(ruleName); - - exportedRules.put(ruleName, longGram); - - if(debugMode) { - System.out.printf("Loaded long rule (named %s) from path %s\n", - ruleName, ruleFile); - } - } - } catch (IOException ioex) { - System.out.printf("Error reading long rule (%s)\n", ioex.getMessage()); - } - } - - public void doLoadGrammar(String grammarName, String grammarPath) { - if(debugMode) { - System.out.printf("Loading grammar (named %s) from path %s\n", - grammarName, grammarPath); - } - - try (FileInputStream inputStream = new FileInputStream(grammarPath)) { - WeightedGrammar newGram = - ServerGrammarReader.fromStream(inputStream).merge((gram, exports) -> { - for(String export : exports.toIterable()) { - if(debugMode) { - System.out.printf("\tLoaded exported rule %s from grammar %s\n", - export, grammarName); - - if(exportedRules.containsKey(export)) { - System.out.printf("\tWarning: Exported rule %s from grammar %s" + - " shadows a pre-existing rule\n", export, grammarName); - } - } - - exportedRules.put(export, gram); - } - - return gram; - }); - - loadedGrammars.put(grammarName, newGram); - } catch (IOException ioex) { - System.out.printf("? Error reading grammar from file" - + " (reason: %s)\n", ioex.getMessage()); - } - - if(debugMode) { - System.out.printf("Loaded grammar (named %s) from path %s\n", - grammarName, grammarPath); - } - } - - public void doGenerateExportedRule(String ruleName) { - String ruleResult = ListUtils.collapseTokens( - exportedRules.get(ruleName) - .generateListValues(ruleName, " ")); - - System.out.println("Generated Result: "); - System.out.println("\t" + ruleResult.replaceAll("\\s+", " ")); - } - - public void doGenerateGrammar(String currentGram, String ruleName) { - doGenerateGrammar(loadedGrammars.get(currentGram), ruleName); - } - - public void doGenerateGrammar(WeightedGrammar currentGram, String ruleName) { - String ruleResult = ListUtils.collapseTokens( - currentGram.generateListValues(ruleName, " ")); - - System.out.println("Generated Result: "); - System.out.println("\t" + ruleResult.replaceAll("\\s+", " ")); - } - - public void doShowExportedRules() { - System.out.printf("Currently exported rules (%d total):\n", - exportedRules.getSize()); - - exportedRules.forEachKey(key -> { - System.out.println("\t" + key); - }); - } - - public void doShowGrammarRules(String gramName) { - WeightedGrammar gram = loadedGrammars.get(gramName); - - IList ruleNames = gram.getRuleNames(); - - System.out.printf("Rules for grammar %s (%d total)\n", - gramName, ruleNames.getSize()); - - ruleNames.forEach(rule -> { - System.out.println("\t" + rule); - }); - } - - public void doShowLoadedGrammars() { - System.out.printf("Currently loaded grammars (%d total):\n", - loadedGrammars.getSize()); - - loadedGrammars.forEachKey(key -> { - System.out.println("\t" + key); - }); - } - - public void doStressTest(int count) { - exportedRules.forEachKey(key -> { - doStressTest(key, count); - }); - } - - public void doStressTest(String ruleName, int count) { - doStressTest(exportedRules.get(ruleName), ruleName, count); - } - - public void doStressTest(WeightedGrammar gram, String ruleName, int count) { - if(debugMode) System.out.println("Stress-testing rule " + ruleName); - - IList res = new FunctionalList<>(); - IList foundTags = new FunctionalList<>(); - - boolean foundBroken = false; - - for(int i = 0; i < count; i++) { - res = gram.generateListValues(ruleName, " "); - - for(String tok : res.toIterable()) { - if(tok.matches("\\[\\S+\\]") && !foundTags.contains(tok)) { - System.out.println("\tWarning: Possible un-expanded rule " + tok + " found" - + " in expansion of " + ruleName); - - doFindRule(tok); - - foundBroken = true; - - foundTags.add(tok); - } - } - } - - if(debugMode) { - if(!foundBroken) System.out.printf("Rule %s succesfully passed stress-testing\n", ruleName); - else System.out.printf("Rule %s failed stress-testing\n", ruleName); - } - } - - private void doFindRule(String ruleName) { - loadedGrammars.forEach((gramName, gram) -> { - if(gram.hasRule(ruleName)) { - System.out.printf("\t\tFound rule %s in grammar %s\n", ruleName, gramName); - } - }); - } -} diff --git a/RGens/src/main/java/bjc/rgens/server/ReaderState.java b/RGens/src/main/java/bjc/rgens/server/ReaderState.java deleted file mode 100644 index 3f5021e..0000000 --- a/RGens/src/main/java/bjc/rgens/server/ReaderState.java +++ /dev/null @@ -1,181 +0,0 @@ -package bjc.rgens.server; - -import java.util.function.Supplier; - -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.gen.WeightedGrammar; - -/** - * Represents the internal state of reader - * - * @author ben - * - */ -public class ReaderState { - private WeightedGrammar currentGrammar; - private String currentRule; - private boolean isUniform; - - private IList exports; - - /** - * Create a new reader state - */ - public ReaderState() { - currentGrammar = new WeightedGrammar<>(); - - // Grammars start out uniform - isUniform = true; - - exports = new FunctionalList<>(); - } - - public void addExport(String export) { - exports.add(export); - } - - public IList getExports() { - return exports; - } - - /** - * Get the rule names for the current grammar - * - * @return The rule names for the current grammar - */ - public IList getRuleNames() { - return currentGrammar.getRuleNames(); - } - - /** - * Check if this reader is currently in uniform mode - * - * @return Whether this grammar is in uniform mode - */ - public boolean isUniform() { - return isUniform; - } - - /** - * Set the current grammar to be the specified one - * - * @param newWorkingGrammar - * The new grammar to use - */ - public void setCurrentGrammar(WeightedGrammar newWorkingGrammar) { - currentGrammar = newWorkingGrammar; - } - - /** - * Set the rule currently being worked on - * - * @param ruleName - * The rule currently being worked on - */ - public void setCurrentRule(String ruleName) { - currentRule = ruleName; - } - - /** - * Set the initial rule of this grammar - * - * @param ruleName - * The initial rule of this grammar - */ - public void setInitialRule(String ruleName) { - currentGrammar.setInitialRule(ruleName); - } - - /** - * Toggle this uniformity setting for this grammar - */ - public void toggleUniformity() { - isUniform = !isUniform; - } - - /** - * Add a case to the current grammar - * - * @param ruleProbability - * The probability for this case to occur - * @param ruleParts - * The parts that make up this case - */ - public void addCase(int ruleProbability, IList ruleParts) { - currentGrammar.addCase(currentRule, ruleProbability, ruleParts); - } - - /** - * Add a special rule to the grammar - * - * @param ruleName The name of the special rule - * @param cse The special case for the rule - */ - public void addSpecialRule(String ruleName, Supplier> cse) { - currentGrammar.addSpecialRule(ruleName, cse); - } - - /** - * Start editing a new rule in the current grammar - * - * @param ruleName - * The name of the new rule to edit - */ - public void startNewRule(String ruleName) { - currentGrammar.addRule(ruleName); - - currentRule = ruleName; - } - - /** - * Convert this package of state into a weighted grammar - * - * @return The grammar represented by this state - */ - public WeightedGrammar getGrammar() { - return currentGrammar; - } - - /** - * Prefix a token onto all of the cases for the specified rule - * - * @param ruleName - * The rule to do prefixing on - * @param prefixToken - * The token to prefix onto each case - * @param additionalProbability - * The probability modification of the prefixed cases - */ - public void prefixRule(String ruleName, String prefixToken, - int additionalProbability) { - currentGrammar.prefixRule(ruleName, prefixToken, - additionalProbability); - } - - /** - * Delete a rule from the current grammar - * - * @param ruleName - * The name of the rule to delete - */ - public void deleteRule(String ruleName) { - currentGrammar.deleteRule(ruleName); - } - - /** - * Suffix a token onto all of the cases for the specified rule - * - * @param ruleName - * The rule to do suffixing on - * @param suffixToken - * The token to suffix onto each case - * @param additionalProbability - * The probability modification of the suffixed cases - */ - public void suffixRule(String ruleName, String suffixToken, - int additionalProbability) { - currentGrammar.suffixRule(ruleName, suffixToken, - additionalProbability); - } -} diff --git a/RGens/src/main/java/bjc/rgens/server/ServerGrammarReader.java b/RGens/src/main/java/bjc/rgens/server/ServerGrammarReader.java deleted file mode 100644 index 77b1e8b..0000000 --- a/RGens/src/main/java/bjc/rgens/server/ServerGrammarReader.java +++ /dev/null @@ -1,326 +0,0 @@ -package bjc.rgens.server; - -import com.mifmif.common.regex.Generex; - -import java.io.InputStream; -import java.util.Random; -import java.util.function.BiFunction; -import java.util.function.Predicate; - -import bjc.utils.data.IPair; -import bjc.utils.data.Pair; -import bjc.utils.funcdata.FunctionalStringTokenizer; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; -import bjc.utils.funcutils.ListUtils; -import bjc.utils.gen.WeightedGrammar; -import bjc.utils.parserutils.RuleBasedConfigReader; - -/** - * Read a grammar from a stream - * - * @author ben - * - */ -public class ServerGrammarReader { - static { - setupReader(); - - initPragmas(); - } - - private static RuleBasedConfigReader reader; - - private static Random numgen = new Random(); - - private static IMap> exportedRules; - - public static void setExportedRules(IMap> rules) { - exportedRules = rules; - } - - /** - * Read a grammar from a stream - * - * @param inputStream - * The stream to load the grammar from - * - * @return A grammar read from the stream - * - */ - public static IPair, IList> - fromStream(InputStream inputStream) { - ReaderState initialState = new ReaderState(); - - WeightedGrammar gram = reader.fromStream(inputStream, initialState).getGrammar(); - - IMap> vars = new FunctionalMap<>(); - - Predicate specialPredicate = (strang) -> { - if(strang.matches("\\[\\S+\\]")) { - return true; - } - - return false; - }; - - BiFunction, IList> - specialAction = (strang, gramm) -> { - return handleSpecialRule(vars, strang, gramm); - }; - - gram.configureSpecial(specialPredicate, specialAction); - - IList exports = initialState.getExports(); - - if(gram.getInitialRule() != null && !exports.contains(gram.getInitialRule())) { - exports.add(gram.getInitialRule()); - } - - return new Pair<>(gram, exports); - } - - private static void initPragmas() { - reader.addPragma("debug", (tokenizer, state) -> { - debugGrammar(state); - }); - - reader.addPragma("uniform", (tokenizer, state) -> { - state.toggleUniformity(); - }); - - reader.addPragma("initial-rule", ServerGrammarReader::initialRule); - - reader.addPragma("remove-rule", ServerGrammarReader::removeRule); - - reader.addPragma("prefix-with", ServerGrammarReader::prefixRule); - reader.addPragma("suffix-with", ServerGrammarReader::suffixRule); - - reader.addPragma("regex-rule", ServerGrammarReader::handleRegexRule); - - reader.addPragma("range-rule", ServerGrammarReader::handleRangeRule); - - reader.addPragma("export-rule", (tokenizer, state) -> { - String ruleName = tokenizer.nextToken(); - - state.addExport(ruleName); - }); - } - - private static void setupReader() { - reader = new RuleBasedConfigReader<>(null, null, null); - - reader.setStartRule((tokenizer, stateTokenPair) -> { - stateTokenPair.doWith((initToken, state) -> { - state.startNewRule(initToken); - - doCase(tokenizer, state); - }); - }); - - reader.setContinueRule((tokenizer, state) -> { - doCase(tokenizer, state); - }); - - reader.setEndRule((tokenizer) -> { - tokenizer.setCurrentRule(null); - }); - } - - private static void debugGrammar(ReaderState state) { - System.out.println("Printing rule names: "); - - for (String currentRule : state.getRuleNames().toIterable()) { - System.out.println("\t" + currentRule); - } - - System.out.println(); - } - - private static IList handleSpecialRule(IMap> vars, String strang, - WeightedGrammar gramm) { - IList retList = new FunctionalList<>(); - - if(strang.matches("\\[\\[\\S+\\]\\]")) { - if(strang.matches("\\[\\[\\S+:=\\S+\\]\\]")) { - doDefineExpandedVariable(vars, strang, gramm); - } else if(strang.matches("\\[\\[\\S+=\\S+\\]\\]")) { - doDefineVariable(vars, strang); - } else if(strang.matches("\\[\\[\\S+\\]\\]")) { - if(GrammarServerEngine.debugMode) { - if(strang.contains("+")) - System.out.println("Double-triggering no-space rule " + strang); - else - System.out.println("Triggered alternate no-space rule " + strang); - } - - doNoSpaceRule(strang, gramm, retList); - } else if (strang.contains("+")) { - if(GrammarServerEngine.debugMode) - System.out.println("Triggered alternate no-space rule " + strang); - - doNoSpaceRule(strang, gramm, retList); - } else { - // @FIXME notify the user they did something wrong - retList.add(strang); - } - } else { - if(strang.matches("\\[\\$\\S+\\-\\S+\\]")) { - retList = doExpandVariableReference(vars, strang, gramm); - } else if(strang.matches("\\[\\$\\S+\\]")) { - String varName = strang.substring(2, strang.length()); - - retList = vars.get(varName); - } else if (exportedRules.containsKey(strang) && - exportedRules.get(strang) != gramm && - !gramm.hasRule(strang)) { - // Only pick external rules if they are both - // a) in a different grammar - // b) not shadowed in the current grammar - WeightedGrammar exportGram = exportedRules.get(strang); - - retList = exportGram.generateGenericValues(strang, (s) -> s, " "); - } else if (strang.contains("+")) { - doNoSpaceRule(strang, gramm, retList); - } else { - // @FIXME notify the user they did something wrong - retList.add(strang); - } - } - - return retList; - } - - private static void handleRangeRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - - int start = Integer.parseInt(tokenizer.nextToken()); - int end = Integer.parseInt(tokenizer.nextToken()); - - state.addSpecialRule(ruleName, () -> { - int genNum = numgen.nextInt((end - start) + 1) + start; - - return new FunctionalList<>(Integer.toString(genNum)); - }); - } - - private static void handleRegexRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - - IList regx = tokenizer.toList(); - Generex regex = new Generex(ListUtils.collapseTokens(regx)); - - state.addSpecialRule(ruleName, () -> { - return new FunctionalList<>(regex.random().split(" ")); - }); - } - - private static int readOptionalProbability(FunctionalStringTokenizer tokenizer, ReaderState state) { - if (state.isUniform()) { - return 1; - } - - return Integer.parseInt(tokenizer.nextToken()); - } - - private static void initialRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String initialRuleName = tokenizer.nextToken(); - - state.setInitialRule(initialRuleName); - } - - private static void prefixRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - String prefixToken = tokenizer.nextToken(); - - int additionalProbability = readOptionalProbability(tokenizer, state); - - state.prefixRule(ruleName, prefixToken, additionalProbability); - } - - private static void removeRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - - state.deleteRule(ruleName); - } - - private static void suffixRule(FunctionalStringTokenizer tokenizer, ReaderState state) { - String ruleName = tokenizer.nextToken(); - String suffixToken = tokenizer.nextToken(); - - int additionalProbability = readOptionalProbability(tokenizer, state); - - state.suffixRule(ruleName, suffixToken, additionalProbability); - } - - private static void doCase(FunctionalStringTokenizer tokenizer, ReaderState state) { - int ruleProbability = readOptionalProbability(tokenizer, state); - - state.addCase(ruleProbability, tokenizer.toList()); - } - - private static void doDefineExpandedVariable(IMap> vars, String strang, WeightedGrammar gramm) { - String[] varParts = strang.split(":="); - - String varName = varParts[0].substring(2); - String ruleName = varParts[1].substring(0, varParts[1].length() - 2); - - IList varValue = gramm.generateGenericValues( - ruleName, (s) -> s, " "); - - vars.put(varName, varValue); - } - - private static void doDefineVariable(IMap> vars, String strang) { - String[] varParts = strang.split("="); - - String varName = varParts[0].substring(2); - String varValue = varParts[1].substring(0, varParts[1].length() - 2); - - vars.put(varName, new FunctionalList<>(varValue)); - } - - private static IList doExpandVariableReference(IMap> vars, String strang, - WeightedGrammar gramm) { - IList retList; - String[] varParts = strang.substring(1, strang.length()).split("-"); - - StringBuilder actualName = new StringBuilder("["); - - for(String varPart : varParts) { - if(varPart.startsWith("$")) { - IList varName = vars.get(varPart.substring(1)); - - if(varName.getSize() != 1) { - // @FIXME notify the user they did something wrong - } - - actualName.append(varName.first() + "-"); - } else { - actualName.append(varPart + "-"); - } - } - - // Trim trailing - - actualName.deleteCharAt(actualName.length() - 1); - - retList = gramm.generateGenericValues(actualName.toString(), (s) -> s, " "); - return retList; - } - - private static void doNoSpaceRule(String strang, WeightedGrammar gramm, IList retList) { - if(!GrammarServerEngine.debugMode) { - IList ruleValue = gramm.generateGenericValues( - strang, (s) -> s.trim(), ""); - - retList.add(ListUtils.collapseTokens(ruleValue)); - } else { - // if(!gramm.hasRule(strang)) - // System.out.println("Warning: Possible unexpanded rule " + strang); - - retList.add(strang); - } - } -} diff --git a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java index 0edaaa8..bc31847 100644 --- a/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java +++ b/RGens/src/main/java/bjc/rgens/text/markov/StandaloneMarkov.java @@ -3,13 +3,12 @@ package bjc.rgens.text.markov; import java.util.Map; public class StandaloneMarkov { - private int k; + private int k; private Map markovHash; - private String firstSub; + private String firstSub; - public StandaloneMarkov(int k, Map markovHash, - String firstSub) { + public StandaloneMarkov(int k, Map markovHash, String firstSub) { this.k = k; this.markovHash = markovHash; this.firstSub = firstSub; @@ -21,8 +20,7 @@ public class StandaloneMarkov { if (i == k) { text.append(firstSub); - if (text.length() > k) - i = text.length(); + if (text.length() > k) i = text.length(); } String sub = text.substring((i - k), (i)); -- cgit v1.2.3