Support surrounding begin and end
This commit is contained in:
@ -10,11 +10,26 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
definition::definition(std::string&& identifier, std::unique_ptr<integer_literal>&& body)
|
||||
declaration::declaration(const std::string& identifier)
|
||||
: m_identifier(identifier)
|
||||
{
|
||||
}
|
||||
|
||||
std::string& declaration::identifier() noexcept
|
||||
{
|
||||
return m_identifier;
|
||||
}
|
||||
|
||||
definition::definition(const std::string& identifier, std::unique_ptr<integer_literal>&& body)
|
||||
: m_identifier(std::move(identifier)), m_body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
void declaration::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
void definition::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
@ -30,8 +45,10 @@ namespace elna::source
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
block::block(std::vector<std::unique_ptr<definition>>&& definitions, std::unique_ptr<statement>&& body)
|
||||
: m_definitions(std::move(definitions)), m_body(std::move(body))
|
||||
block::block(std::vector<std::unique_ptr<definition>>&& definitions,
|
||||
std::vector<std::unique_ptr<declaration>>&& declarations,
|
||||
std::unique_ptr<statement>&& body)
|
||||
: m_definitions(std::move(definitions)), m_declarations(std::move(declarations)), m_body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
@ -50,6 +67,11 @@ namespace elna::source
|
||||
return m_definitions;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<declaration>>& block::declarations() noexcept
|
||||
{
|
||||
return m_declarations;
|
||||
}
|
||||
|
||||
integer_literal::integer_literal(const std::int32_t value)
|
||||
: m_number(value)
|
||||
{
|
||||
@ -138,6 +160,21 @@ namespace elna::source
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
compound_statement::compound_statement(std::vector<std::unique_ptr<statement>>&& statements)
|
||||
: m_statements(std::move(statements))
|
||||
{
|
||||
}
|
||||
|
||||
void compound_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<statement>>& compound_statement::statements()
|
||||
{
|
||||
return m_statements;
|
||||
}
|
||||
|
||||
parser::parser(const std::vector<token>& tokens)
|
||||
: tokens(tokens.cbegin()), end(tokens.cend())
|
||||
{
|
||||
@ -215,14 +252,20 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<definition> parser::parse_definition()
|
||||
{
|
||||
std::string definition_identifier = tokens->identifier(); // Copy.
|
||||
auto definition_identifier = advance(token::type::identifier);
|
||||
|
||||
++tokens;
|
||||
++tokens; // Skip the equals sign.
|
||||
if (!definition_identifier.has_value())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
if (!skip(token::type::equals))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (tokens->of() == source::token::type::number)
|
||||
{
|
||||
auto result = std::make_unique<definition>(std::move(definition_identifier),
|
||||
auto result = std::make_unique<definition>(definition_identifier.value().get().identifier(),
|
||||
std::make_unique<integer_literal>(tokens->number()));
|
||||
++tokens;
|
||||
return result;
|
||||
@ -230,61 +273,168 @@ namespace elna::source
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<statement> parser::parse_bang_statement()
|
||||
std::unique_ptr<declaration> parser::parse_declaration()
|
||||
{
|
||||
auto declaration_identifier = advance(token::type::identifier);
|
||||
|
||||
if (!declaration_identifier.has_value())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<declaration>(declaration_identifier.value().get().identifier());
|
||||
}
|
||||
|
||||
std::unique_ptr<statement> parser::parse_statement()
|
||||
{
|
||||
if (tokens->of() == source::token::type::bang)
|
||||
{
|
||||
++tokens;
|
||||
auto bang_body = parse_expression();
|
||||
if (bang_body != nullptr)
|
||||
{
|
||||
return std::make_unique<bang_statement>(std::move(bang_body));
|
||||
}
|
||||
return parse_bang_statement();
|
||||
}
|
||||
else if (tokens->of() == source::token::type::begin)
|
||||
{
|
||||
return parse_compound_statement();
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(unexpected_token{ *tokens }));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<bang_statement> parser::parse_bang_statement()
|
||||
{
|
||||
if (!advance(token::type::bang))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto bang_body = parse_expression();
|
||||
|
||||
if (bang_body != nullptr)
|
||||
{
|
||||
return std::make_unique<bang_statement>(std::move(bang_body));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<compound_statement> parser::parse_compound_statement()
|
||||
{
|
||||
if (!advance(token::type::begin))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto result = std::make_unique<compound_statement>();
|
||||
std::unique_ptr<statement> next_statement;
|
||||
|
||||
do
|
||||
{
|
||||
if ((next_statement = parse_statement()) == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
result->statements().push_back(std::move(next_statement));
|
||||
}
|
||||
while (tokens->of() != token::type::end);
|
||||
++tokens;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<definition>> parser::parse_definitions()
|
||||
{
|
||||
++tokens; // Skip const.
|
||||
|
||||
std::vector<std::unique_ptr<definition>> definitions;
|
||||
|
||||
while (tokens != end)
|
||||
if (tokens->of() != token::type::let)
|
||||
{
|
||||
return definitions;
|
||||
}
|
||||
++tokens; // Skip const.
|
||||
|
||||
std::unique_ptr<definition> parsed_definition;
|
||||
while ((parsed_definition = parse_definition()) != nullptr)
|
||||
{
|
||||
auto parsed_definition = parse_definition();
|
||||
if (parsed_definition == nullptr)
|
||||
{
|
||||
return definitions;
|
||||
}
|
||||
definitions.push_back(std::move(parsed_definition));
|
||||
|
||||
if (tokens->of() == source::token::type::semicolon)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (tokens->of() == source::token::type::comma)
|
||||
{
|
||||
++tokens;
|
||||
}
|
||||
else if (tokens->of() == source::token::type::semicolon)
|
||||
{
|
||||
++tokens;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return definitions;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<declaration>> parser::parse_declarations()
|
||||
{
|
||||
std::vector<std::unique_ptr<declaration>> declarations;
|
||||
|
||||
if (tokens->of() != token::type::var)
|
||||
{
|
||||
return declarations;
|
||||
}
|
||||
++tokens; // Skip var.
|
||||
|
||||
std::unique_ptr<declaration> parsed_declaration;
|
||||
while ((parsed_declaration = parse_declaration()) != nullptr)
|
||||
{
|
||||
declarations.push_back(std::move(parsed_declaration));
|
||||
|
||||
if (tokens->of() == token::type::comma)
|
||||
{
|
||||
++tokens;
|
||||
}
|
||||
else if (tokens->of() == token::type::semicolon)
|
||||
{
|
||||
++tokens;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return declarations;
|
||||
}
|
||||
|
||||
std::unique_ptr<block> parser::parse_block()
|
||||
{
|
||||
std::vector<std::unique_ptr<definition>> definitions;
|
||||
if (tokens->of() == source::token::type::let)
|
||||
{
|
||||
definitions = parse_definitions();
|
||||
++tokens;
|
||||
}
|
||||
auto parsed_statement = parse_bang_statement();
|
||||
auto definitions = parse_definitions();
|
||||
auto declarations = parse_declarations();
|
||||
auto parsed_statement = parse_statement();
|
||||
|
||||
if (parsed_statement == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<block>(std::move(definitions), std::move(parsed_statement));
|
||||
return std::make_unique<block>(std::move(definitions),
|
||||
std::move(declarations), std::move(parsed_statement));
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<const token>> parser::advance(const token::type token_type)
|
||||
{
|
||||
if (tokens->of() == token_type)
|
||||
{
|
||||
return std::make_optional<>(std::cref(*tokens++));
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
return std::optional<std::reference_wrapper<const token>>();
|
||||
}
|
||||
|
||||
bool parser::skip(const token::type token_type)
|
||||
{
|
||||
if (tokens->of() == token_type)
|
||||
{
|
||||
++tokens;
|
||||
return true;
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user