MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / parser

Class parser

subprojects/llama.cpp/common/jinja/parser.cpp:21–585  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21class parser {
22 const std::vector<token> & tokens;
23 size_t current = 0;
24
25 std::string source; // for error reporting
26
27public:
28 parser(const std::vector<token> & t, const std::string & src) : tokens(t), source(src) {}
29
30 program parse() {
31 statements body;
32 while (current < tokens.size()) {
33 body.push_back(parse_any());
34 }
35 return program(std::move(body));
36 }
37
38 // NOTE: start_pos is the token index, used for error reporting
39 template<typename T, typename... Args>
40 std::unique_ptr<T> mk_stmt(size_t start_pos, Args&&... args) {
41 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
42 assert(start_pos < tokens.size());
43 ptr->pos = tokens[start_pos].pos;
44 return ptr;
45 }
46
47private:
48 const token & peek(size_t offset = 0) const {
49 if (current + offset >= tokens.size()) {
50 static const token end_token{token::eof, "", 0};
51 return end_token;
52 }
53 return tokens[current + offset];
54 }
55
56 token expect(token::type type, const std::string& error) {
57 const auto & t = peek();
58 if (t.t != type) {
59 throw parser_exception("Parser Error: " + error + " (Got " + t.value + ")", source, t.pos);
60 }
61 current++;
62 return t;
63 }
64
65 void expect_identifier(const std::string & name) {
66 const auto & t = peek();
67 if (t.t != token::identifier || t.value != name) {
68 throw parser_exception("Expected identifier: " + name, source, t.pos);
69 }
70 current++;
71 }
72
73 bool is(token::type type) const {
74 return peek().t == type;
75 }
76
77 bool is_identifier(const std::string & name) const {
78 return peek().t == token::identifier && peek().value == name;

Callers 1

parse_from_tokensFunction · 0.85

Calls 1

sizeMethod · 0.65

Tested by

no test coverage detected