Class: O::Parser
- Inherits:
-
Object
- Object
- O::Parser
- 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
-
#content ⇒ Object
readonly
the string data.
Class Method Summary collapse
-
.compile(content) ⇒ Object
a handy method for Parser.new(content).compile.
Instance Method Summary collapse
-
#compile ⇒ Object
compile sugar-syntax into ruby-syntax.
-
#initialize(content) ⇒ Parser
constructor
A new instance of Parser.
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
#content ⇒ Object (readonly)
the string data.
15 16 17 |
# File 'lib/o/parser.rb', line 15 def content @content end |
Class Method Details
Instance Method Details
#compile ⇒ Object
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 |