summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-10-06 19:16:26 -0400
committerBen Culkin <scorpress@gmail.com>2020-10-06 19:16:26 -0400
commite9e0ca7bfe722375e7ccb25d2bafbe395b6c6a59 (patch)
tree8e8e556724683d4d10d7da1f3e85705a50ae33c6 /src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
parenta3ea557c0b7204f56b1499687cc7f82c5b1677a5 (diff)
Info cleanup
Diffstat (limited to 'src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java')
-rwxr-xr-xsrc/main/java/bjc/rgens/parser/elements/RuleCaseElement.java70
1 files changed, 51 insertions, 19 deletions
diff --git a/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java b/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
index 1a2cf85..7fe6603 100755
--- a/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
+++ b/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
@@ -8,48 +8,81 @@ import bjc.rgens.parser.RGrammar;
import bjc.rgens.parser.Rule;
import bjc.rgens.parser.elements.vars.VariableElement;
+/**
+ * Case element which references a rule.
+ *
+ * @author Ben Culkin
+ *
+ */
public class RuleCaseElement extends CaseElement {
+ /**
+ * The elements for this rule.
+ */
public List<VariableElement> elements;
+ /**
+ * Create a new case element to reference a rule.
+ *
+ * @param vl
+ * The text of the reference.
+ */
public RuleCaseElement(String vl) {
super(true);
this.elements = VariableElement.parseElementString(vl);
}
+ /**
+ * Create a new case element to reference a rule.
+ *
+ * @param vl
+ * The text of the reference.
+ * @param elements
+ * The elements of the reference.
+ */
public RuleCaseElement(String vl, List<VariableElement> elements) {
super(true);
this.elements = elements;
}
+ @Override
public void generate(GenerationState state) {
GenerationState newState = state.newBuf();
boolean inName = false;
- for(VariableElement elm : elements) {
+ for (VariableElement elm : elements) {
elm.generate(newState);
- if(inName == false) inName = elm.forbidSpaces;
+ if (inName == false)
+ inName = elm.forbidSpaces;
}
String body = newState.getContents();
- if(inName) {
+ if (inName) {
doGenerate(String.format("[%s]", body), state);
} else {
state.appendContents(body);
}
}
+ /**
+ * Do the generation of a rule element.
+ *
+ * @param acName
+ * The name of the rule to generation.
+ * @param state
+ * The generation state.
+ */
protected void doGenerate(String acName, GenerationState state) {
GenerationState newState = state.newBuf();
Rule rl;
String actName = acName;
-
+
if (actName.startsWith("[^")) {
actName = "[" + actName.substring(2);
@@ -58,12 +91,11 @@ public class RuleCaseElement extends CaseElement {
rl = state.findRule(actName, true);
}
- if(rl != null) {
+ if (rl != null) {
RGrammar destGrammar = rl.belongsTo;
newState.swapGrammar(destGrammar);
- /*
- * Don't postprocess the string, we should only do that
- * once.
+ /*
+ * Don't postprocess the string, we should only do that once.
*/
String res = destGrammar.generate(actName, newState, false);
newState.setContents(res);
@@ -74,17 +106,17 @@ public class RuleCaseElement extends CaseElement {
* Re-get this working again.
*/
/*
- if (ruleSearcher != null) {
- Set<Match<? extends String>> results = ruleSearcher.search(actName, MAX_DISTANCE);
-
- String[] resArray = results.stream().map(Match::getMatch).toArray((i) -> new String[i]);
-
- String msg = String.format("No rule '%s' defined (perhaps you meant %s?)", actName,
- StringUtils.toEnglishList(resArray, false));
-
- throw new GrammarException(msg);
- }
- */
+ * if (ruleSearcher != null) { Set<Match<? extends String>> results =
+ * ruleSearcher.search(actName, MAX_DISTANCE);
+ *
+ * String[] resArray = results.stream().map(Match::getMatch).toArray((i) ->
+ * new String[i]);
+ *
+ * String msg = String.format("No rule '%s' defined (perhaps you meant %s?)",
+ * actName, StringUtils.toEnglishList(resArray, false));
+ *
+ * throw new GrammarException(msg); }
+ */
String msg = String.format("No rule '%s' defined", actName);
throw new GrammarException(msg);