Class: ParsingParcels

Inherits:
Object show all
Defined in:
lib/ceedling/parsing_parcels.rb

Overview

This is a collection of parsing aids to be used in other modules

Instance Method Summary collapse

Instance Method Details

#code_lines(input) {|line| ... } ⇒ Object

This parser accepts a collection of lines which it will sweep through and tidy, giving the purified lines to the block (one line at a time) for further analysis. It analyzes a single line at a time, which is far more memory efficient and faster for large files. However, this requires it to also handle backslash line continuations as a single line at this point.

Parameters:

  • input (IO, File, String)

    The input source to parse line by line

Yields:

  • (line)

    Gives each cleaned line to the block

Yield Parameters:

  • line (String)

    The cleaned code line



20
21
22
# File 'lib/ceedling/parsing_parcels.rb', line 20

def code_lines(input)
  code_lines_with_num(input) { |line, _line_num| yield(line) }
end

#code_lines_with_num(input) {|line, line_num| ... } ⇒ Object

This parser accepts a collection of lines which it will sweep through and tidy, giving the purified lines to the block (one line at a time) for further analysis along with the line number. It analyzes a single line at a time, which is far more memory efficient and faster for large files. However, this requires it to also handle backslash line continuations as a single line at this point.

Parameters:

  • input (IO, File, String)

    The input source to parse line by line

Yields:

  • (line, line_num)

    Gives each cleaned line and its line number to the block

Yield Parameters:

  • line (String)

    The cleaned code line

  • line_num (Integer)

    The line number (1-indexed) where this line appears in the input. For continuation lines (lines ending with backslash), the line number of the first line in the continuation sequence is provided.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ceedling/parsing_parcels.rb', line 35

def code_lines_with_num(input)
  comment_block = false
  full_line = ''
  line_num = 0
  continuation_start_line = 0
  
  input.each_line do |line|
    line_num += 1
    m = line.clean_encoding.match /(.*)\\\s*$/
    if (!m.nil?)
      full_line += m[1]
      continuation_start_line = line_num if full_line == m[1]
    elsif full_line.empty?
      _line, comment_block = clean_code_line( line, comment_block )
      yield( _line, line_num )
    else
      _line, comment_block = clean_code_line( full_line + line, comment_block )
      yield( _line, continuation_start_line )
      full_line = ''
      continuation_start_line = 0
    end
  end    
end