Class: Fmt::Token

Inherits:
Object
  • Object
show all
Includes:
Matchable
Defined in:
lib/fmt/token.rb

Overview

Convenience wrapper for Ripper tokens

Examples:

Ripper Token

[[lineno, column], type, token, state]
[[Integer, Integer], Symbol, String, Object]

See Also:

Instance Attribute Summary collapse

Pattern Matching Support collapse

Helpers collapse

Instance Method Summary collapse

Constructor Details

#initialize(ripper_token) ⇒ Token

Constructor



20
21
22
23
24
25
26
27
28
29
# File 'lib/fmt/token.rb', line 20

def initialize(ripper_token)
  (lineno, column), type, token, state = ripper_token
  @ripper_token = ripper_token
  @lineno = lineno
  @column = column
  @type = type.to_s.delete_prefix("on_").to_sym # strip Ripper's "on_" prefix for parser semantics
  @token = token
  @state = state
  freeze
end

Instance Attribute Details

#columnObject (readonly)

: Integer



33
34
35
# File 'lib/fmt/token.rb', line 33

def column
  @column
end

#linenoObject (readonly)

: Integer



32
33
34
# File 'lib/fmt/token.rb', line 32

def lineno
  @lineno
end

#ripper_tokenObject (readonly) Also known as: deconstruct

: Array[[Integer, Integer], Symbol, String, Object]



31
32
33
# File 'lib/fmt/token.rb', line 31

def ripper_token
  @ripper_token
end

#stateObject (readonly)

: Object



36
37
38
# File 'lib/fmt/token.rb', line 36

def state
  @state
end

#tokenObject (readonly) Also known as: value

: String



35
36
37
# File 'lib/fmt/token.rb', line 35

def token
  @token
end

#typeObject (readonly)

: Symbol



34
35
36
# File 'lib/fmt/token.rb', line 34

def type
  @type
end

Instance Method Details

#arguments_finish?Boolean

Indicates if the token is a right paren (i.e. end of arguments)

Returns:

  • (Boolean)


79
80
81
# File 'lib/fmt/token.rb', line 79

def arguments_finish?
  type == :rparen
end

#arguments_start?Boolean

Indicates if the token is a left paren (i.e. start of arguments)

Returns:

  • (Boolean)


73
74
75
# File 'lib/fmt/token.rb', line 73

def arguments_start?
  type == :lparen
end

#deconstruct_keys(keys = []) ⇒ Object

Returns a Hash representation of the token limited to the given keys



63
64
65
# File 'lib/fmt/token.rb', line 63

def deconstruct_keys(keys = [])
  to_h.select { _1 in ^keys }
end

#identifier?Boolean

Indicates if the token is an identifier (e.g. method name, format specifier, variable name, etc.)

Returns:

  • (Boolean)


97
98
99
# File 'lib/fmt/token.rb', line 97

def identifier?
  type == :ident
end

#key_finish?Boolean

Indicates if the token finishes a key (string formatting named parameter)

Returns:

  • (Boolean)


91
92
93
# File 'lib/fmt/token.rb', line 91

def key_finish?
  type == :rbrace || (type == :op && value == ">")
end

#key_start?Boolean

Indicates if the token starts a key (string formatting named parameter)

Returns:

  • (Boolean)


85
86
87
# File 'lib/fmt/token.rb', line 85

def key_start?
  type == :lbrace || (type == :op && value == "<")
end

#method_name?Boolean

Indicates if the token is a method name (i.e. method name or operator)

Returns:

  • (Boolean)


103
104
105
# File 'lib/fmt/token.rb', line 103

def method_name?
  identifier? || operator?
end

#operator?Boolean

Indicates if the token is an operator

Returns:

  • (Boolean)


109
110
111
# File 'lib/fmt/token.rb', line 109

def operator?
  type == :op
end

#specifier?Boolean

Indicates if the token is a native String format specifier

Returns:

  • (Boolean)

See Also:



122
123
124
# File 'lib/fmt/token.rb', line 122

def specifier?
  identifier? && value in Sigils::FORMAT_SPECIFIERS
end

#to_hObject

Returns a Hash representation of the token



44
45
46
47
48
49
50
51
52
53
# File 'lib/fmt/token.rb', line 44

def to_h
  {
    lineno: lineno,
    column: column,
    type: type,
    token: token,
    value: token,
    state: state
  }
end

#whitespace?Boolean

Indicates if the token is a whitespace

Returns:

  • (Boolean)


115
116
117
# File 'lib/fmt/token.rb', line 115

def whitespace?
  type == :on_sp
end