Class: MaRuKu::In::Markdown::SpanLevelParser::CharSourceStrscan

Inherits:
Object
  • Object
show all
Defined in:
lib/maruku/input/charsource.rb,
lib/maruku/input/charsource.rb

Overview

a wrapper around StringScanner

Instance Method Summary collapse

Constructor Details

#initialize(s, parent = nil) ⇒ CharSourceStrscan

Returns a new instance of CharSourceStrscan.



139
140
141
142
# File 'lib/maruku/input/charsource.rb', line 139

def initialize(s, parent=nil)
  @scanner = StringScanner.new(s)
  @size = s.size
end

Instance Method Details

#consume_whitespaceObject



193
194
195
# File 'lib/maruku/input/charsource.rb', line 193

def consume_whitespace
  @scanner.skip(/\s+/)
end

#cur_charObject

Return current char as a String (or nil).



145
146
147
# File 'lib/maruku/input/charsource.rb', line 145

def cur_char
  @scanner.peek(1)[0]
end

#cur_chars(n) ⇒ Object

Return the next n chars as a String.



150
151
152
# File 'lib/maruku/input/charsource.rb', line 150

def cur_chars(n)
  @scanner.peek(n)
end

#cur_chars_are(string) ⇒ Object

Returns true if string matches what we’re pointing to



180
181
182
# File 'lib/maruku/input/charsource.rb', line 180

def cur_chars_are(string)
  @scanner.peek(string.size) == string
end

#current_remaining_bufferObject

Return the rest of the string



175
176
177
# File 'lib/maruku/input/charsource.rb', line 175

def current_remaining_buffer
  @scanner.rest
end

#describeObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/maruku/input/charsource.rb', line 197

def describe
  len = 75
  num_before = [len/2, @scanner.pos].min
  num_after = [len/2, @scanner.rest_size].min
  num_before_max = @scanner.pos
  num_after_max = @scanner.rest_size

  num_before = [num_before_max, len - num_after].min
  num_after  = [num_after_max, len - num_before].min

  index_start = [@scanner.pos - num_before, 0].max
  index_end   = [@scanner.pos + num_after, @size].min

  size = index_end - index_start

  str = @scanner.string[index_start, size]
  str.gsub!("\n", 'N')
  str.gsub!("\t", 'T')

  if index_end == @size
    str += "EOF"
  end

  pre_s = @scanner.pos - index_start
  pre_s = [pre_s, 0].max
  pre_s2 = [len-pre_s, 0].max
  pre = " " * pre_s

  "-" * len + "\n" +
    str + "\n" +
    "-" * pre_s + "|" + "-" * pre_s2 + "\n" +
    pre + "+--- Byte #{@scanner.pos}\n" +
    "Shown bytes [#{index_start} to #{size}] of #{@size}:\n" +
    @scanner.string.gsub(/^/, ">")
end

#ignore_charObject

Advance the pointer



165
166
167
# File 'lib/maruku/input/charsource.rb', line 165

def ignore_char
  @scanner.getch
end

#ignore_chars(n) ⇒ Object

Advance the pointer by n



170
171
172
# File 'lib/maruku/input/charsource.rb', line 170

def ignore_chars(n)
  n.times { @scanner.getch }
end

#next_charObject

Return the char after current char as a String (or nil).



155
156
157
# File 'lib/maruku/input/charsource.rb', line 155

def next_char
  @scanner.peek(2)[1]
end

#next_matches(r) ⇒ Object

Returns true if Regexp r matches what we’re pointing to



185
186
187
# File 'lib/maruku/input/charsource.rb', line 185

def next_matches(r)
  @scanner.check(r)
end

#read_regexp(r) ⇒ Object



189
190
191
# File 'lib/maruku/input/charsource.rb', line 189

def read_regexp(r)
  r.match(@scanner.scan(r))
end

#shift_charObject

Return a character as a String, advancing the pointer.



160
161
162
# File 'lib/maruku/input/charsource.rb', line 160

def shift_char
  @scanner.getch[0]
end