Class: SlimLint::Parser

Inherits:
Slim::Parser
  • Object
show all
Defined in:
lib/slim_lint/parser.rb

Overview

This version of the Slim::Parser makes the smallest changes it can to preserve newline informatino through the parse. This helps us keep better track of line numbers.

Constant Summary collapse

BLANK_LINE_RE =
/\A\s*\Z/

Instance Method Summary collapse

Instance Method Details

#append(sexp) ⇒ Object



21
22
23
# File 'lib/slim_lint/parser.rb', line 21

def append(sexp)
  @stacks.last << sexp
end

#call(str) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/slim_lint/parser.rb', line 10

def call(str)
  reset(str.split(/\r?\n/))
  push create_container(sexp(:multi, start: [1, 1]))

  parse_line while next_line
  result = pop until @stacks.empty?

  reset
  result
end

#next_lineObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/slim_lint/parser.rb', line 61

def next_line
  @prev_line = @orig_line
  if @lines.empty?
    @orig_line = @line = nil
  else
    @orig_line = @lines.shift
    @lineno += 1
    @line = @orig_line.dup
  end
end

#popObject



29
30
31
32
# File 'lib/slim_lint/parser.rb', line 29

def pop
  @stacks.last.finish = pos
  @stacks.pop
end

#push(sexp) ⇒ Object



25
26
27
# File 'lib/slim_lint/parser.rb', line 25

def push(sexp)
  @stacks << sexp
end

#reset(lines = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slim_lint/parser.rb', line 34

def reset(lines = nil)
  # Since you can indent however you like in Slim, we need to keep a list
  # of how deeply indented you are. For instance, in a template like this:
  #
  #   doctype       # 0 spaces
  #   html          # 0 spaces
  #    head         # 1 space
  #       title     # 4 spaces
  #
  # indents will then contain [0, 1, 4] (when it's processing the last line.)
  #
  # We uses this information to figure out how many steps we must "jump"
  # out when we see an de-indented line.
  @indents = []

  # Whenever we want to output something, we'll *always* output it to the
  # last stack in this array. So when there's a line that expects
  # indentation, we simply push a new stack onto this array. When it
  # processes the next line, the content will then be outputted into that
  # stack.
  @stacks = []

  @lineno = 0
  @lines = lines
  @prev_line = @line = @orig_line = nil
end