diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-11-06 20:56:21 -0400 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-11-06 20:56:21 -0400 |
| commit | cba5ae6bc285439593f4e847c0d3b32e8e608ec8 (patch) | |
| tree | 7a5b6f6ca85aa9984730787d4e20fe38aec0a197 /RGens/src/main/java/bjc | |
| parent | 8d7b542ba7182b701a75a0fd5550e68f421ea696 (diff) | |
Minor source tweaks
More attempted spacing tweaks and stuff
Diffstat (limited to 'RGens/src/main/java/bjc')
5 files changed, 92 insertions, 28 deletions
diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java index a01aea4..3ffb4b2 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammar.java @@ -178,7 +178,6 @@ public class RGrammar { generateCase(start, new GenerationState(contents, rnd, vars)); String body = contents.toString(); - /* * Collapse duplicate spaces. */ @@ -190,11 +189,34 @@ public class RGrammar { * This can be done in the grammars, but it is very tedious to * do so. */ + + /* Handle 's */ body = body.replaceAll(" 's ", "'s "); - body = body.replaceAll(" ([,:.)\\]'\"]) ", "$1 "); - body = body.replaceAll(" (`[(\\[]) ", " $1"); + + /* Handle opening/closing punctuation. */ + body = body.replaceAll("([(\\[]) ", " $1"); + body = body.replaceAll(" ([)\\]'\"])", "$1 "); + + /* Remove spaces around series of opening/closing punctuation. */ + body = body.replaceAll("([(\\[])\\s+([(\\[])", "$1$2"); + body = body.replaceAll("([)\\]])\\s+([)\\]])", "$1$2"); + + /* Handle inter-word punctuation. */ + body = body.replaceAll(" ([,:.])", "$1 "); + + /* Handle intra-word punctuation. */ body = body.replaceAll("\\s?([-/])\\s?", "$1"); + /* + * Collapse duplicate spaces. + */ + body = body.replaceAll("\\s+", " "); + + /* @TODO 11/01/17 Ben Culkin :RegexRule + * Replace this once it is no longer needed. + */ + body = body.replaceAll("\\s(ish|burg|ton|ville|opolis|field|boro|dale)", "$1"); + return body; } @@ -205,7 +227,10 @@ public class RGrammar { case NORMAL: for (CaseElement elm : start.getElements()) { generateElement(elm, state); - state.contents.append(" "); + + if(elm.type != CaseElement.ElementType.VARDEF) { + state.contents.append(" "); + } } break; case SPACEFLATTEN: diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java index a6b21a5..9b89cdd 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarBuilder.java @@ -221,4 +221,25 @@ public class RGrammarBuilder { rules.get(ruleName).replaceCases(newCaseList); } + + public void regexizeRule(String rule, String pattern) { + if (rule == null) { + throw new NullPointerException("rule must not be null"); + } else if(pattern == null) { + throw new NullPointerException("pattern must not be null"); + } else if (rule.equals("")) { + throw new IllegalArgumentException("The empty string is not a valid rule name"); + } + + IList<RuleCase> caseList = rules.get(rule).getCases(); + + IList<RuleCase> newCaseList = new FunctionalList<>(); + + for(RuleCase cse : caseList) { + newCaseList.add(new RegexRuleCase(cse.getElements(), pattern)); + } + + rules.get(rule).replaceCases(newCaseList); + + } } diff --git a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java index 0f04fc1..3d2c921 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RGrammarParser.java @@ -70,6 +70,19 @@ public class RGrammarParser { } }); + pragmas.put("regex-rule", (body, build, level) -> { + int nameIndex = body.indexOf(" "); + + 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."); + } + + String name = body.substring(0, nameIndex).trim(); + String patt = body.substring(nameIndex + 1).trim(); + + build.regexizeRule(name, patt); + }); + pragmas.put("suffix-with", (body, build, level) -> { String[] parts = body.trim().split(" "); diff --git a/RGens/src/main/java/bjc/rgens/newparser/RegexRuleCase.java b/RGens/src/main/java/bjc/rgens/newparser/RegexRuleCase.java index 91e364a..fffa1bd 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RegexRuleCase.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RegexRuleCase.java @@ -1,7 +1,31 @@ package bjc.rgens.newparser; +import bjc.utils.funcdata.IList; + +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + public class RegexRuleCase extends RuleCase { + private Pattern patt; + public RegexRuleCase(IList<CaseElement> elements, String pattern) { - super(REGEX); + super(RuleCase.CaseType.REGEX); + + elementList = elements; + + try { + patt = Pattern.compile(pattern); + } catch (PatternSyntaxException psex) { + IllegalArgumentException iaex = + new IllegalArgumentException("This type requires a valid regular expression parameter"); + + iaex.initCause(psex); + + throw iaex; + } + } + + public Pattern getPattern() { + return patt; } } diff --git a/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java b/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java index 9b2cfc5..217d668 100644 --- a/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java +++ b/RGens/src/main/java/bjc/rgens/newparser/RuleCase.java @@ -39,28 +39,9 @@ public class RuleCase { * <dd>Used as the list of elementList the rule is composed of.</dd> * </dl> */ - private IList<CaseElement> elementList; - - /** - * Create a new case of the specified type. - * - * @param typ - * The type of case to create. - * - * @throws IllegalArgumentException - * If the type requires parameters. - */ - public RuleCase(CaseType typ) { - switch (typ) { - case NORMAL: - case SPACEFLATTEN: - throw new IllegalArgumentException("This type requires an element list parameter"); - case REGEX: - throw new IllegalArgumentException("This type requires an element list and a pattern"); - default: - break; - } + protected IList<CaseElement> elementList; + protected RuleCase(CaseType typ) { type = typ; } @@ -78,6 +59,8 @@ public class RuleCase { * If this type doesn't take a element list parameter. */ public RuleCase(CaseType typ, IList<CaseElement> elements) { + this(typ); + switch (typ) { case NORMAL: case SPACEFLATTEN: @@ -88,8 +71,6 @@ public class RuleCase { throw new IllegalArgumentException("This type doesn't have a element list parameter"); } - type = typ; - elementList = elements; } |
