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
|
module CodeRay module Scanners
class Ruby < Scanner
register_for :rubyfast
RESERVED_WORDS = [
'and', 'def', 'end', 'in', 'or', 'unless', 'begin',
'defined?', 'ensure', 'module', 'redo', 'super', 'until',
'BEGIN', 'break', 'do', 'next', 'rescue', 'then',
'when', 'END', 'case', 'else', 'for', 'retry',
'while', 'alias', 'class', 'elsif', 'if', 'not', 'return',
'undef', 'yield',
]
DEF_KEYWORDS = ['def']
MODULE_KEYWORDS = ['class', 'module']
DEF_NEW_STATE = WordList.new(:initial).
add(DEF_KEYWORDS, :def_expected).
add(MODULE_KEYWORDS, :module_expected)
WORDS_ALLOWING_REGEXP = [
'and', 'or', 'not', 'while', 'until', 'unless', 'if', 'elsif', 'when'
]
REGEXP_ALLOWED = WordList.new(false).
add(WORDS_ALLOWING_REGEXP, :set)
PREDEFINED_CONSTANTS = [
'nil', 'true', 'false', 'self',
'DATA', 'ARGV', 'ARGF', '__FILE__', '__LINE__',
]
IDENT_KIND = WordList.new(:ident).
add(RESERVED_WORDS, :reserved).
add(PREDEFINED_CONSTANTS, :pre_constant)
IDENT = /[a-zA-Z_][a-zA-Z_0-9]*/
METHOD_NAME = / #{IDENT} [?!]? /xo
METHOD_NAME_EX = /
#{IDENT}[?!=]? # common methods: split, foo=, empty?, gsub!
| \*\*? # multiplication and power
| [-+~]@? # plus, minus
| [\/%&|^`] # division, modulo or format strings, &and, |or, ^xor, `system`
| \[\]=? # array getter and setter
| <=?>? | >=? # comparison, rocket operator
| << | >> # append or shift left, shift right
| ===? # simple equality and case equality
/ox
GLOBAL_VARIABLE = / \$ (?: #{IDENT} | [1-9] | 0[a-zA-Z_0-9]* | [~&+`'=\/,;_.<>!@$?*":\\] | -[a-zA-Z_0-9] ) /ox
DOUBLEQ = / " [^"\#\\]* (?: (?: \#\{.*?\} | \#(?:$")? | \\. ) [^"\#\\]* )* "? /mox
SINGLEQ = / ' [^'\\]* (?: \\. [^'\\]* )* '? /mox
STRING = / #{SINGLEQ} | #{DOUBLEQ} /ox
SHELL = / ` [^`\#\\]* (?: (?: \#\{.*?\} | \#(?:$`)? | \\. ) [^`\#\\]* )* `? /mox
REGEXP =%r! / [^/\#\\]* (?: (?: \#\{.*?\} | \#(?:$/)? | \\. ) [^/\#\\]* )* /? !mox
DECIMAL = /\d+(?:_\d+)*/ # doesn't recognize 09 as octal error
OCTAL = /0_?[0-7]+(?:_[0-7]+)*/
HEXADECIMAL = /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/
BINARY = /0b[01]+(?:_[01]+)*/
EXPONENT = / [eE] [+-]? #{DECIMAL} /ox
FLOAT = / #{DECIMAL} (?: #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? ) /
INTEGER = /#{OCTAL}|#{HEXADECIMAL}|#{BINARY}|#{DECIMAL}/
ESCAPE_STRING = /
% (?!\s)
(?:
[qsw]
(?:
\( [^\)\\]* (?: \\. [^\)\\]* )* \)?
|
\[ [^\]\\]* (?: \\. [^\]\\]* )* \]?
|
\{ [^\}\\]* (?: \\. [^\}\\]* )* \}?
|
\< [^\>\\]* (?: \\. [^\>\\]* )* \>?
|
\\ [^\\ ]* \\?
|
( [^a-zA-Z0-9] ) # $1
(?:(?!\1)[^\\])* (?: \\. (?:(?!\1)[^\#\\])* )* \1?
)
|
[QrxWr]?
(?:
\( [^\)\#\\]* (?: (?:\#\{.*?\}|\#|\\.) [^\)\#\\]* )* \)?
|
\[ [^\]\#\\]* (?: (?:\#\{.*?\}|\#|\\.) [^\]\#\\]* )* \]?
|
\{ [^\}\#\\]* (?: (?:\#\{.*?\}|\#|\\.) [^\}\#\\]* )* \}?
|
\< [^\>\#\\]* (?: (?:\#\{.*?\}|\#|\\.) [^\>\#\\]* )* \>?
|
\# [^\# \\]* (?: \\. [^\# \\]* )* \#?
|
\\ [^\\\# ]* (?: (?:\#\{.*?\}|\# ) [^\\\# ]* )* \\?
|
( [^a-zA-Z0-9] ) # $2
(?:(?!\2)[^\#\\])* (?: (?:\#\{.*?\}|\#|\\.) (?:(?!\2)[^\#\\])* )* \2?
)
)
/mox
SYMBOL = /
:
(?:
#{GLOBAL_VARIABLE}
| @@?#{IDENT}
| #{METHOD_NAME_EX}
| #{STRING}
)/ox
HEREDOC = /
<< (?! [\dc] )
(?: [^\n]*? << )?
(?:
([a-zA-Z_0-9]+)
(?: .*? ^\1$ | .* )
|
-([a-zA-Z_0-9]+)
(?: .*? ^\s*\2$ | .* )
|
(["\'`]) (.*?) \3
(?: .*? ^\4$ | .* )
|
- (["\'`]) (.*?) \5
(?: .*? ^\s*\6$ | .* )
)
/mx
RDOC = /
=begin (?!\S) [^\n]* \n?
(?:
(?! =end (?!\S) )
[^\n]* \n?
)*
(?:
=end (?!\S) [^\n]*
)?
/mx
DATA = /
__END__\n
(?:
(?=\#CODE)
|
.*
)
/
private
def scan_tokens tokens, options
state = :initial
regexp_allowed = true
last_token_dot = false
until eos?
match = nil
kind = :error
if scan(/\s+/) # in every state
kind = :space
regexp_allowed = :set if regexp_allowed or matched.index(?\n) # delayed flag setting
elsif scan(/ \#[^\n]* /x) # in every state
kind = :comment
regexp_allowed = :set if regexp_allowed
elsif state == :initial
# IDENTIFIERS, KEYWORDS
if scan(GLOBAL_VARIABLE)
kind = :global_variable
elsif scan(/ @@ #{IDENT} /ox)
kind = :class_variable
elsif scan(/ @ #{IDENT} /ox)
kind = :instance_variable
elsif scan(/ #{DATA} | #{RDOC} /ox)
kind = :comment
elsif scan(METHOD_NAME)
match = matched
if last_token_dot
kind =
if match[/^[A-Z]/]
:constant
else
:ident
end
else
kind = IDENT_KIND[match]
if kind == :ident and match[/^[A-Z]/]
kind = :constant
elsif kind == :reserved
state = DEF_NEW_STATE[match]
regexp_allowed = REGEXP_ALLOWED[match]
end
end
elsif scan(STRING)
kind = :string
elsif scan(SHELL)
kind = :shell
elsif scan(HEREDOC)
kind = :string
elsif check(/\//) and regexp_allowed
scan(REGEXP)
kind = :regexp
elsif scan(ESCAPE_STRING)
match = matched
kind =
case match[0]
when ?s
:symbol
when ?r
:regexp
when ?x
:shell
else
:string
end
elsif scan(/:(?:#{GLOBAL_VARIABLE}|#{METHOD_NAME_EX}|#{STRING})/ox)
kind = :symbol
elsif scan(/
\? (?:
[^\s\\]
|
\\ (?:M-\\C-|C-\\M-|M-\\c|c\\M-|c|C-|M-))? (?: \\ (?: . | [0-7]{3} | x[0-9A-Fa-f][0-9A-Fa-f] )
)
/mx)
kind = :integer
elsif scan(/ [-+*\/%=<>;,|&!()\[\]{}~?] | \.\.?\.? | ::? /x)
kind = :operator
match = matched
regexp_allowed = :set if match[-1,1] =~ /[~=!<>|&^,\(\[+\-\/\*%]\z/
last_token_dot = :set if match == '.' or match == '::'
elsif scan(FLOAT)
kind = :float
elsif scan(INTEGER)
kind = :integer
else
getch
end
elsif state == :def_expected
if scan(/ (?:#{IDENT}::)* (?:#{IDENT}\.)? #{METHOD_NAME_EX} /ox)
kind = :method
else
getch
end
state = :initial
elsif state == :module_expected
if scan(/<</)
kind = :operator
else
if scan(/ (?:#{IDENT}::)* #{IDENT} /ox)
kind = :method
else
getch
end
state = :initial
end
end
text = match || matched
if kind == :regexp and not eos?
text << scan(/[eimnosux]*/)
end
regexp_allowed = (regexp_allowed == :set) # delayed flag setting
last_token_dot = last_token_dot == :set
tokens << [text, kind]
end
tokens
end
end
end end
|