From 69d5b76e1a56a0e2f537b06b133921b45f490371 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 11 Apr 2015 01:47:28 +0200 Subject: Add support for square-bracketed actions --- src/lex.cpp | 13 ++-- src/lex.hpp | 2 +- src/parse.cpp | 222 +++++++++++++++++++++++++++++++--------------------------- src/parse.hpp | 9 +-- src/parse.y | 36 ++++------ 5 files changed, 146 insertions(+), 136 deletions(-) diff --git a/src/lex.cpp b/src/lex.cpp index 3058232..2d56fef 100644 --- a/src/lex.cpp +++ b/src/lex.cpp @@ -166,7 +166,7 @@ int lex_t::unterminated_block(parse_token_value_t *value) { return -1; } -int lex_t::lex_block(parse_token_value_t *value) { +int lex_t::lex_block(parse_token_value_t *value, int token, char open, char close) { size_t parens = 0; bool line_comment = false; bool block_comment = false; @@ -202,10 +202,10 @@ int lex_t::lex_block(parse_token_value_t *value) { pp = false; } else { - if (cur == '{') { + if (cur == open) { parens++; } - else if (cur == '}') { + else if (cur == close) { if (!parens) break; @@ -234,7 +234,7 @@ int lex_t::lex_block(parse_token_value_t *value) { next(true); consume(true); - return TOK_BLOCK; + return token; } int lex_t::lex_symbol(parse_token_value_t *value) { @@ -403,7 +403,10 @@ int lex_t::lex(parse_token_value_t *value) { return TOK_CHAR; case '{': - return lex_block(value); + return lex_block(value, TOK_BLOCK, '{', '}'); + + case '[': + return lex_block(value, TOK_SQBLOCK, '[', ']'); case '"': return lex_string(value); diff --git a/src/lex.hpp b/src/lex.hpp index 9ab6113..9a727a9 100644 --- a/src/lex.hpp +++ b/src/lex.hpp @@ -70,7 +70,7 @@ private: int unterminated_string(parse_token_value_t *value); int lex_string(parse_token_value_t *value); - int lex_block(parse_token_value_t *value); + int lex_block(parse_token_value_t *value, int token, char open, char close); int lex_symbol(parse_token_value_t *value); char current() { diff --git a/src/parse.cpp b/src/parse.cpp index afeafda..faf9618 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -126,35 +126,40 @@ static inline std::string * parse_reduce_15(std::string * v, __attribute__((unus return v; } -static inline solar::symbol_t * parse_reduce_16(solar::symbol_t * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline std::string * parse_reduce_16(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { + *v = "\n\treturn " + *v + ";\n"; return v; } -static inline solar::symbol_t * parse_reduce_17(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline solar::symbol_t * parse_reduce_17(solar::symbol_t * v, __attribute__((unused)) solar::grammar_t * grammar) { + return v; +} + +static inline solar::symbol_t * parse_reduce_18(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_nonterm(*v)); delete v; return ret; } -static inline solar::symbol_t * parse_reduce_18(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline solar::symbol_t * parse_reduce_19(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_term(*v)); delete v; return ret; } -static inline solar::symbol_t * parse_reduce_19(char v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline solar::symbol_t * parse_reduce_20(char v, __attribute__((unused)) solar::grammar_t * grammar) { return new solar::symbol_t(solar::symbol_t::make_char(v)); } -static inline std::string * parse_reduce_20(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline std::string * parse_reduce_21(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { return v; } -static inline std::string * parse_reduce_21(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline std::string * parse_reduce_22(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { return v; } -static inline std::string * parse_reduce_22(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { +static inline std::string * parse_reduce_23(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { return v; } @@ -354,22 +359,26 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus parser->stack[++parser->top].state = 25; return 1; - case TOK_STRING: + case TOK_SQBLOCK: parser->stack[++parser->top].state = 26; return 1; - case TOK_SYMBOL_LC: + case TOK_STRING: parser->stack[++parser->top].state = 27; return 1; - case TOK_SYMBOL_UC: + case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 28; return 1; - case ';': + case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 29; return 1; + case ';': + parser->stack[++parser->top].state = 30; + return 1; + default: return -1; } @@ -378,7 +387,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 17: switch (token) { case 'r': - parser->stack[++parser->top].state = 30; + parser->stack[++parser->top].state = 31; return 1; default: @@ -389,7 +398,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 18: switch (token) { case 'd': - parser->stack[++parser->top].state = 31; + parser->stack[++parser->top].state = 32; return 1; default: @@ -400,7 +409,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 19: switch (token) { case 'r': - parser->stack[++parser->top].state = 32; + parser->stack[++parser->top].state = 33; return 1; default: @@ -411,7 +420,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 20: switch (token) { case 'e': - parser->stack[++parser->top].state = 33; + parser->stack[++parser->top].state = 34; return 1; default: @@ -431,7 +440,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 22: switch (token) { case '(': - parser->stack[++parser->top].state = 34; + parser->stack[++parser->top].state = 35; return 1; default: @@ -445,7 +454,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_symbol = parse_reduce_16(parser->stack[parser->top + 0].value.symbol_term, grammar); + parser->stack[parser->top].value.symbol_symbol = parse_reduce_17(parser->stack[parser->top + 0].value.symbol_term, grammar); parser->stack[++parser->top].state = 22; } break; @@ -463,14 +472,14 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_term = parse_reduce_19(parser->stack[parser->top + 0].value.token.c, grammar); + parser->stack[parser->top].value.symbol_term = parse_reduce_20(parser->stack[parser->top + 0].value.token.c, grammar); switch (parser->stack[parser->top].state) { case 16: parser->stack[++parser->top].state = 23; break; - case 33: - parser->stack[++parser->top].state = 38; + case 34: + parser->stack[++parser->top].state = 39; break; } @@ -478,6 +487,15 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus break; case 26: + switch (token) { + default: + parser->top -= 1; + parser->stack[parser->top].value.symbol_action = parse_reduce_16(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[++parser->top].state = 21; + } + break; + + case 27: switch (token) { default: parser->top -= 2; @@ -486,34 +504,34 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 27: + case 28: switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_symbol = parse_reduce_17(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[parser->top].value.symbol_symbol = parse_reduce_18(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[++parser->top].state = 22; } break; - case 28: + case 29: switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_term = parse_reduce_18(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[parser->top].value.symbol_term = parse_reduce_19(parser->stack[parser->top + 0].value.token.str, grammar); switch (parser->stack[parser->top].state) { case 16: parser->stack[++parser->top].state = 23; break; - case 33: - parser->stack[++parser->top].state = 38; + case 34: + parser->stack[++parser->top].state = 39; break; } } break; - case 29: + case 30: switch (token) { default: parser->top -= 1; @@ -522,10 +540,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 30: + case 31: switch (token) { case 'a': - parser->stack[++parser->top].state = 35; + parser->stack[++parser->top].state = 36; return 1; default: @@ -533,10 +551,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 31: + case 32: switch (token) { case 'e': - parser->stack[++parser->top].state = 36; + parser->stack[++parser->top].state = 37; return 1; default: @@ -544,10 +562,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 32: + case 33: switch (token) { case 'c': - parser->stack[++parser->top].state = 37; + parser->stack[++parser->top].state = 38; return 1; default: @@ -555,18 +573,18 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 33: + case 34: switch (token) { case TOK_CHAR: parser->stack[++parser->top].state = 25; return 1; case TOK_SYMBOL_UC: - parser->stack[++parser->top].state = 28; + parser->stack[++parser->top].state = 29; return 1; case TOK_SYMBOL_LC: - parser->stack[++parser->top].state = 39; + parser->stack[++parser->top].state = 40; return 1; default: @@ -574,18 +592,18 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 34: + case 35: switch (token) { case TOK_SYMBOL: - parser->stack[++parser->top].state = 41; + parser->stack[++parser->top].state = 42; return 1; case TOK_SYMBOL_LC: - parser->stack[++parser->top].state = 42; + parser->stack[++parser->top].state = 43; return 1; case TOK_SYMBOL_UC: - parser->stack[++parser->top].state = 43; + parser->stack[++parser->top].state = 44; return 1; default: @@ -593,10 +611,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 35: + case 36: switch (token) { case '_': - parser->stack[++parser->top].state = 44; + parser->stack[++parser->top].state = 45; return 1; default: @@ -604,10 +622,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 36: + case 37: switch (token) { case 'r': - parser->stack[++parser->top].state = 45; + parser->stack[++parser->top].state = 46; return 1; default: @@ -615,10 +633,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 37: + case 38: switch (token) { case 'e': - parser->stack[++parser->top].state = 46; + parser->stack[++parser->top].state = 47; return 1; default: @@ -626,10 +644,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 38: + case 39: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 47; + parser->stack[++parser->top].state = 48; return 1; default: @@ -637,10 +655,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 39: + case 40: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 48; + parser->stack[++parser->top].state = 49; return 1; default: @@ -648,10 +666,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 40: + case 41: switch (token) { case ')': - parser->stack[++parser->top].state = 49; + parser->stack[++parser->top].state = 50; return 1; default: @@ -659,76 +677,76 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 41: + case 42: switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_varname = parse_reduce_22(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[parser->top].value.symbol_varname = parse_reduce_23(parser->stack[parser->top + 0].value.token.str, grammar); switch (parser->stack[parser->top].state) { - case 34: - parser->stack[++parser->top].state = 40; + case 35: + parser->stack[++parser->top].state = 41; break; - case 47: - parser->stack[++parser->top].state = 53; + case 48: + parser->stack[++parser->top].state = 54; break; - case 56: - parser->stack[++parser->top].state = 57; + case 57: + parser->stack[++parser->top].state = 58; break; } } break; - case 42: + case 43: switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_varname = parse_reduce_20(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[parser->top].value.symbol_varname = parse_reduce_21(parser->stack[parser->top + 0].value.token.str, grammar); switch (parser->stack[parser->top].state) { - case 34: - parser->stack[++parser->top].state = 40; + case 35: + parser->stack[++parser->top].state = 41; break; - case 47: - parser->stack[++parser->top].state = 53; + case 48: + parser->stack[++parser->top].state = 54; break; - case 56: - parser->stack[++parser->top].state = 57; + case 57: + parser->stack[++parser->top].state = 58; break; } } break; - case 43: + case 44: switch (token) { default: parser->top -= 1; - parser->stack[parser->top].value.symbol_varname = parse_reduce_21(parser->stack[parser->top + 0].value.token.str, grammar); + parser->stack[parser->top].value.symbol_varname = parse_reduce_22(parser->stack[parser->top + 0].value.token.str, grammar); switch (parser->stack[parser->top].state) { - case 34: - parser->stack[++parser->top].state = 40; + case 35: + parser->stack[++parser->top].state = 41; break; - case 47: - parser->stack[++parser->top].state = 53; + case 48: + parser->stack[++parser->top].state = 54; break; - case 56: - parser->stack[++parser->top].state = 57; + case 57: + parser->stack[++parser->top].state = 58; break; } } break; - case 44: + case 45: switch (token) { case 'a': - parser->stack[++parser->top].state = 50; + parser->stack[++parser->top].state = 51; return 1; default: @@ -736,10 +754,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 45: + case 46: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 51; + parser->stack[++parser->top].state = 52; return 1; default: @@ -747,10 +765,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 46: + case 47: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 52; + parser->stack[++parser->top].state = 53; return 1; default: @@ -758,18 +776,18 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 47: + case 48: switch (token) { case TOK_SYMBOL: - parser->stack[++parser->top].state = 41; + parser->stack[++parser->top].state = 42; return 1; case TOK_SYMBOL_LC: - parser->stack[++parser->top].state = 42; + parser->stack[++parser->top].state = 43; return 1; case TOK_SYMBOL_UC: - parser->stack[++parser->top].state = 43; + parser->stack[++parser->top].state = 44; return 1; default: @@ -777,7 +795,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 48: + case 49: switch (token) { default: parser->top -= 7; @@ -786,7 +804,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 49: + case 50: switch (token) { default: parser->top -= 5; @@ -795,10 +813,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 50: + case 51: switch (token) { case 'r': - parser->stack[++parser->top].state = 54; + parser->stack[++parser->top].state = 55; return 1; default: @@ -806,7 +824,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 51: + case 52: switch (token) { default: parser->top -= 8; @@ -815,7 +833,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 52: + case 53: switch (token) { default: parser->top -= 8; @@ -824,7 +842,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 53: + case 54: switch (token) { default: parser->top -= 8; @@ -833,10 +851,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 54: + case 55: switch (token) { case 'g': - parser->stack[++parser->top].state = 55; + parser->stack[++parser->top].state = 56; return 1; default: @@ -844,10 +862,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 55: + case 56: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 56; + parser->stack[++parser->top].state = 57; return 1; default: @@ -855,18 +873,18 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 56: + case 57: switch (token) { case TOK_SYMBOL: - parser->stack[++parser->top].state = 41; + parser->stack[++parser->top].state = 42; return 1; case TOK_SYMBOL_LC: - parser->stack[++parser->top].state = 42; + parser->stack[++parser->top].state = 43; return 1; case TOK_SYMBOL_UC: - parser->stack[++parser->top].state = 43; + parser->stack[++parser->top].state = 44; return 1; default: @@ -874,7 +892,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 57: + case 58: switch (token) { default: parser->top -= 12; diff --git a/src/parse.hpp b/src/parse.hpp index 7b3d73f..a3ced3d 100644 --- a/src/parse.hpp +++ b/src/parse.hpp @@ -6,10 +6,11 @@ enum parse_token_t { TOK_BLOCK = 256, TOK_CHAR = 257, - TOK_STRING = 258, - TOK_SYMBOL = 259, - TOK_SYMBOL_LC = 260, - TOK_SYMBOL_UC = 261, + TOK_SQBLOCK = 258, + TOK_STRING = 259, + TOK_SYMBOL = 260, + TOK_SYMBOL_LC = 261, + TOK_SYMBOL_UC = 262, }; typedef struct parse_token_value { diff --git a/src/parse.y b/src/parse.y index 259e0de..ed946e0 100644 --- a/src/parse.y +++ b/src/parse.y @@ -2,6 +2,7 @@ %type SYMBOL_UC {std::string *} str %type SYMBOL_LC {std::string *} str %type BLOCK {std::string *} str +%type SQBLOCK {std::string *} str %type STRING {std::string *} str %type CHAR {char} c @@ -78,9 +79,7 @@ rule |= SYMBOL_LC(lhs) "|=" rhs(rhs) action(action) { } -rhs |= { - return new std::pair, std::vector>(); -} +rhs |= [new std::pair, std::vector>()] rhs |= rhs(rhs) symbol(sym) { rhs->first.push_back(*sym); @@ -112,18 +111,17 @@ rhs |= rhs(rhs) STRING(str) { } -action |= ';' { - return new std::string; -} +action |= ';' [new std::string] -action |= BLOCK(v) { +action |= BLOCK(v) [v] + +action |= SQBLOCK(v) { + *v = "\n\treturn " + *v + ";\n"; return v; } -symbol |= term(v) { - return v; -} +symbol |= term(v) [v] symbol |= SYMBOL_LC(v) { solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_nonterm(*v)); @@ -138,19 +136,9 @@ term |= SYMBOL_UC(v) { return ret; } -term |= CHAR(v) { - return new solar::symbol_t(solar::symbol_t::make_char(v)); -} - +term |= CHAR(v) [new solar::symbol_t(solar::symbol_t::make_char(v))] -varname |= SYMBOL_LC(v) { - return v; -} -varname |= SYMBOL_UC(v) { - return v; -} - -varname |= SYMBOL(v) { - return v; -} +varname |= SYMBOL_LC(v) [v] +varname |= SYMBOL_UC(v) [v] +varname |= SYMBOL(v) [v] -- cgit v1.2.3