Class | YARD::Parser::Ruby::AstNode |
In: |
lib/yard/parser/ruby/ast_node.rb
|
Parent: | Array |
An AST node is characterized by a type and a list of children. It is most easily represented by the s-expression {s} such as:
# AST for "if true; 5 end": s(s(:if, s(:var_ref, s(:kw, "true")), s(s(:int, "5")), nil))
The node type is not considered part of the list, only its children. So +ast[0]+ does not refer to the type, but rather the first child (or object). Items that are not AstNode objects can be part of the list, like Strings or Symbols representing names. To return only the AstNode children of the node, use {children}.
KEYWORDS | = | { :class => true, :alias => true, :lambda => true, :do_block => true, :def => true, :defs => true, :begin => true, :rescue => true, :rescue_mod => true, :if => true, :if_mod => true, :else => true, :elsif => true, :case => true, :when => true, :next => true, :break => true, :retry => true, :redo => true, :return => true, :throw => true, :catch => true, :until => true, :until_mod => true, :while => true, :while_mod => true, :yield => true, :yield0 => true, :zsuper => true, :unless => true, :unless_mod => true, :for => true, :super => true, :return0 => true } | List of all known keywords @return [Hash] |
docstring | -> | comments |
docstring_range | -> | comments_range |
docstring_hash_flag | -> | comments_hash_flag |
source | -> | to_s |
docstring | [RW] | |
docstring_hash_flag | [RW] | |
docstring_range | [RW] | |
file | [W] | |
full_source | [W] | |
group | [RW] | @deprecated Groups are now defined by directives @see Tags::GroupDirective |
line_range | [W] | |
parent | [RW] | @return [AstNode, nil] the node‘s parent or nil if it is a root node. |
source | [RW] | |
source_range | [W] | |
type | [RW] | @return [Symbol] the node‘s unique symbolic type |
Creates a new AST node
@param [Symbol] type the type of node being created @param [Array<AstNode>] arr the child nodes @param [Hash] opts any extra line options @option opts [Fixnum] :line (nil) the line the node starts on in source @option opts [String] :char (nil) the character number the node starts on
in source
@option opts [Fixnum] :listline (nil) a special key like :line but for
list nodes
@option opts [Fixnum] :listchar (nil) a special key like :char but for
list nodes
@option opts [Boolean] :token (nil) whether the node represents a token
Finds the node subclass that should be instantiated for a specific node type
@param [Symbol] type the node type to find a subclass for @return [Class] a subclass of AstNode to instantiate the node with.
Searches through the node and all descendants and returns the first node with a type matching any of node_types, otherwise returns the original node (self).
@example Returns the first method definition in a block of code
ast = YARD.parse_string("if true; def x; end end").ast ast.jump(:def) # => s(:def, s(:ident, "x"), s(:params, nil, nil, nil, nil, # nil), s(s(:void_stmt, )))
@example Returns first ‘def’ or ‘class’ statement
ast = YARD.parse_string("class X; def y; end end") ast.jump(:def, :class).first # =>
@example If the node types are not present in the AST
ast = YARD.parse("def x; end") ast.jump(:def)
@param [Array<Symbol>] node_types a set of node types to match @return [AstNode] the matching node, if one was found @return [self] if no node was found
Traverses the object and yields each node (including descendants) in order.
@yield each descendant node in order @yieldparam [AstNode] self, or a child/descendant node @return [void]