Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dart] Support Dart multiple platforms. #3250

Merged
merged 12 commits into from Aug 23, 2021
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,4 @@ YYYY/MM/DD, github id, Full name, email
2021/07/01, marcauberer, Marc Auberer, marc.auberer@chillibits.com
2021/07/14, renzhentaxibaerde, Renzhentaxi Baerde, renzhentaxibaerde@gmail.com
2021/08/02, minjoosur, Minjoo Sur, msur@salesforce.com
2021/08/08, ansiemens, Yi-Hong Lin, ansiemens@gmail.com
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
writeln(s) ::= <<print(<s>);>>
write(s) ::= <<stdout.write(<s>);>>
write(s) ::= <<TEST_platformStdoutWrite(<s>);>>
writeList(s) ::= <<print(<s; separator="+">);>>

False() ::= "false"
Expand Down Expand Up @@ -242,7 +242,7 @@ TokenGetterListener(X) ::= <<
class LeafListener extends TBaseListener {
void exitA(AContext ctx) {
if (ctx.childCount==2)
stdout.write("${ctx.INT(0)?.symbol.text} ${ctx.INT(1)?.symbol.text} ${ctx.INTs()}");
TEST_platformStdoutWrite("${ctx.INT(0)?.symbol.text} ${ctx.INT(1)?.symbol.text} ${ctx.INTs()}");
else
print(ctx.ID()?.symbol);
}
Expand All @@ -255,7 +255,7 @@ RuleGetterListener(X) ::= <<
class LeafListener extends TBaseListener {
void exitA(AContext ctx) {
if (ctx.childCount==2) {
stdout.write("${ctx.b(0)?.start?.text} ${ctx.b(1)?.start?.text} ${ctx.bs()[0].start?.text}");
TEST_platformStdoutWrite("${ctx.b(0)?.start?.text} ${ctx.b(1)?.start?.text} ${ctx.bs()[0].start?.text}");
} else
print(ctx.b(0)?.start?.text);
}
Expand All @@ -269,7 +269,7 @@ LRListener(X) ::= <<
class LeafListener extends TBaseListener {
void exitE(EContext ctx) {
if (ctx.childCount==3) {
stdout.write("${ctx.e(0)?.start?.text} ${ctx.e(1)?.start?.text} ${ctx.es()[0].start?.text}\n");
print("${ctx.e(0)?.start?.text} ${ctx.e(1)?.start?.text} ${ctx.es()[0].start?.text}");
This conversation was marked as resolved.
Show resolved Hide resolved
} else
print(ctx.INT()?.symbol.text);
}
Expand All @@ -281,7 +281,7 @@ LRWithLabelsListener(X) ::= <<
@parser::definitions {
class LeafListener extends TBaseListener {
void exitCall(CallContext ctx) {
stdout.write("${ctx.e()?.start?.text} ${ctx.eList()}");
TEST_platformStdoutWrite("${ctx.e()?.start?.text} ${ctx.eList()}");
}
void exitInt(IntContext ctx) {
print(ctx.INT()?.symbol.text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static junit.framework.TestCase.*;
import static org.antlr.v4.test.runtime.BaseRuntimeTest.readFile;
import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile;
import static org.junit.Assert.assertArrayEquals;


public class BaseDartTest extends BaseRuntimeTestSupport implements RuntimeTestSupport {
Expand Down Expand Up @@ -147,7 +146,9 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName,
final File dartToolDir = new File(getTempDirPath(), ".dart_tool");
if (cacheDartPackages == null) {
try {
final Process process = Runtime.getRuntime().exec(new String[]{locatePub(), "get"}, null, getTempTestDir());
final Process process =
Runtime.getRuntime().exec(
new String[]{locateDart(), "pub", "get"}, null, getTempTestDir());
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
stderrVacuum.start();
Timer timer = new Timer();
Expand Down Expand Up @@ -207,8 +208,8 @@ public String execClass(String className, boolean compile) {
try {
if (compile) {
String[] args = new String[]{
locateDart2Native(),
className + ".dart", "-o", className
locateDart(),
"compile", "exe", className + ".dart", "-o", className
};
String cmdLine = Utils.join(args, " ");
System.err.println("Compile: " + cmdLine);
Expand Down Expand Up @@ -314,23 +315,6 @@ private String locateTool(String tool) {
throw new RuntimeException("Could not locate " + tool);
}

protected String locatePub() {
String propName = getPropertyPrefix() + "-pub";
String prop = System.getProperty(propName);

if (prop == null || prop.length() == 0) {
prop = locateTool("pub");
}

File file = new File(prop);

if (!file.exists()) {
throw new RuntimeException("Missing system property:" + propName);
}

return file.getAbsolutePath();
}

protected String locateDart() {
String propName = getPropertyPrefix() + "-dart";
String prop = System.getProperty(propName);
Expand All @@ -348,23 +332,6 @@ protected String locateDart() {
return file.getAbsolutePath();
}

protected String locateDart2Native() {
String propName = getPropertyPrefix() + "-dart2native";
String prop = System.getProperty(propName);

if (prop == null || prop.length() == 0) {
prop = locateTool("dart2native");
}

File file = new File(prop);

if (!file.exists()) {
throw new RuntimeException("Missing system property:" + propName);
}

return file.getAbsolutePath();
}

private String locateRuntime() {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final URL runtimeSrc = loader.getResource("Dart");
Expand Down
5 changes: 5 additions & 0 deletions runtime/Dart/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
## 4.9.0

* Initial release

## 4.9.3

* Support web platform.

8 changes: 7 additions & 1 deletion runtime/Dart/lib/antlr4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ export 'src/parser_rule_context.dart';
export 'src/vocabulary.dart';
export 'src/runtime_meta_data.dart';
export 'src/token.dart';
export 'src/prediction_context.dart';
export 'src/prediction_context.dart';

import 'src/util/platform_stub.dart'
if (dart.library.io) 'src/util/platform_io.dart';

/// Hack to workaround not being able to access stdout in tests.
void TEST_platformStdoutWrite(Object? object) => stdoutWrite(object);
3 changes: 2 additions & 1 deletion runtime/Dart/lib/src/atn/src/atn_simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ abstract class ATNSimulator {
if (sharedContextCache == null) return context;

final visited = <PredictionContext, PredictionContext>{};
return PredictionContext.getCachedContext(context, sharedContextCache!, visited);
return PredictionContext.getCachedContext(
context, sharedContextCache!, visited);
}
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/Dart/lib/src/atn/src/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class DecisionEventInfo {
///
/// @since 4.3
class DecisionInfo {


/// The decision number, which is an index into {@link ATN#decisionToState}.
final int decision;

Expand Down
7 changes: 7 additions & 0 deletions runtime/Dart/lib/src/atn/src/lexer_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ import '../../util/murmur_hash.dart';
enum LexerActionType {
/// The type of a [LexerChannelAction] action.
CHANNEL,

/// The type of a [LexerCustomAction] action.
CUSTOM,

/// The type of a [LexerModeAction] action.
MODE,

/// The type of a [LexerMoreAction] action.
MORE,

/// The type of a [LexerPopModeAction] action.
POP_MODE,

/// The type of a [LexerPushModeAction] action.
PUSH_MODE,

/// The type of a [LexerSkipAction] action.
SKIP,

/// The type of a [LexerTypeAction] action.
TYPE,
}
Expand Down
8 changes: 5 additions & 3 deletions runtime/Dart/lib/src/error/src/error_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
* can be found in the LICENSE.txt file in the project root.
*/

import 'dart:io';

import '../../atn/atn.dart';
import '../../dfa/dfa.dart';
import '../../parser.dart';
import '../../recognizer.dart';
import '../../util/bit_set.dart';
import 'errors.dart';

import '../../util/platform_stub.dart'
if (dart.library.io) '../../util/platform_io.dart'
if (dart.library.html) '../../util/platform_html.dart';

abstract class ErrorListener {
/// Upon syntax error, notify any interested parties. This is not how to
/// recover from errors or compute error messages. [ANTLRErrorStrategy]
Expand Down Expand Up @@ -237,7 +239,7 @@ class ConsoleErrorListener extends BaseErrorListener {
/// </pre>
@override
void syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
stderr.writeln('line $line:$column $msg');
stderrWriteln('line $line:$column $msg');
}
}

Expand Down
5 changes: 3 additions & 2 deletions runtime/Dart/lib/src/input_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'interval_set.dart';
import 'token.dart';

import 'util/platform_stub.dart' if (dart.library.io) 'util/platform_io.dart';

/// A simple stream of symbols whose values are represented as integers. This
/// interface provides <em>marked ranges</em> with support for a minimum level
/// of buffering necessary to implement arbitrary lookahead during prediction.
Expand Down Expand Up @@ -248,7 +249,7 @@ class InputStream extends CharStream {
}

static Future<InputStream> fromPath(String path, {Encoding encoding = utf8}) {
return fromStream(File(path).openRead());
return fromStream(readStream(path), encoding: encoding);
}

@override
Expand Down
3 changes: 2 additions & 1 deletion runtime/Dart/lib/src/misc/pair.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Pair<A, B> {

@override
bool operator ==(other) {
return identical(this, other) || other is Pair && a == other.a && b == other.b;
return identical(this, other) ||
other is Pair && a == other.a && b == other.b;
}

@override
Expand Down
8 changes: 5 additions & 3 deletions runtime/Dart/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* can be found in the LICENSE.txt file in the project root.
*/

import 'dart:io';

import 'atn/atn.dart';
import 'error/error.dart';
import 'input_stream.dart';
Expand All @@ -19,6 +17,10 @@ import 'token_factory.dart';
import 'token_stream.dart';
import 'tree/tree.dart';

import 'util/platform_stub.dart'
if (dart.library.io) 'util/platform_io.dart'
if (dart.library.html) 'util/platform_html.dart';

/// This is all the parsing support code essentially; most of it is error recovery stuff. */
abstract class Parser extends Recognizer<ParserATNSimulator> {
/// This field maps from the serialized ATN string to the deserialized [ATN] with
Expand Down Expand Up @@ -723,7 +725,7 @@ abstract class Parser extends Recognizer<ParserATNSimulator> {
if (dfa.states.isNotEmpty) {
if (seenOne) print('');
print('Decision ${dfa.decision}:');
stdout.write(dfa.toString(vocabulary));
stdoutWrite(dfa.toString(vocabulary));
seenOne = true;
}
}
Expand Down
Loading