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
+21 -26
View File
@@ -15,37 +15,32 @@ 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) if token =~ FullToken
if token =~ FullToken tag_name = $1
tag_name = $1 markup = $2
markup = $2 # fetch the tag from registered blocks
# fetch the tag from registered blocks if tag = registered_tags[tag_name]
if tag = registered_tags[tag_name] new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context) @blank &&= new_tag.blank?
@blank &&= new_tag.blank? @nodelist << new_tag
@nodelist << new_tag
else
# end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
end
else else
raise_missing_tag_terminator(token, parse_context) # end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
end end
when token.start_with?(VARSTART)
@nodelist << create_variable(token, parse_context)
@blank = false
else else
@nodelist << token raise_missing_tag_terminator(token, parse_context)
@blank &&= !!(token =~ /\A\s*\z/)
end end
when token.start_with?(VARSTART)
@nodelist << create_variable(token, parse_context)
@blank = false
else
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
end end
rescue SyntaxError => e
e.line_number ||= parse_context.line_number
raise
end 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