1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
{
/// <summary>
/// This class provides a default implementation of the
/// <see cref="IVocabulary"/>
/// interface.
/// </summary>
/// <author>Sam Harwell</author>
public class Vocabulary : IVocabulary
{
private static readonly string[] EmptyNames = Collections.EmptyList<string>();
/// <summary>
/// Gets an empty
/// <see cref="IVocabulary"/>
/// instance.
/// <p>
/// No literal or symbol names are assigned to token types, so
/// <see cref="GetDisplayName(int)"/>
/// returns the numeric value for all tokens
/// except
/// <see cref="TokenConstants.EOF"/>
/// .</p>
/// </summary>
[NotNull]
public static readonly Vocabulary EmptyVocabulary = new Vocabulary(EmptyNames, EmptyNames, EmptyNames);
[NotNull]
private readonly string[] literalNames;
[NotNull]
private readonly string[] symbolicNames;
[NotNull]
private readonly string[] displayNames;
private readonly int maxTokenType;
/// <summary>
/// Constructs a new instance of
/// <see cref="Vocabulary"/>
/// from the specified
/// literal and symbolic token names.
/// </summary>
/// <param name="literalNames">
/// The literal names assigned to tokens, or
/// <see langword="null"/>
/// if no literal names are assigned.
/// </param>
/// <param name="symbolicNames">
/// The symbolic names assigned to tokens, or
/// <see langword="null"/>
/// if no symbolic names are assigned.
/// </param>
/// <seealso cref="GetLiteralName(int)"/>
/// <seealso cref="GetSymbolicName(int)"/>
public Vocabulary(string[] literalNames, string[] symbolicNames)
: this(literalNames, symbolicNames, null)
{
}
/// <summary>
/// Constructs a new instance of
/// <see cref="Vocabulary"/>
/// from the specified
/// literal, symbolic, and display token names.
/// </summary>
/// <param name="literalNames">
/// The literal names assigned to tokens, or
/// <see langword="null"/>
/// if no literal names are assigned.
/// </param>
/// <param name="symbolicNames">
/// The symbolic names assigned to tokens, or
/// <see langword="null"/>
/// if no symbolic names are assigned.
/// </param>
/// <param name="displayNames">
/// The display names assigned to tokens, or
/// <see langword="null"/>
/// to use the values in
/// <paramref name="literalNames"/>
/// and
/// <paramref name="symbolicNames"/>
/// as
/// the source of display names, as described in
/// <see cref="GetDisplayName(int)"/>
/// .
/// </param>
/// <seealso cref="GetLiteralName(int)"/>
/// <seealso cref="GetSymbolicName(int)"/>
/// <seealso cref="GetDisplayName(int)"/>
public Vocabulary(string[] literalNames, string[] symbolicNames, string[] displayNames)
{
this.literalNames = literalNames != null ? literalNames : EmptyNames;
this.symbolicNames = symbolicNames != null ? symbolicNames : EmptyNames;
this.displayNames = displayNames != null ? displayNames : EmptyNames;
this.maxTokenType =
System.Math.Max(this.displayNames.Length,
System.Math.Max(this.literalNames.Length, this.symbolicNames.Length)) - 1;
}
/// <summary>
/// Returns the highest token type value. It can be used to iterate from
/// zero to that number, inclusively, thus querying all stored entries.
/// </summary>
public virtual int getMaxTokenType()
{
return maxTokenType;
}
[return: Nullable]
public virtual string GetLiteralName(int tokenType)
{
if (tokenType >= 0 && tokenType < literalNames.Length)
{
return literalNames[tokenType];
}
return null;
}
[return: Nullable]
public virtual string GetSymbolicName(int tokenType)
{
if (tokenType >= 0 && tokenType < symbolicNames.Length)
{
return symbolicNames[tokenType];
}
if (tokenType == TokenConstants.EOF)
{
return "EOF";
}
return null;
}
[return: NotNull]
public virtual string GetDisplayName(int tokenType)
{
if (tokenType >= 0 && tokenType < displayNames.Length)
{
string displayName = displayNames[tokenType];
if (displayName != null)
{
return displayName;
}
}
string literalName = GetLiteralName(tokenType);
if (literalName != null)
{
return literalName;
}
string symbolicName = GetSymbolicName(tokenType);
if (symbolicName != null)
{
return symbolicName;
}
return tokenType.ToString();
}
}
}