Class: Prism::MatchLastLineNode

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

Overview

Represents a regular expression literal used in the predicate of a conditional to implicitly match against the last line read by an IO object.

if /foo/i then end
   ^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



9406
9407
9408
9409
9410
9411
9412
9413
# File 'lib/prism/node.rb', line 9406

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



9397
9398
9399
# File 'lib/prism/node.rb', line 9397

def closing_loc
  @closing_loc
end

#content_locObject (readonly)

attr_reader content_loc: Location



9394
9395
9396
# File 'lib/prism/node.rb', line 9394

def content_loc
  @content_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



9391
9392
9393
# File 'lib/prism/node.rb', line 9391

def opening_loc
  @opening_loc
end

#unescapedObject (readonly)

attr_reader unescaped: String



9400
9401
9402
# File 'lib/prism/node.rb', line 9400

def unescaped
  @unescaped
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



9416
9417
9418
# File 'lib/prism/node.rb', line 9416

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

#ascii_8bit?Boolean

def ascii_8bit?: () -> bool

Returns:

  • (Boolean)


9491
9492
9493
# File 'lib/prism/node.rb', line 9491

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

#child_nodesObject Also known as: deconstruct

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



9421
9422
9423
# File 'lib/prism/node.rb', line 9421

def child_nodes
  []
end

#closingObject

def closing: () -> String



9466
9467
9468
# File 'lib/prism/node.rb', line 9466

def closing
  closing_loc.slice
end

#comment_targetsObject

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



9431
9432
9433
# File 'lib/prism/node.rb', line 9431

def comment_targets
  [opening_loc, content_loc, closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



9426
9427
9428
# File 'lib/prism/node.rb', line 9426

def compact_child_nodes
  []
end

#contentObject

def content: () -> String



9461
9462
9463
# File 'lib/prism/node.rb', line 9461

def content
  content_loc.slice
end

#copy(**params) ⇒ Object

def copy: (**params) -> MatchLastLineNode



9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
# File 'lib/prism/node.rb', line 9436

def copy(**params)
  MatchLastLineNode.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]



9451
9452
9453
# File 'lib/prism/node.rb', line 9451

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

Returns:

  • (Boolean)


9486
9487
9488
# File 'lib/prism/node.rb', line 9486

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

#extended?Boolean

def extended?: () -> bool

Returns:

  • (Boolean)


9476
9477
9478
# File 'lib/prism/node.rb', line 9476

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

#ignore_case?Boolean

def ignore_case?: () -> bool

Returns:

  • (Boolean)


9471
9472
9473
# File 'lib/prism/node.rb', line 9471

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

#inspect(inspector = NodeInspector.new) ⇒ Object



9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
# File 'lib/prism/node.rb', line 9510

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

Returns:

  • (Boolean)


9481
9482
9483
# File 'lib/prism/node.rb', line 9481

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

#once?Boolean

def once?: () -> bool

Returns:

  • (Boolean)


9506
9507
9508
# File 'lib/prism/node.rb', line 9506

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

#openingObject

def opening: () -> String



9456
9457
9458
# File 'lib/prism/node.rb', line 9456

def opening
  opening_loc.slice
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



9535
9536
9537
# File 'lib/prism/node.rb', line 9535

def type
  :match_last_line_node
end

#utf_8?Boolean

def utf_8?: () -> bool

Returns:

  • (Boolean)


9501
9502
9503
# File 'lib/prism/node.rb', line 9501

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

#windows_31j?Boolean

def windows_31j?: () -> bool

Returns:

  • (Boolean)


9496
9497
9498
# File 'lib/prism/node.rb', line 9496

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