[antlr-interest] Antlr syntax highlighting help
Vincente Cobra
vcobra at gmail.com
Fri Aug 11 17:05:59 PDT 2006
Hi all,
I am trying to add syntax highlighting to JEditorPane using Antlr in java.
The lexer doesn't seem to match the offset I want in the PlainView. The code
is based on what I have seen on the internet :
- The article Customizing a text editor in Sun web pages :
http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html
- The ostermiller syntax package : http://ostermiller.org/syntax/
- The skunkdav : http://skunkdav.sourceforge.net/
- visualemugen :
http://wwwseidl.informatik.tu-muenchen.de/projekte/visualemugen
Can you help me with the following code please?
Thanks in advance.
package editortest.editor;
import antlr.CharScanner;
import antlr.LexerSharedInputState;
import antlr.Token;
import editortest.swing.DocumentReader;
import editortest.syntax.html.HTMLLexer;
import editortest.syntax.html.HTMLTokenTypes;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Segment;
import javax.swing.text.Utilities;
import javax.swing.text.WrappedPlainView;
public class SyntaxView extends WrappedPlainView {
private DocumentReader reader;
private CharScanner lexer;
/**
*
* @param elem
*/
public SyntaxView(Element elem) {
super(elem, true);
try {
Document doc = elem.getDocument();
reader = new DocumentReader(doc);
lexer = new HTMLLexer(reader) {
public void newline() {
}
};
lexer.setColumn(1);
lexer.setTabSize(1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
*
* @param pos
*/
public void updateLexer(int pos) {
try {
Document doc = getDocument();
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(pos);
Element elem = root.getElement(line);
int p0 = elem.getStartOffset();
int p1 = elem.getEndOffset();
reader.update(p0, p1 - p0);
reader.seek((long) p0);
lexer.setInputState(new LexerSharedInputState(reader));
} catch (Exception e) {
}
}
/**
*
* @param g
* @param x
* @param y
* @param startindex
* @param endindex
* @throws javax.swing.text.BadLocationException
* @return
*/
protected int drawUnselectedText(Graphics g, int x, int y, int
startindex,
int endindex) throws BadLocationException {
// instantiate the ColorLexer ...
updateLexer(startindex);
int start = startindex;
int mark = startindex; // Marker on startindex
Token tok = null;
try {
while (true) {
tok = lexer.nextToken(); // get us the next token
if ((tok == null) && (tok.getType() == Token.EOF_TYPE)) {
break;
}
int len = 0;
if (tok.getText() != null) {
len += tok.getText().length();
}
if ((start + tok.getColumn() + len) <= startindex) {
continue; // update areal hasn't even started
}
if ((start + tok.getColumn()) >= endindex) {
break; // Token begins after our update areal
}
if ((start + tok.getColumn()) > mark) { // Token begins
after marker
x = draw(mark, (start + tok.getColumn()) - mark,
Color.black, g, x, y);
// draw area til marker in a neutral way
}
int ps = Math.max(start + tok.getColumn(), startindex); //
begin of token
int pe = Math.min(start + tok.getColumn() + len, endindex);
// end of token
// paint the token in it's proper color like the Lexer tells
us
x = draw(ps, pe - ps, HTMLContext.getColor(tok), g, x, y);
mark = pe; // continue with next token after resetting the
marker
} // while
if (endindex > mark) { // marker didn'tt get everything
x = draw(mark, endindex - mark, Color.black, g, x, y);
// let's draw the rest
}
} catch (Exception e) {
// System.err.println("Exception: " + e.toString());
// e.printStackTrace();
}
return x;
}
/**
* <code>drawSelectedText</code>
*
* @param g a <code>Graphics</code> {@link
SourceCodeView#drawUnselectedText}
* @param x an <code>int</code> {@link
SourceCodeView#drawUnselectedText}
* @param y an <code>int</code> {@link
SourceCodeView#drawUnselectedText}
* @param p0 an <code>int</code> {@link
SourceCodeView#drawUnselectedText}
* @param p1 an <code>int</code> {@link
SourceCodeView#drawUnselectedText}
* @return an <code>int</code> {@link SourceCodeView#drawUnselectedText}
* @exception BadLocationException {@link
SourceCodeView#drawUnselectedText}
*/
protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
throws BadLocationException {
return drawUnselectedText(g, x, y, p0, p1);
}
/**
*
* @param a
* @param l
* @param c
* @param g
* @param x
* @param y
* @throws javax.swing.text.BadLocationException
* @return
*/
private int draw(int a, int l, Color c, Graphics g, int x, int y)
throws BadLocationException {
g.setColor(c);
getDocument().getText(a, l, getLineBuffer());
return Utilities.drawTabbedText(getLineBuffer(), x, y, g, this, a);
}
private final static class HTMLContext {
public static Color getColor(Token t) {
Color c = Color.BLACK;
if (t.getType() == HTMLTokenTypes.PCDATA) {
} else if (t.getType() == HTMLTokenTypes.PCDATA) {
} else if (t.getType() == HTMLTokenTypes.COMMENT) {
c = Color.DARK_GRAY;
} else if (t.getType() == HTMLTokenTypes.COMMENT_DATA) {
c = Color.GREEN;
} else {
c = Color.BLUE;
}
return c;
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060811/d3b9aaef/attachment.html
More information about the antlr-interest
mailing list