From e9ff21333162f6b3a516d91d4c814d5ebb78d88c Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 5 Sep 2018 23:47:34 -0300 Subject: Finally do something with regexizeRule It now works, but is called find-replace-rule instead. There is also reject-rule, to say to reject any rule that doesn't match a provided regex. NOTE: For providing the regexes, if they contain spaces, they must be quoted. --- src/main/java/bjc/rgens/parser/RGrammarParser.java | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/main/java/bjc/rgens/parser/RGrammarParser.java') diff --git a/src/main/java/bjc/rgens/parser/RGrammarParser.java b/src/main/java/bjc/rgens/parser/RGrammarParser.java index 4014baa..a95cefc 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarParser.java +++ b/src/main/java/bjc/rgens/parser/RGrammarParser.java @@ -169,18 +169,32 @@ public class RGrammarParser { build.setBinomial(parts.get(0), Integer.parseInt(parts.get(1)), Integer.parseInt(parts.get(2)), Integer.parseInt(parts.get(3))); }); - pragmas.put("regex-rule", (body, build, level) -> { - int nameIndex = body.indexOf(" "); + /* + * @NOTE 4/9/18 + * + * Consider if we want to replace this with something more akin + * to the `definer` feature from DiceLang. This will work fine + * in most cases, but there are some cases where you'd want the + * extra power. No examples are apparent at the moment. + */ + pragmas.put("find-replace-rule", (body, build, level) -> { + List bits = StringUtils.levelSplit(body, " "); - if(nameIndex == -1) { - throw new GrammarException("Regex-rule pragma takes two arguments: the name of the rule to process, then the regex to apply after the rule has been generated."); + if(bits.size() != 3) { + throw new GrammarException("Regex-rule pragma takes three arguments: the name of the rule to process, then the find/replace pair to apply after the rule has been generated."); } - String name = body.substring(0, nameIndex).trim(); - String patt = body.substring(nameIndex + 1).trim(); + build.findReplaceRule(bits.get(0), bits.get(1), bits.get(2)); + }); + + pragmas.put("reject-rule", (body, build, level) -> { + List bits = StringUtils.levelSplit(body, " "); + + if(bits.size() != 3) { + throw new GrammarException("Reject-rule pragma takes two arguments: the name of the rule to process, then the rejection pattern to apply after the rule has been generated."); + } - throw new GrammarException("Regexize-rule pragma not yet supported"); - //build.regexizeRule(name, patt); + build.rejectRule(bits.get(0), bits.get(1)); }); pragmas.put("prefix-with", (body, build, level) -> { -- cgit v1.2.3