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

Begin clean-up and tests in language

parent 0a085f8a
No related branches found
No related tags found
1 merge request!17Feature/language
#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 >,
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)
{
if (v.size() > 0) {
v += std::string(", I:") + in.string();
} else {
v = std::string("I:") + in.string();
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 my_action<real>
struct action<identifier>
{
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 += std::string("S:") + in.string() + std::string(" ;\n");
}
};
template <>
struct action<binary_op>
{
template <typename Input>
static void
apply(const Input& in, std::string& v)
{
v += "binary_op(" + in.string() + std::string(") ;\n");
}
};
......@@ -135,17 +157,23 @@ 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
parse<language::grammar,
language::action //, language::errors
>(in, name);
} catch (const language::parse_error& e) {
} catch (const parse_error& e) {
const auto p = e.positions.front();
std::cerr << rang::style::bold << p.source << ':' << p.line << ':'
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'
......@@ -155,6 +183,5 @@ parser(const std::string& filename)
std::exit(1);
}
std::cout << "Good bye, " << name << "!" << std::endl;
std::exit(0);
pout() << "Parsed:\n" << name << std::endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment