summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-04 19:28:04 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-04 19:28:04 -0300
commit3e6e258a63d30e9ae57cc3f08efdcf95d6a3c44d (patch)
tree694c04419eab6e8322026a531087c22a22d3da47
parent4bba1173779dc7933c651785d491fb089515e1e2 (diff)
Minor updates
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java11
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java65
-rw-r--r--RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java22
3 files changed, 56 insertions, 42 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
index 0c2e3d9..cb9f686 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java
@@ -291,9 +291,14 @@ public class RGrammarParser {
throw new GrammarException("A rule must be given at least one case in its declaration, and"
+ "seperated from that case by \u2192");
}
-
- System.out.println(
- "WARNING: Empty space separating a declaration and its case is deprecated. Use \u2192 instead");
+
+ /*
+ * @NOTE
+ *
+ * This is true, but I don't care that much anyways.
+ * System.out.println(
+ * "WARNING: Empty space separating a declaration and its case is deprecated. Use \u2192 instead");
+ */
}
String ruleName = declContents.substring(0, declSep).trim();
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java
index 4aace76..eb2ba56 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarSet.java
@@ -17,24 +17,16 @@ import java.util.Set;
*
*/
public class RGrammarSet {
- /*
- * Contains all the grammars in this set.
- */
+ /* Contains all the grammars in this set. */
private Map<String, RGrammar> grammars;
- /*
- * Contains all the exported rules from grammars.
- */
+ /* Contains all the exported rules from grammars. */
private Map<String, RGrammar> exportedRules;
- /*
- * Contains which export came from which grammar.
- */
+ /* Contains which export came from which grammar. */
private Map<String, String> exportFrom;
- /*
- * Contains which file a grammar was loaded from.
- */
+ /* Contains which file a grammar was loaded from. */
private Map<String, String> loadedFrom;
/**
@@ -62,6 +54,7 @@ public class RGrammarSet {
* If the grammar name is invalid.
*/
public void addGrammar(String grammarName, RGrammar gram) {
+ /* Make sure a grammar is valid. */
if (grammarName == null) {
throw new NullPointerException("Grammar name must not be null");
} else if (gram == null) {
@@ -72,11 +65,13 @@ public class RGrammarSet {
grammars.put(grammarName, gram);
+ /* Process exports from the grammar. */
for (Rule export : gram.getExportedRules()) {
exportedRules.put(export.name, gram);
exportFrom.put(export.name, grammarName);
}
+ /* Add exports to grammar. */
gram.setImportedRules(exportedRules);
}
@@ -93,6 +88,7 @@ public class RGrammarSet {
* set.
*/
public RGrammar getGrammar(String grammarName) {
+ /* Check arguments. */
if (grammarName == null) {
throw new NullPointerException("Grammar name must not be null");
} else if (grammarName.equals("")) {
@@ -119,6 +115,7 @@ public class RGrammarSet {
* set.
*/
public RGrammar getExportSource(String exportName) {
+ /* Check arguments. */
if (exportName == null) {
throw new NullPointerException("Export name must not be null");
} else if (exportName.equals("")) {
@@ -146,6 +143,7 @@ public class RGrammarSet {
* this set.
*/
public String exportedFrom(String exportName) {
+ /* Check arguments. */
if (exportName == null) {
throw new NullPointerException("Export name must not be null");
} else if (exportName.equals("")) {
@@ -173,6 +171,7 @@ public class RGrammarSet {
* this set.
*/
public String loadedFrom(String grammarName) {
+ /* Check arguments. */
if (grammarName == null) {
throw new NullPointerException("Grammar name must not be null");
} else if (grammarName.equals("")) {
@@ -217,71 +216,69 @@ public class RGrammarSet {
* 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.
- */
+ /* Execute lines from the configuration file. */
while (scn.hasNextLine()) {
String ln = scn.nextLine().trim();
- /*
- * Ignore blank/comment lines.
- */
+ /* Ignore blank/comment lines. */
if (ln.equals("")) continue;
if (ln.startsWith("#")) continue;
- /*
- * Handle mixed whitespace.
- */
+ /* 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.
- */
+ /* Name and path of grammar. */
String name = ln.substring(0, nameIdx);
Path path = Paths.get(ln.substring(nameIdx).trim());
/*
- * Convert from configuration relative path.
+ * Convert from configuration relative path to
+ * absolute path.
*/
Path convPath = cfgParent.resolve(path);
-
File fle = convPath.toFile();
if (fle.isDirectory()) {
- /*
- * TODO implement subset grammars
- */
+ /* @TODO implement subset grammars */
throw new GrammarException("Sub-grammar sets aren't implemented yet");
} else if (fle.getName().endsWith(".gram")) {
- /*
- * Load grammar files.
- */
+ /* Load grammar file. */
try {
FileReader fis = new FileReader(fle);
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 '%s'");
+ String msg = String.format("Unrecognized file type '%s'");
throw new GrammarException(msg);
}
}
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java
index 7bfd762..97440f6 100644
--- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java
+++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarTest.java
@@ -3,6 +3,7 @@ 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;
/**
@@ -22,22 +23,32 @@ public class RGrammarTest {
URL rsc = RGrammarTest.class.getResource("/server-config-sample.cfg");
try {
- RGrammarSet gramSet = RGrammarSet.fromConfigFile(Paths.get(rsc.toURI()));
+ /* Load a grammar set. */
+ Path cfgPath = Paths.get(rsc.toURI());
+ RGrammarSet gramSet = RGrammarSet.fromConfigFile(cfgPath);
+ /* Generate rule suggestions for all the grammars in the set. */
for (String gramName : gramSet.getGrammars()) {
gramSet.getGrammar(gramName).generateSuggestions();
}
+ /* Generate for each exported rule. */
for (String exportName : gramSet.getExportedRules()) {
RGrammar grammar = gramSet.getExportSource(exportName);
for (int i = 0; i < 10; i++) {
try {
- grammar.generate(exportName);
+ System.out.printf("Generating for exported rule '%s'\n", exportName);
+ String res = grammar.generate(exportName);
+ System.out.printf("\tContents: %s\n", res);
} catch (GrammarException gex) {
- System.out.println("Error in exported rule " + exportName
- + " (loaded from "
- + gramSet.loadedFrom(gramSet.exportedFrom(exportName)));
+ /*
+ * Print out errors with generation.
+ */
+ String fmt = "Error in exported rule '%s' (loaded from '%s')\n";
+ String loadSrc = gramSet.loadedFrom(gramSet.exportedFrom(exportName));
+
+ System.out.printf(fmt, exportName, loadSrc);
System.out.println();
@@ -48,6 +59,7 @@ public class RGrammarTest {
}
}
}
+
} catch (IOException ioex) {
ioex.printStackTrace();
} catch (URISyntaxException urisex) {