Sha256: 440bc007ddbc0b146d2e689340d43af403005daec6931905e019ca9e973e1712

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

# File: char_shorthand.rb

require_relative "atomic_expression"	# Access the superclass

module Regex # This module is used as a namespace

  # A pre-defined character class is in essence a name for a built-in, standard character class.
  class CharShorthand < AtomicExpression
    # A constant Hash that defines all the predefined character shorthands.
    # It contains pairs of the form:
    # a pre-defined character shorthand letter => a CharRange object
    StandardCClasses = {
      'd' => '[0-9]',
      'D' => '[^0-9]',
      'h' => '[0-9a-fA-F]',
      'H' => '[^0-9a-fA-F]',
      's' => '[ \t\r\n\f]',
      'S' => '[^ \t\r\n\f]',
      'w' => '[0-9a-zA-Z_]',
      'W' => '[^0-9a-zA-Z_]'
    }

    # An one-letter abbreviation
    attr_reader(:shortname)

    # Constructor
    def initialize(aShortname)
      @shortname = valid_shortname(aShortname)
    end

    protected
  
    # Conversion method re-definition.
    # Purpose: Return the String representation of the expression.
    def text_repr()
      return "\\#{shortname}"
    end

  private
    # Return the validated short name.
    def valid_shortname(aShortname)
      raise StandardError, "Unknown predefined character class \\#{aShortname}" unless StandardCClasses.include? aShortname

      return aShortname
    end

  end # class

end # module

# End of file

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rley-0.6.00 examples/general/SRL/lib/regex/char_shorthand.rb
rley-0.5.14 examples/general/SRL/lib/regex/char_shorthand.rb
rley-0.5.13 examples/general/SRL/lib/regex/char_shorthand.rb
rley-0.5.12 examples/general/SRL/lib/regex/char_shorthand.rb