Search in sources :

Example 11 with Triple

use of org.antlr.v4.runtime.misc.Triple in project antlr4 by antlr.

the class ParserATNFactory method optional.

/**
 * From {@code (A)?} build either:
 *
 * <pre>
 *  o--A-&gt;o
 *  |     ^
 *  o----&gt;|
 * </pre>
 *
 * or, if {@code A} is a block, just add an empty alt to the end of the
 * block
 */
@Override
public Handle optional(GrammarAST optAST, Handle blk) {
    BlockStartState blkStart = (BlockStartState) blk.left;
    ATNState blkEnd = blk.right;
    preventEpsilonOptionalBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
    boolean greedy = ((QuantifierAST) optAST).isGreedy();
    blkStart.nonGreedy = !greedy;
    epsilon(blkStart, blk.right, !greedy);
    optAST.atnState = blk.left;
    return blk;
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) StarBlockStartState(org.antlr.v4.runtime.atn.StarBlockStartState) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 12 with Triple

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

the class Translator method visitLatex.

@Override
public OutputModelObject visitLatex(BookishParser.LatexContext ctx) {
    String text = ctx.getText().trim();
    // \latex{{...}}
    List<String> stuff = extract(latexPattern, text);
    text = stuff.get(0);
    if (target == Tool.Target.LATEX || target == Tool.Target.LATEX_BOOK) {
        return new Latex(this, null, text, text);
    }
    String relativePath = "images/latex-" + md5hash(text) + ".svg";
    String src = outputDir + "/" + relativePath;
    Path outpath = Paths.get(src);
    if (!Files.exists(outpath)) {
        Triple<String, Float, Float> results = texConverter.tex2svg(text, LATEX, BLOCK_EQN_FONT_SIZE);
        String svg = results.a;
        try {
            System.out.println(outpath);
            Files.write(outpath, svg.getBytes());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return new Latex(this, relativePath, text, text);
}
Also used : Path(java.nio.file.Path) XPath(org.antlr.v4.runtime.tree.xpath.XPath) IOException(java.io.IOException) Latex(us.parr.bookish.model.Latex)

Example 13 with Triple

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

the class Translator method visitBlock_eqn.

@Override
public OutputModelObject visitBlock_eqn(BookishParser.Block_eqnContext ctx) {
    String eqn = stripQuotes(ctx.getText(), 3);
    if (target == Tool.Target.LATEX || target == Tool.Target.LATEX_BOOK) {
        return new BlockEquation(this, null, eqn);
    }
    String relativePath = "images/blkeqn-" + md5hash(eqn) + ".svg";
    String src = outputDir + "/" + relativePath;
    Path outpath = Paths.get(src);
    if (!Files.exists(outpath)) {
        Triple<String, Float, Float> results = texConverter.tex2svg(eqn, BLOCKEQN, BLOCK_EQN_FONT_SIZE);
        String svg = results.a;
        try {
            System.out.println(outpath);
            Files.write(outpath, svg.getBytes());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return new BlockEquation(this, relativePath, eqn);
}
Also used : BlockEquation(us.parr.bookish.model.BlockEquation) Path(java.nio.file.Path) XPath(org.antlr.v4.runtime.tree.xpath.XPath) IOException(java.io.IOException)

Aggregations

Token (org.antlr.v4.runtime.Token)4 ATNState (org.antlr.v4.runtime.atn.ATNState)4 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)4 Rule (org.antlr.v4.tool.Rule)4 File (java.io.File)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)3 Triple (org.antlr.v4.runtime.misc.Triple)3 XPath (org.antlr.v4.runtime.tree.xpath.XPath)3 STGroupFile (org.stringtemplate.v4.STGroupFile)3 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)2 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)2 PlusBlockStartState (org.antlr.v4.runtime.atn.PlusBlockStartState)2 StarBlockStartState (org.antlr.v4.runtime.atn.StarBlockStartState)2 BlockAST (org.antlr.v4.tool.ast.BlockAST)2 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)2 ST (org.stringtemplate.v4.ST)2