Class: Values::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/csv11.rb

Defined Under Namespace

Classes: Quoted, Token, TripleQuoted, Unquoted

Instance Method Summary collapse

Constructor Details

#initializeParser

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!!!

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/csv11.rb', line 114

def match_quoted?
   @buffer.peek(1) == %{"} ||   ## double quote
   @buffer.peek(1) == %{'}      ## single quote
end

#match_triple_quoted?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/csv11.rb', line 107

def match_triple_quoted?
  ## todo/fix: use @buffer.match - """ (next letter MUST Not be "!! e.g.""""" not valid!!!
  @buffer.peek(3) == %{"""} ||  ## double triple quotes
  @buffer.peek(3) == %{'''}     ## single triple quotes
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_lineObject



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   ## consume ','
       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  ## consume ':'

       ## todo/fix:
       ##    do NOT allow names for quoted, triple_quoted for now - why? why not?
       ##   issue a format error: why? why not??

       if @values.empty?
         ### note:special case:
         ##  if first token is a name/key
         ##   consume all the rest!! including commas, colons etc.
         ##    no escape needed for nothing
         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  ## consume ','
         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_quotedObject



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 # nothing found

   if @buffer.peek(1) == '"'   ## double quote
     @buffer.getch  # consume double quote
     value = @buffer.scan_until( /(?=")/)
     @buffer.getch  # consume double quote
     @buffer.skip( /[ \t]*/ )    ## skip trailing WHITESPACE
     puts %{quoted "..." value >>#{value}<<}
     token = Quoted.new( value )
   elsif @buffer.peek(1) == "'"   ## single quote
     @buffer.getch  # consume single quote
     value = @buffer.scan_until( /(?=')/)
     @buffer.getch  # consume single quote
     @buffer.skip( /[ \t]*/ )    ## skip trailing WHITESPACE
     puts %{quoted '...' value >>#{value}<<}
     token = Quoted.new( value )
   else
     ## do nothing; report format error
   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]*/ )    ## skip WHITESPACE

   token = nil  # nothing found

   if match_triple_quoted?    # """...""" or '''...'''
     token = parse_triple_quoted
   elsif match_quoted?        # "..." or '...'
     token = parse_quoted
   else
     token = parse_unquoted( match_name: match_name )
   end
   token
end

#parse_triple_quotedObject



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 # nothing found

   if @buffer.peek(3) == %{"""}   ## double quote
     @buffer.getch  # consume double quote
     @buffer.getch
     @buffer.getch
     value = @buffer.scan_until( /(?=""")/)
     @buffer.getch  # consume double quote
     @buffer.getch
     @buffer.getch
     @buffer.skip( /[ \t]*/ )    ## skip trailing WHITESPACE
     puts %{quoted """...""" value >>#{value}<<}
     token = TripleQuoted.new( value )
   elsif @buffer.peek(3) == %{'''}   ## single quote
     @buffer.getch  # consume single quote
     @buffer.getch
     @buffer.getch
     value = @buffer.scan_until( /(?=''')/)
     @buffer.getch  # consume single quote
     @buffer.getch
     @buffer.getch
     @buffer.skip( /[ \t]*/ )    ## skip trailing WHITESPACE
     puts %{quoted '''...''' value >>#{value}<<}
     token = TripleQuoted.new( value )
   else
     ## do nothing; report format error
   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 )
   ## unquoted value
   puts "collect unquoted token (match_name? => #{match_name})  - rest: >>#{@buffer.rest}<<"

   if match_name
     value = @buffer.scan_until( /(?=[,:]|$)/)

     ## check for reserverd "non-keys" e.g.:
     ###  https: http:
     ###  urn:
     ###  mailto:
     ###  file:
     ###    add some more??
     ##    todo/fix: add ip address e.g. 127.0.0.1: too ??
     reserved_names = %w{ https http urn mailto file }
     name_regex = %r{^[a-zA-Z0-9._-]+$}    ## todo/fix: allow more chars

     if @buffer.peek(1) == ':'
       if reserved_names.include?( value )
          ## continue scan until next comma or eos(end-of-string)!!!
          value << @buffer.scan_until( /(?=,|$)/)
       elsif name_regex.match( value ).nil?
          ## does NOT match name/key pattern
          ## continue scan until next comma or eos(end-of-string)!!!
          value << @buffer.scan_until( /(?=,|$)/)
       else
         ## continue
       end
     end
   else    ## do NOT match name (named values) e.g. do NOT include colon (:)
     value = @buffer.scan_until( /(?=[,]|$)/)
   end

   value = value.rstrip    ## right strip whitespace
   puts "value >>#{value}<<"
   token = Unquoted.new( value )
end