Class: Hephaestus::Actions::StripCommentsAction::BackwardStringScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/hephaestus/actions/strip_comments_action.rb

Overview

A tiny, non-stateful backward string scanner somewhat inspired by Ruby’s StringScanner. Ruby’s StringScanner is unable to seek backward on a string.

Class Method Summary collapse

Class Method Details

.beginning_of_line_pos(expr, initial_pos) ⇒ Object



98
99
100
# File 'lib/hephaestus/actions/strip_comments_action.rb', line 98

def beginning_of_line_pos(expr, initial_pos)
  skip_before(expr, initial_pos) { |char| char == "\n" }
end

.skip_before(expr, initial_pos, &block) ⇒ Object



102
103
104
# File 'lib/hephaestus/actions/strip_comments_action.rb', line 102

def skip_before(expr, initial_pos, &block)
  skip_until(expr, initial_pos, -1, &block)
end

.skip_until(expr, initial_pos, lookup_inc = 0) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hephaestus/actions/strip_comments_action.rb', line 106

def skip_until(expr, initial_pos, lookup_inc = 0)
  pos = initial_pos

  loop do
    break if pos.zero?

    char = expr.source_buffer.source[pos + lookup_inc]
    break if yield(char)

    pos -= 1
  end

  pos
end