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 =

:nodoc:

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

def def_char_matchers(name, char, token)
  byte = char.ord
  match_name = name.match(/\A[A-Z]/) ? "#{name}!" : name
  char = char.dump
  class_eval "    # frozen_string_literal: true\n\n    # force use of #next_token; no string peeking\n    def lookahead_\#{name}?\n      \#{LOOKAHEAD}&.symbol == \#{token}\n    end\n\n    # use token or string peek\n    def peek_\#{name}?\n      @token ? @token.symbol == \#{token} : @str.getbyte(@pos) == \#{byte}\n    end\n\n    # like accept(token_symbols); returns token or nil\n    def \#{name}?\n      if @token&.symbol == \#{token}\n        \#{SHIFT_TOKEN}\n        \#{char}\n      elsif !@token && @str.getbyte(@pos) == \#{byte}\n        @pos += 1\n        \#{char}\n      end\n    end\n\n    # like match(token_symbols); returns token or raises parse_error\n    def \#{match_name}\n      if @token&.symbol == \#{token}\n        \#{SHIFT_TOKEN}\n        \#{char}\n      elsif !@token && @str.getbyte(@pos) == \#{byte}\n        @pos += 1\n        \#{char}\n      else\n        parse_error(\"unexpected %s (expected %p)\",\n                    @token&.symbol || @str[@pos].inspect, \#{char})\n      end\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

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

TODO: move coersion to the token.value method?



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 62

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 = "    parse_error(\"unexpected %s (expected \#{desc})\", token&.symbol)\n  RUBY\n\n  class_eval <<~RUBY, __FILE__, __LINE__ + 1\n    # frozen_string_literal: true\n\n    # lookahead version of match, returning the value\n    def lookahead_\#{name}!\n      token = \#{LOOKAHEAD}\n      if \#{matcher}\n        \#{value}\n      else\n        \#{raise_parse_error}\n      end\n    end\n\n    def \#{name}?\n      token = \#{LOOKAHEAD}\n      if \#{matcher}\n        \#{SHIFT_TOKEN}\n        \#{value}\n      end\n    end\n\n    def \#{match_name}\n      token = \#{LOOKAHEAD}\n      if \#{matcher}\n        \#{SHIFT_TOKEN}\n        \#{value}\n      else\n        \#{raise_parse_error}\n      end\n    end\n  RUBY\nend\n"