Class: Rubycc::Front::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycc/front/token.rb

Overview

A single lexical token. type is one of :num, :float, :ident, :keyword, :punct, :string, :eof. value is an Integer for :num, a Ruby Float for :float (a floating constant), an ASCII-8BIT String of the escape-resolved bytes for :string, a String for :ident/:keyword/:punct, and nil for :eof. base (10/8/16) and suffix accompany a numeric constant so the parser can fix its type: an integer :num carries a base and a normalized u/l suffix run ("", "u", "ul") per 6.4.4.1, while a :float carries no base but a normalized floating suffix ("" for double, "f" for float, "l" for long double, itself treated as double). Both are nil on every other token (a character constant is a :num with base 10 and no suffix). The remaining fields locate the token in the source for diagnostics.

Constant Summary collapse

TYPES =
%i[num float ident keyword punct string eof].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, value:, filename:, line:, column:, source_line:, base: nil, suffix: nil) ⇒ Token

Returns a new instance of Token.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rubycc/front/token.rb', line 21

def initialize(type:, value:, filename:, line:, column:, source_line:, base: nil, suffix: nil)
  @type = type
  @value = value
  @filename = filename
  @line = line
  @column = column
  @source_line = source_line
  @base = base
  @suffix = suffix
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def base
  @base
end

#columnObject (readonly)

Returns the value of attribute column.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def column
  @column
end

#filenameObject (readonly)

Returns the value of attribute filename.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def filename
  @filename
end

#lineObject (readonly)

Returns the value of attribute line.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def line
  @line
end

#source_lineObject (readonly)

Returns the value of attribute source_line.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def source_line
  @source_line
end

#suffixObject (readonly)

Returns the value of attribute suffix.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def suffix
  @suffix
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



19
20
21
# File 'lib/rubycc/front/token.rb', line 19

def value
  @value
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubycc/front/token.rb', line 40

def eof?
  type == :eof
end

#inspectObject



44
45
46
# File 'lib/rubycc/front/token.rb', line 44

def inspect
  "#<Token #{type} #{value.inspect} @#{line}:#{column}>"
end

#keyword?(str) ⇒ Boolean

Returns:

  • (Boolean)


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

def keyword?(str)
  type == :keyword && value == str
end

#punct?(str) ⇒ Boolean

Returns:

  • (Boolean)


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

def punct?(str)
  type == :punct && value == str
end