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.
@@ -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 scan_state
395
- @scan_state ||= ScanState.new(self)
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
- scan_state.instance_eval(&pr)
293
+ instance_eval(&pr)
403
294
  end
404
295
  end
405
296
 
406
297
  def stream_tokens(stream, &b)
407
- scan_state.scanner = stream
408
-
409
- until scan_state.eos?
298
+ until stream.eos?
410
299
  debug { "lexer: #{self.class.tag}" }
411
- debug { "stack: #{scan_state.stack.map(&:name).inspect}" }
412
- debug { "stream: #{scan_state.scanner.peek(20).inspect}" }
413
- success = step(get_state(scan_state.state), scan_state, &b)
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'], scan_state.scanner.getch)
306
+ b.call(Token['Error'], stream.getch)
418
307
  end
419
308
  end
420
309
  end
421
310
 
422
- def step(state, scan_state, &b)
311
+ def step(state, stream, &b)
423
312
  state.rules.each do |rule|
424
- return true if run_rule(rule, scan_state, &b)
313
+ return true if run_rule(rule, stream, &b)
425
314
  end
426
315
 
427
316
  false
428
317
  end
429
318
 
430
- private
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), scan_state, &b)
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
- scan_state.scan(rule.re) do |match|
441
- debug { " got #{match[0].inspect}" }
328
+ scan(stream, rule.re) do
329
+ debug { " got #{stream[0].inspect}" }
442
330
 
443
- scan_state.run_callback(&rule.callback).each do |tok, res|
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
@@ -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 lexer.parent
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 lexer.ruby_lexer
52
+ delegate @ruby_lexer
50
53
  end
51
54
  end
52
55
  end
@@ -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 lexer.shell
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(//) { lexer.shell.reset!; pop! }
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 lexer.shell }
96
- rule(/\$\(|\)|\n/) { delegate lexer.shell }
96
+ rule(/.+?(?=\$\(|\)|\n)/m) { delegate @shell }
97
+ rule(/\$\(|\)|\n/) { delegate @shell }
97
98
  end
98
99
 
99
100
  state :shell_line do
@@ -32,7 +32,7 @@ module Rouge
32
32
  end
33
33
 
34
34
  start do
35
- push :php if lexer.start_inline?
35
+ push :php if start_inline?
36
36
  end
37
37
 
38
38
  keywords = %w(
@@ -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, :bold => true
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
- 'Name.Tag', :fg => :go_get_it, :bold => true
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', :fg => :sandy
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',
@@ -1,5 +1,5 @@
1
1
  module Rouge
2
2
  def self.version
3
- "0.1.0.rc1"
3
+ "0.1.0"
4
4
  end
5
5
  end
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.rc1
5
- prerelease: 6
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: 1.3.1
93
+ version: '0'
94
94
  requirements: []
95
95
  rubyforge_project: rouge
96
96
  rubygems_version: 1.8.23