-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.cpp
115 lines (100 loc) · 3.12 KB
/
parser.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <istream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "xos.h"
namespace xos {
Result<ast::Str> Parser::parseStr() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) {
return res;
}
const Token &tok = res.get();
if (tok.getKind() != Token::string) {
return Result<ast::Str>::BuildError()
<< "Expected string on row " << tok.getRow() << ", col "
<< tok.getCol();
}
return Result<ast::Str>(tok.getVal());
}
Result<ast::Out> Parser::parseOut() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) return res;
const Token &tok = res.get();
if (tok.getKind() != Token::out) {
return Result<ast::Out>::BuildError()
<< "Expected 'out' on row " << tok.getRow() << ", col "
<< tok.getCol();
}
Result<ast::Str> sres = parseStr();
if (sres.hasError()) return sres;
std::unique_ptr<ast::Str> str(sres.Release());
return Result<ast::Out>(std::move(str));
}
Result<ast::Func> Parser::parseFunc() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) return res;
const Token &tok = res.get();
if (tok.getKind() != Token::identifier) {
return Result<ast::Func>::BuildError()
<< "Expected identifier on row " << tok.getRow() << ", col "
<< tok.getCol();
}
std::string name = tok.getVal();
Result<ast::Prototype> pres = parsePrototype(name);
if (pres.hasError()) return pres;
Result<ast::Out> body = parseBody();
if (body.hasError()) return body;
return Result<ast::Func>(std::unique_ptr<ast::Prototype>(pres.Release()),
std::unique_ptr<ast::Out>(body.Release()));
}
Result<ast::Out> Parser::parseBody() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) return res;
const Token &tok = res.get();
if (tok.getKind() == Token::out) {
Result<ast::Str> sres = parseStr();
if (sres.hasError()) return sres;
return Result<ast::Out>(std::unique_ptr<ast::Str>(sres.Release()));
}
return Result<ast::Out>::BuildError()
<< "Expected 'out' on row " << tok.getRow() << ", col "
<< tok.getCol();
}
Result<ast::Prototype> Parser::parsePrototype(std::string name) {
Result<Token> res = lexer_.Lex();
if (res.hasError()) return res;
Token *tok = &res.get();
if (tok->getKind() != Token::lparen) {
return Result<ast::Prototype>::BuildError()
<< "Expected '(' on row " << tok->getRow() << ", col "
<< tok->getCol();
}
while (tok->getKind() != Token::rparen) {
res = lexer_.Lex();
tok = &res.get();
}
res = lexer_.Lex();
tok = &res.get();
if (tok->getKind() != Token::arrow) {
return Result<ast::Prototype>::BuildError()
<< "Expected '=>' on row " << tok->getRow() << ", col "
<< tok->getCol();
}
res = lexer_.Lex();
tok = &res.get();
while (tok->getKind() != Token::rparen) {
res = lexer_.Lex();
tok = &res.get();
}
res = lexer_.Lex();
tok = &res.get();
return Result<ast::Prototype>(name, std::vector<std::string>(), "");
}
} // namespace xos