Sha256: 33d41f5da50eee7459afeaa0d1d256e2c312bbb43ed81413fcc64af1e3c98a4c

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# File: char_range.rb

require_relative 'polyadic_expression'	# Access the superclass

module Regex # This module is used as a namespace

# A binary expression that represents a contiguous range of characters.
# Assumption: characters are ordered by codepoint
class CharRange < PolyadicExpression
	
	# Constructor.
	# [thelowerBound] A character that will be the lower bound value for the range.
	# [theUpperBound] A character that will be the upper bound value for the range.
	# TODO: optimisation. Build a Character if lower bound == upper bound.
	def initialize(theLowerBound, theUpperBound)
		range = validated_range(theLowerBound, theUpperBound)
		super(range)
	end

public
	# Return the lower bound of the range.
	def lower()
		return children.first
	end
	
	# Return the upper bound of the range.
	def upper()
		return children.last
	end	

	# Conversion method re-definition.
	# Purpose: Return the String representation of the concatented expressions.
	def to_str()
		result = lower.to_str() + '-' + upper.to_str()
		
		return result
	end

private
	# Validation method. Returns a couple of Characters.after their validation.
	def validated_range(theLowerBound, theUpperBound)
		raise StandardError, "Character range error: lower bound is greater than upper bound." if theLowerBound.codepoint > theUpperBound.codepoint
		return [theLowerBound, theUpperBound]
	end

end # class

end # module

# End of file

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rley-0.5.11 examples/general/SRL/lib/regex/char_range.rb
rley-0.5.10 examples/general/SRL/lib/regex/char_range.rb
rley-0.5.09 examples/general/SRL/lib/regex/char_range.rb