Module: Hamlit::Parsers::Script

Includes:
Concerns::Error, Concerns::Indentable
Included in:
Hamlit::Parser
Defined in:
lib/hamlit/parsers/script.rb

Constant Summary collapse

INTERNAL_STATEMENTS =
%w[else elsif when].freeze
DEFAULT_SCRIPT_OPTIONS =
{ disable_escape: false }.freeze

Instance Method Summary collapse

Methods included from Concerns::Indentable

#count_indent, #count_width, #indent_label, #next_indent, #next_width, #replace_hard_tabs, #reset_indent, #same_indent?, #validate_indentation!, #with_indented

Methods included from Concerns::Error

#assert_scan!, #copmile_error!, #syntax_error, #syntax_error!

Instance Method Details

#parse_script(scanner, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hamlit/parsers/script.rb', line 13

def parse_script(scanner, options = {})
  assert_scan!(scanner, /=|&=|!=|~/)
  options = DEFAULT_SCRIPT_OPTIONS.merge(options)

  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, true, [:dynamic, code]]
  end

  ast = [:haml, :script, code, options]
  ast += with_indented { parse_lines }
  ast << [:code, 'end']
  ast
end

#parse_silent_script(scanner) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hamlit/parsers/script.rb', line 30

def parse_silent_script(scanner)
  assert_scan!(scanner, /-/)
  if scanner.scan(/#/)
    with_indented { skip_lines }
    return [:multi]
  end

  code = scan_code(scanner)
  ast = [:multi, [:code, code]]
  unless has_block?
    if internal_statement?(code) && !statement_continuing?
      ast << [:code, 'end']
    end
    return ast
  end

  ast += with_indented { parse_lines }
  unless statement_continuing?
    ast << [:code, 'end']
  end
  ast
end