use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class BasicPatternPropertiesTestCase method checkPriority.
public void checkPriority(String patternString, int priority) {
try {
ASTNode node = fParser.parse(patternString);
IExpr pat = AST2Expr.CONST.convert(node);
PatternMatcher matcher = new PatternMatcher(pat);
assertEquals(matcher.getPriority(), priority);
} catch (Exception e) {
e.printStackTrace();
assertEquals(0, priority);
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class BasicPatternPropertiesTestCase method comparePriority.
public void comparePriority(String patternString1, String patternString2, int result) {
try {
ASTNode node = fParser.parse(patternString1);
IExpr pat1 = AST2Expr.CONST.convert(node);
PatternMatcher matcher1 = new PatternMatcher(pat1);
node = fParser.parse(patternString2);
IExpr pat2 = AST2Expr.CONST.convert(node);
PatternMatcher matcher2 = new PatternMatcher(pat2);
assertEquals(matcher1.compareTo(matcher2), result);
} catch (Exception e) {
e.printStackTrace();
assertEquals(Integer.MAX_VALUE, result);
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class Parser method parseExpression.
/**
* See <a href="http://en.wikipedia.org/wiki/Operator-precedence_parser"> Operator -precedence parser</a> for the
* idea, how to parse the operators depending on their precedence.
*
* @param lhs
* the already parsed left-hand-side of the operator
* @param min_precedence
* @return
*/
private ASTNode parseExpression(ASTNode lhs, final int min_precedence) {
ASTNode rhs;
Operator oper;
InfixOperator infixOperator;
PostfixOperator postfixOperator;
while (true) {
if (fToken == TT_NEWLINE) {
return lhs;
}
if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER) || (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT) || (fToken == TT_SLOTSEQUENCE)) {
// lazy evaluation of multiplication
oper = fFactory.get("Times");
if (oper.getPrecedence() >= min_precedence) {
rhs = parseLookaheadOperator(oper.getPrecedence());
lhs = fFactory.createFunction(fFactory.createSymbol(oper.getFunctionName()), lhs, rhs);
continue;
}
} else {
if (fToken == TT_DERIVATIVE) {
lhs = parseDerivative(lhs);
}
if (fToken != TT_OPERATOR) {
break;
}
infixOperator = determineBinaryOperator();
if (infixOperator != null) {
if (infixOperator.getPrecedence() >= min_precedence) {
getNextToken();
ASTNode compoundExpressionNull = parseCompoundExpressionNull(infixOperator, lhs);
if (compoundExpressionNull != null) {
return compoundExpressionNull;
}
while (fToken == TT_NEWLINE) {
getNextToken();
}
rhs = parseLookaheadOperator(infixOperator.getPrecedence());
lhs = infixOperator.createFunction(fFactory, lhs, rhs);
while (fToken == TT_OPERATOR && infixOperator.getGrouping() == InfixOperator.NONE && infixOperator.getOperatorString().equals(fOperatorString)) {
getNextToken();
rhs = parseLookaheadOperator(infixOperator.getPrecedence());
((FunctionNode) lhs).add(rhs);
}
continue;
}
} else {
postfixOperator = determinePostfixOperator();
if (postfixOperator != null) {
if (postfixOperator.getPrecedence() >= min_precedence) {
getNextToken();
lhs = postfixOperator.createFunction(fFactory, lhs);
lhs = parseArguments(lhs);
continue;
}
} else {
throwSyntaxError("Operator: " + fOperatorString + " is no infix or postfix operator.");
}
}
}
break;
}
return lhs;
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class DoubleEvaluator method getVariables.
/**
* Get the variable names from the given expression.
*
* @param expression
* @param result
* a set which contains the variable names
* @param relaxedSyntax
* if <code>true</code> us e function syntax like
* <code>sin(x)</code> otherwise use <code>Sin[x]</code>.
*/
public static void getVariables(String expression, Set<String> result, boolean relaxedSyntax) {
Parser p = new Parser(relaxedSyntax ? ASTNodeFactory.RELAXED_STYLE_FACTORY : ASTNodeFactory.MMA_STYLE_FACTORY, relaxedSyntax);
ASTNode node = p.parse(expression);
getVariables(node, result);
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class Parser method parse.
/**
* Parse the given <code>expression</code> String into an ASTNode.
*
* @param expression
* a formula string which should be parsed.
* @return the parsed ASTNode representation of the given formula string
* @throws SyntaxError
*/
public ASTNode parse(final String expression) throws SyntaxError {
initialize(expression);
final ASTNode temp = parseExpression();
if (fToken != TT_EOF) {
if (fToken == TT_PRECEDENCE_CLOSE) {
throwSyntaxError("Too many closing ')'; End-of-file not reached.");
}
if (fToken == TT_LIST_CLOSE) {
throwSyntaxError("Too many closing '}'; End-of-file not reached.");
}
if (fToken == TT_ARGUMENTS_CLOSE) {
throwSyntaxError("Too many closing ']'; End-of-file not reached.");
}
throwSyntaxError("End-of-file not reached.");
}
return temp;
}
Aggregations