]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/Vocabulary.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / Vocabulary.cpp
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 #include "Token.h"
7
8 #include "Vocabulary.h"
9
10 using namespace antlr4::dfa;
11
12 const Vocabulary Vocabulary::EMPTY_VOCABULARY;
13
14 Vocabulary::Vocabulary(const std::vector<std::string> &literalNames, const std::vector<std::string> &symbolicNames)
15 : Vocabulary(literalNames, symbolicNames, {}) {
16 }
17
18 Vocabulary::Vocabulary(const std::vector<std::string> &literalNames,
19   const std::vector<std::string> &symbolicNames, const std::vector<std::string> &displayNames)
20   : _literalNames(literalNames), _symbolicNames(symbolicNames), _displayNames(displayNames),
21     _maxTokenType(std::max(_displayNames.size(), std::max(_literalNames.size(), _symbolicNames.size())) - 1) {
22   // See note here on -1 part: https://github.com/antlr/antlr4/pull/1146
23 }
24
25 Vocabulary::~Vocabulary() = default;
26
27 Vocabulary Vocabulary::fromTokenNames(const std::vector<std::string> &tokenNames) {
28   if (tokenNames.empty()) {
29     return EMPTY_VOCABULARY;
30   }
31
32   std::vector<std::string> literalNames = tokenNames;
33   std::vector<std::string> symbolicNames = tokenNames;
34   std::locale locale;
35   for (size_t i = 0; i < tokenNames.size(); i++) {
36     const std::string& tokenName = tokenNames[i];
37     if (tokenName.empty()) {
38       continue;
39     } else if (tokenName.front() == '\'') {
40       symbolicNames[i].clear();
41     } else if (std::isupper(tokenName.front(), locale)) {
42       literalNames[i].clear();
43     } else {
44       // wasn't a literal or symbolic name
45       literalNames[i].clear();
46       symbolicNames[i].clear();
47     }
48   }
49
50   return Vocabulary(literalNames, symbolicNames, tokenNames);
51 }
52
53 size_t Vocabulary::getMaxTokenType() const {
54   return _maxTokenType;
55 }
56
57 std::string Vocabulary::getLiteralName(size_t tokenType) const {
58   if (tokenType < _literalNames.size()) {
59     return _literalNames[tokenType];
60   }
61
62   return "";
63 }
64
65 std::string Vocabulary::getSymbolicName(size_t tokenType) const {
66   if (tokenType == Token::EOF) {
67     return "EOF";
68   }
69
70   if (tokenType < _symbolicNames.size()) {
71     return _symbolicNames[tokenType];
72   }
73
74   return "";
75 }
76
77 std::string Vocabulary::getDisplayName(size_t tokenType) const {
78   if (tokenType < _displayNames.size()) {
79     std::string displayName = _displayNames[tokenType];
80     if (!displayName.empty()) {
81       return displayName;
82     }
83   }
84
85   std::string literalName = getLiteralName(tokenType);
86   if (!literalName.empty()) {
87     return literalName;
88   }
89
90   std::string symbolicName = getSymbolicName(tokenType);
91   if (!symbolicName.empty()) {
92     return symbolicName;
93   }
94
95   return std::to_string(tokenType);
96 }