Skip to content

Commit

Permalink
Use int array not string for ATN in Dart (#3578)
Browse files Browse the repository at this point in the history
  • Loading branch information
parrt authored Mar 11, 2022
1 parent b544a64 commit 46a0714
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private String locateTool(String tool) {

final String[] roots = isWindows()
? new String[]{"C:\\tools\\dart-sdk\\bin\\"}
: new String[]{"/usr/local/bin/", "/opt/local/bin/", "/usr/bin/", "/usr/lib/dart/bin/", "/usr/local/opt/dart/libexec"};
: new String[]{"/usr/local/bin/", "/opt/local/bin/", "/opt/homebrew/bin/", "/usr/bin/", "/usr/lib/dart/bin/", "/usr/local/opt/dart/libexec"};

for (String root : roots) {
for (String t : tools) {
Expand Down
11 changes: 4 additions & 7 deletions runtime/Dart/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Parser extends Recognizer<ParserATNSimulator> {
/// bypass alternatives.
///
/// @see ATNDeserializationOptions#isGenerateRuleBypassTransitions()
static final Map<String, ATN> bypassAltsAtnCache = {};
ATN? bypassAltsAtnCache;

/// The error handling strategy for the parser. The default value is a new
/// instance of [DefaultErrorStrategy].
Expand Down Expand Up @@ -295,16 +295,13 @@ abstract class Parser extends Recognizer<ParserATNSimulator> {
'The current parser does not support an ATN with bypass alternatives.');
}

var result = bypassAltsAtnCache[serializedATN];
if (result == null) {
if (bypassAltsAtnCache == null) {
final deserializationOptions = ATNDeserializationOptions(false);
deserializationOptions.setGenerateRuleBypassTransitions(true);
result = ATNDeserializer(deserializationOptions)
.deserialize(serializedATN!.codeUnits);
bypassAltsAtnCache[serializedATN!] = result;
bypassAltsAtnCache = ATNDeserializer(deserializationOptions).deserialize(serializedATN);
}

return result;
return bypassAltsAtnCache!;
}

/// The preferred method of getting a tree pattern. For example, here's a
Expand Down
2 changes: 1 addition & 1 deletion runtime/Dart/lib/src/recognizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class Recognizer<ATNInterpreter extends ATNSimulator> {
///
/// <p>For interpreters, we don't know their serialized ATN despite having
/// created the interpreter from it.</p>
String? get serializedATN {
List<int> get serializedATN {
throw UnsupportedError('there is no serialized ATN');
}

Expand Down
10 changes: 6 additions & 4 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class <parser.name> extends <superClass; null="Parser"> {
String get grammarFileName => '<parser.grammarFileName>';

@override
String get serializedATN => _serializedATN;
List\<int> get serializedATN => _serializedATN;

@override
ATN getATN() {
Expand Down Expand Up @@ -839,7 +839,7 @@ class <lexer.name> extends <superClass; null="Lexer"> {
}

@override
String get serializedATN => _serializedATN;
List\<int> get serializedATN => _serializedATN;

@override
String get grammarFileName => '<lexer.grammarFileName>';
Expand All @@ -853,10 +853,12 @@ class <lexer.name> extends <superClass; null="Lexer"> {
>>

SerializedATN(model) ::= <<
static const String _serializedATN = '<model.serialized; wrap={'<\n><\t>'}>';
static const List\<int> _serializedATN = [
<model.serialized: {s | <s>}; separator=",", wrap>
];

static final ATN _ATN =
ATNDeserializer().deserialize(_serializedATN.codeUnits);
ATNDeserializer().deserialize(_serializedATN);
>>

/// Using a type to init value map, try to init a type; if not in table
Expand Down
2 changes: 1 addition & 1 deletion tool/src/org/antlr/v4/codegen/target/DartTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected STGroup loadTemplates() {

@Override
public boolean isATNSerializedAsInts() {
return false;
return true;
}

@Override
Expand Down

0 comments on commit 46a0714

Please sign in to comment.