All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.intelie.pipes.grammars.PipeParserCore.jj Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
options{
    static = false;
    java_unicode_escape=true;
}

PARSER_BEGIN(PipeParserCore)
package net.intelie.pipes.generated;

import java.util.*;
import net.intelie.pipes.*;
import net.intelie.pipes.ast.*;
import net.intelie.pipes.types.*;
import net.intelie.pipes.util.*;
import static net.intelie.pipes.Function.*;


public class PipeParserCore {
    public AstStack ast;
    public Token getCurrentToken() { jj_ntk(); return token; }

    public FilterParserCore makeFilterParser() throws ParseException {
        PipesParser.Filter parser = new PipesParser.Filter(new PipesTokenManager.Filter((PipesCharStream)token_source.input_stream));
        parser.token = token;
        return parser;
    }

    private void backup(int amount) {
        token_source.input_stream.backup(amount);
        token.next = null;
        jj_ntk = -1;
    }

    public Token peekNextToken() {
        if (token.next != null) return token.next;
        else return token.next = token_source.getNextToken();
    }

    public AstNode parseFilter() throws ParseException {
        FilterParserCore parser = makeFilterParser();
        FilterToken token = parser.startToContinue();
        backup(token.token.image.length());
        this.token = token.prevToken;
        this.token.next = null;
        return token.node;
    }

    public static String unescape(String s) {
        return Escapes.unescape(s);
    }

    private AstNode maybeObject(AstNode node) {
        if (node instanceof CallNode && (
            Function.OP_PROPERTY.equals(((CallNode)node).getName()))
            || Function.OP_PEEK_PROPERTY.equals(((CallNode)node).getName()))
                return ast.fnCopy(OP_OBJFIX, node);
        return node;
    }

    public AstNode safeProp(AstNode e) {
        if (e != null) return e;
        return ast.fnVirt(Function.OP_PROPERTY);
    }
}

PARSER_END(PipeParserCore)

 SKIP: {
    <[" ", "\n", "\t", "\r", "\u3000", "\u00A0"]>
|   <"--" (~["\r", "\n"])*>
|   <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/">
}

 TOKEN: {
   : IN_OVER
|  : IN_AT
|  : IN_INTERPOLATION
}

 TOKEN: {
     : DEFAULT
|    : DEFAULT
|    : DEFAULT
|    : DEFAULT
|    : DEFAULT
}

 TOKEN: {
    
|    : DEFAULT
}

 TOKEN: {
     : DEFAULT
|   
|   
|   
}

TOKEN: {
    
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   ">
|   =">
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
|   
| | | | | | | | | | | | | "> | "> | "> | ()*> | ()*> | ((|"e-"|"e+"|"E-"|"E+")*)?> | ()*)> | ()*) | ("r"()*)> } TOKEN: { <#INTEGER: ["0"-"9"]> | <#IDENTIFIER_START: (~[" ", "\n", "\t", "\r", "\u3000", "#", "$", ",", "+", "-", "!", "(", ")", ":", "^", "|", "&", "[", "]", "\"", "'", "{", "}", "~", "*", "?", "\\", "/", "%", ">", "<", "=", "0"-"9", "@", ";" ])> | <#IDENTIFIER_CHAR: ( | )> | <#ESCAPED_CHAR: "\\" ~[] > | <#DQUOTED_CHAR : ( ~[ "\"", "\\" ] | ) > | <#SQUOTED_CHAR : ( ~[ "\'", "\\" ] | ) > | <#BQUOTED_CHAR : ( ~[ "{", "}", "\\" ] | ) > } TOKEN: { } AstNode expressionStart() : { AstNode e; }{ e = extendedExpression() {return ast.checkEmpty(e);} } AstNode selectStart() : { AstNode e; }{ e = selectClause() {return ast.checkEmpty(e);} } AstNode topLevelStart() : { AstNode e; }{ e = topLevel() {return ast.checkEmpty(e);} } AstNode start() : { AstNode expr; }{ expr = pipe() {return ast.checkEmpty(expr);} } AstNode fullStart() : { AstNode expr = null; }{ expr = fullPipe() {return ast.checkEmpty(expr);} } AstNode moduleStart() : { AstNode expr = null; }{ expr = module() {return ast.checkEmpty(expr);} } AstNode functionDefStart() : { AstNode expr = null; }{ expr = functionDef() {return ast.checkEmpty(expr);} } public List moduleLenient(): { List es = new ArrayList(); AstNode e; boolean stop; }{ ( try { ()? ( e=functionSignatureExpr() | e=functionSignaturePipe() ) { es.add(e); } | stop = moveUntilBeforeNext(DEF) { if (!stop) break label_1; } } catch (ParseException ex) { if (!moveUntilBeforeNext(DEF)) { break label_1; } } )* { return es; } } JAVACODE boolean moveUntilBeforeNext(int kind) { while(peekNextToken().kind != kind && peekNextToken().kind != EOF) { getNextToken(); } return peekNextToken().kind != EOF; } private AstNode functionDef(): { AstNode signature; AstNode module; AstNode body; Token token; boolean star = false; }{ {ast.pushCall(SourceLocation.Type.FUNCTION_DEF, OP_FUNCTION_DEF, OP_FUNCTION_DEFSTAR);} ( { star = true; })? ( signature=functionSignatureExpr() module=module() body=expression() | signature=functionSignaturePipe() module=module() body=pipe() ) { if (star) return ast.fnPop(OP_FUNCTION_DEFSTAR, signature, module, body); else return ast.fnPop(OP_FUNCTION_DEF, signature, module, body); } } private AstNode functionSignatureExpr(): { List args = new ArrayList(); String nameStr; AstNode name; AstNode description = null; }{ {ast.pushCall(SourceLocation.Type.NONE, OP_FUNCTION_SIG);} {ast.pushLiteral(SourceLocation.Type.NONE);} ( nameStr=identifier() {name=ast.litPop(Type.STRING, nameStr);} argumentList(args) | nameStr=identifierOrNumber() {name=ast.litPop(Type.STRING, "@@" + nameStr);} ) (description=literal())? { args.add(0, name); if (description != null) args.add(1, description); return ast.fnPop(OP_FUNCTION_SIG, args); } } private AstNode functionSignaturePipe(): { List args = new ArrayList(); String nameStr; AstNode name; AstNode description = null; }{ {ast.pushCall(SourceLocation.Type.NONE, OP_FUNCTION_SIG);} {ast.pushLiteral(SourceLocation.Type.NONE);} nameStr=identifier() {name=ast.litPop(Type.STRING, "@" +nameStr);} argumentList(args) (description=literal())? { args.add(0, name); if (description != null) args.add(1, description); return ast.fnPop(OP_FUNCTION_SIG, args); } } private void argumentList(List es): { AstNode e; List special = new ArrayList(); boolean empty = true; }{ ( e = singleArgument() { es.add(e); } ( ( e = singleArgument() { es.add(e); }) ? )* )? ( e = singleArgument(ArgumentFlags.ATT_BY) { special.add(e); } )? ( { token_source.SwitchTo(DEFAULT); } e = singleArgument(ArgumentFlags.ATT_OVER) { special.add(e); } )? ( e = singleArgument(ArgumentFlags.ATT_EVERY) { special.add(e); } )? { es.addAll(0, special); } } private AstNode singleArgument(String... annotations): { String id; AstNode defaultValue = null; boolean empty = true; List flags = new ArrayList(); List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.NONE, OP_FUNCTION_ARG);} ( {ast.pushLiteral(SourceLocation.Type.NONE);} id=identifier() { flags.add(ast.litPop(Type.STRING, "#"+id)); } )* ({ast.pushLiteral(SourceLocation.Type.NONE);} { flags.add(ast.litPop(Type.STRING, ArgumentFlags.VAR_ARGS)); })? ({ast.pushLiteral(SourceLocation.Type.NONE);} { flags.add(ast.litPop(Type.STRING, ArgumentFlags.LATE_BINDING)); })? {ast.pushLiteral(SourceLocation.Type.NONE);} id=identifier() { es.add(ast.litPop(Type.STRING, id)); } { defaultValue = ast.litVirt(Type.NULL, null); } ( (defaultValue=expression() { empty = false; })? { flags.add(ast.litVirt(Type.STRING, ArgumentFlags.OPTIONAL)); if (empty) flags.add(ast.litVirt(Type.STRING, ArgumentFlags.EMPTY)); })? { for(String flag : annotations) flags.add(ast.litVirt(Type.STRING, flag)); if (!flags.isEmpty()) { es.add(defaultValue); for(AstNode flag : flags) es.add(flag); } return ast.fnPop(OP_FUNCTION_ARG, es); } } private AstNode fullPipe(): { AstNode module; AstNode filter; AstNode pipe = null; }{ {ast.pushCall(SourceLocation.Type.FULL, PP_FULL);} module = module() { backup(token.next.image.length()); filter = parseFilter(); } ( pipe=pipe())? { if (pipe != null) return ast.fnPop(PP_FULL, module, filter, pipe); else return ast.fnPop(PP_FULL, module, filter); } } private AstNode module(): { List fns = new ArrayList(); List assertions = new ArrayList(); AstNode e; }{ {ast.pushCall(SourceLocation.Type.MODULE, OP_MODULE, OP_MODULE_ASSERT);} ( e = functionDef() { fns.add(e); } | e = expression() { assertions.add(e); } ( e = expression() { assertions.add(ast.fnCopy(ArgumentFlags.ATT_MESSAGE, e)); } )? )* { e = ast.fn(OP_MODULE, fns); if (assertions.size() > 0) { assertions.add(0, e); e = ast.fn(OP_MODULE_ASSERT, assertions); } return ast.pop(e); } } private AstNode pipe(): { AstNode expr; }{ expr = unionOrProductPipe() { return expr; } } private AstNode unionOrProductPipe(): { AstNode expr, other; List es = new ArrayList(); boolean left, right; }{ {ast.pushCall(SourceLocation.Type.PIPE, PP_UNION, PP_JOIN);} expr = sequencePipe() ( other = sequencePipe() { expr = ast.fn(PP_UNION, expr, other); } | ( { left = right = false; } | { left = true; right = false; } | { left = false; right = true; } | { left = right = true; } ) {es.clear();} ( identifierList(es))? other = sequencePipe() { es.add(expr); es.add(other); es.add(0, ast.litVirt(Type.BOOLEAN, right)); es.add(0, ast.litVirt(Type.BOOLEAN, left)); } { expr = ast.fn(PP_JOIN, es); } )* { return ast.pop(expr); } } private void identifierList(List es): { String e; }{ {ast.pushLiteral(SourceLocation.Type.NONE);} e = identifier() { es.add(ast.litPop(Type.STRING, e)); } ( {ast.pushLiteral(SourceLocation.Type.NONE);} e = identifier() { es.add(ast.litPop(Type.STRING, e)); } )* } private AstNode sequencePipe(): { AstNode expr; List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.PIPE, PP_SEQUENCE);} expr = anyPipe() { es.add(expr); } ( expr = anyPipe() { es.add(expr); } )* { if (es.size() == 1) return ast.pop(es.get(0)); else return ast.fnPop(PP_SEQUENCE, es); } } private AstNode anyPipe(): { AstNode expr; boolean safeBefore; }{ {ast.pushCall(SourceLocation.Type.PIPE, PP_ATOMIC);} ( expr = customPipe() | expr = bracketedPipe() | expr = anyPipe() { expr = ast.fn(PP_ATOMIC, expr); } | expr = defaultPipe() ) { return ast.pop(expr); } } private AstNode bracketedPipe(): { AstNode e; }{ e = pipe() { return e; } } private AstNode customPipe(): { String id; AstNode e; List es = new ArrayList(); }{ {ast.pushCallAny(SourceLocation.Type.PIPE);} id = identifier() paramListWithBy(es) { return ast.fnPop("@" + id, es); } } private AstNode defaultPipe(): { AstNode top = null; AstNode window = null; AstNode output = null; List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.PIPE, PP_SIMPLE);} top = topLevel() ( window = windowWithAnnotation() (output = outputWithAnnotation())? | output = outputWithAnnotation() (window = windowWithAnnotation())? )? { es.add(top); if (window != null) es.add(window); if (output != null) es.add(output); return ast.fnPop(PP_SIMPLE, es); } } private AstNode topLevel(): { List es = new ArrayList(); AstNode e = null; }{ {ast.pushCall(SourceLocation.Type.NONE, OP_TOP_LEVEL);} e = selectClause() { es.add(e); } ( e = byClauseWithAnnotation() { es.add(0, e); } )? { return ast.fnPop(OP_TOP_LEVEL, es); } } private AstNode byClauseWithAnnotation(): { AstNode e; }{ {ast.pushCall(SourceLocation.Type.NONE, ArgumentFlags.ATT_BY);} e=byClause() {return ast.fnPop(ArgumentFlags.ATT_BY, e);} } private AstNode byClause(): { List es = new ArrayList(); AstNode e; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_BY);} ( ( e = expression() { return ast.pop(e); } | e = selectClause() { es.add(e); } ( e = expression() { es.add(ast.litVirt(Type.BOOLEAN, true)); es.add(e); })? ) | e = expression() { es.add(ast.litVirt(Type.BOOLEAN, true)); es.add(e); } ) { return ast.fnPop(OP_BY, es); } } private AstNode windowWithAnnotation(): { AstNode e; }{ {ast.pushCall(SourceLocation.Type.NONE, ArgumentFlags.ATT_OVER);} e=window() {return ast.fnPop(ArgumentFlags.ATT_OVER, e);} } private AstNode window(): { List es = new ArrayList(); AstNode e; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_WINDOW);} ( e = expression() | {ast.pushLiteral(SourceLocation.Type.NONE);} ( { es.add(ast.litPop(Type.STRING, "last")); } periodish(es, false) | { es.add(ast.litPop(Type.STRING, "current")); } periodish(es, false) | { es.add(ast.litPop(Type.STRING, "all")); } | { es.add(ast.litPop(Type.STRING, "while")); } e = expression() { es.add(e); } | { es.add(ast.litPop(Type.STRING, "span")); } e = literalish() { es.add(e); } ( e = literalish() { es.add(e); } )? ) { e = ast.fn(OP_WINDOW, es); } ) { return ast.pop(e); } } private AstNode outputWithAnnotation(): { AstNode e; }{ {ast.pushCall(SourceLocation.Type.NONE, ArgumentFlags.ATT_EVERY);} e=output() {return ast.fnPop(ArgumentFlags.ATT_EVERY, e);} } private AstNode output(): { AstNode e; List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_OUTPUT);} ( ( e = expression() | periodish(es, true) { e = ast.fn(OP_OUTPUT, es); } ) | ()? { e = ast.fn(OP_OUTPUT, ast.litVirt(Type.STRING, "end")); } ) { return ast.pop(e); } } private void periodish(List es, boolean addExpr): { Token u = null; AstNode e = null; int size = es.size(); }{ ( LOOKAHEAD(2) e=function() { es.add(e); } | ( e=literalish() { es.add(e); })? ) { if (e == null) e = ast.litVirt(Type.NUMBER, 1); } ( {ast.pushLiteral(SourceLocation.Type.NONE);} u= { es.add(size, ast.litPop(Type.STRING, u.image) ); } )? { if (u == null && addExpr) { es.add(size, ast.litVirt(Type.STRING, "expr") ); } } } private AstNode selectClause(): { AstNode e; List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.SELECT, OP_SELECT_CLAUSE);} ( e = expressionExceptPipe() { es.add(e); } ( ( e = expression() { es.add(e); } )? )* )? { return ast.fnPop(OP_SELECT_CLAUSE, es); } } private AstNode expression(): { AstNode e; }{ ( e = expressionExceptPipe() | {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EXPLODE);} e = pipe() e = explodeSuffix(e) { ast.pop(); } | {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EXPLODE);} e = pipeCall() e = explodeSuffix(e) { ast.pop(); } ) { return e; } } private AstNode expressionExceptPipe(): { AstNode e; }{ e = expanded() { return e; } } private AstNode expanded(): { AstNode e, index = null; int count = 0; }{ ( {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EXPAND);} e = expression() { e = ast.fnPop(OP_EXPAND, e); } | {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EXPLODE);} (
index=literalish())? e = expression() { e = ast.fnPop(OP_EXPLODE, index != null ? index : ast.litVirt(Type.NUMBER, 1), e); } | {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EXPLODE);} ( e = annotated() e = explodeSuffix(e) ) { ast.pop(); } ) { return e; } } private AstNode explodeSuffix(AstNode e): { int count = 0; }{ ( { count += 1; } )* { if (count > 0) e = ast.fn(OP_EXPLODE, ast.litVirt(Type.NUMBER, count), e); return e; } } private AstNode annotated(): { AstNode e; AstNode id; String s; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_NAMED_FIELD, OP_SORT_FIELD);} ( e = transforms() ( ( {ast.pushLiteral(SourceLocation.Type.NONE);} s = identifier() { id = ast.litPop(Type.STRING, s); } | id = parenExpr() ) { e = ast.fn(OP_NAMED_FIELD, e, id); } )? ( { e = ast.fn(OP_SORT_FIELD, e, ast.litVirt(Type.BOOLEAN, true)); } | { e = ast.fn(OP_SORT_FIELD, e, ast.litVirt(Type.BOOLEAN, false)); } )? | { e = ast.fn(OP_SORT_FIELD, ast.fnVirt(Function.OP_PROPERTY), ast.litVirt(Type.BOOLEAN, true)); } | { e = ast.fn(OP_SORT_FIELD, ast.fnVirt(Function.OP_PROPERTY), ast.litVirt(Type.BOOLEAN, false)); } ) { return ast.pop(e); } } private AstNode transforms(): { AstNode e; AstNode e2; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_TRANSFORM);} e = funOperator() ( LOOKAHEAD(1) e2 = pipeOrFun() { e = ast.fn(OP_TRANSFORM, e, e2); } )* { return ast.pop(e); } } private AstNode pipeOrFun(): { AstNode e; }{ ( e = pipeCall() | e = funOperator() | e = bracketedPipe() ) { return e; } } private AstNode pipeCall(): { String id; AstNode e; List es = new ArrayList(); }{ {ast.pushCallAny(SourceLocation.Type.EXPRESSION);} id = identifier() ( paramListWithBy(es) )? { return ast.fnPop("@" + id, es); } } private AstNode funOperator(): { AstNode arg = null; AstNode e; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_FUN);} ( ( arg = argModifier() )? e = funOperator() { if (arg != null) e = ast.fn(OP_FUN, arg, e); } | e = iif() ) { return ast.pop(e); } } private AstNode argModifier(): { String id; AstNode e; List es = new ArrayList(); }{ {ast.pushCall(SourceLocation.Type.NONE, OP_ARGMOD);} ( {ast.pushLiteral(SourceLocation.Type.NONE);} id=identifier() { return ast.fnPop(OP_ARGMOD, ast.litPop(Type.STRING, id)); } | e = argModifier() { es.add(e); } ( ( e = argModifier() { es.add(e); })? )* { return ast.fnPop(OP_ARGMOD, es); } ) } private AstNode iif(): { AstNode e1, e2, e3; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_IIF);} e1=ors() ( e2=iif() e3=iif() { e1 = ast.fn(OP_IIF, e1, e2, e3); } )? { return ast.pop(e1); } } private AstNode ors(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_OR);} lhs=xors() ( rhs=xors() { lhs=ast.fn(OP_OR, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode xors(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_XOR);} lhs=ands() ( (|) rhs=ands() { lhs=ast.fn(OP_XOR, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode ands(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_AND);} lhs=equalities() ( (|) rhs=equalities() { lhs=ast.fn(OP_AND, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode equalities(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_EQ, OP_NEQ);} lhs=comparisons() ( rhs=comparisons() { lhs=ast.fn(OP_EQ, lhs, rhs); } | rhs=comparisons() { lhs=ast.fn(OP_NEQ, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode comparisons(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_GT, OP_GTEQ, OP_LT, OP_LTEQ);} lhs=sums() ( rhs=sums() { lhs=ast.fn(OP_GT, lhs, rhs); } | rhs=sums() { lhs=ast.fn(OP_GTEQ, lhs, rhs); } | rhs=sums() { lhs=ast.fn(OP_LT, lhs, rhs); } | rhs=sums() { lhs=ast.fn(OP_LTEQ, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode sums(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_ADD, OP_SUB);} lhs=mults() ( rhs=mults() { lhs=ast.fn(OP_ADD, lhs, rhs); } | rhs=mults() { lhs=ast.fn(OP_SUB, lhs, rhs); } )* {return ast.pop(lhs); } } private AstNode mults(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_MUL, OP_DIV, OP_MOD, OP_INTDIV);} lhs=pows() ( rhs=pows() { lhs=ast.fn(OP_MUL, lhs, rhs); } |
rhs=pows() { lhs=ast.fn(OP_DIV, lhs, rhs); } | rhs=pows() { lhs=ast.fn(OP_MOD, lhs, rhs); } | rhs=pows() { lhs=ast.fn(OP_INTDIV, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode pows(): { AstNode lhs, rhs; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_POW);} lhs=coalesce() ( rhs=coalesce() { lhs=ast.fn(OP_POW, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode coalesce(): { AstNode lhs, rhs; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_COALESCE);} lhs=unary() ( rhs=unary() { lhs=ast.fn(OP_COALESCE, lhs, rhs); } )* {return ast.pop(lhs);} } private AstNode unary(): { AstNode e; } { {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_NEG, OP_NOT, OP_HAT);} ( e=rightcreate() | e=unary() | e=unary() { e = ast.fn(OP_NEG, e); } | (|) e=unary() { e = ast.fn(OP_NOT, e); } | e=unary() { e = ast.fn(OP_HAT, e); } ) { return ast.pop(e); } } private AstNode rightcreate(): { AstNode e = null; }{ {ast.pushAny(SourceLocation.Type.EXPRESSION);} ( e=indexing() ( e = rightpart(e) )* | ( e = rightpart(e) )+ ) { return ast.pop(e); } } private AstNode rightpart(AstNode e) : { AstNode e2; String id; }{ ( id = identifier() { e = innerCall(safeProp(e), id); } | ( { e = ast.fn(Type.NUMBER.name(), safeProp(e)); } | { e = ast.fn(Type.STRING.name(), safeProp(e)); } ) ( id = identifier() { e = innerCall(e, id); } )? | { e = maybeObject(safeProp(e)); } ( e=propertyBase(OP_PEEK_PROPERTY, e) | e2=parenExpr() { e = ast.fn(OP_PEEK, e, e2); } ) ) { return e; } } private AstNode innerCall(AstNode e, String op) : { List es = new ArrayList(); }{ { es.add(e); } ( paramListWithBy(es) )? { return ast.fn(op, es); } } private AstNode indexing(): { AstNode e; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_GET);} e=primary() ( { List args = new ArrayList(); args.add(e); } paramList(args) { e = ast.fn(OP_GET, args); } )* { return ast.pop(e);} } private AstNode primary(): { AstNode e; }{ ( e=period() | e=rawValue() | LOOKAHEAD(2) e=function() | e=property() ) { return e; } } private AstNode period(): { AstNode e; AstNode unit = null; AstNode zone = null; String id; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_PERIOD);} e=literalish() ( ( unit = literalish() | {ast.pushLiteral(SourceLocation.Type.NONE);} id = identifier() { unit = ast.litPop(Type.STRING, id); } ) ( zone = literalish() | {ast.pushLiteral(SourceLocation.Type.NONE);} id= identifier() { zone = ast.litPop(Type.STRING, id); } )? { e = zone != null ? ast.fn(OP_PERIOD, e, unit, zone) : ast.fn(OP_PERIOD, e, unit); } )? { return ast.pop(e); } } private AstNode literalish(): { AstNode e = null; }{ ( e=literal() | e=macro() | e=filter() | e=parenExpr() ) { return e; } } private AstNode parenExpr(): { List es = new ArrayList(); AstNode e = null; AstNode by = null; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_ROW);} ( e=expression() ( { es.add(e); e = null; } paramList(es) )? ( by = byClauseWithAnnotation() { if (e != null) es.add(e); e = null; es.add(0, by); } )? | e = byClause() | e = window() | e = output() )? { if (e == null) e = ast.fn(OP_ROW, es); return ast.pop(e); } } private AstNode extendedExpression(): { AstNode e = null; }{ ( e = expression() | e = byClause() | e = window() | e = output() ) { return e; } } private AstNode rawValue(): { }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_RAW);} { return ast.fnPop(OP_RAW); } } private AstNode filter(): { }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_FILTER);} { return ast.fnPop(OP_FILTER, parseFilter()); } } private AstNode macro(): { String id; }{ {ast.pushCallAny(SourceLocation.Type.EXPRESSION);} id=identifierOrNumber() (LOOKAHEAD(2) )? { return ast.fnPop("@@" + id); } } private AstNode literal(): { AstNode e; }{ ( e=number() | e=bool() | e=string() | e=nullable() | e = interpolatedString() ) { return e; } } private AstNode string(): { Token e; }{ {ast.pushLiteral(SourceLocation.Type.EXPRESSION);} ( e= { return ast.litPop(Type.STRING, unescape(e.image.substring(1, e.image.length()-1))); } | e= { return ast.litPop(Type.STRING, e.image.substring(2, e.image.length()-1)); } ) } private AstNode interpolatedString(): { Token token; StringBuilder builder = new StringBuilder(); List args = new ArrayList(); AstNode e; }{ {ast.pushCall(SourceLocation.Type.EXPRESSION, OP_CONCAT);} {ast.pushLiteral(SourceLocation.Type.NONE);} ( (token=|token=) {builder.append(token.image);} | { token_source.SwitchTo(DEFAULT); } e=expression() { token_source.SwitchTo(IN_INTERPOLATION); backup(1); } { if (builder.length() != 0) { args.add(ast.lit(Type.STRING, unescape(builder.toString()))); builder.setLength(0); } ast.pop(); ast.pushLiteral(SourceLocation.Type.NONE); args.add(ast.fnCopy(Type.STRING.name(), e)); } )* { if (builder.length() != 0 || args.size() == 0) { args.add(ast.lit(Type.STRING, unescape(builder.toString()))); builder.setLength(0); } ast.pop(); if (args.size() == 1) return ast.pop(args.get(0)); return ast.fnPop(OP_CONCAT, args); } } private AstNode number(): { Token e; }{ {ast.pushLiteral(SourceLocation.Type.EXPRESSION);} e = { return ast.litPop(Type.NUMBER, e.image); } } private AstNode bool(): { Token e; }{ {ast.pushLiteral(SourceLocation.Type.EXPRESSION);} ( e = { return ast.litPop(Type.BOOLEAN, true); } | e = { return ast.litPop(Type.BOOLEAN, false); } ) } private AstNode nullable(): { }{ {ast.pushLiteral(SourceLocation.Type.EXPRESSION);} { return ast.litPop(Type.NULL, null); } } private AstNode function(): { String id; List es = new ArrayList(); }{ {ast.pushCallAny(SourceLocation.Type.EXPRESSION);} id = identifier() paramListWithBy(es) { return ast.fnPop(id, es); } } private void paramListWithBy(List es): { AstNode e; AstNode by = null, window = null, output = null; }{ paramList(es) (by = byClauseWithAnnotation())? ( window = windowWithAnnotation() (output = outputWithAnnotation())? | output = outputWithAnnotation() (window = windowWithAnnotation())? )? { List add = new ArrayList(); if (by != null) { add.add(by); } if (window != null) { add.add(window); } if (output != null) { add.add(output); } es.addAll(0, add); } } private int paramList(List es): { AstNode e; int count=0; }{ ( e = expression() { es.add(e); count++; } ( ( e = expression() { es.add(e); count++; } )? )* )? { return count; } } private String identifierOrNumber(): { String id; Token e; }{ id = identifier() { return id; } | e = { return e.image; } } private String identifier(): { Token e; }{ e = { return e.image; } | e = { return unescape(e.image.substring(1, e.image.length()-1)); } } private AstNode propertyBase(String fn, AstNode target): { String id = null; List args = new ArrayList(); boolean hasArgs = false; }{ { if (target != null) args.add(target); ast.pushLiteral(SourceLocation.Type.NONE); } ( id = identifier() | ) { args.add(ast.litPop(Type.STRING, id)); } (LOOKAHEAD(2) { args.add(ast.litVirt(Type.BOOLEAN, true)); } paramList(args) )? { return ast.fn(fn, args); } } private AstNode property(): { AstNode e; }{ { ast.pushCall(SourceLocation.Type.EXPRESSION, OP_PROPERTY); } e = propertyBase(OP_PROPERTY, null) { return ast.pop(e); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy