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

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Immutable position-independent terminal occurrence.

Constant Summary collapse

EMPTY_TRIVIA =

Signature:

  • Array[GreenTrivia]

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



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/json5/generated_parser.rb', line 341

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)

Signature:

  • Integer?



336
337
338
# File 'lib/json5/generated_parser.rb', line 336

def expected_kind
  @expected_kind
end

#flagsObject (readonly)

Signature:

  • Integer



332
333
334
# File 'lib/json5/generated_parser.rb', line 332

def flags
  @flags
end

#full_widthObject (readonly)

Signature:

  • Integer



333
334
335
# File 'lib/json5/generated_parser.rb', line 333

def full_width
  @full_width
end

#hashObject (readonly)

Signature:

  • Integer



337
338
339
# File 'lib/json5/generated_parser.rb', line 337

def hash
  @hash
end

#kindObject (readonly)

Signature:

  • Integer



328
329
330
# File 'lib/json5/generated_parser.rb', line 328

def kind
  @kind
end

#leadingObject (readonly)

Signature:

  • Array[GreenTrivia]



330
331
332
# File 'lib/json5/generated_parser.rb', line 330

def leading
  @leading
end

#leading_widthObject (readonly)

Signature:

  • Integer



334
335
336
# File 'lib/json5/generated_parser.rb', line 334

def leading_width
  @leading_width
end

#textObject (readonly)

Signature:

  • String



329
330
331
# File 'lib/json5/generated_parser.rb', line 329

def text
  @text
end

#trailingObject (readonly)

Signature:

  • Array[GreenTrivia]



331
332
333
# File 'lib/json5/generated_parser.rb', line 331

def trailing
  @trailing
end

#trailing_widthObject (readonly)

Signature:

  • Integer



335
336
337
# File 'lib/json5/generated_parser.rb', line 335

def trailing_width
  @trailing_width
end

Class Method Details

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

RBS:

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



366
367
368
# File 'lib/json5/generated_parser.rb', line 366

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

.missing(kind:, expected_kind:) ⇒ Object

RBS:

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



358
359
360
361
362
363
# File 'lib/json5/generated_parser.rb', line 358

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?

RBS:

  • (Object? other) -> bool



383
384
385
386
387
388
389
390
391
# File 'lib/json5/generated_parser.rb', line 383

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

RBS:

  • () -> Integer



371
# File 'lib/json5/generated_parser.rb', line 371

def descendant_count = 1

#to_sourceObject

RBS:

  • () -> String



374
375
376
377
378
379
380
# File 'lib/json5/generated_parser.rb', line 374

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