Class: ShellOpts::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



159
160
161
162
# File 'lib/shellopts/parser.rb', line 159

def initialize(tokens)
  @tokens = tokens.dup # Array of token. Consumed by #parse
  @nodes = {}
end

Instance Attribute Details

#commandsObject (readonly)

Commands by UID



157
158
159
# File 'lib/shellopts/parser.rb', line 157

def commands
  @commands
end

#programObject (readonly)

AST root node



154
155
156
# File 'lib/shellopts/parser.rb', line 154

def program
  @program
end

Class Method Details

.parse(tokens) ⇒ Object



300
301
302
# File 'lib/shellopts/parser.rb', line 300

def self.parse(tokens)
  self.new(tokens).parse
end

Instance Method Details

#parseObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/shellopts/parser.rb', line 164

def parse()
  @program = Grammar::Program.parse(@tokens.shift)
  oneline = @tokens.first.lineno == @tokens.last.lineno
  nodes = [@program] # Stack of Nodes. Follows the indentation of the source
  cmds = [@program] # Stack of cmds. Used to keep track of the current command

  while token = @tokens.shift
    # Unwind stack according to indentation
    while token.charno <= nodes.top.token.charno
      node = nodes.pop
      cmds.pop if cmds.top == node
      !nodes.empty? or parse_error(token, "Illegal indent")
    end

    case token.kind
      when :section
        Grammar::Section.parse(nodes.top, token)

      when :option
        # Collect options into option groups if on the same line and not in
        # oneline mode
        options = [token] + @tokens.shift_while { |follow|
          !oneline && follow.kind == :option && follow.lineno == token.lineno
        }
        group = Grammar::OptionGroup.new(cmds.top, token)
        options.each { |option| Grammar::Option.parse(group, option) }
        nodes.push group

      when :command
        parent = nil # Required by #indent
        token.source =~ /^(?:(.*)\.)?([^.]+)$/
        parent_id = $1
        ident = $2.to_sym
        parent_uid = parent_id && parent_id.sub(".", "!.") + "!"

        # Handle dotted command
        if parent_uid
          # Clear stack except for the top-level Program object and then
          # push command objects in the path
          #
          # FIXME: Move to analyzer
#             cmds = cmds[0..0]
#             for ident in parent_uid.split(".").map(&:to_sym)
#               cmds.push cmds.top.commands.find { |c| c.ident == ident } or
#                   parse_error token, "Unknown command: #{ident.sub(/!/, "")}"
#             end
#             parent = cmds.top
          parent = cmds.top
          if !cmds.top.is_a?(Grammar::Program) && token.lineno == cmds.top.token.lineno
            parent = cmds.pop.parent
          end

        # Regular command
        else
          # Don't nest cmds if they are declared on the same line (as it
          # often happens with one-line declarations). Program is special
          # cased as its virtual token is on line 0
          parent = cmds.top
          if !cmds.top.is_a?(Grammar::Program) && token.lineno == cmds.top.token.lineno
            parent = cmds.pop.parent
          end
        end

        command = Grammar::Command.parse(parent, token)
        nodes.push command
        cmds.push command

      when :spec
        spec = Grammar::ArgSpec.parse(cmds.top, token)
        @tokens.shift_while { |token| token.kind == :argument }.each { |token|
          Grammar::Arg.parse(spec, token)
        }

      when :argument
        ; raise # Should never happen

      when :usage
        ; # Do nothing

      when :usage_string
        Grammar::ArgDescr.parse(cmds.top, token)

      when :text
        # Text is only allowed on new lines
        token.lineno > nodes.top.token.lineno

        # Detect indented comment groups (code)
        if nodes.top.is_a?(Grammar::Paragraph)
          code = Grammar::Code.parse(nodes.top.parent, token) # Using parent of paragraph
          @tokens.shift_while { |t|
            if t.kind == :text && t.charno >= token.charno
              code.tokens << t
            elsif t.kind == :blank && @tokens.first&.kind != :blank # Emit last blank line
              if @tokens.first&.charno >= token.charno # But only if it is not the last blank line
                code.tokens << t
              end
            else
              break
            end
          }

        # Detect comment groups (paragraphs)
        else
          if nodes.top.is_a?(Grammar::Command) || nodes.top.is_a?(Grammar::OptionGroup)
            Grammar::Brief.new(nodes.top, token, token.source.sub(/\..*/, "")) if !nodes.top.brief
            parent = nodes.top
          else
            parent = nodes.top.parent
          end

          paragraph = Grammar::Paragraph.parse(parent, token)
          while @tokens.first&.kind == :text && @tokens.first.charno == token.charno
            paragraph.tokens << @tokens.shift
          end
          nodes.push paragraph # Leave paragraph on stack so we can detect code blocks
        end

      when :brief
        parent = nodes.top.is_a?(Grammar::Paragraph) ? nodes.top.parent : nodes.top
        parent.brief.nil? or parse_error token, "Duplicate brief"
        Grammar::Brief.parse(parent, token)

      when :blank
        ; # do nothing

    else
      raise InternalError, "Unexpected token kind: #{token.kind.inspect}"
    end

    # Skip blank lines
    @tokens.shift_while { |token| token.kind == :blank }
  end

  @program
end