-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoc.hpp
62 lines (50 loc) · 1.14 KB
/
Loc.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
#ifndef ALPHA_LOC_HPP
#define ALPHA_LOC_HPP
#include "types.hpp"
struct Loc {
const char* first = nullptr;
const char* last = nullptr;
char* pos = nullptr;
char* tracker = nullptr;
u32_t lineNr = 1;
Loc() = default;
explicit Loc(char*, char*);
virtual ~Loc() { }
u32_t numOfReadChars() const {
return this->pos - this->first;
}
u32_t numOfAllChars() const {
return this->last - this->first;
}
void track() {
this->tracker = this->pos;
}
void backtrack() {
this->pos = this->tracker;
}
/*
* Returns the current character, if any
*/
char current() const {
if (this->eof())
return '\0';
return *this->pos;
}
/*
* Move to the next char: 'current' will now return the next character
*/
void next() {
if (this->eof())
return;
if (this->current() == '\n')
this->lineNr++;
this->pos++;
}
/*
* True if we reached the end of file.
*/
bool eof() const {
return this->pos > this->last || *this->pos == '\0';
}
};
#endif