Method: Pixelart::Image.parse_pixels_strict

Defined in:
lib/pixelart/image.rb

.parse_pixels_strict(rx, txt) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/pixelart/image.rb', line 377

def self.parse_pixels_strict( rx, txt )
    ## must match tokens in regex (rx)  e.g. /0|1|2|3../ or /A|B|C... etc./
    pixels = []

    txt.each_line do |line|
       line = line.strip
       next if line.start_with?( '#' ) || line.empty?   ## note: allow comments and empty lines

       scan = StringScanner.new( line )
       tokens = []
       loop do
         # puts "  pos: #{scan.pos} - size: #{scan.rest.size} - #{scan.rest}"  
         token = scan.scan( rx )
         if token.nil?
          ## todo/fix: raise an exception here
           puts "!! ERROR - parse error; expected match of #{rx.to_s} but got: #{scan.rest}"
           exit 1
         end      
         tokens << token
         
         scan.skip( /[ \t]+/ )    
         break if scan.eos?
       end
       pixels << tokens
    end
    pixels
end