Module: StreamParser
- Defined in:
- lib/stream_parser.rb,
lib/stream_parser/version.rb
Defined Under Namespace
Modules: ClassMethods, HTML
Classes: SyntaxError
Constant Summary
collapse
- VERSION =
'0.4'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#match ⇒ Object
Returns the value of attribute match.
18
19
20
|
# File 'lib/stream_parser.rb', line 18
def match
@match
end
|
Class Method Details
.included(base) ⇒ Object
6
7
8
|
# File 'lib/stream_parser.rb', line 6
def self.included(base)
base.extend ClassMethods
end
|
Instance Method Details
#current_line ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/stream_parser.rb', line 100
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
|
#cursor ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/stream_parser.rb', line 110
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
29
30
31
|
# File 'lib/stream_parser.rb', line 29
def eos?
@index >= (@source.size - 0)
end
|
#forward(by = 1) ⇒ Object
65
66
67
|
# File 'lib/stream_parser.rb', line 65
def forward(by=1)
@index += by
end
|
#gobble(r) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/stream_parser.rb', line 50
def gobble(r)
m = @source.match(r, @index)
if m&.begin(0) == @index
scan_until(r)
end
end
|
#initialize(source) ⇒ Object
20
21
22
23
|
# File 'lib/stream_parser.rb', line 20
def initialize(source)
@source = source
seek(0)
end
|
#next?(r) ⇒ Boolean
83
84
85
|
# File 'lib/stream_parser.rb', line 83
def next?(r)
@source.match(r, @index)&.begin(0) == @index
end
|
#next_char ⇒ Object
75
76
77
|
# File 'lib/stream_parser.rb', line 75
def next_char
@source[@index]
end
|
#next_word ⇒ Object
95
96
97
98
|
# File 'lib/stream_parser.rb', line 95
def next_word
nw = @source.match(/\s*(\S+)/, @index)
nw.nil? ? nil : nw[1]
end
|
#peek(n = 1) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/stream_parser.rb', line 87
def peek(n=1)
if n.is_a?(Regexp)
@source.match(n, @index)
else
@source.slice(@index, n)
end
end
|
#pre_match ⇒ Object
57
58
59
|
# File 'lib/stream_parser.rb', line 57
def pre_match
@source[@old_index...(@index-(@match&.size || 0))]
end
|
#prev_char ⇒ Object
79
80
81
|
# File 'lib/stream_parser.rb', line 79
def prev_char
@source[@index-1]
end
|
#quoted_value(quote_char = '"', escape_chars = ["\\"]) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/stream_parser.rb', line 128
def quoted_value(quote_char = '"', escape_chars = ["\\"])
ret_value = ""
while scan_until(/(#{quote_char}|\Z)/)
if match != quote_char
raise StreamParser::SyntaxError.new("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
61
62
63
|
# File 'lib/stream_parser.rb', line 61
def rewind(by=1)
@index -= by
end
|
#scan_until(r) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/stream_parser.rb', line 33
def scan_until(r)
r = Regexp.new(Regexp.escape(r)) if r.is_a?(String)
match = @source.match(r, @index)
index = match&.begin(0)
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
69
70
71
72
73
|
# File 'lib/stream_parser.rb', line 69
def seek(pos)
@old_index = nil
@match = nil
@index = pos
end
|