lib/wlang/errors.rb in wlang-0.8.5 vs lib/wlang/errors.rb in wlang-0.9.1

- old
+ new

@@ -1,16 +1,80 @@ module WLang # Main error of all WLang errors. - class Error < StandardError; end + class Error < StandardError + + # The parser state whe this error has been raised + attr_accessor :parser_state + + # Optional cause (other lower level exception) + attr_accessor :cause + + # Creates an error instance with a given parser state + def initialize(msg = nil, parser_state = nil, cause = nil) + raise ArgumentError, "msg expected to be nil or a String" unless (msg.nil? or String===msg) + raise ArgumentError, "parser_state expected to be nil or a State"\ + unless (parser_state.nil? or ::WLang::Parser::State===parser_state) + super(msg) + @parser_state = parser_state + @cause = cause + end + + # Returns a friendly wlang backtrace + def wlang_backtrace + parser_state ? parser_state.backtrace : ["no backtrace information, sorry"] + end + + end # class Error - # Raise when something fails when evaluting through the parser context - class EvalError < StandardError; end + # + # Raised by hosted languages when something fails during + # evaluation. + # + class EvalError < ::WLang::Error + + # The expression whose evaluation failed + attr_accessor :expression + + # Creates an error instance with an optional expression that + # failed + def initialize(msg = nil, parser_state = nil, expression = nil, cause = nil) + super(msg, parser_state, cause) + @expression = expression + end + + def to_s + "Evaluation of #{@expression} failed, #{@cause ? @cause.message : ''}" + end + + end # class EvalError + # + # Raised when a variable may not be found in the current + # parser scope + # + class UndefinedVariableError < ::WLang::EvalError + + # Name of the variable that could not be found + attr_accessor :variable + + # Creates an error instance with an optional variable name + def initialize(msg = nil, parser_state = nil, expression = nil, variable = nil) + super(msg, parser_state, expression) + @variable = variable + end + + def to_s + "Unable to find variable #{@variable}" + end + + end # class UndefinedVariableError + # Error raised by a WLang parser instanciation when an error occurs. - class ParseError < StandardError; + class ParseError < ::WLang::Error + # Where did the parsing failed attr_accessor :line, :column - end + end # class ParseError -end # module WLang +end # module WLang \ No newline at end of file