Search in sources :

Example 11 with ConsoleErrorListener

use of org.antlr.v4.runtime.ConsoleErrorListener in project bookish by parrt.

the class Tool method translateString.

public String translateString(Translator trans, String markdown, String startRule) throws Exception {
    CharStream input = CharStreams.fromString(markdown);
    BookishLexer lexer = new BookishLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    BookishParser parser = new BookishParser(tokens, null, 0);
    parser.removeErrorListeners();
    parser.addErrorListener(new ConsoleErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            msg = "Parsing author string: " + msg;
            super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
        }
    });
    Method startMethod = BookishParser.class.getMethod(startRule, (Class[]) null);
    ParseTree doctree = (ParseTree) startMethod.invoke(parser, (Object[]) null);
    // get single chapter
    OutputModelObject omo = trans.visit(doctree);
    ModelConverter converter = new ModelConverter(trans.templates);
    ST outputST = converter.walk(omo);
    return outputST.render();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ST(org.stringtemplate.v4.ST) OutputModelObject(us.parr.bookish.model.OutputModelObject) Method(java.lang.reflect.Method) CharStream(org.antlr.v4.runtime.CharStream) ModelConverter(us.parr.bookish.translate.ModelConverter) BookishParser(us.parr.bookish.parse.BookishParser) BookishLexer(us.parr.bookish.parse.BookishLexer) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) JsonObject(javax.json.JsonObject) OutputModelObject(us.parr.bookish.model.OutputModelObject) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)9 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)8 StringReader (java.io.StringReader)7 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 Reader (java.io.Reader)5 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)5 TitanListener (org.eclipse.titan.common.parsers.TitanListener)5 IOException (java.io.IOException)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)3 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 IFileInfo (org.eclipse.core.filesystem.IFileInfo)2 IFileStore (org.eclipse.core.filesystem.IFileStore)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 SyntacticErrorStorage (org.eclipse.titan.common.parsers.SyntacticErrorStorage)2 ASN1Listener (org.eclipse.titan.designer.parsers.asn1parser.ASN1Listener)2 Asn1Lexer (org.eclipse.titan.designer.parsers.asn1parser.Asn1Lexer)2