abouttreesummaryrefslogcommitdiff
path: root/antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp
diff options
context:
space:
mode:
authorPatrick Schönberger2021-08-14 14:56:12 +0200
committerPatrick Schönberger2021-08-14 14:56:12 +0200
commitc6ad2948bb98d42f8e0883ef82cd14cd2d5eda60 (patch)
tree9e83d6d8f61e56f5d3425b8709314d6bdb9315a9 /antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp
parent9f94b672a5dc32da5ad01742bd4e976315a30d9c (diff)
downloadtoc-c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60.tar.gz
toc-c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60.zip
add antlr source code and ReadMeHEADmain
Diffstat (limited to 'antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp')
-rw-r--r--antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp
new file mode 100644
index 0000000..c77b8bc
--- /dev/null
+++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/InterpreterDataReader.cpp
@@ -0,0 +1,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<uint16_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<uint16_t>(number));
+ }
+
+ ATNDeserializer deserializer;
+ result.atn = deserializer.deserialize(serializedATN);
+ return result;
+}