diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/interfaces/IAST.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/interfaces/IAST.java index efc115501a..952529ae8e 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/interfaces/IAST.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/interfaces/IAST.java @@ -243,6 +243,14 @@ default IAST functionEvaled() { */ public IExpr arg1(); + /** + * Get the argument of the {@link IAST} function (i.e. {@link #get(int)} ) and if the expression + * is the function {@link F#Unevaluated(IExpr)} return the first argument of the + * {@link F#Unevaluated(IExpr)} function. + * + * @param position + * @return + */ default IExpr getUnevaluated(int position) { return get(position); } diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParser.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParser.java index f44d8efa00..96097e3fa7 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParser.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParser.java @@ -70,9 +70,6 @@ public static boolean test(final String str, EvalEngine engine) { try { ExprParser fParser = new ExprParser(engine); final IExpr parsedExpression = fParser.parse(str); - // final Parser fParser = new Parser(); - // final ASTNode parsedAST = fParser.parse(str); - // final IExpr parsedExpression = AST2Expr.CONST.convert(parsedAST); if (parsedExpression != null) { return true; } @@ -92,8 +89,6 @@ public static boolean test(final String str, EvalEngine engine) { */ private final boolean fRelaxedSyntax; - // private List fNodeList = null; - private final EvalEngine fEngine; protected IParserFactory fFactory; @@ -112,12 +107,6 @@ public ExprParser(final EvalEngine engine, final boolean relaxedSyntax) { this(engine, ExprParserFactory.MMA_STYLE_FACTORY, relaxedSyntax); } - // public ExprParser(final EvalEngine engine, final boolean relaxedSyntax, - // boolean packageMode) { - // this(engine, ExprParserFactory.MMA_STYLE_FACTORY, relaxedSyntax, - // packageMode); - // } - /** * @param engine * @param factory @@ -134,16 +123,13 @@ public ExprParser(final EvalEngine engine, IParserFactory factory, final boolean this.fRelaxedSyntax = relaxedSyntax; this.fFactory = factory; this.fEngine = engine; - // if (packageMode) { - // fNodeList = new ArrayList(256); - // } } private IExpr convert(IASTMutable ast) { int headID = ast.headID(); - if (headID >= ID.Blank) { + if (headID >= ID.Blank && headID <= ID.Sqrt) { + // ID.Blank is lowest and ID.Sqrt is highest integer ID in followinh switch statement IExpr expr = F.NIL; - // ID.Blank is lowest integer >ID in switch statement switch (headID) { case ID.Get: if (ast.isAST1() && ast.arg1().isString()) { @@ -295,13 +281,6 @@ private IExpr createInfixFunction(InfixExprOperator infixOperator, IExpr lhs, IE return convert(temp); } return temp; - // if (infixOperator.getOperatorString().equals("//")) { - // // lhs // rhs ==> rhs[lhs] - // IAST function = F.ast(rhs); - // function.add(lhs); - // return function; - // } - // return F.$(F.$s(infixOperator.getFunctionName()), lhs, rhs); } /** @@ -552,16 +531,13 @@ private IExpr getBlanks(IExpr temp) { if (isWhitespace()) { getNextToken(); temp = F.$b(); - // temp = fFactory.createPattern(null, null); } else { getNextToken(); if (fToken == TT_IDENTIFIER) { final IExpr check = getSymbol(true); temp = F.$b(check); - // temp = fFactory.createPattern(null, check); } else { temp = F.$b(); - // temp = fFactory.createPattern(null, null); } } break; @@ -570,16 +546,13 @@ private IExpr getBlanks(IExpr temp) { if (isWhitespace()) { getNextToken(); temp = F.$ps(null, null); - // temp = fFactory.createPattern2(null, null); } else { getNextToken(); if (fToken == TT_IDENTIFIER) { final IExpr check = getSymbol(true); temp = F.$ps(null, check); - // temp = fFactory.createPattern2(null, check); } else { temp = F.$ps(null, null); - // temp = fFactory.createPattern2(null, null); } } break; @@ -588,16 +561,13 @@ private IExpr getBlanks(IExpr temp) { if (isWhitespace()) { getNextToken(); temp = F.$ps(null, null, false, true); - // temp = fFactory.createPattern3(null, null); } else { getNextToken(); if (fToken == TT_IDENTIFIER) { final IExpr check = getSymbol(true); temp = F.$ps(null, check, false, true); - // temp = fFactory.createPattern3(null, check); } else { temp = F.$ps(null, null, false, true); - // temp = fFactory.createPattern3(null, null); } } break; @@ -606,16 +576,13 @@ private IExpr getBlanks(IExpr temp) { if (isWhitespace()) { getNextToken(); temp = F.$b(null, true); - // temp = fFactory.createPattern(null, null, true); } else { getNextToken(); if (fToken == TT_IDENTIFIER) { final IExpr check = getSymbol(true); temp = F.$b(check, true); - // temp = fFactory.createPattern(null, check, true); } else { temp = F.$b(null, true); - // temp = fFactory.createPattern(null, null, true); } } break; @@ -1203,12 +1170,7 @@ private IExpr parseCompoundExpressionNull(InfixExprOperator infixOperator, IExpr if (fToken == TT_EOF || fToken == TT_ARGUMENTS_CLOSE || fToken == TT_LIST_CLOSE || fToken == TT_PRECEDENCE_CLOSE || fToken == TT_COMMA) { return createInfixFunction(infixOperator, lhs, S.Null); - // return infixOperator.createFunction(fFactory, rhs, - // fFactory.createSymbol("Null")); } - // if (fPackageMode && fRecursionDepth < 1) { - // return createInfixFunction(infixOperator, rhs, F.Null); - // } } return null; } @@ -1513,13 +1475,6 @@ private IExpr parseLookaheadOperator(final int min_precedence) { || (fOperatorString.equals(":") && rhs.isSymbol()) || ((infixOperator.getPrecedence() == min_precedence) && (infixOperator.getGrouping() == InfixExprOperator.RIGHT_ASSOCIATIVE))) { - // if (infixOperator.isOperator(";")) { - // rhs = F.Null; - // if (fPackageMode && fRecursionDepth < 1) { - // return createInfixFunction(infixOperator, lhs, - // rhs); - // } - // } rhs = parseExpression(rhs, infixOperator.getPrecedence()); continue; } @@ -1529,9 +1484,6 @@ private IExpr parseLookaheadOperator(final int min_precedence) { if (postfixOperator != null) { if (postfixOperator.getPrecedence() >= min_precedence) { getNextToken(); - // rhs = - // F.$(F.$s(postfixOperator.getFunctionName()), - // rhs); rhs = convert(postfixOperator.createFunction(fFactory, rhs)); continue; } @@ -1588,23 +1540,17 @@ public void parsePackage(final String expression) throws SyntaxError { } if (fToken == TT_EOF) { return; - // return fNodeList; } temp = parseExpression(); fEngine.evaluate(temp); - // fNodeList.add(temp); - // throwSyntaxError("End-of-file not reached."); } - // return fNodeList; } private IExpr parsePrimary(final int min_precedence) { if (fToken == TT_OPERATOR) { if (fOperatorString.equals(".")) { fCurrentChar = '.'; - // fToken = TT_DIGIT; - // return getPart(); return getNumber(false); } final PrefixExprOperator prefixOperator = determinePrefixOperator(); diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParserFactory.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParserFactory.java index 86e0fc4339..90da1a2971 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParserFactory.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/parser/ExprParserFactory.java @@ -454,32 +454,13 @@ private static void init() { fOperatorTokenStartSet = ParserConfig.TRIE_STRING2OPERATORLIST_BUILDER.withMatch(TrieMatch.EXACT).build(); - // if (fuzzyParser) { - // for (int i = 0; i < HEADER_STRINGS.length; i++) { - // if (OPERATOR_STRINGS[i] == "=") { - // addOperator(fOperatorMap, fOperatorTokenStartSet, OPERATOR_STRINGS[i], "Equal", - // EQUAL_OPERATOR); - // } else { - // addOperator(fOperatorMap, fOperatorTokenStartSet, OPERATOR_STRINGS[i], HEADER_STRINGS[i], - // OPERATORS[i]); - // } - // String unicodeChar = org.matheclipse.parser.client.Characters.NamedCharactersMap - // .get(HEADER_STRINGS[i]); - // if (unicodeChar != null) { - // addOperator(fOperatorMap, fOperatorTokenStartSet, unicodeChar, HEADER_STRINGS[i], - // OPERATORS[i]); - // buf.append(unicodeChar); - // } - // } - // } else { for (int i = 0; i < HEADER_STRINGS.length; i++) { addOperator(fOperatorMap, fOperatorTokenStartSet, OPERATOR_STRINGS[i], HEADER_STRINGS[i], OPERATORS[i]); String unicodeChar = org.matheclipse.parser.client.Characters.NamedCharactersMap.get(HEADER_STRINGS[i]); if (unicodeChar != null) { - addOperator(fOperatorMap, fOperatorTokenStartSet, unicodeChar, HEADER_STRINGS[i], - OPERATORS[i]); + addUnicodeOperator(fOperatorMap, fOperatorTokenStartSet, unicodeChar, OPERATORS[i]); buf.append(unicodeChar); } } @@ -493,17 +474,30 @@ public static void initialize() { } public static void addOperator(final Map operatorMap, - final Map> operatorTokenStartSet, final String operatorStr, - final String headStr, final Operator oper) { + final Map> operatorTokenStartSet, final String operatorToken, + final String headStr, final Operator operator) { ArrayList list; - operatorMap.put(headStr, oper); - list = operatorTokenStartSet.get(operatorStr); + operatorMap.put(headStr, operator); + list = operatorTokenStartSet.get(operatorToken); + if (list == null) { + list = new ArrayList(2); + list.add(operator); + operatorTokenStartSet.put(operatorToken, list); + } else { + list.add(operator); + } + } + + private static void addUnicodeOperator(final Map operatorMap, + final Map> operatorTokenStartSet, + final String operatorUnicodeToken, final Operator operator) { + ArrayList list = operatorTokenStartSet.get(operatorUnicodeToken); if (list == null) { list = new ArrayList(2); - list.add(oper); - operatorTokenStartSet.put(operatorStr, list); + list.add(operator); + operatorTokenStartSet.put(operatorUnicodeToken, list); } else { - list.add(oper); + list.add(operator); } } diff --git a/symja_android_library/matheclipse-parser/src/main/java/org/matheclipse/parser/client/operator/ASTNodeFactory.java b/symja_android_library/matheclipse-parser/src/main/java/org/matheclipse/parser/client/operator/ASTNodeFactory.java index bf0455b75b..5a4d414cfc 100644 --- a/symja_android_library/matheclipse-parser/src/main/java/org/matheclipse/parser/client/operator/ASTNodeFactory.java +++ b/symja_android_library/matheclipse-parser/src/main/java/org/matheclipse/parser/client/operator/ASTNodeFactory.java @@ -421,8 +421,7 @@ private static void init() { String unicodeChar = org.matheclipse.parser.client.Characters.NamedCharactersMap.get(HEADER_STRINGS[i]); if (unicodeChar != null) { - addOperator(fOperatorMap, fOperatorTokenStartSet, unicodeChar, HEADER_STRINGS[i], - OPERATORS[i]); + addUnicodeOperator(fOperatorMap, fOperatorTokenStartSet, unicodeChar, OPERATORS[i]); buf.append(unicodeChar); } } @@ -442,16 +441,29 @@ public ASTNodeFactory(boolean ignoreCase) { } public static void addOperator(final Map operatorMap, - final Map> operatorTokenStartSet, final String operatorStr, - final String headStr, final Operator oper) { - operatorMap.put(headStr, oper); - ArrayList list = operatorTokenStartSet.get(operatorStr); + final Map> operatorTokenStartSet, final String operatorToken, + final String headStr, final Operator operator) { + operatorMap.put(headStr, operator); + ArrayList list = operatorTokenStartSet.get(operatorToken); if (list == null) { list = new ArrayList(2); - list.add(oper); - operatorTokenStartSet.put(operatorStr, list); + list.add(operator); + operatorTokenStartSet.put(operatorToken, list); } else { - list.add(oper); + list.add(operator); + } + } + + private static void addUnicodeOperator(final Map operatorMap, + final Map> operatorTokenStartSet, + final String unicodeOperatorToken, final Operator operator) { + ArrayList list = operatorTokenStartSet.get(unicodeOperatorToken); + if (list == null) { + list = new ArrayList(2); + list.add(operator); + operatorTokenStartSet.put(unicodeOperatorToken, list); + } else { + list.add(operator); } } diff --git a/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/ParserTestCase.java b/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/ParserTestCase.java index 72a7de1ff9..eec0d4bcce 100644 --- a/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/ParserTestCase.java +++ b/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/ParserTestCase.java @@ -13,32 +13,29 @@ /** Tests parser function for SimpleParserFactory */ class ParserTestCase { + static Parser PARSE_UNRELAXED = new Parser(); @Test void testParser() { - Parser p = new Parser(); - ASTNode obj = p.parse("-a-b*c!!+d"); + ASTNode obj = PARSE_UNRELAXED.parse("-a-b*c!!+d"); assertEquals("Plus(Plus(Times(-1, a), Times(-1, Times(b, Factorial2(c)))), d)", obj.toString()); } @Test void testParser0() { - Parser p = new Parser(); - ASTNode obj = p.parse("(#^3)&[x][y,z].{a,b,c}"); + ASTNode obj = PARSE_UNRELAXED.parse("(#^3)&[x][y,z].{a,b,c}"); assertEquals("Dot(Function(Power(Slot(1), 3))[x][y, z], List(a, b, c))", obj.toString()); } @Test void testParser1() { - Parser p = new Parser(); - ASTNode obj = p.parse("Integrate[Sin[x]^2+3*x^4, x]"); + ASTNode obj = PARSE_UNRELAXED.parse("Integrate[Sin[x]^2+3*x^4, x]"); assertEquals("Integrate(Plus(Power(Sin(x), 2), Times(3, Power(x, 4))), x)", obj.toString()); } @Test void testParser2() { - Parser p = new Parser(); - ASTNode obj = p.parse("a[][0][1]f[[x]]"); + ASTNode obj = PARSE_UNRELAXED.parse("a[][0][1]f[[x]]"); assertEquals("Times(a()[0][1], Part(f, x))", obj.toString()); } @@ -51,38 +48,34 @@ void testParser3() { @Test void testParser4() { - Parser p = new Parser(); - ASTNode obj = p.parse("$a=2"); + ASTNode obj = PARSE_UNRELAXED.parse("$a=2"); assertEquals("Set($a, 2)", obj.toString()); } @Test void testParser5() { - Parser p = new Parser(); if (!ParserConfig.EXPLICIT_TIMES_OPERATOR) { - ASTNode obj = p.parse("4.7942553860420304E-1"); + ASTNode obj = PARSE_UNRELAXED.parse("4.7942553860420304E-1"); assertEquals("Plus(Times(4.7942553860420304, E), Times(-1, 1))", obj.toString()); - obj = p.parse("4.7942553860420304 * E - 1"); + obj = PARSE_UNRELAXED.parse("4.7942553860420304 * E - 1"); assertEquals("Plus(Times(4.7942553860420304, E), Times(-1, 1))", obj.toString()); } else { - ASTNode obj = p.parse("4.7942553860420304E-1"); + ASTNode obj = PARSE_UNRELAXED.parse("4.7942553860420304E-1"); assertEquals("4.7942553860420304E-1", obj.toString()); } } @Test void testParser6() { - Parser p = new Parser(); - ASTNode obj = p.parse("a+%%%+%3*4!"); + ASTNode obj = PARSE_UNRELAXED.parse("a+%%%+%3*4!"); assertEquals("Plus(a, Out(-3), Times(Out(3), Factorial(4)))", obj.toString()); } @Test void testParser7() { Exception e = assertThrows(Exception.class, () -> { - Parser p = new Parser(); - p.parse("a+%%%+%3*:=4!"); + PARSE_UNRELAXED.parse("a+%%%+%3*:=4!"); }); assertEquals("Syntax error in line: 1 - Operator: := is no prefix operator.\n" + "a+%%%+%3*:=4!\n" + " ^", e.getMessage()); @@ -90,58 +83,50 @@ void testParser7() { @Test void testParser8() { - Parser p = new Parser(); - ASTNode obj = p.parse("-42424242424242424242"); + ASTNode obj = PARSE_UNRELAXED.parse("-42424242424242424242"); assertEquals("-42424242424242424242", obj.toString()); } @Test void testParser9() { - Parser p = new Parser(); - ASTNode obj = p.parse("-42424242424242424242.125"); + ASTNode obj = PARSE_UNRELAXED.parse("-42424242424242424242.125"); assertEquals("-42424242424242424242.125", obj.toString()); } @Test void testParser10() { - Parser p = new Parser(); - ASTNode obj = p.parse("-3/4"); + ASTNode obj = PARSE_UNRELAXED.parse("-3/4"); assertEquals("-3/4", obj.toString()); } @Test void testParser11() { - Parser p = new Parser(); - ASTNode obj = p.parse("-(3/4)"); + ASTNode obj = PARSE_UNRELAXED.parse("-(3/4)"); assertEquals("-3/4", obj.toString()); } @Test void testParser12() { - Parser p = new Parser(); - ASTNode obj = p.parse("-(Pi/4)"); + ASTNode obj = PARSE_UNRELAXED.parse("-(Pi/4)"); assertEquals("Times(-1, Times(1/4, Pi))", obj.toString()); } @Test void testParser13() { - Parser p = new Parser(); - ASTNode obj = p.parse("a*b*c*d"); + ASTNode obj = PARSE_UNRELAXED.parse("a*b*c*d"); assertEquals("Times(a, b, c, d)", obj.toString()); } @Test void testParser14() { - Parser p = new Parser(); - ASTNode obj = p.parse("-a-b*c!!+d"); + ASTNode obj = PARSE_UNRELAXED.parse("-a-b*c!!+d"); assertTrue(obj.dependsOn("d")); assertFalse(obj.dependsOn("x")); } @Test void testParser15() { - Parser p = new Parser(); - ASTNode obj = p.parse( + ASTNode obj = PARSE_UNRELAXED.parse( "Integrate[Sin[a_.*x_]^n_IntegerQ, x_Symbol]:= -Sin[a*x]^(n-1)*Cos[a*x]/(n*a)+(n-1)/n*Integrate[Sin[a*x]^(n-2),x]/;Positive[n]&&FreeQ[a,x]"); assertEquals( "SetDelayed(Integrate(Power(Sin(Times(a_., x_)), n_IntegerQ), x_Symbol), Condition(Plus(Times(Times(-1, Power(Sin(Times(a, x)), Plus(n, Times(-1, 1)))), Times(Cos(Times(a, x)), Power(Times(n, a), -1))), Times(Times(Plus(n, Times(-1, 1)), Power(n, -1)), Integrate(Power(Sin(Times(a, x)), Plus(n, Times(-1, 2))), x))), And(Positive(n), FreeQ(a, x))))", @@ -150,63 +135,55 @@ void testParser15() { @Test void testParser16() { - Parser p = new Parser(); - ASTNode obj = p.parse("f[[1,2]]"); + ASTNode obj = PARSE_UNRELAXED.parse("f[[1,2]]"); assertEquals("Part(f, 1, 2)", obj.toString()); - obj = p.parse("f[[1]][[2]]"); + obj = PARSE_UNRELAXED.parse("f[[1]][[2]]"); assertEquals("Part(Part(f, 1), 2)", obj.toString()); - obj = p.parse("f[[1,2,f[x]]]"); + obj = PARSE_UNRELAXED.parse("f[[1,2,f[x]]]"); assertEquals("Part(f, 1, 2, f(x))", obj.toString()); - obj = p.parse("f[[1]][[2]][[f[x]]]"); + obj = PARSE_UNRELAXED.parse("f[[1]][[2]][[f[x]]]"); assertEquals("Part(Part(Part(f, 1), 2), f(x))", obj.toString()); } @Test void testParser17() { - Parser p = new Parser(); - ASTNode obj = p.parse("\\[Alpha]+\\[Alpha]*f[\\[CapitalSHacek]]"); + ASTNode obj = PARSE_UNRELAXED.parse("\\[Alpha]+\\[Alpha]*f[\\[CapitalSHacek]]"); assertEquals("Plus(α, Times(α, f(Š)))", obj.toString()); } @Test void testParser18() { - Parser p = new Parser(); - ASTNode obj = p.parse("(a+b)[x]"); + ASTNode obj = PARSE_UNRELAXED.parse("(a+b)[x]"); assertEquals("Plus(a, b)[x]", obj.toString()); } @Test void testParser19() { - Parser p = new Parser(); - ASTNode obj = p.parse("B[[;; 2]]"); + ASTNode obj = PARSE_UNRELAXED.parse("B[[;; 2]]"); assertEquals("Part(B, Span(1, 2))", obj.toString()); } @Test void testParser20() { - Parser p = new Parser(); - ASTNode obj = p.parse("B[[;;, 2]]"); + ASTNode obj = PARSE_UNRELAXED.parse("B[[;;, 2]]"); assertEquals("Part(B, Span(1, All), 2)", obj.toString()); } @Test void testParser21a() { - Parser p = new Parser(); - ASTNode obj = p.parse("B[[ ;;1;;-1]]"); + ASTNode obj = PARSE_UNRELAXED.parse("B[[ ;;1;;-1]]"); assertEquals("Part(B, Span(1, 1, -1))", obj.toString()); } @Test void testParser21b() { - Parser p = new Parser(); - ASTNode obj = p.parse("B[[3;;1;;-1]]"); + ASTNode obj = PARSE_UNRELAXED.parse("B[[3;;1;;-1]]"); assertEquals("Part(B, Span(3, 1, -1))", obj.toString()); } @Test void testParser21c() { - Parser p = new Parser(); - ASTNode obj = p.parse("B[[;;;;-1]]"); + ASTNode obj = PARSE_UNRELAXED.parse("B[[;;;;-1]]"); assertEquals("Part(B, Span(1, All, -1))", obj.toString()); } @@ -221,30 +198,26 @@ void testParserFunction() { @Test void testParser22() { - Parser p = new Parser(); - ASTNode obj = p.parse("a+b;"); + ASTNode obj = PARSE_UNRELAXED.parse("a+b;"); assertEquals("CompoundExpression(Plus(a, b), Null)", obj.toString()); } @Test void testParser23() { - Parser p = new Parser(); - ASTNode obj = p.parse("f[x]*f''[x]"); + ASTNode obj = PARSE_UNRELAXED.parse("f[x]*f''[x]"); assertEquals("Times(f(x), Derivative(2)[f][x])", obj.toString()); } @Test void testParser24() { - Parser p = new Parser(); - ASTNode obj = p.parse("x'[t] == 10*(y[t] - x[t])"); + ASTNode obj = PARSE_UNRELAXED.parse("x'[t] == 10*(y[t] - x[t])"); assertEquals("Equal(Derivative(1)[x][t], Times(10, Plus(y(t), Times(-1, x(t)))))", obj.toString()); } @Test void testParser25() { - Parser p = new Parser(); - ASTNode obj = p.parse("Int[f_'[x_]*g_[x_] + f_[x_]*g_'[x_],x_Symbol]"); + ASTNode obj = PARSE_UNRELAXED.parse("Int[f_'[x_]*g_[x_] + f_[x_]*g_'[x_],x_Symbol]"); assertEquals( "Int(Plus(Times(Derivative(1)[f_][x_], g_(x_)), Times(f_(x_), Derivative(1)[g_][x_])), x_Symbol)", obj.toString()); @@ -313,36 +286,31 @@ void testParser28() { @Test void testParser29() { - Parser p = new Parser(); - ASTNode obj = p.parse("FracPart[m_*u_,n_:1]"); + ASTNode obj = PARSE_UNRELAXED.parse("FracPart[m_*u_,n_:1]"); assertEquals("FracPart(Times(m_, u_), Optional(n_, 1))", obj.toString()); } @Test void testParse30() { - Parser p = new Parser(); - ASTNode obj = p.parse("f[x,y,]"); + ASTNode obj = PARSE_UNRELAXED.parse("f[x,y,]"); assertEquals("f(x, y, Null)", obj.toString()); } @Test void testParser31() { - Parser p = new Parser(); - ASTNode obj = p.parse("#1.#123"); + ASTNode obj = PARSE_UNRELAXED.parse("#1.#123"); assertEquals("Dot(Slot(1), Slot(123))", obj.toString()); } @Test void testParser32() { - Parser p = new Parser(); - ASTNode obj = p.parse("(-1)^(a) (b)"); + ASTNode obj = PARSE_UNRELAXED.parse("(-1)^(a) (b)"); assertEquals("Times(Power(-1, a), b)", obj.toString()); } @Test void testParser33() { - Parser p = new Parser(); - ASTNode obj = p.parse( + ASTNode obj = PARSE_UNRELAXED.parse( "Int[(u_)*(y_)^(m_.), x_Symbol] := With[{q = DerivativeDivides[y, u, x]}, Simp[(q*y^(m + 1))/(m + 1), x] /; !FalseQ[q]] /; FreeQ[m, x] && NeQ[m, -1]"); assertEquals( "SetDelayed(Int(Times(u_, Power(y_, m_.)), x_Symbol), Condition(With(List(Set(q, DerivativeDivides(y, u, x))), Condition(Simp(Times(Times(q, Power(y, Plus(m, 1))), Power(Plus(m, 1), -1)), x), Not(FalseQ(q)))), And(FreeQ(m, x), NeQ(m, -1))))", @@ -351,16 +319,14 @@ void testParser33() { @Test void testParse33() { - Parser p = new Parser(); - ASTNode obj = p.parse("f@ g@ h"); + ASTNode obj = PARSE_UNRELAXED.parse("f@ g@ h"); assertEquals("f(g(h))", obj.toString()); } @Test void testParse34() { - Parser p = new Parser(); // http://oeis.org/A005132 - ASTNode obj = p.parse( + ASTNode obj = PARSE_UNRELAXED.parse( "a = {1}; Do[ If[ a[ [ -1 ] ] - n > 0 && Position[ a, a[ [ -1 ] ] - n ] == {}, a = Append[ a, a[ [ -1 ] ] - n ], a = Append[ a, a[ [ -1 ] ] + n ] ], {n, 2, 70} ]; a"); assertEquals( "CompoundExpression(Set(a, List(1)), Do(If(And(Greater(Plus(Part(a, -1), Times(-1, n)), 0), Equal(Position(a, Plus(Part(a, -1), Times(-1, n))), List())), Set(a, Append(a, Plus(Part(a, -1), Times(-1, n)))), Set(a, Append(a, Plus(Part(a, -1), n)))), List(n, 2, 70)), a)", @@ -369,9 +335,8 @@ void testParse34() { @Test void testParse35() { - Parser p = new Parser(); // http://oeis.org/A005132 - ASTNode obj = p.parse(" If[!MatchQ[#,_\\[Rule]_],\n" + ASTNode obj = PARSE_UNRELAXED.parse(" If[!MatchQ[#,_\\[Rule]_],\n" + " Message[Caller::\"UnknownOption\",#];\n" + " (*else*),\n" + " pos=Position[FullOptions,{#[[1]],_,_}];\n" + " If[Length[pos]\\[Equal]0,\n" + " Message[Caller::\"UnknownOption\",#]\n" + " (*else*),\n" @@ -384,16 +349,14 @@ void testParse35() { @Test void testParse36() { // System.out.println(Character.isUnicodeIdentifierPart('\u221E')); - Parser p = new Parser(); - ASTNode obj = p.parse("\u221E"); + ASTNode obj = PARSE_UNRELAXED.parse("\u221E"); assertEquals("Infinity", obj.toString()); } @Test void testParse37() { - Parser p = new Parser(); - ASTNode obj = p.parse(" Do[\n" + " serh=SeriesHead[ser,\\[Omega]];\n" + " \n" - + " (* check for series term that run out of precision *)\n" + ASTNode obj = PARSE_UNRELAXED.parse(" Do[\n" + " serh=SeriesHead[ser,\\[Omega]];\n" + + " \n" + " (* check for series term that run out of precision *)\n" + " If[FreeQ[serh,HoldPattern[SeriesData[_,_,{},_,_,_]]],\n" + " (* No: done. *)\n" + " Break[];\n" + " ];\n" + " ,{i,1,30}\n" + " ]"); @@ -404,9 +367,8 @@ void testParse37() { @Test void testParse38() { - Parser p = new Parser(); - ASTNode obj = - p.parse("MakeGraph[Range[26],\n" + " Mod[#1-#2, 26] == 1 ||\n" + ASTNode obj = PARSE_UNRELAXED + .parse("MakeGraph[Range[26],\n" + " Mod[#1-#2, 26] == 1 ||\n" + " (-1)^#1Mod[#1-#2, 26] == 11 ||\n" + " (-1)^#1Mod[#1-#2, 26] == 7&,\n" + " Type -> Directed]"); @@ -417,8 +379,8 @@ void testParse38() { @Test void testParse39() { - Parser p = new Parser(); - ASTNode obj = p.parse("While[i\\[LessEqual]Length[asympt] && asympt[[i,2]]\\[Equal]0,i++]"); + ASTNode obj = + PARSE_UNRELAXED.parse("While[i\\[LessEqual]Length[asympt] && asympt[[i,2]]\\[Equal]0,i++]"); assertEquals( "While(And(LessEqual(i, Length(asympt)), Equal(Part(asympt, i, 2), 0)), Increment(i))", obj.toString()); @@ -426,36 +388,31 @@ void testParse39() { @Test void testParse40() { - Parser p = new Parser(); - ASTNode obj = p.parse("ff/: Power[ff, i_Integer] = {i}"); + ASTNode obj = PARSE_UNRELAXED.parse("ff/: Power[ff, i_Integer] = {i}"); assertEquals("TagSet(ff, Power(ff, i_Integer), List(i))", obj.toString()); } @Test void testParse41() { - Parser p = new Parser(); - ASTNode obj = p.parse("ff/: Power[ff, i_Integer] := {i}"); + ASTNode obj = PARSE_UNRELAXED.parse("ff/: Power[ff, i_Integer] := {i}"); assertEquals("TagSetDelayed(ff, Power(ff, i_Integer), List(i))", obj.toString()); } @Test void testParse42() { - Parser p = new Parser(); - ASTNode obj = p.parse("m_.k_+b_."); + ASTNode obj = PARSE_UNRELAXED.parse("m_.k_+b_."); assertEquals("Plus(Times(m_., k_), b_.)", obj.toString()); } @Test void testParse43() { - Parser p = new Parser(); - ASTNode obj = p.parse("MakeAssocList[u_,x_Symbol,alst_List:{}] "); + ASTNode obj = PARSE_UNRELAXED.parse("MakeAssocList[u_,x_Symbol,alst_List:{}] "); assertEquals("MakeAssocList(u_, x_Symbol, Optional(alst_List, List()))", obj.toString()); } @Test void testParser44() { - Parser p = new Parser(); - ASTNode obj = p.parse("-1<0<=a<4<5 { - Parser p = new Parser(); - p.parse("\\[DifferentialD] = 4"); + PARSE_UNRELAXED.parse("\\[DifferentialD] = 4"); }); assertEquals(e.getMessage(), "Syntax error in line: 1 - unexpected (named unicode) character: '\\[DifferentialD]'\n" @@ -621,70 +558,63 @@ void testParser63() { @Test void testParser64() { - Parser p = new Parser(); - ASTNode obj = p.parse("test = \" \\[Theta] is using {\\\"1\\\", \\\"2\\\"} instead.\""); + ASTNode obj = + PARSE_UNRELAXED.parse("test = \" \\[Theta] is using {\\\"1\\\", \\\"2\\\"} instead.\""); assertEquals("Set(test, θ is using {\"1\", \"2\"} instead.)", obj.toString()); } @Test void testParser65() { - Parser p = new Parser(); - ASTNode obj = p.parse("2.2250738585072014`*^-308"); + ASTNode obj = PARSE_UNRELAXED.parse("2.2250738585072014`*^-308"); assertEquals("2.2250738585072014E-308", obj.toString()); } @Test void testParser66() { - Parser p = new Parser(); - ASTNode obj = p.parse("a~b~c"); + ASTNode obj = PARSE_UNRELAXED.parse("a~b~c"); assertEquals("b(a, c)", obj.toString()); } @Test void testParser67() { - Parser p = new Parser(); - ASTNode obj = p.parse("a~b~c~d~e"); + ASTNode obj = PARSE_UNRELAXED.parse("a~b~c~d~e"); assertEquals("d(b(a, c), e)", obj.toString()); } @Test void testParser68() { - Parser p = new Parser(); - ASTNode obj = p.parse("8 + 1*^-28"); + ASTNode obj = PARSE_UNRELAXED.parse("8 + 1*^-28"); assertEquals("Plus(8, Times(1, Power(10000000000000000000000000000, -1)))", obj.toString()); // 6 - obj = p.parse("6*^0"); + obj = PARSE_UNRELAXED.parse("6*^0"); assertEquals("Times(6, 1)", obj.toString()); // 3/5 - obj = p.parse("6*^-1"); + obj = PARSE_UNRELAXED.parse("6*^-1"); assertEquals("Times(6, Power(10, -1))", obj.toString()); // 6000 - obj = p.parse("6*^3"); + obj = PARSE_UNRELAXED.parse("6*^3"); assertEquals("Times(6, 1000)", obj.toString()); } @Test void testParser69() { - Parser p = new Parser(); - ASTNode obj = p.parse("2/3 + Pi + 5.5`30"); + ASTNode obj = PARSE_UNRELAXED.parse("2/3 + Pi + 5.5`30"); assertEquals("Plus(2/3, Pi, 5.5)", obj.toString()); } @Test void testParser70() { - Parser p = new Parser(); - ASTNode obj = p.parse("Plot3D[Sin[x*y],{x,-1.5`,1.5`},{y,-1.5`,1.5`}]"); + ASTNode obj = PARSE_UNRELAXED.parse("Plot3D[Sin[x*y],{x,-1.5`,1.5`},{y,-1.5`,1.5`}]"); assertEquals("Plot3D(Sin(Times(x, y)), List(x, -1.5, 1.5), List(y, -1.5, 1.5))", obj.toString()); } @Test void testParser71() { - Parser p = new Parser(); - ASTNode obj = p.parse(" \n" + "a = f[Rule, {#, RandomReal[1, Length[#]]}]&[\n" + ASTNode obj = PARSE_UNRELAXED.parse(" \n" + "a = f[Rule, {#, RandomReal[1, Length[#]]}]&[\n" + "Flatten[constants[[All,2]]]" + "\n]"); assertEquals( "Set(a, Function(f(Rule, List(Slot(1), RandomReal(1, Length(Slot(1))))))[Flatten(Part(constants, All, 2))])", @@ -693,9 +623,9 @@ void testParser71() { @Test void testParser72() { - Parser p = new Parser(); - ASTNode obj = p.parse(" \n" + " constantRules = f[Rule, {#, RandomReal[1, Length[#]]}]&[\n" - + " Flatten[constants[[All,2]]]\n" + " ]"); + ASTNode obj = PARSE_UNRELAXED + .parse(" \n" + " constantRules = f[Rule, {#, RandomReal[1, Length[#]]}]&[\n" + + " Flatten[constants[[All,2]]]\n" + " ]"); assertEquals( "Set(constantRules, Function(f(Rule, List(Slot(1), RandomReal(1, Length(Slot(1))))))[Flatten(Part(constants, All, 2))])", obj.toString()); @@ -703,16 +633,14 @@ void testParser72() { @Test void testParser73() { - Parser p = new Parser(); - ASTNode obj = p.parse("y:r_^n_"); + ASTNode obj = PARSE_UNRELAXED.parse("y:r_^n_"); // Pattern[y,Power[Pattern[r,Blank[]],Pattern[n,Blank[]]]] assertEquals("Pattern(y, Power(r_, n_))", obj.toString()); } @Test void testParser74() { - Parser p = new Parser(); - ASTNode obj = p.parse("Cases[e, y:(a_. x + b_.)^n_ /; FreeQ[{a,b}, x] ]"); + ASTNode obj = PARSE_UNRELAXED.parse("Cases[e, y:(a_. x + b_.)^n_ /; FreeQ[{a,b}, x] ]"); // Cases[e,Condition[Pattern[y,Power[Plus[Times[Optional[Pattern[a,Blank[]]],x],Optional[Pattern[b,Blank[]]]],Pattern[n,Blank[]]]],FreeQ[List[a,b],x]]] assertEquals( "Cases(e, Condition(Pattern(y, Power(Plus(Times(a_., x), b_.), n_)), FreeQ(List(a, b), x)))", @@ -721,8 +649,7 @@ void testParser74() { @Test void testParser75() { - Parser p = new Parser(); - ASTNode obj = p.parse("Cases[e, t:{__Integer} :> t^2]"); + ASTNode obj = PARSE_UNRELAXED.parse("Cases[e, t:{__Integer} :> t^2]"); assertEquals("Cases(e, RuleDelayed(Pattern(t, List(__Integer)), Power(t, 2)))", obj.toString()); } @@ -730,8 +657,7 @@ void testParser75() { // Numerator[n], Denominator[n], a, b}, {0, Infinity}]]//FullForm @Test void testParser76() { - Parser p = new Parser(); - ASTNode obj = p.parse( + ASTNode obj = PARSE_UNRELAXED.parse( "Cases[e, y:(a_. x + b_.)^n_ /; FreeQ[{a,b}, x] && Head[n] == Rational :> {y, a x + b, Numerator[n], Denominator[n], a, b}, {0, Infinity}]"); assertEquals( "Cases(e, RuleDelayed(Condition(Pattern(y, Power(Plus(Times(a_., x), b_.), n_)), And(FreeQ(List(a, b), x), Equal(Head(n), Rational))), List(y, Plus(Times(a, x), b), Numerator(n), Denominator(n), a, b)), List(0, Infinity))", @@ -740,15 +666,13 @@ void testParser76() { @Test void testParser77() { - Parser p = new Parser(); - ASTNode obj = p.parse("a_+b:Power[c_,_Rational]"); + ASTNode obj = PARSE_UNRELAXED.parse("a_+b:Power[c_,_Rational]"); assertEquals("Plus(a_, Pattern(b, Power(c_, _Rational)))", obj.toString()); } @Test void testParser78() { - Parser p = new Parser(); - ASTNode obj = p.parse("WordBoundary ~~ x:DigitCharacter.. ~~ WordBoundary"); + ASTNode obj = PARSE_UNRELAXED.parse("WordBoundary ~~ x:DigitCharacter.. ~~ WordBoundary"); assertEquals( "StringExpression(WordBoundary, Pattern(x, Repeated(DigitCharacter)), WordBoundary)", obj.toString()); @@ -756,39 +680,40 @@ void testParser78() { @Test void testParser79() { - Parser p = new Parser(); - ASTNode obj = p.parse(" (* a test *) "); + ASTNode obj = PARSE_UNRELAXED.parse(" (* a test *) "); assertEquals("Null", obj.toString()); } @Test void testParser80() { - Parser p = new Parser(); - ASTNode obj = p.parse(" 2 *(* a test *) 4"); + ASTNode obj = PARSE_UNRELAXED.parse(" 2 *(* a test *) 4"); assertEquals("Times(2, 4)", obj.toString()); } @Test void testParser81() { - Parser p = new Parser(); - ASTNode obj = p.parse("x|->(x^3)"); + ASTNode obj = PARSE_UNRELAXED.parse("x|->(x^3)"); assertEquals("Function(x, Power(x, 3))", obj.toString()); } @Test void testParser82() { - Parser p = new Parser(); - ASTNode obj = p.parse("x \\[Function] (x^3)"); + ASTNode obj = PARSE_UNRELAXED.parse("x \\[Function] (x^3)"); assertEquals("Function(x, Power(x, 3))", obj.toString()); } @Test void testParser83() { - Parser p = new Parser(); - ASTNode obj = p.parse( + ASTNode obj = PARSE_UNRELAXED.parse( "ProductLog/:ProductLog[k_,z_]/;NumericQ[k]&&!IntegerQ[k]:=WrightOmega[Log[z]+2 Pi I k]"); assertEquals( "TagSetDelayed(ProductLog, Condition(ProductLog(k_, z_), And(NumericQ(k), Not(IntegerQ(k)))), WrightOmega(Plus(Log(z), Times(Times(Times(2, Pi), I), k))))", obj.toString()); } + + @Test + void testParser84() { + ASTNode obj = PARSE_UNRELAXED.parse("x \\[LessEqual] (x^3)"); + assertEquals("LessEqual(x, Power(x, 3))", obj.toString()); + } } diff --git a/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/RelaxedParserTestCase.java b/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/RelaxedParserTestCase.java index a7064063bb..07bad6a3e6 100644 --- a/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/RelaxedParserTestCase.java +++ b/symja_android_library/matheclipse-parser/src/test/java/org/matheclipse/parser/test/RelaxedParserTestCase.java @@ -7,84 +7,74 @@ /** Tests parser functions for the simple parser style */ class RelaxedParserTestCase { + static Parser PARSE_RELAXED = new Parser(true); @Test void testParser0() { - Parser p = new Parser(true); - ASTNode obj = p.parse("Integrate(Sin(x)^2+3*x^4, x)"); + ASTNode obj = PARSE_RELAXED.parse("Integrate(Sin(x)^2+3*x^4, x)"); assertEquals("Integrate(Plus(Power(Sin(x), 2), Times(3, Power(x, 4))), x)", obj.toString()); } @Test void testParser1() { - Parser p = new Parser(true); - ASTNode obj = p.parse("a()(0)(1)f[[x]]"); + ASTNode obj = PARSE_RELAXED.parse("a()(0)(1)f[[x]]"); assertEquals("Times(Times(a(), Times(0, 1)), Part(f, x))", obj.toString()); } @Test void testParser2() { - Parser p = new Parser(true); - ASTNode obj = p.parse("1; 2; 3;"); + ASTNode obj = PARSE_RELAXED.parse("1; 2; 3;"); assertEquals("CompoundExpression(1, 2, 3, Null)", obj.toString()); - obj = p.parse("1; 2; 3"); + obj = PARSE_RELAXED.parse("1; 2; 3"); assertEquals("CompoundExpression(1, 2, 3)", obj.toString()); } @Test void testParser3() { - Parser p = new Parser(true); - ASTNode obj = p.parse("a sin()cos()x()y z"); + ASTNode obj = PARSE_RELAXED.parse("a sin()cos()x()y z"); assertEquals("Times(Times(Times(Times(Times(a, sin()), cos()), x()), y), z)", obj.toString()); } @Test void testParser4() { - Parser p = new Parser(true); - ASTNode obj = p.parse("#1.#123"); + ASTNode obj = PARSE_RELAXED.parse("#1.#123"); assertEquals("Dot(Slot(1), Slot(123))", obj.toString()); } @Test void testParse5() { - Parser p = new Parser(true); - ASTNode obj = p.parse("f@ g@ h"); + ASTNode obj = PARSE_RELAXED.parse("f@ g@ h"); assertEquals("f(g(h))", obj.toString()); } @Test void testParser6() { - Parser p = new Parser(true); - ASTNode obj = p.parse("#\"Column Type\""); + ASTNode obj = PARSE_RELAXED.parse("#\"Column Type\""); assertEquals("Slot(Column Type)", obj.toString()); } @Test void testParser7() { - Parser p = new Parser(true); - ASTNode obj = p.parse("#identifier"); + ASTNode obj = PARSE_RELAXED.parse("#identifier"); assertEquals("Slot(identifier)", obj.toString()); } @Test void testParser8() { - Parser p = new Parser(true); - ASTNode obj = p.parse("Fibonacci(1007,Null)"); + ASTNode obj = PARSE_RELAXED.parse("Fibonacci(1007,Null)"); assertEquals("Fibonacci(1007, Null)", obj.toString()); } @Test void testParser9() { - Parser p = new Parser(true); - ASTNode obj = p.parse("2.33`"); + ASTNode obj = PARSE_RELAXED.parse("2.33`"); assertEquals("2.33", obj.toString()); } @Test void testParser10() { - Parser p = new Parser(true); - ASTNode obj = p.parse("f(t_) = Simplify( r'(t) / Norm( r'(t)), t ∈ Reals);"); + ASTNode obj = PARSE_RELAXED.parse("f(t_) = Simplify( r'(t) / Norm( r'(t)), t ∈ Reals);"); assertEquals( "CompoundExpression(Set(f(t_), Simplify(Times(Derivative(1)[r][t], Power(Norm(Derivative(1)[r][t]), -1)), Element(t, Reals))), Null)", obj.toString()); @@ -92,17 +82,13 @@ void testParser10() { @Test void testParser11() { - Parser p = new Parser(true); - ASTNode obj = p.parse("x[ [ ] ]"); + ASTNode obj = PARSE_RELAXED.parse("x[ [ ] ]"); assertEquals("Part(x)", obj.toString()); } @Test void testParser12() { - Parser p = new Parser(true); - ASTNode obj = p.parse("I_m==a*c"); - assertEquals( - "Equal(I_m, Times(a, c))", - obj.toString()); + ASTNode obj = PARSE_RELAXED.parse("I_m==a*c"); + assertEquals("Equal(I_m, Times(a, c))", obj.toString()); } }