public void highlight(String title, StyledTextComp wText) {
// set up lexer process
String script = wText.getText();
StyledText canvas = wText.getStyledText();
byte[] utf8Script = null;
int[] encodingBytes = null;
try {
utf8Script = script.getBytes("UTF-8");
encodingBytes = new int[utf8Script.length+1];
int runner = 0;
for (int i = 0; i < utf8Script.length; i++) {
runner += (utf8Script[i] < 0 && -((int)utf8Script[i])+128 > 192)?1:0;
encodingBytes[i] = runner;
}
encodingBytes[encodingBytes.length-1] = runner;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
List<String> lines = new ArrayList<String>(canvas.getLineCount());
LexerSource lexerSource = new ByteArrayLexerSource(title, utf8Script, lines, 0, true);
lexer.reset();
lexer.setSource(lexerSource);
lexer.setState(RubyYaccLexer.LexState.EXPR_BEG);
// remember bounds of current token
int leftTokenBorder = 0;
int rightTokenBorder = 0;
int t = 0;
int prevt = 0;
int lastCommentEnd = 0;
ArrayList<StyleRange> ranges = new ArrayList<StyleRange>(200);
ArrayList<Integer> intRanges = new ArrayList<Integer>(400);
try {
boolean keepParsing = true;
while (keepParsing) {
/* take care of comments, which are stripped out by the lexer */
int[] upcomingComment = null;
while ((rightTokenBorder >= lastCommentEnd || rightTokenBorder == 0 ) && (upcomingComment = getUpcomingCommentPos(utf8Script, rightTokenBorder)) != null){
leftTokenBorder = upcomingComment[0];
rightTokenBorder = leftTokenBorder + upcomingComment[1];
lastCommentEnd = rightTokenBorder;
//System.out.println("Found comment -> [" + leftTokenBorder + "," + rightTokenBorder + "]");
ranges.add(tokenToStyleRange(TOKEN_COMMENT, null, prevt));
int left = leftTokenBorder - encodingBytes[leftTokenBorder];
int right = rightTokenBorder-encodingBytes[rightTokenBorder]- left;
intRanges.add(left);
intRanges.add(right);
}
/* read language syntax */
int oldOffset = lexerSource.getOffset();
keepParsing = lexer.advance();
prevt = t;
t = lexer.token();
Object v = lexer.value();
leftTokenBorder = oldOffset;
if (leftTokenBorder < lastCommentEnd && lexerSource.getOffset() > lastCommentEnd){
leftTokenBorder = lastCommentEnd;
}
rightTokenBorder = lexerSource.getOffset();
//System.out.println("Found token " + t + " -> " + lexer.value() + " [" + leftTokenBorder + "," + rightTokenBorder + "]");
// skip whitespace and error formatting
if (t != '\n' && t != -1){
ranges.add(tokenToStyleRange(t, v, prevt));
int left = leftTokenBorder - encodingBytes[leftTokenBorder];
int right = rightTokenBorder-encodingBytes[rightTokenBorder]- (leftTokenBorder - encodingBytes[leftTokenBorder]);
intRanges.add(left);
intRanges.add(right);
}
}
// don't mind anything that might go wrong during parsing
} catch (SyntaxException e) {
// apply the latest style to the rest of the file in case there is a syntax error
if (ranges.size() > 0) {
ranges.remove(ranges.size() - 1);
intRanges.remove(intRanges.size()-1);
intRanges.remove(intRanges.size()-1);
}
ranges.add(tokenToStyleRange(t, null, prevt));
int left = leftTokenBorder - encodingBytes[leftTokenBorder];
intRanges.add(left);
intRanges.add(wText.getText().length() - left);
} catch (Exception e) {
// the lexer will sometimes throw a non-syntax exception when confronted with malformed input
//e.printStackTrace();
}
// don't mind swt errors in case some unforseen input brought the style ranges out of order
try {
canvas.setStyleRanges(ArrayUtils.toPrimitive(intRanges.toArray(new Integer[0])), ranges.toArray(new StyleRange[0]));
}
catch (Exception e){
//e.printStackTrace();
}