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

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

Overview

Immutable position-independent terminal occurrence.

Constant Summary collapse

EMPTY_TRIVIA =

: Array

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.



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_kindObject (readonly)

: Integer?



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

def expected_kind
  @expected_kind
end

#flagsObject (readonly)

: Integer



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

def flags
  @flags
end

#full_widthObject (readonly)

: Integer



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

def full_width
  @full_width
end

#hashObject (readonly)

: Integer



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

def hash
  @hash
end

#kindObject (readonly)

: Integer



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

def kind
  @kind
end

#leadingObject (readonly)

: Array



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

def leading
  @leading
end

#leading_widthObject (readonly)

: Integer



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

def leading_width
  @leading_width
end

#textObject (readonly)

: String



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

def text
  @text
end

#trailingObject (readonly)

: Array



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

def trailing
  @trailing
end

#trailing_widthObject (readonly)

: 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: []) ⇒ Object



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:) ⇒ Object



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) ⇒ Object Also known as: eql?



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_countObject



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

def descendant_count = 1

#to_sourceObject



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