$aFile = File.open("riddle.rb", "r") $strings = [] ; $str_ndx = 0 $comments = [] ; $com_ndx = 0 ; $AString = /"|'|%q|%Q/ $body = "" # state table # 0 normal text # 1 in a # comment # 2 in a ' string # 3 in a " string # 4 in a ` string (command string) # 5 in a %q string (delimited single quote string) # 6 in a %Q string (delimited double quote string) # 7 in a / string (regexp pattern) $state = 0 # read in the file $str_ndx = 0 def escapeInHex(c) case c when "a" then return 0x07 # Bell when "b" then return 0x08 # Backspace when "e" then return 0x1b # Escape when "f" then return 0x0c # Formfeed when "n" then return 0x0a # New Line when "r" then return 0x0d # Carriage Return when "s" then return 0x20 # Space when "t" then return 0x09 # Tab when "v" then return 0x0b # Vertical Tab when "\\" then return "\\" # BackSlash end return '' end def comment str = "" last = "" while (c = $aFile.getc) and (c.chr != "\n") str << c end $comments[$com_ndx] = str $body << "#comments[#{$com_ndx}]\n" $com_ndx += 1 end def singleString str = "" last = "" while (c2 = $aFile.getc) c = c2.chr if c == "'" and last == "\\" str << c last = "" elsif c == "'" str << last break elsif (last == "\\") and (c =~ /[abefnrstv]|\\/) str << escapeInHex(c) last = "" else str << last last = c end end $strings << str $body << "strings[#{$strings.size}]" end def doubleString str = "" last = "" while (c2 = $aFile.getc) c = c2.chr if c == "\"" and last == "\\" str << c last = "" elsif c == "\"" str << last break elsif (last == "\\") and (c =~ /[abefnrstv]|\\/) str << escapeInHex(c) last = "" elsif (last == "\\") and (c =~ /[0-7]/) print "octal parsing not yet implemented" str << last last == c elsif (last == "\\") and (c =~ /[xX]/) print "hex parsing not yet implemented" str << last last == c elsif (last == "\\") and (c =~ "[cC]") print "control parsing not yet implemented" str << last last == c elsif (last == "\\") and (c == "M") print "Meta parsing not yet implemented" str << last last == c elsif (last == "\\") and (c == "M") print "Meta-control parsing not yet implemented" str << last last == c elsif (last == "#") and (c == "{") print "expression parsing not yet implemented" str << last last == c else str << last last = c end end $strings << str $body << "strings[#{$strings.size}]" end while (ch = $aFile.getc) case ch.chr when "#" comment when "'" singleString when '"' doubleString else $body << ch end # case end # while print "\nstrings: #{$strings.size}\n", $strings.inspect print "\ncomments: #{$comments.size}\n", $comments.inspect, "\n" print $body