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 "atn/ATNType.h"
7 #include "atn/LexerATNSimulator.h"
8 #include "dfa/DFA.h"
9 #include "atn/EmptyPredictionContext.h"
10 #include "Exceptions.h"
11 #include "Vocabulary.h"
12 
13 #include "LexerInterpreter.h"
14 
15 using namespace antlr4;
16 
LexerInterpreter(const std::string & grammarFileName,const std::vector<std::string> & tokenNames,const std::vector<std::string> & ruleNames,const std::vector<std::string> & channelNames,const std::vector<std::string> & modeNames,const atn::ATN & atn,CharStream * input)17 LexerInterpreter::LexerInterpreter(const std::string &grammarFileName, const std::vector<std::string> &tokenNames,
18   const std::vector<std::string> &ruleNames, const std::vector<std::string> &channelNames, const std::vector<std::string> &modeNames,
19   const atn::ATN &atn, CharStream *input)
20   : LexerInterpreter(grammarFileName, dfa::Vocabulary::fromTokenNames(tokenNames), ruleNames, channelNames, modeNames, atn, input) {
21 }
22 
LexerInterpreter(const std::string & grammarFileName,const dfa::Vocabulary & vocabulary,const std::vector<std::string> & ruleNames,const std::vector<std::string> & channelNames,const std::vector<std::string> & modeNames,const atn::ATN & atn,CharStream * input)23 LexerInterpreter::LexerInterpreter(const std::string &grammarFileName, const dfa::Vocabulary &vocabulary,
24   const std::vector<std::string> &ruleNames, const std::vector<std::string> &channelNames, const std::vector<std::string> &modeNames,
25   const atn::ATN &atn, CharStream *input)
26   : Lexer(input), _grammarFileName(grammarFileName), _atn(atn), _ruleNames(ruleNames),
27                   _channelNames(channelNames), _modeNames(modeNames),
28                   _vocabulary(vocabulary) {
29 
30   if (_atn.grammarType != atn::ATNType::LEXER) {
31     throw IllegalArgumentException("The ATN must be a lexer ATN.");
32   }
33 
34   for (size_t i = 0; i < atn.maxTokenType; i++) {
35     _tokenNames.push_back(vocabulary.getDisplayName(i));
36   }
37 
38   for (size_t i = 0; i < atn.getNumberOfDecisions(); ++i) {
39     _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState(i), i));
40   }
41   _interpreter = new atn::LexerATNSimulator(this, _atn, _decisionToDFA, _sharedContextCache); /* mem-check: deleted in d-tor */
42 }
43 
~LexerInterpreter()44 LexerInterpreter::~LexerInterpreter()
45 {
46   delete _interpreter;
47 }
48 
getATN() const49 const atn::ATN& LexerInterpreter::getATN() const {
50   return _atn;
51 }
52 
getGrammarFileName() const53 std::string LexerInterpreter::getGrammarFileName() const {
54   return _grammarFileName;
55 }
56 
getTokenNames() const57 const std::vector<std::string>& LexerInterpreter::getTokenNames() const {
58   return _tokenNames;
59 }
60 
getRuleNames() const61 const std::vector<std::string>& LexerInterpreter::getRuleNames() const {
62   return _ruleNames;
63 }
64 
getChannelNames() const65 const std::vector<std::string>& LexerInterpreter::getChannelNames() const {
66   return _channelNames;
67 }
68 
getModeNames() const69 const std::vector<std::string>& LexerInterpreter::getModeNames() const {
70   return _modeNames;
71 }
72 
getVocabulary() const73 const dfa::Vocabulary& LexerInterpreter::getVocabulary() const {
74   return _vocabulary;
75 }
76