-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseState.h
executable file
·54 lines (51 loc) · 1.27 KB
/
ParseState.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
#ifndef INCLUDED_PARSESTATE_H
#define INCLUDED_PARSESTATE_H
#include <string>
#include <utility>
#include <memory>
#include <map>
#include <iosfwd>
class ParseResult;
class ParseState;
typedef std::shared_ptr<ParseState> ParseStatePtr;
typedef std::shared_ptr<ParseResult> ParseResultPtr;
class ParseState
{
public:
ParseState(std::string& input, size_t index=0);
ParseStatePtr from(size_t advance);
std::string substr(int offset)
{
if (offset+index < input.size())
return input.substr(offset+index);
return "";
}
std::string substr(int offset, size_t length)
{
if (offset+index < input.size())
return input.substr(offset+index, length);
return "";
}
int at(size_t offset)
{
if (offset+index < input.size())
return input[offset+index];
return 0;
}
size_t length()
{
return input.size()-index;
}
friend std::ostream& operator<<(std::ostream& os, ParseState&);
std::pair<bool,ParseResultPtr> GetCached(int id);
void PutCached(int id, ParseResultPtr c) {
memos[std::make_pair(index, id)] = c;
}
static void reset() { memos = Memos(); }
private:
size_t index;
std::string& input;
typedef std::map< std::pair<size_t,int>, ParseResultPtr > Memos;
static Memos memos;
};
#endif // INCLUDED_PARSESTATE_H