]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / tree / pattern / ParseTreePattern.h
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.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlr4 {
11 namespace tree {
12 namespace pattern {
13
14   /// <summary>
15   /// A pattern like {@code <ID> = <expr>;} converted to a <seealso cref="ParseTree"/> by
16   /// <seealso cref="ParseTreePatternMatcher#compile(String, int)"/>.
17   /// </summary>
18   class ANTLR4CPP_PUBLIC ParseTreePattern {
19   public:
20     /// <summary>
21     /// Construct a new instance of the <seealso cref="ParseTreePattern"/> class.
22     /// </summary>
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();
33
34     /// <summary>
35     /// Match a specific parse tree against this tree pattern.
36     /// </summary>
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);
42
43     /// <summary>
44     /// Determine whether or not a parse tree matches this tree pattern.
45     /// </summary>
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);
50
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
55     ///
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);
60
61     /// <summary>
62     /// Get the <seealso cref="ParseTreePatternMatcher"/> which created this tree pattern.
63     /// </summary>
64     /// <returns> The <seealso cref="ParseTreePatternMatcher"/> which created this tree
65     /// pattern. </returns>
66     virtual ParseTreePatternMatcher *getMatcher() const;
67
68     /// <summary>
69     /// Get the tree pattern in concrete syntax form.
70     /// </summary>
71     /// <returns> The tree pattern in concrete syntax form. </returns>
72     virtual std::string getPattern() const;
73
74     /// <summary>
75     /// Get the parser rule which serves as the outermost rule for the tree
76     /// pattern.
77     /// </summary>
78     /// <returns> The parser rule which serves as the outermost rule for the tree
79     /// pattern. </returns>
80     virtual int getPatternRuleIndex() const;
81
82     /// <summary>
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"/>.
86     /// </summary>
87     /// <returns> The tree pattern as a <seealso cref="ParseTree"/>. </returns>
88     virtual ParseTree* getPatternTree() const;
89
90   private:
91     const int patternRuleIndex;
92
93     /// This is the backing field for <seealso cref="#getPattern()"/>.
94     const std::string _pattern;
95
96     /// This is the backing field for <seealso cref="#getPatternTree()"/>.
97     ParseTree *_patternTree;
98
99     /// This is the backing field for <seealso cref="#getMatcher()"/>.
100     ParseTreePatternMatcher *const _matcher;
101   };
102
103 } // namespace pattern
104 } // namespace tree
105 } // namespace antlr4