class MaRuKu::In::Markdown::SpanLevelParser::CharSourceManual

a string scanner coded by me

CharSource = CharSourceDebug

Public Class Methods

new(s, parent=nil) click to toggle source
# File lib/maruku/input/charsource.rb, line 23
def initialize(s, parent=nil)
  raise "Passed #{s.class}" if not s.kind_of? String
  @buffer = s
  @buffer_index = 0
  @parent = parent
end

Public Instance Methods

consume_whitespace() click to toggle source
# File lib/maruku/input/charsource.rb, line 84
def consume_whitespace
  while c = cur_char
    break unless (c == ' ' || c == "\t")
    ignore_char
  end
end
cur_char() click to toggle source

Return current char as a String (or nil).

# File lib/maruku/input/charsource.rb, line 31
def cur_char
  cur_chars(1)
end
cur_chars(n) click to toggle source

Return the next n chars as a String.

# File lib/maruku/input/charsource.rb, line 36
def cur_chars(n)
  return nil if @buffer_index >= @buffer.size
  @buffer[@buffer_index, n]
end
cur_chars_are(string) click to toggle source
# File lib/maruku/input/charsource.rb, line 65
def cur_chars_are(string)
  cur_chars(string.size) == string
end
current_remaining_buffer() click to toggle source
# File lib/maruku/input/charsource.rb, line 61
def current_remaining_buffer
  @buffer[@buffer_index, @buffer.size - @buffer_index]
end
describe() click to toggle source
# File lib/maruku/input/charsource.rb, line 91
def describe
  s = describe_pos(@buffer, @buffer_index)
  if @parent
    s += "\n\n" + @parent.describe
  end
  s
end
describe_pos(buffer, buffer_index) click to toggle source
# File lib/maruku/input/charsource.rb, line 99
def describe_pos(buffer, buffer_index)
  len = 75
  num_before = [len/2, buffer_index].min
  num_after = [len/2, buffer.size - buffer_index].min
  num_before_max = buffer_index
  num_after_max = buffer.size - buffer_index

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

  index_start = [buffer_index - num_before, 0].max
  index_end   = [buffer_index + num_after, buffer.size].min

  size = index_end - index_start

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

  if index_end == buffer.size
    str += "EOF"
  end

  pre_s = buffer_index - 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 #{buffer_index}\n"+

    "Shown bytes [#{index_start} to #{size}] of #{buffer.size}:\n"+
    buffer.gsub(/^/, ">")
end
ignore_char() click to toggle source
# File lib/maruku/input/charsource.rb, line 53
def ignore_char
  @buffer_index += 1
end
ignore_chars(n) click to toggle source
# File lib/maruku/input/charsource.rb, line 57
def ignore_chars(n)
  @buffer_index += n
end
next_char() click to toggle source

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

# File lib/maruku/input/charsource.rb, line 42
def next_char
  return nil if @buffer_index + 1 >= @buffer.size
  @buffer[@buffer_index + 1, 1]
end
next_matches(r) click to toggle source
# File lib/maruku/input/charsource.rb, line 69
def next_matches(r)
  r2 = /^.{#{@buffer_index}}#{r}/m
  r2.match @buffer
end
read_regexp(r) click to toggle source
# File lib/maruku/input/charsource.rb, line 74
def read_regexp(r)
  r2 = /^#{r}/
  rest = current_remaining_buffer
  m = r2.match(rest)
  if m
    @buffer_index += m.to_s.size
  end
  m
end
shift_char() click to toggle source
# File lib/maruku/input/charsource.rb, line 47
def shift_char
  c = cur_char
  @buffer_index += 1
  c
end