com.ximpleware.extended.xpath.parser.cup.notworking Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vtd-xml Show documentation
Show all versions of vtd-xml Show documentation
XimpleWare's VTD-XML is, far and away, the industry's most advanced and powerful XML processing model for SOA and Cloud Computing
/*
* Copyright (C) 2002-2011 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.xpath;
import java_cup.runtime.*;
import com.ximpleware.*;
//import com.ximpleware.xpath.*;
parser code {:
Step tempStep;
NodeTest tempNt;
LocationPathExpr tempLPExpr;
public parser (java.io.Reader input) {
super(new Yylex(input));
}
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 XPathParseException{
throw new XPathParseException("Syntax error during parsing: "+ message);
}
public void syntax_error(Symbol cur_token) {
}
public void unrecovered_syntax_error(Symbol cur_token) throws XPathParseException{
throw new XPathParseException("XPath Syntax error: "+cur_token);
}
:};
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 VariableReference;
non terminal FuncExpr FunctionCall;
non terminal FuncName FunctionName;
non terminal Expr Expr, OrExpr, EqualityExpr, AndExpr, RelationalExpr, AdditiveExpr;
non terminal Expr MultiplicativeExpr, UnaryExpr, UnionExpr, PathExpr, FilterExpr;
non terminal Alist ArgumentList;
non terminal Expr PrimaryExpr, FunctionCal, 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 PredicateList, Predicate;
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 = pe; :}
| UnionExpr:une UNION PathExpr:pe
{: RESULT = new BinaryExpr(une, BinaryExpr.UNION, pe);
//throw new XPathParseException("Union not yet supported");
:}
;
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
| 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);
RESULT.setNodeTest(nt);
RESULT.setPredicate(pl);
System.out.println(" Step 3 ");
:}
| AbbreviatedStep:as
{: RESULT = as; :}
;
nodetest ::= NAME:n
{: RESULT = new NodeTest();
RESULT.setTestType(NodeTest.NAMETEST);
RESULT.setNodeName(n.qname);
:}
| 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);
:}
;
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
;
FunctionName ::= FNAME :fn
{: RESULT = fn; :}
;