Class: Poetics::Parser

Inherits:
Object
  • Object
show all
Includes:
Syntax
Defined in:
lib/poetics/parser/parser.rb,
lib/poetics/parser.rb

Defined Under Namespace

Classes: LeftRecursive, MemoEntry, ParseError, RuleInfo

Constant Summary collapse

Rules =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Syntax

#false_value, #hexadecimal, #null_value, #number, #string_value, #true_value, #undefined_value

Constructor Details

#initialize(str, debug = false) ⇒ Parser

This is distinct from setup_parser so that a standalone parser can redefine #initialize and still have access to the proper parser setup code.



18
19
20
# File 'lib/poetics/parser/parser.rb', line 18

def initialize(str, debug=false)
  setup_parser(str, debug)
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



15
16
17
# File 'lib/poetics/parser.rb', line 15

def column
  @column
end

#failed_ruleObject (readonly)

Returns the value of attribute failed_rule.



144
145
146
# File 'lib/poetics/parser/parser.rb', line 144

def failed_rule
  @failed_rule
end

#failing_rule_offsetObject (readonly)

Returns the value of attribute failing_rule_offset.



23
24
25
# File 'lib/poetics/parser/parser.rb', line 23

def failing_rule_offset
  @failing_rule_offset
end

#lineObject (readonly)

Returns the value of attribute line.



15
16
17
# File 'lib/poetics/parser.rb', line 15

def line
  @line
end

#posObject

Returns the value of attribute pos.



24
25
26
# File 'lib/poetics/parser/parser.rb', line 24

def pos
  @pos
end

#resultObject (readonly)

Returns the value of attribute result.



23
24
25
# File 'lib/poetics/parser/parser.rb', line 23

def result
  @result
end

#stringObject (readonly)

Returns the value of attribute string.



22
23
24
# File 'lib/poetics/parser/parser.rb', line 22

def string
  @string
end

Class Method Details

.parse_to_sexp(string) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/poetics/parser.rb', line 6

def self.parse_to_sexp(string)
  parser = new string
  unless parser.parse
    parser.raise_error
  end

  parser.result.to_sexp
end

.rule_info(name, rendered) ⇒ Object



310
311
312
# File 'lib/poetics/parser/parser.rb', line 310

def self.rule_info(name, rendered)
  RuleInfo.new(name, rendered)
end

Instance Method Details

#__hyphen_Object

  • (“ ” | “t” | “n”)*



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/poetics/parser/parser.rb', line 364

def __hyphen_
  while true

    _save1 = self.pos
    while true # choice
      _tmp = match_string(" ")
      break if _tmp
      self.pos = _save1
      _tmp = match_string("\t")
      break if _tmp
      self.pos = _save1
      _tmp = match_string("\n")
      break if _tmp
      self.pos = _save1
      break
    end # end choice

    break unless _tmp
  end
  _tmp = true
  set_failed_rule :__hyphen_ unless _tmp
  return _tmp
end

#_booleanObject

boolean = position (true | false | null | undefined)



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
437
438
439
440
441
442
443
444
445
# File 'lib/poetics/parser/parser.rb', line 410

def _boolean

  _save = self.pos
  while true # sequence
    _tmp = apply(:_position)
    unless _tmp
      self.pos = _save
      break
    end

    _save1 = self.pos
    while true # choice
      _tmp = apply(:_true)
      break if _tmp
      self.pos = _save1
      _tmp = apply(:_false)
      break if _tmp
      self.pos = _save1
      _tmp = apply(:_null)
      break if _tmp
      self.pos = _save1
      _tmp = apply(:_undefined)
      break if _tmp
      self.pos = _save1
      break
    end # end choice

    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_boolean unless _tmp
  return _tmp
end

#_columnObject

column = { current_column }



819
820
821
822
823
824
# File 'lib/poetics/parser/parser.rb', line 819

def _column
  @result = begin;  current_column ; end
  _tmp = true
  set_failed_rule :_column unless _tmp
  return _tmp
end

#_digitsObject

digits = (“0” | /[1-9]/ /[0-9]/*)



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
# File 'lib/poetics/parser/parser.rb', line 619

def _digits

  _save = self.pos
  while true # choice
    _tmp = match_string("0")
    break if _tmp
    self.pos = _save

    _save1 = self.pos
    while true # sequence
      _tmp = scan(/\A(?-mix:[1-9])/)
      unless _tmp
        self.pos = _save1
        break
      end
      while true
        _tmp = scan(/\A(?-mix:[0-9])/)
        break unless _tmp
      end
      _tmp = true
      unless _tmp
        self.pos = _save1
      end
      break
    end # end sequence

    break if _tmp
    self.pos = _save
    break
  end # end choice

  set_failed_rule :_digits unless _tmp
  return _tmp
end

#_endObject

end = !.



354
355
356
357
358
359
360
361
# File 'lib/poetics/parser/parser.rb', line 354

def _end
  _save = self.pos
  _tmp = get_byte
  _tmp = _tmp ? nil : true
  self.pos = _save
  set_failed_rule :_end unless _tmp
  return _tmp
end

#_falseObject

false = “false” Syntax#false_value



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/poetics/parser/parser.rb', line 470

def _false

  _save = self.pos
  while true # sequence
    _tmp = match_string("false")
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; false_value; end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_false unless _tmp
  return _tmp
end

#_hexObject

hex = “0x” < hexdigits+ > hexadecimal(text)



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
# File 'lib/poetics/parser/parser.rb', line 578

def _hex

  _save = self.pos
  while true # sequence
    _tmp = match_string("0x")
    unless _tmp
      self.pos = _save
      break
    end
    _text_start = self.pos
    _save1 = self.pos
    _tmp = apply(:_hexdigits)
    if _tmp
      while true
        _tmp = apply(:_hexdigits)
        break unless _tmp
      end
      _tmp = true
    else
      self.pos = _save1
    end
    if _tmp
      text = get_text(_text_start)
    end
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; hexadecimal(text); end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_hex unless _tmp
  return _tmp
end

#_hexdigitsObject

hexdigits = /[0-9A-Fa-f]/



571
572
573
574
575
# File 'lib/poetics/parser/parser.rb', line 571

def _hexdigits
  _tmp = scan(/\A(?-mix:[0-9A-Fa-f])/)
  set_failed_rule :_hexdigits unless _tmp
  return _tmp
end

#_intObject

int = < digits > number(text)



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/poetics/parser/parser.rb', line 655

def _int

  _save = self.pos
  while true # sequence
    _text_start = self.pos
    _tmp = apply(:_digits)
    if _tmp
      text = get_text(_text_start)
    end
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; number(text); end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_int unless _tmp
  return _tmp
end

#_lineObject

line = { current_line }



811
812
813
814
815
816
# File 'lib/poetics/parser/parser.rb', line 811

def _line
  @result = begin;  current_line ; end
  _tmp = true
  set_failed_rule :_line unless _tmp
  return _tmp
end

#_nullObject

null = “null” Syntax#null_value



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/poetics/parser/parser.rb', line 492

def _null

  _save = self.pos
  while true # sequence
    _tmp = match_string("null")
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; null_value; end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_null unless _tmp
  return _tmp
end

#_numberObject

number = position (real | hex | int)



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
# File 'lib/poetics/parser/parser.rb', line 536

def _number

  _save = self.pos
  while true # sequence
    _tmp = apply(:_position)
    unless _tmp
      self.pos = _save
      break
    end

    _save1 = self.pos
    while true # choice
      _tmp = apply(:_real)
      break if _tmp
      self.pos = _save1
      _tmp = apply(:_hex)
      break if _tmp
      self.pos = _save1
      _tmp = apply(:_int)
      break if _tmp
      self.pos = _save1
      break
    end # end choice

    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_number unless _tmp
  return _tmp
end

#_positionObject

position = line:l column:c { position(l, c) }



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
# File 'lib/poetics/parser/parser.rb', line 827

def _position

  _save = self.pos
  while true # sequence
    _tmp = apply(:_line)
    l = @result
    unless _tmp
      self.pos = _save
      break
    end
    _tmp = apply(:_column)
    c = @result
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin;  position(l, c) ; end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_position unless _tmp
  return _tmp
end

#_realObject

real = < digits “.” digits (“e” /[-+]/? /[0-9]/+)? > number(text)



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
760
761
762
763
764
765
766
767
# File 'lib/poetics/parser/parser.rb', line 681

def _real

  _save = self.pos
  while true # sequence
    _text_start = self.pos

    _save1 = self.pos
    while true # sequence
      _tmp = apply(:_digits)
      unless _tmp
        self.pos = _save1
        break
      end
      _tmp = match_string(".")
      unless _tmp
        self.pos = _save1
        break
      end
      _tmp = apply(:_digits)
      unless _tmp
        self.pos = _save1
        break
      end
      _save2 = self.pos

      _save3 = self.pos
      while true # sequence
        _tmp = match_string("e")
        unless _tmp
          self.pos = _save3
          break
        end
        _save4 = self.pos
        _tmp = scan(/\A(?-mix:[-+])/)
        unless _tmp
          _tmp = true
          self.pos = _save4
        end
        unless _tmp
          self.pos = _save3
          break
        end
        _save5 = self.pos
        _tmp = scan(/\A(?-mix:[0-9])/)
        if _tmp
          while true
            _tmp = scan(/\A(?-mix:[0-9])/)
            break unless _tmp
          end
          _tmp = true
        else
          self.pos = _save5
        end
        unless _tmp
          self.pos = _save3
        end
        break
      end # end sequence

      unless _tmp
        _tmp = true
        self.pos = _save2
      end
      unless _tmp
        self.pos = _save1
      end
      break
    end # end sequence

    if _tmp
      text = get_text(_text_start)
    end
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; number(text); end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_real unless _tmp
  return _tmp
end

#_rootObject

root = - value? - end



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
# File 'lib/poetics/parser/parser.rb', line 318

def _root

  _save = self.pos
  while true # sequence
    _tmp = apply(:__hyphen_)
    unless _tmp
      self.pos = _save
      break
    end
    _save1 = self.pos
    _tmp = apply(:_value)
    unless _tmp
      _tmp = true
      self.pos = _save1
    end
    unless _tmp
      self.pos = _save
      break
    end
    _tmp = apply(:__hyphen_)
    unless _tmp
      self.pos = _save
      break
    end
    _tmp = apply(:_end)
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_root unless _tmp
  return _tmp
end

#_stringObject

string = position “"” < /[^\“]*/ > ”"“ string_value(text)



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# File 'lib/poetics/parser/parser.rb', line 770

def _string

  _save = self.pos
  while true # sequence
    _tmp = apply(:_position)
    unless _tmp
      self.pos = _save
      break
    end
    _tmp = match_string("\"")
    unless _tmp
      self.pos = _save
      break
    end
    _text_start = self.pos
    _tmp = scan(/\A(?-mix:[^\\"]*)/)
    if _tmp
      text = get_text(_text_start)
    end
    unless _tmp
      self.pos = _save
      break
    end
    _tmp = match_string("\"")
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; string_value(text); end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_string unless _tmp
  return _tmp
end

#_trueObject

true = “true” Syntax#true_value



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/poetics/parser/parser.rb', line 448

def _true

  _save = self.pos
  while true # sequence
    _tmp = match_string("true")
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; true_value; end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_true unless _tmp
  return _tmp
end

#_undefinedObject

undefined = “undefined” Syntax#undefined_value



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/poetics/parser/parser.rb', line 514

def _undefined

  _save = self.pos
  while true # sequence
    _tmp = match_string("undefined")
    unless _tmp
      self.pos = _save
      break
    end
    @result = begin; undefined_value; end
    _tmp = true
    unless _tmp
      self.pos = _save
    end
    break
  end # end sequence

  set_failed_rule :_undefined unless _tmp
  return _tmp
end

#_valueObject

value = (string | number | boolean)



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/poetics/parser/parser.rb', line 389

def _value

  _save = self.pos
  while true # choice
    _tmp = apply(:_string)
    break if _tmp
    self.pos = _save
    _tmp = apply(:_number)
    break if _tmp
    self.pos = _save
    _tmp = apply(:_boolean)
    break if _tmp
    self.pos = _save
    break
  end # end choice

  set_failed_rule :_value unless _tmp
  return _tmp
end

#apply(rule) ⇒ Object



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
# File 'lib/poetics/parser/parser.rb', line 247

def apply(rule)
  if m = @memoizations[rule][@pos]
    m.inc!

    prev = @pos
    @pos = m.pos
    if m.ans.kind_of? LeftRecursive
      m.ans.detected = true
      return nil
    end

    @result = m.result

    return m.ans
  else
    lr = LeftRecursive.new(false)
    m = MemoEntry.new(lr, @pos)
    @memoizations[rule][@pos] = m
    start_pos = @pos

    ans = __send__ rule

    m.move! ans, @pos, @result

    # Don't bother trying to grow the left recursion
    # if it's failing straight away (thus there is no seed)
    if ans and lr.detected
      return grow_lr(rule, start_pos, m)
    else
      return ans
    end

    return ans
  end
end

#current_column(target = pos) ⇒ Object

STANDALONE START



27
28
29
30
31
32
33
# File 'lib/poetics/parser/parser.rb', line 27

def current_column(target=pos)
  if c = string.rindex("\n", target-1)
    return target - c - 1
  end

  target + 1
end

#current_line(target = pos) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/poetics/parser/parser.rb', line 35

def current_line(target=pos)
  cur_offset = 0
  cur_line = 0

  string.each_line do |line|
    cur_line += 1
    cur_offset += line.size
    return cur_line if cur_offset >= target
  end

  -1
end

#external_invoke(other, rule, *args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/poetics/parser/parser.rb', line 227

def external_invoke(other, rule, *args)
  old_pos = @pos
  old_string = @string

  @pos = other.pos
  @string = other.string

  begin
    if val = __send__(rule, *args)
      other.pos = @pos
    else
      other.set_failed_rule "#{self.class}##{rule}"
    end
    val
  ensure
    @pos = old_pos
    @string = old_string
  end
end

#failure_caretObject



81
82
83
84
85
86
87
# File 'lib/poetics/parser/parser.rb', line 81

def failure_caret
  l = current_line @failing_rule_offset
  c = current_column @failing_rule_offset

  line = lines[l-1]
  "#{line}\n#{' ' * (c - 1)}^"
end

#failure_characterObject



89
90
91
92
93
# File 'lib/poetics/parser/parser.rb', line 89

def failure_character
  l = current_line @failing_rule_offset
  c = current_column @failing_rule_offset
  lines[l-1][c-1, 1]
end

#failure_infoObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/poetics/parser/parser.rb', line 69

def failure_info
  l = current_line @failing_rule_offset
  c = current_column @failing_rule_offset

  if @failed_rule.kind_of? Symbol
    info = self.class::Rules[@failed_rule]
    "line #{l}, column #{c}: failed rule '#{info.name}' = '#{info.rendered}'"
  else
    "line #{l}, column #{c}: failed rule '#{@failed_rule}'"
  end
end

#failure_onelineObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/poetics/parser/parser.rb', line 95

def failure_oneline
  l = current_line @failing_rule_offset
  c = current_column @failing_rule_offset

  char = lines[l-1][c-1, 1]

  if @failed_rule.kind_of? Symbol
    info = self.class::Rules[@failed_rule]
    "@#{l}:#{c} failed rule '#{info.name}', got '#{char}'"
  else
    "@#{l}:#{c} failed rule '#{@failed_rule}', got '#{char}'"
  end
end

#get_byteObject



167
168
169
170
171
172
173
174
175
# File 'lib/poetics/parser/parser.rb', line 167

def get_byte
  if @pos >= @string.size
    return nil
  end

  s = @string.getbyte @pos
  @pos += 1
  s
end

#get_text(start) ⇒ Object



56
57
58
# File 'lib/poetics/parser/parser.rb', line 56

def get_text(start)
  @string[start..@pos-1]
end

#grow_lr(rule, start_pos, m) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/poetics/parser/parser.rb', line 283

def grow_lr(rule, start_pos, m)
  while true
    @pos = start_pos
    @result = m.result

    ans = __send__ rule
    return nil unless ans

    break if @pos <= m.pos

    m.move! ans, @pos, @result
  end

  @result = m.result
  @pos = m.pos
  return m.ans
end

#linesObject



48
49
50
51
52
# File 'lib/poetics/parser/parser.rb', line 48

def lines
  lines = []
  string.each_line { |l| lines << l }
  lines
end

#match_string(str) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/poetics/parser/parser.rb', line 146

def match_string(str)
  len = str.size
  if @string[pos,len] == str
    @pos += len
    return str
  end

  return nil
end

#parse(rule = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/poetics/parser/parser.rb', line 188

def parse(rule=nil)
  if !rule
    _root ? true : false
  else
    # This is not shared with code_generator.rb so this can be standalone
    method = rule.gsub("-","_hyphen_")
    __send__("_#{method}") ? true : false
  end
end

#position(line, column) ⇒ Object



17
18
19
20
# File 'lib/poetics/parser.rb', line 17

def position(line, column)
  @line = line
  @column = column
end

#raise_errorObject

Raises:



112
113
114
# File 'lib/poetics/parser/parser.rb', line 112

def raise_error
  raise ParseError, failure_oneline
end

#scan(reg) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/poetics/parser/parser.rb', line 156

def scan(reg)
  if m = reg.match(@string[@pos..-1])
    width = m.end(0)
    @pos += width
    return true
  end

  return nil
end

#set_failed_rule(name) ⇒ Object



137
138
139
140
141
142
# File 'lib/poetics/parser/parser.rb', line 137

def set_failed_rule(name)
  if @pos > @failing_rule_offset
    @failed_rule = name
    @failing_rule_offset = @pos
  end
end

#setup_foreign_grammarObject



315
# File 'lib/poetics/parser/parser.rb', line 315

def setup_foreign_grammar; end

#setup_parser(str, debug = false) ⇒ Object

STANDALONE START



3
4
5
6
7
8
9
10
11
12
# File 'lib/poetics/parser/parser.rb', line 3

def setup_parser(str, debug=false)
  @string = str
  @pos = 0
  @memoizations = Hash.new { |h,k| h[k] = {} }
  @result = nil
  @failed_rule = nil
  @failing_rule_offset = -1

  setup_foreign_grammar
end

#show_error(io = STDOUT) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/poetics/parser/parser.rb', line 116

def show_error(io=STDOUT)
  error_pos = @failing_rule_offset
  line_no = current_line(error_pos)
  col_no = current_column(error_pos)

  io.puts "On line #{line_no}, column #{col_no}:"

  if @failed_rule.kind_of? Symbol
    info = self.class::Rules[@failed_rule]
    io.puts "Failed to match '#{info.rendered}' (rule '#{info.name}')"
  else
    io.puts "Failed to match rule '#{@failed_rule}'"
  end

  io.puts "Got: #{string[error_pos,1].inspect}"
  line = lines[line_no-1]
  io.puts "=> #{line}"
  io.print(" " * (col_no + 3))
  io.puts "^"
end

#show_posObject



60
61
62
63
64
65
66
67
# File 'lib/poetics/parser/parser.rb', line 60

def show_pos
  width = 10
  if @pos < width
    "#{@pos} (\"#{@string[0,@pos]}\" @ \"#{@string[@pos,width]}\")"
  else
    "#{@pos} (\"... #{@string[@pos - width, width]}\" @ \"#{@string[@pos,width]}\")"
  end
end