Class: O::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Parser

Returns a new instance of Parser.



12
13
14
# File 'lib/o/parser.rb', line 12

def initialize content
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/o/parser.rb', line 3

def content
  @content
end

Class Method Details

.compile(content) ⇒ Object



6
7
8
9
# File 'lib/o/parser.rb', line 6

def compile content
  parser = Parser.new content
  parser.compile
end

Instance Method Details

#compileObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/o/parser.rb', line 16

def compile
  script = ""
  indent_counts = 0
  block_start = false

  scan do |token, statement|
    case token
    when :block_start
      block_start = true
      statement = statement.sub(":", " do")
      script << statement << "\n"
    when :statement
      script << statement << "\n"
    when :indent
      indent_counts += 1
      script << "\t"*indent_counts
    when :undent
      script << "\t"*indent_counts
    when :dedent
      if block_start
        block_start = false
        script << "\t"*(indent_counts-1) + "end\n"
      else
        script << "\t"*(indent_counts-1)
      end
      indent_counts -= 1
    end
  end
  script
end