Class: O::Parser

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

Overview

convert sugar syntax

develoment:
  database 'postgresql'

to a pure ruby syntax

development do
  database 'postgresql'
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Parser

Returns a new instance of Parser.



26
27
28
# File 'lib/o/parser.rb', line 26

def initialize content
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

the string data.



15
16
17
# File 'lib/o/parser.rb', line 15

def content
  @content
end

Class Method Details

.compile(content) ⇒ Object

a handy method for Parser.new(content).compile



19
20
21
22
23
# File 'lib/o/parser.rb', line 19

def compile(content)
  parser = Parser.new(content)

  parser.compile
end

Instance Method Details

#compileObject

compile sugar-syntax into ruby-syntax



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/o/parser.rb', line 31

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

  scan { |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
  }

  script
end