Class: DotOpts::Parser

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

Constant Summary collapse

RE_PROFILE =

Regular expression to match profile headers.

/^\[(.*)\]/
RE_COMMAND =

Regular expression to match command headers.

/^\w/
RE_ARGUMENT =

Regular expression to match arguments.

/^\s+\S+/
RE_ENVIRONMENT =

Regular expression to match environment setting.

/^\s+\$/
RE_BLANK =

Regular expression to match blank strings.

/^\s*$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Initialize new instance.

Parameters:

  • text (#to_s)


35
36
37
38
39
40
41
42
43
44
# File 'lib/dotopts/parser.rb', line 35

def initialize(text)
  @text = text.to_s

  #
  @commands = []

  # Holds the current commands being parsed.
  @_commands = []
  @_profiles = []
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



47
48
49
# File 'lib/dotopts/parser.rb', line 47

def commands
  @commands
end

#textObject (readonly)

The configuration document text. [String]



50
51
52
# File 'lib/dotopts/parser.rb', line 50

def text
  @text
end

Class Method Details

.parse(text) ⇒ Parser

Convenience constructor for ‘new(text).parse`.

Returns:



25
26
27
28
29
# File 'lib/dotopts/parser.rb', line 25

def self.parse(text)
  parser = new(text)
  parser.parse
  parser.commands
end

Instance Method Details

#parseObject

Parse the configuration text.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ void

This method returns an undefined value.

Parse lines from command onward until another profile or end of document is reached.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Array<String>

Remove intialize blank lines for an array of strings.

Parameters:

  • lines (Array<String>)

Returns:

  • (Array<String>)


137
138
139
# File 'lib/dotopts/parser.rb', line 137

def remove_blanks(lines)
  lines.shift while RE_BLANK =~ lines.first
end