lib/wlang/template.rb in wlang-0.8.4 vs lib/wlang/template.rb in wlang-0.8.5

- old
+ new

@@ -50,14 +50,14 @@ end # Returns template's source text def source_text case @source - when String - @source - else - @source.to_s + when String + @source + else + @source.to_s end end # # Instantiate the template, with an additional context and an output buffer. @@ -70,9 +70,31 @@ instantiated = parser.instantiate unless context.nil? @context.pop end instantiated[0] + end + + # Returns a friendly position of an offset in the source text + def where(offset) + src = source_text + "#{@source_file}:#{src.__wlang_line_of(offset)}:#{src.__wlang_column_of(offset)-1}" + end + + # Raises a WLang::Error for the given offset + def error(offset, msg = "") + src = source_text + line, column = src.__wlang_line_of(offset), src.__wlang_column_of(offset)-1 + raise WLang::Error, "#{@source_file}:#{line}:#{column} #{msg}" + end + + # Raises a friendly ParseError, with positions and so on + def parse_error(offset, msg = "") + src = source_text + line, column = src.__wlang_line_of(offset), src.__wlang_column_of(offset)-1 + ex = ParseError.new("#{@source_file}:#{line}:#{column} #{msg}") + ex.line, ex.column = line, column + raise ex end end # class Template end # module WLang \ No newline at end of file