/// <summary> /// Tries to parse the xpath expression /// </summary> /// <param name="xpath"> /// is the xpath string. </param> /// <param name="isRootlessAccess"> /// if 'true' then PsychoPath engine can't parse xpath expressions starting with / or //. </param> /// <exception cref="XPathParserException."> </exception> /// <returns> the xpath value. /// @since 2.0 </returns> public virtual XPath parse(string xpath, bool isRootlessAccess) { var lexer = new XPath31Lexer(new AntlrInputStream(xpath)); try { lexer.RemoveErrorListeners(); var lexer_error_listener = new ErrorListener <int>(null, lexer, 3); lexer.AddErrorListener(lexer_error_listener); var tokens = new CommonTokenStream(lexer); var parser = new XPath31Parser(tokens); parser.RemoveErrorListeners(); var parser_error_listener = new ErrorListener <IToken>(parser, lexer, 3); parser.AddErrorListener(parser_error_listener); XPath31Parser.XpathContext parse_tree = parser.xpath(); if (lexer_error_listener.had_error || parser_error_listener.had_error) { throw new XPathParserException("ANTLR parser error"); } var sb = new OutputParseTree().OutputTree(parse_tree, tokens, parser, true); // Compute AST representation of XPath expression. var visitor = XPathBuilderVisitor.INSTANCE; var xPath2 = (XPath)visitor.VisitXpath(parse_tree); xPath2.Expression = xpath; var o = new org.eclipse.wst.xml.xpath2.processor.@internal.OutputXPathExpression(); var sb2 = o.OutputTree(xPath2).ToString(); //System.Console.WriteLine("=============================="); //System.Console.WriteLine("IR for expression \"" + xpath + "\""); //System.Console.WriteLine("Expression \"" + xpath + "\""); //System.Console.WriteLine(sb2.ToString()); if (isRootlessAccess) { } return(xPath2); } catch (StaticError e) { throw new XPathParserException(e.code(), e.Message); } catch (Exception e) { throw new XPathParserException(e.Message); } }
static void Try(string ffn) { System.IO.StreamReader file = new System.IO.StreamReader(ffn); string line; while ((line = file.ReadLine()) != null) { System.Console.WriteLine("===== Expression ====="); System.Console.WriteLine(line); var str = new AntlrInputStream(line); var lexer = new XPath31Lexer(str); var tokens = new CommonTokenStream(lexer); var parser = new XPath31Parser(tokens); var listener = new ErrorListener <IToken>(parser, lexer, tokens); parser.AddErrorListener(listener); var tree = parser.xpath(); if (listener.had_error) { System.Console.WriteLine("error in parse."); System.Console.WriteLine(tokens.OutputTokens()); System.Console.WriteLine(tree.OutputTree(tokens)); return; } else { System.Console.WriteLine(tokens.OutputTokens()); System.Console.WriteLine(tree.OutputTree(tokens)); System.Console.WriteLine("parse completed."); } string xpath = "//exprsingle"; var names = Antlr4.Runtime.Tree.Xpath.XPath.FindAll(tree, xpath, parser); foreach (var n in names) { System.Console.WriteLine("match " + n.GetText()); } } }