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

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
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_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

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



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.

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