summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2020-11-11 12:29:59 -0400
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2020-11-11 12:29:59 -0400
commit80aa147aedc91356276d4346efb1ea62ea5b06f9 (patch)
treea57c8f2ad3a7e68ba1c0a5a2bea573a8da277ab6 /src/main/java/bjc/rgens/parser/elements/RuleCaseElement.java
parentc88c846b75dbc806db19a2e3a907bff21fd0c273 (diff)
parente9e0ca7bfe722375e7ccb25d2bafbe395b6c6a59 (diff)
Merge branch 'master' of https://github.com/bculkin2442/rgens
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);