Search in sources :

Example 1 with PSTContext

use of net.sf.latexdraw.parsers.pst.PSTContext in project latexdraw by arnobl.

the class CodeInserter method initialize.

@Override
public void initialize(final URL location, final ResourceBundle resources) {
    label.setText(LangTool.INSTANCE.getBundle().getString("LaTeXDrawFrame.16"));
    // 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 + LSystem.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() + LSystem.EOL);
        }

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

        @Override
        public void visitErrorNode(final ErrorNode node) {
            // NON-NLS
            errorLog.setText(errorLog.getText() + "Error: " + node.getText() + LSystem.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() + "'?" + LSystem.EOL);
            }
        }
    };
    listener.log.addHandler(new Handler() {

        @Override
        public void publish(final LogRecord record) {
            errorLog.setText(errorLog.getText() + record.getMessage() + LSystem.EOL);
        }

        @Override
        public void flush() {
        }

        @Override
        public void 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());
    });
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PSTLexer(net.sf.latexdraw.parsers.pst.PSTLexer) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) PSTLatexdrawListener(net.sf.latexdraw.parsers.pst.PSTLatexdrawListener) Handler(java.util.logging.Handler) PSTContext(net.sf.latexdraw.parsers.pst.PSTContext) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) PSTParser(net.sf.latexdraw.parsers.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)

Example 2 with PSTContext

use of net.sf.latexdraw.parsers.pst.PSTContext in project latexdraw by arnobl.

the class InsertPSTCode method doCmdBody.

@Override
protected void doCmdBody() {
    try {
        final PSTLatexdrawListener listener = new PSTLatexdrawListener();
        final PSTParser parser = new PSTParser(new CommonTokenStream(new PSTLexer(CharStreams.fromString(code))));
        parser.addParseListener(listener);
        parser.pstCode(new PSTContext());
        final IGroup group = ShapeFactory.INST.createGroup();
        group.getShapes().addAll(listener.getShapes());
        if (!group.isEmpty()) {
            final IShape sh = group.size() > 1 ? group : group.getShapeAt(0);
            final IPoint 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();
            if (statusBar != null) {
                statusBar.setText(LangTool.INSTANCE.getBundle().getString("LaTeXDrawFrame.36"));
            }
        }
    } catch (final RecognitionException ex) {
        BadaboomCollector.INSTANCE.add(ex);
        if (statusBar != null) {
            statusBar.setText(LangTool.INSTANCE.getBundle().getString("LaTeXDrawFrame.34"));
        }
    }
    done();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PSTLexer(net.sf.latexdraw.parsers.pst.PSTLexer) PSTLatexdrawListener(net.sf.latexdraw.parsers.pst.PSTLatexdrawListener) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) PSTContext(net.sf.latexdraw.parsers.pst.PSTContext) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) RecognitionException(org.antlr.v4.runtime.RecognitionException) PSTParser(net.sf.latexdraw.parsers.pst.PSTParser)

Aggregations

PSTContext (net.sf.latexdraw.parsers.pst.PSTContext)2 PSTLatexdrawListener (net.sf.latexdraw.parsers.pst.PSTLatexdrawListener)2 PSTLexer (net.sf.latexdraw.parsers.pst.PSTLexer)2 PSTParser (net.sf.latexdraw.parsers.pst.PSTParser)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Handler (java.util.logging.Handler)1 LogRecord (java.util.logging.LogRecord)1 IGroup (net.sf.latexdraw.models.interfaces.shape.IGroup)1 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)1 IShape (net.sf.latexdraw.models.interfaces.shape.IShape)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)1 Recognizer (org.antlr.v4.runtime.Recognizer)1 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)1