From: Charles D Hixson Date: 2001-09-05T07:08:29+09:00 Subject: [ruby-talk:20893] Code Style (a bit long) I have the beginnings of a string parser here, but the style feels wrong. Is there a more Ruby-esque way to do this that doesn't rely on using the built-in Tree function? (I wouldn't want this to be restricted to Ruby syntax. That's just where I'm starting.) 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 = "" str_ndx = 0 while (ch = aFile.getc) case ch.chr when "#" 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 last = "\n" when "'" 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 << case c when "a" then 0x07 # Bell when "b" then 0x08 # Backspace when "e" then 0x1b # Escape when "f" then 0x0c # Formfeed when "n" then 0x0a # New Line when "r" then 0x0d # Carriage Return when "s" then 0x20 # Space when "t" then 0x09 # Tab when "v" then 0x0b # Vertical Tab when "\\" then "\\" # BackSlash end last = "" else str << last last = c end end strings[str_ndx] = str body << "strings[#{str_ndx}]" str_ndx += 1 when '"' 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 << case c when "a" then 0x07 # Bell when "b" then 0x08 # Backspace when "e" then 0x1b # Escape when "f" then 0x0c # Formfeed when "n" then 0x0a # New Line when "r" then 0x0d # Carriage Return when "s" then 0x20 # Space when "t" then 0x09 # Tab when "v" then 0x0b # Vertical Tab when "\\" then "\\" # BackSlash end 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_ndx] = str body << "strings[#{str_ndx}]" str_ndx += 1 else body << ch end # case end # while print "\nstrings: #{str_ndx}\n", strings.inspect print "\ncomments: #{com_ndx}\n", comments.inspect, "\n" print body -- Charles Hixson Copy software legally, the GNU way! Use GNU software, and legally make and share copies of software. See: http://www.gnu.org http://www.redhat.com http://www.linux-mandrake.com http://www.calderasystems.com http://www.linuxapps.com