1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "lex_output_visitor.h"
#include "errors.h"
namespace foundry {
namespace parse {
lex_output_visitor::lex_output_visitor(std::string const &basename, std::ostream &out) :
basename(basename),
out(out)
{
return;
}
void lex_output_visitor::visit(root const &r)
{
out << "%{" << std::endl;
out << "#include \"" << basename << "_cst.hpp\"" << std::endl;
out << "#include \"" << basename << "_parse.hpp\"" << std::endl;
out << "#define YY_USER_ACTION yylloc->first_line = yylloc->last_line = yylineno;" << std::endl;
out << "%}" << std::endl;
out << "" << std::endl;
out << "%option nostdinit" << std::endl;
out << "/*%option nodefault*/" << std::endl;
out << "%option nounput" << std::endl;
out << "%option noyywrap" << std::endl;
out << "%option reentrant" << std::endl;
out << "%option bison-bridge" << std::endl;
out << "%option bison-locations" << std::endl;
out << "%option yylineno" << std::endl;
out << "%option prefix=\"" << basename << "_\"" << std::endl;
out << "IDENT [[:alpha:]_][[:alnum:]_]*" << std::endl;
out << "" << std::endl;
out << "%x COMMENT" << std::endl;
out << "" << std::endl;
out << "%%" << std::endl;
out << "" << std::endl;
out << "<INITIAL>\\/\\* BEGIN(COMMENT);" << std::endl;
out << "<COMMENT>\\*\\/ BEGIN(INITIAL);" << std::endl;
out << "<COMMENT>. /* ignore */" << std::endl;
out << "" << std::endl;
out << "\\ /* ignore */" << std::endl;
out << "\\n /* ignore */" << std::endl;
descend(r.literals);
descend(r.regexes);
}
void lex_output_visitor::visit(rule const &)
{
}
void lex_output_visitor::visit(alternative const &)
{
}
void lex_output_visitor::visit(group const &)
{
}
void lex_output_visitor::visit(regex const &r)
{
std::string const text = r.text.substr(1, r.text.size() - 2);
for(auto i : text)
{
switch(i)
{
case '"':
out << '\\' << i;
break;
default:
out << i;
break;
}
}
out << "\tyylval->string = strdup(yytext); return " << r.name << ";" << std::endl;
}
void lex_output_visitor::visit(string_literal const &l)
{
std::string const text = l.text.substr(1, l.text.size() - 2);
for(auto i : text)
{
switch(i)
{
case ';':
case '|':
case '?':
case '^':
case '*':
case '+':
case '/':
case '\\':
case '(': case ')':
case '<': case '>':
case '[': case ']':
out << '\\' << i;
break;
default:
out << i;
break;
}
}
out << "\treturn " << l.name << ";" << std::endl;
}
void lex_output_visitor::visit(terminal const &)
{
}
void lex_output_visitor::visit(nonterminal const &)
{
}
void lex_output_visitor::visit(unresolved_symbol const &)
{
throw internal_error("unresolved symbol found during output");
}
}
}
|