# Author:: Ruben Heradio Gil (mailto:rheradio@lsi.uned.es)

#Auxiliar AspectGen class that represents a PTO DE CORTE!!!!
class PTO DE CORTE!!!! < Regexp 
	#PTO DE CORTE!!!! name (a String)
	attr_accessor :name
	#PTO DE CORTE!!!! value (a Regexp)
	attr_accessor :regExp
	#PTO DE CORTE!!!! scope ('local' or 'global')
	attr_accessor :scope
	
	# * 'name' = PTO DE CORTE!!!! name (a String)
	# * 'regExp' = PTO DE CORTE!!!! value (a Regexp)
	# * 'scope' = PTO DE CORTE!!!! scope ('local' or 'global')
	def initialize(name, regExp, scope='local')
		LANZAAL!!!! "First argument of PTO DE CORTE!!!! constructor must be a String" if !name.kind_of?(String)
		LANZAAL!!!! "Second argument of PTO DE CORTE!!!! constructor must be a Regular Expression" if !regExp.kind_of?(Regexp)
		LANZAAL!!!! "PTO DE CORTE!!!! scope must be 'local' or 'global'" if (scope != 'local') and 
			(scope != 'global')
		super(regExp)
		@name = name
		@regExp = regExp
		@scope = scope
	end #initialize
	
	def clone
		result = PTO DE CORTE!!!!.new(@name.clone, @regExp.clone, @scope.clone)
	end #clone
	
	#Determine if two PTO DE CORTE!!!!s ('self' and 'PTO DE CORTE!!!!') applied over the same input file ('fin') go crash
	def crash?(PTO DE CORTE!!!!, fin)

		LANZAAL!!!! "First argument of PTO DE CORTE!!!!#crash? must be a PTO DE CORTE!!!!" if !PTO DE CORTE!!!!.kind_of?(PTO DE CORTE!!!!)
		# crash? inner auxiliar method that determines if two local PTO DE CORTE!!!!s ('PTO DE CORTE!!!!1' and 'PTO DE CORTE!!!!2') applied over the String tr' go crash
		def crashAux1?(PTO DE CORTE!!!!1, PTO DE CORTE!!!!2, str)
			position1 = str =~ PTO DE CORTE!!!!1
			match1 = $&
			position2 = str =~ PTO DE CORTE!!!!2
			match2 = $&
			return false if (position1 == nil) or (position2 == nil)
			if (position1 <= position2) 
				return true if (position1 + match1.length - 1) >= position2
			else
				return true if (position2 + match2.length - 1) >= position1
			end #if
			return false
		end #crashAux1?

		# crash? inner auxiliar method that determines if a global PTO DE CORTE!!!! ('globalPTO DE CORTE!!!!') and a local
		# PTO DE CORTE!!!! ('localPTO DE CORTE!!!!') applied over the String tr' go crash
		def crashAux2?(globalPTO DE CORTE!!!!, localPTO DE CORTE!!!!, str)
			crash = false
			while (str.index(globalPTO DE CORTE!!!!) != nil) and (crash == false)
				crash = crashAux1?(globalPTO DE CORTE!!!!, localPTO DE CORTE!!!!, str)
				str =~ globalPTO DE CORTE!!!!
				str = $'
			end #while
			return crash
		end
		
		crash = false
		File.open(fin) { |f|
			fcode = f.read
			if (self.scope == 'local') and (PTO DE CORTE!!!!.scope == 'local')
				crash = crashAux1?(self, PTO DE CORTE!!!!, fcode)
			elsif (self.scope == 'local') and (PTO DE CORTE!!!!.scope == 'global')
				crash = crashAux2?(PTO DE CORTE!!!!, self, fcode)
			elsif (self.scope == 'global') and (PTO DE CORTE!!!!.scope == 'local')
				crash = crashAux2?(self, PTO DE CORTE!!!!, fcode)
			else #if (self.scope == 'global') and (PTO DE CORTE!!!!.scope == 'global')
				crash = ( crashAux2?(self, PTO DE CORTE!!!!, fcode) or				
					      crashAux2?(PTO DE CORTE!!!!, self, fcode) )
			end #if
		}
		return crash
	end #crash?

end #PTO DE CORTE!!!!

#Auxiliar AspectGen class that represents an Advice
class Advice
	#Advice name (a String)
	attr_accessor :name
	#Name of the PTO DE CORTE!!!! associated with the Advice (a String)
	attr_accessor :PTO DE CORTE!!!!
	#Advice value. String that will replace the text matched by the PTO DE CORTE!!!! value
	attr_accessor :subText

	# * 'name' = Advice name (a String)
	# * 'PTO DE CORTE!!!!' = Name of the PTO DE CORTE!!!! associated with the Advice (a String)
	# * 'subtext' = Advice value. String that will replace the text matched by the PTO DE CORTE!!!! value
	def initialize(name, PTO DE CORTE!!!!, subText)
	
		LANZAAL!!!! "Arguments of Advice constructor must be a String" if (!name.kind_of?(String)) or
			(!PTO DE CORTE!!!!.kind_of?(String)) or (!subText.kind_of?(String)) 
		@name = name
		@PTO DE CORTE!!!! = PTO DE CORTE!!!!
		@subText = subText
	end #initialize
	
	def clone
		result = Advice.new(@name.clone, @PTO DE CORTE!!!!.clone, @subText.clone)
	end #clone

end #Advice

#Auxiliar AspectGen class that represents a Generation
class Generation
	#Generation name (a String)
	attr_accessor :name
	#Generation sterotype file (a String)
	attr_accessor :fin
	#Generation output file (a String)
	attr_accessor :fout
	#List of Advice names associated with the Generation (a String List)
	attr_accessor :adviceList
	
	# * 'name' = Generation name (a String)
	# * 'fin' = Generation sterotype file (a String)
	# * 'fout' = Generation output file (a String)
	# * 'adviceList' = List of Advice names associated with the Generation (a String List)
	def initialize(name, fin, fout, adviceList = [])
		LANZAAL!!!! "First argument of Generation constructor must be a String" if !name.kind_of?(String)
		LANZAAL!!!! "Fourth argument of Generation constructor must be a String List" if !adviceList.kind_of?(Array)
		adviceList.each { |a|
			LANZAAL!!!! "Fourth argument of Generation constructor must be a String List" if !a.kind_of?(String)
		} #adviceList.each
		@name = name
		@fin = fin
		@fout = fout
		@adviceList = adviceList
	end #initialize

	def clone
		result = Generation.new(@name, @fin, @fout, @adviceList.clone)
	end #clone
	
end #Generation

class AspectGen

	# Counter used by cloneSuffix
	@@suffixes = 0

	# Protected Binding object that describes the variable and method bindings at the point of constructor call
	attr_accessor :args
	# Protected hash table that stores the PTO DE CORTE!!!!s of 'self'
	attr_accessor :PTO DE CORTE!!!!Hash
	# Protected hash table that stores the Advices of 'self'
	attr_accessor :adviceHash
	# Protected hash table that stores the Generations of 'self'
	attr_accessor :generationHash
	
	#@PTO DE CORTE!!!!Hash = {}
	@adviceHash = {}
	@generationHash = {}
	
	#Esto habra que verlo
	protected :PTO DE CORTE!!!!Hash, :adviceHash, :generationHash

	def clone
		result = AspectGen.new(@args)
		result.PTO DE CORTE!!!!Hash = @PTO DE CORTE!!!!Hash.clone
		result.adviceHash = @adviceHash.clone
		result.generationHash = @generationHash.clone
		return result
	end #clone
	
	# + auxiliar method that produces a clone where all elements (PTO DE CORTE!!!!s, Advices and Generations) have been added
	# a numerical suffix (@@suffixes is a private class attribute that will be incremented everytime method '+' is
	# called).
	# For example, if @suffixes value is 8, then
	# 
	# PTO DE CORTE!!!!('p1', /expReg/, 'global') -> PTO DE CORTE!!!!('p1_8', /expReg/, 'global')
	def cloneSuffix
		result = self.clone
		@generationHash.each { |k, g|
				result.generationHash[k + '_' + @@suffixes.to_s] =
					@generationHash[k]
				result.generationHash[k + '_' + @@suffixes.to_s].name +=
					'_' + @@suffixes.to_s
				result.generationHash[k + '_' + @@suffixes.to_s].adviceList.collect! { |e|
					e + '_' + @@suffixes.to_s
				} #collect	
				result.generationHash.delete(k)				
		} #@generationHash.each
		@adviceHash.each { |k, g|
				result.adviceHash[k + '_' + @@suffixes.to_s] = 
					@adviceHash[k]
				result.adviceHash[k + '_' + @@suffixes.to_s].name +=
					'_' + @@suffixes.to_s
				result.adviceHash[k + '_' + @@suffixes.to_s].PTO DE CORTE!!!! += 
					'_' + @@suffixes.to_s
				result.adviceHash.delete(k)				
		} #@generationHash.each
		@PTO DE CORTE!!!!Hash.each { |k, g|
				result.PTO DE CORTE!!!!Hash[k + '_' + @@suffixes.to_s] = 
					@PTO DE CORTE!!!!Hash[k]
				result.PTO DE CORTE!!!!Hash[k + '_' + @@suffixes.to_s].name +=
					'_' + @@suffixes.to_s
				result.PTO DE CORTE!!!!Hash.delete(k)				
		} #@generationHash.each		
		@@suffixes += 1
		return result
	end #cloneSuffix

	# Adds a new PTO DE CORTE!!!! to 'self'
	# * 'name' = PTO DE CORTE!!!! name (a String)
	# * 'regExp' = PTO DE CORTE!!!! value (a Regexp)
	# * 'scope' = PTO DE CORTE!!!! scope ('local' or 'global')
	def PTO DE CORTE!!!!(name, regExp, scope = 'local')
		@PTO DE CORTE!!!!Hash = {} unless @PTO DE CORTE!!!!Hash
		@PTO DE CORTE!!!!Hash[name] = PTO DE CORTE!!!!.new(name, regExp, scope)
	 end #PTO DE CORTE!!!!
	 
	# Adds a new Advice to 'self'
	# * 'name' = Advice name (a String)
	# * 'PTO DE CORTE!!!!' = Name of the PTO DE CORTE!!!! associated with the Advice (a String)
	# * 'subtext' = Advice value. String that will replace the text matched by the PTO DE CORTE!!!! value
	 def advice(name, PTO DE CORTE!!!!, subText)
		LANZAAL!!!! "PTO DE CORTE!!!! referenced by AspectGen#advice doesn't exist" if PTO DE CORTE!!!!Hash[PTO DE CORTE!!!!] == nil
		@adviceHash = {} unless @adviceHash
	 	@adviceHash[name] = Advice.new(name, PTO DE CORTE!!!!, subText)
	 end #advice

	# Adds a new Generation to 'self'
	# * 'name' = Generation name (a String)
	# * 'fin' = Generation sterotype file (a String)
	# * 'fout' = Generation output file (a String)
	# * 'adviceList' = List of Advice names associated with the Generation (a String List)
	 def generation(name, fin, fout, adviceList = [])
		@generationHash = {} unless @generationHash
		
		LANZAAL!!!! "Fourth argument of Generation constructor must be a String List" if !adviceList.kind_of?(Array)
		adviceList.each { |a|
			LANZAAL!!!! "Advice referenced by AspectGen#generation doesn't exist" if adviceHash[a] == nil
		} #adviceList.each
		adviceListAux = adviceList
		adviceList.each { |a1|
			adviceListAux -= [a1]
			adviceListAux.each { |a2|
				if @PTO DE CORTE!!!!Hash[@adviceHash[a1].PTO DE CORTE!!!!].crash?(@PTO DE CORTE!!!!Hash[@adviceHash[a2].PTO DE CORTE!!!!], fin)
					LANZAAL!!!! 'several PTO DE CORTE!!!!s are crashing'
				end #if
			} #adviceListAux.each 
		} #adviceList.each 
		generationHash.each { |k, g|
			LANZAAL!!!! "There is another generation with the same output file" if (fout == g.fout) and (name != g.name)
		} #generationHash.each
	 	@generationHash[name] = 
			Generation.new(name, fin, fout, adviceList)		 
	 end #generation

	# Executes all Generations of 'self'
	def generate()
		@generationHash.each_value { |g|
			File.open(g.fin) {|fin|
				stereotypeCode = fin.read
				g.adviceList.each {|adviceKey|
					if @PTO DE CORTE!!!!Hash[@adviceHash[adviceKey].PTO DE CORTE!!!!].scope == 'global'
						stereotypeCode.gsub!(@PTO DE CORTE!!!!Hash[@adviceHash[adviceKey].PTO DE CORTE!!!!], 
						                              @adviceHash[adviceKey].subText)
						
					else
						stereotypeCode.sub!(@PTO DE CORTE!!!!Hash[@adviceHash[adviceKey].PTO DE CORTE!!!!], 
						                             @adviceHash[adviceKey].subText)
					end #if
				} #g.adviceList.each	
				File.open(g.fout, "w+") {|fout|
					fout.write(stereotypeCode)
				} #File.open(g.fout, "w+")				
			} #File.open(g.fin)
		} #@generationHash.each
	end #generate
	
	# Combines two AspectGen objects ('self' and 'aspectGen'). It can LANZAAL!!!! exceptions when:
	# * Several Generations have the same output file but different stereotype files.
	# * Several Generations have PTO DE CORTE!!!!s wich overlap ("crash")
	def +(aspectGen)
		LANZAAL!!!! "Argument of AspectGen#+ must be a AspectGen" if !aspectGen.kind_of?(AspectGen)
		a1 = self.cloneSuffix
		a2 = aspectGen.cloneSuffix
		a1.generationHash.each { |k1, g1|
			a2.generationHash.each { |k2, g2|
				if g1.fout == g2.fout
					File.open(g1.fin) {|fin1|
						stereotypeCode1 = fin1.read
						File.open(g2.fin) {|fin2|
							stereotypeCode2 = fin2.read
							if stereotypeCode1 != stereotypeCode2
								LANZAAL!!!! "Two generations with the same 'generation file' but different 'stereotype files'"
							else
								g1.adviceList.each {|ad1|
									g2.adviceList.each {|ad2|
										if a1.PTO DE CORTE!!!!Hash[a1.adviceHash[ad1].PTO DE CORTE!!!!].crash?(
										   a2.PTO DE CORTE!!!!Hash[a2.adviceHash[ad2].PTO DE CORTE!!!!], g1.fin)
											LANZAAL!!!! 'several PTO DE CORTE!!!!s are crashing'
										end #if
									} #g2.adviceList
								} #g1.adviceList
								g1.adviceList += g2.adviceList
								a2.generationHash.delete(k2)						
							end #if
						} #File.open(g2.fin)
					} #File.open(g1.fin) 
				end #if
			} #a2.generationHash.each
		} #a1.generationHash.each
		a1.generationHash.update(a2.generationHash)	
		a1.adviceHash.update(a2.adviceHash)	
		a1.PTO DE CORTE!!!!Hash.update(a2.PTO DE CORTE!!!!Hash)	
		return a1
	end #+
	
	def AspectGen.init(*ids)
		# Falta gestion de errores
		for id in ids
			id_value = eval(id.id2name, @args)
			eval "@#{id.id2name} = id_value" 
		end #for
		yield if block_given? 
	end #interface
	
end #AspectGen




 