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.
7 #include "atn/ATNDeserializer.h"
8 #include "Vocabulary.h"
10 #include "misc/InterpreterDataReader.h"
12 using namespace antlr4::dfa;
13 using namespace antlr4::atn;
14 using namespace antlr4::misc;
16 InterpreterData::InterpreterData(std::vector<std::string> const& literalNames, std::vector<std::string> const& symbolicNames)
17 : vocabulary(literalNames, symbolicNames) {
20 InterpreterData InterpreterDataReader::parseFile(std::string const& fileName) {
21 // The structure of the data file is very simple. Everything is line based with empty lines
22 // separating the different parts. For lexers the layout is:
23 // token literal names:
26 // token symbolic names:
39 // <a single line with comma separated int values> enclosed in a pair of squared brackets.
41 // Data for a parser does not contain channel and mode names.
43 std::ifstream input(fileName);
47 std::vector<std::string> literalNames;
48 std::vector<std::string> symbolicNames;
52 std::getline(input, line, '\n');
53 assert(line == "token literal names:");
55 std::getline(input, line, '\n');
59 literalNames.push_back(line == "null" ? "" : line);
62 std::getline(input, line, '\n');
63 assert(line == "token symbolic names:");
65 std::getline(input, line, '\n');
69 symbolicNames.push_back(line == "null" ? "" : line);
71 InterpreterData result(literalNames, symbolicNames);
73 std::getline(input, line, '\n');
74 assert(line == "rule names:");
76 std::getline(input, line, '\n');
80 result.ruleNames.push_back(line);
83 std::getline(input, line, '\n');
84 if (line == "channel names:") {
86 std::getline(input, line, '\n');
90 result.channels.push_back(line);
93 std::getline(input, line, '\n');
94 assert(line == "mode names:");
96 std::getline(input, line, '\n');
100 result.modes.push_back(line);
104 std::vector<uint16_t> serializedATN;
106 std::getline(input, line, '\n');
107 assert(line == "atn:");
108 std::getline(input, line, '\n');
109 std::stringstream tokenizer(line);
111 while (tokenizer.good()) {
112 std::getline(tokenizer, value, ',');
113 unsigned long number;
115 number = std::strtoul(&value[1], nullptr, 10);
117 number = std::strtoul(value.c_str(), nullptr, 10);
118 serializedATN.push_back(static_cast<uint16_t>(number));
121 ATNDeserializer deserializer;
122 result.atn = deserializer.deserialize(serializedATN);