diff options
Diffstat (limited to 'JPratt/src/examples/java')
4 files changed, 30 insertions, 14 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java b/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java index eb99350..45aac1a 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/AssignCommand.java @@ -18,7 +18,7 @@ class AssignCommand extends NonBinaryCommand<String, String, TestContext> { ParserContext<String, String, TestContext> ctx) throws ParserException { Token<String, String> name = operand.getHead(); - switch(name.getKey()) { + switch (name.getKey()) { case "(literal)": case "(vref)": break; diff --git a/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java b/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java index 71d4e72..67e560c 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/BlockEnter.java @@ -3,6 +3,7 @@ package bjc.pratt.examples; import bjc.pratt.Token; import bjc.utils.data.ITree; import bjc.utils.esodata.Directory; +import bjc.utils.esodata.Stack; import java.util.function.UnaryOperator; @@ -10,12 +11,14 @@ final class BlockEnter implements UnaryOperator<TestContext> { @Override public TestContext apply(TestContext state) { Directory<String, ITree<Token<String, String>>> enclosing = state.scopes.top(); - int currBlockNumber = state.blockCount.pop(); + Stack<Integer> blockCount = state.blockCount; + + int currBlockNumber = blockCount.pop(); state.scopes.push(enclosing.newSubdirectory("block" + currBlockNumber)); - state.blockCount.push(currBlockNumber + 1); - state.blockCount.push(0); + blockCount.push(currBlockNumber + 1); + blockCount.push(0); return state; } diff --git a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java index ebf0cc8..b7d31eb 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/PrattParserTest.java @@ -95,7 +95,7 @@ public class PrattParserTest { lo.addMultiDelimiters("(", ")"); lo.addMultiDelimiters("[", "]"); lo.addMultiDelimiters("{", "}"); - + hi.compile(); lo.compile(); @@ -205,7 +205,8 @@ public class PrattParserTest { parser.addNonInitialCommand(":", infixNon(3)); - parser.addNonInitialCommand("if", ternary(5, 0, "else", litToken("cond"), false)); + NonInitialCommand<String, String, TestContext> ifElse = ternary(5, 0, "else", litToken("cond"), false); + parser.addNonInitialCommand("if", ifElse); parser.addNonInitialCommand(":=", new AssignCommand(10)); @@ -246,18 +247,27 @@ public class PrattParserTest { parser.addNonInitialCommand(".", infixLeft(60)); - parser.addNonInitialCommand("[", postCircumfix(60, 0, "]", litToken("idx"))); + NonInitialCommand<String, String, TestContext> arrayIdx = postCircumfix(60, 0, "]", litToken("idx")); + parser.addNonInitialCommand("[", arrayIdx); - parser.addInitialCommand("if", preTernary(0, 0, 0, "then", "else", litToken("ifelse"))); + InitialCommand<String, String, TestContext> ifThenElse = preTernary(0, 0, 0, "then", "else", + litToken("ifelse")); + parser.addInitialCommand("if", ifThenElse); - parser.addInitialCommand("(", grouping(0, ")", litToken("parens"))); + InitialCommand<String, String, TestContext> parens = grouping(0, ")", litToken("parens")); + parser.addInitialCommand("(", parens); - parser.addInitialCommand("begin", delimited(0, ";", "end", litToken("block"), new BlockEnter(), idfun, - new BlockExit(), true)); + InitialCommand<String, String, TestContext> scoper = delimited(0, ";", "end", litToken("block"), + new BlockEnter(), idfun, new BlockExit(), true); + parser.addInitialCommand("begin", scoper); - parser.addInitialCommand("[", delimited(0, ",", "]", litToken("array"), idfun, idfun, idfun, false)); + InitialCommand<String, String, TestContext> arrayLiteral = delimited(0, ",", "]", litToken("array"), + idfun, idfun, idfun, false); + parser.addInitialCommand("[", arrayLiteral); - parser.addInitialCommand("{", delimited(0, ",", "}", litToken("json"), idfun, idfun, idfun, false)); + InitialCommand<String, String, TestContext> jsonLiteral = delimited(0, ",", "}", litToken("json"), + idfun, idfun, idfun, false); + parser.addInitialCommand("{", jsonLiteral); parser.addInitialCommand("case", unary(5)); @@ -277,4 +287,4 @@ public class PrattParserTest { return parser; } -} +}
\ No newline at end of file diff --git a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java index f6ecf98..0fccc60 100644 --- a/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java +++ b/JPratt/src/examples/java/bjc/pratt/examples/Tokenizer.java @@ -26,6 +26,9 @@ final class Tokenizer implements Function<String, Token<String, String>> { } else if (ctx.scopes.top().containsKey(strang)) { return new StringToken("(vref)", strang); } else if(strang.matches("(?:[\\u00B2\\u00B3\\u00B9\\u2070]|[\\u2074-\\u2079])+")) { + /* + * This regular expression matches series of unicode super-scripts 1-9. + */ return new StringToken("(superexp)", strang); } else { return new StringToken("(literal)", strang); |
