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"
15 /// A pattern like {@code <ID> = <expr>;} converted to a <seealso cref="ParseTree"/> by
16 /// <seealso cref="ParseTreePatternMatcher#compile(String, int)"/>.
18 class ANTLR4CPP_PUBLIC ParseTreePattern {
21 /// Construct a new instance of the <seealso cref="ParseTreePattern"/> class.
23 /// <param name="matcher"> The <seealso cref="ParseTreePatternMatcher"/> which created this
24 /// tree pattern. </param>
25 /// <param name="pattern"> The tree pattern in concrete syntax form. </param>
26 /// <param name="patternRuleIndex"> The parser rule which serves as the root of the
27 /// tree pattern. </param>
28 /// <param name="patternTree"> The tree pattern in <seealso cref="ParseTree"/> form. </param>
29 ParseTreePattern(ParseTreePatternMatcher *matcher, const std::string &pattern, int patternRuleIndex,
30 ParseTree *patternTree);
31 ParseTreePattern(ParseTreePattern const&) = default;
32 virtual ~ParseTreePattern();
35 /// Match a specific parse tree against this tree pattern.
37 /// <param name="tree"> The parse tree to match against this tree pattern. </param>
38 /// <returns> A <seealso cref="ParseTreeMatch"/> object describing the result of the
39 /// match operation. The <seealso cref="ParseTreeMatch#succeeded()"/> method can be
40 /// used to determine whether or not the match was successful. </returns>
41 virtual ParseTreeMatch match(ParseTree *tree);
44 /// Determine whether or not a parse tree matches this tree pattern.
46 /// <param name="tree"> The parse tree to match against this tree pattern. </param>
47 /// <returns> {@code true} if {@code tree} is a match for the current tree
48 /// pattern; otherwise, {@code false}. </returns>
49 virtual bool matches(ParseTree *tree);
51 /// Find all nodes using XPath and then try to match those subtrees against
52 /// this tree pattern.
53 /// @param tree The ParseTree to match against this pattern.
54 /// @param xpath An expression matching the nodes
56 /// @returns A collection of ParseTreeMatch objects describing the
57 /// successful matches. Unsuccessful matches are omitted from the result,
58 /// regardless of the reason for the failure.
59 virtual std::vector<ParseTreeMatch> findAll(ParseTree *tree, const std::string &xpath);
62 /// Get the <seealso cref="ParseTreePatternMatcher"/> which created this tree pattern.
64 /// <returns> The <seealso cref="ParseTreePatternMatcher"/> which created this tree
65 /// pattern. </returns>
66 virtual ParseTreePatternMatcher *getMatcher() const;
69 /// Get the tree pattern in concrete syntax form.
71 /// <returns> The tree pattern in concrete syntax form. </returns>
72 virtual std::string getPattern() const;
75 /// Get the parser rule which serves as the outermost rule for the tree
78 /// <returns> The parser rule which serves as the outermost rule for the tree
79 /// pattern. </returns>
80 virtual int getPatternRuleIndex() const;
83 /// Get the tree pattern as a <seealso cref="ParseTree"/>. The rule and token tags from
84 /// the pattern are present in the parse tree as terminal nodes with a symbol
85 /// of type <seealso cref="RuleTagToken"/> or <seealso cref="TokenTagToken"/>.
87 /// <returns> The tree pattern as a <seealso cref="ParseTree"/>. </returns>
88 virtual ParseTree* getPatternTree() const;
91 const int patternRuleIndex;
93 /// This is the backing field for <seealso cref="#getPattern()"/>.
94 const std::string _pattern;
96 /// This is the backing field for <seealso cref="#getPatternTree()"/>.
97 ParseTree *_patternTree;
99 /// This is the backing field for <seealso cref="#getMatcher()"/>.
100 ParseTreePatternMatcher *const _matcher;
103 } // namespace pattern
105 } // namespace antlr4