Sha256: 9d68dea96daf08f72f322ef991ee3dea29cf4be553dfd84de4cfb3f71e21ee4e

Contents?: true

Size: 866 Bytes

Versions: 3

Compression:

Stored size: 866 Bytes

Contents

# frozen_string_literal: true

module Mutant
  class Expression
    class Parser
      include Anima.new(:types)

      # Parse expression
      #
      # @param [String] input
      #
      # @return [Either<String, Expression>]
      #   if expression is valid
      #
      # @return [nil]
      #   otherwise
      def call(input)
        case expressions(input)
        in []
          Either::Left.new("Expression: #{input.inspect} is invalid")
        in [expression]
          Either::Right.new(expression)
        else
          Either::Left.new("Expression: #{input.inspect} is ambiguous")
        end
      end

    private

      def expressions(input)
        types.each_with_object([]) do |type, aggregate|
          expression = type.try_parse(input) and aggregate << expression
        end
      end

    end # Parser
  end # Expression
end # Mutant

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutant-0.12.4 lib/mutant/expression/parser.rb
mutant-0.12.3 lib/mutant/expression/parser.rb
mutant-0.12.2 lib/mutant/expression/parser.rb