Class: Twofold::Parser

Inherits:
Temple::Parser
  • Object
show all
Defined in:
lib/twofold/parser.rb

Instance Method Summary collapse

Instance Method Details

#call(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/twofold/parser.rb', line 4

def call(input)
  result = [:multi]

  @lines = input.split(/\r?\n/)
  @line_no = 0
  @stack = [result]

  parse_line while next_line

  result
end

#indented(str) {|str.lstrip| ... } ⇒ Object

Yields:

  • (str.lstrip)


50
51
52
53
54
55
56
# File 'lib/twofold/parser.rb', line 50

def indented(str)
  indent = str[/\A\s*/]
  @stack.push([:multi])
  yield str.lstrip
  inner = @stack.pop
  @stack.last << [:twofold, :indented, indent, inner]
end

#next_lineObject



16
17
18
19
20
21
22
23
# File 'lib/twofold/parser.rb', line 16

def next_line
  if @lines.empty?
    @line = nil
  else
    @line = @lines.shift
    @line_no += 1
  end
end

#parse_lineObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/twofold/parser.rb', line 25

def parse_line
  line = @line.lstrip

  case line[0]
  when '\\'
    indented line[1..] do |text|
      @stack.last << [:twofold, :interpolate, text]
    end
  when '|'
    indented line[1..] do |text|
      @stack.last << [:twofold, :interpolate, text]
    end
    @stack.last << [:twofold, :newline]
  when '='
    indented line[1..] do |code|
      @stack.last << [:code, code]
    end
  when nil
    # empty line
  else
    @stack.last << [:code, line]
  end
  @stack.last << [:newline]
end