Module: Xap::Parser::ParseXap

Defined in:
lib/xap/parser/parse_xap.rb

Constant Summary collapse

@@parser =
XapTreetopParser.new

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object

Returns a Treetop node tree for the given xAP message



21
22
23
24
25
26
27
28
29
# File 'lib/xap/parser/parse_xap.rb', line 21

def self.parse(data)
	tree = @@parser.parse(data, :root => :message)

	if !tree
		raise Exception, "Parse error: #{@@parser.failure_reason.inspect} (index #{@@parser.index})"
	end

	tree
end

.simple_parse(data) ⇒ Object

Returns a hash that is equivalent to calling parse(data).to_hash(), but much faster. However, this method does not do any explicit checking for invalid messages or values.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xap/parser/parse_xap.rb', line 34

def self.simple_parse(data)
	Hash[*data.split(/}\n?/).map {|v|
		bl = v.split("\n{\n")
		bl[1] = Hash[*bl[1].to_s.split("\n").map {|v2|
			pair = v2.split(/[=!]/, 2)
			pair[1] = [pair[1]].pack 'H*' if v2 =~ /^[^=!]+!/
			pair
		}.flatten!]
		bl
	}.flatten!(1)]
end