Class: Prism::SymbolNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/node_ext.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/node_ext.rbs,
ext/prism/api_node.c

Overview

Represents a symbol literal or a symbol contained within a %i list.

:foo
^^^^

%i[foo]
   ^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, opening_loc, content_loc, closing_loc, unescaped) ⇒ SymbolNode

Initialize a new SymbolNode node.

: (Source source, Integer node_id, Location location, Integer flags, Location? opening_loc, Location content_loc, Location? closing_loc, String unescaped) -> void

Parameters:



31564
31565
31566
31567
31568
31569
31570
31571
31572
31573
# File 'lib/prism/node.rb', line 31564

def initialize(source, node_id, location, flags, opening_loc, content_loc, closing_loc, unescaped)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @opening_loc = opening_loc
  @content_loc = content_loc
  @closing_loc = closing_loc
  @unescaped = unescaped
end

Class Method Details

.type ⇒ :symbol_node

See Node.type.

: () -> :symbol_node

Returns:

  • (:symbol_node)


31657
31658
31659
# File 'lib/prism/node.rb', line 31657

def self.type
  :symbol_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


31813
31814
31815
31816
31817
31818
31819
31820
# File 'lib/prism/node.rb', line 31813

def ===(other) # :nodoc:
  other.is_a?(SymbolNode) &&
    (flags === other.flags) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (content_loc.nil? == other.content_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?) &&
    (unescaped === other.unescaped)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


31589
31590
31591
# File 'lib/prism/node.rb', line 31589

def accept(visitor)
  visitor.visit_symbol_node(self)
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


31596
31597
31598
# File 'lib/prism/node.rb', line 31596

def child_nodes
  []
end

#closing ⇒ String?

:call-seq:

closing -> String | nil

Slice the location of closing_loc from the source.

: () -> String?

Returns:

  • (String, nil)


31806
31807
31808
# File 'lib/prism/node.rb', line 31806

def closing
  closing_loc&.slice
end

#closing_loc ⇒ Location?

:category: Locations :call-seq:

closing_loc -> Location | nil

Returns the Location represented by closing_loc.

: () -> Location?

Returns:



31748
31749
31750
31751
31752
31753
31754
31755
31756
31757
31758
# File 'lib/prism/node.rb', line 31748

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


31612
31613
31614
# File 'lib/prism/node.rb', line 31612

def compact_child_nodes
  []
end

#content ⇒ String

:call-seq:

content -> String

Slice the location of content_loc from the source.

: () -> String

Returns:

  • (String)


31796
31797
31798
# File 'lib/prism/node.rb', line 31796

def content
  content_loc.slice
end

#content_loc ⇒ Location

:category: Locations :call-seq:

content_loc -> Location

Returns the Location represented by content_loc.

: () -> Location

Returns:



31726
31727
31728
31729
31730
# File 'lib/prism/node.rb', line 31726

def content_loc
  location = @content_loc
  return location if location.is_a?(Location)
  @content_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, content_loc: self.content_loc, closing_loc: self.closing_loc, unescaped: self.unescaped) ⇒ SymbolNode

:call-seq:

copy(**fields) -> SymbolNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?opening_loc: Location?, ?content_loc: Location, ?closing_loc: Location?, ?unescaped: String) -> SymbolNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • opening_loc: (Location, nil) (defaults to: self.opening_loc)
  • content_loc: (Location) (defaults to: self.content_loc)
  • closing_loc: (Location, nil) (defaults to: self.closing_loc)
  • unescaped: (String) (defaults to: self.unescaped)

Returns:



31622
31623
31624
# File 'lib/prism/node.rb', line 31622

def copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, content_loc: self.content_loc, closing_loc: self.closing_loc, unescaped: self.unescaped)
  SymbolNode.new(source, node_id, location, flags, opening_loc, content_loc, closing_loc, unescaped)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


31629
31630
31631
31632
31633
31634
31635
31636
31637
31638
31639
31640
31641
31642
31643
31644
31645
# File 'lib/prism/node.rb', line 31629

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location opening_loc content_loc closing_loc unescaped opening content closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :content_loc             then deconstructed[:content_loc] = self.content_loc
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :unescaped               then deconstructed[:unescaped] = self.unescaped
    when :opening                 then deconstructed[:opening] = self.opening
    when :content                 then deconstructed[:content] = self.content
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


31604
31605
31606
31607
# File 'lib/prism/node.rb', line 31604

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

end

#forced_binary_encoding? ⇒ Boolean

:category: Flags internal bytes forced the encoding to binary

: () -> bool

Returns:

  • (Boolean)


31680
31681
31682
# File 'lib/prism/node.rb', line 31680

def forced_binary_encoding?
  flags.anybits?(SymbolFlags::FORCED_BINARY_ENCODING)
end

#forced_us_ascii_encoding? ⇒ Boolean

:category: Flags internal bytes forced the encoding to US-ASCII

: () -> bool

Returns:

  • (Boolean)


31688
31689
31690
# File 'lib/prism/node.rb', line 31688

def forced_us_ascii_encoding?
  flags.anybits?(SymbolFlags::FORCED_US_ASCII_ENCODING)
end

#forced_utf8_encoding? ⇒ Boolean

:category: Flags internal bytes forced the encoding to UTF-8

: () -> bool

Returns:

  • (Boolean)


31672
31673
31674
# File 'lib/prism/node.rb', line 31672

def forced_utf8_encoding?
  flags.anybits?(SymbolFlags::FORCED_UTF8_ENCODING)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


31662
31663
31664
# File 'lib/prism/node.rb', line 31662

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#opening ⇒ String?

:call-seq:

opening -> String | nil

Slice the location of opening_loc from the source.

: () -> String?

Returns:

  • (String, nil)


31786
31787
31788
# File 'lib/prism/node.rb', line 31786

def opening
  opening_loc&.slice
end

#opening_loc ⇒ Location?

:category: Locations :call-seq:

opening_loc -> Location | nil

Returns the Location represented by opening_loc.

: () -> Location?

Returns:



31699
31700
31701
31702
31703
31704
31705
31706
31707
31708
31709
# File 'lib/prism/node.rb', line 31699

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#save_closing_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the closing_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



31765
31766
31767
# File 'lib/prism/node.rb', line 31765

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_content_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the content_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



31737
31738
31739
# File 'lib/prism/node.rb', line 31737

def save_content_loc(repository)
  repository.enter(node_id, :content_loc)
end

#save_opening_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the opening_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



31716
31717
31718
# File 'lib/prism/node.rb', line 31716

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#type ⇒ :symbol_node

See Node#type.

: () -> :symbol_node

Returns:

  • (:symbol_node)


31650
31651
31652
# File 'lib/prism/node.rb', line 31650

def type
  :symbol_node
end

#unescaped ⇒ String

:call-seq:

unescaped -> String

Returns the unescaped attribute.

: () -> String

Returns:

  • (String)


31774
31775
31776
# File 'lib/prism/node.rb', line 31774

def unescaped
  @unescaped
end

#value ⇒ String

: () -> String

Returns:

  • (String)


494
495
496
# File 'lib/prism/node_ext.rb', line 494

def value # :nodoc
  content
end

#value_loc ⇒ Location

: () -> Location

Returns:



499
500
501
# File 'lib/prism/node_ext.rb', line 499

def value_loc # :nodoc
  content_loc
end