Class: Ibex::Runtime::CST::GreenToken
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::GreenToken
- 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 =
empty_trivia.freeze
Instance Attribute Summary collapse
- #expected_kind ⇒ Integer? readonly
- #flags ⇒ Integer readonly
- #full_width ⇒ Integer readonly
- #hash ⇒ Integer readonly
- #kind ⇒ Integer readonly
- #leading ⇒ Array[GreenTrivia] readonly
- #leading_width ⇒ Integer readonly
- #text ⇒ String readonly
- #trailing ⇒ Array[GreenTrivia] readonly
- #trailing_width ⇒ Integer readonly
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #descendant_count ⇒ Integer
-
#initialize(kind:, text:, leading: EMPTY_TRIVIA, trailing: EMPTY_TRIVIA, flags: 0, expected_kind: nil) ⇒ GreenToken
constructor
A new instance of GreenToken.
- #to_source ⇒ String
- #trivia_width(trivia) ⇒ Integer
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_kind ⇒ Integer? (readonly)
23 24 25 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 23 def expected_kind @expected_kind end |
#flags ⇒ Integer (readonly)
19 20 21 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 19 def flags @flags end |
#full_width ⇒ Integer (readonly)
20 21 22 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 20 def full_width @full_width end |
#hash ⇒ Integer (readonly)
24 25 26 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 24 def hash @hash end |
#kind ⇒ Integer (readonly)
15 16 17 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 15 def kind @kind end |
#leading ⇒ Array[GreenTrivia] (readonly)
17 18 19 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 17 def leading @leading end |
#leading_width ⇒ Integer (readonly)
21 22 23 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 21 def leading_width @leading_width end |
#text ⇒ String (readonly)
16 17 18 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 16 def text @text end |
#trailing ⇒ Array[GreenTrivia] (readonly)
18 19 20 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 18 def trailing @trailing end |
#trailing_width ⇒ Integer (readonly)
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
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
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?
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_count ⇒ Integer
58 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 58 def descendant_count = 1 |
#to_source ⇒ 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
85 86 87 |
# File 'lib/ibex/runtime/cst/green/token.rb', line 85 def trivia_width(trivia) trivia.sum(&:full_width) end |