Search in sources :

Example 1 with PSTLatexdrawListener

use of net.sf.latexdraw.parser.pst.PSTLatexdrawListener in project latexdraw by arnobl.

the class InsertPSTCode method doCmdBody.

@Override
protected void doCmdBody() {
    final PSTLatexdrawListener listener = new PSTLatexdrawListener();
    final PSTLexer lexer = new PSTLexer(CharStreams.fromString(code));
    final PSTParser parser = new PSTParser(new CommonTokenStream(lexer));
    parser.addParseListener(listener);
    parser.pstCode(new PSTContext());
    final Group group = ShapeFactory.INST.createGroup();
    group.getShapes().addAll(listener.flatShapes());
    if (!group.isEmpty()) {
        final Shape sh = group.size() > 1 ? group : group.getShapeAt(0).orElseThrow();
        final Point tl = sh.getTopLeftPoint();
        final double tx = tl.getX() < 0d ? -tl.getX() + 50d : 0d;
        final double ty = tl.getY() < 0d ? -tl.getY() + 50d : 0d;
        shapes = Optional.of(sh);
        sh.translate(tx, ty);
        redo();
        statusBar.setText(lang.getString("codeConverted"));
    }
    parser.getInterpreter().clearDFA();
    lexer.getInterpreter().clearDFA();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Group(net.sf.latexdraw.model.api.shape.Group) PSTLexer(net.sf.latexdraw.parser.pst.PSTLexer) Shape(net.sf.latexdraw.model.api.shape.Shape) PSTLatexdrawListener(net.sf.latexdraw.parser.pst.PSTLatexdrawListener) PSTContext(net.sf.latexdraw.parser.pst.PSTContext) Point(net.sf.latexdraw.model.api.shape.Point) PSTParser(net.sf.latexdraw.parser.pst.PSTParser)

Example 2 with PSTLatexdrawListener

use of net.sf.latexdraw.parser.pst.PSTLatexdrawListener in project latexdraw by arnobl.

the class CodeInserter method initialize.

@Override
public void initialize(final URL location, final ResourceBundle resources) {
    label.setText(lang.getString("PSTParserLimits"));
    // Collecting errors from the parser.
    final ANTLRErrorListener errorListener = new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
            // NON-NLS
            errorLog.setText(errorLog.getText() + "Syntax error: " + msg + SystemUtils.getInstance().eol);
        }
    };
    final PSTLatexdrawListener listener = new PSTLatexdrawListener() {

        @Override
        public void exitUnknowncmds(final PSTParser.UnknowncmdsContext ctx) {
            // NON-NLS
            errorLog.setText(errorLog.getText() + "Unknown command: " + ctx.LATEXCMD().getSymbol().getText() + SystemUtils.getInstance().eol);
        }

        @Override
        public void enterUnknownParamSetting(final PSTParser.UnknownParamSettingContext ctx) {
            // NON-NLS
            errorLog.setText(errorLog.getText() + "Unknown parameter: " + ctx.name.getText() + SystemUtils.getInstance().eol);
        }

        @Override
        public void visitErrorNode(final ErrorNode node) {
            // NON-NLS
            errorLog.setText(errorLog.getText() + "Error: " + node.getText() + SystemUtils.getInstance().eol);
        }

        @Override
        public void exitText(final PSTParser.TextContext ctx) {
            super.exitText(ctx);
            if (ctx.getText().startsWith("\\")) {
                // NON-NLS
                errorLog.setText(errorLog.getText() + "Bad command: '" + ctx.getText() + "'?" + SystemUtils.getInstance().eol);
            }
        }
    };
    listener.log.addHandler(new Handler() {

        @Override
        public void publish(final LogRecord record) {
            errorLog.setText(errorLog.getText() + record.getMessage() + SystemUtils.getInstance().eol);
        }

        @Override
        public void flush() {
        // Nothing to flush
        }

        @Override
        public void close() {
        // Nothing to close
        }
    });
    // On each text change, the code is parsed and errors reported.
    text.textProperty().addListener((observable, oldValue, newValue) -> {
        errorLog.setText("");
        final PSTLexer lexer = new PSTLexer(CharStreams.fromString(newValue));
        lexer.addErrorListener(errorListener);
        final PSTParser parser = new PSTParser(new CommonTokenStream(lexer));
        parser.addParseListener(listener);
        parser.addErrorListener(errorListener);
        parser.pstCode(new PSTContext());
        parser.getInterpreter().clearDFA();
        lexer.getInterpreter().clearDFA();
    });
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PSTLexer(net.sf.latexdraw.parser.pst.PSTLexer) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) PSTLatexdrawListener(net.sf.latexdraw.parser.pst.PSTLatexdrawListener) Handler(java.util.logging.Handler) PSTContext(net.sf.latexdraw.parser.pst.PSTContext) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) PSTParser(net.sf.latexdraw.parser.pst.PSTParser) Recognizer(org.antlr.v4.runtime.Recognizer) LogRecord(java.util.logging.LogRecord) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

PSTContext (net.sf.latexdraw.parser.pst.PSTContext)2 PSTLatexdrawListener (net.sf.latexdraw.parser.pst.PSTLatexdrawListener)2 PSTLexer (net.sf.latexdraw.parser.pst.PSTLexer)2 PSTParser (net.sf.latexdraw.parser.pst.PSTParser)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 Handler (java.util.logging.Handler)1 LogRecord (java.util.logging.LogRecord)1 Group (net.sf.latexdraw.model.api.shape.Group)1 Point (net.sf.latexdraw.model.api.shape.Point)1 Shape (net.sf.latexdraw.model.api.shape.Shape)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 Recognizer (org.antlr.v4.runtime.Recognizer)1 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)1