class IniParse::Parser
def self.parse_types
Array
==== Returns
Returns the line types.
def self.parse_types @@parse_types ||= [] end
def self.parse_types=(types)
types
==== Parameters
classes.
Sets the line types. Handy if you want to add your own custom Line
def self.parse_types=(types) parse_types.replace(types) end
def initialize(source)
source
==== Parameters
Creates a new Parser instance for parsing string +source+.
def initialize(source) @source = source.dup end
def parse
IniParse::Document
==== Returns
Parses the source string and returns the resulting data structure.
def parse IniParse::Generator.gen do |generator| @source.split("\n", -1).each do |line| generator.send(*Parser.parse_line(line)) end end end
def parse_line(line)
IniParse::ParseError: If the line could not be parsed.
==== Raises
Generator instance can add the line to the Document.
comment, and indent, then returns the appropriate tuple so that the
Takes a raw line from an INI document, striping out any inline
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
def strip_comment(line, opts)
Strips in inline comment from a line (or value), removes trailing
def strip_comment(line, opts) if m = /^(^)(?:(;|\#)(\s*)(.*))$$/.match(line) || m = /^(.*?)(?:\s+(;|\#)(\s*)(.*))$/.match(line) # Comment lines. opts[:comment] = m[4].rstrip opts[:comment_prefix] = m[3] opts[:comment_sep] = m[2] # Remove the line content (since an option value may contain a # semi-colon) _then_ get the index of the comment separator. opts[:comment_offset] = line[(m[1].length..-1)].index(m[2]) + m[1].length line = m[1] else line = line.chomp end [line, opts] end
def strip_indent(line, opts)
Removes any leading whitespace from a line, and adds it to the options
def strip_indent(line, opts) if m = /^(\s+).*$/.match(line) line.lstrip! opts[:indent] = m[1] end [line, opts] end