Beispiel #1
0
        //		System.out.println(Arrays.toString(elements));
        // TODO: check for invalid token/rule names, bad syntax
        public virtual XPathElement[] Split(string path)
        {
            AntlrInputStream @in;

            try
            {
                @in = new AntlrInputStream(new StringReader(path));
            }
            catch (IOException ioe)
            {
                throw new ArgumentException("Could not read path: " + path, ioe);
            }
            XPathLexer lexer = new _XPathLexer_87(@in);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new XPathLexerErrorListener());
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);

            try
            {
                tokenStream.Fill();
            }
            catch (LexerNoViableAltException e)
            {
                int    pos = lexer.Column;
                string msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
                throw new ArgumentException(msg, e);
            }
            IList <IToken> tokens = tokenStream.GetTokens();
            //		System.out.println("path="+path+"=>"+tokens);
            IList <XPathElement> elements = new List <XPathElement>();
            int n = tokens.Count;
            int i = 0;

            while (i < n)
            {
                IToken el   = tokens[i];
                IToken next = null;
                switch (el.Type)
                {
                case XPathLexer.Root:
                case XPathLexer.Anywhere:
                {
                    bool anywhere = el.Type == XPathLexer.Anywhere;
                    i++;
                    next = tokens[i];
                    bool invert = next.Type == XPathLexer.Bang;
                    if (invert)
                    {
                        i++;
                        next = tokens[i];
                    }
                    XPathElement pathElement = GetXPathElement(next, anywhere);
                    pathElement.invert = invert;
                    elements.Add(pathElement);
                    i++;
                    break;
                }

                case XPathLexer.TokenRef:
                case XPathLexer.RuleRef:
                case XPathLexer.Wildcard:
                {
                    elements.Add(GetXPathElement(el, false));
                    i++;
                    break;
                }

                case TokenConstants.EOF:
                {
                    goto loop_break;
                }

                default:
                {
                    throw new ArgumentException("Unknowth path element " + el);
                }
                }
            }
            loop_break :;
            return(elements.ToArray());
        }
Beispiel #2
0
        //		System.out.println(Arrays.toString(elements));
        // TODO: check for invalid token/rule names, bad syntax
        public virtual XPathElement[] Split(string path)
        {
            AntlrInputStream @in;
            try
            {
                @in = new AntlrInputStream(new StringReader(path));
            }
            catch (IOException ioe)
            {
                throw new ArgumentException("Could not read path: " + path, ioe);
            }
            XPathLexer lexer = new _XPathLexer_87(@in);
            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new XPathLexerErrorListener());
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            try
            {
                tokenStream.Fill();
            }
            catch (LexerNoViableAltException e)
            {
                int pos = lexer.Column;
                string msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
                throw new ArgumentException(msg, e);
            }
            IList<IToken> tokens = tokenStream.GetTokens();
            //		System.out.println("path="+path+"=>"+tokens);
            IList<XPathElement> elements = new List<XPathElement>();
            int n = tokens.Count;
            int i = 0;
            while (i < n)
            {
                IToken el = tokens[i];
                IToken next = null;
                switch (el.Type)
                {
                    case XPathLexer.Root:
                    case XPathLexer.Anywhere:
                    {
                        bool anywhere = el.Type == XPathLexer.Anywhere;
                        i++;
                        next = tokens[i];
                        bool invert = next.Type == XPathLexer.Bang;
                        if (invert)
                        {
                            i++;
                            next = tokens[i];
                        }
                        XPathElement pathElement = GetXPathElement(next, anywhere);
                        pathElement.invert = invert;
                        elements.Add(pathElement);
                        i++;
                        break;
                    }

                    case XPathLexer.TokenRef:
                    case XPathLexer.RuleRef:
                    case XPathLexer.Wildcard:
                    {
                        elements.Add(GetXPathElement(el, false));
                        i++;
                        break;
                    }

                    case TokenConstants.Eof:
                    {
                        goto loop_break;
                    }

                    default:
                    {
                        throw new ArgumentException("Unknowth path element " + el);
                    }
                }
            }
loop_break: ;
            return elements.ToArray();
        }