Skip to content

Commit 09d5ae3

Browse files
committed
eol
1 parent becebc2 commit 09d5ae3

20 files changed

+2023
-2023
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import org.antlr.v4.runtime.*;
2-
3-
public class IndentationErrorListener extends BaseErrorListener {
4-
private boolean isFirstTime = true;
5-
6-
@Override
7-
public void syntaxError(Recognizer<?, ?> recognizer,
8-
Object offendingSymbol,
9-
int line, int charPositionInLine,
10-
String msg,
11-
RecognitionException e) {
12-
13-
if (isFirstTime) {
14-
isFirstTime = false;
15-
System.out.println();
16-
System.err.println("ERROR:");
17-
}
18-
19-
if (msg.startsWith(LexerWithIndentDedentInjector.TEXT_LEXER)) { // this is a custom error message from the lexer contained a pattern
20-
System.err.println(msg.substring(LexerWithIndentDedentInjector.TEXT_LEXER.length())); // displaying the lexer error message without the pattern
21-
} else { // this is a parser error message
22-
String startOfMessage = "line " + line + ":" + charPositionInLine + "\t ";
23-
if (msg.startsWith("missing INDENT")) {
24-
System.err.println(startOfMessage + "IndentationError: expected an indented block"); // displaying the modified parser error message
25-
} else if (msg.startsWith("extraneous input '<" + LexerWithIndentDedentInjector.TEXT_INSERTED_INDENT)) {
26-
System.err.println(startOfMessage + "IndentationError: unexpected indent"); // displaying the modified parser error message
27-
} else {
28-
System.err.println(startOfMessage + "at " + offendingSymbol + ": " + msg); // displaying the original parser error message
29-
}
30-
}
31-
}
1+
import org.antlr.v4.runtime.*;
2+
3+
public class IndentationErrorListener extends BaseErrorListener {
4+
private boolean isFirstTime = true;
5+
6+
@Override
7+
public void syntaxError(Recognizer<?, ?> recognizer,
8+
Object offendingSymbol,
9+
int line, int charPositionInLine,
10+
String msg,
11+
RecognitionException e) {
12+
13+
if (isFirstTime) {
14+
isFirstTime = false;
15+
System.out.println();
16+
System.err.println("ERROR:");
17+
}
18+
19+
if (msg.startsWith(LexerWithIndentDedentInjector.TEXT_LEXER)) { // this is a custom error message from the lexer contained a pattern
20+
System.err.println(msg.substring(LexerWithIndentDedentInjector.TEXT_LEXER.length())); // displaying the lexer error message without the pattern
21+
} else { // this is a parser error message
22+
String startOfMessage = "line " + line + ":" + charPositionInLine + "\t ";
23+
if (msg.startsWith("missing INDENT")) {
24+
System.err.println(startOfMessage + "IndentationError: expected an indented block"); // displaying the modified parser error message
25+
} else if (msg.startsWith("extraneous input '<" + LexerWithIndentDedentInjector.TEXT_INSERTED_INDENT)) {
26+
System.err.println(startOfMessage + "IndentationError: unexpected indent"); // displaying the modified parser error message
27+
} else {
28+
System.err.println(startOfMessage + "at " + offendingSymbol + ": " + msg); // displaying the original parser error message
29+
}
30+
}
31+
}
3232
}

python/python3-without-actions/src/LexerWithIndentDedentInjector.java

+222-222
Large diffs are not rendered by default.
+41-41
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
import org.antlr.v4.runtime.*;
2-
3-
public class Main {
4-
public static void main(String[] args) throws Exception {
5-
var input = CharStreams.fromFileName(args[0]);
6-
var lexer = new LexerWithIndentDedentInjector(input);
7-
var tokens = new CommonTokenStream(lexer);
8-
var parser = new Python3Parser(tokens);
9-
10-
System.out.println(input.toString()); // displaying the input source code
11-
12-
System.out.println();
13-
tokens.fill(); // explicit loading all tokens from the lexer until the EOF
14-
for (Token t : tokens.getTokens()) {
15-
System.out.println(getTokenMetaDataWithRuleName(parser, t)); // displaying a token metadata
16-
}
17-
18-
parser.removeErrorListeners();
19-
parser.addErrorListener(new IndentationErrorListener());
20-
for (String errMsg : lexer.getErrorMessages()) { // adding lexer error messages before the parser error messages
21-
parser.notifyErrorListeners(errMsg);
22-
}
23-
24-
parser.file_input(); // begin the parsing at the file_input rule and displaying the lexer and the parser error messages
25-
26-
if (lexer.getWarnings().size() > 0) {
27-
System.err.println();
28-
System.err.println("WARNING:");
29-
for (String w : lexer.getWarnings()) {
30-
System.err.println(w); // displaying a warning
31-
}
32-
}
33-
}
34-
35-
private static String getTokenMetaDataWithRuleName(Python3Parser parser, Token t) {
36-
final String metaData = t.toString(); // original format: [@TokenIndex,startIndex:stopIndex='text',<TokenType>,line:charPositionInLine]
37-
final int greaterPos = metaData.lastIndexOf(">,");
38-
return metaData.substring(0, greaterPos) + // modified format: [@TokenIndex,startIndex:stopIndex='text',<TokenType is RuleName>,line:charPositionInLine]
39-
" is " + parser.getVocabulary().getDisplayName(t.getType()) +
40-
metaData.substring(greaterPos);
41-
}
1+
import org.antlr.v4.runtime.*;
2+
3+
public class Main {
4+
public static void main(String[] args) throws Exception {
5+
var input = CharStreams.fromFileName(args[0]);
6+
var lexer = new LexerWithIndentDedentInjector(input);
7+
var tokens = new CommonTokenStream(lexer);
8+
var parser = new Python3Parser(tokens);
9+
10+
System.out.println(input.toString()); // displaying the input source code
11+
12+
System.out.println();
13+
tokens.fill(); // explicit loading all tokens from the lexer until the EOF
14+
for (Token t : tokens.getTokens()) {
15+
System.out.println(getTokenMetaDataWithRuleName(parser, t)); // displaying a token metadata
16+
}
17+
18+
parser.removeErrorListeners();
19+
parser.addErrorListener(new IndentationErrorListener());
20+
for (String errMsg : lexer.getErrorMessages()) { // adding lexer error messages before the parser error messages
21+
parser.notifyErrorListeners(errMsg);
22+
}
23+
24+
parser.file_input(); // begin the parsing at the file_input rule and displaying the lexer and the parser error messages
25+
26+
if (lexer.getWarnings().size() > 0) {
27+
System.err.println();
28+
System.err.println("WARNING:");
29+
for (String w : lexer.getWarnings()) {
30+
System.err.println(w); // displaying a warning
31+
}
32+
}
33+
}
34+
35+
private static String getTokenMetaDataWithRuleName(Python3Parser parser, Token t) {
36+
final String metaData = t.toString(); // original format: [@TokenIndex,startIndex:stopIndex='text',<TokenType>,line:charPositionInLine]
37+
final int greaterPos = metaData.lastIndexOf(">,");
38+
return metaData.substring(0, greaterPos) + // modified format: [@TokenIndex,startIndex:stopIndex='text',<TokenType is RuleName>,line:charPositionInLine]
39+
" is " + parser.getVocabulary().getDisplayName(t.getType()) +
40+
metaData.substring(greaterPos);
41+
}
4242
}

0 commit comments

Comments
 (0)