use of net.sf.latexdraw.parsers.pst.PSTLatexdrawListener 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());
});
}
use of net.sf.latexdraw.parsers.pst.PSTLatexdrawListener 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();
}
Aggregations