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