/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2015:
* Stanislav Poslavsky
* Bolotin Dmitriy
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see .
*/
package cc.redberry.core.number.parser;
import cc.redberry.core.number.Complex;
import cc.redberry.core.number.Rational;
import cc.redberry.core.number.Real;
/**
* @author Stanislav Poslavsky
*/
public class NumberParser> {
@SuppressWarnings("unchecked")
private static final TokenParser[] ComplexTokens = new TokenParser[]{
BracketToken.INSTANCE,
new OperatorToken('+', '-') {
@Override
protected Complex neutral() {
return Complex.ZERO;
}
@Override
protected Complex operation(Complex c1, Complex c2, boolean mode) {
if (mode)
return c1.subtract(c2);
return c1.add(c2);
}
},
new OperatorToken('*', '/') {
@Override
protected Complex neutral() {
return Complex.ONE;
}
@Override
protected Complex operation(Complex c1, Complex c2, boolean mode) {
if (mode)
return c1.divide(c2);
return c1.multiply(c2);
}
},
ComplexToken.INSTANCE
};
@SuppressWarnings("unchecked")
private static final TokenParser[] RealTokens = new TokenParser[]{
BracketToken.INSTANCE,
new OperatorToken('+', '-') {
@Override
protected Real neutral() {
return Rational.ZERO;
}
@Override
protected Real operation(Real c1, Real c2, boolean mode) {
if (mode)
return c1.subtract(c2);
return c1.add(c2);
}
},
new OperatorToken('*', '/') {
@Override
protected Real neutral() {
return Rational.ONE;
}
@Override
protected Real operation(Real c1, Real c2, boolean mode) {
if (mode)
return c1.divide(c2);
return c1.multiply(c2);
}
},
RealToken.INSTANCE
};
public static final NumberParser REAL_PARSER = new NumberParser<>(RealTokens);
public static final NumberParser COMPLEX_PARSER = new NumberParser<>(ComplexTokens);
private final TokenParser[] parsers;
private NumberParser(TokenParser[] parsers) {
this.parsers = parsers;
}
public T parse(String expression) {
expression = expression.trim();
for (TokenParser parser : parsers) {
@SuppressWarnings("unchecked") T element = (T) parser.parse(expression, this);
if (element == null)
continue;
return element;
}
throw new NumberFormatException();
}
}
Ads help maintain this website.