gfy-cpp
Loading...
Searching...
No Matches
lexer.hpp
Go to the documentation of this file.
1#ifndef __LEXER_HPP__
2#define __LEXER_HPP__
3
4#include "token.hpp"
5
6#include <fstream>
7#include <string>
8#include <array>
9#include <locale>
10
14const array<string, 11> KEYWORDS = {
15 "set",
16 "print",
17 "add",
18 "sub",
19 "mul",
20 "div",
21 "mod",
22 "equals",
23 "greater",
24 "less",
25 "define",
26};
27
28using namespace std;
29
36class Lexer {
37 public:
43 Lexer(const char* filename);
44
50 Lexer(const string& filename);
51
55 ~Lexer();
56
63
64 private:
65 static locale loc;
66 ifstream source;
67 string filename;
68 string line_buff;
69 size_t line, column;
70
76 char skip_blanks();
77
83 char pop_char();
84
90 bool is_import_line();
91
98 Type get_type(const string& identifier);
99};
100
101#endif // __LEXER_HPP__
A class to read tokens from a source file.
Definition lexer.hpp:36
ifstream source
Definition lexer.hpp:66
string line_buff
Definition lexer.hpp:68
bool is_import_line()
Check if the current line is an import line.
Definition lexer.cpp:214
Lexer(const char *filename)
Construct a new Lexer object.
Definition lexer.cpp:12
Token next_token()
Get the next token from the source file.
Definition lexer.cpp:43
Type get_type(const string &identifier)
Return the type of a given identifier.
Definition lexer.cpp:228
size_t column
Definition lexer.hpp:69
char skip_blanks()
Skip blanks in the line buffer.
Definition lexer.cpp:192
~Lexer()
Destroy the Lexer object.
Definition lexer.cpp:39
string filename
Definition lexer.hpp:67
size_t line
Definition lexer.hpp:69
char pop_char()
Pop the first character from the line buffer.
Definition lexer.cpp:202
static locale loc
Definition lexer.hpp:65
Definition token.hpp:28
const array< string, 11 > KEYWORDS
The keywords in the language.
Definition lexer.hpp:14
Type
Token types.
Definition token.hpp:12