Sha256: cea50a89a8f12ebbf2184d273512db59c843683980c40c34f5db87ac5c70b291

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module Antelope
  module Generation
    class Constructor
      module First

        def initialize
          @firstifying = []
          super
        end

        def first(token)
          case token
          when Ace::Token::Nonterminal
            firstifying(token) do
              productions = parser.productions[token.name]
              productions.map { |prod|
                first(prod[:items]) }.inject(Set.new, :+)
            end
          when Array
            first_array(token)
          when Ace::Token::Epsilon
            Set.new
          when Ace::Token::Terminal
            Set.new([token])
          else
            incorrect_argument! token, Ace::Token, Array
          end
        end

        private

        def first_array(token)
          token.dup.delete_if { |tok| @firstifying.include?(tok) }.
          each_with_index.take_while do |tok, i|
            if i.zero?
              true
            else
              nullable?(token[i - 1])
            end
          end.map(&:first).map { |tok| first(tok) }.inject(Set.new, :+)
        end

        def firstifying(tok)
          @firstifying << tok
          out = yield
          @firstifying.delete tok
          out
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
antelope-0.0.1 lib/antelope/generation/constructor/first.rb