Class: CSS::Token

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(type, value = nil, flag: nil, unit: nil, position: nil) ⇒ Token

Returns a new instance of Token.

Raises:

  • (ArgumentError)


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

#flagObject (readonly)

Returns the value of attribute flag.



20
21
22
# File 'lib/css/token.rb', line 20

def flag
  @flag
end

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/css/token.rb', line 20

def type
  @type
end

#unitObject (readonly)

Returns the value of attribute unit.



20
21
22
# File 'lib/css/token.rb', line 20

def unit
  @unit
end

#valueObject (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_source!(start_offset, end_offset, newlines) ⇒ Object

Most tokens never have their ‘position` read after parsing, so the tokenizer plants raw offsets + a shared `@newlines` reference here via this method, and `Token#position` materializes the `Position` Data on first read.



65
66
67
68
69
70
# File 'lib/css/token.rb', line 65

def assign_source!(start_offset, end_offset, newlines)
  @start_offset = start_offset
  @end_offset   = end_offset
  @newlines     = newlines
  self
end

#comment?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/css/token.rb', line 51

def comment?
  type == :comment
end

#hashObject



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

def hash
  [type, value, flag, unit].hash
end

#inspectObject

Reads ‘@position` directly so debug-style introspection doesn’t materialize a ‘Position` as a side effect.



83
84
85
86
87
88
89
90
91
# File 'lib/css/token.rb', line 83

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}"          if @position

  "#<CSS::Token #{parts.join(' ')}>"
end

#positionObject

Returns nil for tokens built without source info (i.e. tokens constructed by hand or via ‘Token.new(:eof)`).



74
75
76
77
78
79
# File 'lib/css/token.rb', line 74

def position
  return @position if @position
  return nil unless instance_variable_defined?(:@start_offset)

  @position = compute_position
end

#trivia?Boolean

True for tokens that don’t carry semantic content — used by the parser to skip insignificant tokens between meaningful ones.

Returns:

  • (Boolean)


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

def trivia?
  type == :whitespace || type == :comment
end

#whitespace?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/css/token.rb', line 47

def whitespace?
  type == :whitespace
end