Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.xpath;
import java_cup.runtime.*;
import com.ximpleware.*;
import com.ximpleware.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 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{
Yylex scanner = (Yylex)getScanner();
throw new XPathParseException("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);
//ee is a function expression such as name() or local-name()
//
:}
| EqualityExpr:ee NE RelationalExpr:re
{: RESULT = new BinaryExpr(ee, BinaryExpr.NE, re);
//ee is a function expression such as name() or local-name()
:}
;
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
{: if (ue.next==null)
RESULT = ue.e;
else
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);
FuncExpr tfe = new FuncExpr(fn.i, al);
if (!tfe.checkArgumentCount())
throw new XPathParseException(" argument input for function "+tfe.fname()+" incorrect");
RESULT = tfe;
:}
;
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);
RESULT.optimize();
:}
| AbsoluteLocationPath:alp
{: RESULT = new LocationPathExpr();
RESULT.setPathType(LocationPathExpr.ABSOLUTE_PATH);
//System.out.println(" absolute ");
RESULT.setStep(alp);
RESULT.optimize();
//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.setNextStep(rlp);
rlp.setPrevStep(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.NAMESPACE) &&
(nt.testType>1)){
Yylex scanner = (Yylex)parser.getScanner();
throw new XPathParseException(as.getAxisString()+" axis can't operate on"+
" 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 XPathParseException("No URL found for prefix:"+n.prefix);
RESULT.URL = (String) parser.nsHash.get(n.prefix);
}
:}
| NTEST:n2
{: RESULT = new NodeTest();
if (n2.i!=3)
RESULT.setTestType(n2.i);
else {
if (n2.arg ==null)
RESULT.setTestType(n2.i); //PI0
else {
RESULT.setTestType(4); //PI1
RESULT.setNodeName(n2.arg);
}
}
:}
;
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;
if (e.isFinal() && e.isNumerical()){
RESULT.d = e.evalNumber((VTDNav)null);
Yylex scanner = (Yylex)parser.getScanner();
if(RESULT.d<1)
throw new XPathParseException("invalid index number for predicate",
scanner.getOffset());
RESULT.type = Predicate.simple;
}
RESULT.requireContext = e.requireContext();
//e.markCacheable();
:}
;
VariableReference ::= DOLLAR NAME:n
{:
if (parser.symbolHash.get(n.qname)==null)
throw new XPathParseException("variableExpression not declared ==> $"+n.qname);
RESULT = new VariableExpr(n.qname, (Expr)parser.symbolHash.get(n.qname));
:}
;
FunctionName ::= FNAME :fn
{: RESULT = fn; :}
;