net.intelie.pipes.grammars.PipeParserCore.jj Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
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
| : 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); }
|