diff --git a/src/Lib/Script/Ast/IrModule.cc b/src/Lib/Script/Ast/IrModule.cc index e386d196c4be53202b772710babbdf905f036eaa..dbeb2efd76cd4f1799fa4a2087923ade3e80e9e9 100644 --- a/src/Lib/Script/Ast/IrModule.cc +++ b/src/Lib/Script/Ast/IrModule.cc @@ -3,6 +3,7 @@ #include "IrRoot.hh" #include "IrFunction.hh" #include "IrJob.hh" +#include "Lib/Script/FrontEnd/Lexer.hh" /* ** Protected way of getting a thing from a vector if the inner elements @@ -144,8 +145,25 @@ IrModule::addJob(IrJob *obj) } void -IrModule::parse(std::vector<Token> *) +IrModule::parse(std::vector<Token> *tokens) { - throw std::logic_error("Not implemented"); + if (tokenListIsEmpty(tokens)) { + throw std::runtime_error("Can't parse an empty token list to build an " + "IrModule declaration statement"); + } + + if (tokens->size() != 3) { + Token firstToken = getInnerTokenOpt(tokenListPop(tokens)); + throw std::runtime_error( + firstToken.location().toString() + + ": Invalid syntax, to declare a module use `module mod-name;`, here you have " + + std::to_string(tokens->size()) + " tokens!"); + } + + getInnerTokenOpt(tokenListPop(tokens)).assertTypeSimple(TOKEN_MODULE); + Token nameToken = getInnerTokenOpt(tokenListPop(tokens)); + nameToken.assertType(Token::Type::QNAME); + moduleName = nameToken.asQName(); + getInnerTokenOpt(tokenListPop(tokens)).assertTypeSimple(TOKEN_SEMICOL); } } diff --git a/src/Lib/Script/Ast/Parser/IrRoot.hh b/src/Lib/Script/Ast/Parser/IrRoot.hh index ca310abe61c0d125062690c92a804aaeae708247..0d9af013ddaf28fced7cc23e1e67959c8d0e0113 100644 --- a/src/Lib/Script/Ast/Parser/IrRoot.hh +++ b/src/Lib/Script/Ast/Parser/IrRoot.hh @@ -67,12 +67,6 @@ while (tokenListIsNotEmpty(tokens)) { } std::vector<Token> tokensToNextSemiCol = tokenListPopToNextSimpleToken(tokens, TOKEN_SEMICOL); - if (tokensToNextSemiCol.size() != 3) { - throw std::runtime_error( - firstToken.location().toString() + - ": Invalid syntax, to declare a module use `module mod-name;`, here you have " + - std::to_string(tokensToNextSemiCol.size()) + " tokens!"); - } mainVivyModule = IrElement::create<IrModule>(this, &tokensToNextSemiCol); addModule(mainVivyModule); createdElement = mainVivyModule;