Add support for square-bracketed actions

This commit is contained in:
Matthias Schiffer 2015-04-11 01:47:28 +02:00
parent 0f3c53fb63
commit 69d5b76e1a
5 changed files with 206 additions and 196 deletions

View file

@ -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);

View file

@ -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() {

View file

@ -126,30 +126,31 @@ 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) {
return v;
}
static inline std::string * parse_reduce_21(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {
return v;
}
@ -158,6 +159,10 @@ static inline std::string * parse_reduce_22(std::string * v, __attribute__((unus
return v;
}
static inline std::string * parse_reduce_23(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {
return v;
}
static int parse_do_push(parse_context_t *parser, int token, __attribute__((unused)) solar::grammar_t * grammar) {
while (1) {
switch (parser->stack[parser->top].state) {
@ -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;
}
@ -480,18 +489,18 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 26:
switch (token) {
default:
parser->top -= 2;
parser->stack[parser->top].value.symbol_rhs = parse_reduce_13(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.token.str, grammar);
parser->stack[++parser->top].state = 16;
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 -= 1;
parser->stack[parser->top].value.symbol_symbol = parse_reduce_17(parser->stack[parser->top + 0].value.token.str, grammar);
parser->stack[++parser->top].state = 22;
parser->top -= 2;
parser->stack[parser->top].value.symbol_rhs = parse_reduce_13(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.token.str, grammar);
parser->stack[++parser->top].state = 16;
}
break;
@ -499,21 +508,30 @@ 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_18(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 29:
switch (token) {
default:
parser->top -= 1;
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,20 +540,9 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
}
break;
case 30:
switch (token) {
case 'a':
parser->stack[++parser->top].state = 35;
return 1;
default:
return -1;
}
break;
case 31:
switch (token) {
case 'e':
case 'a':
parser->stack[++parser->top].state = 36;
return 1;
@ -546,7 +553,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 32:
switch (token) {
case 'c':
case 'e':
parser->stack[++parser->top].state = 37;
return 1;
@ -557,16 +564,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 33:
switch (token) {
case TOK_CHAR:
parser->stack[++parser->top].state = 25;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 28;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 39;
case 'c':
parser->stack[++parser->top].state = 38;
return 1;
default:
@ -576,16 +575,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 34:
switch (token) {
case TOK_SYMBOL:
parser->stack[++parser->top].state = 41;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 42;
case TOK_CHAR:
parser->stack[++parser->top].state = 25;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 43;
parser->stack[++parser->top].state = 29;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 40;
return 1;
default:
@ -595,7 +594,15 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 35:
switch (token) {
case '_':
case TOK_SYMBOL:
parser->stack[++parser->top].state = 42;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 43;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 44;
return 1;
@ -606,7 +613,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 36:
switch (token) {
case 'r':
case '_':
parser->stack[++parser->top].state = 45;
return 1;
@ -617,7 +624,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 37:
switch (token) {
case 'e':
case 'r':
parser->stack[++parser->top].state = 46;
return 1;
@ -628,7 +635,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 38:
switch (token) {
case TOK_BLOCK:
case 'e':
parser->stack[++parser->top].state = 47;
return 1;
@ -650,7 +657,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 40:
switch (token) {
case ')':
case TOK_BLOCK:
parser->stack[++parser->top].state = 49;
return 1;
@ -661,23 +668,12 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 41:
switch (token) {
case ')':
parser->stack[++parser->top].state = 50;
return 1;
default:
parser->top -= 1;
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;
break;
case 47:
parser->stack[++parser->top].state = 53;
break;
case 56:
parser->stack[++parser->top].state = 57;
break;
}
return -1;
}
break;
@ -685,18 +681,18 @@ 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_varname = parse_reduce_20(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;
}
@ -709,16 +705,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
parser->top -= 1;
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;
}
@ -727,18 +723,29 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 44:
switch (token) {
case 'a':
parser->stack[++parser->top].state = 50;
return 1;
default:
return -1;
parser->top -= 1;
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 35:
parser->stack[++parser->top].state = 41;
break;
case 48:
parser->stack[++parser->top].state = 54;
break;
case 57:
parser->stack[++parser->top].state = 58;
break;
}
}
break;
case 45:
switch (token) {
case TOK_BLOCK:
case 'a':
parser->stack[++parser->top].state = 51;
return 1;
@ -760,16 +767,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 47:
switch (token) {
case TOK_SYMBOL:
parser->stack[++parser->top].state = 41;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 42;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 43;
case TOK_BLOCK:
parser->stack[++parser->top].state = 53;
return 1;
default:
@ -778,6 +777,25 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
break;
case 48:
switch (token) {
case TOK_SYMBOL:
parser->stack[++parser->top].state = 42;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 43;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 44;
return 1;
default:
return -1;
}
break;
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,47 +813,9 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
}
break;
case 50:
switch (token) {
case 'r':
parser->stack[++parser->top].state = 54;
return 1;
default:
return -1;
}
break;
case 51:
switch (token) {
default:
parser->top -= 8;
parse_reduce_7(parser->stack[parser->top + 7].value.token.str, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 52:
switch (token) {
default:
parser->top -= 8;
parse_reduce_6(parser->stack[parser->top + 7].value.token.str, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 53:
switch (token) {
default:
parser->top -= 8;
parse_reduce_5(parser->stack[parser->top + 5].value.symbol_term, parser->stack[parser->top + 6].value.token.str, parser->stack[parser->top + 7].value.symbol_varname, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 54:
switch (token) {
case 'g':
case 'r':
parser->stack[++parser->top].state = 55;
return 1;
@ -844,9 +824,36 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
}
break;
case 52:
switch (token) {
default:
parser->top -= 8;
parse_reduce_7(parser->stack[parser->top + 7].value.token.str, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 53:
switch (token) {
default:
parser->top -= 8;
parse_reduce_6(parser->stack[parser->top + 7].value.token.str, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 54:
switch (token) {
default:
parser->top -= 8;
parse_reduce_5(parser->stack[parser->top + 5].value.symbol_term, parser->stack[parser->top + 6].value.token.str, parser->stack[parser->top + 7].value.symbol_varname, grammar);
parser->stack[++parser->top].state = 2;
}
break;
case 55:
switch (token) {
case TOK_BLOCK:
case 'g':
parser->stack[++parser->top].state = 56;
return 1;
@ -857,16 +864,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 56:
switch (token) {
case TOK_SYMBOL:
parser->stack[++parser->top].state = 41;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 42;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 43;
case TOK_BLOCK:
parser->stack[++parser->top].state = 57;
return 1;
default:
@ -875,6 +874,25 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
break;
case 57:
switch (token) {
case TOK_SYMBOL:
parser->stack[++parser->top].state = 42;
return 1;
case TOK_SYMBOL_LC:
parser->stack[++parser->top].state = 43;
return 1;
case TOK_SYMBOL_UC:
parser->stack[++parser->top].state = 44;
return 1;
default:
return -1;
}
break;
case 58:
switch (token) {
default:
parser->top -= 12;

View file

@ -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 {

View file

@ -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<solar::symbol_t>, std::vector<std::string>>();
}
rhs |= [new std::pair<std::vector<solar::symbol_t>, std::vector<std::string>>()]
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]