Class: Mdlint::Parser::State

Inherits:
Object
  • Object
show all
Defined in:
lib/mdlint/parser/state.rb,
sig/internal.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ State

Returns a new instance of State.

Parameters:

  • src (Object)


9
10
11
12
13
14
15
16
17
18
# File 'lib/mdlint/parser/state.rb', line 9

def initialize(src)
  @src = src
  @raw_lines = src.split("\n", -1)
  @lines = @raw_lines.map { |line| expand_tabs(line) }
  @line_offsets = build_line_offsets
  @line = 0
  @pos = 0
  @tokens = []
  @level = 0
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



7
8
9
# File 'lib/mdlint/parser/state.rb', line 7

def level
  @level
end

#lineObject

Returns the value of attribute line.



7
8
9
# File 'lib/mdlint/parser/state.rb', line 7

def line
  @line
end

#line_offsetsObject (readonly)

Returns the value of attribute line_offsets.



6
7
8
# File 'lib/mdlint/parser/state.rb', line 6

def line_offsets
  @line_offsets
end

#linesObject (readonly)

Returns the value of attribute lines.



6
7
8
# File 'lib/mdlint/parser/state.rb', line 6

def lines
  @lines
end

#posObject

Returns the value of attribute pos.



7
8
9
# File 'lib/mdlint/parser/state.rb', line 7

def pos
  @pos
end

#raw_linesObject (readonly)

Returns the value of attribute raw_lines.

Returns:

  • (Object)


6
7
8
# File 'lib/mdlint/parser/state.rb', line 6

def raw_lines
  @raw_lines
end

#srcObject (readonly)

Returns the value of attribute src.



6
7
8
# File 'lib/mdlint/parser/state.rb', line 6

def src
  @src
end

#tokensObject

Returns the value of attribute tokens.

Returns:

  • (Object)


7
8
9
# File 'lib/mdlint/parser/state.rb', line 7

def tokens
  @tokens
end

Instance Method Details

#blank_line?(line_num = @line) ⇒ Boolean

Parameters:

  • line_num (Object) (defaults to: @line)

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/mdlint/parser/state.rb', line 42

def blank_line?(line_num = @line)
  return true if line_num >= @lines.length

  @lines[line_num].match?(/\A\s*\z/)
end

#build_line_offsetsObject

Returns:

  • (Object)


72
73
74
75
76
77
78
# File 'lib/mdlint/parser/state.rb', line 72

def build_line_offsets
  offsets = [0]
  @lines.each do |line|
    offsets << offsets.last + line.length + 1
  end
  offsets
end

#current_lineObject

Returns:

  • (Object)


24
25
26
# File 'lib/mdlint/parser/state.rb', line 24

def current_line
  @lines[@line]
end

#eof?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mdlint/parser/state.rb', line 20

def eof?
  @line >= @lines.length
end

#expand_tabs(line) ⇒ String

Parameters:

  • line (Object)

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mdlint/parser/state.rb', line 58

def expand_tabs(line)
  column = 0
  line.each_char.with_object(+'') do |character, expanded|
    if character == "\t"
      spaces = 4 - (column % 4)
      expanded << (" " * spaces)
      column += spaces
    else
      expanded << character
      column += 1
    end
  end
end

#next_lineObject

Returns:

  • (Object)


32
33
34
# File 'lib/mdlint/parser/state.rb', line 32

def next_line
  @line += 1
end

#peek_line(offset = 1) ⇒ Object

Parameters:

  • offset (Object) (defaults to: 1)

Returns:

  • (Object)


52
53
54
# File 'lib/mdlint/parser/state.rb', line 52

def peek_line(offset = 1)
  @lines[@line + offset]
end

#raw_lineObject

Returns:

  • (Object)


28
29
30
# File 'lib/mdlint/parser/state.rb', line 28

def raw_line
  @raw_lines[@line]
end

#remaining_linesObject

Returns:

  • (Object)


48
49
50
# File 'lib/mdlint/parser/state.rb', line 48

def remaining_lines
  @lines[@line..]
end

#skip_blank_linesObject

Returns:

  • (Object)


36
37
38
39
40
# File 'lib/mdlint/parser/state.rb', line 36

def skip_blank_lines
  while !eof? && blank_line?(@line)
    @line += 1
  end
end