Search in sources :

Example 51 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.

the class JQLChecker method prepareParser.

protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final JQLContext jqlContext, final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, jqlContext.getContextDescription() + ": unespected char at pos %s of SQL '%s'", charPositionInLine, jql);
        }
    });
    ParserRuleContext context = parser.parse();
    return new Pair<>(context, tokens);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) JqlLexer(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlLexer) JqlParser(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser) RecognitionException(org.antlr.v4.runtime.RecognitionException) Pair(com.abubusoft.kripton.common.Pair)

Example 52 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.

the class JQLChecker method prepareVariableStatement.

/**
 * <p>
 * Parse the variable parts of a SQL:
 * </p>
 *
 * <ul>
 * <li>where_stmt</li>
 * <li>group_stmt</li>
 * <li>having_stmt</li>
 * <li>order_stmt</li>
 * <li>limit_stmt</li>
 * <li>offset_stmt</li>
 * </ul>
 *
 * @param jql
 * @return
 */
protected Pair<ParserRuleContext, CommonTokenStream> prepareVariableStatement(final JQLContext jqlContext, final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, jqlContext.getContextDescription() + ": unespected char at pos %s of JQL '%s'", charPositionInLine, jql);
        }
    });
    ParserRuleContext context = parser.parse_variable();
    return new Pair<>(context, tokens);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) JqlLexer(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlLexer) JqlParser(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser) RecognitionException(org.antlr.v4.runtime.RecognitionException) Pair(com.abubusoft.kripton.common.Pair)

Example 53 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project beast2 by CompEvol.

the class TreeParser method parseNewick.

/**
 * Parse a newick-ish string and generate the BEAST tree it describes.
 *
 * @param newick string to parse
 * @return root node of tree
 */
public Node parseNewick(String newick) {
    CharStream charStream = CharStreams.fromString(newick);
    // Custom parse/lexer error listener
    BaseErrorListener errorListener = new BaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new TreeParsingException(msg, charPositionInLine, line);
        }
    };
    // Use lexer to produce token stream
    NewickLexer lexer = new NewickLexer(charStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Parse token stream to produce parse tree
    NewickParser parser = new NewickParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    ParseTree parseTree = parser.tree();
    // Traverse parse tree, constructing BEAST tree along the way
    NewickASTVisitor visitor = new NewickASTVisitor();
    return visitor.visit(parseTree);
}
Also used : NewickLexer(beast.util.treeparser.NewickLexer) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NewickParser(beast.util.treeparser.NewickParser)

Example 54 with Recognizer

use of org.antlr.v4.runtime.Recognizer 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 55 with Recognizer

use of org.antlr.v4.runtime.Recognizer 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

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)24 Token (org.antlr.v4.runtime.Token)22 RecognitionException (org.antlr.v4.runtime.RecognitionException)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)15 File (java.io.File)11 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 ATNState (org.antlr.v4.runtime.atn.ATNState)9 IOException (java.io.IOException)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)8 Parser (org.antlr.v4.runtime.Parser)8 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)8 ArrayList (java.util.ArrayList)7 ATN (org.antlr.v4.runtime.atn.ATN)6 Pair (com.abubusoft.kripton.common.Pair)5 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)5 TokenStream (org.antlr.v4.runtime.TokenStream)5 BeetlException (org.beetl.core.exception.BeetlException)5 STGroupString (org.stringtemplate.v4.STGroupString)5 CommonToken (org.antlr.v4.runtime.CommonToken)4