Sha256: dcbe51fa65655af0644a2450b221e81a84637dfa26539f0702fda09284cddb50

Contents?: true

Size: 1.32 KB

Versions: 14

Compression:

Stored size: 1.32 KB

Contents

module Take
  class Unit
    module PredictMatch

      # Makes a prediction based on a set of actions.  If the peek token
      # doesn't have an action, it will try the special action `:_`, and
      # then give up.  If it finds a corresponding action either time, it
      # will run the action.
      #
      # @param actions [Hash<Symbol => Symbol>]
      # @return [Object] the result of the compiliation.
      def predict(*params, actions)

        action = actions.fetch(peek) do
          actions.fetch(:_)
        end

        if action.respond_to?(:call)
          action.call(*params)
        else
          send(:"compile_#{action}", *params)
        end
      end

      # Matches the next token of the input.  If it is not of the
      # expected type, it raises a SyntaxError.  Otherwise, it returns
      # the token.
      #
      # @param name [Symbol] the token to match.
      # @return [Array] the token.
      # @raise SyntaxError if the peeked token is invalid.
      def match(name)
        if name == peek
          input.next
        else
          raise SyntaxError, "Unexpected token #{peek}"
        end
      end

      # Returns the type of the next token.
      #
      # @return [Symbol] the type of the next token.
      def peek
        input.peek[0]
      rescue StopIteration
        nil
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
take-0.0.15 lib/take/unit/predict_match.rb
take-0.0.14 lib/take/unit/predict_match.rb
take-0.0.13 lib/take/unit/predict_match.rb
take-0.0.12 lib/take/unit/predict_match.rb
take-0.0.11 lib/take/unit/predict_match.rb
take-0.0.10 lib/take/unit/predict_match.rb
take-0.0.9 lib/take/unit/predict_match.rb
take-0.0.8 lib/take/unit/predict_match.rb
take-0.0.7 lib/take/unit/predict_match.rb
take-0.0.6 lib/take/unit/predict_match.rb
take-0.0.5 lib/take/unit/predict_match.rb
take-0.0.4 lib/take/unit/predict_match.rb
take-0.0.3 lib/take/unit/predict_match.rb
take-0.0.2 lib/take/unit/predict_match.rb