diff --git a/src/language/PugsParser.cpp b/src/language/PugsParser.cpp index ed2be7c163de9ec37bff8205039001d1f0805e07..e8133acc78b8924325d51d77e93b64bd955eca3d 100644 --- a/src/language/PugsParser.cpp +++ b/src/language/PugsParser.cpp @@ -1,14 +1,14 @@ +#include <PugsOStream.hpp> #include <PugsParser.hpp> -#include <iostream> #include <rang.hpp> -#define TAO_PEGTL_NAMESPACE language #include <pegtl.hpp> #include <pegtl/analyze.hpp> +using namespace TAO_PEGTL_NAMESPACE; + namespace language { -using namespace language; // clang-format off @@ -60,13 +60,21 @@ struct real struct REAL : seq< real, ignored >{}; -struct expression : sor< REAL, INTEGER > {}; +struct IDENTIFIER : seq <identifier, ignored> {}; + +struct expression : sor< REAL, INTEGER , IDENTIFIER> {}; + +struct EXPRESSION : seq< expression, ignored> {}; + +struct binary_op + : seq< EXPRESSION , one< '+' >, EXPRESSION> {}; struct semicol : one< ';' >{}; struct SEMICOL : seq< semicol , ignored > {}; struct instruction - : sor<seq< expression , SEMICOL>, + : sor<seq< expression , SEMICOL >, + seq< binary_op, SEMICOL >, SEMICOL> {}; @@ -75,36 +83,50 @@ struct grammar // clang-format on template <typename Rule> -struct my_action : nothing<Rule> +struct action : nothing<Rule> {}; template <> -struct my_action<integer> +struct action<integer> +{ + template <typename Input> + static void + apply(const Input& in, std::string& v) + { + v += std::string("I:") + in.string() + std::string(" ;\n"); + } +}; + +template <> +struct action<real> +{ + template <typename Input> + static void + apply(const Input& in, std::string& v) + { + v += std::string("R:") + in.string() + std::string(" ;\n"); + } +}; + +template <> +struct action<identifier> { template <typename Input> static void apply(const Input& in, std::string& v) { - if (v.size() > 0) { - v += std::string(", I:") + in.string(); - } else { - v = std::string("I:") + in.string(); - } + v += std::string("S:") + in.string() + std::string(" ;\n"); } }; template <> -struct my_action<real> +struct action<binary_op> { template <typename Input> static void apply(const Input& in, std::string& v) { - if (v.size() > 0) { - v += std::string(", R:") + in.string(); - } else { - v = std::string("R:") + in.string(); - } + v += "binary_op(" + in.string() + std::string(") ;\n"); } }; @@ -135,26 +157,31 @@ parser(const std::string& filename) { std::string name; - const size_t grammar_issues = language::analyze<language::grammar>(); + const size_t grammar_issues = analyze<language::grammar>(); + + pout() << rang::fgB::yellow << "grammar_issues=" << rang::fg::reset + << grammar_issues << '\n'; + + pout() << rang::style::bold << "Parsing file " << rang::style::reset + << rang::style::underline << filename << rang::style::reset + << " ...\n\n"; - std::cout << "grammar_issues=" << grammar_issues << '\n'; - language::read_input in(filename); + read_input in(filename); try { - language::parse<language::grammar, - language::my_action //, language::errors - >(in, name); - } catch (const language::parse_error& e) { + parse<language::grammar, + language::action //, language::errors + >(in, name); + } catch (const parse_error& e) { const auto p = e.positions.front(); - std::cerr << rang::style::bold << p.source << ':' << p.line << ':' - << p.byte_in_line << ": " << rang::style::reset << rang::fgB::red - << "error: " << rang::fg::reset << rang::style::bold << e.what() - << rang::style::reset << '\n' - << in.line_at(p) << '\n' - << std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' - << rang::fg::reset << std::endl; + perr() << rang::style::bold << p.source << ':' << p.line << ':' + << p.byte_in_line << ": " << rang::style::reset << rang::fgB::red + << "error: " << rang::fg::reset << rang::style::bold << e.what() + << rang::style::reset << '\n' + << in.line_at(p) << '\n' + << std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' + << rang::fg::reset << std::endl; std::exit(1); } - std::cout << "Good bye, " << name << "!" << std::endl; - std::exit(0); + pout() << "Parsed:\n" << name << std::endl; }