xpath.jj

来自「JXPath」· JJ 代码 · 共 1,218 行 · 第 1/3 页

JJ
1,218
字号
{String nc1, nc2 = null;}
{
    nc1 = NCName() ( ":" nc2 = NCName() )?
    {
        if (nc2 == null){
            return compiler.qname(null, nc1);
        }
        else {
            return compiler.qname(nc1, nc2);
        }
    }
}

Object QName_Without_CoreFunctions() :
{
    String nc1, nc2 = null;
}
{
    (
            LOOKAHEAD(NCName() ":") nc1 = NCName() ":" nc2 = NCName()
        |
            nc1 = NCName_Without_CoreFunctions()
    )
    {
        if (nc2 == null){
            return compiler.qname(null, nc1);
        }
        else {
            return compiler.qname(nc1, nc2);
        }
    }
}

Object parseExpression() :
{
    Object ex;
}
{
    ex = Expression()
    <EOF>
    {
        return ex;
    }
}

/* ################################################################################### */
/* XSLT Patterns (http://www.w3.org/1999/08/WD-xslt-19990813)                          */
/* ################################################################################### */

/* [XSLT1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern  */

//void Pattern() :
//{}
//{
//        LocationPathPattern() ( <UNION> LocationPathPattern() )* <EOF>
//}
//
//
///* [XSLT2] LocationPathPattern ::=
//   '/' RelativePathPattern? | IdKeyPattern (('/' | '//' RelativePathPattern)? | '//'? RelativePathPattern
//*/
//
//void LocationPathPattern() :
//{}
//{
//        <SLASH> ( RelativePathPattern() )?
//    |    (
//        LOOKAHEAD(IdKeyPattern())
//            IdKeyPattern() ( ( <SLASH> | <SLASHSLASH>) RelativePathPattern() )?
//        |    ( <SLASHSLASH> )? RelativePathPattern()
//        )
//}
//
//
//
///* [XSLT3] IdKeyPattern    ::=    'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')'  */
//
//void IdKeyPattern() :
//{}
//{
//        <ID> "(" <Literal> ")"
//    |    <KEY>  "(" <Literal> "," <Literal> ")"
//}
//
//
///* [XSLT4] RelativePathPattern    ::=    StepPattern | RelativePathPattern '/' StepPattern
//                           | RelativePathPattern '//' StepPattern
//*/
//void RelativePathPattern() :
//{}
//{
//        StepPattern() ( ( <SLASH>| <SLASHSLASH> ) StepPattern()    )*
//}
//
//
///* [XSLT5]    StepPattern    ::=    AbbreviatedAxisSpecifier NodeTest Predicate*   */
//void StepPattern() :
//{}
//{
//        AbbreviatedAxisSpecifier() NodeTest() (Predicate())*
//}



// See XPath Syntax (http://www.w3.org/TR/xpath )


//void XPath() :
//{}
//{
//    LocationPath()
//    <EOF>
//}


/* [1] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath  */
Object LocationPath() :
{Object ex = null;}
{
    (
        ex = RelativeLocationPath()
    |   ex = AbsoluteLocationPath()
    )
    {
        return ex;
    }
}

/* [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? | AbbreviatedAbsoluteLocationPath  */
/* [10]    AbbreviatedAbsoluteLocationPath    ::=    '//' RelativeLocationPath  */

Object AbsoluteLocationPath() :
{
    ArrayList steps = new ArrayList();
}
{
    (
        LOOKAHEAD(LocationStep(steps)) (LocationStep(steps) ( LocationStep(steps) )* )
      | <SLASH>
    )
    {
        return compiler.locationPath(true, steps.toArray());
    }
}

/* [3] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath */

Object RelativeLocationPath() :
{
    ArrayList steps = new ArrayList();
}
{
    (
        NodeTest(steps) ( LocationStep(steps) )*
    )
    {
        return compiler.locationPath(false, steps.toArray());
    }
}

/* [3] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath */
/* [11]    AbbreviatedRelativeLocationPath    ::=    RelativeLocationPath '//' Step  */


/*--------------------*/
/* 2.1 Location Steps */
/*--------------------*/

/* [4] Step ::= AxisSpecifier NodeTest Predicate*   | AbbreviatedStep  */

void LocationStep(ArrayList steps) :
{
    Object t;
    Object s;
}
{
    (
        <SLASH>
    |   <SLASHSLASH>
        {
            // Abbreviated step: descendant-or-self::node()
            t = compiler.nodeTypeTest(Compiler.NODE_TYPE_NODE);
            steps.add(compiler.step(Compiler.AXIS_DESCENDANT_OR_SELF, t, null));
        }
    )
    NodeTest(steps)
}

/* [7] NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')' */

void NodeTest(ArrayList steps) :
{
    int axis;
    int type = -1;
    String instruction = null;
    Object name = null;
    Object s;
    Object p;
    ArrayList ps = new ArrayList();
}
{
    (
        (
            axis = AxisSpecifier()
            (
                LOOKAHEAD(NodeType() "(" ")") type = NodeType() "(" ")"
            |   LOOKAHEAD(<PI>) <PI> "(" <Literal> {
                    instruction = unescape(token.image.substring(1, token.image.length() - 1));
                    } ")"
            |   name = WildcardName()
            )
            |   "."
                {
                    axis = Compiler.AXIS_SELF;
                    type = Compiler.NODE_TYPE_NODE;
                }
            | ".."
                {
                    axis = Compiler.AXIS_PARENT;
                    type = Compiler.NODE_TYPE_NODE;
                }
        )
        (
            p = Predicate()
            {
                ps.add(p);
            }
        )*
    )
    {
        if (name != null){
            s = compiler.nodeNameTest(name);
        }
        else if (instruction != null){
            s = compiler.processingInstructionTest(instruction);
        }
        else {
            s = compiler.nodeTypeTest(type);
        }
        steps.add(compiler.step(axis, s, ps.toArray()));
    }
}

/* [5] AxisSpecifier ::=    AxisName '::' | AbbreviatedAxisSpecifier  */

int AxisSpecifier() :
{
    int axis;
}
{
    (
        axis = AxisName()
    |   axis = AbbreviatedAxisSpecifier()
    )
    {
        return axis;
    }
}

/*----------*/
/* 2.2 Axes */
/*----------*/

/* [6] AxisName ::= 'ancestor' | 'ancestor-or-self' | 'attribute'  | 'child' | 'descendant'
                    | 'descendant-or-self' | 'following' | 'following-sibling' | 'namespace'
                       | 'parent' | 'preceding' | 'preceding-sibling' | 'self'
*/

int AxisName() :
{
    int axis = 0;
}
{
    (
            <AXIS_SELF>                 { axis = Compiler.AXIS_SELF; }
        |   <AXIS_CHILD>                { axis = Compiler.AXIS_CHILD; }
        |   <AXIS_PARENT>               { axis = Compiler.AXIS_PARENT; }
        |   <AXIS_ANCESTOR>             { axis = Compiler.AXIS_ANCESTOR; }
        |   <AXIS_ATTRIBUTE>            { axis = Compiler.AXIS_ATTRIBUTE; }
        |   <AXIS_NAMESPACE>            { axis = Compiler.AXIS_NAMESPACE; }
        |   <AXIS_PRECEDING>            { axis = Compiler.AXIS_PRECEDING; }
        |   <AXIS_FOLLOWING>            { axis = Compiler.AXIS_FOLLOWING; }
        |   <AXIS_DESCENDANT>           { axis = Compiler.AXIS_DESCENDANT; }
        |   <AXIS_ANCESTOR_OR_SELF>     { axis = Compiler.AXIS_ANCESTOR_OR_SELF; }
        |   <AXIS_FOLLOWING_SIBLING>    { axis = Compiler.AXIS_FOLLOWING_SIBLING; }
        |   <AXIS_PRECEDING_SIBLING>    { axis = Compiler.AXIS_PRECEDING_SIBLING; }
        |   <AXIS_DESCENDANT_OR_SELF>   { axis = Compiler.AXIS_DESCENDANT_OR_SELF; }
    )
    {
        return axis;
    }
}

/*----------------*/
/* 2.3 Node Tests */
/*----------------*/

/*----------------*/
/* 2.4 Predicates */
/*----------------*/

/* [8] Predicate ::= '[' PredicateExpr ']'  */
/* [9] PredicateExpr ::=  Expr  */

Object Predicate() :
{
    Object ex;
}
{
    "[" ex = Expression() "]"
    {
        return ex;
    }
}

/* [12]    AbbreviatedStep    ::=    '.'  | '..'  */

/* [13]    AbbreviatedAxisSpecifier    ::=    '@'? */
int AbbreviatedAxisSpecifier() :
{
    int axis = Compiler.AXIS_CHILD;
}
{
    ( "@" {axis = Compiler.AXIS_ATTRIBUTE; } )?

    {
        return axis;
    }
}

/*---------------*/
/* 3 Expressions */
/*---------------*/

/*------------*/
/* 3.1 Basics */
/*------------*/

/*
The effect of the grammar is that the order of precedence is (lowest precedence first):
    or
    and
    =, !=
    <=, <, >=, >
and all operators are left associative.
For example, 3 > 2 > 1 is equivalent to (3 > 2) > 1, which evaluates to false.
*/

/* [14] Expr ::= OrExpr */
Object Expression() :
{Object ex;}
{
    ex = OrExpr()
    {
        return ex;
    }
}

/* [15] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall */

Object PrimaryExpr() :
{
    Object ex = null;
}
{
    (
         ex = VariableReference()
    |    "(" ex = Expression() ")"
    |    <Literal>  { ex = compiler.literal(unescape(token.image.substring(1, token.image.length() - 1))); }
    |    <Number>   { ex = compiler.number(token.image); }
    |    LOOKAHEAD(CoreFunctionName() "(") ex = CoreFunctionCall()
    |    ex = FunctionCall()
    )
    {
        return ex;
    }
}

/*--------------------*/
/* 3.2 Function Calls */
/*--------------------*/

/* [16]    FunctionCall    ::=    FunctionName '(' ( Argument ( ',' Argument)*)? ')'  */
Object FunctionCall() :
{
    Object name;
    ArrayList args;
}
{
    name = FunctionName() args = ArgumentList()
    {
        if (args == null){
            return compiler.function(name, null);
        }
        else {
            return compiler.function(name, args.toArray());
        }
    }
}

Object CoreFunctionCall() :
{
    int code = 0;
    ArrayList args;
}
{

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?