Class: YARD::Parser::Ruby::AstNode
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Array
|
|
Instance Chain:
self,
::Array
|
|
Inherits: |
Array
|
Defined in: | lib/yard/parser/ruby/ast_node.rb |
Overview
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.
Constant Summary
-
KEYWORDS =
List of all known 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}
Creating an AstNode
- #==(other) ⇒ Boolean Internal use only Internal use only
-
.new(type, arr, opts = {}) ⇒ AstNode
constructor
Creates a new AST node.
-
.node_class_for(type) ⇒ Class
Finds the node subclass that should be instantiated for a specific node type.
Traversing a Node
- #children ⇒ Array<AstNode>
-
#jump(*node_types) ⇒ AstNode, self
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). -
#traverse {|self,| ... } ⇒ void
Traverses the object and yields each node (including descendants) in order.
Node Meta Types
- #block? ⇒ Boolean readonly
- #call? ⇒ Boolean readonly
- #condition? ⇒ Boolean readonly
- #def? ⇒ Boolean readonly
- #kw? ⇒ Boolean readonly
- #literal? ⇒ Boolean readonly
- #loop? ⇒ Boolean readonly
- #ref? ⇒ Boolean readonly
- #token? ⇒ Boolean readonly
Getting Line Information
Printing a Node
Managing node state
-
#reset_line_info ⇒ void
private
Resets line information.
-
#unfreeze
Resets node state in tree.
Instance Attribute Summary
-
#comments
readonly
Alias for #docstring.
-
#comments_hash_flag
readonly
Alias for #docstring_hash_flag.
-
#comments_range
readonly
Alias for #docstring_range.
- #docstring (also: #comments) rw
- #docstring_hash_flag (also: #comments_hash_flag) rw
- #docstring_range (also: #comments_range) rw
- #file ⇒ String rw
- #file=(value) rw
- #full_source ⇒ String rw
- #full_source=(value) rw
-
#group
rw
deprecated
Deprecated.
Groups are now defined by directives
- #line_range ⇒ Range rw
- #line_range=(value) rw
- #parent ⇒ AstNode? rw
- #source ⇒ String (also: #to_s) rw
- #source=(value) rw
- #source_range ⇒ Range rw
- #source_range=(value) rw
-
#to_s
readonly
Alias for #source.
- #type ⇒ Symbol rw
Instance Method Summary
Constructor Details
.new(type, arr, opts = {}) ⇒ AstNode
Creates a new AST node
# File 'lib/yard/parser/ruby/ast_node.rb', line 153
def initialize(type, arr, opts = {}) super(arr) self.type = type self.line_range = opts[:line] self.source_range = opts[:char] @fallback_line = opts[:listline] @fallback_source = opts[:listchar] @token = true if opts[:token] @docstring = nil end
Class Method Details
.node_class_for(type) ⇒ Class
Finds the node subclass that should be instantiated for a specific node type
# File 'lib/yard/parser/ruby/ast_node.rb', line 111
def self.node_class_for(type) case type when :params ParameterNode when :call, :fcall, :vcall, :command, :command_call MethodCallNode when :if, :elsif, :if_mod, :unless, :unless_mod ConditionalNode when :for, :while, :while_mod, :until, :until_mod LoopNode when :def, :defs MethodDefinitionNode when :class, :sclass ClassNode when :module ModuleNode else if type.to_s =~ /_ref\Z/ ReferenceNode elsif type.to_s =~ /_literal\Z/ LiteralNode elsif KEYWORDS.key?(type) KeywordNode else AstNode end end end
Instance Attribute Details
#block? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 261
def block? respond_to?(:block) || condition? end
#call? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 241
def call? false end
#comments (readonly)
Alias for #docstring.
# File 'lib/yard/parser/ruby/ast_node.rb', line 50
alias comments docstring
#comments_hash_flag (readonly)
Alias for #docstring_hash_flag.
# File 'lib/yard/parser/ruby/ast_node.rb', line 52
alias comments_hash_flag docstring_hash_flag
#comments_range (readonly)
Alias for #docstring_range.
# File 'lib/yard/parser/ruby/ast_node.rb', line 51
alias comments_range docstring_range
#condition? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 251
def condition? false end
#def? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 246
def def? false end
#docstring (rw) Also known as: #comments
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 43
attr_accessor :docstring, :docstring_range, :source
#docstring_hash_flag (rw) Also known as: #comments_hash_flag
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 42
attr_accessor :docstring_hash_flag
#docstring_range (rw) Also known as: #comments_range
[ GitHub ]#file ⇒ String (rw)
#file=(value) (rw)
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 49
attr_writer :source_range, :line_range, :file, :full_source
#full_source ⇒ String (rw)
#full_source=(value) (rw)
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 49
attr_writer :source_range, :line_range, :file, :full_source
#group (rw)
Groups are now defined by directives
# File 'lib/yard/parser/ruby/ast_node.rb', line 47
attr_accessor :group
#has_line? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 268
def has_line? @line_range ? true : false end
#kw? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 236
def kw? false end
#line_range ⇒ Range
(rw)
# File 'lib/yard/parser/ruby/ast_node.rb', line 70
def line_range reset_line_info unless @line_range @line_range end
#line_range=(value) (rw)
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 49
attr_writer :source_range, :line_range, :file, :full_source
#literal? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 231
def literal? false end
#loop? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 256
def loop? false end
#parent ⇒ AstNode
? (rw)
# File 'lib/yard/parser/ruby/ast_node.rb', line 59
attr_accessor :parent
#ref? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 226
def ref? false end
#source ⇒ String (rw) Also known as: #to_s
# File 'lib/yard/parser/ruby/ast_node.rb', line 89
attr_accessor :docstring, :docstring_range, :source
#source=(value) (rw)
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 43
attr_accessor :docstring, :docstring_range, :source
#source_range ⇒ Range
(rw)
# File 'lib/yard/parser/ruby/ast_node.rb', line 63
def source_range reset_line_info unless @source_range @source_range end
#source_range=(value) (rw)
[ GitHub ]# File 'lib/yard/parser/ruby/ast_node.rb', line 49
attr_writer :source_range, :line_range, :file, :full_source
#to_s (readonly)
Alias for #source.
# File 'lib/yard/parser/ruby/ast_node.rb', line 53
alias to_s source
#token? ⇒ Boolean
(readonly)
# File 'lib/yard/parser/ruby/ast_node.rb', line 220
def token? @token end
#type ⇒ Symbol
(rw)
# File 'lib/yard/parser/ruby/ast_node.rb', line 56
attr_accessor :type
Instance Method Details
#==(other) ⇒ Boolean
#children ⇒ Array<AstNode
>
# File 'lib/yard/parser/ruby/ast_node.rb', line 199
def children @children ||= select {|e| AstNode === e } end
#first_line ⇒ String
# File 'lib/yard/parser/ruby/ast_node.rb', line 278
def first_line full_source.split(/\r?\n/)[line - 1].strip end
#inspect ⇒ String
#jump(*node_types) ⇒ AstNode
, self
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).
#line ⇒ Fixnum
# File 'lib/yard/parser/ruby/ast_node.rb', line 273
def line line_range && line_range.first end
#pretty_print(q) ⇒ nil
# File 'lib/yard/parser/ruby/ast_node.rb', line 290
def pretty_print(q) objs = dup + [:__last__] objs.unshift(type) if type && type != :list = [] << ['docstring', docstring] if @docstring if @source_range || @line_range << ['line', line_range] << ['source', source_range] end objs.pop if .empty? q.group(3, 's(', ')') do q.seplist(objs, nil, :each) do |v| if v == :__last__ q.seplist(, nil, :each) do |arr| k, v2 = *arr q.group(3) do q.text k q.group(3) do q.text ': ' q.pp v2 end end end else q.pp v end end end end
#reset_line_info ⇒ void
(private)
This method returns an undefined value.
Resets line information
# File 'lib/yard/parser/ruby/ast_node.rb', line 341
def reset_line_info if size == 0 self.line_range = @fallback_line self.source_range = @fallback_source elsif !children.empty? f = children.first l = children.last self.line_range = Range.new(f.line_range.first, l.line_range.last) self.source_range = Range.new(f.source_range.first, l.source_range.last) elsif @fallback_line || @fallback_source self.line_range = @fallback_line self.source_range = @fallback_source else self.line_range = 0...0 self.source_range = 0...0 end end
#show ⇒ String
# File 'lib/yard/parser/ruby/ast_node.rb', line 285
def show "\t#{line}: #{first_line}" end
#traverse {|self,| ... } ⇒ void
This method returns an undefined value.
Traverses the object and yields each node (including descendants) in order.
#unfreeze
Resets node state in tree
# File 'lib/yard/parser/ruby/ast_node.rb', line 331
def unfreeze @children = nil end