Class: CSS::Token
- Inherits:
-
Object
- Object
- CSS::Token
- Defined in:
- lib/css/token.rb
Constant Summary collapse
- TYPES =
%i[ ident function at_keyword hash string bad_string url bad_url delim number percentage dimension whitespace cdo cdc comment colon semicolon comma lbracket rbracket lparen rparen lbrace rbrace eof ].freeze
Instance Attribute Summary collapse
-
#flag ⇒ Object
readonly
Returns the value of attribute flag.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Position is intentionally excluded from equality so that hand-built tokens compare equal to parsed tokens.
-
#assign_position!(pos) ⇒ Object
Mutating: assigns the token’s source position and returns self.
- #comment? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(type, value = nil, flag: nil, unit: nil, position: nil) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
-
#trivia? ⇒ Boolean
True for tokens that don’t carry semantic content — used by the parser to skip insignificant tokens between meaningful ones.
- #whitespace? ⇒ Boolean
Constructor Details
#initialize(type, value = nil, flag: nil, unit: nil, position: nil) ⇒ Token
Returns a new instance of Token.
22 23 24 25 26 27 28 29 30 |
# File 'lib/css/token.rb', line 22 def initialize(type, value = nil, flag: nil, unit: nil, position: nil) raise ArgumentError, "unknown token type: #{type.inspect}" unless TYPES.include?(type) @type = type @value = value @flag = flag @unit = unit @position = position end |
Instance Attribute Details
#flag ⇒ Object (readonly)
Returns the value of attribute flag.
20 21 22 |
# File 'lib/css/token.rb', line 20 def flag @flag end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
20 21 22 |
# File 'lib/css/token.rb', line 20 def position @position end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
20 21 22 |
# File 'lib/css/token.rb', line 20 def type @type end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
20 21 22 |
# File 'lib/css/token.rb', line 20 def unit @unit end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
20 21 22 |
# File 'lib/css/token.rb', line 20 def value @value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Position is intentionally excluded from equality so that hand-built tokens compare equal to parsed tokens.
34 35 36 37 38 39 40 |
# File 'lib/css/token.rb', line 34 def ==(other) other.is_a?(Token) && other.type == type && other.value == value && other.flag == flag && other.unit == unit end |
#assign_position!(pos) ⇒ Object
Mutating: assigns the token’s source position and returns self. Used by the tokenizer so each token requires only a single allocation.
63 64 65 66 |
# File 'lib/css/token.rb', line 63 def assign_position!(pos) @position = pos self end |
#comment? ⇒ Boolean
51 52 53 |
# File 'lib/css/token.rb', line 51 def comment? type == :comment end |
#hash ⇒ Object
43 44 45 |
# File 'lib/css/token.rb', line 43 def hash [type, value, flag, unit].hash end |
#inspect ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/css/token.rb', line 68 def inspect parts = ["type=#{type.inspect}"] parts << "value=#{value.inspect}" unless value.nil? parts << "flag=#{flag.inspect}" unless flag.nil? parts << "unit=#{unit.inspect}" unless unit.nil? parts << "@#{position}" unless position.nil? "#<CSS::Token #{parts.join(' ')}>" end |
#trivia? ⇒ Boolean
True for tokens that don’t carry semantic content — used by the parser to skip insignificant tokens between meaningful ones.
57 58 59 |
# File 'lib/css/token.rb', line 57 def trivia? type == :whitespace || type == :comment end |
#whitespace? ⇒ Boolean
47 48 49 |
# File 'lib/css/token.rb', line 47 def whitespace? type == :whitespace end |