From fd23821950f0562a8995735105cd31fdc6d55933 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Fri, 22 Jul 2011 16:55:01 +0100 Subject: * Rejigged configuration * Added --test and --verbose flags for the server config * Started proper test suite * Config parsing now gives more sensible errors + backtrace * Rejigged people and source lists --- lib/object_builder.rb | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'lib/object_builder.rb') 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 -- cgit v1.2.1