Sha256: 8d8d97f41a7414223d85ac95c46115d40d973ce768ccd6683ea0fc1ae8d78305

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require_relative 'grm_symbol'

module Dendroid
  module Syntax
    # A sequence of grammar symbols. This class is used to represent
    # members of right-hand side of production rules
    class SymbolSeq
      extend Forwardable

      # @return [Array<Dendroid::Syntax::GrmSymbol>] The sequence of symbols
      attr_reader :members

      def_delegators(:@members, :[], :empty?, :first, :map, :size)

      # Create a sequence of grammar symbols (as in right-hand side of
      # a production rule).
      # @param symbols [Array<Dendroid::Syntax::GrmSymbol>] An array of symbols.
      def initialize(symbols)
        @members = symbols
      end

      # @return [String] A text representation of the symbol sequence
      def to_s
        members.join(' ')
      end

      # Retrieve all the non-terminal symbols in the sequence.
      # @return [Array<Dendroid::Syntax::NonTerminal>] array of non-terminals.
      def nonterminals
        members.reject(&:terminal?)
      end

      # Retrieve all the terminal symbols in the sequence.
      # @return [Array<Dendroid::Syntax::Terminal>] array of terminals
      def terminals
        members.select(&:terminal?)
      end

      # Predicate method to check whether the sequence always derives (matches)
      # a non-empty sequence of terminal symbols.
      # @return [Boolean]
      def productive?
        empty? || members.all?(&:productive?)
      end

      # Equality operator.
      # @param other [Dendroid::Syntax::SymbolSeq]
      # @return [Boolean] true when members are equal to the ones from `other`
      def ==(other)
        members == other.members
      end

      private

      def valid_members(symbols)
        raise StandardError unless symbols.all? { |symb| symb.is_a?(GrmSymbol) }
      end
    end # class
  end # module
end # module

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dendroid-0.2.04 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.2.03 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.2.02 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.2.01 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.2.00 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.1.00 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.0.12 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.0.11 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.0.10 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.0.9 lib/dendroid/syntax/symbol_seq.rb
dendroid-0.0.8 lib/dendroid/syntax/symbol_seq.rb