Class: Dest::Parser

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

Constant Summary collapse

EXPRESSION =

Provides a match group for the expression.

/\s*#\s*>>\s*(?<expression>.*)/
RESULT =

Provides a match group for the result.

/\s*#\s*=>\s*(?<result>.*)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(filename)
  @filename = filename
end

Class Method Details

.parse(filename) ⇒ Object



39
40
41
# File 'lib/dest/parser.rb', line 39

def self.parse(filename)
  self.new(filename).parse
end

Instance Method Details

#parseObject

Return value is a hash following this structure { :filename => ‘something.rb’,

:values => [ [1, 'sum(5, 5)', '10'] ]  ( Array of Arrays. [ line_number, expression, result])

}



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dest/parser.rb', line 24

def parse
  parsed_output = []

  File.open(@filename).each_line.with_index do |line, num|
    next if line.lstrip[0] != "#"

    if expr = line.match(EXPRESSION)
      parsed_output.push [num, expr["expression"]]
    elsif result = line.match(RESULT)
      parsed_output.last.push(result["result"])
    end
  end
  parsed_output
end