Skip to content

Commit

Permalink
fix: Add m4_define_default PARSER_NS for namespace overwritability
Browse files Browse the repository at this point in the history
  • Loading branch information
sillydan1 committed Aug 25, 2022
1 parent 3cb3e53 commit 8b896b1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
20 changes: 10 additions & 10 deletions src/parser/lex/footer.l
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
m4_changequote()

yy::parser::symbol_type make_NUMBER(const std::string &s, const yy::parser::location_type& loc) {
PARSER_NS ::parser::symbol_type make_NUMBER(const std::string &s, const PARSER_NS ::parser::location_type& loc) {
errno = 0;
long n = strtol (s.c_str(), NULL, 10);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error (loc, "integer is out of range: " + s);
return yy::parser::make_NUMBER ((int) n, loc);
throw PARSER_NS ::parser::syntax_error (loc, "integer is out of range: " + s);
return PARSER_NS ::parser::make_NUMBER ((int) n, loc);
}

yy::parser::symbol_type make_FLOAT(const std::string &s, const yy::parser::location_type& loc) {
PARSER_NS ::parser::symbol_type make_FLOAT(const std::string &s, const PARSER_NS ::parser::location_type& loc) {
try {
double n = std::stod(s.c_str());
return yy::parser::make_FLOAT((double)n, loc);
return PARSER_NS ::parser::make_FLOAT((double)n, loc);
} catch(std::out_of_range& e) {
throw yy::parser::syntax_error (loc, "double is out of range: " + s);
throw PARSER_NS ::parser::syntax_error (loc, "double is out of range: " + s);
}
}

yy::parser::symbol_type make_STRING(const std::string &s, const yy::parser::location_type& loc) {
return yy::parser::make_STRING(s.substr(1, s.size()-2), loc);
PARSER_NS ::parser::symbol_type make_STRING(const std::string &s, const PARSER_NS ::parser::location_type& loc) {
return PARSER_NS ::parser::make_STRING(s.substr(1, s.size()-2), loc);
}

yy::parser::symbol_type make_BOOL(std::string s, const yy::parser::location_type& loc) {
PARSER_NS ::parser::symbol_type make_BOOL(std::string s, const PARSER_NS ::parser::location_type& loc) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
bool b;
std::istringstream(s) >> std::boolalpha >> b;
return yy::parser::make_BOOL(b, loc);
return PARSER_NS ::parser::make_BOOL(b, loc);
}
52 changes: 26 additions & 26 deletions src/parser/lex/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ m4_changequote()
{blank}+ loc.step();
\n+ { loc.lines(yyleng); loc.step(); }

"-" return yy::parser::make_MINUS (loc);
"+" return yy::parser::make_PLUS (loc);
"*" return yy::parser::make_STAR (loc);
"/" return yy::parser::make_SLASH (loc);
"%" return yy::parser::make_PERCENT(loc);
"^" return yy::parser::make_HAT (loc);
"&&" return yy::parser::make_AND (loc);
"||" return yy::parser::make_OR (loc);
"^^" return yy::parser::make_XOR (loc);
"=>" return yy::parser::make_IMPLIES(loc);
">" return yy::parser::make_GT (loc);
">=" return yy::parser::make_GE (loc);
"==" return yy::parser::make_EE (loc);
"!=" return yy::parser::make_NE (loc);
"<=" return yy::parser::make_LE (loc);
"<" return yy::parser::make_LT (loc);
"!" return yy::parser::make_NOT (loc);
"(" return yy::parser::make_LPAREN (loc);
")" return yy::parser::make_RPAREN (loc);
":=" return yy::parser::make_ASSIGN (loc);
";" return yy::parser::make_TERM (loc);
"-" return PARSER_NS ::parser::make_MINUS (loc);
"+" return PARSER_NS ::parser::make_PLUS (loc);
"*" return PARSER_NS ::parser::make_STAR (loc);
"/" return PARSER_NS ::parser::make_SLASH (loc);
"%" return PARSER_NS ::parser::make_PERCENT(loc);
"^" return PARSER_NS ::parser::make_HAT (loc);
"&&" return PARSER_NS ::parser::make_AND (loc);
"||" return PARSER_NS ::parser::make_OR (loc);
"^^" return PARSER_NS ::parser::make_XOR (loc);
"=>" return PARSER_NS ::parser::make_IMPLIES(loc);
">" return PARSER_NS ::parser::make_GT (loc);
">=" return PARSER_NS ::parser::make_GE (loc);
"==" return PARSER_NS ::parser::make_EE (loc);
"!=" return PARSER_NS ::parser::make_NE (loc);
"<=" return PARSER_NS ::parser::make_LE (loc);
"<" return PARSER_NS ::parser::make_LT (loc);
"!" return PARSER_NS ::parser::make_NOT (loc);
"(" return PARSER_NS ::parser::make_LPAREN (loc);
")" return PARSER_NS ::parser::make_RPAREN (loc);
":=" return PARSER_NS ::parser::make_ASSIGN (loc);
";" return PARSER_NS ::parser::make_TERM (loc);

{accmod} return yy::parser::make_ACCMOD(loc);
{type} return yy::parser::make_TYPE(loc);
{accmod} return PARSER_NS ::parser::make_ACCMOD(loc);
{type} return PARSER_NS ::parser::make_TYPE(loc);

{int} return make_NUMBER(yytext, loc);
{flt} return make_FLOAT(yytext, loc);
{str} return make_STRING(yytext, loc);
{bool} return make_BOOL(yytext, loc);
{id} return yy::parser::make_IDENTIFIER (yytext, loc);
. { throw yy::parser::syntax_error(loc, "invalid character: " + std::string(yytext)); }
<<EOF>> return yy::parser::make_YYEOF (loc);
{id} return PARSER_NS ::parser::make_IDENTIFIER (yytext, loc);
. { throw PARSER_NS ::parser::syntax_error(loc, "invalid character: " + std::string(yytext)); }
<<EOF>> return PARSER_NS ::parser::make_YYEOF (loc);
2 changes: 1 addition & 1 deletion src/parser/lex/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ m4_changequote()
/*
m4_include(../mit.license)
*/

m4_define(PARSER_NS, yy)
m4_include(includes.l)
m4_include(skeleton.l)
m4_include(tokens.l)
Expand Down
8 changes: 4 additions & 4 deletions src/parser/lex/skeleton.l
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ m4_changequote()

%{
// A number symbol corresponding to the value in provided string.
yy::parser::symbol_type make_NUMBER(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_FLOAT(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_STRING(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_BOOL(std::string s, const yy::parser::location_type& loc);
PARSER_NS ::parser::symbol_type make_NUMBER(const std::string &s, const PARSER_NS ::parser::location_type& loc);
PARSER_NS ::parser::symbol_type make_FLOAT(const std::string &s, const PARSER_NS ::parser::location_type& loc);
PARSER_NS ::parser::symbol_type make_STRING(const std::string &s, const PARSER_NS ::parser::location_type& loc);
PARSER_NS ::parser::symbol_type make_BOOL(std::string s, const PARSER_NS ::parser::location_type& loc);
%}
2 changes: 2 additions & 0 deletions src/parser/yacc/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ m4_changequote()
/*
m4_include(../mit.license)
*/
m4_define(PARSER_NS, yy)
m4_include(skeleton.y)
%define api.prefix {PARSER_NS}
m4_include(tokens.y)
m4_include(token_types.y)

Expand Down
2 changes: 1 addition & 1 deletion src/parser/yacc/skeleton.y
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ m4_changequote()
// Include the driver
%code {
#include "drivers/driver.h"
void yy::parser::error (const location_type& l, const std::string& m) {
void PARSER_NS ::parser::error (const location_type& l, const std::string& m) {
std::cerr << l << ": " << m << '\n';
}
}

0 comments on commit 8b896b1

Please sign in to comment.