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 {
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;
}
}

View File

@ -13,12 +13,11 @@ public class ExpressionParser implements Parser<Generic> {
}
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();
}

View File

@ -18,11 +18,10 @@ class Factor implements Parser<Generic> {
}
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;
}
}

View File

@ -10,41 +10,30 @@ import javax.annotation.Nullable;
* Date: 10/27/11
* 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() {
}
@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;
}
}
}

View File

@ -18,13 +18,14 @@ class PlusOrMinusTerm implements Parser<Generic> {
}
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<Generic> {
final Generic result = ParserUtils.parseWithRollback(TermParser.parser, pos0, previousSumElement, p);
return sign ? result.negate() : result;
return minus ? result.negate() : result;
}
}