ruby code generation question
Chuck Remes
cremes.devl... at mac.com
Thu Oct 4 15:14:34 UTC 2007
I'm writing a scanner using Ragel and Ruby, but I'm coming up against
a problem that I can't seem to figure out. If I try to wrap the
machine in a class, I get a peculiar error that it can't figure out.
Here's a bit of sample code to illustrate.
#
# @LANG: ruby
#
%%{
machine orcscanner;
letters = [a-zA-Z]+;
numbers = [0-9]+;
spaces = [ \t\n]+;
other = [\[:\.\]\-=|]+;
main := |*
( numbers | letters | spaces | '{' | '}' | other );
*|;
}%%
%% write data;
def parse_line(data)
p = 0;
pe = data.length;
cs = 0;
tokstart = 0;
tokend = 0;
act = 0;
%% write init;
%% write exec;
%% write eof;
if cs == orcscanner_error
puts "SCANNER_ERROR"
end
end
inp = [
"76 } sadf",
"Oct 1 09:50:33.37204 [29193]: {test text}"
]
inp.each { |str|
puts "input = #{str}"
parse_line(str)
}
This all works just fine (though it does nothing). If I wrap it in a
class I get an error:
class SomeClass
<code listed above>
inp = [
"76 } sadf",
"Oct 1 09:50:33.37204 [29193]: {test text}"
]
t = SomeClass.new
inp.each { |str|
puts "input = #{str}"
t.parse_line(str)
}
end
Here's the error I get:
t.rb:146:in `parse_line': undefined local variable or method
`orcscanner_start' for #<SomeClass:0x1cac0> (NameError)
from t.rb:322
from t.rb:320:in `each'
from t.rb:320
Looking at the ruby code I see that orcscanner_start is defined like so:
class << self
attr_accessor :orcscanner_start
end
self.orcscanner_start = 1;
This is adding a class method #orcscanner_start to the current class
(self). I guess it works fine when adding to Object (the default
class) but not my custom class. Why? What am I doing wrong?
The reason I want to do this is so my ragel actions can call methods
or access variables that are part of my custom class. I have things
nicely encapsulated.
Thanks for any help or pointers.
cr
More information about the ragel-users
mailing list