Class: Rouge::RegexLexer

Inherits:
Lexer
  • Object
show all
Defined in:
lib/rouge/lexer.rb

Defined Under Namespace

Classes: Rule, ScanState, State, StateDSL

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lexer

aliases, analyze_text, #debug, default_options, filenames, find, #get_tokens, guess, guess_by_filename, guess_by_mimetype, guess_by_source, lex, #lex, make, mimetypes, new, #option, #options, register, tag

Constructor Details

#initialize(parent = nil, opts = {}, &defn) ⇒ RegexLexer

Returns a new instance of RegexLexer.



376
377
378
379
380
381
382
383
384
# File 'lib/rouge/lexer.rb', line 376

def initialize(parent=nil, opts={}, &defn)
  if parent.is_a? Hash
    opts = parent
    parent = nil
  end

  @parent = parent
  super(opts, &defn)
end

Class Method Details

.[](name) ⇒ Object



394
395
396
# File 'lib/rouge/lexer.rb', line 394

def self.[](name)
  get_state(name)
end

.get_state(name) ⇒ Object



386
387
388
389
390
391
392
# File 'lib/rouge/lexer.rb', line 386

def self.get_state(name)
  return name if name.is_a? State

  state = states[name.to_s]
  raise "unknown state: #{name}" unless state
  state.load!
end

.state(name, &b) ⇒ Object



371
372
373
374
# File 'lib/rouge/lexer.rb', line 371

def self.state(name, &b)
  name = name.to_s
  states[name] = State.new(self, name, &b)
end

.statesObject



367
368
369
# File 'lib/rouge/lexer.rb', line 367

def self.states
  @states ||= {}
end

Instance Method Details

#get_state(name) ⇒ Object



398
399
400
# File 'lib/rouge/lexer.rb', line 398

def get_state(name)
  self.class.get_state(name)
end

#step(state, scan_state, &b) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/rouge/lexer.rb', line 421

def step(state, scan_state, &b)
  state.rules.each do |rule|
    return true if run_rule(rule, scan_state, &b)
  end

  false
end

#stream_tokens(stream, &b) ⇒ Object



402
403
404
405
406
# File 'lib/rouge/lexer.rb', line 402

def stream_tokens(stream, &b)
  scan_state = ScanState.new(self, stream)

  stream_with_state(scan_state, &b)
end

#stream_with_state(scan_state, &b) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/rouge/lexer.rb', line 408

def stream_with_state(scan_state, &b)
  until scan_state.eos?
    debug { "stack: #{scan_state.stack.map(&:name).inspect}" }
    debug { "stream: #{scan_state.scanner.peek(20).inspect}" }
    success = step(get_state(scan_state.state), scan_state, &b)

    if !success
      debug { "    no match, yielding Error" }
      b.call(Token['Error'], scan_state.scanner.getch)
    end
  end
end