Search in sources :

Example 1 with BookishLexer

use of us.parr.bookish.parse.BookishLexer in project bookish by parrt.

the class Tool method parseChapter.

public Pair<BookishParser.DocumentContext, BookishParser> parseChapter(String inputDir, String inputFilename, int chapNumber) throws IOException {
    CharStream input = CharStreams.fromFileName(inputDir + "/" + inputFilename);
    BookishLexer lexer = new BookishLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    BookishParser parser = new BookishParser(tokens, inputFilename, chapNumber);
    BookishParser.DocumentContext doctree = parser.document();
    return new Pair<>(doctree, parser);
}
Also used : BookishLexer(us.parr.bookish.parse.BookishLexer) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream) BookishParser(us.parr.bookish.parse.BookishParser) Pair(org.antlr.v4.runtime.misc.Pair)

Example 2 with BookishLexer

use of us.parr.bookish.parse.BookishLexer 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);
    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 : BookishLexer(us.parr.bookish.parse.BookishLexer) 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) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ModelConverter(us.parr.bookish.translate.ModelConverter) BookishParser(us.parr.bookish.parse.BookishParser)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 BookishLexer (us.parr.bookish.parse.BookishLexer)2 BookishParser (us.parr.bookish.parse.BookishParser)2 Method (java.lang.reflect.Method)1 Pair (org.antlr.v4.runtime.misc.Pair)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 ST (org.stringtemplate.v4.ST)1 OutputModelObject (us.parr.bookish.model.OutputModelObject)1 ModelConverter (us.parr.bookish.translate.ModelConverter)1