-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.hpp
67 lines (54 loc) · 1.36 KB
/
lexer.hpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef LEXER_H_
#define LEXER_H_
#include <vector>
#include <algorithm>
#include <sstream>
struct Token;
using Tokenlist = std::vector<Token>;
using csi = std::string::const_iterator;
using TokenList = std::vector<Token>;
using TokenListCIter = TokenList::const_iterator;
enum TokenType {
OPENING_PARREN,
CLOSING_PARREN,
KEY_WORD,
NUMERIC,
STRING
};
struct Token {
TokenType type;
std::string data;
int index;
Token(TokenType type, std::string data, int index):
type(type), data(data), index(index) {}
};
void skippWhiteSpace(csi& si, const csi& end);
std::string strFromPtr(csi begin, size_t length);
class Tokenizer {
private:
const std::string source;
public:
Tokenizer(const std::string& s): source(s) {}
TokenList tokenize() const;
private:
Token consumeString (csi& si) const;
static bool isNumeric(char c) {
if (c - '0' <= 9 && c - '0' >= 0) {
return true;
}
return false;
}
static bool isWordy(char c) {
return (c != ' ' && c != '(' && c != ')' && c != '\n');
}
template<typename F>
Token consumeToToken(csi& si, TokenType t, F cond) const {
std::ostringstream oss;
while(si < source.cend() && cond(*si)) {
oss << *si;
si++;
}
return Token(t, oss.str(), si-source.cbegin());
}
};
#endif