home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
ruby164.zip
/
rbemx164.zip
/
ruby
/
share
/
doc
/
racc-1.3.9
/
sample
/
array.y
next >
Wrap
Text File
|
2001-04-07
|
973b
|
71 lines
#
# convert Array like string into (Ruby's) Array.
#
class ArrayParser
rule
array : '[' contents ']'
{
result = val[1]
}
| '[' ']'
{
result = []
}
contents: ITEM
{
result = val
}
| contents ',' ITEM
{
result.push val[2]
}
end
---- inner
def parse( str )
str.strip!
@q = []
until str.empty? do
case str
when /\A\s+/
str = $'
when /\A\w+/
@q.push [:ITEM, $&]
str = $'
else
c = str[0,1]
@q.push [c, c]
str = str[1..-1]
end
end
@q.push [false, '$'] # optional from 1.3.7
do_parse
end
def next_token
@q.shift
end
---- footer
if $0 == __FILE__ then
src = <<EOS
[
a, b, c,
d,
e ]
EOS
puts 'parsing:'
print src
puts
puts 'result:'
p ArrayParser.new.parse( src )
end