Minus parser now accepts different minus signs

This commit is contained in:
serso 2016-01-24 21:28:40 +01:00
parent 27ee71a179
commit d5ddd03d99
5 changed files with 24 additions and 36 deletions

View File

@ -20,9 +20,9 @@ class ExponentParser implements Parser<Generic> {
public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException {
int pos0 = p.position.intValue(); 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); final Generic result = ParserUtils.parseWithRollback(UnsignedExponent.parser, pos0, previousSumElement, p);
return sign ? result.negate() : result; return minus ? result.negate() : result;
} }
} }

View File

@ -13,12 +13,11 @@ public class ExpressionParser implements Parser<Generic> {
} }
public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException {
final boolean minus = MinusParser.parser.parse(p, previousSumElement);
boolean sign = MinusParser.parser.parse(p, previousSumElement).isSign();
Generic result = TermParser.parser.parse(p, previousSumElement); Generic result = TermParser.parser.parse(p, previousSumElement);
if (sign) { if (minus) {
result = result.negate(); result = result.negate();
} }

View File

@ -18,11 +18,10 @@ class Factor implements Parser<Generic> {
} }
public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException { public Generic parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) throws ParseException {
final boolean minus = MinusParser.parser.parse(p, previousSumElement);
boolean sign = MinusParser.parser.parse(p, previousSumElement).isSign();
final Generic result = (Generic) UnsignedFactor.parser.parse(p, previousSumElement); final Generic result = (Generic) UnsignedFactor.parser.parse(p, previousSumElement);
return sign ? result.negate() : result; return minus ? result.negate() : result;
} }
} }

View File

@ -10,41 +10,30 @@ import javax.annotation.Nullable;
* Date: 10/27/11 * Date: 10/27/11
* Time: 2:44 PM * Time: 2:44 PM
*/ */
class MinusParser implements Parser<MinusParser.Result> { class MinusParser implements Parser<Boolean> {
public static final Parser<Result> parser = new MinusParser(); public static final Parser<Boolean> parser = new MinusParser();
private MinusParser() { private MinusParser() {
} }
@Nonnull static boolean isMinus(char c) {
public Result parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) { return c == '-' || c == '';
final boolean result; }
int pos0 = p.position.intValue(); @Nonnull
public Boolean parse(@Nonnull Parameters p, @Nullable Generic previousSumElement) {
final int pos0 = p.position.intValue();
ParserUtils.skipWhitespaces(p); ParserUtils.skipWhitespaces(p);
if (p.position.intValue() < p.expression.length() && p.expression.charAt(p.position.intValue()) == '-') { final int pos1 = p.position.intValue();
result = true; if (pos1 < p.expression.length() && isMinus(p.expression.charAt(pos1))) {
p.position.increment(); p.position.increment();
return true;
} else { } else {
result = false;
p.position.setValue(pos0); p.position.setValue(pos0);
} return false;
return new Result(result);
}
public static class Result {
private final boolean sign;
public Result(boolean sign) {
this.sign = sign;
}
public boolean isSign() {
return sign;
} }
} }
} }

View File

@ -18,13 +18,14 @@ class PlusOrMinusTerm implements Parser<Generic> {
} }
public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
int pos0 = p.position.intValue(); final int pos0 = p.position.intValue();
ParserUtils.skipWhitespaces(p); ParserUtils.skipWhitespaces(p);
boolean sign = false; boolean minus = false;
if (p.position.intValue() < p.expression.length() && (p.expression.charAt(p.position.intValue()) == '+' || p.expression.charAt(p.position.intValue()) == '-')) { final int pos1 = p.position.intValue();
sign = p.expression.charAt(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(); p.position.increment();
} else { } else {
ParserUtils.throwParseException(p, pos0, Messages.msg_10, '+', '-'); ParserUtils.throwParseException(p, pos0, Messages.msg_10, '+', '-');
@ -32,7 +33,7 @@ class PlusOrMinusTerm implements Parser<Generic> {
final Generic result = ParserUtils.parseWithRollback(TermParser.parser, pos0, previousSumElement, p); final Generic result = ParserUtils.parseWithRollback(TermParser.parser, pos0, previousSumElement, p);
return sign ? result.negate() : result; return minus ? result.negate() : result;
} }
} }