Class: MaRuKu::In::Markdown::SpanLevelParser::CharSourceManual

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

Overview

a string scanner coded by me

Instance Method Summary collapse

Constructor Details

#initialize(s, parent = nil) ⇒ CharSourceManual

Returns a new instance of CharSourceManual.



23
24
25
26
27
28
# 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

Instance Method Details

#consume_whitespaceObject



84
85
86
87
88
89
# 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_charObject

Return current char as a String (or nil).



31
32
33
# File 'lib/maruku/input/charsource.rb', line 31

def cur_char
  cur_chars(1)
end

#cur_chars(n) ⇒ Object

Return the next n chars as a String.



36
37
38
39
# 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) ⇒ Object



65
66
67
# File 'lib/maruku/input/charsource.rb', line 65

def cur_chars_are(string)
  cur_chars(string.size) == string
end

#current_remaining_bufferObject



61
62
63
# File 'lib/maruku/input/charsource.rb', line 61

def current_remaining_buffer
  @buffer[@buffer_index, @buffer.size - @buffer_index]
end

#describeObject



91
92
93
94
95
96
97
# 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) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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_charObject



53
54
55
# File 'lib/maruku/input/charsource.rb', line 53

def ignore_char
  @buffer_index += 1
end

#ignore_chars(n) ⇒ Object



57
58
59
# File 'lib/maruku/input/charsource.rb', line 57

def ignore_chars(n)
  @buffer_index += n
end

#next_charObject

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



42
43
44
45
# 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) ⇒ Object



69
70
71
72
# File 'lib/maruku/input/charsource.rb', line 69

def next_matches(r)
  r2 = /^.{#{@buffer_index}}#{r}/m
  r2.match @buffer
end

#read_regexp(r) ⇒ Object



74
75
76
77
78
79
80
81
82
# 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_charObject



47
48
49
50
51
# File 'lib/maruku/input/charsource.rb', line 47

def shift_char
  c = cur_char
  @buffer_index += 1
  c
end