private ExpressionType getExpressionType() { skipWhiteSpace(); for (Pattern p : patternMap.keySet()) { Matcher expMatch = p.matcher(myInput.substring(myCurrentPosition)); if (expMatch.lookingAt()) return patternMap.get(p); } throw new ParserException("Unexpected Character " + currentCharacter()); }
/** * Converts given string into expression tree. * * @param input expression given in the language to be parsed * @return expression tree representing the given formula */ public Expression makeExpression(String input) { myInput = input; myCurrentPosition = 0; Expression result = parseExpression(); skipWhiteSpace(); if (notAtEndOfString()) { throw new ParserException( "Unexpected characters at end of the string: " + myInput.substring(myCurrentPosition), ParserException.Type.EXTRA_CHARACTERS); } return result; }
private Expression parseParenExpression() { ArrayList<Expression> elements = new ArrayList<Expression>(); Matcher expMatcher = EXPRESSION_BEGIN_REGEX.matcher(myInput); expMatcher.find(myCurrentPosition); String commandName = expMatcher.group(1); myCurrentPosition = expMatcher.end(); while (notAtEndOfString()) { skipWhiteSpace(); if (currentCharacter() == ')') { myCurrentPosition++; return expressionCreator(commandName, elements); } elements.add(parseExpression()); } throw new ParserException( "Expected close paren, instead found " + myInput.substring(myCurrentPosition)); }