Skip to content

Example: Adding a parser for syntax checking

Peter Froud edited this page May 24, 2023 · 2 revisions

Overview steps:

  1. Create an implementation of the Parser interface. To make it easy, use AbstractParser as a base class.
  2. Then you'll need to write the parse() method, which returns a ParseResult. To make it easy, use DefaultParseResult as a base class.
  3. The ParseResult should contain some ParserNotices. If you use DefaultParserNotice as a base class, then you just need to specify a line number and a message.
  4. Call addParser on the text area.

Example code:

class ExampleParser extends AbstractParser {
    @Override
    public ParseResult parse(RSyntaxDocument document, String style) {
        DefaultParseResult result = new DefaultParseResult(this);
        result.addNotice(new DefaultParserNotice(this, "Message", 4));
        return result;
    }
}
textArea.addParser(new ExampleParser());

A red squiggle underline will appear on line 4:

image

And a tooltip will appear when you move your mouse over the line:

image