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

Lexical analyzer for the GSC programming language. More...

#include <scanner.hpp>

Collaboration diagram for Scanner:

Public Member Functions

 Scanner (std::string_view program)
 Constructs a Scanner object.
 
void scanTokens ()
 Scans the entire source code and generates a list of tokens.
 
std::vector< TokengetTokens () const
 

Private Member Functions

void addToken (TokenType type)
 
void addToken (TokenType type, std::any literal)
 
void scanToken ()
 
void identifier ()
 
void number ()
 
void string ()
 
bool match (const char &expected)
 
char peek () const
 
char peekNext () const
 
bool isAtEnd () const
 
char advance ()
 

Private Attributes

std::string_view program
 
std::vector< Tokentokens
 
int start = 0
 
int current = 0
 
int line = 1
 

Detailed Description

Lexical analyzer for the GSC programming language.

The Scanner class is responsible for tokenizing the input source code. It reads the source code character by character and generates a list of tokens that represent the structure of the code.

Definition at line 23 of file scanner.hpp.

Constructor & Destructor Documentation

◆ Scanner()

Scanner::Scanner ( std::string_view  program)

Constructs a Scanner object.

Parameters
programThe source code to be scanned.
Note
The program is stored as a string_view to avoid unnecessary copies.

Definition at line 167 of file scanner.cpp.

167 : program{program} {
168 tokens.reserve(256);
169}
std::string_view program
Definition scanner.hpp:25
std::vector< Token > tokens
Definition scanner.hpp:26

Member Function Documentation

◆ addToken() [1/2]

void Scanner::addToken ( TokenType  type)
private

Definition at line 12 of file scanner.cpp.

12{ addToken(type, nullptr); }
void addToken(TokenType type)
Definition scanner.cpp:12

◆ addToken() [2/2]

void Scanner::addToken ( TokenType  type,
std::any  literal 
)
private

Definition at line 14 of file scanner.cpp.

14 {
15 std::string text{program.substr(start, current - start)};
16 tokens.emplace_back(type, std::move(text), std::move(literal), line);
17}
int current
Definition scanner.hpp:28
int line
Definition scanner.hpp:29
int start
Definition scanner.hpp:27

◆ advance()

char Scanner::advance ( )
private

Definition at line 165 of file scanner.cpp.

165{ return program[current++]; }

◆ getTokens()

std::vector< Token > Scanner::getTokens ( ) const

Definition at line 180 of file scanner.cpp.

180{ return tokens; }

◆ identifier()

void Scanner::identifier ( )
private

Definition at line 98 of file scanner.cpp.

98 {
99 while (isAlphaNumeric(peek())) {
100 advance();
101 }
102
103 std::string text = std::string{program.substr(start, current - start)};
104
105 TokenType type = keywords.count(text) ? keywords.at(text) : IDENTIFIER;
106 addToken(type);
107}
char advance()
Definition scanner.cpp:165
char peek() const
Definition scanner.cpp:147
bool isAlphaNumeric(const char c)
Definition scanner.cpp:10
static const std::map< std::string_view, TokenType > keywords
Definition scanner.hpp:8
TokenType
Enum representing the different types of tokens in the GSC language.
Definition tokenType.hpp:10
@ IDENTIFIER
Definition tokenType.hpp:33

◆ isAtEnd()

bool Scanner::isAtEnd ( ) const
private

Definition at line 161 of file scanner.cpp.

161 {
162 return current >= static_cast<int>(program.size());
163}

◆ match()

bool Scanner::match ( const char &  expected)
private

Definition at line 138 of file scanner.cpp.

138 {
139 if (isAtEnd() || program[current] != expected) {
140 return false;
141 }
142
143 current++;
144 return true;
145}
bool isAtEnd() const
Definition scanner.cpp:161

◆ number()

void Scanner::number ( )
private

Definition at line 109 of file scanner.cpp.

109 {
110 while (isDigit(peek())) {
111 advance();
112 }
113
114 int numberLiteral =
115 std::stoi(std::string{program.substr(start, current - start)});
116 addToken(NUMBER, numberLiteral);
117}
bool isDigit(const char c)
Definition scanner.cpp:4
@ NUMBER
Definition tokenType.hpp:35

◆ peek()

char Scanner::peek ( ) const
private

Definition at line 147 of file scanner.cpp.

147 {
148 if (isAtEnd()) {
149 return '\0';
150 }
151 return program[current];
152}

◆ peekNext()

char Scanner::peekNext ( ) const
private

Definition at line 154 of file scanner.cpp.

154 {
155 if (current + 1 >= static_cast<int>(program.size())) {
156 return '\0';
157 }
158 return program[current + 1];
159}

◆ scanToken()

void Scanner::scanToken ( )
private

Definition at line 19 of file scanner.cpp.

19 {
20 char c = advance();
21 switch (c) {
22 // One or two character tokens
23 case '(':
25 break;
26 case ')':
28 break;
29 case '{':
31 break;
32 case '}':
34 break;
35 case '-':
37 break;
38 case '+':
40 break;
41 case ';':
43 break;
44 case '*':
46 break;
47 case '!':
48 addToken(match('=') ? BANG_EQUAL : BANG);
49 break;
50 case '=':
52 break;
53 case '<':
54 addToken(match('=') ? LESS_EQUAL : LESS);
55 break;
56 case '>':
58 break;
59
60 // Integer division vs. C-style comments
61 case '/':
62 if (match('/')) {
63 while (peek() != '\n' && !isAtEnd())
64 advance();
65 } else {
67 }
68 break;
69
70 // Ignore white spaces
71 case ' ':
72 case '\r':
73 case '\t':
74 break;
75
76 // New line
77 case '\n':
78 line++;
79 break;
80
81 // Identifiers
82 case '"':
83 string();
84 break;
85 default:
86 if (isDigit(c)) {
87 number();
88 } else if (isAlpha(c)) {
89 identifier();
90 } else {
91 // Report error but continue scanning
92 error(line, "Unexpected character.");
93 }
94 break;
95 };
96}
bool match(const char &expected)
Definition scanner.cpp:138
void identifier()
Definition scanner.cpp:98
void number()
Definition scanner.cpp:109
void string()
Definition scanner.cpp:119
void error(const int &line, const std::string &message)
Report an error in the given line.
Definition error.cpp:12
bool isAlpha(const char c)
Definition scanner.cpp:6
@ BANG
Definition tokenType.hpp:23
@ GREATER
Definition tokenType.hpp:27
@ GREATER_EQUAL
Definition tokenType.hpp:28
@ SEMICOLON
Definition tokenType.hpp:18
@ EQUAL_EQUAL
Definition tokenType.hpp:26
@ EQUAL
Definition tokenType.hpp:25
@ RIGHT_BRACE
Definition tokenType.hpp:15
@ BANG_EQUAL
Definition tokenType.hpp:24
@ PLUS
Definition tokenType.hpp:17
@ LESS
Definition tokenType.hpp:29
@ STAR
Definition tokenType.hpp:20
@ SLASH
Definition tokenType.hpp:19
@ LEFT_BRACE
Definition tokenType.hpp:14
@ LEFT_PAREN
Definition tokenType.hpp:12
@ LESS_EQUAL
Definition tokenType.hpp:30
@ RIGHT_PAREN
Definition tokenType.hpp:13
@ MINUS
Definition tokenType.hpp:16

◆ scanTokens()

void Scanner::scanTokens ( )

Scans the entire source code and generates a list of tokens.

Definition at line 171 of file scanner.cpp.

171 {
172 while (!isAtEnd()) {
173 start = current;
174 scanToken();
175 }
176
177 tokens.emplace_back(END_OF_FILE, "", nullptr, line);
178}
void scanToken()
Definition scanner.cpp:19
@ END_OF_FILE
Definition tokenType.hpp:50

◆ string()

void Scanner::string ( )
private

Definition at line 119 of file scanner.cpp.

119 {
120 while (peek() != '"' && !isAtEnd()) {
121 if (peek() == '\n')
122 line++;
123 advance();
124 }
125
126 if (isAtEnd()) {
127 // Report error but finish scanning without crashing
128 error(line, "Unterminated string.");
129 return;
130 }
131
132 advance();
133
134 std::string word{program.substr(start + 1, current - start - 2)};
135 addToken(STRING, std::move(word));
136}
@ STRING
Definition tokenType.hpp:34

Member Data Documentation

◆ current

int Scanner::current = 0
private

Definition at line 28 of file scanner.hpp.

◆ line

int Scanner::line = 1
private

Definition at line 29 of file scanner.hpp.

◆ program

std::string_view Scanner::program
private

Definition at line 25 of file scanner.hpp.

◆ start

int Scanner::start = 0
private

Definition at line 27 of file scanner.hpp.

◆ tokens

std::vector<Token> Scanner::tokens
private

Definition at line 26 of file scanner.hpp.


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