Skip to content

Commit

Permalink
add ParseError
Browse files Browse the repository at this point in the history
  • Loading branch information
alingse committed Dec 8, 2024
1 parent b0ee026 commit 21c98e8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
102 changes: 95 additions & 7 deletions java/src/main/java/thriftlabs/thriftparser/Thrift.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,128 @@

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.CharStream;

public class Thrift {
public static final int CommentChannel = 2; // 定义评论通道的常量

public static ParserResult parse(CharStream stream) {
ParseErrorListener listener = new ParseErrorListener();
ThriftLexer lexer = new ThriftLexer(stream);
lexer.addErrorListener(listener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ThriftParser parser = new ThriftParser(tokens);
parser.addErrorListener(listener);
ThriftParser.DocumentContext document = parser.document();
return new ParserResult(lexer, tokens, parser, document);
List<ParseError> errors = listener.getErrors();
return new ParserResult(lexer, tokens, parser, document, errors);
}

public static ParserResult parse(String content) {
CharStream stream = CharStreams.fromString(content);
ThriftLexer lexer = new ThriftLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ThriftParser parser = new ThriftParser(tokens);
ThriftParser.DocumentContext document = parser.document();
return new ParserResult(lexer, tokens, parser, document);
return parse(stream);
}

public static class ParserResult {
public ThriftLexer lexer;
public CommonTokenStream tokens;
public ThriftParser parser;
public ThriftParser.DocumentContext document;
public List<ParseError> errors;

public ParserResult(ThriftLexer lexer, CommonTokenStream tokens, ThriftParser parser,
ThriftParser.DocumentContext document) {
ThriftParser.DocumentContext document, List<ParseError> errors) {
this.lexer = lexer;
this.tokens = tokens;
this.parser = parser;
this.document = document;
this.errors = errors;
}

public boolean isSuccess() {
return this.errors.isEmpty();
}
}

public static class ParseError {
private final int line;
private final int charPositionInLine;
private final String offendingText;
private final String message;

public ParseError(int line, int charPositionInLine, String offendingText, String message) {
this.line = line;
this.charPositionInLine = charPositionInLine;
this.offendingText = offendingText;
this.message = message;
}

public int getLine() {
return line;
}

public int getCharPositionInLine() {
return charPositionInLine;
}

public String getOffendingText() {
return offendingText;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return "ParseError{" +
"line=" + line +
", charPositionInLine=" + charPositionInLine +
", offendingText='" + offendingText + '\'' +
", message='" + message + '\'' +
'}';
}
}

public static class ParseErrorListener implements ANTLRErrorListener {
private final List<ParseError> errors = new ArrayList<>();

public List<ParseError> getErrors() {
return errors;
}

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
ParseError error = new ParseError(line, charPositionInLine, offendingSymbol.toString(), msg);
this.errors.add(error);
}

@Override
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
BitSet ambigAlts, ATNConfigSet configs) {
}

@Override
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
BitSet conflictingAlts, ATNConfigSet configs) {
}

@Override
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction,
ATNConfigSet configs) {
}

}
}
10 changes: 10 additions & 0 deletions java/src/test/java/thriftlabs/thriftparser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.antlr.v4.runtime.tree.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

Expand Down Expand Up @@ -41,5 +44,12 @@ public void testThrift() {
Thrift.ParserResult td = Thrift.parse(content);
List<ThriftParser.HeaderContext> headers = td.document.header();
assertEquals(headers.size(), 4);
assertTrue(td.isSuccess());
}

@Test
public void testFailThrift() {
Thrift.ParserResult td = Thrift.parse("123-1");
assertFalse(td.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import thriftlabs.thriftparser.Thrift;
import thriftlabs.thriftparser.ThriftParser;
Expand All @@ -24,6 +25,7 @@ public class ParseIntegrationTest {
@Test
public void testThrift() {
Thrift.ParserResult td = Thrift.parse(content);
assertTrue(td.isSuccess());
List<ThriftParser.HeaderContext> headers = td.document.header();
assertEquals(headers.size(), 4);
}
Expand Down

0 comments on commit 21c98e8

Please sign in to comment.