Sha256: 021856be56504a33312815b32529670cdad286b4b914b973a81edbecdd0a85ee

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

require "shuwar/tokenizer"

module Shuwar
  class Parser
    def initialize(tok)
      @enum = tok.enum_for :each_token
    end

    def get_object
      case @enum.peek
        when Tokenizer::OpenParen
          @enum.next # Eat OpenParen
          tmp = []
          until @enum.peek.is_a? Tokenizer::CloseParen
            tmp.push get_object
          end
          @enum.next # Eat CloseParen
          tmp
        when Tokenizer::Quote
          @enum.next # Eat Quote
          [:quote, get_object]
        when Tokenizer::CloseParen
          raise "Got some close paren here. Why?"
        else
          @enum.next
      end
    end

    def input_end?
      begin
        @enum.peek
        false
      rescue StopIteration
        true
      end
    end

    def each_object!
      until input_end?
        yield get_object
      end
    end

    def each_object(&block)
      if block
        each_object! &block
      else
        enum_for :each_object!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shuwar-1.3.0 lib/shuwar/parser.rb
shuwar-1.2.0 lib/shuwar/parser.rb