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 "Vocabulary.h"
10 using namespace antlr4::dfa;
12 const Vocabulary Vocabulary::EMPTY_VOCABULARY;
14 Vocabulary::Vocabulary(const std::vector<std::string> &literalNames, const std::vector<std::string> &symbolicNames)
15 : Vocabulary(literalNames, symbolicNames, {}) {
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
25 Vocabulary::~Vocabulary() = default;
27 Vocabulary Vocabulary::fromTokenNames(const std::vector<std::string> &tokenNames) {
28 if (tokenNames.empty()) {
29 return EMPTY_VOCABULARY;
32 std::vector<std::string> literalNames = tokenNames;
33 std::vector<std::string> symbolicNames = tokenNames;
35 for (size_t i = 0; i < tokenNames.size(); i++) {
36 const std::string& tokenName = tokenNames[i];
37 if (tokenName.empty()) {
39 } else if (tokenName.front() == '\'') {
40 symbolicNames[i].clear();
41 } else if (std::isupper(tokenName.front(), locale)) {
42 literalNames[i].clear();
44 // wasn't a literal or symbolic name
45 literalNames[i].clear();
46 symbolicNames[i].clear();
50 return Vocabulary(literalNames, symbolicNames, tokenNames);
53 size_t Vocabulary::getMaxTokenType() const {
57 std::string Vocabulary::getLiteralName(size_t tokenType) const {
58 if (tokenType < _literalNames.size()) {
59 return _literalNames[tokenType];
65 std::string Vocabulary::getSymbolicName(size_t tokenType) const {
66 if (tokenType == Token::EOF) {
70 if (tokenType < _symbolicNames.size()) {
71 return _symbolicNames[tokenType];
77 std::string Vocabulary::getDisplayName(size_t tokenType) const {
78 if (tokenType < _displayNames.size()) {
79 std::string displayName = _displayNames[tokenType];
80 if (!displayName.empty()) {
85 std::string literalName = getLiteralName(tokenType);
86 if (!literalName.empty()) {
90 std::string symbolicName = getSymbolicName(tokenType);
91 if (!symbolicName.empty()) {
95 return std::to_string(tokenType);