-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
67 lines (55 loc) · 1.37 KB
/
token.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
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Created by koji on 1/8/22.
//
#ifndef FEMTO_RE_TOKEN_H
#define FEMTO_RE_TOKEN_H
#include <optional>
#include <memory>
enum TokenKind {
// TK_TAGにrename
TK_START_TAG,
TK_CHAR,
TK_EOF
};
enum StartOrEnd {
TAG_START,
TAG_END
};
class StartOrEndTag {
public:
StartOrEnd start_or_end;
std::string name;
};
class Token {
public:
TokenKind kind; // TODO: start_or_end_tagにrenameする
std::optional<StartOrEndTag> start_tag;
std::optional<char> c;
std::shared_ptr<Token> next;
explicit Token(StartOrEndTag start_tag): kind(TK_START_TAG), start_tag(start_tag) {}
explicit Token(char c): kind(TK_CHAR), c(c) {}
explicit Token(TokenKind kind): kind(kind) {}
// std::optional<Token*> next;
};
enum TokenizationState {
ST_DATA,
ST_TAG_OPEN,
ST_TAG_NAME,
ST_END_TAG_OPEN,
};
class Tokenizer {
public:
std::shared_ptr<Token> tokenize(std::istream& istream);
private:
Token reconsume_tag_name_state(std::istream &istream, StartOrEndTag start_tag, char current_char);
bool can_tokenize(std::string str);
char re_consume(std::istream& istream, unsigned int size = 1);
Token generate_tag_token(std::istream& istream);
TokenizationState state {ST_DATA};
char consume(std::istream& istream);
};
class TokenFactory {
public:
static Token createStartTagToken();
};
#endif //FEMTO_RE_TOKEN_H