Search in sources :

Example 1 with KalangParser

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();
}
Also used : KalangParser(kalang.antlr.KalangParser) Test(org.junit.Test)

Example 2 with KalangParser

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();
}
Also used : KalangParser(kalang.antlr.KalangParser) RuleContext(org.antlr.v4.runtime.RuleContext) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Token(org.antlr.v4.runtime.Token) RecognitionException(org.antlr.v4.runtime.RecognitionException) Parser(org.antlr.v4.runtime.Parser) KalangParser(kalang.antlr.KalangParser)

Example 3 with KalangParser

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;
}
Also used : KalangParser(kalang.antlr.KalangParser) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) AntlrErrorString(kalang.util.AntlrErrorString) RecognitionException(org.antlr.v4.runtime.RecognitionException) KalangParser(kalang.antlr.KalangParser) Parser(org.antlr.v4.runtime.Parser)

Example 4 with KalangParser

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;
}
Also used : AstBuilder(kalang.compiler.AstBuilder) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) KalangParser(kalang.antlr.KalangParser) RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) Token(org.antlr.v4.runtime.Token) RecognitionException(org.antlr.v4.runtime.RecognitionException) KalangParser(kalang.antlr.KalangParser) Parser(org.antlr.v4.runtime.Parser)

Aggregations

KalangParser (kalang.antlr.KalangParser)4 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)3 Parser (org.antlr.v4.runtime.Parser)3 RecognitionException (org.antlr.v4.runtime.RecognitionException)3 RuleContext (org.antlr.v4.runtime.RuleContext)2 Token (org.antlr.v4.runtime.Token)2 AstBuilder (kalang.compiler.AstBuilder)1 AntlrErrorString (kalang.util.AntlrErrorString)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)1 Test (org.junit.Test)1