diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-22 17:10:50 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-22 17:10:50 -0400 |
| commit | 9d06ef82f53e156334ba86fbfedbdf02eb93552f (patch) | |
| tree | c4d4d9c13a4c75580aa21974ebc80df26dd8cae3 /RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java | |
| parent | 3cd931c1317ebe49cf109673640e0b2d916f884d (diff) | |
Reimplement more old features
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java')
| -rw-r--r-- | RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java index e67e356..fbcd8f7 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java @@ -1,7 +1,13 @@ package bjc.rgens.newparser; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +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.Set; /** @@ -99,7 +105,7 @@ public class RGrammarSet { } else if(!exportedRules.containsKey(exportName)) { throw new IllegalArgumentException(String.format("No export with name '%s' found", exportName)); } - + return exportedRules.get(exportName); } @@ -120,4 +126,84 @@ 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 { + RGrammarSet set = new RGrammarSet(); + + RGrammarParser parser = new RGrammarParser(); + + 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+", " "); + + 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. + */ + Path convPath = cfgParent.resolve(path); + + File fle = convPath.toFile(); + + if(fle.isDirectory()) { + /* + * TODO implement subset grammars + */ + throw new GrammarException("Sub-grammar sets aren't implemented yet"); + } else if(fle.getName().endsWith(".gram")) { + /* + * Load grammar files. + */ + try { + RGrammar gram = parser.readGrammar(new FileInputStream(fle)); + set.addGrammar(name, gram); + } catch(GrammarException gex) { + throw new GrammarException( + String.format("Error loading file '%s'", path), gex); + } + } else { + throw new GrammarException(String.format("Unrecognized file '%s'")); + } + } + } + + return set; + } } |
