From 920e1df643541bb461fdbd77f513f8636b9de11a Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Sun, 5 Jul 2015 16:07:47 -0400 Subject: [PATCH] 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. --- lib/liquid/block_body.rb | 47 +++++++++++++++------------------- lib/liquid/parser_switching.rb | 2 +- lib/liquid/template.rb | 3 +++ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index b93d4a6..9efa214 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -15,37 +15,32 @@ module Liquid def parse(tokenizer, parse_context) parse_context.line_number = tokenizer.line_number while token = tokenizer.shift - begin - unless token.empty? - case - when token.start_with?(TAGSTART) - if token =~ FullToken - tag_name = $1 - markup = $2 - # fetch the tag from registered blocks - if tag = registered_tags[tag_name] - new_tag = tag.parse(tag_name, markup, tokenizer, parse_context) - @blank &&= new_tag.blank? - @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 + unless token.empty? + case + when token.start_with?(TAGSTART) + if token =~ FullToken + tag_name = $1 + markup = $2 + # fetch the tag from registered blocks + if tag = registered_tags[tag_name] + new_tag = tag.parse(tag_name, markup, tokenizer, parse_context) + @blank &&= new_tag.blank? + @nodelist << new_tag 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 - when token.start_with?(VARSTART) - @nodelist << create_variable(token, parse_context) - @blank = false else - @nodelist << token - @blank &&= !!(token =~ /\A\s*\z/) + raise_missing_tag_terminator(token, parse_context) end + when token.start_with?(VARSTART) + @nodelist << create_variable(token, parse_context) + @blank = false + else + @nodelist << token + @blank &&= !!(token =~ /\A\s*\z/) end - rescue SyntaxError => e - e.line_number ||= parse_context.line_number - raise end parse_context.line_number = tokenizer.line_number end diff --git a/lib/liquid/parser_switching.rb b/lib/liquid/parser_switching.rb index 631a9a5..363e93e 100644 --- a/lib/liquid/parser_switching.rb +++ b/lib/liquid/parser_switching.rb @@ -8,7 +8,6 @@ module Liquid begin return strict_parse_with_error_context(markup) rescue SyntaxError => e - e.line_number = line_number @options.warnings << e return lax_parse(markup) end @@ -20,6 +19,7 @@ module Liquid def strict_parse_with_error_context(markup) strict_parse(markup) rescue SyntaxError => e + e.line_number = line_number e.markup_context = markup_context(markup) raise e end diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 07d56b2..57aa291 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -120,6 +120,9 @@ module Liquid @root = Document.parse(tokenize(source), parse_context) @warnings = parse_context.warnings self + rescue SyntaxError => e + e.line_number ||= parse_context.line_number + raise end def registers