summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/RGrammarSet.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-05 12:17:17 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-05 12:28:09 -0300
commit5c5e39be939e36e27c0d2b6bcbb14ba27bb88e33 (patch)
tree6deca1da63f493fecc03223fc557ee8722137ada /src/main/java/bjc/rgens/parser/RGrammarSet.java
parenteac274292f7053560d0f087acc182a5fcd86b162 (diff)
Move config loading to new class
Move config loading to a new class, in preparation for adding template support
Diffstat (limited to 'src/main/java/bjc/rgens/parser/RGrammarSet.java')
-rwxr-xr-xsrc/main/java/bjc/rgens/parser/RGrammarSet.java110
1 files changed, 1 insertions, 109 deletions
diff --git a/src/main/java/bjc/rgens/parser/RGrammarSet.java b/src/main/java/bjc/rgens/parser/RGrammarSet.java
index 4be4790..b84f5f0 100755
--- a/src/main/java/bjc/rgens/parser/RGrammarSet.java
+++ b/src/main/java/bjc/rgens/parser/RGrammarSet.java
@@ -1,14 +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.TreeMap;
-import java.util.Scanner;
import java.util.Set;
/**
@@ -27,7 +21,7 @@ public class RGrammarSet {
private Map<String, String> exportFrom;
/* 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;
@@ -214,106 +208,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();
-
- long startCFGTime = System.nanoTime();
-
- /* 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().toString().endsWith(".gram")) {
- /* Load grammar file. */
- try {
- long startFileTime = System.nanoTime();
-
- BufferedReader fis = Files.newBufferedReader(convPath);
- RGrammar gram = RGrammarParser.readGrammar(fis);
-
- fis.close();
-
- long endFileTime = System.nanoTime();
-
- long fileTime = endFileTime - startFileTime;
-
- if(PERF)
- System.err.printf("\tPERF: Read grammar %s in %d ns (%f s)\n", convPath, fileTime, fileTime / 1000000000.0);
-
- /* 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);
- }
- }
- }
-
- long endCFGTime = System.nanoTime();
-
- long cfgDur = endCFGTime - startCFGTime;
-
- if(PERF)
- System.err.printf("\n\nPERF: Read config file %s in %d ns (%f s)\n", cfgFile, cfgDur, cfgDur / 1000000000.0);
-
- return set;
- }
}