Skip to content

Commit bd2ddc0

Browse files
committed
🔎 Improve parse error debugging
This is especially helpful when making big changes to the parser. :)
1 parent 1d072c2 commit bd2ddc0

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

lib/net/imap/response_parser.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,20 +1592,6 @@ def next_token
15921592
end
15931593
end
15941594

1595-
def parse_error(fmt, *args)
1596-
if IMAP.debug
1597-
$stderr.printf("@str: %s\n", @str.dump)
1598-
$stderr.printf("@pos: %d\n", @pos)
1599-
$stderr.printf("@lex_state: %s\n", @lex_state)
1600-
if @token
1601-
$stderr.printf("@token.symbol: %s\n", @token.symbol)
1602-
$stderr.printf("@token.value: %s\n", @token.value.inspect)
1603-
end
1604-
end
1605-
raise ResponseParseError, format(fmt, *args)
1606-
end
16071595
end
1608-
16091596
end
1610-
16111597
end

lib/net/imap/response_parser/parser_utils.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ def lookahead
5454
def shift_token
5555
@token = nil
5656
end
57+
58+
def parse_error(fmt, *args)
59+
msg = format(fmt, *args)
60+
if IMAP.debug
61+
local_path = File.dirname(__dir__)
62+
tok = @token ? "%s: %p" % [@token.symbol, @token.value] : "nil"
63+
warn "%s %s: %s" % [self.class, __method__, msg]
64+
warn " tokenized : %s" % [@str[...@pos].dump]
65+
warn " remaining : %s" % [@str[@pos..].dump]
66+
warn " @lex_state: %s" % [@lex_state]
67+
warn " @pos : %d" % [@pos]
68+
warn " @token : %s" % [tok]
69+
caller_locations(1..20).each_with_index do |cloc, idx|
70+
next unless cloc.path&.start_with?(local_path)
71+
warn " caller[%2d]: %-30s (%s:%d)" % [
72+
idx,
73+
cloc.base_label,
74+
File.basename(cloc.path, ".rb"),
75+
cloc.lineno
76+
]
77+
end
78+
end
79+
raise ResponseParseError, msg
80+
end
81+
5782
end
5883
end
5984
end

0 commit comments

Comments
 (0)