Class: Rouge::RegexLexer::ScanState

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

Constant Summary collapse

MAX_NULL_STEPS =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lexer, scanner, stack = nil) ⇒ ScanState

Returns a new instance of ScanState.



243
244
245
246
247
# File 'lib/rouge/lexer.rb', line 243

def initialize(lexer, scanner, stack=nil)
  @lexer = lexer
  @scanner = scanner
  @stack = stack || [lexer.get_state(:root)]
end

Instance Attribute Details

#lexerObject

Returns the value of attribute lexer.



242
243
244
# File 'lib/rouge/lexer.rb', line 242

def lexer
  @lexer
end

#scannerObject

Returns the value of attribute scanner.



240
241
242
# File 'lib/rouge/lexer.rb', line 240

def scanner
  @scanner
end

#stackObject

Returns the value of attribute stack.



241
242
243
# File 'lib/rouge/lexer.rb', line 241

def stack
  @stack
end

Class Method Details

.delegate(m, target) ⇒ Object



234
235
236
237
238
# File 'lib/rouge/lexer.rb', line 234

def self.delegate(m, target)
  define_method(m) do |*a, &b|
    send(target).send(m, *a, &b)
  end
end

Instance Method Details

#delegate(lexer, text = nil) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/rouge/lexer.rb', line 295

def delegate(lexer, text=nil)
  debug { "    delegating to #{lexer.name}" }
  text ||= scanner[0]

  lexer.lex(text) do |tok, val|
    debug { "    delegated token: #{tok.inspect}, #{val.inspect}" }
    token(tok, val)
  end
end

#group(tok) ⇒ Object



291
292
293
# File 'lib/rouge/lexer.rb', line 291

def group(tok)
  token(tok, scanner[@group_count += 1])
end

#in_state?(state_name) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/rouge/lexer.rb', line 261

def in_state?(state_name)
  stack.map(&:name).include? state_name.to_s
end

#pop!Object



249
250
251
252
253
254
# File 'lib/rouge/lexer.rb', line 249

def pop!
  raise 'empty stack!' if stack.empty?

  debug { "    popping stack" }
  stack.pop
end

#push(state_name) ⇒ Object



256
257
258
259
# File 'lib/rouge/lexer.rb', line 256

def push(state_name)
  debug { "    pushing #{state_name}" }
  stack.push(state.relative_state(state_name))
end

#run_callback(&callback) ⇒ Object



276
277
278
279
280
281
282
283
# File 'lib/rouge/lexer.rb', line 276

def run_callback(&callback)
  Enumerator.new do |y|
    @output_stream = y
    @group_count = 0
    instance_exec(self, &callback)
    @output_stream = nil
  end
end

#scan(re, &b) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/rouge/lexer.rb', line 311

def scan(re, &b)
  @null_steps ||= 0

  if @null_steps >= MAX_NULL_STEPS
    debug { "    too many scans without consuming the string!" }
    return false
  end

  scanner.scan(re)

  if scanner.matched?
    if scanner.matched_size == 0
      @null_steps += 1
    else
      @null_steps = 0
    end

    yield self
    return true
  end

  return false
end

#stateObject



305
306
307
308
# File 'lib/rouge/lexer.rb', line 305

def state
  raise 'empty stack!' if stack.empty?
  stack.last
end

#state?(state_name) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/rouge/lexer.rb', line 265

def state?(state_name)
  state_name.to_s == state.name
end

#token(tok, val = nil) ⇒ Object



285
286
287
288
289
# File 'lib/rouge/lexer.rb', line 285

def token(tok, val=nil)
  raise 'no output stream' unless @output_stream

  @output_stream << [Token[tok], val || scanner[0]]
end