[ragel-users] Help beginning with Ragel
Iñaki Baz Castillo
ibc at aliax.net
Mon Jan 26 22:34:42 UTC 2009
Hi, I'm beginning with Ragel. For now I've readed the documentation and try to
do some easy examples with Ruby host language (no C extensions for now).
I just want to parse a SIP URI like this:
sip:alice at mydomain.org
and extract the following data into a Ruby SIP_URI class instance:
@protocol => "sip"
@user => "alice"
@host => "mydomain.org"
I've already done this easy parser but I don't know how to extract the parsed
data into the Ruby attributes. The code I've done for now is:
----------------------------------------------------------
class SIP_URI
attr_accessor :protocol, :user, :host
def initialize
@protocol = ""
@user = ""
@host = ""
end
def to_s
#puts @protocol.to_s + ':' + @user.to_s + '@' + @host.to_s
# puts "Not implemented yet..."
end
end
@my_sip_uri = SIP_URI.new
%%{
machine sip_uri;
protocol = ('sip'|'sips');
user = [a-zA-Z0-9]+;
host = [a-zA-Z0-9]+;
main := protocol ':' user '@' host;
}%%
%% write data;
def run_machine(data)
puts "Running Ragel the state machine with input #{data}..."
# Initialization
data = data.unpack("c*") if data.is_a?(String)
p = 0
pe = data.length
tag = nil
%% write init;
%% write exec;
puts "Finished. The state of the machine is: #{cs}"
puts "p: #{p} pe: #{pe}"
end
run_machine ARGV[0]
puts @my_sip_uri
----------------------------------------------------------
It works, of course, but it's not very useful as it is now :)
To get the parsed data fragments I've readed about the usage of:
%%{
action _tag { mark_tag = p }
action tag { tag = data[mark_tag..p-1] }
machine sip_uri;
protocol = ('sip'|'sips') >_tag %tag ;
user = [a-zA-Z0-9]+ >_tag %tag ;
host = [a-zA-Z0-9]+ >_tag %tag ;
main := protocol ':' user '@' host;
}%%
I understand that "tag" will be a data array fragment starting by "p" and
finishing in each found expression. But I have no idea (and have found no
doc) about how to get that info into Ruby variables.
If I compile the above code with the new block I get an error anyway:
example02.rl:21:2: there is no previous specification name
example02.rl:28: action lookup of "_tag" failed
example02.rl:28: action lookup of "tag" failed
In the documentation I read about a variable "ts":
ts - This must be a pointer to character data. In Java and Ruby code
this must be an integer.
but have no idea of how to use it.
Any help or explanation please? Thanks a lot.
--
Iñaki Baz Castillo
More information about the ragel-users
mailing list