class YARD::Parser::Ruby::AstNode
the AstNode children of the node, use {#children}.
list, like Strings or Symbols representing names. To return only
(or object). Items that are not AstNode
objects can be part of the
So ast[0]
does not refer to the type, but rather the first child
The node type is not considered part of the list, only its children.
s(s(:if, s(:var_ref, s(:kw, “true”)), s(s(:int, “5”)), nil))
# AST for “if true; 5 end”:
is most easily represented by the s-expression {#s} such as:
An AST node is characterized by a type and a list of children. It
def self.node_class_for(type)
-
(Class)
- a subclass of AstNode to instantiate the node with.
Parameters:
-
type
(Symbol
) -- the node type to find a subclass for
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
def ==(other)
- Private: -
Returns:
-
(Boolean)
- whether the node is equal to another by checking
def ==(other) super && type == other.type end
def block?
-
(Boolean)
- whether the node has a block
def block? respond_to?(:block) || condition? end
def call?
-
(Boolean)
- whether the node is a method call
def call? false end
def children
-
(Array
- the {AstNode} children inside the node)
def children @children ||= select {|e| AstNode === e } end
def condition?
-
(Boolean)
- whether the node is a if/elsif/else condition
def condition? false end
def def?
-
(Boolean)
- whether the node is a method definition
def def? false end
def file
-
(String)
- the filename the node was parsed from
def file return parent.file if parent @file end
def first_line
-
(String)
- the first line of source represented by the node.
def first_line full_source.split(/\r?\n/)[line - 1].strip end
def full_source
-
(String)
- the full source that the node was parsed from
def full_source return parent.full_source if parent return @full_source if @full_source return IO.read(@file) if file && File.exist?(file) end
def has_line?
-
(Boolean)
- whether the node has a {#line_range} set
def has_line? @line_range ? true : false end
def initialize(type, arr, opts = {})
(**opts)
-
:token
(Boolean
) -- whether the node represents a token -
:listchar
(Fixnum
) -- a special key like :char but for -
:listline
(Fixnum
) -- a special key like :line but for -
:char
(String
) -- the character number the node starts on -
:line
(Fixnum
) -- the line the node starts on in source
Parameters:
-
opts
(Hash
) -- any extra line options -
arr
(Array
) -- the child nodes -
type
(Symbol
) -- the type of node being created
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
def inspect
-
(String)
- inspects the object
def inspect typeinfo = type && type != :list ? ':' + type.to_s + ', ' : '' 's(' + typeinfo + map(&:inspect).join(", ") + ')' end
def jump(*node_types)
-
(self)
- if no node was found -
(AstNode)
- the matching node, if one was found
Parameters:
-
node_types
(Array
) -- a set of node types to match
Other tags:
- Example: If the node types are not present in the AST -
Example: Returns first 'def' or 'class' statement -
Example: Returns the first method definition in a block of code -
def jump(*node_types) traverse {|child| return(child) if node_types.include?(child.type) } self end
def kw?
-
(Boolean)
- whether the node is a keyword
def kw? false end
def line
-
(Fixnum)
- the starting line number of the node
def line line_range && line_range.first end
def line_range
-
(Range)
- the line range in {#full_source} represented
def line_range reset_line_info unless @line_range @line_range end
def literal?
-
(Boolean)
- whether the node is a literal value
def literal? false end
def loop?
-
(Boolean)
- whether the node is a loop
def loop? false end
def pretty_print(q)
-
(nil)
- pretty prints the node
def pretty_print(q) objs = dup + [:__last__] objs.unshift(type) if type && type != :list options = [] options << ['docstring', docstring] if @docstring if @source_range || @line_range options << ['line', line_range] options << ['source', source_range] end objs.pop if options.empty? q.group(3, 's(', ')') do q.seplist(objs, nil, :each) do |v| if v == :__last__ q.seplist(options, 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
def ref?
-
(Boolean)
- whether the node is a reference (variable,
def ref? false end
def reset_line_info
-
(void)
-
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
def show
-
(String)
- the first line of source the node represents
def show "\t#{line}: #{first_line}" end
def source
-
(String)
- the parse of {#full_source} that the node represents
def source return parent.full_source[source_range] if parent full_source end
def source_range
-
(Range)
- the character range in {#full_source} represented
def source_range reset_line_info unless @source_range @source_range end
def token?
-
(Boolean)
- whether the node is a token
def token? @token end
def traverse
-
(void)
-
Other tags:
- Yieldparam: self, - or a child/descendant node
Other tags:
- Yield: - each descendant node in order
def traverse nodes = [self] until nodes.empty? node = nodes.pop yield node nodes += node.children.reverse unless node.children.empty? end end
def unfreeze
def unfreeze @children = nil end