Module: Net::IMAP::ResponseParser::ParserUtils::Generator

Included in:
Net::IMAP::ResponseParser
Defined in:
lib/net/imap/response_parser/parser_utils.rb

Overview

:nodoc:

Constant Summary collapse

LOOKAHEAD =
"(@token ||= next_token)"
SHIFT_TOKEN =
"(@token = nil)"

Instance Method Summary collapse

Instance Method Details

#def_char_matchers(name, char, token) ⇒ Object

we can skip lexer for single character matches, as a shortcut



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
# File 'lib/net/imap/response_parser/parser_utils.rb', line 17

def def_char_matchers(name, char, token)
  match_name = name.match(/\A[A-Z]/) ? "#{name}!" : name
  char = char.dump
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    # frozen_string_literal: true

    # force use of #next_token; no string peeking
    def lookahead_#{name}?
      #{LOOKAHEAD}&.symbol == #{token}
    end

    # use token or string peek
    def peek_#{name}?
      @token ? @token.symbol == #{token} : @str[@pos] == #{char}
    end

    # like accept(token_symbols); returns token or nil
    def #{name}?
      if @token&.symbol == #{token}
        #{SHIFT_TOKEN}
        #{char}
      elsif !@token && @str[@pos] == #{char}
        @pos += 1
        #{char}
      end
    end

    # like match(token_symbols); returns token or raises parse_error
    def #{match_name}
      if @token&.symbol == #{token}
        #{SHIFT_TOKEN}
        #{char}
      elsif !@token && @str[@pos] == #{char}
        @pos += 1
        #{char}
      else
        parse_error("unexpected %s (expected %p)",
                    @token&.symbol || @str[@pos].inspect, #{char})
      end
    end
  RUBY
end

#def_token_matchers(name, *token_symbols, coerce: nil, send: nil) ⇒ Object

TODO: move coersion to the token.value method?



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
# File 'lib/net/imap/response_parser/parser_utils.rb', line 61

def def_token_matchers(name, *token_symbols, coerce: nil, send: nil)
  match_name = name.match(/\A[A-Z]/) ? "#{name}!" : name

  if token_symbols.size == 1
    token   = token_symbols.first
    matcher = "token&.symbol == %p" % [token]
    desc    = token
  else
    matcher = "%p.include? token&.symbol" % [token_symbols]
    desc    = token_symbols.join(" or ")
  end

  value = "(token.value)"
  value = coerce.to_s + value   if coerce
  value = [value, send].join(".") if send

  raise_parse_error = <<~RUBY
    parse_error("unexpected %s (expected #{desc})", token&.symbol)
  RUBY

  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    # frozen_string_literal: true

    # lookahead version of match, returning the value
    def lookahead_#{name}!
      token = #{LOOKAHEAD}
      if #{matcher}
        #{value}
      else
        #{raise_parse_error}
      end
    end

    def #{name}?
      token = #{LOOKAHEAD}
      if #{matcher}
        #{SHIFT_TOKEN}
        #{value}
      end
    end

    def #{match_name}
      token = #{LOOKAHEAD}
      if #{matcher}
        #{SHIFT_TOKEN}
        #{value}
      else
        #{raise_parse_error}
      end
    end
  RUBY

end