Skip to content
Snippets Groups Projects
Commit b5225b99 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Reorganize ASTBuilder to be a class

parent 9cb762eb
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -7,9 +7,9 @@ using namespace TAO_PEGTL_NAMESPACE;
#include <pegtl/contrib/parse_tree.hpp>
namespace language
{
struct rearrange : parse_tree::apply<rearrange>
using namespace language;
struct ASTBuilder::rearrange : parse_tree::apply<ASTBuilder::rearrange>
{
template <typename... States>
static void
......@@ -55,7 +55,7 @@ struct rearrange : parse_tree::apply<rearrange>
}
};
struct simplify_unary : parse_tree::apply<simplify_unary>
struct ASTBuilder::simplify_unary : parse_tree::apply<ASTBuilder::simplify_unary>
{
template <typename... States>
static void
......@@ -110,7 +110,7 @@ struct simplify_unary : parse_tree::apply<simplify_unary>
}
};
struct simplify_statement_bloc : parse_tree::apply<simplify_statement_bloc>
struct ASTBuilder::simplify_statement_bloc : parse_tree::apply<ASTBuilder::simplify_statement_bloc>
{
template <typename... States>
static void
......@@ -128,7 +128,7 @@ struct simplify_statement_bloc : parse_tree::apply<simplify_statement_bloc>
}
};
struct simplify_for_statement_bloc : parse_tree::apply<simplify_for_statement_bloc>
struct ASTBuilder::simplify_for_statement_bloc : parse_tree::apply<ASTBuilder::simplify_for_statement_bloc>
{
template <typename... States>
static void
......@@ -142,7 +142,7 @@ struct simplify_for_statement_bloc : parse_tree::apply<simplify_for_statement_bl
}
};
struct simplify_for_init : parse_tree::apply<simplify_for_init>
struct ASTBuilder::simplify_for_init : parse_tree::apply<ASTBuilder::simplify_for_init>
{
template <typename... States>
static void
......@@ -156,7 +156,7 @@ struct simplify_for_init : parse_tree::apply<simplify_for_init>
}
};
struct simplify_for_test : parse_tree::apply<simplify_for_test>
struct ASTBuilder::simplify_for_test : parse_tree::apply<ASTBuilder::simplify_for_test>
{
template <typename... States>
static void
......@@ -170,7 +170,7 @@ struct simplify_for_test : parse_tree::apply<simplify_for_test>
}
};
struct simplify_for_post : parse_tree::apply<simplify_for_post>
struct ASTBuilder::simplify_for_post : parse_tree::apply<ASTBuilder::simplify_for_post>
{
template <typename... States>
static void
......@@ -184,7 +184,7 @@ struct simplify_for_post : parse_tree::apply<simplify_for_post>
}
};
struct simplify_stream_statement : parse_tree::apply<simplify_stream_statement>
struct ASTBuilder::simplify_stream_statement : parse_tree::apply<ASTBuilder::simplify_stream_statement>
{
template <typename... States>
static void
......@@ -199,7 +199,8 @@ struct simplify_stream_statement : parse_tree::apply<simplify_stream_statement>
};
template <typename Rule>
using selector = parse_tree::selector<Rule,
using selector =
parse_tree::selector<Rule,
parse_tree::store_content::on<true_kw,
false_kw,
integer,
......@@ -221,8 +222,8 @@ using selector = parse_tree::selector<Rule,
for_statement,
break_kw,
continue_kw>,
rearrange::on<product, affectation, expression>,
simplify_unary::on<unary_minus, unary_plus, unary_not, unary_expression>,
ASTBuilder::rearrange::on<product, affectation, expression>,
ASTBuilder::simplify_unary::on<unary_minus, unary_plus, unary_not, unary_expression>,
parse_tree::remove_content::on<plus_op,
minus_op,
multiply_op,
......@@ -250,22 +251,20 @@ using selector = parse_tree::selector<Rule,
unary_minusminus,
post_minusminus,
post_plusplus>,
simplify_for_statement_bloc::on<for_statement_bloc>,
ASTBuilder::simplify_for_statement_bloc::on<for_statement_bloc>,
parse_tree::discard_empty::on<ignored, semicol, bloc>,
simplify_statement_bloc::on<statement_bloc>,
simplify_for_init::on<for_init>,
simplify_for_test::on<for_test>,
simplify_for_post::on<for_post>,
simplify_stream_statement::on<ostream_statement>>;
} // namespace language
ASTBuilder::simplify_statement_bloc::on<statement_bloc>,
ASTBuilder::simplify_for_init::on<for_init>,
ASTBuilder::simplify_for_test::on<for_test>,
ASTBuilder::simplify_for_post::on<for_post>,
ASTBuilder::simplify_stream_statement::on<ostream_statement>>;
template <typename InputT>
std::unique_ptr<language::Node>
buildAST(InputT& input)
ASTBuilder::build(InputT& input)
{
return parse_tree::parse<language::grammar, language::Node, language::selector, nothing, language::errors>(input);
return parse_tree::parse<language::grammar, language::Node, selector, nothing, language::errors>(input);
}
template std::unique_ptr<language::Node> buildAST(read_input<>& input);
template std::unique_ptr<language::Node> buildAST(string_input<>& input);
template std::unique_ptr<language::Node> ASTBuilder::build(read_input<>& input);
template std::unique_ptr<language::Node> ASTBuilder::build(string_input<>& input);
#ifndef AST_BUILDER_HPP
#define AST_BUILDER_HPP
#include <ASTNode.hpp>
#include <pegtl.hpp>
#include <ASTNode.hpp>
struct ASTBuilder
{
private:
struct rearrange;
struct simplify_unary;
struct simplify_statement_bloc;
struct simplify_for_statement_bloc;
struct simplify_for_init;
struct simplify_for_test;
struct simplify_for_post;
struct simplify_stream_statement;
public:
template <typename InputT>
std::unique_ptr<language::Node> buildAST(InputT& input);
static std::unique_ptr<language::Node> build(InputT& input);
};
#endif // AST_BUILDER_HPP
......@@ -462,7 +462,7 @@ parser(const std::string& filename)
std::unique_ptr<language::Node> root_node;
read_input input(filename);
try {
root_node = buildAST(input);
root_node = ASTBuilder::build(input);
std::cout << " - AST is built ...... [done]\n";
language::ASTSymbolTableBuilder{*root_node};
......
......@@ -11,7 +11,7 @@
using namespace language; \
\
string_input input{data, "test.pgs"}; \
auto ast = buildAST(input); \
auto ast = ASTBuilder::build(input); \
\
std::stringstream ast_output; \
ast_output << '\n' << ASTPrinter{*ast, ASTPrinter::Format::raw, {ASTPrinter::Info::none}}; \
......@@ -594,7 +594,7 @@ clog << "log " << l << "\n";
using namespace language;
string_input input{data, "test.pgs"};
REQUIRE_THROWS_AS(buildAST(input), parse_error);
REQUIRE_THROWS_AS(ASTBuilder::build(input), parse_error);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment