mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Lexer.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <istream>
8#include <unordered_map>
9#include <vector>
10
11
12namespace m {
13
14namespace ast {
15
16struct M_EXPORT Lexer
17{
18 public:
21 const char *filename;
22 std::istream &in;
23
24 private:
25 using Keywords_t = std::unordered_map<ThreadSafePooledString, TokenType>;
26 using buf_t = std::vector<char>;
28 int c_;
29 Position pos_, start_;
31
32 public:
33 explicit Lexer(Diagnostic &diag, ThreadSafeStringPool &pool, const char *filename, std::istream &in)
34 : diag(diag)
35 , pool(pool)
36 , filename(filename)
37 , in(in)
38 , pos_(filename)
39 , start_(pos_)
40 {
41 buf_.reserve(32);
42 initialize_keywords();
43 c_ = '\n';
44 }
45
47 void initialize_keywords();
48
50 Token next();
51
52 private:
54 int step() {
55 switch (c_) {
56 case '\n':
57 pos_.column = 1;
58 pos_.line++;
59 break;
60
61 default:
62 pos_.column++;
63 break;
64 }
65 return c_ = in.get();
66 }
67
68 void push() {
69 buf_.push_back(c_);
70 step();
71 }
72
73 bool accept(const int c) {
74 if (c == this->c_) {
75 push();
76 return true;
77 }
78 return false;
79 }
80
82 buf_.push_back('\0');
83 return pool(buf_.data());
84 }
85
86 /* Lexer routines. */
87 Token read_keyword_or_identifier();
88 Token read_number();
89 Token read_string_literal();
90 Token read_date_or_datetime();
91 Token read_instruction();
92};
93
94}
95
96}
‍mutable namespace
Definition: Backend.hpp:10
ThreadSafeStringPool::proxy_type ThreadSafePooledString
Definition: Pool.hpp:464
unsigned line
Definition: Position.hpp:14
unsigned column
Definition: Position.hpp:15
const char * filename
Definition: Lexer.hpp:21
ThreadSafePooledString internalize()
Definition: Lexer.hpp:81
int step()
Reads the next character from in to c_, and updates pos_ accordingly.
Definition: Lexer.hpp:54
Position pos_
Definition: Lexer.hpp:29
Lexer(Diagnostic &diag, ThreadSafeStringPool &pool, const char *filename, std::istream &in)
Definition: Lexer.hpp:33
Diagnostic & diag
Definition: Lexer.hpp:19
ThreadSafeStringPool & pool
Definition: Lexer.hpp:20
std::istream & in
Definition: Lexer.hpp:22
bool accept(const int c)
Definition: Lexer.hpp:73
std::unordered_map< ThreadSafePooledString, TokenType > Keywords_t
Definition: Lexer.hpp:25
std::vector< char > buf_t
Definition: Lexer.hpp:26
buf_t buf_
Definition: Lexer.hpp:30
Keywords_t keywords_
Definition: Lexer.hpp:27
void push()
Definition: Lexer.hpp:68