1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#!/usr/bin/env ruby -ws
$v ||= false
stack = []
last_token = nil
reduce_line = nil
def munge s
renames = [
"'='", "tEQL",
"'!'", "tBANG",
"'%'", "tPERCENT",
"'&'", "tAMPER2",
"'('", "tLPAREN2",
"')'", "tRPAREN",
"'*'", "tSTAR2",
"'+'", "tPLUS",
"','", "tCOMMA",
"'-'", "tMINUS",
"'.'", "tDOT",
"'/'", "tDIVIDE",
"';'", "tSEMI",
"':'", "tCOLON",
"'<'", "tLT",
"'>'", "tGT",
"'?'", "tEH",
"'['", "tLBRACK",
"'\\n'", "tNL",
"']'", "tRBRACK",
"'^'", "tCARET",
"'`'", "tBACK_REF2",
"'{'", "tLCURLY",
"'|'", "tPIPE",
"'}'", "tRCURLY",
"'~'", "tTILDE",
'"["', "tLBRACK",
# 2.0 changes?
'"<=>"', "tCMP",
'"=="', "tEQ",
'"==="', "tEQQ",
'"!~"', "tNMATCH",
'"=~"', "tMATCH",
'">="', "tGEQ",
'"<="', "tLEQ",
'"!="', "tNEQ",
'"<<"', "tLSHFT",
'">>"', "tRSHFT",
'"*"', "tSTAR",
'".."', "tDOT2",
'"&"', "tAMPER",
'"&&"', "tANDOP",
'"&."', "tLONELY",
'"||"', "tOROP",
'"..."', "tDOT3",
'"**"', "tPOW",
'"unary+"', "tUPLUS",
'"unary-"', "tUMINUS",
'"[]"', "tAREF",
'"[]="', "tASET",
'"::"', "tCOLON2",
'"{ arg"', "tLBRACE_ARG",
'"( arg"', "tLPAREN_ARG",
'"("', "tLPAREN",
'rparen', "tRPAREN",
'"{"', "tLBRACE",
'"=>"', "tASSOC",
'"->"', "tLAMBDA",
'":: at EXPR_BEG"', "tCOLON3",
'"**arg"', "tDSTAR",
'","', "tCOMMA",
# other
'kTERMINATOR', "tSTRING_END",
'"kTERMINATOR"', "tSTRING_END",
'kTRCURLY', "tSTRING_DEND",
'"symbol literal"', "tSYMBEG",
'"string literal"', "tSTRING_BEG",
'"backtick literal"', "tXSTRING_BEG",
'"regexp literal"', "tREGEXP_BEG",
'"word list"', "tWORDS_BEG",
'"verbatim word list"', "tQWORDS_BEG",
'"symbol list"', "tSYMBOLS_BEG",
'"verbatim symbol list"', "tQSYMBOLS_BEG",
'"terminator"', "tSTRING_END",
'"\'}\'"', "tSTRING_DEND",
'"string literal"',"tSTRING_BEG",
'"literal content"', "tSTRING_CONTENT",
/\$/, "", # try to remove these lumps?
'tLBRACK2', "tLBRACK", # HACK
"' '", "tSPACE", # needs to be later to avoid bad hits
"/* empty */", "none",
/^\s*$/, "",
"keyword_BEGIN", "klBEGIN",
"keyword_END", "klEND",
/keyword_(\w+)/, proc { "k#{$1.upcase}" },
/\bk_([a-z_]+)/, proc { "k#{$1.upcase}" },
/modifier_(\w+)/, proc { "k#{$1.upcase}_MOD" },
"kVARIABLE", "keyword_variable", # ugh
"tCONST", "kCONST",
# 2.6 collapses klBEGIN to kBEGIN
"klBEGIN", "kBEGIN",
"klEND", "kEND",
/keyword_(\w+)/, proc { "k#{$1.upcase}" },
/\bk_([^_][a-z_]+)/, proc { "k#{$1.upcase}" },
/modifier_(\w+)/, proc { "k#{$1.upcase}_MOD" },
"kVARIABLE", "keyword_variable", # ugh: this is a rule name
# UGH
"k_LINE__", "k__LINE__",
"k_FILE__", "k__FILE__",
"k_ENCODING__", "k__ENCODING__",
'"defined?"', "kDEFINED",
"<none>", "none",
'"do (for condition)"', "kDO_COND",
'"do (for lambda)"', "kDO_LAMBDA",
'"do (for block)"', "kDO_BLOCK",
'"local variable or method"', "tIDENTIFIER",
/\"(\w+) \(modifier\)\"/, proc { |x| "k#{$1.upcase}_MOD" },
/\"(\w+)\"/, proc { |x| "k#{$1.upcase}" },
/\"`(\w+)'\"/, proc { |x| "k#{$1.upcase}" },
/@(\d+)(\s+|$)/, "",
/\$?@(\d+) */, "", # TODO: remove?
/_EXPR/, "",
]
renames.each_slice(2) do |(a, b)|
if Proc === b then
s.gsub!(a, &b)
else
s.gsub!(a, b)
end
end
if s.empty? then
nil
else
s.strip.squeeze " "
end
end
ARGF.each_line do |line|
case line
when /^(Stack now|Entering state|Shifting|Cleanup|Starting)/ then
# do nothing
when /^vtable_/ then
# do nothing
when /Gem::MissingSpecError/ then
# do nothing -- ruby 2.5 is being bitchy?
when /^Reading a token: Next token is token (.*?) \(\)/ then
token = munge $1
next if last_token == token
puts "next token is %p" % [token]
last_token = token
when /^Reading a token: / then
next # skip
when /^Reading a token$/ then # wtf?
next # skip
when /^(?:add_delayed_token|parser_dispatch)/ then # dunno what this is yet
next # skip
when /^read\s+:(\w+)/ then # read :tNL(tNL) nil
token = munge $1
next if last_token == token
puts "next token is %p" % [token]
last_token = token
when /^Next token is token ("[^"]+"|\S+)/ then
token = munge $1
next if last_token == token
puts "next token is %p" % [token]
last_token = token
when /^read\s+false/ then # read false($end) "$end"
puts "next token is EOF"
when /^Now at end of input./ then
# do nothing
when /^.:scan=>\["([^"]+)"/ then
puts "scan = %p" % [$1]
when /^.:getch=>\["([^"]+)/ then
puts "SCAN = %p" % [$1]
when /^Reducing stack by rule (\d+) \(line (\d+)\):/ then
reduce_line = $2.to_i
when /^ \$\d+ = (?:token|nterm) (.+) \(.*\)/ then
item = $1
stack << munge(item)
when /^-> \$\$ = (?:token|nterm) (.+) \(.*\)/ then
stack << "none" if stack.empty?
item = munge $1
x = stack.compact.map { |s| munge s.strip }.compact.join " "
if x != item then # prevent kdef -> kdef
if $v && reduce_line then
puts "reduce #{x} --> #{item} at #{reduce_line}".squeeze " "
else
puts "reduce #{x} --> #{item}".squeeze " "
end
puts
end
reduce_line = nil
stack.clear
when /^reduce/ then # ruby_parser side
s = munge line.chomp
next if s =~ /reduce\s+(\w+) --> \1/
puts s
puts
when /^(\w+_stack)\.(\w+)/ then
# TODO: make pretty, but still informative w/ line numbers etc
puts line.gsub("true", "1").gsub("false", "0")
# puts "#{$1}(#{$2})"
when /^(\w+_stack(\(\w+\))?: \S+)/ then
# _data = $v ? line.chomp : $1
# puts line
# TODO: make pretty, but still informative w/ line numbers etc
puts line.gsub("true", "1").gsub("false", "0")
when /^lex_state: :?([\w|()]+) -> :?([\w|]+)(?: (?:at|from) (.*))?/ then
a, b, c = $1.upcase, $2.upcase, $3
a.gsub!(/EXPR_/, "")
b.gsub!(/EXPR_/, "")
if c && $v then
puts "lex_state: #{a} -> #{b} at #{c}"
else
puts "lex_state: #{a} -> #{b}"
end
when /debug|FUCK/ then
puts line.chomp
when /^(#.*parse error|on )/ then
puts line.chomp
when /^(goto|shift| +\[|$)/ then # racc
# do nothing
# when /^Reading a token: Now at end of input./ then
# # puts "EOF"
# when /^Reading a token: Next token is token (.+)/ then
# puts "READ: #{$1.inspect}"
when /^accept/ then
puts "DONE"
else
puts "unparsed: #{line.chomp}"
end
end
|