From 6597de68e459eb5f6f02d2b0662637b04d2baea1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 18 Apr 2015 00:02:05 +0200 Subject: Add support for C++ namespaces --- src/parse.y | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'src/parse.y') diff --git a/src/parse.y b/src/parse.y index 7b46368..0d9bb05 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1,10 +1,16 @@ +%namespace solar + %header { #include "grammar.hpp" } %source { +namespace solar { + typedef std::vector> vars_t; typedef std::pair, vars_t> rhs_t; + +} } @@ -26,23 +32,26 @@ typedef std::pair, vars_t> rhs_t; %type CHAR {unsigned char} c +%type namespace {std::vector *} +%destructor namespace delete + %type rhs {rhs_t *} %destructor rhs delete %type action {std::string *} %destructor action delete -%type symbol {solar::symbol_t *} +%type symbol {symbol_t *} %destructor symbol delete -%type term {solar::symbol_t *} +%type term {symbol_t *} %destructor term delete %type varname {std::string *} %destructor varname delete -%extra_arg {__attribute__((unused)) solar::grammar_t *} grammar +%extra_arg {__attribute__((unused)) grammar_t *} grammar grammar |=; @@ -61,6 +70,10 @@ directive |= "%destructor" symbol(sym) varname(name) { grammar->destructors.insert(std::make_pair(*sym, *name)); } +directive |= "%namespace" namespace(ns) { + grammar->ns = *ns; +} + directive |= "%source" BLOCK(block) { grammar->source_block = *block; } @@ -74,7 +87,7 @@ directive |= "%extra_arg" BLOCK(type) varname(name) { } directive |= SYMBOL(lhs) "|=" rhs(rhs) action(action) { - grammar->add_rule({solar::item_t(*lhs, rhs->first), rhs->second, *action}); + grammar->add_rule({item_t(*lhs, rhs->first), rhs->second, *action}); } directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' ';' { @@ -82,7 +95,7 @@ directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' ';' { vars.emplace_back(); for (unsigned int c = c1; c <= c2; c++) - grammar->add_rule({solar::item_t(*lhs, {solar::symbol_t::make_char(c)}), vars, std::string()}); + grammar->add_rule({item_t(*lhs, {symbol_t::make_char(c)}), vars, std::string()}); } @@ -111,7 +124,7 @@ rhs |= rhs(=rhs) symbol(sym) '(' '=' varname(var) ')' { rhs |= rhs(=rhs) STRING(str) { for (char c : *str) { - rhs->first.push_back(solar::symbol_t::make_char(c)); + rhs->first.push_back(symbol_t::make_char(c)); rhs->second.emplace_back(); } @@ -124,11 +137,18 @@ action |= BLOCK(=v) [v] action |= SQBLOCK(v) [new std::string("return " + *v + ";")] -symbol |= SYMBOL(v) [new solar::symbol_t(solar::symbol_t::make_nonterm(*v))] +namespace |= varname(v) [new std::vector {*v}] +namespace |= namespace(=ns) "::" varname(v) { + ns->push_back(*v); + return ns; +} + + +symbol |= SYMBOL(v) [new symbol_t(symbol_t::make_nonterm(*v))] symbol |= term(=v) [v] -term |= SYMBOL_UC(v) [new solar::symbol_t(solar::symbol_t::make_term(*v))] -term |= CHAR(v) [new solar::symbol_t(solar::symbol_t::make_char(v))] +term |= SYMBOL_UC(v) [new symbol_t(symbol_t::make_term(*v))] +term |= CHAR(v) [new symbol_t(symbol_t::make_char(v))] varname |= SYMBOL_UC(=v) [v] varname |= SYMBOL(=v) [v] -- cgit v1.2.3