Class: Lmt::Tangle::Tangler::ConditionalProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(extension_context) ⇒ ConditionalProcessor

Returns a new instance of ConditionalProcessor.



356
357
358
359
360
361
362
363
364
# File 'lib/lmt/lmt.rb', line 356

def initialize(extension_context)
  @if_expression = /^!\s+if\s+(.*)$/
  @elsif_expression = /^!\s+elsif\s+(.*)$/
  @else_expression = /^!\s+else/
  @end_expression = /^!\s+end$/
  @output_enabled = true
  @stack = []
  @extension_context = extension_context
end

Instance Method Details

#check_block_balanceObject



392
393
394
# File 'lib/lmt/lmt.rb', line 392

def check_block_balance
  throw "unbalanced blocks" unless @stack.empty?
end

#should_output(line) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/lmt/lmt.rb', line 366

def should_output(line)
  case line
    when @if_expression
      condition = $1
      prior_state = @output_enabled
      @output_enabled = !!@extension_context.get_binding.eval(condition)
      @stack.push([:if, prior_state, !@output_enabled])
    when @elsif_expression
      throw "elsif statement missing if"  if @stack.empty?
      condition = $1
      type, prior_state, execute_else = @stack.pop()
      @output_enabled = execute_else && !!@extension_context.get_binding.eval(condition)
      @stack.push([type, prior_state, execute_else && !@output_enabled])
    when @else_expression
      throw "else statement missing if" if @stack.empty?
      type, prior_state, execute_else = @stack.pop()
      @output_enabled = execute_else
      @stack.push([type, prior_state, execute_else])
    when @end_expression
      throw "end statement missing begin" if @stack.empty?
      type, prior_state, execute_else = @stack.pop()
      @output_enabled = prior_state
  end
  @output_enabled
end