From d5ddd03d99c68411b1e0c2c69b76aa8db75431df Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 24 Jan 2016 21:28:40 +0100 Subject: [PATCH] Minus parser now accepts different minus signs --- .../main/java/jscl/text/ExponentParser.java | 4 +-- .../main/java/jscl/text/ExpressionParser.java | 5 ++- jscl/src/main/java/jscl/text/Factor.java | 5 ++- jscl/src/main/java/jscl/text/MinusParser.java | 35 +++++++------------ .../main/java/jscl/text/PlusOrMinusTerm.java | 11 +++--- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/jscl/src/main/java/jscl/text/ExponentParser.java b/jscl/src/main/java/jscl/text/ExponentParser.java index 3c99dfa3..fca07f30 100644 --- a/jscl/src/main/java/jscl/text/ExponentParser.java +++ b/jscl/src/main/java/jscl/text/ExponentParser.java @@ -20,9 +20,9 @@ class ExponentParser implements Parser { public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { int pos0 = p.position.intValue(); - boolean sign = MinusParser.parser.parse(p, previousSumElement).isSign(); + final boolean minus = MinusParser.parser.parse(p, previousSumElement); final Generic result = ParserUtils.parseWithRollback(UnsignedExponent.parser, pos0, previousSumElement, p); - return sign ? result.negate() : result; + return minus ? result.negate() : result; } } diff --git a/jscl/src/main/java/jscl/text/ExpressionParser.java b/jscl/src/main/java/jscl/text/ExpressionParser.java index 2a61851e..40f85e67 100644 --- a/jscl/src/main/java/jscl/text/ExpressionParser.java +++ b/jscl/src/main/java/jscl/text/ExpressionParser.java @@ -13,12 +13,11 @@ public class ExpressionParser implements Parser { } public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { - - boolean sign = MinusParser.parser.parse(p, previousSumElement).isSign(); + final boolean minus = MinusParser.parser.parse(p, previousSumElement); Generic result = TermParser.parser.parse(p, previousSumElement); - if (sign) { + if (minus) { result = result.negate(); } diff --git a/jscl/src/main/java/jscl/text/Factor.java b/jscl/src/main/java/jscl/text/Factor.java index d2f05150..86c962f1 100644 --- a/jscl/src/main/java/jscl/text/Factor.java +++ b/jscl/src/main/java/jscl/text/Factor.java @@ -18,11 +18,10 @@ class Factor implements Parser { } public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { - - boolean sign = MinusParser.parser.parse(p, previousSumElement).isSign(); + final boolean minus = MinusParser.parser.parse(p, previousSumElement); final Generic result = (Generic) UnsignedFactor.parser.parse(p, previousSumElement); - return sign ? result.negate() : result; + return minus ? result.negate() : result; } } diff --git a/jscl/src/main/java/jscl/text/MinusParser.java b/jscl/src/main/java/jscl/text/MinusParser.java index 1b633a23..c29a32f6 100644 --- a/jscl/src/main/java/jscl/text/MinusParser.java +++ b/jscl/src/main/java/jscl/text/MinusParser.java @@ -10,41 +10,30 @@ import javax.annotation.Nullable; * Date: 10/27/11 * Time: 2:44 PM */ -class MinusParser implements Parser { +class MinusParser implements Parser { - public static final Parser parser = new MinusParser(); + public static final Parser parser = new MinusParser(); private MinusParser() { } - @Nonnull - public Result parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) { - final boolean result; + static boolean isMinus(char c) { + return c == '-' || c == '−'; + } - int pos0 = p.position.intValue(); + @Nonnull + public Boolean parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) { + final int pos0 = p.position.intValue(); ParserUtils.skipWhitespaces(p); - if (p.position.intValue() < p.expression.length() && p.expression.charAt(p.position.intValue()) == '-') { - result = true; + final int pos1 = p.position.intValue(); + if (pos1 < p.expression.length() && isMinus(p.expression.charAt(pos1))) { p.position.increment(); + return true; } else { - result = false; p.position.setValue(pos0); - } - - return new Result(result); - } - - public static class Result { - private final boolean sign; - - public Result(boolean sign) { - this.sign = sign; - } - - public boolean isSign() { - return sign; + return false; } } } diff --git a/jscl/src/main/java/jscl/text/PlusOrMinusTerm.java b/jscl/src/main/java/jscl/text/PlusOrMinusTerm.java index 91e2b587..9128f5f5 100644 --- a/jscl/src/main/java/jscl/text/PlusOrMinusTerm.java +++ b/jscl/src/main/java/jscl/text/PlusOrMinusTerm.java @@ -18,13 +18,14 @@ class PlusOrMinusTerm implements Parser { } public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { - int pos0 = p.position.intValue(); + final int pos0 = p.position.intValue(); ParserUtils.skipWhitespaces(p); - boolean sign = false; - if (p.position.intValue() < p.expression.length() && (p.expression.charAt(p.position.intValue()) == '+' || p.expression.charAt(p.position.intValue()) == '-')) { - sign = p.expression.charAt(p.position.intValue()) == '-'; + boolean minus = false; + final int pos1 = p.position.intValue(); + if (pos1 < p.expression.length() && (p.expression.charAt(pos1) == '+' || MinusParser.isMinus(p.expression.charAt(pos1)))) { + minus = MinusParser.isMinus(p.expression.charAt(pos1)); p.position.increment(); } else { ParserUtils.throwParseException(p, pos0, Messages.msg_10, '+', '-'); @@ -32,7 +33,7 @@ class PlusOrMinusTerm implements Parser { final Generic result = ParserUtils.parseWithRollback(TermParser.parser, pos0, previousSumElement, p); - return sign ? result.negate() : result; + return minus ? result.negate() : result; } }