Class: CLabs::CaseGen::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Parser

Returns a new instance of Parser.



74
75
76
77
78
# File 'lib/casegen.rb', line 74

def initialize(data)
  @data = data
  @agents = []
  parse
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



72
73
74
# File 'lib/casegen.rb', line 72

def agents
  @agents
end

Instance Method Details

#parseObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/casegen.rb', line 80

def parse
  lines = @data.split(/\n/)
  while !lines.empty?
    line = lines.shift
    next if line.strip.empty?

    data = nil
    agent_class, reference_agents = parse_agent(line)

    next_line = lines.shift
    if next_line =~ /^-+/
      data = parse_data(lines).join("\n")
    else
      raise ParserException.new("Expected hyphen line after the agent declaration for <#{agent_class}>")
    end

    @agents << agent_class.new(data, reference_agents)
  end
end

#parse_agent(line) ⇒ Object

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/casegen.rb', line 111

def parse_agent(line)
  agent_name, *reference_agent_names = line.split(/(?=\()/)
  raise ParserException.new("Nested agents ( e.g. a(b(c)) ) not supported yet") if reference_agent_names.length > 1
  if reference_agent_names.length > 0
    reference_agent_names = reference_agent_names[0].gsub(/\(|\)/, '').split(/,/)
    reference_agent_names.collect! do |name|
      name.strip
    end
  else
    reference_agent_names = []
  end

  [agent_name, reference_agent_names].flatten.each do |a_name|
    raise ParserException.new("Unregistered agent <#{a_name}> in agent name data <#{line}>") if !Agents.instance.id_registered?(a_name)
  end

  reference_agents = []
  reference_agent_names.each do |ref_name|
    @agents.each do |agent|
      reference_agents << agent if agent.class.agent_id =~ /#{ref_name}/i
    end
  end
  agent_class = Agents.instance.get_agent_by_id(agent_name)
  [agent_class, reference_agents]
end

#parse_data(lines) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/casegen.rb', line 100

def parse_data(lines)
  end_index = -1
  lines.each_with_index do |line, index|
    if line =~ /^-+/
      end_index = index - 2
      return lines.slice!(0, end_index)
    end
  end
  return lines.slice!(0, lines.length)
end