Class: YARD::DocstringParser
- Inherits:
-
Object
- Object
- YARD::DocstringParser
- Defined in:
- lib/yard/docstring_parser.rb
Overview
Parses text and creates a Docstring object to represent documentation for a CodeObjects::Base. To create a new docstring, you should initialize the parser and call #parse followed by #to_docstring.
Constant Summary collapse
- META_MATCH =
The regular expression to match the tag syntax
/^@(!)?((?:\w\.?)+)(?:\s+(.*))?$/i
Instance Attribute Summary collapse
-
#directives ⇒ Array<Directive>
readonly
A list of directives identified by the parser.
-
#handler ⇒ Handlers::Base?
The handler parsing this docstring.
-
#library ⇒ Tags::Library
The tag library being used to identify registered tags in the docstring.
-
#object ⇒ CodeObjects::Base?
The object associated with the docstring being parsed.
-
#raw_text ⇒ String
readonly
The complete input string to the parser.
-
#state ⇒ OpenStruct
readonly
Any arbitrary state to be passed between tags during parsing.
-
#tags ⇒ Array<Tag>
readonly
The list of meta-data tags identified by the parser.
-
#text ⇒ String
readonly
The parsed text portion of the docstring, with tags removed.
Class Method Summary collapse
-
.after_parse {|parser| ... } ⇒ void
Creates a callback that is called after a docstring is successfully parsed.
Instance Method Summary collapse
-
#initialize(library = Tags::Library.instance) ⇒ DocstringParser
constructor
Creates a new parser to parse docstring data.
-
#parse(content, object = nil, handler = nil) ⇒ self
Parses all content and returns itself.
-
#to_docstring ⇒ Docstring
Translates parsed text into a Docstring object.
Constructor Details
#initialize(library = Tags::Library.instance) ⇒ DocstringParser
Creates a new parser to parse docstring data
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/yard/docstring_parser.rb', line 74 def initialize(library = Tags::Library.instance) @text = "" @raw_text = "" = [] @directives = [] @library = library @object = nil @handler = nil @state = OpenStruct.new end |
Instance Attribute Details
#directives ⇒ Array<Directive> (readonly)
Returns a list of directives identified by the parser. This list will not be passed on to the Docstring object.
45 46 47 |
# File 'lib/yard/docstring_parser.rb', line 45 def directives @directives end |
#handler ⇒ Handlers::Base?
Returns the handler parsing this docstring. May be nil if this docstring parser is not initialized through.
61 62 63 |
# File 'lib/yard/docstring_parser.rb', line 61 def handler @handler end |
#library ⇒ Tags::Library
Returns the tag library being used to identify registered tags in the docstring.
65 66 67 |
# File 'lib/yard/docstring_parser.rb', line 65 def library @library end |
#object ⇒ CodeObjects::Base?
Returns the object associated with the docstring being parsed. May be nil if the docstring is not attached to any object.
56 57 58 |
# File 'lib/yard/docstring_parser.rb', line 56 def object @object end |
#raw_text ⇒ String (readonly)
Returns the complete input string to the parser.
36 37 38 |
# File 'lib/yard/docstring_parser.rb', line 36 def raw_text @raw_text end |
#state ⇒ OpenStruct (readonly)
Returns any arbitrary state to be passed between tags during parsing. Mainly used by directives to coordinate behaviour (so that directives can be aware of other directives used in a docstring).
51 52 53 |
# File 'lib/yard/docstring_parser.rb', line 51 def state @state end |
#tags ⇒ Array<Tag> (readonly)
Returns the list of meta-data tags identified by the parser.
40 41 42 |
# File 'lib/yard/docstring_parser.rb', line 40 def end |
#text ⇒ String (readonly)
Returns the parsed text portion of the docstring, with tags removed.
33 34 35 |
# File 'lib/yard/docstring_parser.rb', line 33 def text @text end |
Class Method Details
.after_parse {|parser| ... } ⇒ void
This method returns an undefined value.
Creates a callback that is called after a docstring is successfully parsed. Use this method to perform sanity checks on a docstring’s tag data, or add any extra tags automatically to a docstring.
21 22 23 |
# File 'lib/yard/docstring_parser.rb', line 21 def self.after_parse(&block) self.after_parse_callbacks << block end |
Instance Method Details
#parse(content, object = nil, handler = nil) ⇒ self
Parses all content and returns itself.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/yard/docstring_parser.rb', line 97 def parse(content, object = nil, handler = nil) @object = object @handler = handler @raw_text = content text = parse_content(content) # Remove trailing/leading whitespace / newlines @text = text.gsub(/\A[\r\n\s]+|[\r\n\s]+\Z/, '') call_directives_after_parse call_after_parse_callbacks self end |
#to_docstring ⇒ Docstring
Returns translates parsed text into a Docstring object.
111 112 113 |
# File 'lib/yard/docstring_parser.rb', line 111 def to_docstring Docstring.new!(text, , object, raw_text) end |