Class: Prism::InterpolatedRegularExpressionNode

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

Overview

Represents a regular expression literal that contains interpolation.

/foo #{bar} baz/
^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opening_loc, parts, closing_loc, flags, location) ⇒ InterpolatedRegularExpressionNode

def initialize: (opening_loc: Location, parts: Array, closing_loc: Location, flags: Integer, location: Location) -> void



7961
7962
7963
7964
7965
7966
7967
# File 'lib/prism/node.rb', line 7961

def initialize(opening_loc, parts, closing_loc, flags, location)
  @opening_loc = opening_loc
  @parts = parts
  @closing_loc = closing_loc
  @flags = flags
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location



7955
7956
7957
# File 'lib/prism/node.rb', line 7955

def closing_loc
  @closing_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



7949
7950
7951
# File 'lib/prism/node.rb', line 7949

def opening_loc
  @opening_loc
end

#partsObject (readonly)

attr_reader parts: Array



7952
7953
7954
# File 'lib/prism/node.rb', line 7952

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



7970
7971
7972
# File 'lib/prism/node.rb', line 7970

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

#ascii_8bit?Boolean

def ascii_8bit?: () -> bool

Returns:

  • (Boolean)


8044
8045
8046
# File 'lib/prism/node.rb', line 8044

def ascii_8bit?
  flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



7980
7981
7982
# File 'lib/prism/node.rb', line 7980

def child_nodes
  [*parts]
end

#closingObject

def closing: () -> String



8019
8020
8021
# File 'lib/prism/node.rb', line 8019

def closing
  closing_loc.slice
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



7990
7991
7992
# File 'lib/prism/node.rb', line 7990

def comment_targets
  [opening_loc, *parts, closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7985
7986
7987
# File 'lib/prism/node.rb', line 7985

def compact_child_nodes
  [*parts]
end

#copy(**params) ⇒ Object

def copy: (**params) -> InterpolatedRegularExpressionNode



7995
7996
7997
7998
7999
8000
8001
8002
8003
# File 'lib/prism/node.rb', line 7995

def copy(**params)
  InterpolatedRegularExpressionNode.new(
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:parts) { parts },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:flags) { flags },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



8009
8010
8011
# File 'lib/prism/node.rb', line 8009

def deconstruct_keys(keys)
  { opening_loc: opening_loc, parts: parts, closing_loc: closing_loc, flags: flags, location: location }
end

#euc_jp?Boolean

def euc_jp?: () -> bool

Returns:

  • (Boolean)


8039
8040
8041
# File 'lib/prism/node.rb', line 8039

def euc_jp?
  flags.anybits?(RegularExpressionFlags::EUC_JP)
end

#extended?Boolean

def extended?: () -> bool

Returns:

  • (Boolean)


8029
8030
8031
# File 'lib/prism/node.rb', line 8029

def extended?
  flags.anybits?(RegularExpressionFlags::EXTENDED)
end

#ignore_case?Boolean

def ignore_case?: () -> bool

Returns:

  • (Boolean)


8024
8025
8026
# File 'lib/prism/node.rb', line 8024

def ignore_case?
  flags.anybits?(RegularExpressionFlags::IGNORE_CASE)
end

#inspect(inspector = NodeInspector.new) ⇒ Object



8063
8064
8065
8066
8067
8068
8069
8070
8071
# File 'lib/prism/node.rb', line 8063

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "├── parts: #{inspector.list("#{inspector.prefix}", parts)}"
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  flags = [("ignore_case" if ignore_case?), ("extended" if extended?), ("multi_line" if multi_line?), ("euc_jp" if euc_jp?), ("ascii_8bit" if ascii_8bit?), ("windows_31j" if windows_31j?), ("utf_8" if utf_8?), ("once" if once?)].compact
  inspector << "└── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector.to_str
end

#multi_line?Boolean

def multi_line?: () -> bool

Returns:

  • (Boolean)


8034
8035
8036
# File 'lib/prism/node.rb', line 8034

def multi_line?
  flags.anybits?(RegularExpressionFlags::MULTI_LINE)
end

#once?Boolean

def once?: () -> bool

Returns:

  • (Boolean)


8059
8060
8061
# File 'lib/prism/node.rb', line 8059

def once?
  flags.anybits?(RegularExpressionFlags::ONCE)
end

#openingObject

def opening: () -> String



8014
8015
8016
# File 'lib/prism/node.rb', line 8014

def opening
  opening_loc.slice
end

#optionsObject

Returns a numeric value that represents the flags that were used to create the regular expression.



30
31
32
33
34
35
# File 'lib/prism/node_ext.rb', line 30

def options
  o = flags & (RegularExpressionFlags::IGNORE_CASE | RegularExpressionFlags::EXTENDED | RegularExpressionFlags::MULTI_LINE)
  o |= Regexp::FIXEDENCODING if flags.anybits?(RegularExpressionFlags::EUC_JP | RegularExpressionFlags::WINDOWS_31J | RegularExpressionFlags::UTF_8)
  o |= Regexp::NOENCODING if flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
  o
end

#set_newline_flag(newline_marked) ⇒ Object



7974
7975
7976
7977
# File 'lib/prism/node.rb', line 7974

def set_newline_flag(newline_marked)
  first = parts.first
  first.set_newline_flag(newline_marked) if first
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



8087
8088
8089
# File 'lib/prism/node.rb', line 8087

def type
  :interpolated_regular_expression_node
end

#utf_8?Boolean

def utf_8?: () -> bool

Returns:

  • (Boolean)


8054
8055
8056
# File 'lib/prism/node.rb', line 8054

def utf_8?
  flags.anybits?(RegularExpressionFlags::UTF_8)
end

#windows_31j?Boolean

def windows_31j?: () -> bool

Returns:

  • (Boolean)


8049
8050
8051
# File 'lib/prism/node.rb', line 8049

def windows_31j?
  flags.anybits?(RegularExpressionFlags::WINDOWS_31J)
end