use of kalang.antlr.KalangParser in project kalang by kasonyang.
the class AntlrTest method testParser.
@Test
public void testParser() throws IOException {
KalangParser parser = new KalangParser(this.createTokenStream());
parser.compilationUnit();
}
use of kalang.antlr.KalangParser in project kalang by kasonyang.
the class ErrorRecoverTest method testCode.
public void testCode(String code) {
KalangParser parser = new KalangParser(TokenStreamFactory.createTokenStream(code));
parser.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void recover(Parser recognizer, RecognitionException e) {
Token ot = e.getOffendingToken();
System.out.println("offending token:" + ot.getText());
IntervalSet exceptedTokens = e.getExpectedTokens();
RuleContext ctx = e.getCtx();
if (ctx != null) {
System.out.println("context:" + ctx.getClass().getName());
}
System.out.println("offending state:" + e.getOffendingState());
String excTks = exceptedTokens.toString(KalangLexer.VOCABULARY);
System.out.println("excepted:" + excTks);
super.recover(recognizer, e);
}
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
System.out.println("calling recover inline");
// IntervalSet exceptedTokens = recognizer.getExpectedTokens();
// if(exceptedTokens.contains(KalangLexer.SEMI)){
// Token curToken = recognizer.getCurrentToken();
// recognizer.getTokenFactory().create(
// new Pair(
// curToken.getTokenSource()
// ,
// ), lastErrorIndex, text, lastErrorIndex, lastErrorIndex, lastErrorIndex, lastErrorIndex, lastErrorIndex);
// }
Token v = super.recoverInline(recognizer);
if (v != null)
System.out.println("inserted token:" + v.getText());
return v;
}
});
parser.compilationUnit();
}
use of kalang.antlr.KalangParser in project kalang by kasonyang.
the class KalangCompiler method createParser.
@Override
public KalangParser createParser(CompilationUnit compilationUnit, CommonTokenStream tokenStream) {
KalangParser parser = new KalangParser(tokenStream);
parser.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Diagnosis diagnosis = new Diagnosis(compilationUnit.getCompileContext(), Diagnosis.Kind.ERROR, OffsetRangeHelper.getOffsetRange(e.getOffendingToken()), msg, compilationUnit.getSource());
diagnosisHandler.handleDiagnosis(diagnosis);
}
});
return parser;
}
use of kalang.antlr.KalangParser in project kalang by kasonyang.
the class AstBuilderFactory method createAstBuilder.
public static AstBuilder createAstBuilder(CompilationUnit source, TokenStream tokens) {
KalangParser p = new KalangParser(tokens);
AstBuilder sp = new AstBuilder(source, p);
p.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Token end = e.getOffendingToken();
Token start;
RuleContext ctx = e.getCtx();
if (ctx instanceof ParserRuleContext) {
start = ((ParserRuleContext) ctx).getStart();
} else {
start = end;
}
sp.getDiagnosisReporter().report(Diagnosis.Kind.ERROR, msg, start, end);
}
});
return sp;
}
Aggregations