summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/RGrammarSet.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/rgens/parser/RGrammarSet.java')
-rwxr-xr-x[-rw-r--r--]src/main/java/bjc/rgens/parser/RGrammarSet.java129
1 files changed, 29 insertions, 100 deletions
diff --git a/src/main/java/bjc/rgens/parser/RGrammarSet.java b/src/main/java/bjc/rgens/parser/RGrammarSet.java
index 975510a..b110d21 100644..100755
--- a/src/main/java/bjc/rgens/parser/RGrammarSet.java
+++ b/src/main/java/bjc/rgens/parser/RGrammarSet.java
@@ -1,13 +1,8 @@
package bjc.rgens.parser;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
-import java.util.Scanner;
+import java.util.TreeMap;
import java.util.Set;
/**
@@ -16,26 +11,33 @@ import java.util.Set;
* @author EVE
*/
public class RGrammarSet {
+ public String name;
+
+ public ConfigSet belongsTo;
+
+ public RGrammar exportGrammar;
+
/* Contains all the grammars in this set. */
private Map<String, RGrammar> grammars;
/* Contains all the exported rules from grammars. */
- private Map<String, RGrammar> exportedRules;
-
- /* Contains which export came from which grammar. */
- private Map<String, String> exportFrom;
+ private Map<String, Rule> exportedRules;
/* Contains which file a grammar was loaded from. */
- private Map<String, String> loadedFrom;
+ public Map<String, String> loadedFrom;
+
+ public static final boolean PERF = true;
+ public static final boolean DEBUG = true;
/** Create a new set of randomized grammars. */
public RGrammarSet() {
grammars = new HashMap<>();
- exportedRules = new HashMap<>();
+ exportedRules = new TreeMap<>();
- exportFrom = new HashMap<>();
loadedFrom = new HashMap<>();
+
+ exportGrammar = new RGrammar(exportedRules);
}
/**
@@ -61,12 +63,17 @@ public class RGrammarSet {
}
grammars.put(grammarName, gram);
+ gram.belongsTo = this;
/* Process exports from the grammar. */
for (Rule export : gram.getExportedRules()) {
- exportedRules.put(export.name, gram);
+ if(exportedRules.containsKey(export.name))
+ System.err.printf("WARN: Shadowing rule %s in %s from %s\n", export.name, export.belongsTo.name, grammarName);
- exportFrom.put(export.name, grammarName);
+ exportedRules.put(export.name, export);
+
+ if(DEBUG)
+ System.err.printf("\t\tDEBUG: %s (%d cases) exported from %s\n", export.name, export.getCases().getSize(), grammarName);
}
/* Add exports to grammar. */
@@ -123,7 +130,7 @@ public class RGrammarSet {
throw new IllegalArgumentException(msg);
}
- return exportedRules.get(exportName);
+ return exportedRules.get(exportName).belongsTo;
}
/**
@@ -152,7 +159,12 @@ public class RGrammarSet {
throw new IllegalArgumentException(msg);
}
- return exportFrom.getOrDefault(exportName, "Unknown");
+ String nm = exportedRules.get(exportName).belongsTo.name;
+ if(nm == null) {
+ return "Unknown";
+ }
+
+ return nm;
}
/**
@@ -204,87 +216,4 @@ public class RGrammarSet {
public Set<String> getExportedRules() {
return exportedRules.keySet();
}
-
- /**
- * Load a grammar set from a configuration file.
- *
- * @param cfgFile
- * The configuration file to load from.
- *
- * @return
- * The grammar set created by the configuration file.
- *
- * @throws IOException
- * If something goes wrong during configuration loading.
- */
- public static RGrammarSet fromConfigFile(Path cfgFile) throws IOException {
- /* The grammar set to hand back. */
- RGrammarSet set = new RGrammarSet();
-
- /* Get the directory that contains the config file. */
- Path cfgParent = cfgFile.getParent();
-
- try(Scanner scn = new Scanner(cfgFile)) {
- /* Execute lines from the configuration file. */
- while (scn.hasNextLine()) {
- String ln = scn.nextLine().trim();
-
- /* Ignore blank/comment lines. */
- if (ln.equals("")) continue;
-
- if (ln.startsWith("#")) continue;
-
- /* Handle mixed whitespace. */
- ln = ln.replaceAll("\\s+", " ");
-
- /*
- * Get the place where the name of the grammar
- * ends.
- */
- int nameIdx = ln.indexOf(" ");
- if (nameIdx == -1) {
- throw new GrammarException("Must specify a name for a loaded grammar");
- }
-
- /* Name and path of grammar. */
- String name = ln.substring(0, nameIdx);
- Path path = Paths.get(ln.substring(nameIdx).trim());
-
- /*
- * Convert from configuration relative path to
- * absolute path.
- */
- Path convPath = cfgParent.resolve(path.toString());
-
- //if(Files.isDirectory(convPath)) {
- // /* @TODO implement subset grammars */
- // throw new GrammarException("Sub-grammar sets aren't implemented yet");
- //} else if (convPath.getFileName().endsWith(".gram")) {
- /* Load grammar file. */
- try {
- BufferedReader fis = Files.newBufferedReader(convPath);
- RGrammar gram = RGrammarParser.readGrammar(fis);
- fis.close();
-
- /* Add grammar to the set. */
- set.addGrammar(name, gram);
-
- /*
- * Mark where the grammar came
- * from.
- */
- set.loadedFrom.put(name, path.toString());
- } catch (GrammarException gex) {
- String msg = String.format("Error loading file '%s'", path);
- throw new GrammarException(msg, gex);
- }
- //} else {
- // String msg = String.format("Unrecognized file type '%s'", convPath.getFileName());
- // throw new GrammarException(msg);
- //}
- }
- }
-
- return set;
- }
}