Sha256: 55a7dce8e9885b97fa9af220e3b562894df13ac5ad4f241bd0f1e085ee111834

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Dendroid
  # The namespace for all classes used to build a grammar.
  module Syntax
    # Abstract class for grammar symbols.
    # A grammar symbol is an element that appears in grammar rules.
    class GrmSymbol
      # @return [String] The name of the grammar symbol
      attr_reader :name

      # Constructor.
      # aSymbolName [String] The name of the grammar symbol.
      def initialize(symbolName)
        @name = valid_name(symbolName)
      end

      # The String representation of the grammar symbol
      # @return [String]
      def to_s
        name.to_s
      end

      # Equality testing (based on symbol names)
      # @return [Boolean]
      def ==(other)
        name == other.name
      end

      private

      def valid_name(symbolName)
        if symbolName.is_a?(String)
          stripped = symbolName.strip
          if stripped.empty?
            err_msg = 'A symbol name cannot be empty.'
            raise StandardError, err_msg
          end
          stripped.to_sym
        else
          symbolName
        end
      end
    end # class
  end # module
end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dendroid-0.0.9 lib/dendroid/syntax/grm_symbol.rb
dendroid-0.0.8 lib/dendroid/syntax/grm_symbol.rb