68 lines
2.7 KiB
C++
68 lines
2.7 KiB
C++
//============================================================================//
|
|
// //
|
|
// artichoke programming language //
|
|
// //
|
|
// Copyright (C) 2025 Erick Saul Guzman Ramos, whoami.artichoke.dev //
|
|
// //
|
|
// //
|
|
// This program is free software: you can redistribute it and/or modify //
|
|
// it under the terms of the GNU Affero General Public License as published //
|
|
// by the Free Software Foundation, either version 3 of the License, or //
|
|
// (at your option) any later version. //
|
|
// //
|
|
// This program is distributed in the hope that it will be useful, //
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
// GNU Affero General Public License for more details. //
|
|
// //
|
|
// You should have received a copy of the GNU Affero General Public License //
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
|
|
// //
|
|
//============================================================================//
|
|
|
|
#include <artichoke/Parser/Parser.hpp>
|
|
|
|
namespace arti::lang {
|
|
|
|
Parser::Parser(std::string source) noexcept
|
|
: unitName{}
|
|
, sourceCode{ source }
|
|
, tokenizer{ source } { }
|
|
|
|
Parser::Parser(std::string unitName, std::string source) noexcept
|
|
: unitName{ unitName }
|
|
, sourceCode{ source }
|
|
, tokenizer{ source } { }
|
|
|
|
Expected<ast::AST> Parser::parse() {
|
|
auto unit = ast::MakeNode<ast::AST>();
|
|
auto decl = ast::Optional<ast::TopLevelDeclNode>{};
|
|
bool keepParsing = true;
|
|
|
|
unit->unitName = this->unitName;
|
|
|
|
while (keepParsing) {
|
|
if (auto ok = parseTopLevelDeclaration(); ! ok) {
|
|
return Unexpected<>{ std::move(ok).error() };
|
|
}
|
|
else {
|
|
decl = std::move(ok).value();
|
|
|
|
if (! decl.has_value()) {
|
|
keepParsing = false;
|
|
}
|
|
else {
|
|
unit->declarations.push_back(std::move(decl).value());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (auto eof = consume(TokenV::tkEOF, "end of compilation unit"); ! eof) {
|
|
return Unexpected<>{ std::move(eof).error() };
|
|
}
|
|
|
|
return unit;
|
|
}
|
|
|
|
} // namespace arti::lang
|