Module: Yamlr::Reader::Parser

Defined in:
lib/yamlr/reader/parser.rb

Defined Under Namespace

Classes: IndentError

Class Method Summary collapse

Class Method Details

.check_indent(line, spc, idt, num) ⇒ Object

invalid number of spaces, line num and truncated line for context

Raises:



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

def self.check_indent(line, spc, idt, num)
  raise IndentError, "#{num} #{line[1..50]}" if spc % idt != 0
end

.parse(line, opts, num) ⇒ Object



6
7
8
9
10
11
# File 'lib/yamlr/reader/parser.rb', line 6

def self.parse(line, opts, num)
  results = self.parse_line(line, opts)
  phs = self.results_to_hash(results, opts)
  self.check_indent(line, phs[:spc], opts[:indent], num)
  phs
end

.parse_line(line, opts) ⇒ Object

parses line, returns array, dependent on Node module



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
46
47
48
49
50
51
# File 'lib/yamlr/reader/parser.rb', line 21

def self.parse_line(line, opts)
  nod = Yamlr::Reader::Node
  spc = opts[:space]
  hsh = opts[:hash]
  sym = opts[:symbol]
  dcs = opts[:doc_start]
  dct = opts[:doc_term]
  arr = opts[:array]
  com = opts[:comment]

  begin
    case line
    when nod.left_match(arr, spc) then            [:arr, Regexp.last_match.captures]
    when nod.left_match(com, spc) then            [:com, Regexp.last_match.captures]
    when nod.hashkey_sym(hsh, sym, spc) then      [:hky, Regexp.last_match.captures, nil, true]
    when nod.hashkey(hsh, spc) then               [:hky, Regexp.last_match.captures]
    when nod.hashpair_sym_all(hsh, sym, spc) then [:hpr, Regexp.last_match.captures, true, true]
    when nod.hashpair_sym_key(hsh, sym, spc) then [:hpr, Regexp.last_match.captures, true]
    when nod.hashpair_sym_val(hsh, sym, spc) then [:hpr, Regexp.last_match.captures, nil, true]
    when nod.hashpair(hsh, sym, spc) then         [:hpr, Regexp.last_match.captures]
    when /^\s*$/ then                             [:bla]
    when nod.document(dcs, spc) then              [:dcs]
    when nod.document(dct, spc) then              [:dct]
    else
      raise 'MALFORMED'
    end
  rescue => e
    # log or whatever
    [:mal, '', nil, line.strip]
  end
end

.results_to_hash(results, opt) ⇒ Object

creates hash with array vals, includes options



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yamlr/reader/parser.rb', line 55

def self.results_to_hash(results, opt)
  #raise results.flatten.to_s + " results.class => #{results.class}"
  msg, spc, key, val, ask, asv = results.flatten
  { :msg => msg,
    :spc => (spc.nil? ? 0 : spc.length),
    :key => key.to_s.sub(/^:/, ''),
    :val => val.to_s.sub(/^:/, ''),
    :ask => ask,
    :asv => asv,
    :opt => opt}
end