Class: Packcr::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/packcr/context.rb

Defined Under Namespace

Classes: StopParsing

Instance Method Summary collapse

Constructor Details

#initialize(path, lines: false, debug: false, ascii: false, lang: nil) ⇒ Context

Returns a new instance of Context.



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
# File 'lib/packcr/context.rb', line 6

def initialize(path, lines: false, debug: false, ascii: false, lang: nil)
  if !path
    raise ArgumentError, "bad path: #{path}";
  end

  @iname = path
  @ifile = File.open(path, "rb")
  dirname = File.dirname(path)
  basename = File.basename(path, ".*")
  if !lang
    lang = File.extname(basename)[1..-1]&.to_sym
    if lang
      basename = File.basename(basename, ".*")
    else
      lang = :c
    end
  end
  if dirname == "."
    path = basename
  else
    path = File.join(dirname, basename)
  end

  @lang = lang.to_sym
  case @lang
  when :c
    @sname = path + ".c"
    @hname = path + ".h"
    @hid = File.basename(@hname).upcase.gsub(/[^A-Z0-9]/, "_")
  when :rb
    @sname = path + ".rb"
    @hname = nil
  else
    raise "unexpected lang: #{@lang}"
  end

  @lines = !!lines
  @debug = !!debug
  @ascii = !!ascii

  @errnum = 0
  @linenum = 0
  @charnum = 0
  @linepos = 0
  @bufpos = 0
  @bufcur = 0

  @esource = []
  @eheader = []
  @source = []
  @header = []
  @lheader = []
  @location = []
  @rules = []
  @rulehash = {}
  @buffer = Packcr::Buffer.new

  if block_given?
    yield(self)
  end
end

Instance Method Details

#auxil_defObject



93
94
95
96
# File 'lib/packcr/context.rb', line 93

def auxil_def
  type = auxil_type
  "#{type}#{type =~ /\*$/ ? "" : " "}"
end

#auxil_typeObject



81
82
83
# File 'lib/packcr/context.rb', line 81

def auxil_type
  @auxil_type || "void *"
end

#class_nameObject



89
90
91
# File 'lib/packcr/context.rb', line 89

def class_name
  prefix.gsub(/(?:_|^)([a-z])/) { $1.upcase }
end

#column_numberObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/packcr/context.rb', line 131

def column_number
  unless @bufpos + @bufcur >= @linepos
    raise "invalid position: expect #{@bufpos + @bufcur} >= #{@linepos}"
  end
  offset = @linepos > @bufpos ? @linepos - @bufpos : 0
  if @ascii
    @charnum + @bufcur - offset
  else
    @charnum + @buffer.count_characters(offset, @bufcur)
  end
end

#commit_bufferObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/packcr/context.rb', line 159

def commit_buffer
  if @buffer.len < @bufcur
    raise "unexpected buffer state: length(#{@buffer.len}), current(#{@bufcur})"
  end
  if @linepos < @bufpos + @bufcur
    count = @ascii ? @bufcur : @buffer.count_characters(0, @bufcur)
    @charnum += count
  end
  @buffer.add_pos(@bufcur)
  @bufpos = @bufpos + @bufcur
  @bufcur = 0
end

#dump_optionsObject



405
406
407
408
409
410
411
# File 'lib/packcr/context.rb', line 405

def dump_options
  $stdout.print "    value_type: '\#{value_type}'\n    auxil_type: '\#{auxil_type}'\n    prefix: '\#{prefix}'\n  EOS\nend\n"

#eof?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/packcr/context.rb', line 103

def eof?
  refill_buffer(1) < 1
end

#eol?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/packcr/context.rb', line 107

def eol?
  return false if eof?

  case @buffer[@bufcur]
  when 0xa
    @bufcur += 1
    @linenum += 1
    @charnum = 0
    @linepos = @bufpos + @bufcur
    true
  when 0xd
    @bufcur += 1
    if !eof? && @buffer[@bufcur] == 0xd
      @bufcur += 1
    end
    @linenum += 1
    @charnum = 0
    @linepos = @bufpos + @bufcur
    true
  else
    false
  end
end

#error(line, col, message) ⇒ Object



72
73
74
75
# File 'lib/packcr/context.rb', line 72

def error(line, col, message)
  warn "#{@iname}:#{line}:#{col}: #{message}"
  @errnum += 1
end

#generateObject



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'lib/packcr/context.rb', line 946

def generate
  if @hname
    File.open(@hname, "wt") do |hio|
      hstream = ::Packcr::Stream.new(hio, @hname, @lines ? 0 : nil)

      hstream.write Packcr.template("context/header.#{@lang}.erb", binding), rewrite_line_directive: true
    end
  end

  File.open(@sname, "wt") do |sio|
    sstream = ::Packcr::Stream.new(sio, @sname, @lines ? 0 : nil)

    sstream.write Packcr.template("context/source.#{@lang}.erb", binding), rewrite_line_directive: true

    eol?
    if !eof?
      sstream.write("\n")
    end
    commit_buffer
    if @lines && !eof?
      sstream.write_line_directive(@iname, @linenum)
    end
    while refill_buffer > 0
      write_buffer(sstream)
      commit_buffer
    end
  end

  if !@errnum.zero?
    File.unlink(@hname) if @name
    File.unlink(@sname)
    return false
  end
  true
end

#inspectObject



68
69
70
# File 'lib/packcr/context.rb', line 68

def inspect
  "#<#{self.class}:0x%016x>" % object_id
end

#make_rulehashObject



143
144
145
146
147
# File 'lib/packcr/context.rb', line 143

def make_rulehash
  @rules.each do |rule|
    @rulehash[rule.name] = rule
  end
end

#match_blankObject



227
228
229
# File 'lib/packcr/context.rb', line 227

def match_blank
  match_character_set(" \t\v\f")
end

#match_character(ch) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/packcr/context.rb', line 182

def match_character(ch)
  if refill_buffer(1) >= 1
    if @buffer[@bufcur].ord == ch.ord
      @bufcur += 1
      return true
    end
  end
  false
end

#match_character_anyObject



231
232
233
234
235
236
237
# File 'lib/packcr/context.rb', line 231

def match_character_any
  if refill_buffer(1) >= 1
    @bufcur += 1
    return true
  end
  false
end

#match_character_classObject



334
335
336
# File 'lib/packcr/context.rb', line 334

def match_character_class
  match_quotation_("[", "]", "character class")
end

#match_character_range(min, max) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/packcr/context.rb', line 192

def match_character_range(min, max)
  if refill_buffer(1) >= 1
    c = @buffer[@bufcur].ord
    if (min..max) === c
      @bufcur += 1
      return true
    end
  end
  false
end

#match_character_set(chars) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/packcr/context.rb', line 203

def match_character_set(chars)
  if refill_buffer(1) >= 1
    c = @buffer[@bufcur].ord
    chars.each_byte do |ch|
      if c == ch
        @bufcur += 1
        return true
      end
    end
  end
  false
end

#match_code_blockObject



362
363
364
365
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
# File 'lib/packcr/context.rb', line 362

def match_code_block
  l = @linenum
  m = column_number
  if match_character("{".ord)
    d = 1
    while true
      if eof?
        error l + 1, m + 1, "Premature EOF in code block"
        break
      end
      if match_directive_c || match_comment_c || match_comment_cxx || match_quotation_single || match_quotation_double
        next
      end
      if match_character("{".ord)
        d += 1
      elsif match_character("}".ord)
        d -= 1
        if d == 0
          break
        end
      else
        if !eol?
          if match_character("$".ord)
            if @lang == :rb
              @buffer[@bufcur - 1] = "__"
            else
              @buffer[@bufcur - 1] = "_"
            end
          else
            match_character_any
          end
        end
      end
    end
    return true
  end
  return false
end

#match_commentObject



314
315
316
# File 'lib/packcr/context.rb', line 314

def match_comment
  match_section_line_("#")
end

#match_comment_cObject



318
319
320
# File 'lib/packcr/context.rb', line 318

def match_comment_c
  match_section_block_("/*", "*/", "C comment")
end

#match_comment_cxxObject



322
323
324
# File 'lib/packcr/context.rb', line 322

def match_comment_cxx
  match_section_line_("//")
end

#match_directive_cObject



310
311
312
# File 'lib/packcr/context.rb', line 310

def match_directive_c
  match_section_line_continuable_("#")
end


401
402
403
# File 'lib/packcr/context.rb', line 401

def match_footer_start
  match_string("%%")
end

#match_identifierObject



354
355
356
357
358
359
360
# File 'lib/packcr/context.rb', line 354

def match_identifier
  if match_character_range("a".ord, "z".ord) || match_character_range("A".ord, "Z".ord) || match_character("_".ord)
    nil while match_character_range("a".ord, "z".ord) || match_character_range("A".ord, "Z".ord) || match_character_range("0".ord, "9".ord) || match_character("_".ord)
    return true
  end
  false
end

#match_numberObject



346
347
348
349
350
351
352
# File 'lib/packcr/context.rb', line 346

def match_number
  if match_character_range("0".ord, "9".ord)
    nil while match_character_range("0".ord, "9".ord)
    return true
  end
  return false
end

#match_quotation_(left, right, name) ⇒ Object



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
# File 'lib/packcr/context.rb', line 284

def match_quotation_(left, right, name)
  l = @linenum
  m = column_number
  if match_string(left)
    while !match_string(right)
      if eof?
        error l + 1, m + 1, "Premature EOF in #{name}"
        break
      end
      if match_character("\\".ord)
        if !eol?
          match_character_any
        end
      else
        if eol?
          error l + 1, m + 1, "Premature EOF in #{name}"
          break
        end
        match_character_any
      end
    end
    return true
  end
  false
end

#match_quotation_doubleObject



330
331
332
# File 'lib/packcr/context.rb', line 330

def match_quotation_double
  match_quotation_("\"", "\"", "double quotation")
end

#match_quotation_singleObject



326
327
328
# File 'lib/packcr/context.rb', line 326

def match_quotation_single
  match_quotation_("\'", "\'", "single quotation")
end

#match_section_block_(left, right, name) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/packcr/context.rb', line 266

def match_section_block_(left, right, name)
  l = @linenum
  m = column_number
  if match_string(left)
    while !match_string(right)
      if eof?
        error l + 1, m + 1, "Premature EOF in #{name}"
        break
      end
      if !eol?
        match_character_any
      end
    end
    return true
  end
  false
end

#match_section_line_(head) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/packcr/context.rb', line 239

def match_section_line_(head)
  if match_string(head)
    while !eol? && !eof?
      match_character_any
    end
    return true
  end
  false
end

#match_section_line_continuable_(head) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/packcr/context.rb', line 249

def match_section_line_continuable_(head)
  if match_string(head)
    while !eof?
      pos = @bufcur
      if eol?
        if @buffer[pos - 1] != "\\".ord
          break
        end
      else
        match_character_any
      end
    end
    return true
  end
  false
end

#match_spacesObject



338
339
340
341
342
343
344
# File 'lib/packcr/context.rb', line 338

def match_spaces
  n = 0
  while match_blank || eol? || match_comment
    n += 1
  end
  n > 0
end

#match_string(str) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/packcr/context.rb', line 216

def match_string(str)
  n = str.length
  if refill_buffer(n) >= n
    if @buffer.to_s[@bufcur, n] == str
      @bufcur += n
      return true
    end
  end
  false
end

#parseObject



864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# File 'lib/packcr/context.rb', line 864

def parse
  match_spaces

  b = true
  while true
    if eof? || match_footer_start
      break
    end
    if (
        parse_directive_include("%earlysource", @esource) ||
        parse_directive_include("%earlycommon", @esource, @eheader) ||
        parse_directive_include("%source", @source) ||
        parse_directive_include("%lateheader", @lheader) ||
        parse_directive_include("%header", @header) ||
        parse_directive_include("%common", @source, @header) ||
        parse_directive_include("%location", @location) ||
        parse_directive_string("%value", "@value_type", must_not_be_empty: true, must_not_be_void: true) ||
        parse_directive_string("%auxil", "@auxil_type", must_not_be_empty: true, must_not_be_void: true) ||
        parse_directive_string("%prefix", "@prefix", must_not_be_empty: true, must_be_identifier: true)
      )
      b = true
    elsif match_character("%")
      l = @linenum
      m = column_number
      error l + 1, m + 1, "Invalid directive"
      match_identifier
      match_spaces
      b = true
    else
      l = @linenum
      m = column_number
      n = @charnum
      o = @linepos
      node = parse_rule
      if node == nil
        if b
          error l + 1, m + 1, "Illegal rule syntax"
          b = false
        end
        @linenum = l
        @charnum = n
        @linepos = o
        if !match_identifier && !match_spaces
          match_character_any
        end
      else
        @rules.push(node)
        b = true
      end
    end
    commit_buffer
  end

  if @location.empty?
    @location = nil
  end
  commit_buffer

  make_rulehash
  @rules.each do |rule|
    rule.expr.link_references(self)
  end
  @rules[1..-1]&.each do |rule|
    if rule.ref == 0
      error rule.line + 1, rule.col + 1, "Never used rule '#{rule.name}'"
    elsif rule.ref < 0 # impossible?
      error rule.line + 1, rule.col + 1, "Multiple definition of rule '#{rule.name}'"
    end
  end

  @rules.each do |rule|
    rule.verify(self)
  end

  if @debug
    @rules.each(&:debug_dump)
    dump_options
  end

  @errnum.zero?
end

#parse_directive_include(name, *outputs) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/packcr/context.rb', line 417

def parse_directive_include(name, *outputs)
  if !match_string(name)
    return false
  end

  match_spaces

  pos = @bufcur
  l = @linenum
  m = column_number
  if match_code_block
    q = @bufcur
    match_spaces
    outputs.each do |output|
      code = Packcr::CodeBlock.new(@buffer.to_s[pos + 1, q - pos - 2], q - pos - 2, l, m)
      output.push(code)
    end
  else
    error l + 1, m + 1, "Illegal #{name} syntax"
  end
  true
end

#parse_directive_string(name, varname, must_not_be_empty: false, must_not_be_void: false, must_be_identifier: false) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/packcr/context.rb', line 440

def parse_directive_string(name, varname, must_not_be_empty: false, must_not_be_void: false, must_be_identifier: false)
  l = @linenum
  m = column_number
  if !match_string(name)
    return false
  end

  match_spaces
  pos = @bufcur
  lv = @linenum
  mv = column_number
  s = nil
  if match_quotation_single || match_quotation_double
    q = @bufcur
    match_spaces
    s = @buffer.to_s[pos + 1, q - pos - 2]
    if !Packcr.unescape_string(s, false)
      error lv + 1, mv + 1, "Illegal escape sequence"
    end
  else
    error l + 1, m + 1, "Illegal #{name} syntax"
  end

  if s
    valid = true
    s.sub!(/\A\s+/, "")
    s.sub!(/\s+\z/, "")
    is_empty = must_not_be_empty && s !~ /[^\s]/
    if is_empty
      error lv + 1, mv + 1, "Empty string"
      vaild = false
    end
    if must_not_be_void && s == "void"
      error lv + 1, mv + 1, "'void' not allowed"
      vaild = false
    end
    if !is_empty && must_be_identifier && !Packcr.is_identifier_string(s)
      error lv + 1, mv + 1, "Invalid identifier"
      valid = false
    end
    if instance_variable_get(varname) != nil
      error l + 1, m + 1, "Multiple #{name} definition"
      valid
    end
    if valid
      instance_variable_set(varname, s)
    end
  end
  return true
end

#parse_expression(rule) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/packcr/context.rb', line 790

def parse_expression(rule)
  pos = @bufcur
  l = @linenum
  n = @charnum
  o = @linepos
  n_s = parse_sequence(rule)
  if !n_s
    raise StopParsing
  end
  q = @bufcur
  if (match_character("/".ord))
    @bufcur = q
    n_e = Packcr::Node::AlternateNode.new
    n_e.nodes << n_s
    while match_character("/".ord)
      match_spaces
      n_s = parse_sequence(rule)
      if !n_s
        raise StopParsing
      end
      n_e.nodes << n_s
    end
  else
    n_e = n_s
  end
  return n_e
rescue StopParsing
  @bufcur = pos
  @linenum = l
  @charnum = n
  @linepos = o
  return nil
end

#parse_primary(rule) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/packcr/context.rb', line 494

def parse_primary(rule)
  pos = @bufcur
  l = @linenum
  m = column_number
  n = @charnum
  o = @linepos
  if match_identifier
    q = @bufcur
    r = s = nil
    match_spaces
    if match_character(":".ord)
      match_spaces
      r = @bufcur
      if !match_identifier
        raise StopParsing
      end
      s = @bufcur
      match_spaces
    end
    if match_string("<-")
      raise StopParsing
    end

    n_p = Packcr::Node::ReferenceNode.new
    if r == nil
      name = @buffer.to_s
      name = name[pos, q - pos]
      unless q >= pos
        raise "Internal error"
      end
      n_p.var = nil
      n_p.index = nil
      n_p.name = name
    else
      var = @buffer.to_s
      var = var[pos, q - pos]
      unless s != nil # s should have a valid value when r has a valid value
        raise "Internal error"
      end
      unless q >= pos
        raise "Internal error"
      end

      n_p.var = var
      if var.ord == "_".ord
        error l + 1, m + 1, "Leading underscore in variable name '#{var}'"
      end

      i = rule.vars.index do |ref|
        unless ref.is_a?(Packcr::Node::ReferenceNode)
          raise "Unexpected node type: #{ref.class}"
        end
        var == ref.var
      end
      if !i
        i = rule.vars.length
        rule.vars << n_p
      end
      n_p.index = i
      unless s >= r
        raise "Internal error"
      end

      name = @buffer.to_s
      name = name[r, s - r]
      n_p.name = name
    end
    n_p.line = l
    n_p.col = m
  elsif match_character("(")
    match_spaces
    n_p = parse_expression(rule)
    if !n_p
      raise StopParsing
    end
    if !match_character(")")
      raise StopParsing
    end
    match_spaces
  elsif match_character("<")
    capts = rule.capts
    match_spaces
    n_p = Packcr::Node::CaptureNode.new
    n_p.index = capts.length
    rule.capts << n_p
    expr = parse_expression(rule)
    n_p.expr = expr
    if !expr || !match_character(">")
      rule.capts = rule.capts[0, n_p.index]
      raise StopParsing
    end
    match_spaces
  elsif match_character("$")
    match_spaces
    pos2 = @bufcur
    if match_number
      q = @bufcur
      s = @buffer.to_s
      s = s[pos2, q - pos2]
      match_spaces
      n_p = Packcr::Node::ExpandNode.new
      unless q >= pos2
        raise StopParsing
      end
      index = s.to_i
      n_p.index = index
      if index == nil
        error l + 1, m + 1, "Invalid unsigned number '#{s}'"
      elsif index == 0
        error l + 1, m + 1, "0 not allowed"
      elsif s.ord == "0".ord
        error l + 1, m + 1, "0-prefixed number not allowed"
        n_p.index = 0
      end
      if index > 0 && index != nil
        n_p.index = index - 1
        n_p.line = l
        n_p.col = m
      end
    else
      raise StopParsing
    end
  elsif match_character(".")
    match_spaces
    n_p = Packcr::Node::CharclassNode.new
    n_p.value = nil
    if !@ascii
      @utf8 = true
    end
  elsif match_character_class
    q = @bufcur
    charclass = @buffer.to_s
    charclass = charclass[pos + 1, q - pos - 2]
    match_spaces
    n_p = Packcr::Node::CharclassNode.new
    Packcr.unescape_string(charclass, true)
    if !@ascii
      charclass.force_encoding(Encoding::UTF_8)
    end
    if !@ascii && !charclass.valid_encoding?
      error l + 1, m + 1, "Invalid UTF-8 string"
    end
    if !@ascii && !charclass.empty?
      @utf8 = true
    end
    n_p.value = charclass
  elsif match_quotation_single || match_quotation_double
    q = @bufcur
    string = @buffer.to_s
    string = string[pos + 1, q - pos - 2]
    match_spaces
    n_p = ::Packcr::Node::StringNode.new
    Packcr.unescape_string(string, true)
    if !@ascii
      string.force_encoding(Encoding::UTF_8)
    end
    if !@ascii && !string.valid_encoding?
      error l + 1, m + 1, "Invalid UTF-8 string"
    end
    n_p.value = string
  elsif match_code_block
    q = @bufcur
    text = @buffer.to_s
    text = text[pos + 1, q - pos - 2]
    codes = rule.codes
    match_spaces
    n_p = Packcr::Node::ActionNode.new
    n_p.code = Packcr::CodeBlock.new(text, Packcr.find_trailing_blanks(text), l, m)
    n_p.index = codes.length
    codes.push(n_p)
  else
    raise StopParsing
  end
  n_p
rescue StopParsing
  @bufcur = pos
  @linenum = l
  @charnum = n
  @linepos = o
  return nil
end

#parse_ruleObject



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/packcr/context.rb', line 824

def parse_rule
  pos = @bufcur
  l = @linenum
  m = column_number
  n = @charnum
  o = @linepos
  if !match_identifier
    raise StopParsing
  end

  q = @bufcur
  match_spaces
  if !match_string("<-")
    raise StopParsing
  end
  match_spaces

  n_r = Packcr::Node::RuleNode.new
  expr = parse_expression(n_r)
  n_r.expr = expr
  if !expr
    raise StopParsing
  end
  unless q >= pos
    raise "Internal error"
  end
  name = @buffer.to_s
  name = name[pos, q - pos]
  n_r.name = name
  n_r.line = l
  n_r.col = m
  n_r
rescue StopParsing
  @bufcur = pos
  @linenum = l
  @charnum = n
  @linepos = o
  return nil
end

#parse_sequence(rule) ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'lib/packcr/context.rb', line 761

def parse_sequence(rule)
  pos = @bufcur
  l = @linenum
  n = @charnum
  o = @linepos
  n_t = parse_term(rule)
  if !n_t
    raise StopParsing
  end
  n_u = parse_term(rule);
  if n_u
    n_s = Packcr::Node::SequenceNode.new
    n_s.nodes << n_t
    n_s.nodes << n_u
    while (n_t = parse_term(rule))
      n_s.nodes << n_t
    end
  else
    n_s = n_t
  end
  n_s
rescue StopParsing
  @bufcur = pos
  @linenum = l
  @charnum = n
  @linepos = o
  return nil
end

#parse_term(rule) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'lib/packcr/context.rb', line 676

def parse_term(rule)
  pos = @bufcur
  l = @linenum
  n = @charnum
  o = @linepos
  if match_character("&")
    t = "&".ord
  elsif match_character("!")
    t = "!".ord
  else
    t = 0
  end
  if t
    match_spaces
  end

  n_p = parse_primary(rule)
  if !n_p
    raise StopParsing
  end
  if match_character("*")
    match_spaces
    n_q = Packcr::Node::QuantityNode.new
    n_q.min = 0
    n_q.max = -1
    n_q.expr = n_p
  elsif match_character("+")
    match_spaces
    n_q = Packcr::Node::QuantityNode.new
    n_q.min = 1
    n_q.max = -1
    n_q.expr = n_p
  elsif match_character("?")
    match_spaces
    n_q = Packcr::Node::QuantityNode.new
    n_q.min = 0
    n_q.max = 1
    n_q.expr = n_p
  else
    n_q = n_p
  end

  case t
  when "&".ord
    n_r = Packcr::Node::PredicateNode.new
    n_r.neg = false
    n_r.expr = n_q
  when "!".ord
    n_r = Packcr::Node::PredicateNode.new
    n_r.neg = true
    n_r.expr = n_q
  else
    n_r = n_q
  end

  if match_character("~")
    match_spaces
    pos2 = @bufcur
    l2 = @linenum
    m = column_number
    if match_code_block
      q = @bufcur
      text = @buffer.to_s
      text = text[pos2 + 1, q - pos2 - 2]
      match_spaces
      n_t = Packcr::Node::ErrorNode.new
      n_t.expr = n_r
      n_t.code = Packcr::CodeBlock.new(text, Packcr.find_trailing_blanks(text), l2, m);
      n_t.index = rule.codes.length
      rule.codes.push(n_t)
    else
      raise StopParsing
    end
  else
    n_t = n_r
  end
  n_t
rescue StopParsing
  @bufcur = pos
  @linenum = l
  @charnum = n
  @linepos = o
  return nil
end

#prefixObject



85
86
87
# File 'lib/packcr/context.rb', line 85

def prefix
  @prefix || "pcc"
end

#refill_buffer(num = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/packcr/context.rb', line 149

def refill_buffer(num = nil)
  while !num || @buffer.len - @bufcur < num
    c = @ifile.getc
    break if c.nil?
    @buffer.add(c.ord)
  end

  return @buffer.len - @bufcur
end

#rule(name) ⇒ Object



413
414
415
# File 'lib/packcr/context.rb', line 413

def rule(name)
  @rulehash[name]
end

#value_defObject



98
99
100
101
# File 'lib/packcr/context.rb', line 98

def value_def
  type = value_type
  "#{type}#{type =~ /\*$/ ? "" : " "}"
end

#value_typeObject



77
78
79
# File 'lib/packcr/context.rb', line 77

def value_type
  @value_type || "int"
end

#write_buffer(stream) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/packcr/context.rb', line 172

def write_buffer(stream)
  n = @buffer.len
  text = @buffer.to_s
  if n > 0 && text[-1] == "\r"
    text = text[0..-2]
  end
  stream.write_text(text)
  @bufcur = n
end