From: Ben Pfaff Date: Sun, 4 Jul 2021 20:07:24 +0000 (-0700) Subject: Rename macro_expander to macro_call. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b44c001ed0f2d05e41b5e5a3e484f0c06da5dad1;p=pspp Rename macro_expander to macro_call. --- diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index a273587a66..b252d3aefd 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -1836,10 +1836,10 @@ lex_source_get (const struct lex_source *src_) In the common case where there is no macro to expand, the loop is not entered. */ - struct macro_expander *me; - int n_call = macro_expander_create ( + struct macro_call *mc; + int n_call = macro_call_create ( src->lexer->macros, &src->tokens[src->middle & src->mask].token, - &me); + &mc); for (int middle_ofs = 1; !n_call; middle_ofs++) { if (src->front - src->middle <= middle_ofs && !lex_source_get__ (src)) @@ -1865,7 +1865,7 @@ lex_source_get (const struct lex_source *src_) macro_expander_add() reports an error and to give better error messages. */ src->middle += middle_ofs + 1; - n_call = macro_expander_add (me, &mt); + n_call = macro_call_add (mc, &mt); src->middle -= middle_ofs + 1; } if (n_call < 0) @@ -1873,7 +1873,7 @@ lex_source_get (const struct lex_source *src_) /* False alarm: no macro expansion after all. Use first token as lookahead. We'll retry macro expansion from the second token next time around. */ - macro_expander_destroy (me); + macro_call_destroy (mc); src->middle++; return true; } @@ -1887,8 +1887,8 @@ lex_source_get (const struct lex_source *src_) the line number into macro_expander_get_expansion(). */ src->middle += n_call; struct macro_tokens expansion = { .n = 0 }; - macro_expander_get_expansion (me, src->reader->syntax, &expansion); - macro_expander_destroy (me); + macro_call_expand (mc, src->reader->syntax, &expansion); + macro_call_destroy (mc); src->middle -= n_call; /* Convert the macro expansion into syntax for possible error messages later. */ diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c index 05bde719ad..3346692e10 100644 --- a/src/language/lexer/macro.c +++ b/src/language/lexer/macro.c @@ -538,121 +538,114 @@ macro_set_add (struct macro_set *set, struct macro *m) /* Macro expander. */ -enum me_state +enum mc_state { /* Error state. */ - ME_ERROR, + MC_ERROR, - /* Accumulating tokens in me->params toward the end of any type of + /* Accumulating tokens in mc->params toward the end of any type of argument. */ - ME_ARG, + MC_ARG, /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */ - ME_ENCLOSE, + MC_ENCLOSE, /* Expecting a keyword for a keyword argument. */ - ME_KEYWORD, + MC_KEYWORD, /* Expecting an equal sign for a keyword argument. */ - ME_EQUALS, + MC_EQUALS, /* Macro fully parsed and ready for expansion. */ - ME_FINISHED, + MC_FINISHED, }; -/* Macro expander. - - Macro expansion has two phases. Phase 1 uses an FSM driven by - macro_expander_create() and macro_expander_add() to identify the macro being - called and obtain its arguments. 'state' identifies the FSM state. - - Phase 2 is macro expansion via macro_expander_get_expansion(). */ -struct macro_expander +/* Parsing macro calls. This is a FSM driven by macro_call_create() and + macro_call_add() to identify the macro being called and obtain its + arguments. 'state' identifies the FSM state. */ +struct macro_call { - /* Phases 1 and 2. */ const struct macro_set *macros; const struct macro *macro; struct macro_tokens **args; - /* Phase 1 only. */ - enum me_state state; + enum mc_state state; size_t n_tokens; const struct macro_param *param; /* Parameter currently being parsed. */ - /* Phase 2 only. */ enum segmenter_mode segmenter_mode; }; /* Completes macro expansion by initializing arguments that weren't supplied to their defaults. */ static int -me_finished (struct macro_expander *me) +mc_finished (struct macro_call *mc) { - me->state = ME_FINISHED; - for (size_t i = 0; i < me->macro->n_params; i++) - if (!me->args[i]) - me->args[i] = &me->macro->params[i].def; - return me->n_tokens; + mc->state = MC_FINISHED; + for (size_t i = 0; i < mc->macro->n_params; i++) + if (!mc->args[i]) + mc->args[i] = &mc->macro->params[i].def; + return mc->n_tokens; } static int -me_next_arg (struct macro_expander *me) +mc_next_arg (struct macro_call *mc) { - if (!me->param) + if (!mc->param) { - assert (!me->macro->n_params); - return me_finished (me); + assert (!mc->macro->n_params); + return mc_finished (mc); } - else if (me->param->positional) + else if (mc->param->positional) { - me->param++; - if (me->param >= &me->macro->params[me->macro->n_params]) - return me_finished (me); + mc->param++; + if (mc->param >= &mc->macro->params[mc->macro->n_params]) + return mc_finished (mc); else { - me->state = (!me->param->positional ? ME_KEYWORD - : me->param->arg_type == ARG_ENCLOSE ? ME_ENCLOSE - : ME_ARG); + mc->state = (!mc->param->positional ? MC_KEYWORD + : mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE + : MC_ARG); return 0; } } else { - for (size_t i = 0; i < me->macro->n_params; i++) - if (!me->args[i]) + for (size_t i = 0; i < mc->macro->n_params; i++) + if (!mc->args[i]) { - me->state = ME_KEYWORD; + mc->state = MC_KEYWORD; return 0; } - return me_finished (me); + return mc_finished (mc); } } static int -me_error (struct macro_expander *me) +mc_error (struct macro_call *mc) { - me->state = ME_ERROR; + mc->state = MC_ERROR; return -1; } static int -me_add_arg (struct macro_expander *me, const struct macro_token *mt) +mc_add_arg (struct macro_call *mc, const struct macro_token *mt) { - const struct macro_param *p = me->param; + const struct macro_param *p = mc->param; const struct token *token = &mt->token; if ((token->type == T_ENDCMD || token->type == T_STOP) && p->arg_type != ARG_CMDEND) { msg (SE, _("Unexpected end of command reading argument %s " - "to macro %s."), me->param->name, me->macro->name); + "to macro %s."), mc->param->name, mc->macro->name); - return me_error (me); + return mc_error (mc); } - me->n_tokens++; + mc->n_tokens++; - struct macro_tokens **argp = &me->args[p - me->macro->params]; + struct macro_tokens **argp = &mc->args[p - mc->macro->params]; if (!*argp) *argp = xzalloc (sizeof **argp); struct macro_tokens *arg = *argp; @@ -660,13 +653,13 @@ me_add_arg (struct macro_expander *me, const struct macro_token *mt) { macro_tokens_add (arg, mt); if (arg->n >= p->n_tokens) - return me_next_arg (me); + return mc_next_arg (mc); return 0; } else if (p->arg_type == ARG_CMDEND) { if (token->type == T_ENDCMD || token->type == T_STOP) - return me_next_arg (me); + return mc_next_arg (mc); macro_tokens_add (arg, mt); return 0; } @@ -675,14 +668,14 @@ me_add_arg (struct macro_expander *me, const struct macro_token *mt) const struct token *end = p->arg_type == ARG_CHAREND ? &p->charend : &p->enclose[1]; if (token_equal (token, end)) - return me_next_arg (me); + return mc_next_arg (mc); macro_tokens_add (arg, mt); return 0; } } static int -me_expected (struct macro_expander *me, const struct macro_token *actual, +mc_expected (struct macro_call *mc, const struct macro_token *actual, const struct token *expected) { const struct substring actual_s @@ -692,25 +685,25 @@ me_expected (struct macro_expander *me, const struct macro_token *actual, msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s " "to macro %s."), (int) actual_s.length, actual_s.string, expected_s, - me->param->name, me->macro->name); + mc->param->name, mc->macro->name); free (expected_s); - return me_error (me); + return mc_error (mc); } static int -me_enclose (struct macro_expander *me, const struct macro_token *mt) +mc_enclose (struct macro_call *mc, const struct macro_token *mt) { const struct token *token = &mt->token; - me->n_tokens++; + mc->n_tokens++; - if (token_equal (&me->param->enclose[0], token)) + if (token_equal (&mc->param->enclose[0], token)) { - me->state = ME_ARG; + mc->state = MC_ARG; return 0; } - return me_expected (me, mt, &me->param->enclose[0]); + return mc_expected (mc, mt, &mc->param->enclose[0]); } static const struct macro_param * @@ -730,107 +723,107 @@ macro_find_parameter_by_name (const struct macro *m, struct substring name) } static int -me_keyword (struct macro_expander *me, const struct macro_token *mt) +mc_keyword (struct macro_call *mc, const struct macro_token *mt) { const struct token *token = &mt->token; if (token->type != T_ID) - return me_finished (me); + return mc_finished (mc); - const struct macro_param *p = macro_find_parameter_by_name (me->macro, + const struct macro_param *p = macro_find_parameter_by_name (mc->macro, token->string); if (p) { - size_t arg_index = p - me->macro->params; - me->param = p; - if (me->args[arg_index]) + size_t arg_index = p - mc->macro->params; + mc->param = p; + if (mc->args[arg_index]) { msg (SE, _("Argument %s multiply specified in call to macro %s."), - p->name, me->macro->name); - return me_error (me); + p->name, mc->macro->name); + return mc_error (mc); } - me->n_tokens++; - me->state = ME_EQUALS; + mc->n_tokens++; + mc->state = MC_EQUALS; return 0; } - return me_finished (me); + return mc_finished (mc); } static int -me_equals (struct macro_expander *me, const struct macro_token *mt) +mc_equals (struct macro_call *mc, const struct macro_token *mt) { const struct token *token = &mt->token; - me->n_tokens++; + mc->n_tokens++; if (token->type == T_EQUALS) { - me->state = ME_ARG; + mc->state = MC_ARG; return 0; } - return me_expected (me, mt, &(struct token) { .type = T_EQUALS }); + return mc_expected (mc, mt, &(struct token) { .type = T_EQUALS }); } /* If TOKEN is the first token of a call to a macro in MACROS, create a new - macro expander, initializes *MEP to it. Returns 0 if more tokens are needed - and should be added via macro_expander_add() or 1 if the caller should next - call macro_expander_get_expansion(). + macro expander, initializes *MCP to it. Returns 0 if more tokens are needed + and should be added via macro_call_add() or 1 if the caller should next call + macro_call_get_expansion(). - If TOKEN is not the first token of a macro call, returns -1 and sets *MEP to + If TOKEN is not the first token of a macro call, returns -1 and sets *MCP to NULL. */ int -macro_expander_create (const struct macro_set *macros, - const struct token *token, - struct macro_expander **mep) +macro_call_create (const struct macro_set *macros, + const struct token *token, + struct macro_call **mcp) { const struct macro *macro = (token->type == T_ID || token->type == T_MACRO_ID ? macro_set_find (macros, token->string.string) : NULL); if (!macro) { - *mep = NULL; + *mcp = NULL; return -1; } - struct macro_expander *me = xmalloc (sizeof *me); - *me = (struct macro_expander) { + struct macro_call *mc = xmalloc (sizeof *mc); + *mc = (struct macro_call) { .macros = macros, .macro = macro, .n_tokens = 1, - .state = (!macro->n_params ? ME_FINISHED - : !macro->params[0].positional ? ME_KEYWORD - : macro->params[0].arg_type == ARG_ENCLOSE ? ME_ENCLOSE - : ME_ARG), - .args = macro->n_params ? xcalloc (macro->n_params, sizeof *me->args) : NULL, + .state = (!macro->n_params ? MC_FINISHED + : !macro->params[0].positional ? MC_KEYWORD + : macro->params[0].arg_type == ARG_ENCLOSE ? MC_ENCLOSE + : MC_ARG), + .args = macro->n_params ? xcalloc (macro->n_params, sizeof *mc->args) : NULL, .param = macro->params, }; - *mep = me; + *mcp = mc; - return me->state == ME_FINISHED ? 1 : 0; + return mc->state == MC_FINISHED ? 1 : 0; } void -macro_expander_destroy (struct macro_expander *me) +macro_call_destroy (struct macro_call *mc) { - if (!me) + if (!mc) return; - for (size_t i = 0; i < me->macro->n_params; i++) + for (size_t i = 0; i < mc->macro->n_params; i++) { - struct macro_tokens *a = me->args[i]; - if (a && a != &me->macro->params[i].def) + struct macro_tokens *a = mc->args[i]; + if (a && a != &mc->macro->params[i].def) { macro_tokens_uninit (a); free (a); } } - free (me->args); - free (me); + free (mc->args); + free (mc); } -/* Adds TOKEN to the collection of tokens in ME that potentially need to be +/* Adds TOKEN to the collection of tokens in MC that potentially need to be macro expanded. Returns -1 if the tokens added do not actually invoke a macro. The caller @@ -839,32 +832,32 @@ macro_expander_destroy (struct macro_expander *me) Returns 0 if the macro expander needs more tokens, for macro arguments or to decide whether this is actually a macro invocation. The caller should call - macro_expander_add() again with the next token. + macro_call_add() again with the next token. Returns a positive number to indicate that the returned number of tokens invoke a macro. The number returned might be less than the number of tokens added because it can take a few tokens of lookahead to determine whether the macro invocation is finished. The caller should call - macro_expander_get_expansion() to obtain the expansion. */ + macro_call_get_expansion() to obtain the expansion. */ int -macro_expander_add (struct macro_expander *me, const struct macro_token *mt) +macro_call_add (struct macro_call *mc, const struct macro_token *mt) { - switch (me->state) + switch (mc->state) { - case ME_ERROR: + case MC_ERROR: return -1; - case ME_ARG: - return me_add_arg (me, mt); + case MC_ARG: + return mc_add_arg (mc, mt); - case ME_ENCLOSE: - return me_enclose (me, mt); + case MC_ENCLOSE: + return mc_enclose (mc, mt); - case ME_KEYWORD: - return me_keyword (me, mt); + case MC_KEYWORD: + return mc_keyword (mc, mt); - case ME_EQUALS: - return me_equals (me, mt); + case MC_EQUALS: + return mc_equals (mc, mt); default: NOT_REACHED (); @@ -891,7 +884,7 @@ struct parse_macro_function_ctx size_t n_input; int nesting_countdown; const struct macro_set *macros; - const struct macro_expander *me; + const struct macro_call *mc; const struct macro_expansion_stack *stack; struct string_map *vars; bool *expand; @@ -900,7 +893,7 @@ struct parse_macro_function_ctx static void macro_expand (const struct macro_tokens *, int nesting_countdown, const struct macro_set *, - const struct macro_expander *, struct string_map *vars, + const struct macro_call *, struct string_map *vars, const struct macro_expansion_stack *stack, bool *expand, bool *break_, struct macro_tokens *exp); @@ -929,11 +922,11 @@ parse_function_arg (struct parse_macro_function_ctx *ctx, if (token->type == T_MACRO_ID) { const struct macro_param *param = macro_find_parameter_by_name ( - ctx->me->macro, token->string); + ctx->mc->macro, token->string); if (param) { - size_t param_idx = param - ctx->me->macro->params; - const struct macro_tokens *marg = ctx->me->args[param_idx]; + size_t param_idx = param - ctx->mc->macro->params; + const struct macro_tokens *marg = ctx->mc->args[param_idx]; for (size_t i = 0; i < marg->n; i++) { if (i) @@ -945,12 +938,12 @@ parse_function_arg (struct parse_macro_function_ctx *ctx, if (is_bang_star (ctx->input, ctx->n_input, i)) { - for (size_t i = 0; i < ctx->me->macro->n_params; i++) + for (size_t i = 0; i < ctx->mc->macro->n_params; i++) { - if (!ctx->me->macro->params[i].positional) + if (!ctx->mc->macro->params[i].positional) break; - const struct macro_tokens *marg = ctx->me->args[i]; + const struct macro_tokens *marg = ctx->mc->args[i]; for (size_t j = 0; j < marg->n; j++) { if (i || j) @@ -978,7 +971,7 @@ parse_function_arg (struct parse_macro_function_ctx *ctx, .n_input = ctx->n_input - i, .nesting_countdown = ctx->nesting_countdown, .macros = ctx->macros, - .me = ctx->me, + .mc = ctx->mc, .stack = ctx->stack, .vars = ctx->vars, .expand = ctx->expand, @@ -1141,7 +1134,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, input_consumed)) { for (size_t i = 0; i < args.n; i++) - if (!unquote_string (args.strings[i], ctx->me->segmenter_mode, output)) + if (!unquote_string (args.strings[i], ctx->mc->segmenter_mode, output)) ds_put_cstr (output, args.strings[i]); } else if (parse_macro_function (ctx, &args, ss_cstr ("!HEAD"), 1, 1, @@ -1149,10 +1142,10 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, { struct string tmp; const char *s = unquote_string_in_place (args.strings[0], - ctx->me->segmenter_mode, &tmp); + ctx->mc->segmenter_mode, &tmp); struct macro_tokens mts = { .n = 0 }; - macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode, + macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode, ctx->stack); if (mts.n > 0) ds_put_substring (output, mts.mts[0].representation); @@ -1169,7 +1162,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, else if (parse_macro_function (ctx, &args, ss_cstr ("!QUOTE"), 1, 1, input_consumed)) { - if (unquote_string (args.strings[0], ctx->me->segmenter_mode, NULL)) + if (unquote_string (args.strings[0], ctx->mc->segmenter_mode, NULL)) ds_put_cstr (output, args.strings[0]); else { @@ -1217,10 +1210,10 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, { struct string tmp; const char *s = unquote_string_in_place (args.strings[0], - ctx->me->segmenter_mode, &tmp); + ctx->mc->segmenter_mode, &tmp); struct macro_tokens mts = { .n = 0 }; - macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode, + macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode, ctx->stack); if (mts.n > 1) { @@ -1233,7 +1226,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, else if (parse_macro_function (ctx, &args, ss_cstr ("!UNQUOTE"), 1, 1, input_consumed)) { - if (!unquote_string (args.strings[0], ctx->me->segmenter_mode, output)) + if (!unquote_string (args.strings[0], ctx->mc->segmenter_mode, output)) ds_put_cstr (output, args.strings[0]); } else if (parse_macro_function (ctx, &args, ss_cstr ("!UPCASE"), 1, 1, @@ -1241,7 +1234,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, { struct string tmp; const char *s = unquote_string_in_place (args.strings[0], - ctx->me->segmenter_mode, &tmp); + ctx->mc->segmenter_mode, &tmp); char *upper = utf8_to_upper (s); ds_put_cstr (output, upper); free (upper); @@ -1252,10 +1245,10 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, { struct macro_tokens mts = { .n = 0 }; macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]), - ctx->me->segmenter_mode, ctx->stack); + ctx->mc->segmenter_mode, ctx->stack); struct macro_tokens exp = { .n = 0 }; macro_expand (&mts, ctx->nesting_countdown - 1, - ctx->macros, ctx->me, ctx->vars, + ctx->macros, ctx->mc, ctx->vars, &(struct macro_expansion_stack) { .name = "!EVAL", .next = ctx->stack, @@ -1282,7 +1275,7 @@ struct expr_context { int nesting_countdown; const struct macro_set *macros; - const struct macro_expander *me; + const struct macro_call *mc; const struct macro_expansion_stack *stack; struct string_map *vars; bool *expand; @@ -1329,7 +1322,7 @@ macro_evaluate_literal (const struct expr_context *ctx, .n_input = end - p, .nesting_countdown = ctx->nesting_countdown, .macros = ctx->macros, - .me = ctx->me, + .mc = ctx->mc, .stack = ctx->stack, .vars = ctx->vars, .expand = ctx->expand, @@ -1337,7 +1330,7 @@ macro_evaluate_literal (const struct expr_context *ctx, struct string function_output = DS_EMPTY_INITIALIZER; size_t function_consumed = parse_function_arg (&fctx, 0, &function_output); struct string unquoted = DS_EMPTY_INITIALIZER; - if (unquote_string (ds_cstr (&function_output), ctx->me->segmenter_mode, + if (unquote_string (ds_cstr (&function_output), ctx->mc->segmenter_mode, &unquoted)) { ds_swap (&function_output, &unquoted); @@ -1412,9 +1405,9 @@ macro_evaluate_relational (const struct expr_context *ctx, } struct string lhs_tmp, rhs_tmp; - int cmp = strcmp (unquote_string_in_place (lhs, ctx->me->segmenter_mode, + int cmp = strcmp (unquote_string_in_place (lhs, ctx->mc->segmenter_mode, &lhs_tmp), - unquote_string_in_place (rhs, ctx->me->segmenter_mode, + unquote_string_in_place (rhs, ctx->mc->segmenter_mode, &rhs_tmp)); ds_destroy (&lhs_tmp); ds_destroy (&rhs_tmp); @@ -1528,14 +1521,14 @@ static char * macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, + const struct macro_call *mc, const struct macro_expansion_stack *stack, struct string_map *vars, bool *expand) { const struct expr_context ctx = { .nesting_countdown = nesting_countdown, .macros = macros, - .me = me, + .mc = mc, .stack = stack, .vars = vars, .expand = expand, @@ -1547,18 +1540,18 @@ static bool macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, + const struct macro_call *mc, const struct macro_expansion_stack *stack, struct string_map *vars, bool *expand, double *number) { char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown, - macros, me, stack, vars, expand); + macros, mc, stack, vars, expand); if (!s) return false; struct macro_tokens mts = { .n = 0 }; - macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, stack); + macro_tokens_from_string__ (&mts, ss_cstr (s), mc->segmenter_mode, stack); if (mts.n != 1 || !token_is_number (&mts.mts[0].token)) { macro_error (stack, mts.n > 0 ? &mts.mts[0] : NULL, @@ -1601,7 +1594,7 @@ find_ifend_clause (const struct macro_token *p, const struct macro_token *end) static size_t macro_expand_if (const struct macro_token *tokens, size_t n_tokens, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, + const struct macro_call *mc, const struct macro_expansion_stack *stack, struct string_map *vars, bool *expand, bool *break_, struct macro_tokens *exp) @@ -1615,7 +1608,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, p++; char *result = macro_evaluate_expression (&p, end - p, nesting_countdown, - macros, me, + macros, mc, stack, vars, expand); if (!result) return 0; @@ -1683,7 +1676,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, .mts = CONST_CAST (struct macro_token *, start), .n = n, }; - macro_expand (&mts, nesting_countdown, macros, me, vars, + macro_expand (&mts, nesting_countdown, macros, mc, vars, &(struct macro_expansion_stack) { .name = "!IF", .next = stack, @@ -1696,7 +1689,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, static size_t macro_parse_let (const struct macro_token *tokens, size_t n_tokens, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, + const struct macro_call *mc, const struct macro_expansion_stack *stack, struct string_map *vars, bool *expand) { @@ -1715,7 +1708,7 @@ macro_parse_let (const struct macro_token *tokens, size_t n_tokens, } const struct substring var_name = p->token.string; if (is_macro_keyword (var_name) - || macro_find_parameter_by_name (me->macro, var_name)) + || macro_find_parameter_by_name (mc->macro, var_name)) { macro_error (stack, p < end ? p : NULL, _("Cannot use argument name or macro keyword " @@ -1734,7 +1727,7 @@ macro_parse_let (const struct macro_token *tokens, size_t n_tokens, p++; char *value = macro_evaluate_expression (&p, end - p, nesting_countdown, - macros, me, stack, vars, expand); + macros, mc, stack, vars, expand); if (!value) return 0; @@ -1768,7 +1761,7 @@ find_doend (const struct macro_expansion_stack *stack, static size_t macro_expand_do (const struct macro_token *tokens, size_t n_tokens, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, + const struct macro_call *mc, const struct macro_expansion_stack *stack, struct string_map *vars, bool *expand, struct macro_tokens *exp) @@ -1788,7 +1781,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, } const struct substring var_name = p->token.string; if (is_macro_keyword (var_name) - || macro_find_parameter_by_name (me->macro, var_name)) + || macro_find_parameter_by_name (mc->macro, var_name)) { macro_error (stack, p, _("Cannot use argument name or macro " "keyword as !DO variable.")); @@ -1805,13 +1798,13 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, { p++; char *list = macro_evaluate_expression (&p, end - p, nesting_countdown, - macros, me, &next_stack, vars, + macros, mc, &next_stack, vars, expand); if (!list) return 0; struct macro_tokens items = { .n = 0 }; - macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode, + macro_tokens_from_string__ (&items, ss_cstr (list), mc->segmenter_mode, stack); free (list); @@ -1842,7 +1835,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, bool break_ = false; macro_expand (&inner, nesting_countdown, macros, - me, vars, &next_stack, expand, &break_, exp); + mc, vars, &next_stack, expand, &break_, exp); if (break_) break; } @@ -1853,7 +1846,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, p++; double first; if (!macro_evaluate_number (&p, end - p, nesting_countdown, - macros, me, &next_stack, + macros, mc, &next_stack, vars, expand, &first)) return 0; @@ -1868,7 +1861,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, double last; if (!macro_evaluate_number (&p, end - p, nesting_countdown, - macros, me, &next_stack, + macros, mc, &next_stack, vars, expand, &last)) return 0; @@ -1878,7 +1871,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, { p++; if (!macro_evaluate_number (&p, end - p, nesting_countdown, - macros, me, &next_stack, + macros, mc, &next_stack, vars, expand, &by)) return 0; @@ -1921,7 +1914,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, bool break_ = false; macro_expand (&inner, nesting_countdown, - macros, me, vars, &next_stack, expand, &break_, + macros, mc, vars, &next_stack, expand, &break_, exp); if (break_) break; @@ -1941,7 +1934,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, static void macro_expand (const struct macro_tokens *mts, int nesting_countdown, const struct macro_set *macros, - const struct macro_expander *me, struct string_map *vars, + const struct macro_call *mc, struct string_map *vars, const struct macro_expansion_stack *stack, bool *expand, bool *break_, struct macro_tokens *exp) { @@ -1963,13 +1956,13 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, { const struct macro_token *mt = &mts->mts[i]; const struct token *token = &mt->token; - if (token->type == T_MACRO_ID && me) + if (token->type == T_MACRO_ID && mc) { const struct macro_param *param = macro_find_parameter_by_name ( - me->macro, token->string); + mc->macro, token->string); if (param) { - const struct macro_tokens *arg = me->args[param - me->macro->params]; + const struct macro_tokens *arg = mc->args[param - mc->macro->params]; if (*expand && param->expand_arg) macro_expand (arg, nesting_countdown, macros, NULL, NULL, @@ -1985,13 +1978,13 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, if (is_bang_star (mts->mts, mts->n, i)) { - for (size_t j = 0; j < me->macro->n_params; j++) + for (size_t j = 0; j < mc->macro->n_params; j++) { - const struct macro_param *param = &me->macro->params[j]; + const struct macro_param *param = &mc->macro->params[j]; if (!param->positional) break; - const struct macro_tokens *arg = me->args[j]; + const struct macro_tokens *arg = mc->args[j]; if (*expand && param->expand_arg) macro_expand (arg, nesting_countdown, macros, NULL, NULL, @@ -2009,7 +2002,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, size_t n = macro_expand_if (&mts->mts[i], mts->n - i, nesting_countdown, - macros, me, stack, + macros, mc, stack, vars, expand, break_, exp); if (n > 0) { @@ -2025,19 +2018,19 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, if (value) { macro_tokens_from_string__ (exp, ss_cstr (value), - me->segmenter_mode, stack); + mc->segmenter_mode, stack); continue; } } if (*expand) { - struct macro_expander *subme; - int retval = macro_expander_create (macros, token, &subme); + struct macro_call *subme; + int retval = macro_call_create (macros, token, &subme); for (size_t j = 1; !retval; j++) { const struct macro_token endcmd = { .token = { .type = T_ENDCMD } }; - retval = macro_expander_add ( + retval = macro_call_add ( subme, i + j < mts->n ? &mts->mts[i + j] : &endcmd); } if (retval > 0) @@ -2052,11 +2045,11 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, .last_line = subme->macro->last_line, .next = stack, }, expand, break_, exp); - macro_expander_destroy (subme); + macro_call_destroy (subme); continue; } - macro_expander_destroy (subme); + macro_call_destroy (subme); } if (token->type != T_MACRO_ID) @@ -2081,7 +2074,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, .n_input = mts->n - i, .nesting_countdown = nesting_countdown, .macros = macros, - .me = me, + .mc = mc, .stack = stack, .vars = vars, .expand = expand, @@ -2093,7 +2086,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, i += function_consumed - 1; macro_tokens_from_string__ (exp, function_output.ss, - me->segmenter_mode, stack); + mc->segmenter_mode, stack); ds_destroy (&function_output); continue; @@ -2101,7 +2094,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, size_t n = macro_parse_let (&mts->mts[i], mts->n - i, nesting_countdown, - macros, me, stack, vars, expand); + macros, mc, stack, vars, expand); if (n > 0) { i += n - 1; @@ -2109,7 +2102,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, } n = macro_expand_do (&mts->mts[i], mts->n - i, - nesting_countdown, macros, me, stack, + nesting_countdown, macros, mc, stack, vars, expand, exp); if (n > 0) { @@ -2129,21 +2122,20 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, } void -macro_expander_get_expansion (struct macro_expander *me, - enum segmenter_mode segmenter_mode, - struct macro_tokens *exp) +macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode, + struct macro_tokens *exp) { - assert (me->state == ME_FINISHED); - me->segmenter_mode = segmenter_mode; + assert (mc->state == MC_FINISHED); + mc->segmenter_mode = segmenter_mode; bool expand = true; struct macro_expansion_stack stack = { - .name = me->macro->name, - .file_name = me->macro->file_name, - .first_line = me->macro->first_line, - .last_line = me->macro->last_line, + .name = mc->macro->name, + .file_name = mc->macro->file_name, + .first_line = mc->macro->first_line, + .last_line = mc->macro->last_line, }; - macro_expand (&me->macro->body, settings_get_mnest (), - me->macros, me, NULL, &stack, &expand, NULL, exp); + macro_expand (&mc->macro->body, settings_get_mnest (), + mc->macros, mc, NULL, &stack, &expand, NULL, exp); } diff --git a/src/language/lexer/macro.h b/src/language/lexer/macro.h index c21093d362..9f8d603a23 100644 --- a/src/language/lexer/macro.h +++ b/src/language/lexer/macro.h @@ -25,8 +25,6 @@ #include "language/lexer/segment.h" #include "language/lexer/token.h" -struct macro_expander; - /* A token along with the syntax that was tokenized to produce it. The syntax allows the token to be turned back into syntax accurately. */ struct macro_token @@ -128,17 +126,17 @@ macro_set_is_empty (const struct macro_set *set) return hmap_is_empty (&set->macros); } -/* Macro expansion. */ +/* Parsing and expanding macro calls. */ + +struct macro_call; -int macro_expander_create (const struct macro_set *, - const struct token *, - struct macro_expander **); -void macro_expander_destroy (struct macro_expander *); +int macro_call_create (const struct macro_set *, const struct token *, + struct macro_call **); +int macro_call_add (struct macro_call *, const struct macro_token *); -int macro_expander_add (struct macro_expander *, const struct macro_token *); +void macro_call_expand (struct macro_call *, enum segmenter_mode segmenter_mode, + struct macro_tokens *); -void macro_expander_get_expansion (struct macro_expander *, - enum segmenter_mode segmenter_mode, - struct macro_tokens *); +void macro_call_destroy (struct macro_call *); #endif /* macro.h */