Class: HamlParser::IndentTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/haml_parser/indent_tracker.rb

Defined Under Namespace

Classes: HardTabNotAllowed, InconsistentIndent, IndentMismatch

Instance Method Summary collapse

Constructor Details

#initialize(on_enter: nil, on_leave: nil) ⇒ IndentTracker

Returns a new instance of IndentTracker.



31
32
33
34
35
36
# File 'lib/haml_parser/indent_tracker.rb', line 31

def initialize(on_enter: nil, on_leave: nil)
  @indent_levels = [0]
  @on_enter = on_enter || lambda { |_level, _text| }
  @on_leave = on_leave || lambda { |_level, _text| }
  @comment_level = nil
end

Instance Method Details

#check_indent_level!(lineno) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/haml_parser/indent_tracker.rb', line 68

def check_indent_level!(lineno)
  if @indent_levels.size >= 3
    previous_size = @indent_levels[-2] - @indent_levels[-3]
    current_size = @indent_levels[-1] - @indent_levels[-2]
    if previous_size != current_size
      raise InconsistentIndent.new(previous_size, current_size, lineno)
    end
  end
end

#current_levelObject



60
61
62
# File 'lib/haml_parser/indent_tracker.rb', line 60

def current_level
  @indent_levels.last
end

#enter_comment!Object



64
65
66
# File 'lib/haml_parser/indent_tracker.rb', line 64

def enter_comment!
  @comment_level = @indent_levels[-2]
end

#finishObject



56
57
58
# File 'lib/haml_parser/indent_tracker.rb', line 56

def finish
  indent_leave(0, '', -1)
end

#process(line, lineno) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/haml_parser/indent_tracker.rb', line 38

def process(line, lineno)
  if line =~ /\A\t/
    raise HardTabNotAllowed.new(lineno)
  end
  indent, text = split(line)
  indent_level = indent.size

  unless text.empty?
    track(indent_level, text, lineno)
  end
  [text, indent]
end

#split(line) ⇒ Object



51
52
53
54
# File 'lib/haml_parser/indent_tracker.rb', line 51

def split(line)
  m = line.match(/\A( *)(.*)\z/)
  [m[1], m[2]]
end