Skip to content

Commit 4d58cec

Browse files
committed
♻️ Extract ParserUtils module
1 parent a40c0cf commit 4d58cec

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed

lib/net/imap/response_parser.rb

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# frozen_string_literal: true
22

33
require_relative "errors"
4+
require_relative "response_parser/parser_utils"
45

56
module Net
67
class IMAP < Protocol
78

89
# Parses an \IMAP server response.
910
class ResponseParser
11+
include ParserUtils
12+
1013
# :call-seq: Net::IMAP::ResponseParser.new -> Net::IMAP::ResponseParser
1114
def initialize
1215
@str = nil
@@ -1422,46 +1425,6 @@ def accept_spaces
14221425
end
14231426
end
14241427

1425-
def match(*args, lex_state: @lex_state)
1426-
if @token && lex_state != @lex_state
1427-
parse_error("invalid lex_state change to %s with unconsumed token",
1428-
lex_state)
1429-
end
1430-
begin
1431-
@lex_state, original_lex_state = lex_state, @lex_state
1432-
token = lookahead
1433-
unless args.include?(token.symbol)
1434-
parse_error('unexpected token %s (expected %s)',
1435-
token.symbol.id2name,
1436-
args.collect {|i| i.id2name}.join(" or "))
1437-
end
1438-
shift_token
1439-
return token
1440-
ensure
1441-
@lex_state = original_lex_state
1442-
end
1443-
end
1444-
1445-
# like match, but does not raise error on failure.
1446-
#
1447-
# returns and shifts token on successful match
1448-
# returns nil and leaves @token unshifted on no match
1449-
def accept(*args)
1450-
token = lookahead
1451-
if args.include?(token.symbol)
1452-
shift_token
1453-
token
1454-
end
1455-
end
1456-
1457-
def lookahead
1458-
@token ||= next_token
1459-
end
1460-
1461-
def shift_token
1462-
@token = nil
1463-
end
1464-
14651428
def next_token
14661429
case @lex_state
14671430
when EXPR_BEG
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class IMAP < Protocol
5+
class ResponseParser
6+
# basic utility methods for parsing.
7+
#
8+
# (internal API, subject to change)
9+
module ParserUtils # :nodoc:
10+
11+
private
12+
13+
def match(*args, lex_state: @lex_state)
14+
if @token && lex_state != @lex_state
15+
parse_error("invalid lex_state change to %s with unconsumed token",
16+
lex_state)
17+
end
18+
begin
19+
@lex_state, original_lex_state = lex_state, @lex_state
20+
token = lookahead
21+
unless args.include?(token.symbol)
22+
parse_error('unexpected token %s (expected %s)',
23+
token.symbol.id2name,
24+
args.collect {|i| i.id2name}.join(" or "))
25+
end
26+
shift_token
27+
return token
28+
ensure
29+
@lex_state = original_lex_state
30+
end
31+
end
32+
33+
# like match, but does not raise error on failure.
34+
#
35+
# returns and shifts token on successful match
36+
# returns nil and leaves @token unshifted on no match
37+
def accept(*args)
38+
token = lookahead
39+
if args.include?(token.symbol)
40+
shift_token
41+
token
42+
end
43+
end
44+
45+
def lookahead
46+
@token ||= next_token
47+
end
48+
49+
def shift_token
50+
@token = nil
51+
end
52+
end
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)