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

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

Go to download

Jexl is an implementation of the JSTL Expression Language with extensions.

There is a newer version: 1.1-hudson-20090508
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.
 */

/**
 *  Jexl : Java Expression Language
 *
 *  @author Geir Magnusson Jr.
 *  @author Mark H. Wilkinson
 *
 *  @version $Id: Parser.jjt 584037 2007-10-12 03:35:21Z proyal $
 */

options
{
   MULTI=true;
   STATIC=false;
   VISITOR=true;
   NODE_USES_PARSER=true;
   UNICODE_INPUT=true;
}

PARSER_BEGIN(Parser)

package org.apache.commons.jexl.parser;

import java.io.Reader;
import java.io.ByteArrayInputStream;

import org.apache.commons.jexl.util.introspection.Uberspect;

public class Parser
{

    private Uberspect uberspect;

    public void setUberspect(Uberspect uberspect)
    {
        this.uberspect = uberspect;
    }

    protected Uberspect getUberspect()
    {
        return uberspect;
    }

    public SimpleNode parse(Reader reader)
        throws Exception
    {
        ReInit(reader);

        /*
         *  lets do the 'Unique Init' in here to be
         *  safe - it's a pain to remember
         */

        SimpleNode tree = JexlScript();
        return tree;
    }
}

PARSER_END(Parser)


SKIP : /* WHITE SPACE */
{
    
|
  " "
| "\t"
| "\n"
| "\r"
| "\f"
}

TOKEN : /* LITERALS */
{
  < INTEGER_LITERAL: ()+ >
|
  < FLOAT_LITERAL: ()+ "."()+ >
}

/*
 * Program structuring syntax follows.
 */

SimpleNode JexlScript() :
{
   String name;
}
{
    (
       Statement()
    )*
    
   { return jjtThis;}
}


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

void EmptyFunction() : {}
{
    "empty"
    ( Reference() | "(" Reference() ")" )
}

void SizeFunction() : {}
{
    "size" "(" Reference() ")"
}

void Identifier() :
{
   Token t;
}
{
    t=
    {
       jjtThis.val = t.image;
    }
}


/*
 * Expression syntax follows.
 */

void Expression() : {}
{
  LOOKAHEAD( PrimaryExpression() "=" )
  Assignment()
|
  ConditionalOrExpression()
}

void Assignment() #Assignment(2) :
{}
{
  PrimaryExpression() "=" Expression()
}

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

void ConditionalAndExpression() #void :
{}
{
  InclusiveOrExpression()
  (
    "&&" InclusiveOrExpression() #AndNode(2)
  |
    "and" 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)
   |
     "eq" RelationalExpression() #EQNode(2)
   |
     "!=" RelationalExpression() #NENode(2)
   |
     "ne" RelationalExpression() #NENode(2)
  )*
}

void RelationalExpression() #void :
{}
{
  AdditiveExpression()
  (
    "<" AdditiveExpression() #LTNode(2)
   |
    "lt" AdditiveExpression() #LTNode(2)
   |
    ">" AdditiveExpression() #GTNode(2)
   |
    "gt" AdditiveExpression() #GTNode(2)
   |
    "<=" AdditiveExpression() #LENode(2)
   |
    "le" AdditiveExpression() #LENode(2)
   |
    ">=" AdditiveExpression() #GENode(2)
   |
    "ge" AdditiveExpression() #GENode(2)
  )*
}

void AdditiveExpression() #void :
{}
{
  MultiplicativeExpression()
  (
    "+" MultiplicativeExpression() #AddNode(2)
   |
    "-" MultiplicativeExpression() #SubtractNode(2)
  )*
}

void MultiplicativeExpression() #void :
{}
{
  UnaryExpression()
  (
    "*" UnaryExpression() #MulNode(2)
   |
    "/" UnaryExpression() #DivNode(2)
   |
    "div" UnaryExpression() #DivNode(2)
   |
    "%" UnaryExpression() #ModNode(2)
   |
    "mod" UnaryExpression() #ModNode(2)
  )*
}

void UnaryExpression() #void :
{}
{
  "-" UnaryExpression() #UnaryMinusNode(1)
|
  "~" UnaryExpression() #BitwiseComplNode(1)
|
  "!" UnaryExpression() #NotNode(1)
|
  "not" UnaryExpression() #NotNode(1)
|
  PrimaryExpression()
}

void PrimaryExpression() #void :
{}
{
  Literal()
|
  LOOKAHEAD(3)
  Reference()
|
  "(" Expression() ")"
|
  EmptyFunction()
|
  SizeFunction()
|
  LOOKAHEAD(3)
  ArrayLiteral()
|
  MapLiteral()
}


void Literal() #void :
{
   Token t;
}
{
  IntegerLiteral()
|
  FloatLiteral()
|
  BooleanLiteral()
|
  StringLiteral()
|
  NullLiteral()
}

void NullLiteral() : {}
{
    "null"
}

void BooleanLiteral() #void :
{}
{
  "true" #TrueNode
|
  "false" #FalseNode
}

void IntegerLiteral() :
{
  Token t;
}
{
 (
  t=
    {
       jjtThis.val = Integer.valueOf(t.image);
    }
 )
}

void FloatLiteral() :
{
  Token t;
}
{
 (
  t=
    {
       jjtThis.val = Float.valueOf(t.image);
    }
 )
}

void StringLiteral() :
{
   Token t;
}
{
 (
  t=
    {
       jjtThis.literal = t.image.substring(1,t.image.length()-1);
    }
 )
}

/*
 * Statement syntax follows.
 */

void Statement() #void :
{}
{
  ";"
|
  LOOKAHEAD(3)
  Block()
|
  LOOKAHEAD( Reference() ";" ) ReferenceExpression()
|
  LOOKAHEAD( PrimaryExpression() "=" ) StatementExpression()
|
  ExpressionExpression()
|
  IfStatement()
|
  ForeachStatement()
|
  WhileStatement()
}


void ExpressionExpression() : {}
{
    Expression() ";"
}

void StatementExpression() : {}
{
  Assignment() ";"
}

void ReferenceExpression() : {}
{
  Reference() ";"
}

void IfStatement() :{}
{
  "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) "else" Statement() ]
}


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

void ForeachStatement() :
{}
{
  "foreach" "(" Reference() "in"  Reference() ")" Statement()
}

void MapLiteral() : {}
{
  "["  MapEntry() ( "," MapEntry() )* "]"
}

void ArrayLiteral() : {}
{
  "["  Parameter() ( "," Parameter() )* "]"
}

void MapEntry() : {}
{
    Parameter() "=>" Parameter()
}

void Method() : {}
{
   Identifier() "("[ Parameter() ( "," Parameter() )* ] ")"
}

void ArrayAccess() : {}
{
    Identifier() ("[" ( LOOKAHEAD(3) Expression() | IntegerLiteral() | Reference() ) "]")+
}

void SizeMethod() : {}
{
	"size" "(" ")"
}

void Reference() : {}
{
  (LOOKAHEAD(Identifier() "[" ( Expression() | IntegerLiteral() | Reference()) "]")
      ArrayAccess()
    |
      Identifier()
    |
      LOOKAHEAD(3)
      MapLiteral()
    |
      ArrayLiteral()
  )
  (LOOKAHEAD(2) "."
    (
      LOOKAHEAD(Identifier() "[" ( Expression() | IntegerLiteral() | Reference()) "]") ArrayAccess() |
//      (LOOKAHEAD(3) Method() | Identifier() |  IntegerLiteral() )
      (LOOKAHEAD(3) Method() | SizeMethod() | Identifier() |  IntegerLiteral() )

    )
  )*
}


void Parameter() #void: {}
{
   LOOKAHEAD(3) Expression() |  Literal() | Reference()
}

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

TOKEN :
{
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy