Class: Ibex::Runtime::CST::GreenToken

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/green/token.rb,
sig/ibex/runtime/cst/green/token.rbs

Overview

Immutable position-independent terminal occurrence.

Constant Summary collapse

EMPTY_TRIVIA =

Returns:

empty_trivia.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, text:, leading: EMPTY_TRIVIA, trailing: EMPTY_TRIVIA, flags: 0, expected_kind: nil) ⇒ GreenToken

Returns a new instance of GreenToken.

RBS:

  • (kind: Integer, text: String, ?leading: Array[GreenTrivia], ?trailing: Array[GreenTrivia], ?flags: Integer, ?expected_kind: Integer?) -> void

Parameters:

  • kind: (Integer)
  • text: (String)
  • leading: (Array[GreenTrivia]) (defaults to: EMPTY_TRIVIA)
  • trailing: (Array[GreenTrivia]) (defaults to: EMPTY_TRIVIA)
  • flags: (Integer) (defaults to: 0)
  • expected_kind: (Integer, nil) (defaults to: nil)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ibex/runtime/cst/green/token.rb', line 28

def initialize(
  kind:, text:, leading: EMPTY_TRIVIA, trailing: EMPTY_TRIVIA, flags: 0, expected_kind: nil
)
  @kind = kind
  @text = text.encoding == Encoding::BINARY && text.frozen? ? text : text.b.freeze
  @leading = leading.frozen? ? leading : leading.dup.freeze
  @trailing = trailing.frozen? ? trailing : trailing.dup.freeze
  @flags = flags
  @expected_kind = expected_kind
  @leading_width = trivia_width(@leading)
  @trailing_width = trivia_width(@trailing)
  @full_width = @leading_width + @text.bytesize + @trailing_width
  @hash = @kind.hash ^ @text.hash ^ @leading.hash ^ @trailing.hash ^ @flags.hash ^ @expected_kind.hash
  freeze
end

Instance Attribute Details

#expected_kindInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


23
24
25
# File 'lib/ibex/runtime/cst/green/token.rb', line 23

def expected_kind
  @expected_kind
end

#flagsInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


19
20
21
# File 'lib/ibex/runtime/cst/green/token.rb', line 19

def flags
  @flags
end

#full_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


20
21
22
# File 'lib/ibex/runtime/cst/green/token.rb', line 20

def full_width
  @full_width
end

#hashInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


24
25
26
# File 'lib/ibex/runtime/cst/green/token.rb', line 24

def hash
  @hash
end

#kindInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


15
16
17
# File 'lib/ibex/runtime/cst/green/token.rb', line 15

def kind
  @kind
end

#leadingArray[GreenTrivia] (readonly)

Signature:

  • Array[GreenTrivia]

Returns:



17
18
19
# File 'lib/ibex/runtime/cst/green/token.rb', line 17

def leading
  @leading
end

#leading_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


21
22
23
# File 'lib/ibex/runtime/cst/green/token.rb', line 21

def leading_width
  @leading_width
end

#textString (readonly)

Signature:

  • String

Returns:

  • (String)


16
17
18
# File 'lib/ibex/runtime/cst/green/token.rb', line 16

def text
  @text
end

#trailingArray[GreenTrivia] (readonly)

Signature:

  • Array[GreenTrivia]

Returns:



18
19
20
# File 'lib/ibex/runtime/cst/green/token.rb', line 18

def trailing
  @trailing
end

#trailing_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


22
23
24
# File 'lib/ibex/runtime/cst/green/token.rb', line 22

def trailing_width
  @trailing_width
end

Class Method Details

.lexical_error(kind:, text:, leading: []) ⇒ GreenToken

RBS:

  • (kind: Integer, text: String, ?leading: Array[GreenTrivia]) -> GreenToken

Parameters:

  • kind: (Integer)
  • text: (String)
  • leading: (Array[GreenTrivia]) (defaults to: [])

Returns:



53
54
55
# File 'lib/ibex/runtime/cst/green/token.rb', line 53

def self.lexical_error(kind:, text:, leading: [])
  new(kind: kind, text: text, leading: leading, flags: Flags::CONTAINS_ERROR)
end

.missing(kind:, expected_kind:) ⇒ GreenToken

RBS:

  • (kind: Integer, expected_kind: Integer) -> GreenToken

Parameters:

  • kind: (Integer)
  • expected_kind: (Integer)

Returns:



45
46
47
48
49
50
# File 'lib/ibex/runtime/cst/green/token.rb', line 45

def self.missing(kind:, expected_kind:)
  new(
    kind: kind, text: "", expected_kind: expected_kind,
    flags: Flags::CONTAINS_MISSING | Flags::SYNTHETIC
  )
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

RBS:

  • (untyped other) -> bool

Parameters:

  • other (Object)

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/ibex/runtime/cst/green/token.rb', line 70

def ==(other)
  other.is_a?(GreenToken) &&
    @kind == other.kind &&
    @text == other.text &&
    @leading == other.leading &&
    @trailing == other.trailing &&
    @flags == other.flags &&
    @expected_kind == other.expected_kind
end

#descendant_countInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


58
# File 'lib/ibex/runtime/cst/green/token.rb', line 58

def descendant_count = 1

#to_sourceString

RBS:

  • () -> String

Returns:

  • (String)


61
62
63
64
65
66
67
# File 'lib/ibex/runtime/cst/green/token.rb', line 61

def to_source
  source = String.new(encoding: Encoding::BINARY)
  @leading.each { |trivia| source << trivia.text }
  source << @text
  @trailing.each { |trivia| source << trivia.text }
  source
end

#trivia_width(trivia) ⇒ Integer

RBS:

  • (Array[GreenTrivia] trivia) -> Integer

Parameters:

Returns:

  • (Integer)


85
86
87
# File 'lib/ibex/runtime/cst/green/token.rb', line 85

def trivia_width(trivia)
  trivia.sum(&:full_width)
end