1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "antlr4-common.h"
14 /// Represent a subset of XPath XML path syntax for use in identifying nodes in
18 /// Split path into words and separators {@code /} and {@code //} via ANTLR
19 /// itself then walk path elements from left to right. At each separator-word
20 /// pair, find set of nodes. Next stage uses those as work list.</para>
23 /// The basic interface is
24 /// <seealso cref="XPath#findAll ParseTree.findAll"/>{@code (tree, pathString, parser)}.
25 /// But that is just shorthand for:</para>
28 /// <seealso cref="XPath"/> p = new <seealso cref="XPath#XPath XPath"/>(parser, pathString);
29 /// return p.<seealso cref="#evaluate evaluate"/>(tree);
33 /// See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this
34 /// allows operators:</para>
37 /// <dt>/</dt> <dd>root</dd>
38 /// <dt>//</dt> <dd>anywhere</dd>
39 /// <dt>!</dt> <dd>invert; this must appear directly after root or anywhere
44 /// and path elements:</para>
47 /// <dt>ID</dt> <dd>token name</dd>
48 /// <dt>'string'</dt> <dd>any string literal token from the grammar</dd>
49 /// <dt>expr</dt> <dd>rule name</dd>
50 /// <dt>*</dt> <dd>wildcard matching any node</dd>
54 /// Whitespace is not allowed.</para>
56 class ANTLR4CPP_PUBLIC XPath {
58 static const std::string WILDCARD; // word not operator/separator
59 static const std::string NOT; // word for invert operator
61 XPath(Parser *parser, const std::string &path);
64 // TODO: check for invalid token/rule names, bad syntax
65 virtual std::vector<std::unique_ptr<XPathElement>> split(const std::string &path);
67 static std::vector<ParseTree *> findAll(ParseTree *tree, std::string const& xpath, Parser *parser);
69 /// Return a list of all nodes starting at {@code t} as root that satisfy the
70 /// path. The root {@code /} is relative to the node passed to
71 /// <seealso cref="#evaluate"/>.
72 virtual std::vector<ParseTree *> evaluate(ParseTree *t);
78 /// Convert word like {@code *} or {@code ID} or {@code expr} to a path
79 /// element. {@code anywhere} is {@code true} if {@code //} precedes the
81 virtual std::unique_ptr<XPathElement> getXPathElement(Token *wordToken, bool anywhere);