require "Parser"

module EO
	class LexicalUnit < Array
		def initialize input, separators, unit_class, &block
			contents = input.scan( /((.+?)([#{separators}]))|(.+)/ )
			contents.each { |unit| 
				unless unit[1].nil?
					self << unit_class.new( *unit[1,2], &block ) 
				else
					self << unit_class.new( unit[3], "", &block )
				end
			}
		end
	end

# A paragraph is a collection of sentances separated by !?.
	class Paragrafo < LexicalUnit
		def initialize(input, &block)	# input being a String
			super input, "!?.", Sintagmo, &block
		end
	end

# A sentence is a collection of phrazes separated by ,;:
	class Sintagmo < LexicalUnit
		EXCLAIM = "!"
		QUESTION = "?"
		PERIOD = "."
		attr_reader :punctuate
		def initialize(input, separator, &block)
			@punctuate = separator
			super input, ",;:", Frazo, &block
		end
	end

# A phraze is a collection of words separated by whitespace
	class Frazo < Array
		attr_reader :punctuate
		COMMA=","
		SEMICOLON=";"
		COLON=":"
		def initialize(input, separator, &block)
			@punctuate = separator
			input.split.each { |vorto|
				vorto.tr! %q{"'<>}, ''
				self << Parser.parse( vorto, &block )
			}
		end
	end
end
