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}.

Methods

==   block?   call?   children   condition?   def?   file   first_line   full_source   has_line?   inspect   jump   kw?   line   line_range   literal?   loop?   new   node_class_for   pretty_print   ref?   show   source   source_range   token?   traverse   unfreeze  

Constants

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]

External Aliases

docstring -> comments
docstring_range -> comments_range
docstring_hash_flag -> comments_hash_flag
source -> to_s

Attributes

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

Public Class methods

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.

Public Instance methods

@return [Boolean] whether the node is equal to another by checking

  the list and type

@private

@return [Boolean] whether the node has a block

@return [Boolean] whether the node is a method call

@return [Array<AstNode>] the {AstNode} children inside the node

@return [Boolean] whether the node is a if/elsif/else condition

@return [Boolean] whether the node is a method definition

@return [String] the filename the node was parsed from

@return [String] the first line of source represented by the node.

@return [String] the full source that the node was parsed from

@return [Boolean] whether the node has a {line_range} set

@return [String] inspects the object

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

@return [Boolean] whether the node is a keyword

@return [Fixnum] the starting line number of the node

@return [Range] the line range in {full_source} represented

  by the node

@return [Boolean] whether the node is a literal value

@return [Boolean] whether the node is a loop

@return [nil] pretty prints the node

@return [Boolean] whether the node is a reference (variable,

  constant name)

@return [String] the first line of source the node represents

@return [String] the parse of {full_source} that the node represents

@return [Range] the character range in {full_source} represented

  by the node

@return [Boolean] whether the node is a token

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]

Resets node state in tree

[Validate]