#!/usr/bin/ruby =begin S式のパーサ =end class SParser def SParser.parse(istream) parserd = [] last_by_level = [parsed] current = [] on_string = false on_ignore = true while !istream.eof? c = istream.read(1) if on_string then if c == '\\' then if c = istream.read(1) then current << c end elsif c == '"' then on_string = false else current << c end else case c when '\\': if c = istream.read(1) then current << c end when '(': if current && !current.empty? then last_by_level.last << current.join current = [] end next_level = [] last_by_level.last << next_level last_by_level << next_level when ')': if current && !current.empty? then last_by_level.last << current.join current = [] end last_by_level.pop when '"': on_string = true when /\s/: if current && !current.empty? then last_by_level.last << current.join current = [] end else current << c end end end if last_by_level.size != 1 then raise Exception::new("incorrect EDIF!!") end return parsed end end if $0 == __FILE__ then open(ARGV.shift){|io| p SParser.parse(io) } end