/proc/self/cwd/external/antlr4-cpp-runtime~/runtime/src/Vocabulary.h
Line | Count | Source |
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 dfa { |
12 | | |
13 | | /// This class provides a default implementation of the <seealso cref="Vocabulary"/> |
14 | | /// interface. |
15 | | class ANTLR4CPP_PUBLIC Vocabulary final { |
16 | | public: |
17 | | /// Gets an empty <seealso cref="Vocabulary"/> instance. |
18 | | /// |
19 | | /// <para> |
20 | | /// No literal or symbol names are assigned to token types, so |
21 | | /// <seealso cref="#getDisplayName(int)"/> returns the numeric value for all tokens |
22 | | /// except <seealso cref="Token#EOF"/>.</para> |
23 | | [[deprecated("Use the default constructor of Vocabulary instead.")]] static const Vocabulary EMPTY_VOCABULARY; |
24 | | |
25 | 2 | Vocabulary() {} |
26 | | |
27 | | Vocabulary(const Vocabulary&) = default; |
28 | | |
29 | | /// <summary> |
30 | | /// Constructs a new instance of <seealso cref="Vocabulary"/> from the specified |
31 | | /// literal and symbolic token names. |
32 | | /// </summary> |
33 | | /// <param name="literalNames"> The literal names assigned to tokens, or {@code null} |
34 | | /// if no literal names are assigned. </param> |
35 | | /// <param name="symbolicNames"> The symbolic names assigned to tokens, or |
36 | | /// {@code null} if no symbolic names are assigned. |
37 | | /// </param> |
38 | | /// <seealso cref= #getLiteralName(int) </seealso> |
39 | | /// <seealso cref= #getSymbolicName(int) </seealso> |
40 | | Vocabulary(std::vector<std::string> literalNames, std::vector<std::string> symbolicNames); |
41 | | |
42 | | /// <summary> |
43 | | /// Constructs a new instance of <seealso cref="Vocabulary"/> from the specified |
44 | | /// literal, symbolic, and display token names. |
45 | | /// </summary> |
46 | | /// <param name="literalNames"> The literal names assigned to tokens, or {@code null} |
47 | | /// if no literal names are assigned. </param> |
48 | | /// <param name="symbolicNames"> The symbolic names assigned to tokens, or |
49 | | /// {@code null} if no symbolic names are assigned. </param> |
50 | | /// <param name="displayNames"> The display names assigned to tokens, or {@code null} |
51 | | /// to use the values in {@code literalNames} and {@code symbolicNames} as |
52 | | /// the source of display names, as described in |
53 | | /// <seealso cref="#getDisplayName(int)"/>. |
54 | | /// </param> |
55 | | /// <seealso cref= #getLiteralName(int) </seealso> |
56 | | /// <seealso cref= #getSymbolicName(int) </seealso> |
57 | | /// <seealso cref= #getDisplayName(int) </seealso> |
58 | | Vocabulary(std::vector<std::string> literalNames, std::vector<std::string> symbolicNames, |
59 | | std::vector<std::string> displayNames); |
60 | | |
61 | | /// <summary> |
62 | | /// Returns the highest token type value. It can be used to iterate from |
63 | | /// zero to that number, inclusively, thus querying all stored entries. </summary> |
64 | | /// <returns> the highest token type value </returns> |
65 | 0 | constexpr size_t getMaxTokenType() const { return _maxTokenType; } |
66 | | |
67 | | /// <summary> |
68 | | /// Gets the string literal associated with a token type. The string returned |
69 | | /// by this method, when not {@code null}, can be used unaltered in a parser |
70 | | /// grammar to represent this token type. |
71 | | /// |
72 | | /// <para>The following table shows examples of lexer rules and the literal |
73 | | /// names assigned to the corresponding token types.</para> |
74 | | /// |
75 | | /// <table> |
76 | | /// <tr> |
77 | | /// <th>Rule</th> |
78 | | /// <th>Literal Name</th> |
79 | | /// <th>Java String Literal</th> |
80 | | /// </tr> |
81 | | /// <tr> |
82 | | /// <td>{@code THIS : 'this';}</td> |
83 | | /// <td>{@code 'this'}</td> |
84 | | /// <td>{@code "'this'"}</td> |
85 | | /// </tr> |
86 | | /// <tr> |
87 | | /// <td>{@code SQUOTE : '\'';}</td> |
88 | | /// <td>{@code '\''}</td> |
89 | | /// <td>{@code "'\\''"}</td> |
90 | | /// </tr> |
91 | | /// <tr> |
92 | | /// <td>{@code ID : [A-Z]+;}</td> |
93 | | /// <td>n/a</td> |
94 | | /// <td>{@code null}</td> |
95 | | /// </tr> |
96 | | /// </table> |
97 | | /// </summary> |
98 | | /// <param name="tokenType"> The token type. |
99 | | /// </param> |
100 | | /// <returns> The string literal associated with the specified token type, or |
101 | | /// {@code null} if no string literal is associated with the type. </returns> |
102 | | std::string_view getLiteralName(size_t tokenType) const; |
103 | | |
104 | | /// <summary> |
105 | | /// Gets the symbolic name associated with a token type. The string returned |
106 | | /// by this method, when not {@code null}, can be used unaltered in a parser |
107 | | /// grammar to represent this token type. |
108 | | /// |
109 | | /// <para>This method supports token types defined by any of the following |
110 | | /// methods:</para> |
111 | | /// |
112 | | /// <ul> |
113 | | /// <li>Tokens created by lexer rules.</li> |
114 | | /// <li>Tokens defined in a <code>tokens{}</code> block in a lexer or parser |
115 | | /// grammar.</li> |
116 | | /// <li>The implicitly defined {@code EOF} token, which has the token type |
117 | | /// <seealso cref="Token#EOF"/>.</li> |
118 | | /// </ul> |
119 | | /// |
120 | | /// <para>The following table shows examples of lexer rules and the literal |
121 | | /// names assigned to the corresponding token types.</para> |
122 | | /// |
123 | | /// <table> |
124 | | /// <tr> |
125 | | /// <th>Rule</th> |
126 | | /// <th>Symbolic Name</th> |
127 | | /// </tr> |
128 | | /// <tr> |
129 | | /// <td>{@code THIS : 'this';}</td> |
130 | | /// <td>{@code THIS}</td> |
131 | | /// </tr> |
132 | | /// <tr> |
133 | | /// <td>{@code SQUOTE : '\'';}</td> |
134 | | /// <td>{@code SQUOTE}</td> |
135 | | /// </tr> |
136 | | /// <tr> |
137 | | /// <td>{@code ID : [A-Z]+;}</td> |
138 | | /// <td>{@code ID}</td> |
139 | | /// </tr> |
140 | | /// </table> |
141 | | /// </summary> |
142 | | /// <param name="tokenType"> The token type. |
143 | | /// </param> |
144 | | /// <returns> The symbolic name associated with the specified token type, or |
145 | | /// {@code null} if no symbolic name is associated with the type. </returns> |
146 | | std::string_view getSymbolicName(size_t tokenType) const; |
147 | | |
148 | | /// <summary> |
149 | | /// Gets the display name of a token type. |
150 | | /// |
151 | | /// <para>ANTLR provides a default implementation of this method, but |
152 | | /// applications are free to override the behavior in any manner which makes |
153 | | /// sense for the application. The default implementation returns the first |
154 | | /// result from the following list which produces a non-{@code null} |
155 | | /// result.</para> |
156 | | /// |
157 | | /// <ol> |
158 | | /// <li>The result of <seealso cref="#getLiteralName"/></li> |
159 | | /// <li>The result of <seealso cref="#getSymbolicName"/></li> |
160 | | /// <li>The result of <seealso cref="Integer#toString"/></li> |
161 | | /// </ol> |
162 | | /// </summary> |
163 | | /// <param name="tokenType"> The token type. |
164 | | /// </param> |
165 | | /// <returns> The display name of the token type, for use in error reporting or |
166 | | /// other user-visible messages which reference specific token types. </returns> |
167 | | std::string getDisplayName(size_t tokenType) const; |
168 | | |
169 | | private: |
170 | | std::vector<std::string> const _literalNames; |
171 | | std::vector<std::string> const _symbolicNames; |
172 | | std::vector<std::string> const _displayNames; |
173 | | const size_t _maxTokenType = 0; |
174 | | }; |
175 | | |
176 | | } // namespace atn |
177 | | } // namespace antlr4 |