Class: TomParse::Parser
- Inherits:
-
Object
- Object
- TomParse::Parser
- Defined in:
- lib/tomparse/parser.rb
Overview
Encapsulate parsed tomdoc documentation.
TODO: Currently uses lazy evaluation, eventually this should be removed and simply parsed all at once.
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
Class Method Summary collapse
-
.valid?(text) ⇒ Boolean
Validate given comment text.
Instance Method Summary collapse
-
#arguments ⇒ Object
(also: #args)
Arguments list.
-
#deprecated? ⇒ Boolean
Check if method is deprecated.
-
#description ⇒ Object
Description of method or class/module.
-
#examples ⇒ Object
List of use examples of a method or class/module.
-
#initialize(text, parse_options = {}) ⇒ Parser
constructor
Public: Initialize a TomDoc object.
-
#internal? ⇒ Boolean
Check if method is internal.
-
#options ⇒ Object
(also: #keyword_arguments)
Keyword arguments, aka Options.
-
#parse ⇒ Object
Internal: Parse the Tomdoc formatted comment.
-
#public? ⇒ Boolean
Check if method is public.
-
#raises ⇒ Object
A list of errors a method might raise.
-
#returns ⇒ Object
The list of retrun values a method can return.
-
#sections ⇒ Object
List of comment sections.
-
#signature_fields ⇒ Object
A list of signature fields.
-
#signatures ⇒ Object
A list of alternate method signatures.
-
#status ⇒ Object
Method status, can be
Public,InternalorDeprecated. -
#tags ⇒ Object
List of tags.
-
#to_s ⇒ Object
Raw documentation text.
-
#tomdoc ⇒ Object
The raw comment text cleaned-up and ready for section parsing.
-
#valid? ⇒ Boolean
Validate raw comment.
-
#validate ⇒ Object
Validate raw comment.
-
#yields ⇒ Object
Description of a methods yield procedure.
Constructor Details
#initialize(text, parse_options = {}) ⇒ Parser
Public: Initialize a TomDoc object.
text - The raw text of a method or class/module comment.
Returns new TomDoc instance.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tomparse/parser.rb', line 18 def initialize(text, ={}) @raw = text.to_s.strip @arguments = [] @options = [] @examples = [] @returns = [] @raises = [] @signatures = [] @signature_fields = [] @tags = [] #parse unless @raw.empty? end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
11 12 13 |
# File 'lib/tomparse/parser.rb', line 11 def raw @raw end |
Class Method Details
.valid?(text) ⇒ Boolean
Validate given comment text.
Returns true if comment is valid, otherwise false.
43 44 45 |
# File 'lib/tomparse/parser.rb', line 43 def self.valid?(text) new(text).valid? end |
Instance Method Details
#arguments ⇒ Object Also known as: args
Arguments list.
Returns list of arguments.
137 138 139 140 141 |
# File 'lib/tomparse/parser.rb', line 137 def arguments parsed { @arguments } end |
#deprecated? ⇒ Boolean
Check if method is deprecated.
Returns true if method is deprecated.
247 248 249 250 251 |
# File 'lib/tomparse/parser.rb', line 247 def deprecated? parsed { @status == 'Deprecated' } end |
#description ⇒ Object
Description of method or class/module.
Returns description String.
128 129 130 131 132 |
# File 'lib/tomparse/parser.rb', line 128 def description parsed { @description } end |
#examples ⇒ Object
List of use examples of a method or class/module.
Returns String of examples.
157 158 159 160 161 |
# File 'lib/tomparse/parser.rb', line 157 def examples parsed { @examples } end |
#internal? ⇒ Boolean
Check if method is internal.
Returns true if method is internal.
238 239 240 241 242 |
# File 'lib/tomparse/parser.rb', line 238 def internal? parsed { @status == 'Internal' } end |
#options ⇒ Object Also known as: keyword_arguments
Keyword arguments, aka Options.
Returns list of options.
147 148 149 150 151 |
# File 'lib/tomparse/parser.rb', line 147 def parsed { @options } end |
#parse ⇒ Object
Internal: Parse the Tomdoc formatted comment.
Returns true if there was a comment to parse.
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 |
# File 'lib/tomparse/parser.rb', line 314 def parse @parsed = true sections = smart_split(tomdoc) return false if sections.empty? # We are assuming that the first section is always description. # And it should be, but people aren't always proper, so perhaps # this can be made a little smarter in the future. parse_description(sections.shift) # The second section may be arguments. if sections.first && sections.first =~ /^\w+\s+\-/m parse_arguments(sections.shift) end current = sections.shift while current case type = section_type(current) when :arguments parse_arguments(current) when :options (current) when :example parse_example(current) when :examples parse_examples(current) when :yields parse_yields(current) when :returns parse_returns(current) when :raises parse_raises(current) when :signature parse_signature(current) when Symbol parse_tag(current) end current = sections.shift end return @parsed end |
#public? ⇒ Boolean
Check if method is public.
Returns true if method is public.
229 230 231 232 233 |
# File 'lib/tomparse/parser.rb', line 229 def public? parsed { @status == 'Public' } end |
#raises ⇒ Object
A list of errors a method might raise.
Returns Array of method raises descriptions.
184 185 186 187 188 |
# File 'lib/tomparse/parser.rb', line 184 def raises parsed { @raises } end |
#returns ⇒ Object
The list of retrun values a method can return.
Returns Array of method return descriptions.
175 176 177 178 179 |
# File 'lib/tomparse/parser.rb', line 175 def returns parsed { @returns } end |
#sections ⇒ Object
List of comment sections. These are divided simply on "\n\n".
Returns Array of comment sections.
119 120 121 122 123 |
# File 'lib/tomparse/parser.rb', line 119 def sections parsed { @sections } end |
#signature_fields ⇒ Object
A list of signature fields.
Returns Array of field definitions.
202 203 204 205 206 |
# File 'lib/tomparse/parser.rb', line 202 def signature_fields parsed { @signature_fields } end |
#signatures ⇒ Object
A list of alternate method signatures.
Returns Array of signatures.
193 194 195 196 197 |
# File 'lib/tomparse/parser.rb', line 193 def signatures parsed { @signatures } end |
#status ⇒ Object
Method status, can be Public, Internal or Deprecated.
Returns [String]
220 221 222 223 224 |
# File 'lib/tomparse/parser.rb', line 220 def status parsed { @status } end |
#tags ⇒ Object
List of tags.
Returns an associatve array of tags. [Array<Array
211 212 213 214 215 |
# File 'lib/tomparse/parser.rb', line 211 def parsed { @tags } end |
#to_s ⇒ Object
Raw documentation text.
Returns String of raw documentation text.
36 37 38 |
# File 'lib/tomparse/parser.rb', line 36 def to_s @raw end |
#tomdoc ⇒ Object
The raw comment text cleaned-up and ready for section parsing.
Returns cleaned-up comment String.
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 |
# File 'lib/tomparse/parser.rb', line 80 def tomdoc lines = raw.split("\n") # remove remark symbol if lines.all?{ |line| /^\s*#/ =~ line } lines = lines.map do |line| line =~ /^(\s*#)/ ? line.sub($1, '') : nil end end # for some reason the first line is coming in without indention # regardless, so we temporary remove it first = lines.shift # remove indention spaces = lines.map do |line| next if line.strip.empty? md = /^(\s*)/.match(line) md ? md[1].size : nil end.compact space = spaces.min || 0 lines = lines.map do |line| if line.strip.empty? line.strip else line[space..-1] end end # put first line back lines.unshift(first.sub(/^\s*/,'')) if first lines.compact.join("\n") end |
#valid? ⇒ Boolean
Validate raw comment.
TODO: This needs improvement.
Returns true if comment is valid, otherwise false.
52 53 54 55 56 57 58 59 |
# File 'lib/tomparse/parser.rb', line 52 def valid? begin new(text).validate true rescue ParseError false end end |
#validate ⇒ Object
Validate raw comment.
Returns true if comment is valid. Raises ParseError if comment is not valid.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/tomparse/parser.rb', line 65 def validate if !raw.include?('Returns') raise ParseError.new("No `Returns' statement.") end if sections.size < 2 raise ParseError.new("No description section found.") end true end |
#yields ⇒ Object
Description of a methods yield procedure.
Returns String decription of yield procedure.
166 167 168 169 170 |
# File 'lib/tomparse/parser.rb', line 166 def yields parsed { @yields } end |