Class: Prism::RegularExpressionNode

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 with no interpolation.

/foo/i
^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opening_loc, content_loc, closing_loc, unescaped, flags, location) ⇒ RegularExpressionNode

def initialize: (opening_loc: Location, content_loc: Location, closing_loc: Location, unescaped: String, flags: Integer, location: Location) -> void



11773
11774
11775
11776
11777
11778
11779
11780
# File 'lib/prism/node.rb', line 11773

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

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location



11764
11765
11766
# File 'lib/prism/node.rb', line 11764

def closing_loc
  @closing_loc
end

#content_locObject (readonly)

attr_reader content_loc: Location



11761
11762
11763
# File 'lib/prism/node.rb', line 11761

def content_loc
  @content_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



11758
11759
11760
# File 'lib/prism/node.rb', line 11758

def opening_loc
  @opening_loc
end

#unescapedObject (readonly)

attr_reader unescaped: String



11767
11768
11769
# File 'lib/prism/node.rb', line 11767

def unescaped
  @unescaped
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



11783
11784
11785
# File 'lib/prism/node.rb', line 11783

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

#ascii_8bit?Boolean

def ascii_8bit?: () -> bool



11858
11859
11860
# File 'lib/prism/node.rb', line 11858

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

#child_nodesObject Also known as: deconstruct

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



11788
11789
11790
# File 'lib/prism/node.rb', line 11788

def child_nodes
  []
end

#closingObject

def closing: () -> String



11833
11834
11835
# File 'lib/prism/node.rb', line 11833

def closing
  closing_loc.slice
end

#comment_targetsObject

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



11798
11799
11800
# File 'lib/prism/node.rb', line 11798

def comment_targets
  [opening_loc, content_loc, closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



11793
11794
11795
# File 'lib/prism/node.rb', line 11793

def compact_child_nodes
  []
end

#contentObject

def content: () -> String



11828
11829
11830
# File 'lib/prism/node.rb', line 11828

def content
  content_loc.slice
end

#copy(**params) ⇒ Object

def copy: (**params) -> RegularExpressionNode



11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
# File 'lib/prism/node.rb', line 11803

def copy(**params)
  RegularExpressionNode.new(
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:content_loc) { content_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:unescaped) { unescaped },
    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]



11818
11819
11820
# File 'lib/prism/node.rb', line 11818

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

#euc_jp?Boolean

def euc_jp?: () -> bool



11853
11854
11855
# File 'lib/prism/node.rb', line 11853

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

#extended?Boolean

def extended?: () -> bool



11843
11844
11845
# File 'lib/prism/node.rb', line 11843

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

#ignore_case?Boolean

def ignore_case?: () -> bool



11838
11839
11840
# File 'lib/prism/node.rb', line 11838

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

#inspect(inspector = NodeInspector.new) ⇒ Object



11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
# File 'lib/prism/node.rb', line 11877

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "├── content_loc: #{inspector.location(content_loc)}\n"
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector << "├── unescaped: #{unescaped.inspect}\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



11848
11849
11850
# File 'lib/prism/node.rb', line 11848

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

#once?Boolean

def once?: () -> bool



11873
11874
11875
# File 'lib/prism/node.rb', line 11873

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

#openingObject

def opening: () -> String



11823
11824
11825
# File 'lib/prism/node.rb', line 11823

def opening
  opening_loc.slice
end

#optionsObject

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



48
49
50
51
52
53
# File 'lib/prism/node_ext.rb', line 48

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

#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



11902
11903
11904
# File 'lib/prism/node.rb', line 11902

def type
  :regular_expression_node
end

#utf_8?Boolean

def utf_8?: () -> bool



11868
11869
11870
# File 'lib/prism/node.rb', line 11868

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

#windows_31j?Boolean

def windows_31j?: () -> bool



11863
11864
11865
# File 'lib/prism/node.rb', line 11863

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