Module: Pray::StatementSurface

Defined in:
lib/pray/statement_surface.rb

Defined Under Namespace

Classes: Reader

Class Method Summary collapse

Class Method Details

.expand_brace_block(statement) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pray/statement_surface.rb', line 24

def expand_brace_block(statement)
  keyword = leading_identifier(statement)
  return nil unless keyword

  after_keyword = statement[keyword.length..].lstrip
  # Only keyword{…}, keyword(…){…}, or keyword "…"{…} — not spec.exports = {…}.
  header = split_brace_header(after_keyword)
  return nil unless header

  args, after_open = header
  close_offset = matching_close_brace(after_open)
  return nil unless close_offset
  return nil unless after_open[(close_offset + 1)..].to_s.strip.empty?

  body = after_open[0...close_offset].strip
  return nil unless Literal.is_balanced?(body)

  header_args = unwrap_outer_parens(args)
  open = header_args.empty? ? "#{keyword} do" : "#{keyword} #{header_args} do"
  output = [open]
  output.concat(expand_statement_surface(body)) unless body.empty?
  output << "end"
  output
end

.expand_one_surface(statement) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/pray/statement_surface.rb', line 14

def expand_one_surface(statement)
  trimmed = statement.strip
  return [] if trimmed.empty?

  braced = expand_brace_block(trimmed)
  return braced if braced

  [normalize_keyword_call(trimmed)]
end

.expand_statement_surface(statement) ⇒ Object



7
8
9
10
11
12
# File 'lib/pray/statement_surface.rb', line 7

def expand_statement_surface(statement)
  trimmed = statement.strip
  return [] if trimmed.empty?

  Literal.split_top_level(trimmed, ";").flat_map { |segment| expand_one_surface(segment) }
end

.find_top_level_char(input, needle) ⇒ Object



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
# File 'lib/pray/statement_surface.rb', line 216

def find_top_level_char(input, needle)
  depth = 0
  quote = nil
  escaped = false
  input.each_char.with_index do |character, index|
    if quote
      if escaped
        escaped = false
      elsif character == "\\"
        escaped = true
      elsif character == quote
        quote = nil
      end
      next
    end

    case character
    when '"', "'"
      quote = character
    when "(", "[", "{"
      return index if depth.zero? && character == needle

      depth += 1
    when ")", "]", "}"
      depth -= 1
    else
      return index if depth.zero? && character == needle
    end
  end
  nil
end

.leading_identifier(input) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pray/statement_surface.rb', line 129

def leading_identifier(input)
  trimmed = input.lstrip
  end_index = 0
  while end_index < trimmed.length
    character = trimmed[end_index]
    break unless character.match?(/[A-Za-z0-9_]/)

    end_index += 1
  end
  return nil if end_index.zero?

  ident = trimmed[0...end_index]
  return nil unless ident[0].match?(/[A-Za-z]/)

  ident
end

.matching_close_brace(input) ⇒ Object



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
# File 'lib/pray/statement_surface.rb', line 159

def matching_close_brace(input)
  depth = 1
  quote = nil
  escaped = false
  input.each_char.with_index do |character, index|
    if quote
      if escaped
        escaped = false
      elsif character == "\\"
        escaped = true
      elsif character == quote
        quote = nil
      end
      next
    end

    case character
    when '"', "'"
      quote = character
    when "{"
      depth += 1
    when "}"
      depth -= 1
      return index if depth.zero?
    end
  end
  nil
end

.matching_close_delimited(input, open, close) ⇒ Object



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
# File 'lib/pray/statement_surface.rb', line 188

def matching_close_delimited(input, open, close)
  return nil unless input.start_with?(open)

  depth = 0
  quote = nil
  escaped = false
  input.each_char.with_index do |character, index|
    if quote
      if escaped
        escaped = false
      elsif character == "\\"
        escaped = true
      elsif character == quote
        quote = nil
      end
      next
    end

    if character == open
      depth += 1
    elsif character == close
      depth -= 1
      return index if depth.zero?
    end
  end
  nil
end

.matching_close_paren(input) ⇒ Object



155
156
157
# File 'lib/pray/statement_surface.rb', line 155

def matching_close_paren(input)
  matching_close_delimited(input, "(", ")")
end

.normalize_keyword_call(statement) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pray/statement_surface.rb', line 71

def normalize_keyword_call(statement)
  spaced = normalize_spaced_block_opener(statement)
  return spaced if spaced

  keyword = leading_identifier(statement)
  return statement unless keyword

  after_keyword = statement[keyword.length..].lstrip
  return statement unless after_keyword.start_with?("(")

  close = matching_close_paren(after_keyword)
  return statement unless close

  inner = after_keyword[1...close].strip
  trailing = after_keyword[(close + 1)..].to_s.strip
  trailing.empty? ? "#{keyword} #{inner}" : "#{keyword} #{inner} #{trailing}"
end

.normalize_spaced_block_opener(statement) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/pray/statement_surface.rb', line 89

def normalize_spaced_block_opener(statement)
  trimmed = statement.strip
  %w[pray template].each do |keyword|
    next unless trimmed.start_with?(keyword)

    rest = trimmed[keyword.length..].lstrip
    return "#{keyword} do" if rest == "do"
  end
  nil
end

.split_brace_header(after_keyword) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pray/statement_surface.rb', line 49

def split_brace_header(after_keyword)
  if after_keyword.start_with?("{")
    return ["", after_keyword[1..]]
  end
  if after_keyword.start_with?("(")
    close = matching_close_paren(after_keyword)
    return nil unless close

    trailing = after_keyword[(close + 1)..].to_s.lstrip
    return nil unless trailing.start_with?("{")

    return [after_keyword[1...close].strip, trailing[1..]]
  end
  first = after_keyword[0]
  return nil unless ['"', "'", ":"].include?(first)

  brace_offset = find_top_level_char(after_keyword, "{")
  return nil unless brace_offset

  [after_keyword[0...brace_offset].strip, after_keyword[(brace_offset + 1)..]]
end

.split_symbol_assignment(statement) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pray/statement_surface.rb', line 100

def split_symbol_assignment(statement)
  trimmed = statement.strip
  call = split_symbol_call(trimmed)
  return call if call

  match = trimmed.match(/\A(\S+)\s+(.+)\z/)
  return nil unless match

  key = match[1].strip
  value = match[2].strip
  return nil if key.empty? || value.empty?

  [key, value]
end

.split_symbol_call(statement) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pray/statement_surface.rb', line 115

def split_symbol_call(statement)
  key = leading_identifier(statement)
  return nil unless key

  after_key = statement[key.length..].lstrip
  return nil unless after_key.start_with?("(") && after_key.end_with?(")")
  return nil unless matching_close_paren(after_key) == after_key.length - 1

  inner = after_key[1...-1].strip
  return nil if inner.empty?

  [key, inner]
end

.unwrap_outer_parens(input) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/pray/statement_surface.rb', line 146

def unwrap_outer_parens(input)
  trimmed = input.strip
  if trimmed.start_with?("(") && matching_close_paren(trimmed) == trimmed.length - 1
    return trimmed[1...-1].strip
  end

  trimmed
end