/
RubyStepSyntaxHighlighter.java
357 lines (294 loc) · 11.6 KB
/
RubyStepSyntaxHighlighter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package org.typeexit.kettle.plugin.steps.ruby;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.commons.lang.ArrayUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.jruby.CompatVersion;
import org.jruby.common.RubyWarnings;
import org.jruby.lexer.yacc.ByteArrayLexerSource;
import org.jruby.lexer.yacc.LexerSource;
import org.jruby.lexer.yacc.RubyYaccLexer;
import org.jruby.lexer.yacc.SyntaxException;
import org.jruby.lexer.yacc.Token;
import org.jruby.parser.ParserConfiguration;
import org.jruby.parser.ParserSupport;
import org.jruby.parser.RubyParserResult;
import org.jruby.parser.Tokens;
import org.jruby.Ruby;
import org.pentaho.di.ui.core.widget.StyledTextComp;
import org.typeexit.kettle.plugin.steps.ruby.RubyStepMeta.RubyVersion;
public class RubyStepSyntaxHighlighter {
RubyYaccLexer lexer;
ParserSupport parserSupport;
Color[] colors;
final int TOKEN_COMMENT = -100;
String[] STANDARD_GLOBAL_FUNCTIONS = {"abort", "autoload", "autoload?", "binding", "block_given?", "callcc", "caller", "chomp", "chomp!", "chop",
"chop!", "evel", "exec", "exit", "exit!", "fail", "fork", "format", "getc", "gets", "gsub", "gsub!", "iterator?", "load", "open", "p", "print", "printf", "putc", "puts", "rand",
"readline", "readlines", "scan", "select", "sleep", "split", "sprintf", "srand", "sub", "sub!", "syscall", "system", "test", "trap", "warn"
};
String[] STANDARD_METHODS = { "allocate", "clone", "display", "dup", "enum_for", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "inherited", "inspect", "instance_of?", "is_a?",
"kind_of?", "method", "methods", "new", "nil?", "object_id", "respond_to?", "send", "superclass", "taint", "tainted?", "to_a", "to_enum", "to_s", "untaint"
};
String[] PSEUDO_KEYWORDS = { "at_exit", "attr", "attr_accessor", "attr_reader", "attr_writer", "include", "lambda", "load", "proc", "loop", "private", "protected", "public", "raise", "catch",
"java_import", "require", "import", "include_package"
};
SortedSet<String> GLOBAL_FUNCTIONS_SET = new TreeSet<String>(Arrays.asList(STANDARD_GLOBAL_FUNCTIONS));
SortedSet<String> STANDARD_METHODS_SET = new TreeSet<String>(Arrays.asList(STANDARD_METHODS));
SortedSet<String> PSEUDO_KEYWORDS_SET = new TreeSet<String>(Arrays.asList(PSEUDO_KEYWORDS));
final int COLOR_BLACK = 0;
final int COLOR_GREENISH = 1;
final int COLOR_BLUE = 2;
final int COLOR_ORANGE = 4;
final int COLOR_RED = 5;
final int COLOR_GREEN = 6;
final int COLOR_GRAY = 7;
final int STYLE_DEFAULT = 0;
final int STYLE_STRING = 1;
final int STYLE_SYMBOL = 2;
final int STYLE_KEYWORD = 3;
final int STYLE_GLOBAL_FUNCTION = 4;
final int STYLE_STANDARD_METHOD = 5;
final int STYLE_LITERAL_BOUNDARY = 6;
final int STYLE_COMMENT = 7;
final int STYLE_CONSTANT = 8;
final int STYLE_VARIABLE = 9;
StyleRange[] styles;
public RubyStepSyntaxHighlighter() {
// -- the colors to use --
Display display = Display.getDefault();
colors = new Color[] {
new Color(display, new RGB(0, 0, 0)), // black
new Color(display, new RGB(63, 127, 95)), // Greenish
new Color(display, new RGB(0, 0, 192)), // Blue
new Color(display, new RGB(127, 0, 85)), // -- not used --
new Color(display, new RGB(255, 102, 0)), // Orange
new Color(display, new RGB(225, 0, 0)), // Red
new Color(display, new RGB(0, 128, 0)), // Green
new Color(display, new RGB(128, 128, 128)) // Gray
};
styles = new StyleRange[] {
new StyleRange(0, 0, null, null, SWT.NORMAL),
new StyleRange(0, 0, colors[COLOR_RED], null, SWT.NORMAL),
new StyleRange(0, 0, colors[COLOR_ORANGE], null, SWT.NORMAL),
new StyleRange(0, 0, colors[COLOR_BLUE], null, SWT.BOLD),
new StyleRange(0, 0, colors[COLOR_GREEN], null, SWT.NORMAL),
new StyleRange(0, 0, colors[COLOR_GREEN], null, SWT.NORMAL),
new StyleRange(0, 0, colors[COLOR_GRAY], null, SWT.BOLD),
new StyleRange(0, 0, colors[COLOR_GREENISH], null, SWT.ITALIC),
new StyleRange(0, 0, colors[COLOR_GRAY], null, SWT.BOLD),
new StyleRange(0, 0, colors[COLOR_GRAY], null, SWT.NORMAL)
};
// -- lexer for finding language parts --
lexer = new RubyYaccLexer();
ParserSupport parserSupport = new ParserSupport();
RubyStepMeta meta = new RubyStepMeta();
Ruby runtime = RubyStepFactory.createScriptingContainer(true,meta.getRubyVersion()).getProvider().getRuntime();
ParserConfiguration parserConfig = new ParserConfiguration(runtime, 0, true, CompatVersion.BOTH);
parserSupport.setConfiguration(parserConfig);
parserSupport.setResult(new RubyParserResult());
parserSupport.setWarnings(new RubyWarnings(null));
parserSupport.initTopLocalVariables();
lexer.setEncoding(RubyYaccLexer.UTF8_ENCODING);
lexer.setParserSupport(parserSupport);
lexer.setState(RubyYaccLexer.LexState.EXPR_BEG);
}
private StyleRange tokenToStyleRange(int t, Object value, int prevt) {
// determine keyword style up front
if (t >= Tokens.kCLASS && t <= Tokens.kDO_LAMBDA) {
return styles[STYLE_KEYWORD];
}
switch (t) {
case TOKEN_COMMENT:
return styles[STYLE_COMMENT];
case Tokens.tSTRING_BEG:
case Tokens.tSTRING_CONTENT:
case Tokens.tSTRING_END:
case Tokens.tSTRING_DBEG:
case Tokens.tSTRING_DVAR:
return styles[STYLE_STRING];
case Tokens.tCONSTANT:
return styles[STYLE_CONSTANT];
case Tokens.tGVAR:
case Tokens.tIVAR:
return styles[STYLE_VARIABLE];
case Tokens.tREGEXP_BEG:
case Tokens.tREGEXP_END:
case Tokens.tPIPE:
return styles[STYLE_LITERAL_BOUNDARY];
case Tokens.tSYMBEG:
return styles[STYLE_SYMBOL];
case Tokens.tIDENTIFIER:
if (prevt == Tokens.tSYMBEG) {
return styles[STYLE_SYMBOL];
}
// fall through
case Tokens.tFID:
if (value instanceof Token && PSEUDO_KEYWORDS_SET.contains(((Token) value).getValue().toString())) {
return styles[STYLE_KEYWORD];
}
if (value instanceof Token && STANDARD_METHODS_SET.contains(((Token) value).getValue().toString())) {
return styles[STYLE_STANDARD_METHOD];
}
if (value instanceof Token && GLOBAL_FUNCTIONS_SET.contains(((Token) value).getValue().toString())) {
return styles[STYLE_GLOBAL_FUNCTION];
}
default:
return styles[STYLE_DEFAULT];
}
}
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();
prevt = t;
t = lexer.nextToken();
keepParsing = (t == 0 ? false: true);
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();
}
}
// returns position and length pair of a comment that starts at this position (forwarding through whitespace)
// return null if there's no comment coming up
private int[] getUpcomingCommentPos(byte[] utf8Script, int pos) {
// if we're in the middle of a string or regex, there's no comments
if (lexer.getStrTerm() != null)
return null;
// looking for next comment while ignoring whitespace
boolean searchingComment = true;
boolean isComment = false;
int idx = pos;
do {
if (idx >= utf8Script.length) {
searchingComment = false;
break;
}
switch (utf8Script[idx]) {
case '\t':
case ' ':
case '\n':
case '\r':
idx++;
break;
case '#':
isComment = true;
searchingComment = false;
break;
default:
searchingComment = false;
}
} while (searchingComment);
if (isComment) {
// now to determine it's length, just scan up to \n or EOF
int end = idx;
boolean foundEnd = false;
do{
end += 1;
if (end >= utf8Script.length){
foundEnd = true;
break;
}
switch(utf8Script[end]){
case '\n':
foundEnd = true;
}
}while(!foundEnd);
return new int[] {idx, end-idx};
} else {
return null;
}
}
public void newRubyVersionSelected(RubyVersion rubyVersion) {
// consider setting explicit compat level on parser configuration
}
}