Method: OpenC3::ConfigParser#parse_loop

Defined in:
lib/openc3/config/config_parser.rb,
ext/openc3/ext/config_parser/config_parser.c

#parse_loop(io, yield_non_keyword_lines, remove_quotes, size, rx) ⇒ Object

Iterates over each line of the io object and yields the keyword and parameters



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/openc3/config/config_parser.rb', line 466

def parse_loop(io, yield_non_keyword_lines, remove_quotes, size, rx)
  string_concat = false
  @line_number = 0
  @keyword = nil
  @parameters = []
  @line = ''
  errors = []

  while true
    @line_number += 1

    if @@progress_callback && ((@line_number % 10) == 0)
      @@progress_callback.call(io.pos / size) if size > 0.0
    end

    begin
      line = io.readline
    rescue Exception
      break
    end

    line.strip!
    # Ensure the line length is not 0
    next if line.length == 0

    if string_concat
      # Skip comment lines after a string concatenation
      if (line[0] == '#')
        next
      end
      # Remove the opening quote if we're continuing the line
      line = line[1..-1]
    end

    # Check for string continuation
    case line[-1]
    when '+', '\\' # String concatenation
      newline = line[-1] == '+'
      # Trim off the concat character plus any spaces, e.g. "line"        trim = line[0..-2].strip()
      # Now trim off the last quote so it will flow into the next line
      @line += trim[0..-2]
      @line += "\n" if newline
      string_concat = true
      next
    when '&' # Line continuation
      @line += line[0..-2]
      next
    else
      @line += line
    end
    string_concat = false

    data = @line.scan(rx)
    first_item = ''
    if data.length > 0
      first_item += data[0]
    end

    if (first_item.length == 0) || (first_item[0] == '#')
      @keyword = nil
    else
      @keyword = first_item.upcase
    end
    @parameters = []

    # Ignore lines without keywords: comments and blank lines
    if @keyword.nil?
      if yield_non_keyword_lines
        begin
          yield(@keyword, @parameters)
        rescue => error
          errors << error
        end
      end
      @line = ''
      next
    end

    length = data.length
    if length > 1
      (1..(length - 1)).each do |index|
        string = data[index]

        # Don't process trailing comments such as:
        # KEYWORD PARAM #This is a comment
        # But still process Ruby string interpolations such as:
        # KEYWORD PARAM #{var}
        if (string.length > 0) && (string[0] == '#')
          if !((string.length > 1) && (string[1] == '{'))
            break
          end
        end

        if remove_quotes
          @parameters << string.remove_quotes
        else
          @parameters << string
        end
      end
    end

    begin
      yield(@keyword, @parameters)
    rescue => error
      errors << error
    end
    @line = ''
  end

  parse_errors(errors)

  @@progress_callback.call(1.0) if @@progress_callback

  return nil
end