Skip to content

Commit

Permalink
Merge pull request #50 from cwisniew/fix-for-tru-fal
Browse files Browse the repository at this point in the history
Changes so variables starting with tru / fal will no longer fail
  • Loading branch information
Phergus authored Aug 16, 2020
2 parents 3e1bfd0 + 740932f commit 19ef2d6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
6 changes: 0 additions & 6 deletions src/main/antlr/net/rptools/parser/expression.g
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ constantExpression:
|
HEXNUMBER
|
TRUE
|
FALSE
|
variable
|
t1:SINGLE_QUOTED_STRING {#t1.setType(STRING);}
Expand Down Expand Up @@ -177,8 +173,6 @@ GE : ">=" ;
GT : ">" ;
LT : "<" ;
LE : "<=" ;
TRUE : "true";
FALSE : "false";

// Math operators
PLUS : '+' ;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/rptools/parser/DeterministicTreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package net.rptools.parser;

import static net.rptools.parser.ExpressionParserTokenTypes.ASSIGNEE;
import static net.rptools.parser.ExpressionParserTokenTypes.FALSE;
import static net.rptools.parser.ExpressionParserTokenTypes.FUNCTION;
import static net.rptools.parser.ExpressionParserTokenTypes.HEXNUMBER;
import static net.rptools.parser.ExpressionParserTokenTypes.NUMBER;
import static net.rptools.parser.ExpressionParserTokenTypes.OPERATOR;
import static net.rptools.parser.ExpressionParserTokenTypes.PROMPTVARIABLE;
import static net.rptools.parser.ExpressionParserTokenTypes.STRING;
import static net.rptools.parser.ExpressionParserTokenTypes.TRUE;
import static net.rptools.parser.ExpressionParserTokenTypes.UNARY_OPERATOR;
import static net.rptools.parser.ExpressionParserTokenTypes.VARIABLE;

Expand Down Expand Up @@ -52,8 +50,6 @@ public AST evaluate(AST node, VariableResolver resolver) throws ParserException
case NUMBER:
case HEXNUMBER:
case ASSIGNEE:
case TRUE:
case FALSE:
node.setNextSibling(evaluate(node.getNextSibling(), resolver));
return node;
case VARIABLE:
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/rptools/parser/EvaluationTreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package net.rptools.parser;

import static net.rptools.parser.ExpressionParserTokenTypes.ASSIGNEE;
import static net.rptools.parser.ExpressionParserTokenTypes.FALSE;
import static net.rptools.parser.ExpressionParserTokenTypes.FUNCTION;
import static net.rptools.parser.ExpressionParserTokenTypes.HEXNUMBER;
import static net.rptools.parser.ExpressionParserTokenTypes.NUMBER;
import static net.rptools.parser.ExpressionParserTokenTypes.OPERATOR;
import static net.rptools.parser.ExpressionParserTokenTypes.PROMPTVARIABLE;
import static net.rptools.parser.ExpressionParserTokenTypes.STRING;
import static net.rptools.parser.ExpressionParserTokenTypes.TRUE;
import static net.rptools.parser.ExpressionParserTokenTypes.UNARY_OPERATOR;
import static net.rptools.parser.ExpressionParserTokenTypes.VARIABLE;

Expand Down Expand Up @@ -54,10 +52,6 @@ public Object evaluate(AST node, VariableResolver resolver) throws ParserExcepti
String name = node.getText();
return name;
}
case TRUE:
return BigDecimal.ONE;
case FALSE:
return BigDecimal.ZERO;
case NUMBER:
{
BigDecimal d = new BigDecimal(node.getText());
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/rptools/parser/InlineTreeFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ private void format(AST node, StringBuilder sb) {
case VARIABLE:
case NUMBER:
case HEXNUMBER:
case TRUE:
case FALSE:
{
sb.append(node.getText());
return;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/rptools/parser/MapVariableResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package net.rptools.parser;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand All @@ -26,11 +27,23 @@
public class MapVariableResolver implements VariableResolver {
private final Map<String, Object> variables = new CaseInsensitiveHashMap<Object>();

private static final Map<String, Object> constants =
Map.of(
"true", BigDecimal.ONE,
"false", BigDecimal.ZERO);

public MapVariableResolver() {
variables.putAll(constants);
}

public boolean containsVariable(String name) throws ParserException {
return containsVariable(name, VariableModifiers.None);
}

public void setVariable(String name, Object value) throws ParserException {
if (constants.containsKey(name)) {
throw new ParserException(name + " can not be the target of assignment.");
}
setVariable(name, VariableModifiers.None, value);
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/net/rptools/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,24 @@ private void evaluateStringExpression(

assertEquals(answer, result);
}

public void testAssignToTrue() throws ParserException {
Parser p = new Parser();
try {
evaluateExpression(p, "true = 2", BigDecimal.valueOf(2));
fail("Was able to assign to 'true'");
} catch (ParserException ex) {
// Expected
}
}

public void testAssignToFalse() throws ParserException {
Parser p = new Parser();
try {
evaluateExpression(p, "false = 2", BigDecimal.valueOf(2));
fail("Was able to assign to 'fakse'");
} catch (ParserException ex) {
// Expected
}
}
}

0 comments on commit 19ef2d6

Please sign in to comment.