Class: Packcr::Node::CharclassNode

Inherits:
Packcr::Node show all
Defined in:
lib/packcr/node/charclass_node.rb,
lib/packcr/generated/node/charclass_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Packcr::Node

#alt, #link_references, #nodes, #seq, #sequence?, #setup, #setup_rule, #verify_captures, #verify_variables

Constructor Details

#initialize(value = nil) ⇒ CharclassNode

Returns a new instance of CharclassNode.



6
7
8
# File 'lib/packcr/node/charclass_node.rb', line 6

def initialize(value = nil)
  @value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/packcr/node/charclass_node.rb', line 4

def value
  @value
end

Instance Method Details

#debug_dump(indent = 0) ⇒ Object



10
11
12
13
14
# File 'lib/packcr/node/charclass_node.rb', line 10

def debug_dump(indent = 0)
  $stdout.print "#{" " * indent}Charclass(value:'"
  Packcr.dump_escaped_string(value)
  $stdout.print "')\n"
end

#generate_code(gen, onfail, indent, unwrap, oncut: nil) ⇒ Object



20
21
22
23
24
# File 'lib/packcr/node/charclass_node.rb', line 20

def generate_code(gen, onfail, indent, unwrap, oncut: nil)
  return generate_ascii_code(gen, onfail, indent, unwrap) if gen.ascii

  generate_utf8_charclass_code(gen, onfail, indent, unwrap)
end

#generate_reverse_code(gen, onsuccess, indent, unwrap, oncut: nil) ⇒ Object



26
27
28
29
30
# File 'lib/packcr/node/charclass_node.rb', line 26

def generate_reverse_code(gen, onsuccess, indent, unwrap, oncut: nil)
  raise "unexpected" if gen.ascii

  generate_utf8_charclass_reverse_code(gen, onsuccess, indent, unwrap)
end

#get_any_code(gen, onfail, indent, unwrap, charclass) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/packcr/generated/node/charclass_node.rb', line 100

def get_any_code(gen, onfail, indent, unwrap, charclass)
  case gen.lang
  when :c
    erbout = +""
    erbout << "if (packcr_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n".freeze

    if gen.location
      erbout << "packcr_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
    end
    erbout << "ctx->position_offset++;\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "if refill_buffer(1) < 1\n  throw(#{onfail})\nend\n".freeze

    if gen.location
      erbout << "@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
    end
    erbout << "@position_offset += 1\n".freeze

    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#get_code(gen, onfail, indent, unwrap, charclass, n, a) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
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
60
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
# File 'lib/packcr/generated/node/charclass_node.rb', line 4

def get_code(gen, onfail, indent, unwrap, charclass, n, a)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n    char c;\n    if (packcr_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n    c = ctx->buffer.buf[ctx->position_offset];\n".freeze
    if !a && charclass =~ /\A[^\\]-.\z/

      if a
        erbout << "    if (c >= '#{Packcr.escape_character(charclass[0])}' && c <= '#{Packcr.escape_character(charclass[2])}') goto L#{format("%04d", onfail)};\n".freeze

      else
        erbout << "    if (!(c >= '#{Packcr.escape_character(charclass[0])}' && c <= '#{Packcr.escape_character(charclass[2])}')) goto L#{format("%04d", onfail)};\n".freeze
      end

    else

      if a
        erbout << "    if (\n".freeze

      else
        erbout << "    if (!(\n".freeze
      end
      i = 0
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        if i + 2 < n && charclass[i + 1] == "-"
          erbout << "        (c >= '#{Packcr.escape_character(charclass[i])}' && c <= '#{Packcr.escape_character(charclass[i + 2])}')#{i + 3 == n ? "" : " ||"}\n".freeze

          i += 2
        else
          erbout << "        c == '#{Packcr.escape_character(charclass[i])}'#{i + 1 == n ? "" : " ||"}\n".freeze
        end
        i += 1
      end
      if a
        erbout << "    ) goto L#{format("%04d", onfail)};\n".freeze

      else
        erbout << "    )) goto L#{format("%04d", onfail)};\n".freeze
      end
    end
    if gen.location
      erbout << "    packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
    end
    erbout << "    ctx->position_offset++;\n}\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "if refill_buffer(1) < 1\n  throw(#{onfail})\nend\nc#{gen.level} = @buffer[@position_offset]\n".freeze
    if !a && charclass =~ /\A[^\\]-.\z/

      if a
        erbout << "if c#{gen.level} >= \"#{Packcr.escape_character(charclass[0])}\" && c#{gen.level} <= \"#{Packcr.escape_character(charclass[2])}\"\n  throw(#{onfail})\nend\n".freeze

      else
        erbout << "unless c#{gen.level} >= \"#{Packcr.escape_character(charclass[0])}\" && c#{gen.level} <= \"#{Packcr.escape_character(charclass[2])}\"\n  throw(#{onfail})\nend\n".freeze
      end

    else

      if a
        erbout << "if (\n".freeze

      else
        erbout << "unless (\n".freeze
      end
      i = 0
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        if i + 2 < n && charclass[i + 1] == "-"
          erbout << "  (c#{gen.level} >= \"#{Packcr.escape_character(charclass[i])}\" && c#{gen.level} <= \"#{Packcr.escape_character(charclass[i + 2])}\")#{i + 3 == n ? "" : " ||"}\n".freeze

          i += 2
        else
          erbout << "  c#{gen.level} == \"#{Packcr.escape_character(charclass[i])}\"#{i + 1 == n ? "" : " ||"}\n".freeze
        end
        i += 1
      end
      erbout << ")\n  throw(#{onfail})\nend\n".freeze

    end
    if gen.location
      erbout << "@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
    end
    erbout << "@position_offset += 1\n".freeze
    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#get_fail_code(gen, onfail, indent, unwrap) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/packcr/generated/node/charclass_node.rb', line 127

def get_fail_code(gen, onfail, indent, unwrap)
  case gen.lang
  when :c
    erbout = +""
    erbout << "goto L#{format("%04d", onfail)};\n".freeze

    erbout
  when :rb
    erbout = +""
    erbout << "throw(#{onfail})\n".freeze

    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#get_one_code(gen, onfail, indent, unwrap, charclass, n, a) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/packcr/generated/node/charclass_node.rb', line 144

def get_one_code(gen, onfail, indent, unwrap, charclass, n, a)
  case gen.lang
  when :c
    erbout = +""
    if a
      erbout << "if (\n    packcr_refill_buffer(ctx, 1) < 1 ||\n    ctx->buffer.buf[ctx->position_offset] == '#{Packcr.escape_character(charclass[i])}'\n) goto L#{format("%04d", onfail)};\n".freeze

    else
      erbout << "if (\n    packcr_refill_buffer(ctx, 1) < 1 ||\n    ctx->buffer.buf[ctx->position_offset] != '#{Packcr.escape_character(charclass[0])}'\n) goto L#{format("%04d", onfail)};\n".freeze

    end
    if gen.location
      erbout << "    packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
    end
    erbout << "ctx->position_offset++;\n".freeze
    erbout
  when :rb
    erbout = +""
    if a
      erbout << "if (\n  refill_buffer(1) < 1 ||\n  @buffer[@position_offset] == \"#{Packcr.escape_character(charclass[0])}\"\n)\n  throw(#{onfail})\nend\n".freeze

    else
      erbout << "if (\n  refill_buffer(1) < 1 ||\n  @buffer[@position_offset] != \"#{Packcr.escape_character(charclass[0])}\"\n)\n  throw(#{onfail})\nend\n".freeze

    end
    if gen.location
      erbout << "@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
    end
    erbout << "@position_offset += 1\n".freeze
    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#get_utf8_code(gen, onfail, indent, unwrap, charclass, n) ⇒ Object



179
180
181
182
183
184
185
186
187
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
215
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/packcr/generated/node/charclass_node.rb', line 179

def get_utf8_code(gen, onfail, indent, unwrap, charclass, n)
  case gen.lang
  when :c
    erbout = +""
    a = charclass && charclass[0] == "^"
    i = a ? 1 : 0
    erbout << "{\n    int u;\n    const size_t n = packcr_get_char_as_utf32(ctx, &u);\n    if (n == 0) goto L#{format("%04d", onfail)};\n".freeze

    if charclass && !(a && n == 1) # not '.' or '[^]'
      u0 = 0
      r = false
      if a
        erbout << "    if (\n".freeze

      else
        erbout << "    if (!(\n".freeze
      end
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        u = charclass[i].ord
        i += 1
        if r
          # character range
          erbout << "        (u >= 0x#{format("%06x", u0)} && u <= 0x#{format("%06x", u)})".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position
          # single character
          erbout << "        u == 0x#{format("%06x", u)}".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] == "-"
          i += 1
          u0 = u
          r = true
        else
          raise "unexpected charclass #{charclass[i]}"
        end
      end
      if a
        erbout << "    ) goto L#{format("%04d", onfail)};\n".freeze

      else
        erbout << "    )) goto L#{format("%04d", onfail)};\n".freeze
      end
    end
    if gen.location
      erbout << "    packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
    end
    erbout << "    ctx->position_offset += n;\n}\n".freeze

    erbout
  when :rb
    erbout = +""
    a = charclass && charclass[0] == "^"
    i = a ? 1 : 0
    erbout << "if refill_buffer(1) < 1\n  throw(#{onfail})\nend\nu#{gen.level} = @buffer[@position_offset]\n".freeze

    if charclass && !(a && n == 1) # not '.' or '[^]'
      u0 = nil
      r = false
      if a
        erbout << "if (\n".freeze

      else
        erbout << "if (!(\n".freeze
      end
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        u = charclass[i]
        i += 1
        if r
          # character range
          erbout << "  (u#{gen.level} >= #{u0.dump} && u#{gen.level} <= #{u.dump})".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position
          # single character
          erbout << "  u#{gen.level} == #{u.dump}".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] == "-"
          i += 1
          u0 = u
          r = true
        else
          raise "unexpected charclass #{charclass[i]}"
        end
      end
      if a
        erbout << ")\n".freeze

      else
        erbout << "))\n".freeze
      end
      erbout << "  throw(#{onfail})\nend\n".freeze
    end
    if gen.location
      erbout << "@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
    end
    erbout << "@position_offset += 1\n".freeze

    erbout
  when :rs
    erbout = +""
    a = charclass && charclass[0] == "^"
    i = a ? 1 : 0
    any_code = !charclass || (a && n == 1)
    erbout << "let (#{any_code ? "_" : ""}u, n) = self.input.get_char_as_utf32();\nif n == 0 {\n    return throw(#{onfail});\n}\n".freeze

    unless any_code
      erbout << "if ".freeze
      if !a

        erbout << "!(".freeze
      end
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        u = charclass[i].ord
        i += 1
        if r
          # character range

          erbout << "(0x#{format("%06x", u0)}..=0x#{format("%06x", u)}).contains(&u)".freeze

          if i < n
            erbout << " || ".freeze
          end
          u0 = 0
          r = false
        elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position
          # single character

          erbout << "u == 0x#{format("%06x", u)}".freeze
          if i < n
            erbout << " || ".freeze
          end
          u0 = 0
          r = false
        elsif charclass[i] == "-"
          i += 1
          u0 = u
          r = true
        else
          raise "unexpected charclass #{charclass[i]}"
        end
      end
      if !a
        erbout << ") ".freeze
      end

      erbout << "{\n    return throw(#{onfail});\n}\n".freeze
    end
    erbout << "self.input.position_offset += n;\n".freeze

    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#get_utf8_reverse_code(gen, onsuccess, indent, unwrap, charclass, n) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/packcr/generated/node/charclass_node.rb', line 366

def get_utf8_reverse_code(gen, onsuccess, indent, unwrap, charclass, n)
  case gen.lang
  when :rb
    erbout = +""
    a = charclass && charclass[0] == "^"
    i = a ? 1 : 0
    erbout << "if refill_buffer(1) >= 1\n  u#{gen.level} = @buffer[@position_offset]\n".freeze

    if charclass && !(a && n == 1) # not '.' or '[^]'
      u0 = nil
      r = false
      if a
        erbout << "  unless (\n".freeze

      else
        erbout << "  if (\n".freeze
      end
      while i < n
        if charclass[i] == "\\" && i + 1 < n
          i += 1
        end
        u = charclass[i]
        i += 1
        if r
          # character range
          erbout << "    (u#{gen.level} >= #{u0.dump} && u#{gen.level} <= #{u.dump})".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position
          # single character
          erbout << "    u#{gen.level} == #{u.dump}".freeze
          if i < n
            erbout << " ||".freeze
          end
          erbout << "\n".freeze

          u0 = 0
          r = false
        elsif charclass[i] == "-"
          i += 1
          u0 = u
          r = true
        else
          raise "unexpected charclass #{charclass[i]}"
        end
      end
      erbout << "  )\n".freeze

      if gen.location
        erbout << "    @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
      end
      erbout << "    @position_offset += 1\n    throw(#{onsuccess})\n  end\n".freeze

    else # '.' or '[^]'
      if gen.location
        erbout << "  @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)\n".freeze
      end
      erbout << "  @position_offset += 1\n  throw(#{onsuccess})\n".freeze
    end
    erbout << "end\n".freeze

    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end

#reachabilityObject



32
33
34
35
36
37
38
# File 'lib/packcr/node/charclass_node.rb', line 32

def reachability
  charclass = value
  n = charclass&.length || 0
  return Packcr::CODE_REACH__BOTH if charclass.nil? || n > 0

  Packcr::CODE_REACH__ALWAYS_FAIL
end

#reversible?(gen) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/packcr/node/charclass_node.rb', line 16

def reversible?(gen)
  gen.lang == :rb && !gen.ascii
end