/** * When the name of this ParserExtension is encountered by a parser with which the extension is * registered, the parser calls this routine to parse the summation subexpression. The * subexpression has the form (<variable>,<lower-limit>,<upper-limit>,<expression>). This method * is not meant to be called directly */ public void doParse(Parser parser, ParserContext context) { int tok = context.next(); String open = context.tokenString; if (tok == ParserContext.OPCHARS && (open.equals("(") || (open.equals("[") && (context.options & Parser.BRACKETS) != 0) || (open.equals("{") && (context.options & Parser.BRACES) != 0))) { String close = open.equals("(") ? ")" : (open.equals("[") ? "]" : "}"); tok = context.next(); // Must be an identifier. if (tok != ParserContext.IDENTIFIER) throw new ParseError( "Expected the summation variable as the first argument of " + name + ".", context); String varName = context.tokenString; tok = context.next(); if (tok != ParserContext.OPCHARS || !context.tokenString.equals(",")) throw new ParseError( "Exprected a comma after the index variable, " + varName + ".", context); parser.parseExpression(context); tok = context.next(); if (tok != ParserContext.OPCHARS || !context.tokenString.equals(",")) throw new ParseError( "Exprected a comma after the lower limit expression for " + name + ".", context); parser.parseExpression(context); tok = context.next(); if (tok != ParserContext.OPCHARS || !context.tokenString.equals(",")) throw new ParseError( "Exprected a comma after the upper limit expression for " + name + ".", context); Variable v = new Variable(varName); context.mark(); // Temporoarily add the summation variable to the symbol table. context.add(v); ExpressionProgram saveProg = context.prog; context.prog = new ExpressionProgram(); // Compile the expression into a new program. parser.parseExpression(context); tok = context.next(); if (tok != ParserContext.OPCHARS || !context.tokenString.equals(close)) throw new ParseError( "Expected a \"" + close + "\" at the end of the paramter list for " + name + ".", context); context.revert(); // Restore the state of the ParserContext. saveProg.addCommandObject(new Cmd(v, context.prog)); context.prog = saveProg; } else throw new ParseError("Parentheses required around parameters of summation.", context); } // end doParse()