Ejemplo n.º 1
0
        //please kill me
        private static ASTNodePathStep parseStep(ASTNodeAbstractExpr node)
        {
            ASTNodePathStep step = new ASTNodePathStep();

            if (node.content.Count == 1 && node.getTokenType(0) == Token.DOT)
            {
                step.axisType     = ASTNodePathStep.AXIS_TYPE_NULL;
                step.nodeTestType = ASTNodePathStep.NODE_TEST_TYPE_ABBR_DOT;
            }
            else if (node.content.Count == 1 && node.getTokenType(0) == Token.DBL_DOT)
            {
                step.axisType     = ASTNodePathStep.AXIS_TYPE_NULL;
                step.nodeTestType = ASTNodePathStep.NODE_TEST_TYPE_ABBR_DBL_DOT;
            }
            else
            {
                int i = 0;
                if (node.content.Count > 0 && node.getTokenType(0) == Token.AT)
                {
                    step.axisType = ASTNodePathStep.AXIS_TYPE_ABBR;
                    i            += 1;
                }
                else if (node.content.Count > 1 && node.getTokenType(0) == Token.QNAME && node.getTokenType(1) == Token.DBL_COLON)
                {
                    int axisVal = ASTNodePathStep.validateAxisName(((XPathQName)node.getToken(0).val).ToString());
                    if (axisVal == -1)
                    {
                        throw new XPathSyntaxException();
                    }
                    step.axisType = ASTNodePathStep.AXIS_TYPE_EXPLICIT;
                    step.axisVal  = axisVal;
                    i            += 2;
                }
                else
                {
                    step.axisType = ASTNodePathStep.AXIS_TYPE_NULL;
                }

                if (node.content.Count > i && node.getTokenType(i) == Token.WILDCARD)
                {
                    step.nodeTestType = ASTNodePathStep.NODE_TEST_TYPE_WILDCARD;
                }
                else if (node.content.Count > i && node.getTokenType(i) == Token.NSWILDCARD)
                {
                    step.nodeTestType      = ASTNodePathStep.NODE_TEST_TYPE_NSWILDCARD;
                    step.nodeTestNamespace = (String)node.getToken(i).val;
                }
                else if (node.content.Count > i && node.getTokenType(i) == Token.QNAME)
                {
                    step.nodeTestType  = ASTNodePathStep.NODE_TEST_TYPE_QNAME;
                    step.nodeTestQName = (XPathQName)node.getToken(i).val;
                }
                else if (node.content.Count > i && node.content[i] is ASTNodeFunctionCall)
                {
                    if (!ASTNodePathStep.validateNodeTypeTest((ASTNodeFunctionCall)node.content[i]))
                    {
                        throw new XPathSyntaxException();
                    }
                    step.nodeTestType = ASTNodePathStep.NODE_TEST_TYPE_FUNC;
                    step.nodeTestFunc = (ASTNodeFunctionCall)node.content[i];
                }
                else
                {
                    throw new XPathSyntaxException();
                }
                i += 1;

                while (i < node.content.Count)
                {
                    if (node.content[i] is ASTNodePredicate)
                    {
                        step.predicates.Add(node.content[i]);
                    }
                    else
                    {
                        throw new XPathSyntaxException();
                    }
                    i++;
                }
            }

            return(step);
        }
Ejemplo n.º 2
0
        private static void parsePathExpr(ASTNode node)
        {
            if (node is ASTNodeAbstractExpr)
            {
                ASTNodeAbstractExpr absNode = (ASTNodeAbstractExpr)node;
                int[] pathOps = { Token.SLASH, Token.DBL_SLASH };
                ASTNodeAbstractExpr.Partition part = absNode.partition(pathOps, 0, absNode.content.Count);

                if (part.separators.Count == 0)
                {
                    //filter expression or standalone step
                    if (isStep(absNode))
                    {
                        ASTNodePathStep step = parseStep(absNode);
                        ASTNodeLocPath  path = new ASTNodeLocPath();
                        path.clauses.Add(step);
                        absNode.condense(path, 0, absNode.content.Count);
                    }
                    else
                    {
                        //filter expr
                        ASTNodeFilterExpr filt = parseFilterExp(absNode);
                        if (filt != null)
                        {
                            absNode.condense(filt, 0, absNode.content.Count);
                        }
                    }
                }
                else
                {
                    //path expression (but first clause may be filter expr)
                    ASTNodeLocPath path = new ASTNodeLocPath();
                    path.separators = part.separators;

                    if (part.separators.Count == 1 && absNode.content.Count == 1 && vectInt(part.separators, 0) == Token.SLASH)
                    {
                        //empty absolute path
                    }
                    else
                    {
                        for (int i = 0; i < part.pieces.Count; i++)
                        {
                            ASTNodeAbstractExpr x = (ASTNodeAbstractExpr)part.pieces[i];
                            if (isStep(x))
                            {
                                ASTNodePathStep step = parseStep(x);
                                path.clauses.Add(step);
                            }
                            else
                            {
                                if (i == 0)
                                {
                                    if (x.content.Count == 0)
                                    {
                                        //absolute path expr; first clause is null
                                        /* do nothing */
                                    }
                                    else
                                    {
                                        //filter expr
                                        ASTNodeFilterExpr filt = parseFilterExp(x);
                                        if (filt != null)
                                        {
                                            path.clauses.Add(filt);
                                        }
                                        else
                                        {
                                            path.clauses.Add(x);
                                        }
                                    }
                                }
                                else
                                {
                                    throw new XPathSyntaxException();
                                }
                            }
                        }
                    }
                    absNode.condense(path, 0, absNode.content.Count);
                }
            }

            for (IEnumerator e = node.getChildren().GetEnumerator(); e.MoveNext();)
            {
                parsePathExpr((ASTNode)e.Current);
            }
        }