]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeProperty.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / tree / ParseTreeProperty.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
13   /// <summary>
14   /// Associate a property with a parse tree node. Useful with parse tree listeners
15   /// that need to associate values with particular tree nodes, kind of like
16   /// specifying a return value for the listener event method that visited a
17   /// particular node. Example:
18   ///
19   /// <pre>
20   /// ParseTreeProperty&lt;Integer&gt; values = new ParseTreeProperty&lt;Integer&gt;();
21   /// values.put(tree, 36);
22   /// int x = values.get(tree);
23   /// values.removeFrom(tree);
24   /// </pre>
25   ///
26   /// You would make one decl (values here) in the listener and use lots of times
27   /// in your event methods.
28   /// </summary>
29   template<typename V>
30   class ANTLR4CPP_PUBLIC ParseTreeProperty {
31   public:
32     virtual ~ParseTreeProperty() {}
33     virtual V get(ParseTree *node) {
34       return _annotations[node];
35     }
36     virtual void put(ParseTree *node, V value) {
37       _annotations[node] = value;
38     }
39     virtual V removeFrom(ParseTree *node) {
40       auto value = _annotations[node];
41       _annotations.erase(node);
42       return value;
43     }
44
45   protected:
46     std::map<ParseTree*, V> _annotations;
47   };
48
49 } // namespace tree
50 } // namespace antlr4