2015-04-02 11:52:05 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "output.hpp"
|
|
|
|
|
|
|
|
#include <cerrno>
|
2015-04-06 20:34:12 +02:00
|
|
|
#include <cstring>
|
2015-04-02 11:52:05 +02:00
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
|
|
|
|
namespace solar {
|
|
|
|
|
|
|
|
output_t::output_t(const generator_t *generator0, const char *header, const char *source)
|
|
|
|
: prefix_str("parse_"),
|
2015-04-05 02:37:25 +02:00
|
|
|
token_prefix_str("TOK_"),
|
2015-04-02 11:52:05 +02:00
|
|
|
stack_size(100),
|
2015-04-06 20:34:12 +02:00
|
|
|
generator(generator0),
|
|
|
|
header_filename(header) {
|
2015-04-02 11:52:05 +02:00
|
|
|
header_file = std::fopen(header, "w");
|
|
|
|
if (!header_file)
|
|
|
|
throw std::system_error(errno, std::generic_category(), "unable to open header output file for writing");
|
|
|
|
|
|
|
|
source_file = std::fopen(source, "w");
|
|
|
|
if (!source_file)
|
|
|
|
throw std::system_error(errno, std::generic_category(), "unable to open source output file for writing");
|
|
|
|
|
2015-04-06 18:50:03 +02:00
|
|
|
for (const std::string &nonterm : generator->get_nonterminals())
|
2015-04-06 21:16:13 +02:00
|
|
|
symbol_values.insert(std::make_pair(symbol_t::make_nonterm(nonterm.c_str()), "symbol_" + nonterm));
|
2015-04-06 18:50:03 +02:00
|
|
|
|
|
|
|
for (const symbol_t &term : generator->get_terminals()) {
|
|
|
|
if (term.get_type() == SYMBOL_TYPE_TERM)
|
2015-04-06 21:16:13 +02:00
|
|
|
tokens.insert(std::make_pair(term.get_value(), tokens.size()));
|
2015-04-06 18:50:03 +02:00
|
|
|
|
2015-04-06 21:16:13 +02:00
|
|
|
symbol_values.insert(std::make_pair(term, "token." + generator->get_term_type(term).second));
|
2015-04-02 11:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_tokens() {
|
|
|
|
if (tokens.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::fprintf(header_file, "enum %stoken_t {\n", prefix());
|
|
|
|
|
|
|
|
for (const auto &token : tokens)
|
|
|
|
std::fprintf(header_file, "\t%s%s = %u,\n", token_prefix(), token.first.c_str(), token.second + 256);
|
|
|
|
|
|
|
|
std::fprintf(header_file, "};\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_token_value() {
|
2015-04-05 02:28:52 +02:00
|
|
|
std::fprintf(header_file, "typedef struct %stoken_value {\n", prefix());
|
2015-04-06 18:50:03 +02:00
|
|
|
|
|
|
|
std::map<std::string, std::string> token_values;
|
|
|
|
|
|
|
|
for (const auto &term : generator->get_terminals()) {
|
|
|
|
const auto &type = generator->get_term_type(term);
|
|
|
|
if (!type.first.empty())
|
2015-04-06 21:16:13 +02:00
|
|
|
token_values.insert(std::make_pair(type.second, type.first));
|
2015-04-06 18:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &value : token_values)
|
|
|
|
std::fprintf(header_file, "\t%s %s;\n", value.second.c_str(), value.first.c_str());
|
|
|
|
|
2015-04-05 02:28:52 +02:00
|
|
|
std::fprintf(header_file, "} %stoken_value_t;\n\n", prefix());
|
2015-04-02 11:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_header() {
|
2015-04-08 23:49:42 +02:00
|
|
|
std::fprintf(header_file, "#pragma once\n\n");
|
|
|
|
|
|
|
|
if (!generator->get_header_block().empty())
|
|
|
|
std::fprintf(header_file, "%s\n", generator->get_header_block().c_str());
|
|
|
|
|
2015-04-02 11:52:05 +02:00
|
|
|
emit_tokens();
|
|
|
|
emit_token_value();
|
|
|
|
|
|
|
|
std::fprintf(header_file, "typedef struct %scontext %scontext_t;\n", prefix(), prefix());
|
|
|
|
}
|
|
|
|
|
2015-04-06 18:50:03 +02:00
|
|
|
void output_t::emit_reduction(unsigned rule_id) {
|
|
|
|
const auto &rule = generator->get_rules()[rule_id];
|
|
|
|
|
|
|
|
std::fprintf(source_file, "static inline ");
|
|
|
|
|
|
|
|
const item_t &item = std::get<0>(rule);
|
|
|
|
const std::string &type = generator->get_nonterm_type(item.get_lhs());
|
|
|
|
if (type.empty())
|
|
|
|
std::fprintf(source_file, "void");
|
|
|
|
else
|
|
|
|
std::fprintf(source_file, "%s", type.c_str());
|
|
|
|
|
|
|
|
std::fprintf(source_file, " %sreduce_%u(", prefix(), rule_id);
|
|
|
|
|
|
|
|
bool empty = true;
|
|
|
|
for (unsigned i = 0; i < std::get<1>(rule).size(); i++) {
|
|
|
|
const std::string &var = std::get<1>(rule)[i];
|
|
|
|
|
|
|
|
if (var.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!empty)
|
|
|
|
std::fprintf(source_file, ", ");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "%s %s", generator->get_type(item.get_rhs()[i]).c_str(), var.c_str());
|
|
|
|
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty)
|
|
|
|
std::fprintf(source_file, "void");
|
|
|
|
|
|
|
|
std::fprintf(source_file, ") {");
|
|
|
|
std::fprintf(source_file, "%s", std::get<2>(rule).c_str());
|
2015-04-05 02:28:52 +02:00
|
|
|
std::fprintf(source_file, "}\n\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_reductions() {
|
|
|
|
const auto &rules = generator->get_rules();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rules.size(); i++) {
|
2015-04-06 18:50:03 +02:00
|
|
|
if (!std::get<2>(rules[i]).empty())
|
|
|
|
emit_reduction(i);
|
2015-04-05 02:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 11:52:05 +02:00
|
|
|
void output_t::emit_state_shift(unsigned i) {
|
|
|
|
std::fprintf(source_file, "\t\t\tswitch (token) {\n");
|
|
|
|
|
|
|
|
if (generator->get_shifts().find(std::make_pair(i, symbol_t::make_nonterm(""))) != generator->get_shifts().end()) {
|
|
|
|
std::fprintf(source_file, "\t\t\tcase 0:\n");
|
|
|
|
std::fprintf(source_file, "\t\t\t\treturn 0;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &token : generator->get_terminals()) {
|
|
|
|
auto it = generator->get_shifts().find(std::make_pair(i, token));
|
|
|
|
if (it == generator->get_shifts().end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (token.get_type() == SYMBOL_TYPE_CHAR)
|
|
|
|
std::fprintf(source_file, "\t\t\tcase '%c':\n", token.get_value()[0]);
|
|
|
|
else
|
2015-04-05 02:37:25 +02:00
|
|
|
std::fprintf(source_file, "\t\t\tcase %s%s:\n", token_prefix(), token.get_value().c_str());
|
2015-04-02 11:52:05 +02:00
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second));
|
2015-04-06 18:50:03 +02:00
|
|
|
std::fprintf(source_file, "\t\t\t\tparser->stack[parser->top].value.token = *value;\n");
|
2015-04-02 11:52:05 +02:00
|
|
|
std::fprintf(source_file, "\t\t\t\treturn 1;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\tdefault:\n");
|
|
|
|
std::fprintf(source_file, "\t\t\t\treturn -1;\n");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t}\n");
|
|
|
|
}
|
|
|
|
|
2015-04-05 02:28:52 +02:00
|
|
|
void output_t::emit_state_reduce(const item_t &item, int rule_id) {
|
2015-04-06 18:50:03 +02:00
|
|
|
const auto &rhs = item.get_rhs();
|
|
|
|
if (rhs.size())
|
|
|
|
std::fprintf(source_file, "\t\t\tparser->top -= %u;\n", unsigned(rhs.size()));
|
|
|
|
|
|
|
|
if (rule_id >= 0) {
|
|
|
|
const std::string &type = generator->get_nonterm_type(item.get_lhs());
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t");
|
|
|
|
if (!type.empty())
|
|
|
|
std::fprintf(source_file, "parser->stack[parser->top].value.symbol_%s = ", item.get_lhs().c_str());
|
|
|
|
std::fprintf(source_file, "%sreduce_%i(", prefix(), rule_id);
|
|
|
|
|
|
|
|
bool empty = true;
|
|
|
|
const auto &vars = std::get<1>(generator->get_rules()[rule_id]);
|
|
|
|
for (unsigned i = 0; i < vars.size(); i++) {
|
|
|
|
const std::string &var = vars[i];
|
|
|
|
if (var.empty())
|
|
|
|
continue;
|
2015-04-02 11:52:05 +02:00
|
|
|
|
2015-04-06 18:50:03 +02:00
|
|
|
if (!empty)
|
|
|
|
std::fprintf(source_file, ", ");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "parser->stack[parser->top + %u].value.%s", i, symbol_values[rhs[i]].c_str());
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, ");\n");
|
|
|
|
}
|
2015-04-05 02:28:52 +02:00
|
|
|
|
2015-04-02 11:52:05 +02:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> gotos;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < generator->get_state_count(); i++) {
|
|
|
|
auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs()));
|
|
|
|
if (it == generator->get_gotos().end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gotos.emplace_back(i, it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotos.size() == 1) {
|
|
|
|
std::fprintf(source_file, "\t\t\tparser->stack[++parser->top].state = %u;\n", gotos[0].second);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::fprintf(source_file, "\t\t\tswitch (parser->stack[parser->top].state) {\n");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < generator->get_state_count(); i++) {
|
|
|
|
auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs()));
|
|
|
|
if (it == generator->get_gotos().end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\tcase %u:\n", unsigned(i));
|
|
|
|
std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second));
|
|
|
|
std::fprintf(source_file, "\t\t\t\tbreak;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_state(unsigned i) {
|
|
|
|
std::fprintf(source_file, "\t\tcase %u:\n", i);
|
|
|
|
|
|
|
|
auto it = generator->get_reductions().find(i);
|
2015-04-05 02:28:52 +02:00
|
|
|
if (it == generator->get_reductions().end()) {
|
2015-04-02 11:52:05 +02:00
|
|
|
emit_state_shift(i);
|
2015-04-05 02:28:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const auto &rule = generator->get_rules()[it->second];
|
2015-04-06 18:50:03 +02:00
|
|
|
emit_state_reduce(std::get<0>(rule), std::get<2>(rule).empty() ? -1 : it->second);
|
2015-04-05 02:28:52 +02:00
|
|
|
}
|
2015-04-02 11:52:05 +02:00
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\tbreak;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::emit_states() {
|
|
|
|
for (size_t i = 0; i < generator->get_state_count(); i++)
|
|
|
|
emit_state(i);
|
|
|
|
}
|
|
|
|
|
2015-04-06 20:34:12 +02:00
|
|
|
void output_t::emit_header_include() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char sep = '\\';
|
|
|
|
#else
|
|
|
|
const char sep = '/';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *slash = std::strrchr(header_filename.c_str(), sep);
|
|
|
|
const char *basename = slash ? slash+1 : header_filename.c_str();
|
|
|
|
|
2015-04-08 23:49:42 +02:00
|
|
|
std::fprintf(source_file, "#include \"%s\"\n\n", basename);
|
2015-04-06 20:34:12 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 11:52:05 +02:00
|
|
|
void output_t::emit_source() {
|
2015-04-06 20:34:12 +02:00
|
|
|
emit_header_include();
|
|
|
|
|
2015-04-08 23:49:42 +02:00
|
|
|
if (!generator->get_source_block().empty())
|
|
|
|
std::fprintf(source_file, "%s\n\n", generator->get_source_block().c_str());
|
|
|
|
|
2015-04-06 18:50:03 +02:00
|
|
|
std::fprintf(source_file, "typedef union %ssymbol_value {\n", prefix());
|
|
|
|
std::fprintf(source_file, "\t%stoken_value_t token;\n", prefix());
|
|
|
|
|
|
|
|
for (const auto &nonterm : generator->get_nonterminals()) {
|
|
|
|
const std::string &type = generator->get_nonterm_type(nonterm);
|
|
|
|
|
|
|
|
if (!type.empty())
|
|
|
|
std::fprintf(source_file, "\t%s symbol_%s;\n", type.c_str(), nonterm.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, "} %ssymbol_value_t;\n\n", prefix());
|
|
|
|
|
2015-04-02 11:52:05 +02:00
|
|
|
std::fprintf(source_file, "typedef struct %scontext_state {\n", prefix());
|
|
|
|
std::fprintf(source_file, "\tunsigned state;\n");
|
2015-04-06 18:50:03 +02:00
|
|
|
std::fprintf(source_file, "\t%ssymbol_value_t value;\n", prefix());
|
2015-04-02 11:52:05 +02:00
|
|
|
std::fprintf(source_file, "} %scontext_state_t;\n\n", prefix());
|
|
|
|
|
2015-04-05 02:28:52 +02:00
|
|
|
std::fprintf(source_file, "struct %scontext {\n", prefix());
|
2015-04-02 11:52:05 +02:00
|
|
|
std::fprintf(source_file, "\tunsigned top;\n");
|
|
|
|
std::fprintf(source_file, "\t%scontext_state_t stack[%u];\n", prefix(), stack_size);
|
2015-04-05 02:28:52 +02:00
|
|
|
std::fprintf(source_file, "};\n\n");
|
|
|
|
|
|
|
|
emit_reductions();
|
2015-04-02 11:52:05 +02:00
|
|
|
|
|
|
|
std::fprintf(source_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value) {\n", prefix(), prefix(), prefix());
|
|
|
|
std::fprintf(source_file, "\twhile (1) {\n");
|
|
|
|
std::fprintf(source_file, "\t\tswitch (parser->stack[parser->top].state) {\n");
|
|
|
|
|
|
|
|
emit_states();
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t}\n");
|
|
|
|
std::fprintf(source_file, "\t}\n");
|
|
|
|
std::fprintf(source_file, "}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_t::write() {
|
|
|
|
emit_header();
|
|
|
|
emit_source();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|