Method: CDDL::Parser#initialize

Defined in:
lib/cddl.rb

#initialize(source_text) ⇒ Parser

Returns a new instance of Parser.



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
# File 'lib/cddl.rb', line 46

def initialize(source_text)
  @abnf = Peggy::ABNF.new
  _cresult = @abnf.compile! ABNF_SPEC, ignore: :s
  presult = @abnf.parse? :cddl, (source_text + PRELUDE)
  expected_length = source_text.length + PRELUDE.length
  if expected_length != presult
    upto = @abnf.parse_results.keys.max
    puts "UPTO: #{upto}" if $advanced
    pp @abnf.parse_results[upto] if $advanced
    pp @abnf.parse_results[presult] if $advanced
    puts "SO FAR: #{presult}"  if $advanced
    puts @abnf.ast? if $advanced
    presult ||= 0
    part1 = source_text[[presult - 100, 0].max...presult]
    part3 = source_text[upto...[upto + 100, source_text.length].min]
    if upto - presult < 100
      part2 = source_text[presult...upto]
    else
      part2 = source_text[presult, 50] + "......." + (source_text[upto-50, 50] || "")
    end
    warn "*** Look for syntax problems around the #{
           "%%%".colorize(background: :light_yellow)} markers:\n#{
           part1}#{"%%%".colorize(color: :green, background: :light_yellow)}#{
           part2}#{"%%%".colorize(color: :red, background: :light_yellow)}#{
           part3}"
    raise ParseError, "*** Parse error at #{presult} upto #{upto} of #{
                      source_text.length} (#{expected_length})."
  end
  puts @abnf.ast? if $debug_ast
  @ast = @abnf.ast?
  # our little argument stack for rule processing
  @insides = []
  # collect error information
  @last_message = ""
end