Class: Slither::Parser
- Inherits:
-
Object
- Object
- Slither::Parser
- Defined in:
- lib/slither/parser.rb
Instance Method Summary collapse
-
#initialize(definition, file_io) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parse_by_bytes ⇒ Object
Constructor Details
#initialize(definition, file_io) ⇒ Parser
Returns a new instance of Parser.
3 4 5 6 7 8 |
# File 'lib/slither/parser.rb', line 3 def initialize(definition, file_io) @definition = definition @file = file_io # This may be used in the future for non-linear or repeating sections @mode = :linear end |
Instance Method Details
#parse ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/slither/parser.rb', line 10 def parse parsed = {} @file.each_line do |line| line&.chomp! next if line.empty? @definition.sections.each do |section| if section.match(line) validate_length(line, section) parsed = fill_content(line, section, parsed) end end end @definition.sections.each do |section| unless parsed[section.name] || section.optional raise(Slither::RequiredSectionNotFoundError, "Required section '#{section.name}' was not found.") end end parsed end |
#parse_by_bytes ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/slither/parser.rb', line 35 def parse_by_bytes parsed = {} all_section_lengths = @definition.sections.map(&:length) byte_length = all_section_lengths.max all_section_lengths.each do |bytes| next unless bytes != byte_length raise( Slither::SectionsNotSameLengthError, "All sections must have the same number of bytes for parse by bytes" ) end while (record = @file.read(byte_length)) unless remove_newlines! && byte_length == record.length parsed_line = (record) raise(Slither::LineWrongSizeError, "Line wrong size: No newline at #{byte_length} bytes. #{parsed_line}") end record.force_encoding(@file.external_encoding) @definition.sections.each do |section| parsed = fill_content(record, section, parsed) if section.match(record) end end @definition.sections.each do |section| unless parsed[section.name] || section.optional raise(Slither::RequiredSectionNotFoundError, "Required section '#{section.name}' was not found.") end end parsed end |