Beispiel #1
0
        public static object parse(Expr rootParser, XPathLexer lexer)
        {
            string token = lexer.peak();
            char   ch    = token[0];

            if (ch == '(')
            {
                lexer.next();

                var expr = rootParser.parse(lexer);

                if (!lexer.next().Equals(")"))
                {
                    throw new Exception("Error: Unclosed parentheses");
                }

                return(expr);
            }

            if (ch == '"' || ch == '\'')
            {
                lexer.next();

                Dictionary <string, object> r = new Dictionary <string, object>();
                r.Add("type", XPathAnalyzer.ExprType.LITERAL);
                r.Add("string", token.Substring(1, (token.Length - 1) - 1));//token.slice(1, -1)

                return(r);
            }

            if (ch == '$')
            {
                throw new Exception("Error: Variable reference are not implemented");
            }

            if (XPathLexer.RegexTest(token, @"^\d+$") || XPathLexer.RegexTest(token, @"^(\d+)?\.\d+$"))
            {
                lexer.next();

                Dictionary <string, object> r = new Dictionary <string, object>();
                r.Add("type", XPathAnalyzer.ExprType.NUMBER);
                r.Add("number", float.Parse(token, CultureInfo.InvariantCulture));//token.slice(1, -1)

                return(r);
            }

            if (lexer.peak(1) == "(" && !NodeTypeValidator.isValid(lexer.peak()))
            {
                return(FunctionCall.parse(rootParser, lexer));
            }

            return(null);
            //throw new Exception("Error: Unhandle Expresion!");
        }
Beispiel #2
0
        public static bool isValidOp(XPathLexer lexer)
        {
            if (string.IsNullOrEmpty(lexer.peak()))
            {
                return(false);
            }

            char ch = lexer.peak()[0];

            return(ch == '(' ||
                   ch == '\\' ||
                   ch == '\'' ||
                   ch == '$' ||
                   XPathLexer.RegexTest(ch + "", @"^\d+$") ||
                   XPathLexer.RegexTest(ch + "", @"^d+$^(\d+)?\.\d+$") ||
                   ((lexer.peak(1) != null) && (lexer.peak(1).Equals("(")) && !NodeTypeValidator.isValid(lexer.peak())));
        }
Beispiel #3
0
        public static object parse(Expr rootParser, XPathLexer lexer)
        {
            Dictionary <string, object> absoluteLocation = new Dictionary <string, object>();

            absoluteLocation.Add("type", XPathAnalyzer.ExprType.ABSOLUTE_LOCATION_PATH);

            while (!lexer.empty() && (lexer.peak()[0] == '/'))
            {
                if (!absoluteLocation.ContainsKey("steps"))
                {
                    absoluteLocation.Add("steps", new List <Dictionary <string, object> >());
                }

                if (lexer.next().Equals("/"))
                {
                    var next = lexer.peak();

                    if (!lexer.empty() && (next.Equals(".") || next.Equals("..") || next.Equals("@") || next.Equals("*") || XPathLexer.RegexTest(next, @"(?![0 - 9])[\w]")))
                    {
                        ((List <Dictionary <string, object> >)absoluteLocation["steps"]).Add(Step.parse(rootParser, lexer));
                    }
                }
                else
                {
                    Dictionary <string, object> itm = new Dictionary <string, object>();
                    itm.Add("axis", XPathAnalyzer.AxisSpecifier.DESCENDANT_OR_SELF);

                    Dictionary <string, object> test = new Dictionary <string, object>();
                    test.Add("type", XPathAnalyzer.NodeType.NODE);

                    itm.Add("test", test);

                    ((List <Dictionary <string, object> >)absoluteLocation["steps"]).Add(itm);

                    ((List <Dictionary <string, object> >)absoluteLocation["steps"]).Add(Step.parse(rootParser, lexer));
                }
            }
            return(absoluteLocation);
        }