Module: Tap::Support::TDoc::ConfigParser

Includes:
RDoc::RubyToken, RDoc::TokenStream
Defined in:
lib/tap/support/tdoc.rb

Overview

Provides methods extending an RDoc::RubyParser such that the parser will produce TDoc::ConfigAttr instances in the place of RDoc::Attr instances during attribute parsing.

Constant Summary collapse

CONFIG_ACCESSORS =
['config', 'config_attr']

Instance Method Summary collapse

Instance Method Details

#get_tk_to_nlObject

Gets tokens until the next TkNL



265
266
267
268
269
270
271
272
# File 'lib/tap/support/tdoc.rb', line 265

def get_tk_to_nl
  tokens = []
  while !(tk = get_tk).kind_of?(TkNL)
    tokens.push tk
  end
  unget_tk(tk)
  tokens
end

#parse_attr_accessor(context, single, tk, comment) ⇒ Object

Overrides the standard parse_attr_accessor method to hook in parsing of the config accessors. If the input token is not named as one of the CONFIG_ACCESSORS, it will be processed normally.



344
345
346
347
348
349
350
351
# File 'lib/tap/support/tdoc.rb', line 344

def parse_attr_accessor(context, single, tk, comment)
  case tk.name
  when 'config', 'config_attr'
      parse_config(context, single,  tk, comment)
  else
    super
  end
end

#parse_config(context, single, tk, comment) ⇒ Object

Works like the original parse_attr_accessor, except that the arg name is parsed from the config syntax and added attribute will be a TDoc::ConfigAttr. For example:

class TaskDoc < Tap::Task
  config [:key, 'value']   # comment
end

produces an attribute named :key in the current config_rw mode.

(see ‘rdoc/parsers/parse_rb’ line 2509)



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/tap/support/tdoc.rb', line 285

def parse_config(context, single, tk, comment)
  tks = get_tk_to_nl
  
  key_tk = nil
  value_tk = nil

  tks.each do |token|
    next if token.kind_of?(TkSPACE)
    
    if key_tk == nil
      case token
      when TkSYMBOL then key_tk = token
      when TkLPAREN then next
      else break
      end
    else
      case token
      when TkCOMMA then value_tk = token
      else
        value_tk = token if value_tk.kind_of?(TkCOMMA)
        break
      end
    end
  end

  text = ""
  if tks.last.kind_of?(TkCOMMENT)
    text = tks.last.text.chomp("\n").chomp("\r")
    unget_tk(tks.last)
      
    # If nodoc is given, don't document
      
    tmp = RDoc::CodeObject.new
    read_documentation_modifiers(tmp, RDoc::ATTR_MODIFIERS)
    text = nil unless tmp.document_self
  end
  
  tks.reverse_each {|token| unget_tk(token) }
  return if key_tk == nil || text == nil
  
  arg = key_tk.text[1..-1]
  default = nil
  if value_tk
    if text =~ /(.*):no_default:(.*)/
      text = $1 + $2
    else
      default = value_tk.text
    end
  end
  att = TDoc::ConfigAttr.new(text, arg, "RW", comment)
  att.config_declaration = get_tkread
  att.default = default
   
  context.add_attribute(att)
end