-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathruby.rb
2363 lines (1919 loc) · 59.2 KB
/
ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
##
# This file contains stuff stolen outright from:
#
# rtags.rb -
# ruby-lex.rb - ruby lexcal analyzer
# ruby-token.rb - ruby tokens
# by Keiju ISHITSUKA (Nippon Rational Inc.)
#
require 'ripper'
require_relative 'ripper_state_lex'
##
# Extracts code elements from a source file returning a TopLevel object
# containing the constituent file elements.
#
# This file is based on rtags
#
# RubyParser understands how to document:
# * classes
# * modules
# * methods
# * constants
# * aliases
# * private, public, protected
# * private_class_function, public_class_function
# * private_constant, public_constant
# * module_function
# * attr, attr_reader, attr_writer, attr_accessor
# * extra accessors given on the command line
# * metaprogrammed methods
# * require
# * include
#
# == Method Arguments
#
#--
# NOTE: I don't think this works, needs tests, remove the paragraph following
# this block when known to work
#
# The parser extracts the arguments from the method definition. You can
# override this with a custom argument definition using the :args: directive:
#
# ##
# # This method tries over and over until it is tired
#
# def go_go_go(thing_to_try, tries = 10) # :args: thing_to_try
# puts thing_to_try
# go_go_go thing_to_try, tries - 1
# end
#
# If you have a more-complex set of overrides you can use the :call-seq:
# directive:
#++
#
# The parser extracts the arguments from the method definition. You can
# override this with a custom argument definition using the :call-seq:
# directive:
#
# ##
# # This method can be called with a range or an offset and length
# #
# # :call-seq:
# # my_method(Range)
# # my_method(offset, length)
#
# def my_method(*args)
# end
#
# The parser extracts +yield+ expressions from method bodies to gather the
# yielded argument names. If your method manually calls a block instead of
# yielding or you want to override the discovered argument names use
# the :yields: directive:
#
# ##
# # My method is awesome
#
# def my_method(&block) # :yields: happy, times
# block.call 1, 2
# end
#
# == Metaprogrammed Methods
#
# To pick up a metaprogrammed method, the parser looks for a comment starting
# with '##' before an identifier:
#
# ##
# # This is a meta-programmed method!
#
# add_my_method :meta_method, :arg1, :arg2
#
# The parser looks at the token after the identifier to determine the name, in
# this example, :meta_method. If a name cannot be found, a warning is printed
# and 'unknown is used.
#
# You can force the name of a method using the :method: directive:
#
# ##
# # :method: some_method!
#
# By default, meta-methods are instance methods. To indicate that a method is
# a singleton method instead use the :singleton-method: directive:
#
# ##
# # :singleton-method:
#
# You can also use the :singleton-method: directive with a name:
#
# ##
# # :singleton-method: some_method!
#
# You can define arguments for metaprogrammed methods via either the
# :call-seq:, :arg: or :args: directives.
#
# Additionally you can mark a method as an attribute by
# using :attr:, :attr_reader:, :attr_writer: or :attr_accessor:. Just like
# for :method:, the name is optional.
#
# ##
# # :attr_reader: my_attr_name
#
# == Hidden methods and attributes
#
# You can provide documentation for methods that don't appear using
# the :method:, :singleton-method: and :attr: directives:
#
# ##
# # :attr_writer: ghost_writer
# # There is an attribute here, but you can't see it!
#
# ##
# # :method: ghost_method
# # There is a method here, but you can't see it!
#
# ##
# # this is a comment for a regular method
#
# def regular_method() end
#
# Note that by default, the :method: directive will be ignored if there is a
# standard rdocable item following it.
class RDoc::Parser::Ruby < RDoc::Parser
parse_files_matching(/\.rbw?$/)
include RDoc::TokenStream
include RDoc::Parser::RubyTools
##
# RDoc::NormalClass type
NORMAL = "::"
##
# RDoc::SingleClass type
SINGLE = "<<"
##
# Creates a new Ruby parser.
def initialize(top_level, file_name, content, options, stats)
super
content = handle_tab_width(content)
@size = 0
@token_listeners = nil
content = RDoc::Encoding.remove_magic_comment content
@scanner = RDoc::Parser::RipperStateLex.parse(content)
@content = content
@scanner_point = 0
@prev_seek = nil
@markup = @options.markup
@track_visibility = :nodoc != @options.visibility
@encoding = @options.encoding
reset
end
##
# Return +true+ if +tk+ is a newline.
def tk_nl?(tk)
:on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]
end
##
# Retrieves the read token stream and replaces +pattern+ with +replacement+
# using gsub. If the result is only a ";" returns an empty string.
def get_tkread_clean pattern, replacement # :nodoc:
read = get_tkread.gsub(pattern, replacement).strip
return '' if read == ';'
read
end
##
# Extracts the visibility information for the visibility token +tk+
# and +single+ class type identifier.
#
# Returns the visibility type (a string), the visibility (a symbol) and
# +singleton+ if the methods following should be converted to singleton
# methods.
def get_visibility_information tk, single # :nodoc:
vis_type = tk[:text]
singleton = single == SINGLE
vis =
case vis_type
when 'private' then :private
when 'protected' then :protected
when 'public' then :public
when 'private_class_method' then
singleton = true
:private
when 'public_class_method' then
singleton = true
:public
when 'module_function' then
singleton = true
:public
else
raise RDoc::Error, "Invalid visibility: #{tk.name}"
end
return vis_type, vis, singleton
end
##
# Look for the first comment in a file that isn't a shebang line.
def collect_first_comment
skip_tkspace
comment = ''.dup
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
first_line = true
first_comment_tk_kind = nil
line_no = nil
tk = get_tk
while tk && (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])
comment_body = retrieve_comment_body(tk)
if first_line and comment_body =~ /\A#!/ then
skip_tkspace
tk = get_tk
elsif first_line and comment_body =~ /\A#\s*-\*-/ then
first_line = false
skip_tkspace
tk = get_tk
else
break if first_comment_tk_kind and not first_comment_tk_kind === tk[:kind]
first_comment_tk_kind = tk[:kind]
line_no = tk[:line_no] if first_line
first_line = false
comment << comment_body
tk = get_tk
if :on_nl === tk then
skip_tkspace_without_nl
tk = get_tk
end
end
end
unget_tk tk
new_comment comment, line_no
end
##
# Consumes trailing whitespace from the token stream
def consume_trailing_spaces # :nodoc:
skip_tkspace_without_nl
end
##
# Creates a new attribute in +container+ with +name+.
def create_attr container, single, name, rw, comment # :nodoc:
att = RDoc::Attr.new get_tkread, name, rw, comment, single == SINGLE
record_location att
container.add_attribute att
@stats.add_attribute att
att
end
##
# Creates a module alias in +container+ at +rhs_name+ (or at the top-level
# for "::") with the name from +constant+.
def create_module_alias container, constant, rhs_name # :nodoc:
mod = if rhs_name =~ /^::/ then
@store.find_class_or_module rhs_name
else
container.find_module_named rhs_name
end
container.add_module_alias mod, rhs_name, constant, @top_level
end
##
# Aborts with +msg+
def error(msg)
msg = make_message msg
abort msg
end
##
# Looks for a true or false token.
def get_bool
skip_tkspace
tk = get_tk
if :on_kw == tk[:kind] && 'true' == tk[:text]
true
elsif :on_kw == tk[:kind] && ('false' == tk[:text] || 'nil' == tk[:text])
false
else
unget_tk tk
true
end
end
##
# Look for the name of a class of module (optionally with a leading :: or
# with :: separated named) and return the ultimate name, the associated
# container, and the given name (with the ::).
def get_class_or_module container, ignore_constants = false
skip_tkspace
name_t = get_tk
given_name = ''.dup
# class ::A -> A is in the top level
if :on_op == name_t[:kind] and '::' == name_t[:text] then # bug
name_t = get_tk
container = @top_level
given_name << '::'
end
skip_tkspace_without_nl
given_name << name_t[:text]
is_self = name_t[:kind] == :on_op && name_t[:text] == '<<'
new_modules = []
while !is_self && (tk = peek_tk) and :on_op == tk[:kind] and '::' == tk[:text] do
prev_container = container
container = container.find_module_named name_t[:text]
container ||=
if ignore_constants then
c = RDoc::NormalModule.new name_t[:text]
c.store = @store
new_modules << [prev_container, c]
c
else
c = prev_container.add_module RDoc::NormalModule, name_t[:text]
c.ignore unless prev_container.document_children
@top_level.add_to_classes_or_modules c
c
end
record_location container
get_tk
skip_tkspace
if :on_lparen == peek_tk[:kind] # ProcObjectInConstant::()
parse_method_or_yield_parameters
break
end
name_t = get_tk
unless :on_const == name_t[:kind] || :on_ident == name_t[:kind]
raise RDoc::Error, "Invalid class or module definition: #{given_name}"
end
if prev_container == container and !ignore_constants
given_name = name_t[:text]
else
given_name << '::' + name_t[:text]
end
end
skip_tkspace_without_nl
return [container, name_t, given_name, new_modules]
end
##
# Skip opening parentheses and yield the block.
# Skip closing parentheses too when exists.
def skip_parentheses(&block)
left_tk = peek_tk
if :on_lparen == left_tk[:kind]
get_tk
ret = skip_parentheses(&block)
right_tk = peek_tk
if :on_rparen == right_tk[:kind]
get_tk
end
ret
else
yield
end
end
##
# Return a superclass, which can be either a constant of an expression
def get_class_specification
tk = peek_tk
if tk.nil?
return ''
elsif :on_kw == tk[:kind] && 'self' == tk[:text]
return 'self'
elsif :on_gvar == tk[:kind]
return ''
end
res = get_constant
skip_tkspace_without_nl
get_tkread # empty out read buffer
tk = get_tk
return res unless tk
case tk[:kind]
when :on_nl, :on_comment, :on_embdoc, :on_semicolon then
unget_tk(tk)
return res
end
res += parse_call_parameters(tk)
res
end
##
# Parse a constant, which might be qualified by one or more class or module
# names
def get_constant
res = ""
skip_tkspace_without_nl
tk = get_tk
while tk && ((:on_op == tk[:kind] && '::' == tk[:text]) || :on_const == tk[:kind]) do
res += tk[:text]
tk = get_tk
end
unget_tk(tk)
res
end
##
# Get an included module that may be surrounded by parens
def get_included_module_with_optional_parens
skip_tkspace_without_nl
get_tkread
tk = get_tk
end_token = get_end_token tk
return '' unless end_token
nest = 0
continue = false
only_constant = true
while tk != nil do
is_element_of_constant = false
case tk[:kind]
when :on_semicolon then
break if nest == 0
when :on_lbracket then
nest += 1
when :on_rbracket then
nest -= 1
when :on_lbrace then
nest += 1
when :on_rbrace then
nest -= 1
if nest <= 0
# we might have a.each { |i| yield i }
unget_tk(tk) if nest < 0
break
end
when :on_lparen then
nest += 1
when end_token[:kind] then
if end_token[:kind] == :on_rparen
nest -= 1
break if nest <= 0
else
break if nest <= 0
end
when :on_rparen then
nest -= 1
when :on_comment, :on_embdoc then
@read.pop
if :on_nl == end_token[:kind] and "\n" == tk[:text][-1] and
(!continue or (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) != 0) then
break if !continue and nest <= 0
end
when :on_comma then
continue = true
when :on_ident then
continue = false if continue
when :on_kw then
case tk[:text]
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
nest += 1
when 'if', 'unless', 'while', 'until', 'rescue'
# postfix if/unless/while/until/rescue must be EXPR_LABEL
nest += 1 unless (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) != 0
when 'end'
nest -= 1
break if nest == 0
end
when :on_const then
is_element_of_constant = true
when :on_op then
is_element_of_constant = true if '::' == tk[:text]
end
only_constant = false unless is_element_of_constant
tk = get_tk
end
if only_constant
get_tkread_clean(/\s+/, ' ')
else
''
end
end
##
# Little hack going on here. In the statement:
#
# f = 2*(1+yield)
#
# We see the RPAREN as the next token, so we need to exit early. This still
# won't catch all cases (such as "a = yield + 1"
def get_end_token tk # :nodoc:
case tk[:kind]
when :on_lparen
token = RDoc::Parser::RipperStateLex::Token.new
token[:kind] = :on_rparen
token[:text] = ')'
token
when :on_rparen
nil
else
token = RDoc::Parser::RipperStateLex::Token.new
token[:kind] = :on_nl
token[:text] = "\n"
token
end
end
##
# Retrieves the method container for a singleton method.
def get_method_container container, name_t # :nodoc:
prev_container = container
container = container.find_module_named(name_t[:text])
unless container then
constant = prev_container.constants.find do |const|
const.name == name_t[:text]
end
if constant then
parse_method_dummy prev_container
return
end
end
unless container then
# TODO seems broken, should starting at Object in @store
obj = name_t[:text].split("::").inject(Object) do |state, item|
state.const_get(item)
end rescue nil
type = obj.class == Class ? RDoc::NormalClass : RDoc::NormalModule
unless [Class, Module].include?(obj.class) then
warn("Couldn't find #{name_t[:text]}. Assuming it's a module")
end
if type == RDoc::NormalClass then
sclass = obj.superclass ? obj.superclass.name : nil
container = prev_container.add_class type, name_t[:text], sclass
else
container = prev_container.add_module type, name_t[:text]
end
record_location container
end
container
end
##
# Extracts a name or symbol from the token stream.
def get_symbol_or_name
tk = get_tk
case tk[:kind]
when :on_symbol then
text = tk[:text].sub(/^:/, '')
next_tk = peek_tk
if next_tk && :on_op == next_tk[:kind] && '=' == next_tk[:text] then
get_tk
text << '='
end
text
when :on_ident, :on_const, :on_gvar, :on_cvar, :on_ivar, :on_op, :on_kw then
tk[:text]
when :on_tstring, :on_dstring then
tk[:text][1..-2]
else
raise RDoc::Error, "Name or symbol expected (got #{tk})"
end
end
##
# Marks containers between +container+ and +ancestor+ as ignored
def suppress_parents container, ancestor # :nodoc:
while container and container != ancestor do
container.suppress unless container.documented?
container = container.parent
end
end
##
# Look for directives in a normal comment block:
#
# # :stopdoc:
# # Don't display comment from this point forward
#
# This routine modifies its +comment+ parameter.
def look_for_directives_in container, comment
@preprocess.handle comment, container do |directive, param|
case directive
when 'method', 'singleton-method',
'attr', 'attr_accessor', 'attr_reader', 'attr_writer' then
false # handled elsewhere
when 'section' then
break unless container.kind_of?(RDoc::Context)
container.set_current_section param, comment.dup
comment.text = ''
break
end
end
comment.remove_private
end
##
# Adds useful info about the parser to +message+
def make_message message
prefix = "#{@file_name}:".dup
tk = peek_tk
prefix << "#{tk[:line_no]}:#{tk[:char_no]}:" if tk
"#{prefix} #{message}"
end
##
# Creates a comment with the correct format
def new_comment comment, line_no = nil
c = RDoc::Comment.new comment, @top_level, :ruby
c.line = line_no
c.format = @markup
c
end
##
# Creates an RDoc::Attr for the name following +tk+, setting the comment to
# +comment+.
def parse_attr(context, single, tk, comment)
line_no = tk[:line_no]
args = parse_symbol_arg 1
if args.size > 0 then
name = args[0]
rw = "R"
skip_tkspace_without_nl
tk = get_tk
if :on_comma == tk[:kind] then
rw = "RW" if get_bool
else
unget_tk tk
end
att = create_attr context, single, name, rw, comment
att.line = line_no
read_documentation_modifiers att, RDoc::ATTR_MODIFIERS
else
warn "'attr' ignored - looks like a variable"
end
end
##
# Creates an RDoc::Attr for each attribute listed after +tk+, setting the
# comment for each to +comment+.
def parse_attr_accessor(context, single, tk, comment)
line_no = tk[:line_no]
args = parse_symbol_arg
rw = "?"
tmp = RDoc::CodeObject.new
read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
# TODO In most other places we let the context keep track of document_self
# and add found items appropriately but here we do not. I'm not sure why.
return if @track_visibility and not tmp.document_self
case tk[:text]
when "attr_reader" then rw = "R"
when "attr_writer" then rw = "W"
when "attr_accessor" then rw = "RW"
else
rw = '?'
end
for name in args
att = create_attr context, single, name, rw, comment
att.line = line_no
end
end
##
# Parses an +alias+ in +context+ with +comment+
def parse_alias(context, single, tk, comment)
line_no = tk[:line_no]
skip_tkspace
if :on_lparen === peek_tk[:kind] then
get_tk
skip_tkspace
end
new_name = get_symbol_or_name
skip_tkspace
if :on_comma === peek_tk[:kind] then
get_tk
skip_tkspace
end
begin
old_name = get_symbol_or_name
rescue RDoc::Error
return
end
al = RDoc::Alias.new(get_tkread, old_name, new_name, comment,
single == SINGLE)
record_location al
al.line = line_no
read_documentation_modifiers al, RDoc::ATTR_MODIFIERS
context.add_alias al
@stats.add_alias al
al
end
##
# Extracts call parameters from the token stream.
def parse_call_parameters(tk)
end_token = case tk[:kind]
when :on_lparen
:on_rparen
when :on_rparen
return ""
else
:on_nl
end
nest = 0
loop do
break if tk.nil?
case tk[:kind]
when :on_semicolon
break
when :on_lparen
nest += 1
when end_token
if end_token == :on_rparen
nest -= 1
break if RDoc::Parser::RipperStateLex.end?(tk) and nest <= 0
else
break if RDoc::Parser::RipperStateLex.end?(tk)
end
when :on_comment, :on_embdoc
unget_tk(tk)
break
when :on_op
if tk[:text] =~ /^(.{1,2})?=$/
unget_tk(tk)
break
end
end
tk = get_tk
end
get_tkread_clean "\n", " "
end
##
# Parses a class in +context+ with +comment+
def parse_class container, single, tk, comment
line_no = tk[:line_no]
declaration_context = container
container, name_t, given_name, = get_class_or_module container
if name_t[:kind] == :on_const
cls = parse_class_regular container, declaration_context, single,
name_t, given_name, comment
elsif name_t[:kind] == :on_op && name_t[:text] == '<<'
case name = skip_parentheses { get_class_specification }
when 'self', container.name
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
parse_statements container, SINGLE
return # don't update line
else
cls = parse_class_singleton container, name, comment
end
else
warn "Expected class name or '<<'. Got #{name_t[:kind]}: #{name_t[:text].inspect}"
return
end
cls.line = line_no
# after end modifiers
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
cls
end
##
# Parses and creates a regular class
def parse_class_regular container, declaration_context, single, # :nodoc:
name_t, given_name, comment
superclass = '::Object'
if given_name =~ /^::/ then
declaration_context = @top_level
given_name = $'
end
tk = peek_tk
if tk[:kind] == :on_op && tk[:text] == '<' then
get_tk
skip_tkspace
superclass = get_class_specification
superclass = '(unknown)' if superclass.empty?
end
cls_type = single == SINGLE ? RDoc::SingleClass : RDoc::NormalClass
cls = declaration_context.add_class cls_type, given_name, superclass
cls.ignore unless container.document_children
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
record_location cls
cls.add_comment comment, @top_level
@top_level.add_to_classes_or_modules cls
@stats.add_class cls
suppress_parents container, declaration_context unless cls.document_self
parse_statements cls
cls
end
##
# Parses a singleton class in +container+ with the given +name+ and
# +comment+.
def parse_class_singleton container, name, comment # :nodoc:
other = @store.find_class_named name
unless other then
if name =~ /^::/ then
name = $'
container = @top_level
end
other = container.add_module RDoc::NormalModule, name
record_location other
# class << $gvar
other.ignore if name.empty?
other.add_comment comment, @top_level
end
# notify :nodoc: all if not a constant-named class/module
# (and remove any comment)
unless name =~ /\A(::)?[A-Z]/ then
other.document_self = nil
other.document_children = false
other.clear_comment
end
@top_level.add_to_classes_or_modules other
@stats.add_class other
read_documentation_modifiers other, RDoc::CLASS_MODIFIERS
parse_statements(other, SINGLE)
other
end
##
# Parses a constant in +context+ with +comment+. If +ignore_constants+ is
# true, no found constants will be added to RDoc.
def parse_constant container, tk, comment, ignore_constants = false
line_no = tk[:line_no]
name = tk[:text]
skip_tkspace_without_nl
return unless name =~ /^\w+$/
new_modules = []
if :on_op == peek_tk[:kind] && '::' == peek_tk[:text] then
unget_tk tk
container, name_t, _, new_modules = get_class_or_module container, true
name = name_t[:text]
end
is_array_or_hash = false
if peek_tk && :on_lbracket == peek_tk[:kind]
get_tk
nest = 1
while bracket_tk = get_tk
case bracket_tk[:kind]
when :on_lbracket
nest += 1
when :on_rbracket
nest -= 1
break if nest == 0
end
end
skip_tkspace_without_nl
is_array_or_hash = true
end
unless peek_tk && :on_op == peek_tk[:kind] && '=' == peek_tk[:text] then
return false
end
get_tk
unless ignore_constants
new_modules.each do |prev_c, new_module|
prev_c.add_module_by_normal_module new_module
new_module.ignore unless prev_c.document_children
@top_level.add_to_classes_or_modules new_module