Class: Herb::Token

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/token.rb,
ext/herb/extension.c

Overview

: type serialized_token = { | value: String, | range: serialized_range?, | location: serialized_location?, | type: String | }

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Constructor Details

#initialize(value, range, location, type) ⇒ Token

: (String, Range, Location, String) -> void



20
21
22
23
24
25
# File 'lib/herb/token.rb', line 20

def initialize(value, range, location, type)
  @value = value
  @range = range
  @location = location
  @type = type
end

Instance Attribute Details

#locationObject (readonly)

: Location



16
17
18
# File 'lib/herb/token.rb', line 16

def location
  @location
end

#rangeObject (readonly)

: Range



15
16
17
# File 'lib/herb/token.rb', line 15

def range
  @range
end

#typeObject (readonly)

: String



17
18
19
# File 'lib/herb/token.rb', line 17

def type
  @type
end

#valueObject (readonly)

: String



14
15
16
# File 'lib/herb/token.rb', line 14

def value
  @value
end

Instance Method Details

#colorize_position(position) ⇒ Object

: (Position) -> String



62
63
64
# File 'lib/herb/token.rb', line 62

def colorize_position(position)
  white("(") + cyan(position.line.to_s) + white(":") + cyan(position.column.to_s) + white(")")
end

#colorize_rangeObject

: () -> String



57
58
59
# File 'lib/herb/token.rb', line 57

def colorize_range
  white("[") + cyan(range.from.to_s) + white(", ") + cyan(range.to.to_s) + white("]")
end

#inspectObject

: () -> String



67
68
69
# File 'lib/herb/token.rb', line 67

def inspect
  "#{white("#<")}#{bold(yellow("Herb::Token"))} #{white("type=")}#{bright_magenta("\"#{type}\"")} #{white("value=")}#{green(value_inspect)} #{white("range=")}#{colorize_range} #{white("start=")}#{colorize_position(location.start)} #{white("end=")}#{colorize_position(location.end)}#{white(">")}"
end

#to_hashObject

: () -> serialized_token



28
29
30
31
32
33
34
35
# File 'lib/herb/token.rb', line 28

def to_hash
  {
    value: value,
    range: range&.to_a,
    location: location&.to_hash,
    type: type,
  }
end

#to_json(state = nil) ⇒ Object

: (?untyped) -> String



38
39
40
# File 'lib/herb/token.rb', line 38

def to_json(state = nil)
  to_hash.to_json(state)
end

#tree_inspectObject

: () -> String



43
44
45
# File 'lib/herb/token.rb', line 43

def tree_inspect
  "#{green("\"#{value.force_encoding("utf-8")}\"")} #{dimmed("(location: #{location.tree_inspect})")}"
end

#value_inspectObject

: () -> String



48
49
50
51
52
53
54
# File 'lib/herb/token.rb', line 48

def value_inspect
  if type == "TOKEN_EOF"
    "<EOF>".inspect
  else
    value.inspect
  end
end