Class: Rucc::Token

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

Constant Summary collapse

SPACE_TOKEN =
Token.new(T::SPACE)
NEWLINE_TOKEN =
Token.new(T::NEWLINE)
EOF_TOKEN =
Token.new(T::EOF)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, file: nil, count: nil, line: nil, column: nil, hideset: nil, space: false, bol: nil, id: nil, sval: nil, c: nil, enc: nil, is_vararg: nil, position: nil) ⇒ Token

Returns a new instance of Token.

Parameters:

  • kind (T)
  • file (FileIO) (defaults to: nil)
  • count (Integer) (defaults to: nil)
  • hideset (Set) (defaults to: nil)
  • id (String, Keyword, Op) (defaults to: nil)
  • sval (String) (defaults to: nil)
  • c (Integer) (defaults to: nil)
  • enc (ENC) (defaults to: nil)
  • is_vararg (Boolean) (defaults to: nil)
  • position (Integer) (defaults to: nil)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rucc/token.rb', line 18

def initialize(kind,
               file: nil,
               count: nil,             # Token number in a file, counting from 0
               line: nil, column: nil, # Token location in a file
               hideset: nil,           # used by the preprocessor for macro expansion
               space: false,           # true if the token has a leading space
               bol: nil,               # true if the token is at the beginning of a line
               id: nil,                           # KEYWORD
               sval: nil,      c: nil, enc: nil,  # STRING or CHAR
               is_vararg: nil, position: nil      # MACRO_PARAM
              )
  @kind = kind
  @file = file
  @count = count
  @line = line; @column = column
  @hideset = hideset
  @space = space
  @bol = bol

  # value
  @id = id                                      # KEYWORD
  @sval = sval;           @c = c; @enc = enc;   # STRING or CHAR
  @is_vararg = is_vararg; @position = position  # MACRO_PARAM

  # Flag representing completion of macro expansion
  @expanded = false
end

Instance Attribute Details

#bolObject

Returns the value of attribute bol.



45
46
47
# File 'lib/rucc/token.rb', line 45

def bol
  @bol
end

#cObject (readonly)

Returns the value of attribute c.



45
46
47
# File 'lib/rucc/token.rb', line 45

def c
  @c
end

#columnObject

Returns the value of attribute column.



45
46
47
# File 'lib/rucc/token.rb', line 45

def column
  @column
end

#countObject (readonly)

Returns the value of attribute count.



45
46
47
# File 'lib/rucc/token.rb', line 45

def count
  @count
end

#encObject

Returns the value of attribute enc.



45
46
47
# File 'lib/rucc/token.rb', line 45

def enc
  @enc
end

#expandedObject

Returns the value of attribute expanded.



45
46
47
# File 'lib/rucc/token.rb', line 45

def expanded
  @expanded
end

#fileObject (readonly)

Returns the value of attribute file.



45
46
47
# File 'lib/rucc/token.rb', line 45

def file
  @file
end

#hidesetObject

Returns the value of attribute hideset.



45
46
47
# File 'lib/rucc/token.rb', line 45

def hideset
  @hideset
end

#idObject

Returns the value of attribute id.



45
46
47
# File 'lib/rucc/token.rb', line 45

def id
  @id
end

#is_varargObject (readonly)

Returns the value of attribute is_vararg.



45
46
47
# File 'lib/rucc/token.rb', line 45

def is_vararg
  @is_vararg
end

#kindObject

Returns the value of attribute kind.



45
46
47
# File 'lib/rucc/token.rb', line 45

def kind
  @kind
end

#lineObject

Returns the value of attribute line.



45
46
47
# File 'lib/rucc/token.rb', line 45

def line
  @line
end

#positionObject (readonly)

Returns the value of attribute position.



45
46
47
# File 'lib/rucc/token.rb', line 45

def position
  @position
end

#spaceObject

Returns the value of attribute space.



45
46
47
# File 'lib/rucc/token.rb', line 45

def space
  @space
end

#svalObject

Returns the value of attribute sval.



45
46
47
# File 'lib/rucc/token.rb', line 45

def sval
  @sval
end

Class Method Details

.is_ident?(tok, s) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


105
106
107
# File 'lib/rucc/token.rb', line 105

def is_ident?(tok, s)
  tok.kind == T::IDENT && tok.sval == s
end

.is_keyword?(tok, id) ⇒ Boolean

Parameters:

  • tok (Token)
  • id (String)

Returns:

  • (Boolean)


98
99
100
# File 'lib/rucc/token.rb', line 98

def is_keyword?(tok, id)
  tok.kind == T::KEYWORD && tok.id == id
end

Instance Method Details

#to_sString

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rucc/token.rb', line 49

def to_s
  case kind
  when T::IDENT
    return sval
  when T::KEYWORD
    case self
    when Operator, Keyword
      return str
    else
      return "#{id}"
    end
  when T::CHAR
    return "#{encoding_prefix(enc)}'#{Util.quote_char(c)}'"
  when T::NUMBER
    return sval
  when T::STRING
    return "#{encoding_prefix(enc)}\"#{Util.quote_cstring(sval)}\""
  when T::EOF
    return "(eof)"
  when T::INVALID
    return "#{c}"
  when T::NEWLINE
    return "(newline)"
  when T::SPACE
    return "(space)"
  when T::MACRO_PARAM
    return "(macro-param)"
  else
    raise "internal error: unknown token kind: #{kind}"
  end
end