Line data Source code
1 : #include "../../../../inc/parsingAnalysis/algorithm/topDown.h"
2 :
3 : namespace nicole {
4 :
5 : const std::expected<std::shared_ptr<AST_IMPORT>, Error>
6 0 : TopDown::parseImport() const noexcept {
7 0 : const auto firsToken{tkStream_.current()};
8 0 : if (auto res = tryEat(); !res) {
9 0 : return createError(res.error());
10 0 : }
11 0 : if (tkStream_.current()->type() != TokenType::STRING) {
12 0 : return createError(ERROR_TYPE::SINTAX, "missing file of import at " +
13 0 : tkStream_.current()->locInfo());
14 0 : }
15 0 : const Token token{*tkStream_.current()};
16 0 : if (auto res = tryEat(); !res) {
17 0 : return createError(res.error());
18 0 : }
19 0 : std::filesystem::path currentFilePath{token.location().file()};
20 : // Gets the base path of the current file
21 0 : currentFilePath = currentFilePath.parent_path();
22 : // builds the path based on the import in the current file's directory
23 0 : currentFilePath =
24 0 : currentFilePath / token.raw().substr(1, token.raw().size() - 2);
25 0 : if (!parsedFiles_.count(currentFilePath)) {
26 0 : parsedFiles_.insert(currentFilePath);
27 0 : const std::expected<TokenStream, Error> tokens{
28 0 : lexer_.analyze(currentFilePath)};
29 0 : if (!tokens) {
30 0 : return createError(tokens.error());
31 0 : }
32 0 : const std::expected<std::monostate, Error> isInserted{
33 0 : tkStream_.insertAfter(*tokens, tkStream_.currentPos() + 1)};
34 0 : if (!isInserted) {
35 0 : return createError(isInserted.error());
36 0 : }
37 0 : }
38 0 : if (tkStream_.current()->type() != TokenType::SEMICOLON) {
39 0 : return createError(ERROR_TYPE::SINTAX, "missing ; of import statement at " +
40 0 : tkStream_.current()->locInfo());
41 0 : }
42 0 : return Builder::createImport(
43 0 : SourceLocation{*firsToken, *tkStream_.lastRead()}, currentFilePath);
44 0 : }
45 :
46 : const std::expected<std::shared_ptr<AST_PRINT>, Error>
47 0 : TopDown::parsePrint() const noexcept {
48 0 : const auto firsToken{tkStream_.current()};
49 0 : if (auto res = tryEat(); !res) {
50 0 : return createError(res.error());
51 0 : }
52 :
53 0 : const std::expected<std::vector<std::shared_ptr<AST>>, Error> arguemnts{
54 0 : parseArguments({TokenType::LP, TokenType::RP}, false)};
55 0 : if (!arguemnts) {
56 0 : return createError(arguemnts.error());
57 0 : }
58 :
59 0 : if (tkStream_.current()->type() != TokenType::SEMICOLON) {
60 0 : return createError(ERROR_TYPE::SINTAX, "missing ; of print statement at " +
61 0 : tkStream_.lastRead()->locInfo());
62 0 : }
63 0 : return Builder::createPrint(SourceLocation{*firsToken, *tkStream_.lastRead()},
64 0 : *arguemnts);
65 0 : }
66 :
67 : } // namespace nicole
|