Class: Reco::Preprocessor

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Preprocessor

Returns a new instance of Preprocessor.



10
11
12
13
14
15
16
# File 'lib/reco/preprocessor.rb', line 10

def initialize(source)
  @scanner = Scanner.new source
  @output = ''
  @level = 0
  @options = {}
  @captures = []
end

Class Method Details

.preprocess(source) ⇒ Object



6
7
8
# File 'lib/reco/preprocessor.rb', line 6

def self.preprocess(source)
  new(source).execute
end

Instance Method Details

#begin_code(options) ⇒ Object



32
33
34
# File 'lib/reco/preprocessor.rb', line 32

def begin_code(options)
  @options = options
end

#dedentObject



55
56
57
58
59
60
61
62
# File 'lib/reco/preprocessor.rb', line 55

def dedent
  @level -= 1
  fail "unexpected dedent" if @level < 0
  if @captures[0] == @level
    @captures.shift
    dedent
  end
end

#executeObject



18
19
20
21
# File 'lib/reco/preprocessor.rb', line 18

def execute
  @scanner.scan { |token| send token.shift, *token } until @scanner.done?
  @output
end

#fail(message) ⇒ Object

Raises:



64
65
66
# File 'lib/reco/preprocessor.rb', line 64

def fail(message)
  raise Error, message
end

#indent(capture = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/reco/preprocessor.rb', line 46

def indent(capture = nil)
  @level += 1
  if capture
    record "__capture #{capture}"
    @captures.unshift @level
    indent
  end
end


28
29
30
# File 'lib/reco/preprocessor.rb', line 28

def print_string(string)    
  record "__out.push #{Reco::Util.inspect_string(string)}" unless string.empty?
end

#record(line) ⇒ Object



23
24
25
26
# File 'lib/reco/preprocessor.rb', line 23

def record(line)
  @output += @level.times.collect { "  " }.join
  @output += "#{line}\n"
end

#record_code(code) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/reco/preprocessor.rb', line 36

def record_code(code)
  if code != 'end'
    if @options[:print]
      record @options[:safe] ? "__out.push #{code}" : "__out.push __sanitize #{code}"
    else
      record code
    end
  end
end