diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-01-16 22:23:44 -0400 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-01-16 22:23:44 -0400 |
| commit | 9675e56458f701cca1a070a9700dcf30edf9ef66 (patch) | |
| tree | 7b14382432e66c3837439fcdf3def926fcfb8718 /RGens/src/main/java/bjc/rgens/newparser | |
| parent | 0c8618df90260fdb2213286fce274d0bc58f394a (diff) | |
Update
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/newparser')
| -rw-r--r-- | RGens/src/main/java/bjc/rgens/newparser/RGrammars.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammars.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammars.java new file mode 100644 index 0000000..b898726 --- /dev/null +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammars.java @@ -0,0 +1,47 @@ +package bjc.rgens.newparser; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Get access to the included grammars. + * + * @author Ben Culkin + */ +public class RGrammars { + private static RGrammarSet gramSet; + + private static void loadSet() { + URL rsc = RGrammarTest.class.getResource("/server-config-sample.cfg"); + + try { + Path cfgPath = Paths.get(rsc.toURI()); + + gramSet = RGrammarSet.fromConfigFile(cfgPath); + } catch (IOException | URISyntaxException ex) { + RuntimeException rtex = new RuntimeException("Could not load grammars"); + + rtex.initCause(ex); + + throw rtex; + } + } + + public static String generateExport(String exportName) throws GrammarException { + if(gramSet == null) loadSet(); + + if(!gramSet.getExportedRules().contains(exportName)) { + throw new GrammarException(String.format("No built-in rule named %s", exportName)); + } + + RGrammar gram = gramSet.getExportSource(exportName); + + String res = gram.generate(exportName); + if(exportName.contains("+")) res = res.replaceAll("\\s+", ""); + + return res; + } +} |
