Module: StreamParser

Defined in:
lib/stream_parser.rb,
lib/stream_parser/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'0.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#matchObject

Returns the value of attribute match.



15
16
17
# File 'lib/stream_parser.rb', line 15

def match
  @match
end

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/stream_parser.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#current_lineObject



78
79
80
81
82
83
84
85
86
# File 'lib/stream_parser.rb', line 78

def current_line
  start = @source.rindex(/(\n|\A)/, @index-(@match&.length||0)) || 0
  start += 1 if @source[start] == "\n"

  uptop = @source.index(/(\n|\z)/, @index)
  uptop -= 1 if @source[uptop] == "\n"

  @source[start..uptop]
end

#cursorObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stream_parser.rb', line 88

def cursor
  start = @source.rindex(/(\n|\A)/, @index-(@match&.length||0)) || 0
  start += 1 if @source[start] == "\n"

  uptop = @source.index(/(\n|\z)/, @index)
  uptop -= 1 if @source[uptop] == "\n"

  lineno = @source[0..start].count("\n") + 1

  output =  "#{lineno.to_s.rjust(4)}: #{@source[start..uptop]}\n     "
  output << if @match
    " #{'-'* (@index-(@match.length)-start)}#{'^'*(@match.length)}"
  else
    "^"
  end
  output
end

#eos?Boolean

def parse

# implmentation

end

Returns:

  • (Boolean)


26
27
28
# File 'lib/stream_parser.rb', line 26

def eos?
  @index >= (@source.size - 0)
end

#forward(by = 1) ⇒ Object



55
56
57
# File 'lib/stream_parser.rb', line 55

def forward(by=1)
  @index += by
end

#initialize(source) ⇒ Object



17
18
19
20
# File 'lib/stream_parser.rb', line 17

def initialize(source)
  @source = source
  seek(0)
end

#next_charObject



65
66
67
# File 'lib/stream_parser.rb', line 65

def next_char
  @source[@index]
end

#next_wordObject



73
74
75
76
# File 'lib/stream_parser.rb', line 73

def next_word
  nw = @source.match(/\s*(\S+)/, @index)
  nw.nil? ? nil : nw[1]
end

#pre_matchObject



47
48
49
# File 'lib/stream_parser.rb', line 47

def pre_match
  @source[@old_index...(@index-(@match&.size || 0))]
end

#prev_charObject



69
70
71
# File 'lib/stream_parser.rb', line 69

def prev_char
  @source[@index-1]
end

#quoted_value(quote_char = '"', escape_chars = ["\\"]) ⇒ Object



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

def quoted_value(quote_char = '"', escape_chars = ["\\"])
  ret_value = ""
  while scan_until(/(#{quote_char}|\Z)/)
    if match != quote_char
      raise Net::HTTPHeaderSyntaxError.new("Invalid Set-Cookie header format: unbalanced quotes (#{quote_char})")
    elsif !escape_chars.include?(pre_match[-1])
      ret_value << pre_match
      return ret_value
    else
      ret_value << pre_match[0...-1] << match
    end
  end
end

#rewind(by = 1) ⇒ Object



51
52
53
# File 'lib/stream_parser.rb', line 51

def rewind(by=1)
  @index -= by
end

#scan_until(r) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stream_parser.rb', line 30

def scan_until(r)
  r = Regexp.new(Regexp.escape(r)) if r.is_a?(String)
  index = @source.index(r, @index)
  match = @source.match(r, @index)
  
  if match
    @match = match.to_s
    @old_index = @index
    @index = index + @match.size
  else
    @match = nil
    @old_index = @index
    @index = @source.size
  end
  match
end

#seek(pos) ⇒ Object



59
60
61
62
63
# File 'lib/stream_parser.rb', line 59

def seek(pos)
  @old_index = nil
  @match = nil
  @index = pos
end