-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.h
128 lines (102 loc) · 2.68 KB
/
scan.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef SCAN_H
#define SCAN_H
#include "error.h"
#include "types.h"
#include <unordered_map>
#include <string>
namespace amps
{
class scan_iterator;
class scan
{
std::unordered_map<std::string, token_types> keywords_;
metainfo metainfo_;
std::string file_;
uint16_t line_;
error &error_;
private:
metadata code_block(const std::string &content,
size_t &position);
metadata text_block(const std::string &content,
size_t &position,
bool force);
void parse_block(const std::string &content);
void scan_code(const scan_iterator &it, metadata &data);
void parse_string(const scan_iterator &it, metadata &data);
void parse_number(const scan_iterator &it, metadata &data);
void parse_id(const scan_iterator &it, metadata &data);
public:
scan(error &err);
~scan() = default;
scan(const scan&) = delete;
scan(scan&&) = delete;
scan &operator=(vobject &) = delete;
scan &operator=(vobject &&) = delete;
void do_scan(const std::string &content);
metainfo &get_metainfo();
};
inline metainfo &scan::get_metainfo()
{
metainfo_.rehash();
return metainfo_;
}
class scan_iterator
{
friend class scan;
const std::string &data_;
mutable size_t cursor_;
scan_iterator(const std::string &data) :
data_(data),
cursor_(0)
{
}
bool is_eol() const
{
return (cursor_ >= data_.size());
}
size_t cursor() const
{
return cursor_;
}
char look() const
{
return data_[cursor_];
}
void skip_all() const
{
while (!is_eol()) {
next();
}
}
bool next() const
{
if (cursor_ > data_.size() - 1) {
return false;
}
++cursor_;
return true;
}
bool check(char c) const
{
return (!is_eol() && c == look());
}
bool match(char c) const
{
if (!check(c)) {
return false;
}
if (!next()) {
return false;
}
return true;
}
std::string substr(size_t start, size_t len) const
{
if (len == 0) {
len = data_.size();
}
return std::string(data_, start, len);
}
};
}
#endif // SCAN_H