class DotOpts::Parser
Constants
- RE_ARGUMENT
Regular expression to match arguments.
- RE_BLANK
Regular expression to match blank strings.
- RE_COMMAND
Regular expression to match command headers.
- RE_ENVIRONMENT
Regular expression to match environment setting.
- RE_PROFILE
Regular expression to match profile headers.
Attributes
commands[R]
text[R]
The configuration document text. [String]
Public Class Methods
new(text)
click to toggle source
Initialize new instance.
@param [#to_s] text
# File lib/dotopts/parser.rb, line 35 def initialize(text) @text = text.to_s # @commands = [] # Holds the current commands being parsed. @_commands = [] @_profiles = [] end
parse(text)
click to toggle source
Convenience constructor for ‘new(text).parse`.
@return [Parser]
# File lib/dotopts/parser.rb, line 25 def self.parse(text) parser = new(text) parser.parse parser.commands end
Public Instance Methods
parse()
click to toggle source
Parse the configuration text.
# File lib/dotopts/parser.rb, line 55 def parse lines = @text.lines.to_a remove_blanks(lines) # put initial non-profiled settings last #if lines.first !~ RE_PROFILE # index = lines.index{ |line| line =~ RE_PROFILE } # if index # lines = lines[index..-1] + ['[]'] + lines[0...index] # else # #lines = ['[]'] + lines # end #end parse_profiles(lines) end
parse_command(lines)
click to toggle source
Parse lines from command onward until another profile or end of document is reached.
@return [void]
# File lib/dotopts/parser.rb, line 97 def parse_command(lines) previous = nil while line = lines.first case line when RE_BLANK when RE_COMMAND if previous != :command @commands.concat @_commands @_commands = [] end if @_profiles.empty? @_commands << Command.new(line.strip, :profile=>nil) else @_profiles.each do |profile| @_commands << Command.new(line.strip, :profile=>profile) end end previous = :command when RE_ARGUMENT, RE_ENVIRONMENT if @_commands.empty? raise SyntaxError, "no command before arguments\n@ #{line}" end @_commands.each{ |c| c << line } previous = :argument when RE_PROFILE @commands.concat @_commands @_commands = [] @_profiles = [] return end lines.shift end @commands.concat @_commands end
parse_profiles(lines)
click to toggle source
# File lib/dotopts/parser.rb, line 76 def parse_profiles(lines) @_profiles = [] until lines.empty? line = lines.first.rstrip case line when RE_BLANK lines.shift when RE_PROFILE @_profiles << $1 lines.shift else #@_commands = [] parse_command(lines) end end end
remove_blanks(lines)
click to toggle source
Remove intialize blank lines for an array of strings.
@param [Array<String>] lines
@return [Array<String>]
# File lib/dotopts/parser.rb, line 137 def remove_blanks(lines) lines.shift while RE_BLANK =~ lines.first end