Class: Parse

Inherits:
Object
  • Object
show all
Defined in:
lib/aml/parse.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, file, partial = false, bundle = false) ⇒ Parse

Returns a new instance of Parse.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aml/parse.rb', line 11

def initialize(path, file, partial=false, bundle=false)
	@path	=	path
	@regex	=	{
		:attribute		=>	/@\(\:(?<name>[\w|\-]+)\)/,
		:comment		=>	/%!--[^%]*--%$/,
		:variable		=>	/\@\(((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)/
	}
	@types	=	{
		:method			=>	LineType.new(/^(\s{1,})?::((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)(\{(?<attributes>.+)\})?(?<value>.+)?/),
		:variable_def	=>	LineType.new(/^(\s{1,})?\@((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\s?(\=)\s?(?<value>.+)?$/),
		:mixin			=>	LineType.new(/^(\s{1,})?%\(((?<bundle>[\w|\-]+)\.)?(?<name>[^~][\w|\-]+)\)(\{(?<attributes>.+)\})?[^\{]?/),
		:mixin_def		=>	LineType.new(/^%%(?<name>[\w|\-]+)(\((?<attributes>.+?)\))?{/),
		:mixin_end		=> 	LineType.new(/^\}$/),
		:partial		=>	LineType.new(/^(\s{1,})?%\(\~((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)(\{(?<attributes>.+)\}[^\{]?)?$/),
		:tag			=>	LineType.new(/^(\s{1,})?(?<!%)%(?<close>\/{0,2})?(?<name>[\w|\-]+)(?<tab_reset>\*{1,})?(\#(?<id_first>(\@\(([\w|-]+\.)?)?[\w|\-]+(\))?))?(?<class>(\.[\w|\-]+)?{1,})?(\#(?<id_last>[\w|\-]+))?(\{(?<attributes>.+)\})?(?<text>.+)?/),
		:empty			=>	LineType.new(/^$/),
		:eval			=>	LineType.new(/^(\s{1,})?==(\s{1,})(?<value>.+)?/),
		:string			=>	LineType.new(/(\s{1,})?(?<value>.+)?/)
	}
	@file	=	{}
	@watch	=	[]
	@mixin 	=	[]
	read(file, partial, bundle)
end

Instance Method Details

#fileObject



2
3
4
# File 'lib/aml/parse.rb', line 2

def file
	return @file
end

#mixinObject



8
9
10
# File 'lib/aml/parse.rb', line 8

def mixin
	return @mixin
end

#mixin_watch(struct) ⇒ Object



76
77
78
79
80
# File 'lib/aml/parse.rb', line 76

def mixin_watch(struct)
	struct.reject{|k,v| k[:type] != :mixin}.each do |partial|
		@mixin << {:file => "mixin.aml", :bundle=> partial[:bundle], :partial => false} if partial[:bundle] != 'core'
	end
end

#partial_watch(struct) ⇒ Object



71
72
73
74
75
# File 'lib/aml/parse.rb', line 71

def partial_watch(struct)
	struct.reject{|k,v| k[:type] != :partial}.each do |partial|
		@watch << {:file => "#{partial[:name]}.aml", :bundle=> partial[:bundle], :partial => true}
	end
end

#read(file, partial, bundle) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aml/parse.rb', line 35

def read(file, partial, bundle)
	line_number = 0
	line_struct = []
	File.open(File.join(@path,file)).read.gsub(@regex[:comment],'').each_line do |line|
		@types.each do |type, line_type|
			if match = line_type.match?(line)
				struct = Hash[match.names.zip(match.captures)]
				struct = Hash[struct.map{|(k,v)| [k.to_sym,v]}]
				struct[:index] = line.match(/^\t{0,}/).to_s.length
				struct[:number] = line_number+=1
				struct[:type] = type
				match.names.each do |name|
					struct[name.to_sym] = line_type.send(name.to_s, match)
				end
				if struct[:name].to_s[0,1] == '.'
					#set undefined bundle
					struct[:bundle] = 'core'
					struct[:name] = struct[:name][1,struct[:name].length] 
				end
				struct[:bundle] = false if struct[:bundle].to_s.length == 0
				#set undefined method bundle
				struct[:bundle] = 'core' if struct[:type] == :method and !struct[:bundle]
				struct[:bundle] = bundle if bundle != false
				struct[:id] = struct[:id_first].to_s.length > 0 ? struct[:id_first] : struct[:id_last]
				struct.delete(:id_first)
				struct.delete(:id_last)
				line_struct << Hash[struct.sort] if struct[:type] != :empty
				break
			end
		end
	end
	@file = {:name => file, :line => line_struct}
	@watch << {:file => file, :bundle => false, :partial => false}
	partial_watch(line_struct)
	mixin_watch(line_struct)
end

#watchObject



5
6
7
# File 'lib/aml/parse.rb', line 5

def watch
	return @watch
end