diff --git a/src/language/ASTBuilder.cpp b/src/language/ASTBuilder.cpp index 1b7f36b275e57ec7d4357d6c97ae18ca0ab194ca..6dbadf26307ff20d7760f3b105fb7646a730866f 100644 --- a/src/language/ASTBuilder.cpp +++ b/src/language/ASTBuilder.cpp @@ -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,73 +199,72 @@ struct simplify_stream_statement : parse_tree::apply<simplify_stream_statement> }; template <typename Rule> -using selector = parse_tree::selector<Rule, - parse_tree::store_content::on<true_kw, - false_kw, - integer, - real, - literal, - name, - B_set, - N_set, - Z_set, - R_set, - string_type, - cout_kw, - cerr_kw, - clog_kw, - declaration, - if_statement, - do_while_statement, - while_statement, - for_statement, - break_kw, - continue_kw>, - rearrange::on<product, affectation, expression>, - simplify_unary::on<unary_minus, unary_plus, unary_not, unary_expression>, - parse_tree::remove_content::on<plus_op, - minus_op, - multiply_op, - divide_op, - lesser_op, - lesser_or_eq_op, - greater_op, - greater_or_eq_op, - eqeq_op, - not_eq_op, - and_op, - or_op, - xor_op, - bitand_op, - bitor_op, - eq_op, - multiplyeq_op, - divideeq_op, - pluseq_op, - minuseq_op, - bit_andeq_op, - bit_xoreq_op, - bit_oreq_op, - unary_plusplus, - unary_minusminus, - post_minusminus, - post_plusplus>, - 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 +using selector = + parse_tree::selector<Rule, + parse_tree::store_content::on<true_kw, + false_kw, + integer, + real, + literal, + name, + B_set, + N_set, + Z_set, + R_set, + string_type, + cout_kw, + cerr_kw, + clog_kw, + declaration, + if_statement, + do_while_statement, + while_statement, + for_statement, + break_kw, + continue_kw>, + 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, + divide_op, + lesser_op, + lesser_or_eq_op, + greater_op, + greater_or_eq_op, + eqeq_op, + not_eq_op, + and_op, + or_op, + xor_op, + bitand_op, + bitor_op, + eq_op, + multiplyeq_op, + divideeq_op, + pluseq_op, + minuseq_op, + bit_andeq_op, + bit_xoreq_op, + bit_oreq_op, + unary_plusplus, + unary_minusminus, + post_minusminus, + post_plusplus>, + ASTBuilder::simplify_for_statement_bloc::on<for_statement_bloc>, + parse_tree::discard_empty::on<ignored, semicol, bloc>, + 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); diff --git a/src/language/ASTBuilder.hpp b/src/language/ASTBuilder.hpp index 76388f3403c4a666bbb755df696e2e6f05a27314..0fc767cd8ed8f0a6b6633918fcae24a922a68d64 100644 --- a/src/language/ASTBuilder.hpp +++ b/src/language/ASTBuilder.hpp @@ -1,11 +1,24 @@ #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; -template <typename InputT> -std::unique_ptr<language::Node> buildAST(InputT& input); + public: + template <typename InputT> + static std::unique_ptr<language::Node> build(InputT& input); +}; #endif // AST_BUILDER_HPP diff --git a/src/language/PugsParser.cpp b/src/language/PugsParser.cpp index 69c945a9d2b1dc6223b784f02f942c79a4daff98..dd465fd7f471d5faf09adc1b219fea8a96dbf7f2 100644 --- a/src/language/PugsParser.cpp +++ b/src/language/PugsParser.cpp @@ -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}; diff --git a/tests/test_ASTBuilder.cpp b/tests/test_ASTBuilder.cpp index 1d661bfd891583a701013cc772164dcd838aec0e..1aef598c1180da9593046c77873e9d512976af07 100644 --- a/tests/test_ASTBuilder.cpp +++ b/tests/test_ASTBuilder.cpp @@ -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); } } }