Class: Fmt::Token
Overview
Convenience wrapper for Ripper tokens
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
: Integer.
-
#lineno ⇒ Object
readonly
: Integer.
-
#ripper_token ⇒ Object
(also: #deconstruct)
readonly
: Array[[Integer, Integer], Symbol, String, Object].
-
#state ⇒ Object
readonly
: Object.
-
#token ⇒ Object
(also: #value)
readonly
: String.
-
#type ⇒ Object
readonly
: Symbol.
Pattern Matching Support collapse
-
#deconstruct_keys(keys = []) ⇒ Object
Returns a Hash representation of the token limited to the given keys.
Helpers collapse
-
#arguments_finish? ⇒ Boolean
Indicates if the token is a right paren (i.e. end of arguments).
-
#arguments_start? ⇒ Boolean
Indicates if the token is a left paren (i.e. start of arguments).
-
#identifier? ⇒ Boolean
Indicates if the token is an identifier (e.g. method name, format specifier, variable name, etc.).
-
#key_finish? ⇒ Boolean
Indicates if the token finishes a key (string formatting named parameter).
-
#key_start? ⇒ Boolean
Indicates if the token starts a key (string formatting named parameter).
-
#method_name? ⇒ Boolean
Indicates if the token is a method name (i.e. method name or operator).
-
#operator? ⇒ Boolean
Indicates if the token is an operator.
-
#specifier? ⇒ Boolean
Indicates if the token is a native String format specifier.
-
#whitespace? ⇒ Boolean
Indicates if the token is a whitespace.
Instance Method Summary collapse
-
#initialize(ripper_token) ⇒ Token
constructor
Constructor.
-
#to_h ⇒ Object
Returns a Hash representation of the token.
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
#column ⇒ Object (readonly)
: Integer
33 34 35 |
# File 'lib/fmt/token.rb', line 33 def column @column end |
#lineno ⇒ Object (readonly)
: Integer
32 33 34 |
# File 'lib/fmt/token.rb', line 32 def lineno @lineno end |
#ripper_token ⇒ Object (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 |
#state ⇒ Object (readonly)
: Object
36 37 38 |
# File 'lib/fmt/token.rb', line 36 def state @state end |
#token ⇒ Object (readonly) Also known as: value
: String
35 36 37 |
# File 'lib/fmt/token.rb', line 35 def token @token end |
#type ⇒ Object (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)
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)
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.)
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)
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)
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)
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
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
122 123 124 |
# File 'lib/fmt/token.rb', line 122 def specifier? identifier? && value in Sigils::FORMAT_SPECIFIERS end |
#to_h ⇒ Object
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
115 116 117 |
# File 'lib/fmt/token.rb', line 115 def whitespace? type == :on_sp end |