Class: Herb::Token
- Inherits:
-
Object
- Object
- Herb::Token
- 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
-
#location ⇒ Object
readonly
: Location.
-
#range ⇒ Object
readonly
: Range.
-
#type ⇒ Object
readonly
: String.
-
#value ⇒ Object
readonly
: String.
Instance Method Summary collapse
-
#colorize_position(position) ⇒ Object
: (Position) -> String.
-
#colorize_range ⇒ Object
: () -> String.
-
#initialize(value, range, location, type) ⇒ Token
constructor
: (String, Range, Location, String) -> void.
-
#inspect ⇒ Object
: () -> String.
-
#to_hash ⇒ Object
: () -> serialized_token.
-
#to_json(state = nil) ⇒ Object
: (?untyped) -> String.
-
#tree_inspect ⇒ Object
: () -> String.
-
#value_inspect ⇒ Object
: () -> String.
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
#location ⇒ Object (readonly)
: Location
16 17 18 |
# File 'lib/herb/token.rb', line 16 def location @location end |
#range ⇒ Object (readonly)
: Range
15 16 17 |
# File 'lib/herb/token.rb', line 15 def range @range end |
#type ⇒ Object (readonly)
: String
17 18 19 |
# File 'lib/herb/token.rb', line 17 def type @type end |
#value ⇒ Object (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_range ⇒ Object
: () -> 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 |
#inspect ⇒ Object
: () -> 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_hash ⇒ Object
: () -> 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_inspect ⇒ Object
: () -> 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_inspect ⇒ Object
: () -> 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 |