-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.cpp
More file actions
98 lines (77 loc) · 2.26 KB
/
parser.cpp
File metadata and controls
98 lines (77 loc) · 2.26 KB
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
// Copyright © 2014-2016 Ryan Leckey, All Rights Reserved.
// Distributed under the MIT License
// See accompanying file LICENSE
#include <boost/filesystem.hpp>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include "arrow/parser.hpp"
#include "arrow/log.hpp"
using arrow::Parser;
namespace fs = boost::filesystem;
Parser::Parser(
ptr<std::istream> is, const std::string& filename
) : _t(is, filename) {
}
auto Parser::parse() -> ptr<ast::Module> {
// Declare the top-level, automatic module
// The name of the module is built from the name of the given file
auto stem = fs::path(_t._filename).stem().string();
// Parse: block
auto block = parse_block(true);
if (!block) return nullptr;
// Make
auto mod = std::make_shared<ast::Module>(block->span, stem, block,
_t._filename);
// // Attempt to match statements until the end of the stream
// while (!_t.empty()) {
// // Parse a statement (try)
// auto statement = parse_statement();
// if (statement) {
// // Add it to the module
// mod->statements.push_back(statement);
//
// // Extend the module span
// mod->span = mod->span.extend(statement->span);
// }
// }
return mod;
}
auto Parser::expect(token::Type type, bool consume) -> ptr<token::Token> {
return expect({type}, consume);
}
auto Parser::expect(
std::initializer_list<token::Type> types, bool consume
) -> ptr<token::Token> {
std::vector<token::Type> types_v(types);
return expect(types_v, consume);
}
auto Parser::expect(
std::vector<token::Type> types, bool consume
) -> ptr<token::Token> {
// auto tok = consume ? _t.pop() : _t.peek();
auto tok = _t.peek();
if (std::find(types.begin(), types.end(), tok->type) == types.end()) {
if (types.size() == 1) {
Log::get().error(tok->span, "expected {}, found {}", types[0], tok->type);
} else {
std::stringstream msg;
msg << "expected one of ";
unsigned index = 0;
for (auto& type : types) {
msg << type;
if (index != 0) {
msg << ", ";
}
index += 1;
}
msg << "; found " << tok->type;
Log::get().error(tok->span, msg.str().c_str());
}
return nullptr;
}
// Found; consume
if (consume) _t.pop();
return tok;
}