Rule names are now always written to interpreter data file.

Updated loaders for Java and C++.
This commit is contained in:
Mike Lischke 2017-05-05 17:00:41 +02:00
parent 126eec7092
commit 25eeb94498
3 changed files with 35 additions and 32 deletions

View File

@ -26,6 +26,9 @@ InterpreterData InterpreterDataReader::parseFile(std::string const& fileName) {
// token symbolic names:
// ...
//
// rule names:
// ...
//
// channel names:
// ...
//
@ -35,7 +38,7 @@ InterpreterData InterpreterDataReader::parseFile(std::string const& fileName) {
// 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, but a "rule names:" part instead.
// Data for a parser does not contain channel and mode names.
std::ifstream input(fileName);
if (!input.good())
@ -68,17 +71,17 @@ InterpreterData InterpreterDataReader::parseFile(std::string const& fileName) {
InterpreterData result(literalNames, symbolicNames);
std::getline(input, line, '\n');
if (line == "rule names:") {
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
assert(line == "rule names:");
while (true) {
std::getline(input, line, '\n');
if (line.empty())
break;
result.ruleNames.push_back(line);
};
result.ruleNames.push_back(line);
};
} else {
assert(line == "channel names:");
std::getline(input, line, '\n');
if (line == "channel names:") {
while (true) {
std::getline(input, line, '\n');
if (line.empty())

View File

@ -38,6 +38,9 @@ public class InterpreterDataReader {
* token symbolic names:
* ...
*
* rule names:
* ...
*
* channel names:
* ...
*
@ -47,10 +50,11 @@ public class InterpreterDataReader {
* 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, but a "rule names:" part instead.
* Data for a parser does not contain channel and mode names.
*/
public static InterpreterData parseFile(String fileName) {
InterpreterData result = new InterpreterData();
result.ruleNames = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
@ -78,18 +82,15 @@ public class InterpreterDataReader {
result.vocabulary = new VocabularyImpl(literalNames.toArray(new String[0]), symbolicNames.toArray(new String[0]));
line = br.readLine();
if ( line.equals("rule names:") ) {
result.ruleNames = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if ( line.isEmpty() )
break;
result.ruleNames.add(line);
}
}
else {
if ( !line.equals("channel names:") )
throw new RuntimeException("Unexpected data entry");
if ( !line.equals("rule names:") )
throw new RuntimeException("Unexpected data entry");
while ((line = br.readLine()) != null) {
if ( line.isEmpty() )
break;
result.ruleNames.add(line);
}
if ( line.equals("channel names:") ) { // Additional lexer data.
result.channels = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if ( line.isEmpty() )
@ -106,8 +107,7 @@ public class InterpreterDataReader {
break;
result.modes.add(line);
}
}
}
line = br.readLine();
if ( !line.equals("atn:") )

View File

@ -714,6 +714,13 @@ public class Tool {
}
content.append("\n");
content.append("rule names:\n");
names = g.getRuleNames();
for (String name : names) {
content.append(name + "\n");
}
content.append("\n");
if ( g.isLexer() ) {
content.append("channel names:\n");
content.append("DEFAULT_TOKEN_CHANNEL\n");
@ -728,13 +735,6 @@ public class Tool {
content.append(mode + "\n");
}
}
else {
content.append("rule names:\n");
names = g.getRuleNames();
for (String name : names) {
content.append(name + "\n");
}
}
content.append("\n");
IntegerList serializedATN = ATNSerializer.getSerialized(g.atn);