private XPathExprList ParseAbsolutePath()
        {
            XPathExprList path  = null;
            XPathToken    token = this.NextToken();

            if (token != null)
            {
                XPathTokenID tokenID = token.TokenID;
                if (tokenID == XPathTokenID.Slash)
                {
                    path = new XPathExprList();
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.Child, NodeQName.Empty, QueryNodeType.Root)));
                }
                else if (tokenID == XPathTokenID.DblSlash)
                {
                    path = new XPathExprList();
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.Child, NodeQName.Empty, QueryNodeType.Root)));
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.DescendantOrSelf, NodeQName.Empty, QueryNodeType.All)));
                }
                else
                {
                    this.PushToken(token);
                }
            }
            if (path != null)
            {
                this.ParseRelativePath(path);
            }
            return(path);
        }
Example #2
0
        XPathExprList ParseAbsolutePath()
        {
            XPathExprList path  = null;
            XPathToken    token = this.NextToken();

            if (null != token)
            {
                switch (token.TokenID)
                {
                default:
                    this.PushToken(token);
                    break;

                case XPathTokenID.Slash:
                    path = new XPathExprList();
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.Child, NodeQName.Empty, QueryNodeType.Root)));
                    break;

                case XPathTokenID.DblSlash:
                    // '//' is special. If found at the start of an absolute path, it implies that the descendant-or-self axis
                    // is applied to the ROOT
                    path = new XPathExprList();
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.Child, NodeQName.Empty, QueryNodeType.Root)));
                    path.Add(new XPathStepExpr(new NodeSelectCriteria(QueryAxisType.DescendantOrSelf, NodeQName.Empty, QueryNodeType.All)));
                    break;
                }
            }

            if (null != path)
            {
                this.ParseRelativePath(path);
            }

            return(path);
        }
        private XPathExpr ParseFunctionExpression()
        {
            XPathExpr  expr;
            XPathToken token = this.NextToken(XPathTokenID.Function);

            if (token == null)
            {
                return(null);
            }
            NodeQName name = this.QualifyName(token.Prefix, token.Name);

            this.NextToken(XPathTokenID.LParen, QueryCompileError.InvalidFunction);
            XPathExprList args = new XPathExprList();

            while ((expr = this.ParseExpression()) != null)
            {
                args.Add(expr);
                if (this.NextToken(XPathTokenID.Comma) == null)
                {
                    break;
                }
            }
            XPathExpr expr2 = null;

            if (this.functionLibraries != null)
            {
                QueryFunction function = null;
                for (int i = 0; i < this.functionLibraries.Length; i++)
                {
                    function = this.functionLibraries[i].Bind(name.Name, name.Namespace, args);
                    if (function != null)
                    {
                        expr2 = new XPathFunctionExpr(function, args);
                        break;
                    }
                }
            }
            if ((expr2 == null) && (this.context != null))
            {
                XPathResultType[] argTypes = new XPathResultType[args.Count];
                for (int j = 0; j < args.Count; j++)
                {
                    argTypes[j] = XPathXsltFunctionExpr.ConvertTypeToXslt(args[j].ReturnType);
                }
                string prefix = this.context.LookupPrefix(name.Namespace);
                IXsltContextFunction function2 = this.context.ResolveFunction(prefix, name.Name, argTypes);
                if (function2 != null)
                {
                    expr2 = new XPathXsltFunctionExpr(this.context, function2, args);
                }
            }
            if (expr2 == null)
            {
                this.ThrowError(QueryCompileError.UnsupportedFunction);
            }
            this.NextToken(XPathTokenID.RParen, QueryCompileError.InvalidFunction);
            return(expr2);
        }
        private XPathExpr ParseNumberExpression()
        {
            XPathToken token = this.NextTokenClass(XPathTokenID.Number);

            if (token != null)
            {
                return(new XPathNumberExpr(token.Number));
            }
            return(null);
        }
        private XPathToken NextToken(XPathTokenID id, QueryCompileError error)
        {
            XPathToken token = this.NextToken(id);

            if (token == null)
            {
                this.ThrowError(error);
            }
            return(token);
        }
        private XPathExpr ParseLiteralExpression()
        {
            XPathToken token = this.NextToken(XPathTokenID.Literal);

            if (token != null)
            {
                return(new XPathStringExpr(token.Name));
            }
            return(null);
        }
Example #7
0
        XPathExpr ParseRelationalExpression()
        {
            XPathExpr leftExpr = this.ParseAdditiveExpression();

            if (null != leftExpr)
            {
                RelationOperator op;

                do
                {
                    op = RelationOperator.None;

                    XPathToken token = this.NextToken();

                    if (null != token)
                    {
                        switch (token.TokenID)
                        {
                        default:
                            this.PushToken(token);
                            break;

                        case XPathTokenID.Lt:
                            op = RelationOperator.Lt;
                            break;

                        case XPathTokenID.Lte:
                            op = RelationOperator.Le;
                            break;

                        case XPathTokenID.Gt:
                            op = RelationOperator.Gt;
                            break;

                        case XPathTokenID.Gte:
                            op = RelationOperator.Ge;
                            break;
                        }
                        if (RelationOperator.None != op)
                        {
                            XPathExpr rightExpr = this.ParseAdditiveExpression();

                            if (null == rightExpr)
                            {
                                this.ThrowError(QueryCompileError.InvalidExpression);
                            }

                            leftExpr = new XPathRelationExpr(op, leftExpr, rightExpr);
                        }
                    }
                } while (RelationOperator.None != op);
            }

            return(leftExpr);
        }
Example #8
0
 internal XPathLexer(string xpath, bool resolveKeywords)
 {
     this.resolveKeywords = resolveKeywords;
     this.xpath           = string.Copy(xpath);
     this.xpathLength     = this.xpath.Length;
     this.tokenStart      = 0;
     this.currChar        = 0;
     this.ch         = '\0';
     this.previousID = XPathTokenID.Unknown;
     this.token      = new XPathToken();
     this.ConsumeWhitespace();
 }
 internal XPathLexer(string xpath, bool resolveKeywords)
 {
     this.resolveKeywords = resolveKeywords;
     this.xpath = string.Copy(xpath);
     this.xpathLength = this.xpath.Length;
     this.tokenStart = 0;
     this.currChar = 0;
     this.ch = '\0';
     this.previousID = XPathTokenID.Unknown;
     this.token = new XPathToken();
     this.ConsumeWhitespace();
 }
 private XPathToken NextToken(XPathTokenID id)
 {
     XPathToken token = this.NextToken();
     if (token != null)
     {
         if (id == token.TokenID)
         {
             return token;
         }
         this.readToken = token;
     }
     return null;
 }
 private XPathToken NextTokenClass(XPathTokenID tokenClass)
 {
     XPathToken token = this.NextToken();
     if (token != null)
     {
         if ((token.TokenID & tokenClass) != XPathTokenID.Unknown)
         {
             return token;
         }
         this.readToken = token;
     }
     return null;
 }
Example #12
0
        XPathExpr ParseMultiplicativeExpression()
        {
            XPathExpr leftExpr = this.ParseUnaryExpression();

            if (null != leftExpr)
            {
                MathOperator op;

                do
                {
                    op = MathOperator.None;

                    XPathToken token = this.NextToken();

                    if (null != token)
                    {
                        switch (token.TokenID)
                        {
                        default:
                            this.PushToken(token);
                            break;

                        case XPathTokenID.Multiply:
                            op = MathOperator.Multiply;
                            break;

                        case XPathTokenID.Div:
                            op = MathOperator.Div;
                            break;

                        case XPathTokenID.Mod:
                            op = MathOperator.Mod;
                            break;
                        }
                        if (MathOperator.None != op)
                        {
                            XPathExpr rightExpr = this.ParseUnaryExpression();

                            if (null == rightExpr)
                            {
                                this.ThrowError(QueryCompileError.InvalidExpression);
                            }

                            leftExpr = new XPathMathExpr(op, leftExpr, rightExpr);
                        }
                    }
                } while (MathOperator.None != op);
            }

            return(leftExpr);
        }
        private XPathToken NextToken(XPathTokenID id)
        {
            XPathToken token = this.NextToken();

            if (token != null)
            {
                if (id == token.TokenID)
                {
                    return(token);
                }
                this.readToken = token;
            }
            return(null);
        }
        private XPathToken NextTokenClass(XPathTokenID tokenClass)
        {
            XPathToken token = this.NextToken();

            if (token != null)
            {
                if ((token.TokenID & tokenClass) != XPathTokenID.Unknown)
                {
                    return(token);
                }
                this.readToken = token;
            }
            return(null);
        }
        private XPathExpr ParseRelationalExpression()
        {
            XPathExpr left = this.ParseAdditiveExpression();

            if (left != null)
            {
                RelationOperator none;
                do
                {
                    none = RelationOperator.None;
                    XPathToken token = this.NextToken();
                    if (token != null)
                    {
                        switch (token.TokenID)
                        {
                        case XPathTokenID.Gt:
                            none = RelationOperator.Gt;
                            break;

                        case XPathTokenID.Gte:
                            none = RelationOperator.Ge;
                            break;

                        case XPathTokenID.Lt:
                            none = RelationOperator.Lt;
                            break;

                        case XPathTokenID.Lte:
                            none = RelationOperator.Le;
                            break;

                        default:
                            this.PushToken(token);
                            break;
                        }
                        if (none != RelationOperator.None)
                        {
                            XPathExpr right = this.ParseAdditiveExpression();
                            if (right == null)
                            {
                                this.ThrowError(QueryCompileError.InvalidExpression);
                            }
                            left = new XPathRelationExpr(none, left, right);
                        }
                    }
                }while (none != RelationOperator.None);
            }
            return(left);
        }
Example #16
0
        XPathToken NextTokenClass(XPathTokenID tokenClass)
        {
            XPathToken token = this.NextToken();

            if (null != token)
            {
                if (0 != (token.TokenID & tokenClass))
                {
                    return(token);
                }

                this.readToken = token;
            }

            return(null);
        }
        XPathToken NextToken(XPathTokenID id)
        {
            XPathToken token = this.NextToken();

            if (null != token)
            {
                if (id == token.TokenID)
                {
                    return token;
                }

                this.readToken = token;
            }

            return null;
        }
 private XPathToken NextToken()
 {
     if (this.readToken == null)
     {
         while (this.lexer.MoveNext())
         {
             if (XPathTokenID.Whitespace != this.lexer.Token.TokenID)
             {
                 return this.lexer.Token;
             }
         }
         return null;
     }
     XPathToken readToken = this.readToken;
     this.readToken = null;
     return readToken;
 }
        private XPathToken NextToken()
        {
            if (this.readToken == null)
            {
                while (this.lexer.MoveNext())
                {
                    if (XPathTokenID.Whitespace != this.lexer.Token.TokenID)
                    {
                        return(this.lexer.Token);
                    }
                }
                return(null);
            }
            XPathToken readToken = this.readToken;

            this.readToken = null;
            return(readToken);
        }
        private XPathExpr ParseMultiplicativeExpression()
        {
            XPathExpr left = this.ParseUnaryExpression();

            if (left != null)
            {
                MathOperator none;
                do
                {
                    none = MathOperator.None;
                    XPathToken token = this.NextToken();
                    if (token != null)
                    {
                        XPathTokenID tokenID = token.TokenID;
                        if (tokenID == XPathTokenID.Multiply)
                        {
                            none = MathOperator.Multiply;
                        }
                        else if (tokenID == XPathTokenID.Mod)
                        {
                            none = MathOperator.Mod;
                        }
                        else if (tokenID == XPathTokenID.Div)
                        {
                            none = MathOperator.Div;
                        }
                        else
                        {
                            this.PushToken(token);
                        }
                        if (none != MathOperator.None)
                        {
                            XPathExpr right = this.ParseUnaryExpression();
                            if (right == null)
                            {
                                this.ThrowError(QueryCompileError.InvalidExpression);
                            }
                            left = new XPathMathExpr(none, left, right);
                        }
                    }
                }while (none != MathOperator.None);
            }
            return(left);
        }
Example #21
0
        internal XPathExpr Parse()
        {
            XPathExpr expr = this.ParseExpression();

            if (null == expr)
            {
                this.ThrowError(QueryCompileError.InvalidExpression);
            }

            // If we stopped before the entire xpath was lexed, we hit something we could not tokenize
            XPathToken lastToken = this.NextToken();

            if (null != lastToken)
            {
                this.ThrowError(QueryCompileError.UnexpectedToken);
            }

            return(expr);
        }
        XPathToken NextToken()
        {
            if (null != this.readToken)
            {
                XPathToken nextToken = this.readToken;

                this.readToken = null;
                return nextToken;
            }

            while (this.lexer.MoveNext())
            {
                if (XPathTokenID.Whitespace != this.lexer.Token.TokenID)
                {
                    return this.lexer.Token;
                }
            }

            return null;
        }
        internal XPathExpr ParseVariableExpression()
        {
            XPathExpr expr = null;

            if (this.context != null)
            {
                XPathToken token = this.NextToken(XPathTokenID.Variable);
                if (token != null)
                {
                    NodeQName            name     = this.QualifyName(token.Prefix, token.Name);
                    string               prefix   = this.context.LookupPrefix(name.Namespace);
                    IXsltContextVariable variable = this.context.ResolveVariable(prefix, name.Name);
                    if (variable != null)
                    {
                        expr = new XPathXsltVariableExpr(this.context, variable);
                    }
                }
            }
            return(expr);
        }
Example #24
0
        XPathToken NextToken()
        {
            if (null != this.readToken)
            {
                XPathToken nextToken = this.readToken;

                this.readToken = null;
                return(nextToken);
            }

            while (this.lexer.MoveNext())
            {
                if (XPathTokenID.Whitespace != this.lexer.Token.TokenID)
                {
                    return(this.lexer.Token);
                }
            }

            return(null);
        }
        private XPathExpr ParseAdditiveExpression()
        {
            XPathExpr left = this.ParseMultiplicativeExpression();

            if (left != null)
            {
                MathOperator none;
                do
                {
                    none = MathOperator.None;
                    XPathToken token = this.NextToken();
                    if (token != null)
                    {
                        switch (token.TokenID)
                        {
                        case XPathTokenID.Plus:
                            none = MathOperator.Plus;
                            break;

                        case XPathTokenID.Minus:
                            none = MathOperator.Minus;
                            break;

                        default:
                            this.PushToken(token);
                            break;
                        }
                        if (none != MathOperator.None)
                        {
                            XPathExpr right = this.ParseMultiplicativeExpression();
                            if (right == null)
                            {
                                this.ThrowError(QueryCompileError.InvalidExpression);
                            }
                            left = new XPathMathExpr(none, left, right);
                        }
                    }
                }while (none != MathOperator.None);
            }
            return(left);
        }
        private QueryAxisType ParseAxisSpecifier()
        {
            if (this.NextToken(XPathTokenID.AtSign) != null)
            {
                return(QueryAxisType.Attribute);
            }
            QueryAxisType none  = QueryAxisType.None;
            XPathToken    token = this.NextTokenClass(XPathTokenID.Axis);

            if (token != null)
            {
                switch (token.TokenID)
                {
                case XPathTokenID.Attribute:
                    none = QueryAxisType.Attribute;
                    break;

                case XPathTokenID.Child:
                    none = QueryAxisType.Child;
                    break;

                case XPathTokenID.Descendant:
                    none = QueryAxisType.Descendant;
                    break;

                case XPathTokenID.DescendantOrSelf:
                    none = QueryAxisType.DescendantOrSelf;
                    break;

                case XPathTokenID.Self:
                    none = QueryAxisType.Self;
                    break;

                default:
                    this.ThrowError(QueryCompileError.UnsupportedAxis);
                    break;
                }
                this.NextToken(XPathTokenID.DblColon, QueryCompileError.InvalidAxisSpecifier);
            }
            return(none);
        }
        internal XPathLexer(string xpath, bool resolveKeywords)
        {
            this.resolveKeywords = resolveKeywords;

            // Hold on to a copy of the string so it can't be changed out from under us
            this.xpath       = string.Copy(xpath);
            this.xpathLength = this.xpath.Length;

            // Start at the beginning
            this.tokenStart = 0;
            this.currChar   = 0;

            this.ch         = char.MinValue;
            this.previousID = XPathTokenID.Unknown;

            // We will not create new tokens, we will simply change the old one.
            // This will be the only XPathToken instance created by the lexer
            // The 'next token' data can be more quickly communicated to the parser if they both hold a reference to the data.
            this.token = new XPathToken();

            // Strip leading whitespace
            ConsumeWhitespace();
        }
 private void PushToken(XPathToken token)
 {
     this.readToken = token;
 }
 private void PushToken(XPathToken token)
 {
     this.readToken = token;
 }
Example #30
0
        XPathExpr ParseFunctionExpression()
        {
            XPathToken functionToken = this.NextToken(XPathTokenID.Function);

            if (null == functionToken)
            {
                return(null);
            }

            NodeQName functionName = this.QualifyName(functionToken.Prefix, functionToken.Name);

            this.NextToken(XPathTokenID.LParen, QueryCompileError.InvalidFunction);

            XPathExprList args = new XPathExprList();

            // Read in arguments
            XPathExpr arg;

            while (null != (arg = this.ParseExpression()))
            {
                args.Add(arg);
                if (null == this.NextToken(XPathTokenID.Comma))
                {
                    break;
                }
            }

            // Bind to the function
            // Try each library until we can bind the function
            XPathExpr functionImpl = null;

            if (null != this.functionLibraries)
            {
                QueryFunction fun = null;
                for (int i = 0; i < this.functionLibraries.Length; ++i)
                {
                    if (null != (fun = this.functionLibraries[i].Bind(functionName.Name, functionName.Namespace, args)))
                    {
                        functionImpl = new XPathFunctionExpr(fun, args);
                        break;
                    }
                }
            }

            // Try to bind using the XsltContext
            if (null == functionImpl && this.context != null)
            {
                XPathResultType[] argTypes = new XPathResultType[args.Count];
                for (int i = 0; i < args.Count; ++i)
                {
                    argTypes[i] = XPathXsltFunctionExpr.ConvertTypeToXslt(args[i].ReturnType);
                }
                string prefix = this.context.LookupPrefix(functionName.Namespace);
                IXsltContextFunction xsltFun = this.context.ResolveFunction(prefix, functionName.Name, argTypes);
                if (xsltFun != null)
                {
                    functionImpl = new XPathXsltFunctionExpr(this.context, xsltFun, args);
                }
            }

            if (null == functionImpl)
            {
                this.ThrowError(QueryCompileError.UnsupportedFunction);
            }

            this.NextToken(XPathTokenID.RParen, QueryCompileError.InvalidFunction);
            return(functionImpl);
        }
        private NodeSelectCriteria ParseNodeTest(QueryAxisType axisType)
        {
            QueryNodeType principalNodeType;
            QueryAxis     axis  = QueryDataModel.GetAxis(axisType);
            NodeQName     empty = NodeQName.Empty;
            XPathToken    token = this.NextTokenClass(XPathTokenID.NameTest);

            if (token != null)
            {
                switch (token.TokenID)
                {
                case XPathTokenID.Wildcard:
                    empty = new NodeQName(QueryDataModel.Wildcard, QueryDataModel.Wildcard);
                    goto Label_0085;

                case XPathTokenID.NameWildcard:
                    empty = this.QualifyName(token.Prefix, QueryDataModel.Wildcard);
                    goto Label_0085;

                case XPathTokenID.NameTest:
                    empty = this.QualifyName(token.Prefix, token.Name);
                    goto Label_0085;
                }
                this.ThrowError(QueryCompileError.UnexpectedToken);
            }
Label_0085:
            principalNodeType = QueryNodeType.Any;
            if (!empty.IsEmpty)
            {
                principalNodeType = axis.PrincipalNodeType;
            }
            else
            {
                token = this.NextTokenClass(XPathTokenID.NodeType);
                if (token == null)
                {
                    return(null);
                }
                switch (token.TokenID)
                {
                case XPathTokenID.Comment:
                    principalNodeType = QueryNodeType.Comment;
                    break;

                case XPathTokenID.Text:
                    principalNodeType = QueryNodeType.Text;
                    break;

                case XPathTokenID.Processing:
                    principalNodeType = QueryNodeType.Processing;
                    break;

                case XPathTokenID.Node:
                    principalNodeType = QueryNodeType.All;
                    break;

                default:
                    this.ThrowError(QueryCompileError.UnsupportedNodeTest);
                    break;
                }
                if (((byte)(axis.ValidNodeTypes & principalNodeType)) == 0)
                {
                    this.ThrowError(QueryCompileError.InvalidNodeType);
                }
                this.NextToken(XPathTokenID.LParen, QueryCompileError.InvalidNodeTest);
                this.NextToken(XPathTokenID.RParen, QueryCompileError.InvalidNodeTest);
            }
            return(new NodeSelectCriteria(axisType, empty, principalNodeType));
        }
Example #32
0
 void PushToken(XPathToken token)
 {
     Fx.Assert(null == this.readToken, "");
     this.readToken = token;
 }
        internal XPathLexer(string xpath, bool resolveKeywords)
        {
            this.resolveKeywords = resolveKeywords;

            // Hold on to a copy of the string so it can't be changed out from under us
            this.xpath = string.Copy(xpath);
            this.xpathLength = this.xpath.Length;

            // Start at the beginning
            this.tokenStart = 0;
            this.currChar = 0;

            this.ch = char.MinValue;
            this.previousID = XPathTokenID.Unknown;

            // We will not create new tokens, we will simply change the old one.
            // This will be the only XPathToken instance created by the lexer
            // The 'next token' data can be more quickly communicated to the parser if they both hold a reference to the data.
            this.token = new XPathToken();

            // Strip leading whitespace
            ConsumeWhitespace();
        }
        XPathToken NextTokenClass(XPathTokenID tokenClass)
        {
            XPathToken token = this.NextToken();

            if (null != token)
            {
                if (0 != (token.TokenID & tokenClass))
                {
                    return token;
                }

                this.readToken = token;
            }

            return null;
        }
 void PushToken(XPathToken token)
 {
     Fx.Assert(null == this.readToken, "");
     this.readToken = token;
 }