blob: 9fe8dea775e45853b25b9477b359dda43b79d819 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package bjc.rgens.parser;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
* Get access to the included grammars.
*
* @author Ben Culkin
*/
public class RGrammars {
private static ConfigSet cfgSet;
private static void loadSet() {
try {
URI rsc = RGrammarTest.class.getResource("/server-config-sample.gcfg").toURI();
Map<String, String> env = new HashMap<>();
env.put("create", "true");
/* Ensure we can get at the file we need */
@SuppressWarnings("unused")
FileSystem zipfs = FileSystems.newFileSystem(rsc, env);
Path cfgPath = Paths.get(rsc);
cfgSet = ConfigLoader.fromConfigFile(cfgPath);
} catch (IOException | URISyntaxException ex) {
RuntimeException rtex = new RuntimeException("Could not load grammars");
rtex.initCause(ex);
throw rtex;
}
}
/**
* Generate an exported rule.
*
* @param exportName
* The rule to generate.
* @return The generated rule
* @throws GrammarException
* If something went wrong.
*/
public static String generateExport(String exportName) throws GrammarException {
if (cfgSet == null)
loadSet();
for(RGrammarSet gramSet : cfgSet.grammars.values()) {
if (!gramSet.getExportedRules().contains(exportName)) {
continue;
}
RGrammar gram = gramSet.getExportSource(exportName);
String res = gram.generate(exportName);
if (exportName.contains("+"))
res = res.replaceAll("\\s+", "");
return res;
}
throw new GrammarException(String.format("No exported rule named %s", exportName));
}
}
|