GSC Interpreter
A Turing-complete interpreter developed for a compiler course
Loading...
Searching...
No Matches
Token Class Reference

Represents a token in the source code. More...

#include <token.hpp>

Collaboration diagram for Token:

Public Member Functions

 Token (TokenType type, std::string lexeme, std::any literal, int line)
 Constructs a Token object.
 
TokenType getType () const
 
std::string getLexeme () const
 
std::any getLiteral () const
 
int getLine () const
 
std::string toString () const
 

Private Attributes

const TokenType type
 
const std::string lexeme
 
const std::any literal
 
const int line
 

Detailed Description

Represents a token in the source code.

The Token class encapsulates the type, lexeme, literal value, and line number of a token. It provides a method to convert the token to a string representation.

Definition at line 14 of file token.hpp.

Constructor & Destructor Documentation

◆ Token()

Token::Token ( TokenType  type,
std::string  lexeme,
std::any  literal,
int  line 
)

Constructs a Token object.

Parameters
typeThe type of the token.
lexemeThe lexeme of the token.
literalThe literal value of the token (if any).
lineThe line number where the token was found.
Note
The literal value can be of any type, so it is stored as std::any.
The lexeme and literal values are moved into the object to avoid copying.

Definition at line 4 of file token.cpp.

5 : type{type}, lexeme{std::move(lexeme)}, literal{std::move(literal)},
6 line{line} {}
const int line
Definition token.hpp:19
const TokenType type
Definition token.hpp:16
const std::string lexeme
Definition token.hpp:17
const std::any literal
Definition token.hpp:18

Member Function Documentation

◆ getLexeme()

std::string Token::getLexeme ( ) const

Definition at line 10 of file token.cpp.

10{ return lexeme; }

◆ getLine()

int Token::getLine ( ) const

Definition at line 14 of file token.cpp.

14{ return line; }

◆ getLiteral()

std::any Token::getLiteral ( ) const

Definition at line 12 of file token.cpp.

12{ return literal; }

◆ getType()

TokenType Token::getType ( ) const

Definition at line 8 of file token.cpp.

8{ return type; }

◆ toString()

std::string Token::toString ( ) const

Definition at line 16 of file token.cpp.

16 {
17 std::string literal_str;
18
19 switch (type) {
20 case (TRUE):
21 literal_str = "true";
22 break;
23 case (FALSE):
24 literal_str = "false";
25 break;
26 case (NUMBER):
27 literal_str = std::to_string(std::any_cast<int>(literal));
28 break;
29 case (STRING):
30 try {
31 literal_str = std::any_cast<std::string>(literal);
32 } catch (const std::bad_any_cast &e) {
33 literal_str =
34 static_cast<std::string>(std::any_cast<const char *>(literal));
35 }
36 break;
37 case (IDENTIFIER):
38 literal_str = lexeme;
39 break;
40 default:
41 literal_str = "nil";
42 break;
43 }
44
45 return ::toString(type) + " " + lexeme + " " + literal_str;
46}
@ NUMBER
Definition tokenType.hpp:35
@ IDENTIFIER
Definition tokenType.hpp:33
@ FALSE
Definition tokenType.hpp:43
@ TRUE
Definition tokenType.hpp:42
@ STRING
Definition tokenType.hpp:34

Member Data Documentation

◆ lexeme

const std::string Token::lexeme
private

Definition at line 17 of file token.hpp.

◆ line

const int Token::line
private

Definition at line 19 of file token.hpp.

◆ literal

const std::any Token::literal
private

Definition at line 18 of file token.hpp.

◆ type

const TokenType Token::type
private

Definition at line 16 of file token.hpp.


The documentation for this class was generated from the following files: