Add a tools/spec_parse.py script.

This makes it possible to run the spec parser in a way which is a bit
more like the other tools that we have (e.g., tools/test.py):

  > tools/spec_parse.py tests/language/callable_test.dart

It still requires the developer to run `make parser` in
tools/spec_parser and hence does not run on a buildbot, but it's one
step forward.

Change-Id: I68ad6cea55bc02dddac21558acec33fc4bfc1981
Reviewed-on: https://dart-review.googlesource.com/9620
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Erik Ernst 2017-10-10 16:03:36 +00:00 committed by commit-bot@chromium.org
parent 9545cf6e2f
commit b61f1047d5
4 changed files with 92 additions and 23 deletions

View file

@ -25,14 +25,22 @@ import java.util.Stack;
}
@parser::members {
public static String filePath = null;
public static boolean filePathHasBeenPrinted = true;
private String filePath = null;
private boolean errorHasOccurred = false;
/** Parse library, return true if success, false if errors occurred. */
public boolean parseLibrary(String filePath) throws RecognitionException {
this.filePath = filePath;
errorHasOccurred = false;
libraryDefinition();
return !errorHasOccurred;
}
// Grammar debugging friendly output, 'The Definitive ANTLR Reference', p247.
public String getErrorMessage(RecognitionException e, String[] tokenNames) {
List stack = getRuleInvocationStack(e, this.getClass().getName());
String msg = null;
if ( e instanceof NoViableAltException ) {
if (e instanceof NoViableAltException) {
NoViableAltException nvae = (NoViableAltException)e;
msg = "no viable alt; token=" + e.token +
" (decision=" + nvae.decisionNumber +
@ -42,9 +50,9 @@ import java.util.Stack;
else {
msg = super.getErrorMessage(e, tokenNames);
}
if (!filePathHasBeenPrinted) {
filePathHasBeenPrinted = true;
System.err.println(">>> Parse error in " + filePath + ":");
if (!errorHasOccurred) {
errorHasOccurred = true;
System.err.println("Parse error in " + filePath + ":");
}
return stack + " " + msg;
}

View file

@ -99,10 +99,6 @@ unbalanced_brace_test: Skip # Not yet supported.
unsigned_right_shift_test: Skip # Not yet supported (but this will be OK!).
unsupported_operators_test: Skip # Not yet supported.
variable_declaration_metadata_test: Skip # Not yet supported.
# Tests using generic methods.
generic_methods_unused_parameter_test: Skip # Not yet supported.
generic_methods_named_parameters_test: Skip # Not yet supported.
generic_function_typedef2_test: Skip # Not yet supported.
# Tests using assert in initializer list.

50
tools/spec_parse.py Executable file
View file

@ -0,0 +1,50 @@
#!/usr/bin/env python
#
# Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
# This script runs the parser which is generated using ANTLR 3 from
# docs/language/Dart.g. It relies on a certain environment and is hence
# usable locally where this environment can be obtained, but it may not be
# possible to run it, e.g., on build bots. The requirements are as follows:
#
# - `make parser` in spec_parser has been executed successfully.
# - A suitable JVM is in the PATH and may be executed as 'java'.
# - the ANTLR3 jar is available as /usr/share/java/antlr3-runtime.jar.
import os
import string
import subprocess
import sys
import utils
def Help(missing):
print('Execution of the spec parser failed. Missing: ' + missing)
print('Please read the comment near the top of spec_parse.py.\n')
sys.exit(1)
def Main():
args = sys.argv[1:]
tools_dir = os.path.dirname(os.path.realpath(__file__))
spec_parser_dir = os.path.join(tools_dir, 'spec_parser')
spec_parser_file = os.path.join(spec_parser_dir, 'SpecParser.class')
antlr_jar = '/usr/share/java/antlr3-runtime.jar'
class_path = string.join([spec_parser_dir, antlr_jar], ':')
command = ['java', '-cp', class_path, 'SpecParser'] + args
if not os.path.exists(antlr_jar): Help(antlr_jar)
if not os.path.exists(spec_parser_file): Help('"make parser" in spec_parser')
with utils.CoreDumpArchiver(args):
exit_code = subprocess.call(command)
utils.DiagnoseExitCode(exit_code, command)
return exit_code
if __name__ == '__main__':
sys.exit(Main())

View file

@ -6,27 +6,42 @@ import org.antlr.runtime.*;
/// Class for `main` which will parse files given as command line arguments.
public class SpecParser {
static boolean verbose = false;
private static void helpAndExit() {
System.err.println("Expected arguments: [--verbose] <file>...");
System.exit(1);
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Expected a file path as argument.");
System.exit(1);
}
boolean verbose = false;
int numberOfFileArguments = 0;
int numberOfFailures = 0;
if (args.length == 0) helpAndExit();
numberOfFileArguments = args.length;
for (int i = 0; i < args.length; i++) {
String filePath = args[i];
if (filePath.equals("--verbose")) {
verbose = true;
numberOfFileArguments--;
if (numberOfFileArguments == 0) helpAndExit();
continue;
}
CharStream charStream = new ANTLRFileStream(filePath);
DartLexer lexer = new DartLexer(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DartParser parser = new DartParser(tokens);
DartParser.filePath = filePath;
DartParser.filePathHasBeenPrinted = false;
if (verbose) System.err.println(">>> Parsing file: " + filePath);
parser.libraryDefinition();
if (verbose) System.err.println("Parsing file: " + filePath);
DartParser parser = new DartParser(new CommonTokenStream(
new DartLexer(new ANTLRFileStream(filePath))));
if (!parser.parseLibrary(filePath)) numberOfFailures++;
}
if (numberOfFailures > 0) {
System.err.println("Parsed " + numberOfFileArguments + " files, " +
numberOfFailures + " failing.");
System.exit(1);
} else {
if (numberOfFileArguments == 1) {
System.out.println("Parsed successfully.");
} else {
System.out.println("Parsed " + numberOfFileArguments +
" files successfully.");
}
}
}
}