Class: IniParse::Parser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Parser

Creates a new Parser instance for parsing string source.

Parameters

source<String>

The source string.



31
32
33
# File 'lib/iniparse/parser.rb', line 31

def initialize(source)
  @source = source.dup
end

Class Method Details

.parse_line(line) ⇒ Object

Takes a raw line from an INI document, striping out any inline comment, and indent, then returns the appropriate tuple so that the Generator instance can add the line to the Document.

Raises

IniParse::ParseError: If the line could not be parsed.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iniparse/parser.rb', line 56

def parse_line(line)
  sanitized, opts = strip_indent(*strip_comment(line, {}))

  parsed = nil
  @@parse_types.each do |type|
    break if (parsed = type.parse(sanitized, opts))
  end

  if parsed.nil?
    raise IniParse::ParseError,
      "A line of your INI document could not be parsed to a " \
      "LineType: #{line.inspect}."
  end

  parsed
end

.parse_typesObject

Returns the line types.

Returns

Array



9
10
11
# File 'lib/iniparse/parser.rb', line 9

def self.parse_types
  @@parse_types ||= []
end

.parse_types=(types) ⇒ Object

Sets the line types. Handy if you want to add your own custom Line classes.

Parameters

types<Array>

An array containing Line classes.



19
20
21
# File 'lib/iniparse/parser.rb', line 19

def self.parse_types=(types)
  parse_types.replace(types)
end

Instance Method Details

#parseObject

Parses the source string and returns the resulting data structure.

Returns

IniParse::Document



40
41
42
43
44
45
46
# File 'lib/iniparse/parser.rb', line 40

def parse
  IniParse::Generator.gen do |generator|
    @source.split("\n", -1).each do |line|
      generator.send(*Parser.parse_line(line))
    end
  end
end