-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
54 lines (44 loc) · 1.41 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
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <string>
#include <utility>
#include <vector>
using namespace std;
enum token {
t_lparen, // '('
t_rparen, // ')'
t_backslash, // '\'
t_dot, // '.'
t_equals, // '='
t_ident, // non-keyword strings
t_eol, // '\n'
t_error // anything unrecognised
};
string token_string(enum token t);
class Lexer {
private:
// Pairs of tokens and indexes into the idents vector.
vector<pair<enum token, ssize_t> > tokens;
vector<string> idents;
// Current index into the token sequence.
size_t token_index;
// Last identifier string
string last_ident;
public:
Lexer() {}
Lexer(string input) { tokenise(input); }
void tokenise(string in);
void set_index(size_t i) { token_index = i; }
size_t get_index() { return token_index; }
enum token peek(size_t i);
enum token next();
enum token prev();
bool has_next() { return token_index < tokens.size(); }
bool has_prev() { return token_index > 0; }
enum token match_next(enum token expected);
enum token match_prev(enum token expected);
string match_next_ident();
string match_prev_ident();
void rewind() { token_index = 0; }
void fastforward() { token_index = tokens.size(); }
string identifier() { return last_ident; }
};