summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/RGrammarParser.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-09-05 17:19:41 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-09-05 17:19:41 -0300
commit1116d8450c0115ebb8a4201d130d2de6d5b1a107 (patch)
treed0f6a770cc7c8beda1d9cb1ce862750c9f0895b0 /src/main/java/bjc/rgens/parser/RGrammarParser.java
parente26cdec45a32c2fc3069dea7cddceab5e40a4a8b (diff)
Simplify affix application
This simplifies the internal way affixes are applied, as well as adding a new circumfix-with pragma
Diffstat (limited to 'src/main/java/bjc/rgens/parser/RGrammarParser.java')
-rwxr-xr-xsrc/main/java/bjc/rgens/parser/RGrammarParser.java44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/main/java/bjc/rgens/parser/RGrammarParser.java b/src/main/java/bjc/rgens/parser/RGrammarParser.java
index ba1bd8d..3fe4886 100755
--- a/src/main/java/bjc/rgens/parser/RGrammarParser.java
+++ b/src/main/java/bjc/rgens/parser/RGrammarParser.java
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Set;
import static bjc.rgens.parser.RGrammarLogging.*;
+import static bjc.rgens.parser.RGrammarBuilder.AffixType;
/**
* Reads {@link RGrammar} from a input stream.
*
@@ -182,30 +183,17 @@ public class RGrammarParser {
//build.regexizeRule(name, patt);
});
- pragmas.put("suffix-with", (body, build, level) -> {
- int idx = body.indexOf(" ");
-
- if (idx == -1) {
- String msg = "Suffix-with pragma takes at least two arguments, the name of the rule to suffix, then what to suffix it with\n\tThis can be more than one token, to get them suffixed as a group";
-
- throw new GrammarException(msg);
- }
-
- build.suffixWith(body.substring(0, idx), parseElementString(body.substring(idx + 1)).getLeft());
- });
-
pragmas.put("prefix-with", (body, build, level) -> {
- int idx = body.indexOf(" ");
-
- if (idx == -1) {
- String msg = "Prefix-with pragma takes at least two arguments, the name of the rule to prefix, then what to prefix it with\n\tThis can be more than one token, to get them prefixed as a group";
-
- throw new GrammarException(msg);
- }
+ doAffixWith(body, build, level, AffixType.PREFIX);
+ });
- build.prefixWith(body.substring(0, idx), parseElementString(body.substring(idx + 1)).getLeft());
+ pragmas.put("suffix-with", (body, build, level) -> {
+ doAffixWith(body, build, level, AffixType.SUFFIX);
});
+ pragmas.put("circumfix-with", (body, build, level) -> {
+ doAffixWith(body, build, level, AffixType.CIRCUMFIX);
+ });
/*
* @NOTE 9/4/18
*
@@ -254,6 +242,22 @@ public class RGrammarParser {
});
}
+ private static void doAffixWith(String body, RGrammarBuilder build, int level, AffixType afxType) {
+ int idx = body.indexOf(" ");
+
+ if (idx == -1) {
+ String msg = "Affixing pragma %s-with takes at least two arguments, the name of the rule to affix, then what to affix it with\n\tThis can be more than one token, to get them affixed as a group";
+
+ throw new GrammarException(String.format(msg, afxType.toString().toLowerCase()));
+ }
+
+ String rName = body.substring(0, idx);
+
+ IList<CaseElement> elms = parseElementString(body.substring(idx + 1)).getLeft();
+
+ build.affixWith(rName, elms, afxType);
+ }
+
private static void doAutoVar(String body, RGrammarBuilder build, int level, boolean isRule) {
List<String> bits = StringUtils.levelSplit(body, " ");