-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
64 lines (55 loc) · 1.69 KB
/
parser.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
#pragma once
#include "cst.h"
#include "lexer.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <vector>
namespace grp {
struct ParserOption {
std::string mainInputFile;
std::vector<std::string> includePaths;
static ParserOption createDefaultOption(const std::string mainInputFile);
};
class ParserContext {
ParserOption option;
std::unique_ptr<llvm::vfs::FileSystem> fs;
IdentifierInterner ii;
llvm::BumpPtrAllocator alloc;
public:
ParserContext(const ParserOption &option);
const ParserOption &getOption() const { return option; }
llvm::vfs::FileSystem &getFS() const { return *fs.get(); }
IdentifierInterner &getIdentifierInterner() { return ii; }
llvm::BumpPtrAllocator &getAllocator() { return alloc; }
};
class CSTParser {
ParserContext &context;
llvm::SourceMgr srcMgr;
// note: SourceMgr has a stack of included file, we keep the corresponding
// Lexers
std::vector<Lexer> lexerStack;
IDTy ID_include;
Lexer &topLexer() { return lexerStack.back(); }
void skipEmptyLexers();
Token expect(TokenKind kind) {
Token result = topLexer().lex();
if (result.getKind() != kind) {
// TODO: diag
}
return result;
}
void includeFile(llvm::StringRef path);
public:
CSTParser(ParserContext &context);
CST *parseSubCST();
// parse an expression without handling of include
ExpressionCST *parseRawExpressionCST();
IdentifierCST *parseIdentifierCST();
StringCST *parseStringCST();
CodeStringCST *parseCodeStringCST();
IntCST *parseIntCST();
VectorCST *parseVectorCST();
// parse a top-level CST(which must be an expression), include's are handled
ExpressionCST *parseTopCST();
};
} // namespace grp