/** * params := <LPAREN> (expr (<COMMA> expr)*)? <RPAREN> */ protected AstParameters params() throws ScanException, ParseException { consumeToken(LPAREN); List<AstNode> l = Collections.emptyList(); AstNode v = expr(false); if (v != null) { l = new ArrayList<AstNode>(); l.add(v); while (token.getSymbol() == COMMA) { consumeToken(); l.add(expr(true)); } } consumeToken(RPAREN); return new AstParameters(l); }
/** * params := <LPAREN> (expr (<COMMA> expr)*)? <RPAREN> */ protected AstParameters params() throws ScanException, ParseException { consumeToken(LPAREN); List<AstNode> l = Collections.emptyList(); AstNode v = expr(false); if (v != null) { l = new ArrayList<AstNode>(); l.add(v); while (token.getSymbol() == COMMA) { consumeToken(); l.add(expr(true)); } } consumeToken(RPAREN); return new AstParameters(l); }
/** * consume current token (get next token); throw exception if the current token doesn't * match the expected symbol. */ protected final Token consumeToken(Symbol expected) throws ScanException, ParseException { if (token.getSymbol() != expected) { fail(expected); } return consumeToken(); }
/** * consume current token (get next token); throw exception if the current token doesn't * match the expected symbol. */ protected final Token consumeToken(Symbol expected) throws ScanException, ParseException { if (token.getSymbol() != expected) { fail(expected); } return consumeToken(); }
/** * dynmamic := <START_EVAL_DYNAMIC> expr <END_EVAL> * deferred := <START_EVAL_DEFERRED> expr <END_EVAL> */ protected AstEval eval(boolean required, boolean deferred) throws ScanException, ParseException { AstEval v = null; Symbol start_eval = deferred ? START_EVAL_DEFERRED : START_EVAL_DYNAMIC; if (token.getSymbol() == start_eval) { consumeToken(); v = new AstEval(expr(true), deferred); consumeToken(END_EVAL); } else if (required) { fail(start_eval); } return v; }
/** * dynmamic := <START_EVAL_DYNAMIC> expr <END_EVAL> * deferred := <START_EVAL_DEFERRED> expr <END_EVAL> */ protected AstEval eval(boolean required, boolean deferred) throws ScanException, ParseException { AstEval v = null; Symbol start_eval = deferred ? START_EVAL_DEFERRED : START_EVAL_DYNAMIC; if (token.getSymbol() == start_eval) { consumeToken(); v = new AstEval(expr(true), deferred); consumeToken(END_EVAL); } else if (required) { fail(start_eval); } return v; }
/** * text := <TEXT> */ protected AstNode text() throws ScanException, ParseException { AstNode v = null; if (token.getSymbol() == TEXT) { v = new AstText(token.getImage()); consumeToken(); } return v; }
/** * text := <TEXT> */ protected AstNode text() throws ScanException, ParseException { AstNode v = null; if (token.getSymbol() == TEXT) { v = new AstText(token.getImage()); consumeToken(); } return v; }
/** * expr := or (<QUESTION> expr <COLON> expr)? */ protected AstNode expr(boolean required) throws ScanException, ParseException { AstNode v = or(required); if (v == null) { return null; } if (token.getSymbol() == QUESTION) { consumeToken(); AstNode a = expr(true); consumeToken(COLON); AstNode b = expr(true); v = createAstChoice(v, a, b); } return v; }
/** * expr := or (<QUESTION> expr <COLON> expr)? */ protected AstNode expr(boolean required) throws ScanException, ParseException { AstNode v = or(required); if (v == null) { return null; } if (token.getSymbol() == QUESTION) { consumeToken(); AstNode a = expr(true); consumeToken(COLON); AstNode b = expr(true); v = createAstChoice(v, a, b); } return v; }
/** * nonliteral := <IDENTIFIER> | function | <LPAREN> expr <RPAREN> * function := (<IDENTIFIER> <COLON>)? <IDENTIFIER> <LPAREN> list? <RPAREN> */ protected AstNode nonliteral() throws ScanException, ParseException { AstNode v = null; switch (token.getSymbol()) { case IDENTIFIER: String name = consumeToken().getImage(); if (token.getSymbol() == COLON && lookahead(0).getSymbol() == IDENTIFIER && lookahead(1).getSymbol() == LPAREN) { // ns:f(...) consumeToken(); name += ":" + token.getImage(); consumeToken(); } if (token.getSymbol() == LPAREN) { // function v = function(name, params()); } else { // identifier v = identifier(name); } break; case LPAREN: consumeToken(); v = expr(true); consumeToken(RPAREN); v = new AstNested(v); break; } return v; }
/** * nonliteral := <IDENTIFIER> | function | <LPAREN> expr <RPAREN> * function := (<IDENTIFIER> <COLON>)? <IDENTIFIER> <LPAREN> list? <RPAREN> */ protected AstNode nonliteral() throws ScanException, ParseException { AstNode v = null; switch (token.getSymbol()) { case IDENTIFIER: String name = consumeToken().getImage(); if (token.getSymbol() == COLON && lookahead(0).getSymbol() == IDENTIFIER && lookahead(1).getSymbol() == LPAREN) { // ns:f(...) consumeToken(); name += ":" + token.getImage(); consumeToken(); } if (token.getSymbol() == LPAREN) { // function v = function(name, params()); } else { // identifier v = identifier(name); } break; case LPAREN: consumeToken(); v = expr(true); consumeToken(RPAREN); v = new AstNested(v); break; } return v; }
/** * add := add (<PLUS> mul | <MINUS> mul)* */ protected AstNode add(boolean required) throws ScanException, ParseException { AstNode v = mul(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case PLUS: consumeToken(); v = createAstBinary(v, mul(true), AstBinary.ADD); break; case MINUS: consumeToken(); v = createAstBinary(v, mul(true), AstBinary.SUB); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.ADD) { v = getExtensionHandler(consumeToken()).createAstNode(v, mul(true)); break; } default: return v; } } }
/** * eq := cmp (<EQ> cmp | <NE> cmp)* */ protected AstNode eq(boolean required) throws ScanException, ParseException { AstNode v = cmp(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case EQ: consumeToken(); v = createAstBinary(v, cmp(true), AstBinary.EQ); break; case NE: consumeToken(); v = createAstBinary(v, cmp(true), AstBinary.NE); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.EQ) { v = getExtensionHandler(consumeToken()).createAstNode(v, cmp(true)); break; } default: return v; } } }
/** * eq := cmp (<EQ> cmp | <NE> cmp)* */ protected AstNode eq(boolean required) throws ScanException, ParseException { AstNode v = cmp(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case EQ: consumeToken(); v = createAstBinary(v, cmp(true), AstBinary.EQ); break; case NE: consumeToken(); v = createAstBinary(v, cmp(true), AstBinary.NE); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.EQ) { v = getExtensionHandler(consumeToken()).createAstNode(v, cmp(true)); break; } default: return v; } } }
/** * add := add (<PLUS> mul | <MINUS> mul)* */ protected AstNode add(boolean required) throws ScanException, ParseException { AstNode v = mul(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case PLUS: consumeToken(); v = createAstBinary(v, mul(true), AstBinary.ADD); break; case MINUS: consumeToken(); v = createAstBinary(v, mul(true), AstBinary.SUB); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.ADD) { v = getExtensionHandler(consumeToken()).createAstNode(v, mul(true)); break; } default: return v; } } }
/** * or := and (<OR> and)* */ protected AstNode or(boolean required) throws ScanException, ParseException { AstNode v = and(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case OR: consumeToken(); v = createAstBinary(v, and(true), AstBinary.OR); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.OR) { v = getExtensionHandler(consumeToken()).createAstNode(v, and(true)); break; } default: return v; } } }
/** * or := and (<OR> and)* */ protected AstNode or(boolean required) throws ScanException, ParseException { AstNode v = and(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case OR: consumeToken(); v = createAstBinary(v, and(true), AstBinary.OR); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.OR) { v = getExtensionHandler(consumeToken()).createAstNode(v, and(true)); break; } default: return v; } } }
/** * and := eq (<AND> eq)* */ protected AstNode and(boolean required) throws ScanException, ParseException { AstNode v = eq(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case AND: consumeToken(); v = createAstBinary(v, eq(true), AstBinary.AND); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.AND) { v = getExtensionHandler(consumeToken()).createAstNode(v, eq(true)); break; } default: return v; } } }
/** * and := eq (<AND> eq)* */ protected AstNode and(boolean required) throws ScanException, ParseException { AstNode v = eq(required); if (v == null) { return null; } while (true) { switch (token.getSymbol()) { case AND: consumeToken(); v = createAstBinary(v, eq(true), AstBinary.AND); break; case EXTENSION: if (getExtensionHandler(token).getExtensionPoint() == ExtensionPoint.AND) { v = getExtensionHandler(consumeToken()).createAstNode(v, eq(true)); break; } default: return v; } } }