Skip to content

Commit

Permalink
Clean up parser handling of (additional) Unicode operator tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Oct 9, 2023
1 parent 387a802 commit 18144cb
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -92,8 +89,6 @@ public static boolean test(final String str, EvalEngine engine) {
*/
private final boolean fRelaxedSyntax;

// private List<IExpr> fNodeList = null;

private final EvalEngine fEngine;

protected IParserFactory fFactory;
Expand All @@ -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
Expand All @@ -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<IExpr>(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()) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -493,17 +474,30 @@ public static void initialize() {
}

public static void addOperator(final Map<String, Operator> operatorMap,
final Map<String, ArrayList<Operator>> operatorTokenStartSet, final String operatorStr,
final String headStr, final Operator oper) {
final Map<String, ArrayList<Operator>> operatorTokenStartSet, final String operatorToken,
final String headStr, final Operator operator) {
ArrayList<Operator> list;
operatorMap.put(headStr, oper);
list = operatorTokenStartSet.get(operatorStr);
operatorMap.put(headStr, operator);
list = operatorTokenStartSet.get(operatorToken);
if (list == null) {
list = new ArrayList<Operator>(2);
list.add(operator);
operatorTokenStartSet.put(operatorToken, list);
} else {
list.add(operator);
}
}

private static void addUnicodeOperator(final Map<String, Operator> operatorMap,
final Map<String, ArrayList<Operator>> operatorTokenStartSet,
final String operatorUnicodeToken, final Operator operator) {
ArrayList<Operator> list = operatorTokenStartSet.get(operatorUnicodeToken);
if (list == null) {
list = new ArrayList<Operator>(2);
list.add(oper);
operatorTokenStartSet.put(operatorStr, list);
list.add(operator);
operatorTokenStartSet.put(operatorUnicodeToken, list);
} else {
list.add(oper);
list.add(operator);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -442,16 +441,29 @@ public ASTNodeFactory(boolean ignoreCase) {
}

public static void addOperator(final Map<String, Operator> operatorMap,
final Map<String, ArrayList<Operator>> operatorTokenStartSet, final String operatorStr,
final String headStr, final Operator oper) {
operatorMap.put(headStr, oper);
ArrayList<Operator> list = operatorTokenStartSet.get(operatorStr);
final Map<String, ArrayList<Operator>> operatorTokenStartSet, final String operatorToken,
final String headStr, final Operator operator) {
operatorMap.put(headStr, operator);
ArrayList<Operator> list = operatorTokenStartSet.get(operatorToken);
if (list == null) {
list = new ArrayList<Operator>(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<String, Operator> operatorMap,
final Map<String, ArrayList<Operator>> operatorTokenStartSet,
final String unicodeOperatorToken, final Operator operator) {
ArrayList<Operator> list = operatorTokenStartSet.get(unicodeOperatorToken);
if (list == null) {
list = new ArrayList<Operator>(2);
list.add(operator);
operatorTokenStartSet.put(unicodeOperatorToken, list);
} else {
list.add(operator);
}
}

Expand Down
Loading

0 comments on commit 18144cb

Please sign in to comment.