lib/macros4cuke/templating/engine.rb in macros4cuke-0.3.09 vs lib/macros4cuke/templating/engine.rb in macros4cuke-0.3.10

- old
+ new

@@ -286,38 +286,43 @@ tag_literal = scanner.scan(/<(?:[^\\<>]|\\.)*>/) result << [:dynamic, tag_literal.gsub(/^<|>$/, '')] unless tag_literal.nil? # ... or scan plain text at current position text_literal = scanner.scan(/(?:[^\\<>]|\\.)+/) - result << [:static, text_literal] unless text_literal.nil? - - if tag_literal.nil? && text_literal.nil? - # Unsuccessful scanning: we have improperly balanced chevrons. - # We will analyze the opening and closing chevrons... - no_escaped = aTextLine.gsub(/\\[<>]/, "--") # First: replace escaped chevron(s) - unbalance = 0 # = count of < - count of > (can only be 0 or -temporarily- 1) - - no_escaped.scan(/(.)/) do |match| - case match[0] - when '<' - unbalance += 1 - when '>' - unbalance -= 1 - end - - raise StandardError, "Nested opening chevron '<'." if unbalance > 1 - raise StandardError, "Missing opening chevron '<'." if unbalance < 0 - end - - raise StandardError, "Missing closing chevron '>'." if unbalance == 1 - raise StandardError, "Cannot parse:\n'#{aTextLine}'" - end + result << [:static, text_literal] unless text_literal.nil? + identify_parse_error(aTextLine) if tag_literal.nil? && text_literal.nil? end return result end private + # Called when the given text line could not be parsed. + # Raises an exception with the syntax issue identified. + # @param aTextLine [String] A text line from the template. + def self.identify_parse_error(aTextLine) + # Unsuccessful scanning: we typically have improperly balanced chevrons. + # We will analyze the opening and closing chevrons... + no_escaped = aTextLine.gsub(/\\[<>]/, "--") # First: replace escaped chevron(s) + unbalance = 0 # This variable equals: count of < - count of > (can only be 0 or -temporarily- 1) + + no_escaped.scan(/(.)/) do |match| + case match[0] + when '<' + unbalance += 1 + when '>' + unbalance -= 1 + end + + raise StandardError, "Nested opening chevron '<'." if unbalance > 1 + raise StandardError, "Missing opening chevron '<'." if unbalance < 0 + end + + raise StandardError, "Missing closing chevron '>'." if unbalance == 1 + raise StandardError, "Cannot parse:\n'#{aTextLine}'" + end + + # Create the internal representation of the given template. def compile(aSourceTemplate) # Split the input text into lines. input_lines = aSourceTemplate.split(/\r\n?|\n/) \ No newline at end of file