Class: Values::Parser
- Inherits:
-
Object
show all
- Defined in:
- lib/csv11.rb
Defined Under Namespace
Classes: Quoted, Token, TripleQuoted, Unquoted
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
38
39
|
# File 'lib/csv11.rb', line 38
def initialize
end
|
Instance Method Details
#match_quoted? ⇒ Boolean
todo/fix: use @buffer.match - “ (next letter MUST Not be ”!! e.g.“” not valid!!!
114
115
116
117
|
# File 'lib/csv11.rb', line 114
def match_quoted?
@buffer.peek(1) == %{"} || @buffer.peek(1) == %{'} end
|
#match_triple_quoted? ⇒ Boolean
107
108
109
110
111
|
# File 'lib/csv11.rb', line 107
def match_triple_quoted?
@buffer.peek(3) == %{"""} || @buffer.peek(3) == %{'''} end
|
#parse(str) ⇒ Object
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/csv11.rb', line 234
def parse(str)
puts ""
puts "**** parse >>#{str}<<"
@values = []
@buffer = StringScanner.new(str)
parse_line
@values
end
|
#parse_line ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/csv11.rb', line 50
def parse_line
loop do
t = parse_token
if t.nil?
puts "!! format error: expected token with value, returns nil - rest is >>#{@buffer.rest}<<"
break
end
if @buffer.peek(1) == ','
@buffer.getch puts "adding value >>#{t.value}<<"
@values << t.value
elsif @buffer.eos?
puts "adding (last) value >>#{t.value}<<"
@values << t.value
break
elsif @buffer.peek(1) == ':'
@buffer.getch
if @values.empty?
value = @buffer.rest
value = value.strip
puts "adding (single-line) first named value >>#{t.value}<< : >>#{value}<<"
@values << [t.value,value]
break
else
t2 = parse_token( match_name: false )
puts "adding named value >>#{t.value}<< : >>#{t2.value}<<"
@values << [t.value,t2.value]
if @buffer.peek(1) == ','
@buffer.getch elsif @buffer.eos?
break
else
puts "!! format error: expected comma (,) or EOS - rest is >>#{@buffer.rest}<<"
break
end
end
else
puts "!! format error: expected comma (,) or colon (:) or EOS - rest is >>#{@buffer.rest}<<"
break
end
end
end
|
#parse_quoted ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/csv11.rb', line 152
def parse_quoted
token = nil
if @buffer.peek(1) == '"' @buffer.getch value = @buffer.scan_until( /(?=")/)
@buffer.getch @buffer.skip( /[ \t]*/ ) puts %{quoted "..." value >>#{value}<<}
token = Quoted.new( value )
elsif @buffer.peek(1) == "'" @buffer.getch value = @buffer.scan_until( /(?=')/)
@buffer.getch @buffer.skip( /[ \t]*/ ) puts %{quoted '...' value >>#{value}<<}
token = Quoted.new( value )
else
end
token
end
|
#parse_token(match_name: true) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/csv11.rb', line 217
def parse_token( match_name: true )
@buffer.skip( /[ \t]*/ )
token = nil
if match_triple_quoted? token = parse_triple_quoted
elsif match_quoted? token = parse_quoted
else
token = parse_unquoted( match_name: match_name )
end
token
end
|
#parse_triple_quoted ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/csv11.rb', line 120
def parse_triple_quoted
token = nil
if @buffer.peek(3) == %{"""} @buffer.getch @buffer.getch
@buffer.getch
value = @buffer.scan_until( /(?=""")/)
@buffer.getch @buffer.getch
@buffer.getch
@buffer.skip( /[ \t]*/ ) puts %{quoted """...""" value >>#{value}<<}
token = TripleQuoted.new( value )
elsif @buffer.peek(3) == %{'''} @buffer.getch @buffer.getch
@buffer.getch
value = @buffer.scan_until( /(?=''')/)
@buffer.getch @buffer.getch
@buffer.getch
@buffer.skip( /[ \t]*/ ) puts %{quoted '''...''' value >>#{value}<<}
token = TripleQuoted.new( value )
else
end
token
end
|
#parse_unquoted(match_name: true) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/csv11.rb', line 177
def parse_unquoted( match_name: true )
puts "collect unquoted token (match_name? => #{match_name}) - rest: >>#{@buffer.rest}<<"
if match_name
value = @buffer.scan_until( /(?=[,:]|$)/)
reserved_names = %w{ https http urn mailto file }
name_regex = %r{^[a-zA-Z0-9._-]+$}
if @buffer.peek(1) == ':'
if reserved_names.include?( value )
value << @buffer.scan_until( /(?=,|$)/)
elsif name_regex.match( value ).nil?
value << @buffer.scan_until( /(?=,|$)/)
else
end
end
else value = @buffer.scan_until( /(?=[,]|$)/)
end
value = value.rstrip puts "value >>#{value}<<"
token = Unquoted.new( value )
end
|