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

org.apache.commons.jexl2.parser.Parser.jjt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


options
{
   MULTI=true;
   STATIC=false;
   VISITOR=true;
   NODE_SCOPE_HOOK=true;
   NODE_CLASS="JexlNode";
   UNICODE_INPUT=true;
}

PARSER_BEGIN(Parser)

package org.apache.commons.jexl2.parser;

import java.io.Reader;
import org.apache.commons.jexl2.JexlInfo;

public class Parser extends JexlParser
{
    public boolean ALLOW_REGISTERS = false;

    public ASTJexlScript parse(Reader reader, JexlInfo info)
        throws ParseException {
        /*
         * If registers are allowed, the default parser state has to be REGISTERS.
         */
        if (ALLOW_REGISTERS) {
            token_source.defaultLexState = REGISTERS;
        }
        ReInit(reader);
        /*
         *  lets do the 'Unique Init' in here to be
         *  safe - it's a pain to remember
         */

        ASTJexlScript tree = JexlScript();
        tree.value = info;
        return tree;
    }

}

PARSER_END(Parser)


/***************************************
 *     Skip & Number literal tokens
 ***************************************/

<*> SKIP : /* WHITE SPACE */
{
      <"##" (~["\n","\r"])* ("\n" | "\r" | "\r\n")? >
    | <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/">
    | <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")? >
    | " "
    | "\t"
    | "\n"
    | "\r"
    | "\f"
}

<*> TOKEN : /* KEYWORDS */
{
      < IF : "if" >
    | < ELSE : "else" >
    | < FOR : "for" >
    | < FOREACH : "foreach" > : FOR_EACH_IN
    | < WHILE : "while" >
    | < NEW : "new" >
    | < VAR : "var" >
    | < EMPTY : "empty" >
    | < SIZE : "size" >
    | < NULL : "null" >
    | < TRUE : "true" >
    | < FALSE : "false" >
    | < RETURN : "return" >
}

 TOKEN : /* foreach in */
{
    < IN : "in" > : DEFAULT
}

<*> TOKEN : { /* SEPARATORS */
      < LPAREN : "(" >
    | < RPAREN : ")" >
    | < LCURLY : "{" >
    | < RCURLY : "}" >
    | < LBRACKET : "[" >
    | < RBRACKET : "]" >
    | < SEMICOL : ";" >
    | < COLON : ":" >
    | < COMMA : "," >
    | < DOT : "." >
}

<*> TOKEN : { /* CONDITIONALS */
      < QMARK : "?" >
    | < ELVIS : "?:" >
    | < AND : "&&" | "and" >
    | < OR : "||" | "or" >
}

<*> TOKEN : { /* COMPARISONS */
      < eq : "==" | "eq" >
    | < ne : "!=" | "ne" >
    | < req : "=~" >
    | < rne : "!~" >
    | < gt : ">" | "gt" >
    | < ge : ">=" | "ge" >
    | < lt : "<" | "lt" >
    | < le : "<=" | "le" >
}

<*> TOKEN : { /* OPERATORS */
      < assign : "=" >
    | < mod : "%" | "mod" >
    | < div : "/" | "div" >
    | < not : "!" | "not" >
    | < plus : "+" >
    | < minus : "-" >
    | < mult : "*" >
    | < tilda : "~" >
    | < and : "&" >
    | < or : "|" >
    | < xor : "^" >
}

/***************************************
 *     Identifier & String tokens
 ***************************************/

<*> TOKEN : /* IDENTIFIERS */
{
  < IDENTIFIER:  (|)* >
|
  < #LETTER: [ "a"-"z", "A"-"Z", "_", "$", "@" ] >
|
  < #DIGIT: [ "0"-"9"] >
}

 TOKEN : /* REGISTERS: parser.ALLOW_REGISTER must be set to true before calling parse */
{
  < REGISTER: "#" (["0"-"9"])+ >
}

<*> TOKEN : /* LITERALS */
{
    < INTEGER_LITERAL:
        ( "0" (["0"-"7"])* | ["1"-"9"] (["0"-"9"])* | "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ )
        (["l","L","h","H"])?
    >
 |
  < FLOAT_LITERAL: ()+ "." ()+ ((["e","E"])(["+","-"])?()+)? (["d","D","f","F","b","B"])? >
}

<*> TOKEN :
{
  < STRING_LITERAL:
    "\"" (~["\"","\\","\n","\r","\u2028","\u2029"] | "\\" ~["\n","\r","\u2028","\u2029"])* "\""
  |
    "'" (~["'","\\","\n","\r","\u2028","\u2029"] | "\\" ~["\n","\r","\u2028","\u2029"])* "'"
  >
}

/***************************************
 *      Statements
 ***************************************/

ASTJexlScript JexlScript() : {}
{
   ( Statement() )* 
   { return jjtThis;}
}

void Statement() #void : {}
{
    
    | LOOKAHEAD(3) Block()
    | IfStatement()
    | ForeachStatement()
    | WhileStatement()
    | ExpressionStatement()
    | ReturnStatement()
    | Var()
}

void Block() #Block : {}
{
     ( Statement() )* 
}


void ExpressionStatement() #void : {}
{
    Expression() (LOOKAHEAD(1) Expression() #Ambiguous())* (LOOKAHEAD(2) )?
}


void IfStatement() : {}
{
      Expression()  Statement() ( LOOKAHEAD(1)  Statement() )?
}


void WhileStatement() : {}
{
      Expression()  Statement()
}


void ForeachStatement() : {}
{  
      LValueVar()   Expression()  Statement()
|
      LValueVar()   Expression()  Statement()
}

void ReturnStatement() : {}
{
     Expression()
}


/***************************************
 *      Expression syntax
 ***************************************/

void Expression() #void : {}
{
    ConditionalExpression() (LOOKAHEAD(1)  Expression() #Assignment(2) )?
}

void Assignment() #Assignment(2) : {}
{
    ConditionalExpression()  Expression()
}

void Var() #void : {}
{
     DeclareVar() (LOOKAHEAD(1)  Expression() #Assignment(2))?
}

void DeclareVar() #Var :
{
    Token t;
}
{
    t= { declareVariable(jjtThis, t.image); }
}

void LValueVar() #Reference : {}
{   
    LOOKAHEAD(1)  DeclareVar() DotReference() 
|
    LOOKAHEAD(1) Identifier(true) DotReference()
}

/***************************************
 *      Conditional & relational
 ***************************************/

void ConditionalExpression() #void : {}
{
  ConditionalOrExpression()
  (
     Expression()  Expression() #TernaryNode(3)
  |
     Expression() #TernaryNode(2)
  )?
}

void ConditionalOrExpression() #void :
{}
{
  ConditionalAndExpression()
  (
     ConditionalAndExpression() #OrNode(2)
  )*
}

void ConditionalAndExpression() #void :
{}
{
  InclusiveOrExpression()
  (
     InclusiveOrExpression() #AndNode(2)
  )*
}

void InclusiveOrExpression() #void :
{}
{
  ExclusiveOrExpression()
  (  ExclusiveOrExpression() #BitwiseOrNode(2) )*
}

void ExclusiveOrExpression() #void :
{}
{
  AndExpression()
  (  AndExpression() #BitwiseXorNode(2) )*
}

void AndExpression() #void :
{}
{
  EqualityExpression()
  (  EqualityExpression() #BitwiseAndNode(2) )*
}

void EqualityExpression() #void :
{}
{
  RelationalExpression()
  (
      RelationalExpression() #EQNode(2)
   |
      RelationalExpression() #NENode(2)
  )?
}

void RelationalExpression() #void :
{}
{
  AdditiveExpression()
  (
     AdditiveExpression() #LTNode(2)
   |
     AdditiveExpression() #GTNode(2)
   |
     AdditiveExpression() #LENode(2)
   |
     AdditiveExpression() #GENode(2)
   |
     AdditiveExpression() #ERNode(2) // equals regexp
   |
     AdditiveExpression() #NRNode(2) // not equals regexp
  )?
}

/***************************************
 *      Arithmetic
 ***************************************/

void AdditiveExpression() #AdditiveNode(>1) : {}
{
    MultiplicativeExpression() ( LOOKAHEAD(1) AdditiveOperator() MultiplicativeExpression())*
}

void AdditiveOperator() : {}
{
     { jjtThis.image = "+"; }
|
     { jjtThis.image = "-"; }
}

void MultiplicativeExpression() #void : {}
{
  UnaryExpression()
  (
     UnaryExpression() #MulNode(2)
   |
    
UnaryExpression() #DivNode(2) | UnaryExpression() #ModNode(2) )* } void UnaryExpression() #void : {} { UnaryExpression() #UnaryMinusNode(1) | UnaryExpression() #BitwiseComplNode(1) | UnaryExpression() #NotNode(1) | PrimaryExpression() } /*************************************** * Identifier & Literals ***************************************/ void Identifier(boolean top) : { Token t; } { t= { jjtThis.image = top? checkVariable(jjtThis, t.image) : t.image; } | t= { jjtThis.image = t.image; jjtThis.setRegister(t.image); } } void StringIdentifier() #Identifier : { Token t; } { t= { jjtThis.image = Parser.buildString(t.image, true); } } void Literal() #void : { Token t; } { IntegerLiteral() | FloatLiteral() | BooleanLiteral() | StringLiteral() | NullLiteral() } void NullLiteral() : {} { } void BooleanLiteral() #void : {} { #TrueNode | #FalseNode } void IntegerLiteral() #NumberLiteral : { Token t; } { t= { jjtThis.image = t.image; jjtThis.setNatural(t.image); } } void FloatLiteral() #NumberLiteral: { Token t; } { t= { jjtThis.image = t.image; jjtThis.setReal(t.image); } } void StringLiteral() : { Token t; } { t= { jjtThis.image = Parser.buildString(t.image, true); } } void ArrayLiteral() : {} { (Expression() ( Expression() )*)? } void MapLiteral() : {} { ( MapEntry() ( MapEntry() )* | ) } void MapEntry() : {} { Expression() Expression() } /*************************************** * Functions & Methods ***************************************/ void EmptyFunction() : {} { LOOKAHEAD(3) Expression() | Reference() } void SizeFunction() : {} { Expression() } void Function() #FunctionNode: {} { Identifier() Identifier() [ Expression() ( Expression() )* ] } void Method() #MethodNode: {} { Identifier() [ Expression() ( Expression() )* ] } void AnyMethod() #void : {} { LOOKAHEAD() SizeMethod() | LOOKAHEAD(Identifier() ) Method() } void SizeMethod() : {} { } void Constructor() #ConstructorNode() : {} { [ Expression() ( Expression() )* ] } /*************************************** * References ***************************************/ void PrimaryExpression() #void : {} { LOOKAHEAD(2) Reference() | LOOKAHEAD( ) EmptyFunction() | LOOKAHEAD( ) SizeFunction() | LOOKAHEAD( ) Constructor() | LOOKAHEAD( (Expression())? ) MapLiteral() | LOOKAHEAD( (Expression() | ) ) ArrayLiteral() | Literal() } void ArrayAccess() : {} { Identifier() (LOOKAHEAD(1) Expression() )+ } void DotReference() #void : {} { ( ( LOOKAHEAD(Identifier() ) ArrayAccess() | ( LOOKAHEAD(2) AnyMethod() | Identifier() | IntegerLiteral() | StringIdentifier() ) ) )* } void Reference() : {} { ( LOOKAHEAD() Constructor() | LOOKAHEAD(Identifier() ) ArrayAccess() | LOOKAHEAD(Identifier() Identifier() ) Function() | LOOKAHEAD(Identifier() ) Method() | LOOKAHEAD() MapLiteral() | LOOKAHEAD() ArrayLiteral() | LOOKAHEAD( Expression() ) ReferenceExpression() | StringLiteral() | Identifier(true) ) DotReference() } /** * ReferenceExpression is a subclass of ArrayAccess */ void ReferenceExpression() : {} { Expression() (LOOKAHEAD(1) Expression() )* }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy