lib/hamlit/parsers/script.rb in hamlit-0.4.2 vs lib/hamlit/parsers/script.rb in hamlit-0.4.3

- old
+ new

@@ -19,12 +19,12 @@ def parse_script(scanner, options = {}) assert_scan!(scanner, /=|&=|!=/) options = DEFAULT_SCRIPT_OPTIONS.merge(options) - code = scan_code(scanner) - return syntax_error("There's no Ruby code for = to evaluate.") if code.empty? + code, with_comment = scan_code(scanner, comment_check: true) + return syntax_error("There's no Ruby code for = to evaluate.") if code.empty? && !with_comment unless has_block? return [:dynamic, code] if options[:disable_escape] return escape_html([:dynamic, code], options[:force_escape]) end @@ -46,40 +46,47 @@ if scanner.scan(/#/) with_indented { skip_lines } return [:multi] end - ast = [:code] - ast << scan_code(scanner) - return ast unless has_block? + ast = [:multi, [:code, scan_code(scanner)]] + unless has_block? + ast << [:code, 'end'] if @current_indent > next_indent + return ast + end - ast = [:multi, ast] ast += with_indented { parse_lines } ast << [:code, 'end'] unless same_indent?(next_line) && internal_statement?(next_line) ast end private - def scan_code(scanner) + def scan_code(scanner, comment_check: false) code = '' loop do code += (scanner.scan(/.+/) || '').strip break unless code =~ /,\Z/ @current_lineno += 1 scanner = StringScanner.new(current_line) code += ' ' end - remove_comment(code) + remove_comment(code, comment_check: comment_check) end - def remove_comment(code) + def remove_comment(code, comment_check: false) result = '' + with_comment = false Ripper.lex(code).each do |(row, col), type, str| - next if type == :on_comment + if type == :on_comment + with_comment = true + next + end result += str end + + return [result, with_comment] if comment_check result end def has_block? next_indent == @current_indent + 1