From a6cf9b0956f68a1e68fbbd1a2716fae3398c2582 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 18 Apr 2015 18:30:02 +0200 Subject: Refactor parser rule variable handling --- src/parse.y | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/parse.y') diff --git a/src/parse.y b/src/parse.y index 9e445e7..9959b0b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -7,7 +7,8 @@ %source { namespace solar { -typedef std::vector> vars_t; +typedef std::pair var_t; +typedef std::vector vars_t; typedef std::pair, vars_t> rhs_t; } @@ -38,6 +39,9 @@ typedef std::pair, vars_t> rhs_t; %type rhs {rhs_t *} %destructor rhs delete +%type variable {var_t *} +%destructor variable delete + %type action {std::string *} %destructor action delete @@ -50,6 +54,8 @@ typedef std::pair, vars_t> rhs_t; %type csymbol {std::string *} %destructor csymbol delete +%type consume {bool} + %extra_arg {__attribute__((unused)) grammar_t *} grammar @@ -100,23 +106,9 @@ directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' ';' { rhs |= [new rhs_t()] -rhs |= rhs(=rhs) symbol(sym) { - rhs->first.push_back(*sym); - rhs->second.emplace_back(); - - return rhs; -} - -rhs |= rhs(=rhs) symbol(sym) '(' csymbol(var) ')' { +rhs |= rhs(=rhs) symbol(sym) variable(var) { rhs->first.push_back(*sym); - rhs->second.emplace_back(*var, true); - - return rhs; -} - -rhs |= rhs(=rhs) symbol(sym) '(' '=' csymbol(var) ')' { - rhs->first.push_back(*sym); - rhs->second.emplace_back(*var, false); + rhs->second.push_back(*var); return rhs; } @@ -131,6 +123,13 @@ rhs |= rhs(=rhs) STRING(str) { } +variable |= [new var_t] +variable |= '(' consume(consume) csymbol(var) ')' [new var_t(*var, consume)] + +consume |= [false] +consume |= '=' [true] + + action |= ';' [new std::string] action |= BLOCK(=v) [v] action |= SQBLOCK(v) [new std::string("return " + *v + ";")] -- cgit v1.2.3