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
|
/* 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.
*/
#include "atn/ATN.h"
#include "atn/ATNDeserializer.h"
#include "Vocabulary.h"
#include "misc/InterpreterDataReader.h"
using namespace antlr4::dfa;
using namespace antlr4::atn;
using namespace antlr4::misc;
InterpreterData::InterpreterData(std::vector<std::string> const& literalNames, std::vector<std::string> const& symbolicNames)
: vocabulary(literalNames, symbolicNames) {
}
InterpreterData InterpreterDataReader::parseFile(std::string const& fileName) {
// The structure of the data file is very simple. Everything is line based with empty lines
// separating the different parts. For lexers the layout is:
// token literal names:
// ...
//
// token symbolic names:
// ...
//
// rule names:
// ...
//
// channel names:
// ...
//
// mode names:
// ...
//
// atn:
// <a single line with comma separated int values> enclosed in a pair of squared brackets.
//
// Data for a parser does not contain channel and mode names.
std::ifstream input(fileName);
if (!input.good())
return {};
std::vector<std::string> literalNames;
std::vector<std::string> symbolicNames;
std::string line;
std::getline(input, line, '\n');
assert(line == "token literal names:");
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
literalNames.push_back(line == "null" ? "" : line);
};
std::getline(input, line, '\n');
assert(line == "token symbolic names:");
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
symbolicNames.push_back(line == "null" ? "" : line);
};
InterpreterData result(literalNames, symbolicNames);
std::getline(input, line, '\n');
assert(line == "rule names:");
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
result.ruleNames.push_back(line);
};
std::getline(input, line, '\n');
if (line == "channel names:") {
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
result.channels.push_back(line);
};
std::getline(input, line, '\n');
assert(line == "mode names:");
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
result.modes.push_back(line);
};
}
std::vector<int32_t> serializedATN;
std::getline(input, line, '\n');
assert(line == "atn:");
std::getline(input, line, '\n');
std::stringstream tokenizer(line);
std::string value;
while (tokenizer.good()) {
std::getline(tokenizer, value, ',');
unsigned long number;
if (value[0] == '[')
number = std::strtoul(&value[1], nullptr, 10);
else
number = std::strtoul(value.c_str(), nullptr, 10);
serializedATN.push_back(static_cast<int32_t>(number));
}
ATNDeserializer deserializer;
result.atn = deserializer.deserialize(serializedATN);
return result;
}
|