class Aster::Parser

Public Instance Methods

parse(text) click to toggle source
# File lib/aster/parser.rb, line 9
def parse(text)
  lines = text.split("\n")
  parse_lines lines
end

Private Instance Methods

parse_line(line) click to toggle source
# File lib/aster/parser.rb, line 46
def parse_line(line)
  Command.new raw_parse_line(line)
end
parse_lines(lines) click to toggle source
# File lib/aster/parser.rb, line 50
def parse_lines(lines)
  current_indent = (lines.first ? text_indent(lines.first) : 0)
  last_line = nil
  data = []
  lines.each_with_index do |line, i|
    # Skip Empty lines
    next if line.strip.empty?
    
    # Skip Comments
    next if line.strip[0] == "#"

    next_line = lines[i + 1]
    indent, next_indent = text_indent(line), text_indent(next_line)
    command = parse_line(line)

    # Grab sub lines if possible
    if next_indent > indent
      j = i + 1
      sublines = []
      while(look_ahead_line = lines[j])
        look_ahead_indent = text_indent(look_ahead_line)
        if look_ahead_indent > indent
          sublines << lines.slice!(j)
        else
          break
        end
      end

      begin
        command.sub_commands = parse_lines sublines
      rescue
        command.sub_commands = :GARBAGE
      end
      command.sub_text = sublines.map do |subline|
        subline.gsub(/^[ ]{#{next_indent*2}}/, '')
      end.join("\n")
    end
    data << command
  end
  data
end
raw_parse_line(line) click to toggle source
# File lib/aster/parser.rb, line 24
def raw_parse_line(line)
  @sexp ||= SexpParser.new

  result = @sexp.parse(line)

  if result && result.value
    res = result.value
    def res.garbage?
      false
    end
    res
  else
    # garbage mode
    result = line.split(' ', 2)
    def result.garbage?
      true
    end
    result
  end

end
text_indent(text) click to toggle source
# File lib/aster/parser.rb, line 16
def text_indent(text)
  return -1 if text.nil?
  white_space = text.match(/^( +)/)
  return 0 if white_space.nil?
  size = white_space[0].size
  size % 2 == 0 ? size / 2 : -1
end