From 46a07148dc678688eed6b7b30c9e2573561949ef Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 10 Mar 2022 17:45:23 -0800 Subject: [PATCH] Use int array not string for ATN in Dart (#3578) --- .../org/antlr/v4/test/runtime/dart/BaseDartTest.java | 2 +- runtime/Dart/lib/src/parser.dart | 11 ++++------- runtime/Dart/lib/src/recognizer.dart | 2 +- .../org/antlr/v4/tool/templates/codegen/Dart/Dart.stg | 10 ++++++---- tool/src/org/antlr/v4/codegen/target/DartTarget.java | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java index 30e3456513..dbfbb278f5 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java @@ -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) { diff --git a/runtime/Dart/lib/src/parser.dart b/runtime/Dart/lib/src/parser.dart index 65fb472bfa..de002e377f 100644 --- a/runtime/Dart/lib/src/parser.dart +++ b/runtime/Dart/lib/src/parser.dart @@ -27,7 +27,7 @@ abstract class Parser extends Recognizer { /// bypass alternatives. /// /// @see ATNDeserializationOptions#isGenerateRuleBypassTransitions() - static final Map bypassAltsAtnCache = {}; + ATN? bypassAltsAtnCache; /// The error handling strategy for the parser. The default value is a new /// instance of [DefaultErrorStrategy]. @@ -295,16 +295,13 @@ abstract class Parser extends Recognizer { '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 diff --git a/runtime/Dart/lib/src/recognizer.dart b/runtime/Dart/lib/src/recognizer.dart index dbac8f0fb3..a46edbe3bd 100644 --- a/runtime/Dart/lib/src/recognizer.dart +++ b/runtime/Dart/lib/src/recognizer.dart @@ -85,7 +85,7 @@ abstract class Recognizer { /// ///

For interpreters, we don't know their serialized ATN despite having /// created the interpreter from it.

- String? get serializedATN { + List get serializedATN { throw UnsupportedError('there is no serialized ATN'); } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg index 059b845f5f..769b5cc0ee 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg @@ -217,7 +217,7 @@ class extends { String get grammarFileName => ''; @override - String get serializedATN => _serializedATN; + List\ get serializedATN => _serializedATN; @override ATN getATN() { @@ -839,7 +839,7 @@ class extends { } @override - String get serializedATN => _serializedATN; + List\ get serializedATN => _serializedATN; @override String get grammarFileName => ''; @@ -853,10 +853,12 @@ class extends { >> SerializedATN(model) ::= << -static const String _serializedATN = '<\t>'}>'; +static const List\ _serializedATN = [ + }; 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 diff --git a/tool/src/org/antlr/v4/codegen/target/DartTarget.java b/tool/src/org/antlr/v4/codegen/target/DartTarget.java index 066d37100e..64dc2ee3b5 100644 --- a/tool/src/org/antlr/v4/codegen/target/DartTarget.java +++ b/tool/src/org/antlr/v4/codegen/target/DartTarget.java @@ -73,7 +73,7 @@ protected STGroup loadTemplates() { @Override public boolean isATNSerializedAsInts() { - return false; + return true; } @Override