Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SpecParser.java
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5import java.io.*;
6import java.util.*;
7import org.antlr.v4.runtime.*;
8import org.antlr.v4.runtime.dfa.*;
9import org.antlr.v4.runtime.atn.*;
10
15
16class DartErrorListener implements ANTLRErrorListener {
17 public void reportAmbiguity(
18 Parser recognizer,
19 DFA dfa,
20 int startIndex,
21 int stopIndex,
22 boolean exact,
23 BitSet ambigAlts,
24 ATNConfigSet configs) {}
25
27 Parser recognizer,
28 DFA dfa,
29 int startIndex,
30 int stopIndex,
31 BitSet conflictingAlts,
32 ATNConfigSet configs) {}
33
35 Parser recognizer,
36 DFA dfa,
37 int startIndex,
38 int stopIndex,
39 int prediction,
40 ATNConfigSet configs) {}
41
42 public void syntaxError(
43 Recognizer<?,?> recognizer,
44 Object offendingSymbol,
45 int line,
46 int charPositionInLine,
47 String msg,
48 RecognitionException e) {
49 if (!DartParser.errorHasOccurred) DartParser.prepareForErrors();
50 }
51}
52
53/// Class for `main` which will parse files given as command line arguments.
54public class SpecParser {
55 private static void normalExit() {
56 // Terminate with exit code indicating normal termination.
57 System.exit(0);
58 }
59
60 private static void compileTimeErrorExit() {
61 // Terminate with exit code indicating a parse error.
62 System.exit(245);
63 }
64
65 private static void helpAndExit() {
66 System.err.println("Expected arguments: [--verbose] <file>...");
67 normalExit();
68 }
69
70 /// Receive command lines from standard input and produce feedback about
71 /// the outcome on standard output and standard error, as expected by
72 /// tools/test.py when running with `--batch`.
73 public static void runAsBatch() throws IOException, RecognitionException {
75 long startTime = System.currentTimeMillis();
76 InputStreamReader input = new InputStreamReader(System.in);
77 BufferedReader bufferedInput = new BufferedReader(input);
78 String cmdLine;
79
80 System.out.println(">>> BATCH START");
81 while ((cmdLine = bufferedInput.readLine()) != null) {
82 if (cmdLine.length() == 0) {
83 System.out.println(
84 ">>> BATCH END (" +
85 (result.numberOfFileArguments - result.numberOfFailures) +
86 "/" + result.numberOfFileArguments + ") " +
87 (System.currentTimeMillis() - startTime) + "ms");
88 if (result.numberOfFailures > 0) compileTimeErrorExit();
89 normalExit();
90 }
91
92 String[] lineArgs = cmdLine.split("\\s+");
93 result = parseFiles(lineArgs);
94 // Write stderr end token and flush.
95 System.err.println(">>> EOF STDERR");
96 String resultPassString =
97 result.numberOfFailures == 0 ? "PASS" : "PARSE_FAIL";
98 System.out.println(">>> TEST " + resultPassString + " " +
99 (System.currentTimeMillis() - startTime) + "ms");
100 startTime = System.currentTimeMillis();
101 }
102 }
103
104 /// From [arguments], obey the flags ("--<flag_name>", "-<any>") if known and
105 /// ignore them if unknown; treat the remaining [arguments] as file paths and
106 /// parse each of them. Return a [ParsingResult] specifying how many files
107 /// were parsed, and how many of them failed to parse.
108 private static ParsingResult parseFiles(String[] arguments)
109 throws IOException, RecognitionException {
111 boolean verbose = false;
112
113 result.numberOfFileArguments = arguments.length;
114 for (int i = 0; i < arguments.length; i++) {
115 String filePath = arguments[i];
116 if (filePath.startsWith("-")) {
117 result.numberOfFileArguments--;
118 if (result.numberOfFileArguments == 0) return result;
119 if (filePath.equals("--verbose")) verbose = true;
120 if (filePath.equals("--batch")) runAsBatch();
121 // Ignore all other flags.
122 continue;
123 }
124 if (verbose) System.err.println("Parsing file: " + filePath);
125 DartLexer lexer = new DartLexer(new ANTLRFileStream(filePath));
126 DartParser parser = new DartParser(new CommonTokenStream(lexer));
127 ANTLRErrorListener errorListener = new DartErrorListener();
128 lexer.addErrorListener(errorListener);
129 parser.addErrorListener(errorListener);
130 if (!parser.parseLibrary(filePath)) result.numberOfFailures++;
131 }
132 return result;
133 }
134
135 public static void main(String[] args) throws Exception {
136 if (args.length == 0) helpAndExit();
137 ParsingResult result = parseFiles(args);
138
139 if (result.numberOfFileArguments == 0) {
140 helpAndExit();
141 } else if (result.numberOfFileArguments == 1) {
142 if (result.numberOfFailures > 0) {
143 System.err.println("Parsing failed");
144 compileTimeErrorExit();
145 } else {
146 System.out.println("Parsing succeeded.");
147 }
148 } else {
149 if (result.numberOfFailures > 0) {
150 System.err.println(
151 "Parsed " + result.numberOfFileArguments + " files, " +
152 result.numberOfFailures + " failed.");
153 compileTimeErrorExit();
154 } else {
155 System.out.println("Parsed " + result.numberOfFileArguments +
156 " files successfully.");
157 }
158 }
159 }
160}
void syntaxError(Recognizer<?,?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)
void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs)
void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs)
void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs)
int numberOfFileArguments
Class for main which will parse files given as command line arguments.
static void main(String[] args)
static void runAsBatch()
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
parser
Definition zip.py:78
Definition DFA.h:17