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 "Exceptions.h"
15 /// A tree pattern matching mechanism for ANTLR <seealso cref="ParseTree"/>s.
17 /// Patterns are strings of source input text with special tags representing
18 /// token or rule references such as:
20 /// {@code <ID> = <expr>;}
22 /// Given a pattern start rule such as {@code statement}, this object constructs
23 /// a <seealso cref="ParseTree"/> with placeholders for the {@code ID} and {@code expr}
24 /// subtree. Then the <seealso cref="#match"/> routines can compare an actual
25 /// <seealso cref="ParseTree"/> from a parse with this pattern. Tag {@code <ID>} matches
26 /// any {@code ID} token and tag {@code <expr>} references the result of the
27 /// {@code expr} rule (generally an instance of {@code ExprContext}.
29 /// Pattern {@code x = 0;} is a similar pattern that matches the same pattern
30 /// except that it requires the identifier to be {@code x} and the expression to
33 /// The <seealso cref="#matches"/> routines return {@code true} or {@code false} based
34 /// upon a match for the tree rooted at the parameter sent in. The
35 /// <seealso cref="#match"/> routines return a <seealso cref="ParseTreeMatch"/> object that
36 /// contains the parse tree, the parse tree pattern, and a map from tag name to
37 /// matched nodes (more below). A subtree that fails to match, returns with
38 /// <seealso cref="ParseTreeMatch#mismatchedNode"/> set to the first tree node that did not
41 /// For efficiency, you can compile a tree pattern in string form to a
42 /// <seealso cref="ParseTreePattern"/> object.
44 /// See {@code TestParseTreeMatcher} for lots of examples.
45 /// <seealso cref="ParseTreePattern"/> has two static helper methods:
46 /// <seealso cref="ParseTreePattern#findAll"/> and <seealso cref="ParseTreePattern#match"/> that
47 /// are easy to use but not super efficient because they create new
48 /// <seealso cref="ParseTreePatternMatcher"/> objects each time and have to compile the
49 /// pattern in string form before using it.
51 /// The lexer and parser that you pass into the <seealso cref="ParseTreePatternMatcher"/>
52 /// constructor are used to parse the pattern in string form. The lexer converts
53 /// the {@code <ID> = <expr>;} into a sequence of four tokens (assuming lexer
54 /// throws out whitespace or puts it on a hidden channel). Be aware that the
55 /// input stream is reset for the lexer (but not the parser; a
56 /// <seealso cref="ParserInterpreter"/> is created to parse the input.). Any user-defined
57 /// fields you have put into the lexer might get changed when this mechanism asks
58 /// it to scan the pattern string.
60 /// Normally a parser does not accept token {@code <expr>} as a valid
61 /// {@code expr} but, from the parser passed in, we create a special version of
62 /// the underlying grammar representation (an <seealso cref="ATN"/>) that allows imaginary
63 /// tokens representing rules ({@code <expr>}) to match entire rules. We call
64 /// these <em>bypass alternatives</em>.
66 /// Delimiters are {@code <} and {@code >}, with {@code \} as the escape string
67 /// by default, but you can set them to whatever you want using
68 /// <seealso cref="#setDelimiters"/>. You must escape both start and stop strings
69 /// {@code \<} and {@code \>}.
71 class ANTLR4CPP_PUBLIC ParseTreePatternMatcher {
73 class CannotInvokeStartRule : public RuntimeException {
75 CannotInvokeStartRule(const RuntimeException &e);
76 ~CannotInvokeStartRule();
79 // Fixes https://github.com/antlr/antlr4/issues/413
80 // "Tree pattern compilation doesn't check for a complete parse"
81 class StartRuleDoesNotConsumeFullPattern : public RuntimeException {
83 StartRuleDoesNotConsumeFullPattern() = default;
84 StartRuleDoesNotConsumeFullPattern(StartRuleDoesNotConsumeFullPattern const&) = default;
85 ~StartRuleDoesNotConsumeFullPattern();
87 StartRuleDoesNotConsumeFullPattern& operator=(StartRuleDoesNotConsumeFullPattern const&) = default;
90 /// Constructs a <seealso cref="ParseTreePatternMatcher"/> or from a <seealso cref="Lexer"/> and
91 /// <seealso cref="Parser"/> object. The lexer input stream is altered for tokenizing
92 /// the tree patterns. The parser is used as a convenient mechanism to get
93 /// the grammar name, plus token, rule names.
94 ParseTreePatternMatcher(Lexer *lexer, Parser *parser);
95 virtual ~ParseTreePatternMatcher();
98 /// Set the delimiters used for marking rule and token tags within concrete
99 /// syntax used by the tree pattern parser.
101 /// <param name="start"> The start delimiter. </param>
102 /// <param name="stop"> The stop delimiter. </param>
103 /// <param name="escapeLeft"> The escape sequence to use for escaping a start or stop delimiter.
105 /// <exception cref="IllegalArgumentException"> if {@code start} is {@code null} or empty. </exception>
106 /// <exception cref="IllegalArgumentException"> if {@code stop} is {@code null} or empty. </exception>
107 virtual void setDelimiters(const std::string &start, const std::string &stop, const std::string &escapeLeft);
110 /// Does {@code pattern} matched as rule {@code patternRuleIndex} match {@code tree}? </summary>
111 virtual bool matches(ParseTree *tree, const std::string &pattern, int patternRuleIndex);
114 /// Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a
115 /// compiled pattern instead of a string representation of a tree pattern.
117 virtual bool matches(ParseTree *tree, const ParseTreePattern &pattern);
120 /// Compare {@code pattern} matched as rule {@code patternRuleIndex} against
121 /// {@code tree} and return a <seealso cref="ParseTreeMatch"/> object that contains the
122 /// matched elements, or the node at which the match failed.
124 virtual ParseTreeMatch match(ParseTree *tree, const std::string &pattern, int patternRuleIndex);
127 /// Compare {@code pattern} matched against {@code tree} and return a
128 /// <seealso cref="ParseTreeMatch"/> object that contains the matched elements, or the
129 /// node at which the match failed. Pass in a compiled pattern instead of a
130 /// string representation of a tree pattern.
132 virtual ParseTreeMatch match(ParseTree *tree, const ParseTreePattern &pattern);
135 /// For repeated use of a tree pattern, compile it to a
136 /// <seealso cref="ParseTreePattern"/> using this method.
138 virtual ParseTreePattern compile(const std::string &pattern, int patternRuleIndex);
141 /// Used to convert the tree pattern string into a series of tokens. The
142 /// input stream is reset.
144 virtual Lexer* getLexer();
147 /// Used to collect to the grammar file name, token names, rule names for
148 /// used to parse the pattern into a parse tree.
150 virtual Parser* getParser();
152 // ---- SUPPORT CODE ----
154 virtual std::vector<std::unique_ptr<Token>> tokenize(const std::string &pattern);
156 /// Split "<ID> = <e:expr>;" into 4 chunks for tokenizing by tokenize().
157 virtual std::vector<Chunk> split(const std::string &pattern);
162 std::string _escape; // e.g., \< and \> must escape BOTH!
164 /// Recursively walk {@code tree} against {@code patternTree}, filling
165 /// {@code match.}<seealso cref="ParseTreeMatch#labels labels"/>.
167 /// <returns> the first node encountered in {@code tree} which does not match
168 /// a corresponding node in {@code patternTree}, or {@code null} if the match
169 /// was successful. The specific node returned depends on the matching
170 /// algorithm used by the implementation, and may be overridden. </returns>
171 virtual ParseTree* matchImpl(ParseTree *tree, ParseTree *patternTree, std::map<std::string, std::vector<ParseTree *>> &labels);
173 /// Is t <expr> subtree?
174 virtual RuleTagToken* getRuleTagToken(ParseTree *t);
180 void InitializeInstanceFields();
183 } // namespace pattern
185 } // namespace antlr4