Rescue and re-raise syntax errors in Template#parse to add line numbers.

This can be done now that the parse context has the line number
information, so it doesn't need to be added on closer to the original
exception.  This has the advantage of not having to rescue and re-raise the
exception multiple times, and simplifies liquid-c which would otherwise
have to rescue the exception in BlockBody#parse.
This commit is contained in:
Dylan Thacker-Smith
2015-07-08 19:21:59 -04:00
parent cebf75b8d7
commit 920e1df643
3 changed files with 25 additions and 27 deletions
-5
View File
@@ -15,7 +15,6 @@ module Liquid
def parse(tokenizer, parse_context) def parse(tokenizer, parse_context)
parse_context.line_number = tokenizer.line_number parse_context.line_number = tokenizer.line_number
while token = tokenizer.shift while token = tokenizer.shift
begin
unless token.empty? unless token.empty?
case case
when token.start_with?(TAGSTART) when token.start_with?(TAGSTART)
@@ -43,10 +42,6 @@ module Liquid
@blank &&= !!(token =~ /\A\s*\z/) @blank &&= !!(token =~ /\A\s*\z/)
end end
end end
rescue SyntaxError => e
e.line_number ||= parse_context.line_number
raise
end
parse_context.line_number = tokenizer.line_number parse_context.line_number = tokenizer.line_number
end end
+1 -1
View File
@@ -8,7 +8,6 @@ module Liquid
begin begin
return strict_parse_with_error_context(markup) return strict_parse_with_error_context(markup)
rescue SyntaxError => e rescue SyntaxError => e
e.line_number = line_number
@options.warnings << e @options.warnings << e
return lax_parse(markup) return lax_parse(markup)
end end
@@ -20,6 +19,7 @@ module Liquid
def strict_parse_with_error_context(markup) def strict_parse_with_error_context(markup)
strict_parse(markup) strict_parse(markup)
rescue SyntaxError => e rescue SyntaxError => e
e.line_number = line_number
e.markup_context = markup_context(markup) e.markup_context = markup_context(markup)
raise e raise e
end end
+3
View File
@@ -120,6 +120,9 @@ module Liquid
@root = Document.parse(tokenize(source), parse_context) @root = Document.parse(tokenize(source), parse_context)
@warnings = parse_context.warnings @warnings = parse_context.warnings
self self
rescue SyntaxError => e
e.line_number ||= parse_context.line_number
raise
end end
def registers def registers