Sha256: c3c5d945a5a15f0f5411acf5a871adc6340ffa907c077b18cd0d22f844fdbdb3

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

class Language
  class Parser
    attr_accessor :input, :buffer, :output, :root, :cursor

    def initialize(root:, input:, cursor: 0, buffer: "", output: Output.new)
      @root = root
      @input = input
      @cursor = cursor
      @buffer = buffer
      @output = output
    end

    def parse(check_end_of_input: true)
      @root.parse(self)

      unless @cursor == @input.size || !check_end_of_input
        raise NotEndOfInput, self
      end

      @output.present? ? @output : Output.new(@buffer.empty? ? nil : @buffer)
    end

    def consume(n)
      raise EndOfInput, self unless @cursor + n <= @input.size

      @buffer += @input[@cursor, n]
      @cursor += n
    end

    def aka(name)
      if @buffer.empty?
        @output = Output.new({ name => @output })
      else
        @output[name] = Output.new(@buffer)
        @buffer = ""
      end
    end

    def next?(string)
      @input[@cursor...(@cursor + string.size)] == string
    end

    def buffer?
      @buffer != ""
    end

    def output?
      @output.present?
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
language-ruby-1.1.2 lib/language/parser.rb
language-ruby-1.0.1 lib/language/parser.rb
language-ruby-1.0.0 lib/language/parser.rb
language-ruby-0.9.3 lib/language/parser.rb
language-ruby-0.9.2 lib/language/parser.rb
language-ruby-0.9.0 lib/language/parser.rb
language-ruby-0.8.4 lib/language/parser.rb
language-ruby-0.8.3 lib/language/parser.rb
language-ruby-0.8.2 lib/language/parser.rb
language-ruby-0.8.1 lib/language/parser.rb
language-ruby-0.8.0 lib/language/parser.rb