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

com.ximpleware.extended.xpath.parser.cup Maven / Gradle / Ivy

Go to download

XimpleWare's VTD-XML is, far and away, the industry's most advanced and powerful XML processing model for SOA and Cloud Computing

The newest version!
/* 
 * Copyright (C) 2002-2012 XimpleWare, [email protected]
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package com.ximpleware.extended.xpath;
import java_cup.runtime.*;
import com.ximpleware.extended.*;
//import com.ximpleware.extended.xpath.*;
import java.util.*;
parser code {:  
  public Hashtable nsHash;
  public Hashtable symbolHash;
  Step tempStep;
  NodeTest tempNt;
  LocationPathExpr tempLPExpr;
 // ExprList tmpEL1, tmpEL2;
  
  public parser (java.io.Reader input) {
    super(new Yylex(input));
    nsHash = null;
  }
  
  public parser (java.io.InputStream input) {
    super(new Yylex(input));
  }

  public static void main(String args[]){
    try {
      parser p = new parser(System.in);
      Object result = p.parse().value;
      //System.out.println(((Expr)result).evalNumber(null));
      //System.out.println(((Expr)result));
    }
    catch (Exception e) {
		System.out.println("caught: "+e);
    }
  }
 
  public void report_error(String message, Object info) {
	//throw new XPathParseException("Syntax error during parsing");
  }

  public void report_fatal_error(String message, Object info) throws XPathParseExceptionHuge{
	throw new XPathParseExceptionHuge("Syntax error during parsing: "+ message);
  }

  public void syntax_error(Symbol cur_token) {
	
  }
  
  public void unrecovered_syntax_error(Symbol cur_token) throws XPathParseExceptionHuge{
	 Yylex scanner = (Yylex)getScanner();
	 throw new XPathParseExceptionHuge("XPath Syntax error: "+cur_token, scanner.getOffset());
  }
 :};

terminal ADD, SUB, DOT, DDOT, AT, COMMA, LP, RP, LB, RB, GT, LT, GE, LE, EQ, NE;
terminal MULT, SLASH, DSLASH, DIV, MOD, AND, OR, UNION, UMINUS, DOLLAR;
terminal Double NUMBER;
terminal String LITERAL;

terminal FuncName FNAME;
terminal Ntest NTEST;
terminal NameType NAME;
terminal AxisType AXISNAME;


non terminal FuncExpr FunctionCall; 
non terminal FuncName FunctionName;
non terminal Expr Expr, OrExpr, EqualityExpr, AndExpr, RelationalExpr, AdditiveExpr;
non terminal Expr MultiplicativeExpr, UnaryExpr, PathExpr, FilterExpr, VariableReference;
non terminal UnionExpr UnionExpr;
non terminal Alist ArgumentList;
non terminal Expr PrimaryExpr, Argument;
non terminal LocationPathExpr LocationPath ;
non terminal AxisType AxisSpecifier, AbbreviatedAxisSpecifier;
non terminal NodeTest nodetest ;
non terminal Step Step, AbbreviatedStep, RelativeLocationPath, AbsoluteLocationPath;
non terminal Step AbbreviatedAbsoluteLocationPath, AbbreviatedRelativeLocationPath;
non terminal Predicate Predicate,PredicateList; 

precedence left OR;
precedence left AND;
precedence left EQ, NE;
precedence left GE, LE, GT, LT;
precedence nonassoc UMINUS;

Expr    	::= OrExpr:o
		{: RESULT = o; :}
		;

OrExpr 		::= AndExpr:a 
		{: RESULT = a; :}
		|  OrExpr:o OR AndExpr:a
		{: RESULT = new BinaryExpr(o,BinaryExpr.OR,a);:}
		; 
AndExpr 	::= EqualityExpr: ee
		{: RESULT = ee; :}
		|  AndExpr:a AND EqualityExpr:ee
		{: RESULT = new BinaryExpr(a, BinaryExpr.AND, ee); :}
		;

EqualityExpr    ::= RelationalExpr:re  
		{: RESULT = re; :}
		|   EqualityExpr:ee EQ RelationalExpr:re  
		{: RESULT = new BinaryExpr(ee, BinaryExpr.EQ, re); :}
		|   EqualityExpr:ee NE RelationalExpr:re 
		{: RESULT = new BinaryExpr(ee, BinaryExpr.NE, re); :}
		;

RelationalExpr  ::= AdditiveExpr:ae  
		{: RESULT = ae; :}
  		|    RelationalExpr:re LT AdditiveExpr:ae  
		{: RESULT = new BinaryExpr(re, BinaryExpr.LT, ae); :}
		|    RelationalExpr:re GT AdditiveExpr:ae  
		{: RESULT = new BinaryExpr(re, BinaryExpr.GT, ae); :}
		|    RelationalExpr:re LE AdditiveExpr:ae  
		{: RESULT = new BinaryExpr(re, BinaryExpr.LE, ae);  :}
		|    RelationalExpr:re GE AdditiveExpr:ae  
		{: RESULT = new BinaryExpr(re, BinaryExpr.GE, ae); :}
		;

AdditiveExpr    ::= MultiplicativeExpr:me  
		{: RESULT = me; :}
		|   AdditiveExpr:ae ADD MultiplicativeExpr:me  
		{: RESULT = new BinaryExpr(ae, BinaryExpr.ADD, me); :}
		|    AdditiveExpr:ae SUB MultiplicativeExpr:me  
		{: RESULT = new BinaryExpr(ae, BinaryExpr.SUB, me); :}
		;

MultiplicativeExpr    ::=  UnaryExpr:ue  
		{: RESULT = ue; :}
   		|    MultiplicativeExpr:me MULT UnaryExpr:ue  
		{: RESULT = new BinaryExpr(me, BinaryExpr.MULT, ue); :}
		|    MultiplicativeExpr:me DIV UnaryExpr:ue  
		{: RESULT = new BinaryExpr(me, BinaryExpr.DIV, ue); :}
		|    MultiplicativeExpr:me MOD UnaryExpr:ue   
		{: RESULT = new BinaryExpr(me, BinaryExpr.MOD, ue); :}
		;

UnaryExpr    	::=    UnionExpr:ue  
		{: RESULT = ue; :}
  		|    SUB UnaryExpr:ue   
		{: RESULT = new UnaryExpr( BinaryExpr.SUB, ue); :}
		 %prec UMINUS
		;


UnionExpr    	::=    PathExpr:pe  
		{: RESULT =  new UnionExpr(pe); 
		:}		
   		|    PathExpr:pe UNION UnionExpr:une
   		{:
   		   RESULT = new UnionExpr(pe);
   		   RESULT.next = une;
		:}
		;

PathExpr     	::= 	LocationPath:lp  
		{:  RESULT = lp; :}
   		|    FilterExpr:fe   
		{:  RESULT = fe; :}
  		|    FilterExpr:fe SLASH RelativeLocationPath:rlp
		{:   parser.tempLPExpr = new LocationPathExpr();
		     parser.tempLPExpr.setStep(rlp);
		     RESULT = new PathExpr(fe, parser.tempLPExpr);	
		:}
		|    FilterExpr:fe DSLASH RelativeLocationPath:rlp
		{:   parser.tempStep = new Step();

		     parser.tempStep.setAxisType(AxisType.DESCENDANT_OR_SELF);
		     parser.tempNt = new NodeTest();
		     parser.tempNt.setTestType(NodeTest.NODE);

		     parser.tempStep.setNodeTest(parser.tempNt);

		     parser.tempStep.setNextStep(rlp);
		     rlp.setPrevStep(parser.tempStep);
		     
		     /*parser.tempStep2 = new Step();
		     parser.tempNt = new NodeTest();
		     parser.tempStep2.setAxisType(AxisType.SELF);
		     parser.tempNt.setTestType(NodeTest.NODE);

		     parser.tempStep2.setNodeTest(parser.tempNt);
	
  		     parser.tempStep2.setNextStep(parser.tempStep);
		     parser.tempStep.setPrevStep(parser.tempStep2);*/

		     parser.tempLPExpr = new LocationPathExpr();
		     parser.tempLPExpr.setStep(parser.tempStep);
		     RESULT = new PathExpr(fe, parser.tempLPExpr);
		:}
		;

FilterExpr   	::=  PrimaryExpr:pe  
		{: RESULT = pe; :}
  		|    FilterExpr:fe Predicate:p 
		{: RESULT = new FilterExpr(fe, p);:}
		;

PrimaryExpr     ::=  VariableReference: ve
	    {: RESULT = ve; :} 
		|    LP Expr:e RP 
		{: RESULT = e; :} 
		|    LITERAL:le  
		{: RESULT = new LiteralExpr(le); :} 
		|    NUMBER:ne  
		{: RESULT = new NumExpr(ne.doubleValue()); :} 
		|    FunctionCall:fc
		{: RESULT = fc; :} 		
		;

FunctionCall    ::=    FunctionName:fn LP ArgumentList:al RP
		{: RESULT = new FuncExpr(fn.i, al); :} 
		;

ArgumentList	::=    
		{: RESULT = null; :} 
	     	|    Argument:a
		{: RESULT = new Alist();
		   RESULT.e = a; :}  
		|    Argument:a COMMA ArgumentList:al
		{: RESULT = new Alist();
		   RESULT.e = a;
		   RESULT.next = al;
		:}   
		;

Argument   	::=    Expr:e
		{: RESULT = e; :}  
	   	; 


LocationPath    ::=    RelativeLocationPath:rlp	
		{: RESULT = new LocationPathExpr();
		   RESULT.setStep(rlp); 
		:}
		|    AbsoluteLocationPath:alp	
		{: RESULT = new LocationPathExpr();
		   RESULT.setPathType(LocationPathExpr.ABSOLUTE_PATH);
		   //System.out.println(" absolute ");
		   RESULT.setStep(alp);
		   //startStep = currentStep=null;
		:}
		;

AbsoluteLocationPath ::=  SLASH 			
		{:  RESULT = null; :}
		|    SLASH RelativeLocationPath:rlp		
		{:  RESULT = rlp;   :}
		|    AbbreviatedAbsoluteLocationPath:aalp	
		{:  RESULT = aalp;   :}
		;

RelativeLocationPath ::=  Step:s				
		{: RESULT = s; :}
		|    Step:s SLASH RelativeLocationPath:rlp  	
		{: //if (s == rlp) throw new XPathParseException("$1 = $3!!!!");
		   s.nextS = rlp;						
		   rlp.prevS = s;							 
		   RESULT= s;
		:}
		|    AbbreviatedRelativeLocationPath:arlp 	
		{: RESULT = arlp;	:}
		;

Step		::= AxisSpecifier:as nodetest:nt PredicateList:pl 
		{: RESULT = new Step();
      	   RESULT.setAxisType(as.i);
      	   if ( (as.i== AxisType.ATTRIBUTE
	        	 || as.i == AxisType.DESCENDANT
      	         || as.i == AxisType.PRECEDING
      	         || as.i == AxisType.FOLLOWING
      	         || as.i == AxisType.FOLLOWING_SIBLING
      	         || as.i == AxisType.PRECEDING_SIBLING) && 
      	        (nt.testType>1)){
      	          Yylex scanner = (Yylex)parser.getScanner();
      	          throw new XPathParseExceptionHuge(as.getAxisString()+" axis can't operate on"+
      	                 " node(), comment(), pi(), or text()",
      	         	scanner.getOffset());
      	         	}
      	         	
      	   RESULT.setNodeTest(nt);
		   RESULT.setPredicate(pl);
		   	
		   //System.out.println(" Step 3 ");
		   
		:}
      		|    AbbreviatedStep:abs			 
		{: RESULT = abs; 
		   //System.out.println(" step 4"); 
		:}
		;


nodetest	::=    NAME:n 
		{: RESULT = new NodeTest();
	 	   RESULT.setTestType(NodeTest.NAMETEST);
		   RESULT.setNodeName(n.qname);
		   if (n.localname!=null){
		   	 RESULT.setNodeNameNS(n.prefix,n.localname);
		   	 if (parser.nsHash==null || parser.nsHash.get(n.prefix) ==null)
		   	    throw new XPathParseExceptionHuge("No URL found for prefix:"+n.prefix);
		   	 RESULT.URL = (String) parser.nsHash.get(n.prefix);
		   }
		:}
		|    NTEST:n2				
		{: RESULT = new NodeTest();
		  RESULT.setTestType(n2.i);				
		:}
		;

PredicateList	::=
		{: RESULT = null;
	      	:} 
	     	|    Predicate:p PredicateList:pl  		
		{:  p.nextP = pl;
		    RESULT = p;
		:}	
		;

AxisSpecifier	::=    AXISNAME:an				
		{:  RESULT = an; :}
	      	|    AbbreviatedAxisSpecifier:aas		
		{:  RESULT = aas; :}
	      	;

AbbreviatedAxisSpecifier  ::=				
		{: RESULT = new AxisType(); RESULT.i = AxisType.CHILD; :}
		| AT				
	 	{: RESULT = new AxisType(); RESULT.i = AxisType.ATTRIBUTE; :}
		;

AbbreviatedStep	::=    DOT	
		{:  RESULT = new Step();
		    parser.tempNt = new NodeTest();
  		    parser.tempNt.setTestType(NodeTest.NODE);
		    RESULT.setAxisType(AxisType.SELF);
		    RESULT.setNodeTest(parser.tempNt);				 
		:}

		|    DDOT	
		{:  RESULT = new Step();
		    parser.tempNt = new NodeTest();
  		    parser.tempNt.setTestType(NodeTest.NODE);
		    RESULT.setAxisType(AxisType.PARENT);
		    RESULT.setNodeTest(parser.tempNt);
		    // System.out.println("result  " + RESULT.toString());
		:}
		;

AbbreviatedAbsoluteLocationPath ::= DSLASH RelativeLocationPath:rlp	
		{:
		  RESULT = new Step();
		  RESULT.setAxisType(AxisType.DESCENDANT_OR_SELF);
		  parser.tempNt = new NodeTest();
		  parser.tempNt.setTestType(NodeTest.NODE);
		  RESULT.setNodeTest(parser.tempNt);
		  RESULT.setNextStep(rlp);
		  rlp.setPrevStep(RESULT);
		:}
		;

AbbreviatedRelativeLocationPath ::=   Step:s DSLASH RelativeLocationPath:rlp
		{: RESULT = new Step();
		  RESULT.setAxisType(AxisType.DESCENDANT_OR_SELF);
		  parser.tempNt = new NodeTest();
					  
		  parser.tempNt.setTestType(NodeTest.NODE);
		  RESULT.setNodeTest(parser.tempNt);
		  s.setNextStep(RESULT);
		  RESULT.setPrevStep(s);
		  RESULT.setNextStep(rlp);
		  rlp.setPrevStep(RESULT);
		  RESULT = s;
		:}
		;


Predicate 	::=    LB Expr:e RB 
		{:	RESULT = new Predicate();
   		        RESULT.expr= e; 
		:}   
	   	;


VariableReference ::= DOLLAR NAME:n	{:
            if (parser.symbolHash.get(n.qname)==null)
		      throw new XPathParseExceptionHuge("variableExpression not declared ==> $"+n.qname);
			RESULT = new VariableExpr(n.qname, (Expr)parser.symbolHash.get(n.qname));	
			:}
		  ;

FunctionName 	::=  FNAME :fn
		{: RESULT = fn; :} 
	        ;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy