diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-07-22 16:55:54 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-07-22 16:55:54 +0100 |
commit | 25b32914b72a5f709eca773f3511cc89c2e710c2 (patch) | |
tree | 4dab9e78e4c6b49220a837b38d463328c05e2983 /lib/object_builder.rb | |
parent | 3185e5d746abda1b7f42ecdbd74ec14359fda3bc (diff) | |
parent | fd23821950f0562a8995735105cd31fdc6d55933 (diff) |
merge
Diffstat (limited to 'lib/object_builder.rb')
-rw-r--r-- | lib/object_builder.rb | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/lib/object_builder.rb b/lib/object_builder.rb index 7cb808c..a455b3e 100644 --- a/lib/object_builder.rb +++ b/lib/object_builder.rb @@ -41,12 +41,14 @@ # TODO: finish this convoluted example, if it kills me # class ObjectBuilder - class BuildException < Exception; end + class BuildException < StandardError; end - attr_reader :result + attr_reader :result + attr_accessor :block_result def initialize(context, *args) @context = context + @result = nil builder_setup(*args) end @@ -93,11 +95,38 @@ class ObjectBuilder end def load(file) + parse(File.read(file), file) + end + + def parse(string, file="string") builder = self.new - builder.instance_eval(File.read(file), file) + begin + builder.instance_eval(string, file) + rescue NameError => ex + # + # Ugh. Catch NameError and re-raise as a BuildException + # + f,l = ex.backtrace.first.split(":").first(2) + if f == file + build_ex = BuildException.new "Unknown word `#{ex.name}' in #{file} at line #{l}" + build_ex.set_backtrace ex.backtrace + raise build_ex + else + raise ex + end + rescue SyntaxError, ArgumentError => ex + if ex.backtrace.find{|l| l =~ /^#{file}:(\d+):/} + build_ex = BuildException.new "#{ex.message} in #{file} at line #{$1}" + build_ex.set_backtrace ex.backtrace + raise build_ex + else + raise ex + end + end + builder.result end - + def inherited(*args) initialize_class end @@ -106,7 +135,7 @@ class ObjectBuilder @words = {} end end - + initialize_class end |