-
Notifications
You must be signed in to change notification settings - Fork 4
/
lexer.h
44 lines (37 loc) · 1.36 KB
/
lexer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
#pragma once
#include <istream>
#include <vector>
#include <string>
#include <stdexcept>
class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
std::string name; // for identifiers: variable name
Token(char kind) :kind(kind) {}
Token(char kind, double value) :kind(kind), value(value) {}
Token(char kind, const std::string& name) :kind(kind), name(name) {}
};
const char number = '8'; // number token
const char name = 'a'; // name token
const char let = 'L'; // variable declaration token
const char constant = 'C'; // constant declaration token
const std::string var_declkey = "let"; // variable declaration keyword
const std::string const_declkey = "const"; // constant declaration keyword
const char help = 'h'; // help token
const char quit = 'q'; // quit token
const char print = ';'; // print token
class Token_stream {
public:
explicit Token_stream(std::istream& is);
Token get();
void putback(Token t);
void ignore();
void ignore(char c);
private:
std::istream& is; // input stream to read from
std::vector<Token> buffer; // here is where we keep Tokens put back using putback()
};
class Lexer_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};