lib/fault_injection/parser.rb in faultinjection-0.0.2 vs lib/fault_injection/parser.rb in faultinjection-0.0.3
- old
+ new
@@ -3,23 +3,48 @@
# parser.rb:30 => line 30 in parser.rb
#
module FaultInjection
class Parser
def self.compile src
- method_pat = '([A-Z][A-Za-z0-9_]*)(?:#|::)([a-z_][a-zA-Z0-9_]*)'
+ method_pat = /
+ ([A-Z][A-Za-z0-9_]*) # class name
+ (?: \#|:: ) # '#' or '::', they are treated as same now.
+ ([a-z_][a-zA-Z0-9_]*) # method name
+ /x
+ method_lineno_pat = /
+ ([A-Z][A-Za-z0-9_]*) # class name
+ (?: \#|:: ) # '#' or '::', they are treated as same.
+ ([a-z_][a-zA-Z0-9_]*) # method name
+ (?: :(\d+) ) # line number in method
+ /x
+
+ file_line_pat = /^([A-Za-z0-9_.~-]+):(\d+)$/
src.gsub!(/\s/,'')
- if src =~ /([A-Za-z0-9_.~-]+):(\d+)/
- return FaultConditionLine.new($1,$2.to_i)
-
+ if src =~ file_line_pat
+ return FaultConditionLine.new($~[1],$~[2].to_i)
+
+ elsif src =~ method_lineno_pat
+ #STDERR.puts("Class=#{$~[1]},method='#{$~[2]}',line='#{$~[3]}'")
+ klass = eval($~[1]) rescue raise(NameError,"No such class:'#{$~[1]}'")
+ method = $~[2]
+ line_in_meth = $~[3].to_i
+
+ # Proc#to_s contains file name & line number in which
+ # the method is defined.
+ method_info = klass.new.method(method).to_proc.to_s
+ method_info =~ /@([^:]+):(\d+)>$/
+ #STDERR.puts("file=#{$~[1]},line=#{$~[2]}")
+ return FaultConditionLine.new($~[1], $~[2].to_i + line_in_meth)
+
else
methods = src.split(">")
methods.map! do |e|
- unless e =~ Regexp.new(method_pat)
+ unless e =~ method_pat
raise ArgumentError,"Invalid method name:'#{e}'"
end
-
- [$1.to_sym,$2.to_sym]
+
+ [$~[1].to_sym, $~[2].to_sym]
end
c = FaultConditionCall.new
c.stack_pattern = methods.reverse # callee first