Class: Rubycc::Preprocess::PPToken

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycc/preprocess/pp_token.rb

Overview

A preprocessing token (ISO C 6.4). Unlike a Front::Token it holds only the source spelling (text), deferring interpretation of numbers, literals and keywords to the conversion that ends translation phase 4. type is one of:

:identifier  an identifier or keyword spelling
:pp_number   a preprocessing number (6.4.8), broader than a C constant
:string      a string literal, quotes and escapes still verbatim
:char        a character constant, quotes and escapes still verbatim
:punct       a punctuator (including the pp-only "#" and "##")
:newline     an end-of-line marker kept so directives stay line-oriented
:other       any single character matching no other category (6.4p1)
:eof         the end of the translation unit

The location fields follow the same 1-based line/column convention as Front::Token, and point at the token's first character on its physical line (a token spliced across a line continuation keeps its start line). space_before records whether whitespace or a comment preceded the token, which the directive layer consults to classify a macro definition. suppress is the token's own set of macro names it must not be re-expanded as (6.10.3.4, "painted blue"): a frozen list carried along every copy, so each token remembers the expansions it was already born from.

Constant Summary collapse

NO_SUPPRESS =

The suppression set of a token that came straight from source and has yet to pass through any macro; shared so unpainted tokens cost no allocation.

[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, text:, filename:, line:, column:, source_line:, space_before: false, suppress: NO_SUPPRESS) ⇒ PPToken

Returns a new instance of PPToken.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubycc/preprocess/pp_token.rb', line 33

def initialize(type:, text:, filename:, line:, column:, source_line:, space_before: false,
               suppress: NO_SUPPRESS)
  @type = type
  @text = text
  @filename = filename
  @line = line
  @column = column
  @source_line = source_line
  @space_before = space_before
  @suppress = suppress
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def column
  @column
end

#filenameObject (readonly)

Returns the value of attribute filename.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def filename
  @filename
end

#lineObject (readonly)

Returns the value of attribute line.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def line
  @line
end

#source_lineObject (readonly)

Returns the value of attribute source_line.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def source_line
  @source_line
end

#space_beforeObject (readonly)

Returns the value of attribute space_before.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def space_before
  @space_before
end

#suppressObject (readonly)

Returns the value of attribute suppress.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def suppress
  @suppress
end

#textObject (readonly)

Returns the value of attribute text.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



31
32
33
# File 'lib/rubycc/preprocess/pp_token.rb', line 31

def type
  @type
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rubycc/preprocess/pp_token.rb', line 49

def eof?
  @type == :eof
end

#inspectObject



57
58
59
# File 'lib/rubycc/preprocess/pp_token.rb', line 57

def inspect
  "#<PPToken #{type} #{text.inspect} @#{line}:#{column}>"
end

#newline?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rubycc/preprocess/pp_token.rb', line 45

def newline?
  @type == :newline
end

#punct?(str) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rubycc/preprocess/pp_token.rb', line 53

def punct?(str)
  @type == :punct && @text == str
end