Class: ErlangConfig::ErlEnumeration

Inherits:
Object
  • Object
show all
Defined in:
lib/erlang_config/erlnumeration.rb

Direct Known Subclasses

ErlList, ErlTuple

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ErlEnumeration

Returns a new instance of ErlEnumeration.



4
5
6
7
# File 'lib/erlang_config/erlnumeration.rb', line 4

def initialize(str)
  @str = str
  @elements = []
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



3
4
5
# File 'lib/erlang_config/erlnumeration.rb', line 3

def elements
  @elements
end

#strObject

Returns the value of attribute str.



3
4
5
# File 'lib/erlang_config/erlnumeration.rb', line 3

def str
  @str
end

Instance Method Details

#get_element_strsObject

parse @str to elements to the end of @str, and each elements must be separated by “,”



50
51
52
53
54
55
56
57
58
59
# File 'lib/erlang_config/erlnumeration.rb', line 50

def get_element_strs
  element_strs=[]
  str_to_parse = @str[1,@str.length-2].strip  # between [ and ], or { and }
  until str_to_parse == ""
    term_str, new_str_to_parse = parse_first_term(str_to_parse)
    element_strs << term_str
    str_to_parse = new_str_to_parse
  end
  element_strs
end

#parseObject



9
10
11
12
# File 'lib/erlang_config/erlnumeration.rb', line 9

def parse
  strs = get_element_strs
  @elements = parse_element_strs(strs)
end

#parse_element_strs(strs) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/erlang_config/erlnumeration.rb', line 61

def parse_element_strs(strs)
  strs.map {|term_str|
    term = ErlTerm.decode(term_str)
    if term.is_a?(ErlTuple) or term.is_a?(ErlList)
      term_strs = term.get_element_strs
      term.elements = term.parse_element_strs(term_strs)
    end
    term
  }
end

#parse_first_term(str_to_parse) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/erlang_config/erlnumeration.rb', line 31

def parse_first_term(str_to_parse)
  open_str = str_to_parse[/^(\[|\{|\"|\'|<<|#Ref|<)/,1]
  if open_str
    close_str = ERL_CLOSE_STRS[open_str]
    pos_open_str = str_to_parse.index(open_str)
    pos_close_str = pos_close_str(str_to_parse,open_str)
    term_str = str_to_parse[pos_open_str..(pos_close_str+close_str.length-1)]
    new_str_to_parse = str_to_parse[(pos_close_str+close_str.length), str.length]
  else
    pos_open_str = 0
    pos_close_str = str_to_parse.index(",") || str_to_parse.length
    term_str = str_to_parse[pos_open_str..(pos_close_str-1)]
    new_str_to_parse = str_to_parse[pos_close_str+1, str.length]
  end
  new_str_to_parse = "" if new_str_to_parse.nil?
  [term_str, new_str_to_parse.gsub(/^[,\s]+/,"")]
end

#pos_close_str(str, open_str) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/erlang_config/erlnumeration.rb', line 14

def pos_close_str(str, open_str)
  close_str = ERL_CLOSE_STRS[open_str]
  if open_str == close_str
    return str.index(close_str,1)
  else
    open_count = 1
    for i in ((str.index(open_str)+1)..(str.length))
      opened = (str[i,open_str.length ] == open_str)
      open_count += 1 if opened
      closed = (str[i,close_str.length] == close_str)
      open_count -= 1 if closed
      return i if (closed && open_count==0)
    end
    raise "Parse error, not found matching close of #{open_str} in #{str}"
  end
end