rouge 0.1.0.rc1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rouge/lexer.rb +105 -131
- data/lib/rouge/lexers/erb.rb +7 -4
- data/lib/rouge/lexers/make.rb +6 -5
- data/lib/rouge/lexers/php.rb +1 -1
- data/lib/rouge/themes/thankful_eyes.rb +5 -5
- data/lib/rouge/version.rb +1 -1
- metadata +4 -4
data/lib/rouge/lexer.rb
CHANGED
@@ -214,119 +214,6 @@ module Rouge
|
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
217
|
-
class ScanState
|
218
|
-
def self.delegate(m, target)
|
219
|
-
define_method(m) do |*a, &b|
|
220
|
-
send(target).send(m, *a, &b)
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
attr_accessor :scanner
|
225
|
-
attr_accessor :stack
|
226
|
-
attr_accessor :lexer
|
227
|
-
def initialize(lexer, scanner=nil, stack=nil)
|
228
|
-
@lexer = lexer
|
229
|
-
@scanner = scanner
|
230
|
-
@stack = stack || [lexer.get_state(:root)]
|
231
|
-
end
|
232
|
-
|
233
|
-
def pop!
|
234
|
-
raise 'empty stack!' if stack.empty?
|
235
|
-
|
236
|
-
debug { " popping stack" }
|
237
|
-
stack.pop
|
238
|
-
end
|
239
|
-
|
240
|
-
def push(state_name=nil, &b)
|
241
|
-
# use the top of the stack by default
|
242
|
-
if state_name || b
|
243
|
-
push_state = state.relative_state(state_name, &b)
|
244
|
-
else
|
245
|
-
push_state = self.state
|
246
|
-
end
|
247
|
-
|
248
|
-
debug { " pushing #{push_state.name}" }
|
249
|
-
stack.push(push_state)
|
250
|
-
end
|
251
|
-
|
252
|
-
def in_state?(state_name)
|
253
|
-
stack.map(&:name).include? state_name.to_s
|
254
|
-
end
|
255
|
-
|
256
|
-
def state?(state_name)
|
257
|
-
state_name.to_s == state.name
|
258
|
-
end
|
259
|
-
|
260
|
-
delegate :debug, :lexer
|
261
|
-
|
262
|
-
delegate :[], :scanner
|
263
|
-
delegate :captures, :scanner
|
264
|
-
delegate :peek, :scanner
|
265
|
-
delegate :eos?, :scanner
|
266
|
-
|
267
|
-
def run_callback(&callback)
|
268
|
-
Enumerator.new do |y|
|
269
|
-
@output_stream = y
|
270
|
-
@group_count = 0
|
271
|
-
instance_exec(self, &callback)
|
272
|
-
@output_stream = nil
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
def token(tok, val=:__absent__)
|
277
|
-
val = scanner[0] if val == :__absent__
|
278
|
-
val ||= ''
|
279
|
-
|
280
|
-
raise 'no output stream' unless @output_stream
|
281
|
-
|
282
|
-
@output_stream << [Token[tok], val]
|
283
|
-
end
|
284
|
-
|
285
|
-
def group(tok)
|
286
|
-
token(tok, scanner[@group_count += 1])
|
287
|
-
end
|
288
|
-
|
289
|
-
def delegate(lexer, text=nil)
|
290
|
-
debug { " delegating to #{lexer.inspect}" }
|
291
|
-
text ||= scanner[0]
|
292
|
-
|
293
|
-
lexer.lex(text, :continue => true) do |tok, val|
|
294
|
-
debug { " delegated token: #{tok.inspect}, #{val.inspect}" }
|
295
|
-
token(tok, val)
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
def state
|
300
|
-
raise 'empty stack!' if stack.empty?
|
301
|
-
stack.last
|
302
|
-
end
|
303
|
-
|
304
|
-
MAX_NULL_STEPS = 5
|
305
|
-
def scan(re, &b)
|
306
|
-
@null_steps ||= 0
|
307
|
-
|
308
|
-
if @null_steps >= MAX_NULL_STEPS
|
309
|
-
debug { " too many scans without consuming the string!" }
|
310
|
-
return false
|
311
|
-
end
|
312
|
-
|
313
|
-
scanner.scan(re)
|
314
|
-
|
315
|
-
if scanner.matched?
|
316
|
-
if scanner.matched_size == 0
|
317
|
-
@null_steps += 1
|
318
|
-
else
|
319
|
-
@null_steps = 0
|
320
|
-
end
|
321
|
-
|
322
|
-
yield self
|
323
|
-
return true
|
324
|
-
end
|
325
|
-
|
326
|
-
return false
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
217
|
class StateDSL
|
331
218
|
attr_reader :rules
|
332
219
|
def initialize(rules)
|
@@ -391,56 +278,57 @@ module Rouge
|
|
391
278
|
self.class.get_state(name)
|
392
279
|
end
|
393
280
|
|
394
|
-
def
|
395
|
-
@
|
281
|
+
def stack
|
282
|
+
@stack ||= [get_state(:root)]
|
283
|
+
end
|
284
|
+
|
285
|
+
def state
|
286
|
+
stack.last or raise 'empty stack!'
|
396
287
|
end
|
397
288
|
|
398
289
|
def reset!
|
399
290
|
@scan_state = nil
|
400
291
|
|
401
292
|
self.class.start_procs.each do |pr|
|
402
|
-
|
293
|
+
instance_eval(&pr)
|
403
294
|
end
|
404
295
|
end
|
405
296
|
|
406
297
|
def stream_tokens(stream, &b)
|
407
|
-
|
408
|
-
|
409
|
-
until scan_state.eos?
|
298
|
+
until stream.eos?
|
410
299
|
debug { "lexer: #{self.class.tag}" }
|
411
|
-
debug { "stack: #{
|
412
|
-
debug { "stream: #{
|
413
|
-
success = step(get_state(
|
300
|
+
debug { "stack: #{stack.map(&:name).inspect}" }
|
301
|
+
debug { "stream: #{stream.peek(20).inspect}" }
|
302
|
+
success = step(get_state(state), stream, &b)
|
414
303
|
|
415
304
|
if !success
|
416
305
|
debug { " no match, yielding Error" }
|
417
|
-
b.call(Token['Error'],
|
306
|
+
b.call(Token['Error'], stream.getch)
|
418
307
|
end
|
419
308
|
end
|
420
309
|
end
|
421
310
|
|
422
|
-
def step(state,
|
311
|
+
def step(state, stream, &b)
|
423
312
|
state.rules.each do |rule|
|
424
|
-
return true if run_rule(rule,
|
313
|
+
return true if run_rule(rule, stream, &b)
|
425
314
|
end
|
426
315
|
|
427
316
|
false
|
428
317
|
end
|
429
318
|
|
430
|
-
|
431
|
-
def run_rule(rule, scan_state, &b)
|
319
|
+
def run_rule(rule, stream, &b)
|
432
320
|
case rule
|
433
321
|
when String
|
434
322
|
debug { " entering mixin #{rule}" }
|
435
|
-
res = step(get_state(rule),
|
323
|
+
res = step(get_state(rule), stream, &b)
|
436
324
|
debug { " exiting mixin #{rule}" }
|
437
325
|
res
|
438
326
|
when Rule
|
439
327
|
debug { " trying #{rule.inspect}" }
|
440
|
-
|
441
|
-
debug { " got #{
|
328
|
+
scan(stream, rule.re) do
|
329
|
+
debug { " got #{stream[0].inspect}" }
|
442
330
|
|
443
|
-
|
331
|
+
run_callback(stream, &rule.callback).each do |tok, res|
|
444
332
|
debug { " yielding #{tok.to_s.inspect}, #{res.inspect}" }
|
445
333
|
b.call(Token[tok], res)
|
446
334
|
end
|
@@ -448,5 +336,91 @@ module Rouge
|
|
448
336
|
end
|
449
337
|
end
|
450
338
|
|
339
|
+
def run_callback(stream, &callback)
|
340
|
+
Enumerator.new do |y|
|
341
|
+
@output_stream = y
|
342
|
+
@group_count = 0
|
343
|
+
@last_matches = stream
|
344
|
+
instance_exec(stream, &callback)
|
345
|
+
@last_matches = nil
|
346
|
+
@output_stream = nil
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
MAX_NULL_STEPS = 5
|
351
|
+
def scan(scanner, re, &b)
|
352
|
+
@null_steps ||= 0
|
353
|
+
|
354
|
+
if @null_steps >= MAX_NULL_STEPS
|
355
|
+
debug { " too many scans without consuming the string!" }
|
356
|
+
return false
|
357
|
+
end
|
358
|
+
|
359
|
+
scanner.scan(re)
|
360
|
+
|
361
|
+
if scanner.matched?
|
362
|
+
if scanner.matched_size == 0
|
363
|
+
@null_steps += 1
|
364
|
+
else
|
365
|
+
@null_steps = 0
|
366
|
+
end
|
367
|
+
|
368
|
+
yield self
|
369
|
+
return true
|
370
|
+
end
|
371
|
+
|
372
|
+
return false
|
373
|
+
end
|
374
|
+
|
375
|
+
def token(tok, val=:__absent__)
|
376
|
+
val = @last_matches[0] if val == :__absent__
|
377
|
+
val ||= ''
|
378
|
+
|
379
|
+
raise 'no output stream' unless @output_stream
|
380
|
+
|
381
|
+
@output_stream << [Token[tok], val]
|
382
|
+
end
|
383
|
+
|
384
|
+
def group(tok)
|
385
|
+
token(tok, @last_matches[@group_count += 1])
|
386
|
+
end
|
387
|
+
|
388
|
+
def delegate(lexer, text=nil)
|
389
|
+
debug { " delegating to #{lexer.inspect}" }
|
390
|
+
text ||= @last_matches[0]
|
391
|
+
|
392
|
+
lexer.lex(text, :continue => true) do |tok, val|
|
393
|
+
debug { " delegated token: #{tok.inspect}, #{val.inspect}" }
|
394
|
+
token(tok, val)
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
def push(state_name=nil, &b)
|
399
|
+
# use the top of the stack by default
|
400
|
+
if state_name || b
|
401
|
+
push_state = state.relative_state(state_name, &b)
|
402
|
+
else
|
403
|
+
push_state = self.state
|
404
|
+
end
|
405
|
+
|
406
|
+
debug { " pushing #{push_state.name}" }
|
407
|
+
stack.push(push_state)
|
408
|
+
end
|
409
|
+
|
410
|
+
def pop!
|
411
|
+
raise 'empty stack!' if stack.empty?
|
412
|
+
|
413
|
+
debug { " popping stack" }
|
414
|
+
stack.pop
|
415
|
+
end
|
416
|
+
|
417
|
+
def in_state?(state_name)
|
418
|
+
stack.map(&:name).include? state_name.to_s
|
419
|
+
end
|
420
|
+
|
421
|
+
def state?(state_name)
|
422
|
+
state_name.to_s == state.name
|
423
|
+
end
|
424
|
+
|
451
425
|
end
|
452
426
|
end
|
data/lib/rouge/lexers/erb.rb
CHANGED
@@ -10,8 +10,6 @@ module Rouge
|
|
10
10
|
return 0.4 if text =~ /<%.*%>/
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :parent
|
14
|
-
attr_reader :ruby_lexer
|
15
13
|
def initialize(opts={})
|
16
14
|
@parent = opts.delete(:parent) || 'html'
|
17
15
|
if @parent.is_a? String
|
@@ -24,6 +22,11 @@ module Rouge
|
|
24
22
|
super(opts)
|
25
23
|
end
|
26
24
|
|
25
|
+
start do
|
26
|
+
@parent.reset!
|
27
|
+
@ruby_lexer.reset!
|
28
|
+
end
|
29
|
+
|
27
30
|
open = /<%%|<%=|<%#|<%-|<%/
|
28
31
|
close = /%%>|-%>|%>/
|
29
32
|
|
@@ -33,7 +36,7 @@ module Rouge
|
|
33
36
|
rule open, 'Comment.Preproc', :ruby
|
34
37
|
|
35
38
|
rule /.+?(?=#{open})|.+/m do
|
36
|
-
delegate
|
39
|
+
delegate @parent
|
37
40
|
end
|
38
41
|
end
|
39
42
|
|
@@ -46,7 +49,7 @@ module Rouge
|
|
46
49
|
rule close, 'Comment.Preproc', :pop!
|
47
50
|
|
48
51
|
rule /.+?(?=#{close})|.+/m do
|
49
|
-
delegate
|
52
|
+
delegate @ruby_lexer
|
50
53
|
end
|
51
54
|
end
|
52
55
|
end
|
data/lib/rouge/lexers/make.rb
CHANGED
@@ -20,15 +20,16 @@ module Rouge
|
|
20
20
|
|
21
21
|
line = /(?:\\.|\\\n|[^\\\n])*/m
|
22
22
|
|
23
|
-
attr_reader :shell
|
24
23
|
def initialize(opts={})
|
25
24
|
super
|
26
25
|
@shell = Shell.new(opts)
|
27
26
|
end
|
28
27
|
|
28
|
+
start { @shell.reset! }
|
29
|
+
|
29
30
|
state :root do
|
30
31
|
rule /^(?:[\t ]+.*\n|\n)+/ do
|
31
|
-
delegate
|
32
|
+
delegate @shell
|
32
33
|
end
|
33
34
|
|
34
35
|
# rule /\$\((?:.*\\\n|.*\n)+/ do
|
@@ -86,14 +87,14 @@ module Rouge
|
|
86
87
|
push :shell_line
|
87
88
|
end
|
88
89
|
|
89
|
-
rule(//) {
|
90
|
+
rule(//) { @shell.reset!; pop! }
|
90
91
|
end
|
91
92
|
|
92
93
|
state :shell do
|
93
94
|
# macro interpolation
|
94
95
|
rule /\$\(\s*[a-z_]\w*\s*\)/i, 'Name.Variable'
|
95
|
-
rule(/.+?(?=\$\(|\)|\n)/m) { delegate
|
96
|
-
rule(/\$\(|\)|\n/) { delegate
|
96
|
+
rule(/.+?(?=\$\(|\)|\n)/m) { delegate @shell }
|
97
|
+
rule(/\$\(|\)|\n/) { delegate @shell }
|
97
98
|
end
|
98
99
|
|
99
100
|
state :shell_line do
|
data/lib/rouge/lexers/php.rb
CHANGED
@@ -28,7 +28,7 @@ module Rouge
|
|
28
28
|
'Generic.Error', :fg => :aluminum1, :bg => :scarletred2
|
29
29
|
style 'Keyword', :fg => :sandy, :bold => true
|
30
30
|
style 'Operator',
|
31
|
-
'Punctuation', :fg => :backlit
|
31
|
+
'Punctuation', :fg => :backlit
|
32
32
|
style 'Generic.Deleted', :fg => :scarletred2
|
33
33
|
style 'Generic.Inserted', :fg => :go_get_it
|
34
34
|
style 'Generic.Emph', :italic => true
|
@@ -44,17 +44,17 @@ module Rouge
|
|
44
44
|
'Name.Class',
|
45
45
|
'Name.Decorator',
|
46
46
|
'Name.Namespace',
|
47
|
-
'Name.Label',
|
48
47
|
'Name.Builtin.Pseudo',
|
49
|
-
'Name.Exception',
|
50
|
-
|
48
|
+
'Name.Exception', :fg => :go_get_it, :bold => true
|
49
|
+
style 'Name.Label',
|
50
|
+
'Name.Tag', :fg => :schrill, :bold => true
|
51
51
|
style 'Literal.Number',
|
52
52
|
'Literal.String.Symbol', :fg => :pink_merengue, :bold => true
|
53
53
|
style 'Literal.String', :fg => :dune, :bold => true
|
54
54
|
style 'Literal.String.Escape',
|
55
55
|
'Literal.String.Char',
|
56
56
|
'Literal.String.Interpol', :fg => :backlit, :bold => true
|
57
|
-
style 'Name.Builtin', :
|
57
|
+
style 'Name.Builtin', :bold => true
|
58
58
|
style 'Name.Entity', :fg => '#999999', :bold => true
|
59
59
|
style 'Text.Whitespace', :fg => '#BBBBBB'
|
60
60
|
style 'Name.Function',
|
data/lib/rouge/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rouge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jay Adkisson
|
@@ -88,9 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
89
|
none: false
|
90
90
|
requirements:
|
91
|
-
- - ! '
|
91
|
+
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
93
|
+
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project: rouge
|
96
96
|
rubygems_version: 1.8.23
|