hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
}
\f
-/* Macro expander. */
+/* Macro call parsing.. */
enum mc_state
{
enum mc_state state;
size_t n_tokens;
const struct macro_param *param; /* Parameter currently being parsed. */
-
- enum segmenter_mode segmenter_mode;
};
/* Completes macro expansion by initializing arguments that weren't supplied to
NOT_REACHED ();
}
}
+\f
+/* Macro expansion. */
+
+struct macro_expander
+ {
+ const struct macro_set *macros;
+ const struct macro *macro;
+ struct macro_tokens **args;
+ enum segmenter_mode segmenter_mode;
+ };
/* Each argument to a macro function is one of:
size_t n_input;
int nesting_countdown;
const struct macro_set *macros;
- const struct macro_call *mc;
+ const struct macro_expander *me;
const struct macro_expansion_stack *stack;
struct string_map *vars;
bool *expand;
static void
macro_expand (const struct macro_tokens *, int nesting_countdown,
const struct macro_set *,
- const struct macro_call *, struct string_map *vars,
+ const struct macro_expander *, struct string_map *vars,
const struct macro_expansion_stack *stack,
bool *expand, bool *break_,
struct macro_tokens *exp);
{
const struct macro_token *tokens = ctx->input;
const struct token *token = &tokens[i].token;
- if (token->type == T_MACRO_ID)
+ if (token->type == T_MACRO_ID && ctx->me->macro)
{
const struct macro_param *param = macro_find_parameter_by_name (
- ctx->mc->macro, token->string);
+ ctx->me->macro, token->string);
if (param)
{
- size_t param_idx = param - ctx->mc->macro->params;
- const struct macro_tokens *marg = ctx->mc->args[param_idx];
+ size_t param_idx = param - ctx->me->macro->params;
+ const struct macro_tokens *marg = ctx->me->args[param_idx];
for (size_t i = 0; i < marg->n; i++)
{
if (i)
if (is_bang_star (ctx->input, ctx->n_input, i))
{
- for (size_t i = 0; i < ctx->mc->macro->n_params; i++)
+ for (size_t i = 0; i < ctx->me->macro->n_params; i++)
{
- if (!ctx->mc->macro->params[i].positional)
+ if (!ctx->me->macro->params[i].positional)
break;
- const struct macro_tokens *marg = ctx->mc->args[i];
+ const struct macro_tokens *marg = ctx->me->args[i];
for (size_t j = 0; j < marg->n; j++)
{
if (i || j)
.n_input = ctx->n_input - i,
.nesting_countdown = ctx->nesting_countdown,
.macros = ctx->macros,
- .mc = ctx->mc,
+ .me = ctx->me,
.stack = ctx->stack,
.vars = ctx->vars,
.expand = ctx->expand,
input_consumed))
{
for (size_t i = 0; i < args.n; i++)
- if (!unquote_string (args.strings[i], ctx->mc->segmenter_mode, output))
+ if (!unquote_string (args.strings[i], ctx->me->segmenter_mode, output))
ds_put_cstr (output, args.strings[i]);
}
else if (parse_macro_function (ctx, &args, ss_cstr ("!HEAD"), 1, 1,
{
struct string tmp;
const char *s = unquote_string_in_place (args.strings[0],
- ctx->mc->segmenter_mode, &tmp);
+ ctx->me->segmenter_mode, &tmp);
struct macro_tokens mts = { .n = 0 };
- macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode,
+ macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode,
ctx->stack);
if (mts.n > 0)
ds_put_substring (output, mts.mts[0].representation);
else if (parse_macro_function (ctx, &args, ss_cstr ("!QUOTE"), 1, 1,
input_consumed))
{
- if (unquote_string (args.strings[0], ctx->mc->segmenter_mode, NULL))
+ if (unquote_string (args.strings[0], ctx->me->segmenter_mode, NULL))
ds_put_cstr (output, args.strings[0]);
else
{
{
struct string tmp;
const char *s = unquote_string_in_place (args.strings[0],
- ctx->mc->segmenter_mode, &tmp);
+ ctx->me->segmenter_mode, &tmp);
struct macro_tokens mts = { .n = 0 };
- macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->mc->segmenter_mode,
+ macro_tokens_from_string__ (&mts, ss_cstr (s), ctx->me->segmenter_mode,
ctx->stack);
if (mts.n > 1)
{
else if (parse_macro_function (ctx, &args, ss_cstr ("!UNQUOTE"), 1, 1,
input_consumed))
{
- if (!unquote_string (args.strings[0], ctx->mc->segmenter_mode, output))
+ if (!unquote_string (args.strings[0], ctx->me->segmenter_mode, output))
ds_put_cstr (output, args.strings[0]);
}
else if (parse_macro_function (ctx, &args, ss_cstr ("!UPCASE"), 1, 1,
{
struct string tmp;
const char *s = unquote_string_in_place (args.strings[0],
- ctx->mc->segmenter_mode, &tmp);
+ ctx->me->segmenter_mode, &tmp);
char *upper = utf8_to_upper (s);
ds_put_cstr (output, upper);
free (upper);
{
struct macro_tokens mts = { .n = 0 };
macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]),
- ctx->mc->segmenter_mode, ctx->stack);
+ ctx->me->segmenter_mode, ctx->stack);
struct macro_tokens exp = { .n = 0 };
macro_expand (&mts, ctx->nesting_countdown - 1,
- ctx->macros, ctx->mc, ctx->vars,
+ ctx->macros, ctx->me, ctx->vars,
&(struct macro_expansion_stack) {
.name = "!EVAL",
.next = ctx->stack,
{
int nesting_countdown;
const struct macro_set *macros;
- const struct macro_call *mc;
+ const struct macro_expander *me;
const struct macro_expansion_stack *stack;
struct string_map *vars;
bool *expand;
.n_input = end - p,
.nesting_countdown = ctx->nesting_countdown,
.macros = ctx->macros,
- .mc = ctx->mc,
+ .me = ctx->me,
.stack = ctx->stack,
.vars = ctx->vars,
.expand = ctx->expand,
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->mc->segmenter_mode,
+ if (unquote_string (ds_cstr (&function_output), ctx->me->segmenter_mode,
&unquoted))
{
ds_swap (&function_output, &unquoted);
}
struct string lhs_tmp, rhs_tmp;
- int cmp = strcmp (unquote_string_in_place (lhs, ctx->mc->segmenter_mode,
+ int cmp = strcmp (unquote_string_in_place (lhs, ctx->me->segmenter_mode,
&lhs_tmp),
- unquote_string_in_place (rhs, ctx->mc->segmenter_mode,
+ unquote_string_in_place (rhs, ctx->me->segmenter_mode,
&rhs_tmp));
ds_destroy (&lhs_tmp);
ds_destroy (&rhs_tmp);
macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
int nesting_countdown,
const struct macro_set *macros,
- const struct macro_call *mc,
+ const struct macro_expander *me,
const struct macro_expansion_stack *stack,
struct string_map *vars, bool *expand)
{
const struct expr_context ctx = {
.nesting_countdown = nesting_countdown,
.macros = macros,
- .mc = mc,
+ .me = me,
.stack = stack,
.vars = vars,
.expand = expand,
macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
int nesting_countdown,
const struct macro_set *macros,
- const struct macro_call *mc,
+ const struct macro_expander *me,
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, mc, stack, vars, expand);
+ macros, me, stack, vars, expand);
if (!s)
return false;
struct macro_tokens mts = { .n = 0 };
- macro_tokens_from_string__ (&mts, ss_cstr (s), mc->segmenter_mode, stack);
+ macro_tokens_from_string__ (&mts, ss_cstr (s), me->segmenter_mode, stack);
if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
{
macro_error (stack, mts.n > 0 ? &mts.mts[0] : NULL,
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_call *mc,
+ const struct macro_expander *me,
const struct macro_expansion_stack *stack,
struct string_map *vars,
bool *expand, bool *break_, struct macro_tokens *exp)
p++;
char *result = macro_evaluate_expression (&p, end - p,
nesting_countdown,
- macros, mc,
+ macros, me,
stack, vars, expand);
if (!result)
return 0;
.mts = CONST_CAST (struct macro_token *, start),
.n = n,
};
- macro_expand (&mts, nesting_countdown, macros, mc, vars,
+ macro_expand (&mts, nesting_countdown, macros, me, vars,
&(struct macro_expansion_stack) {
.name = "!IF",
.next = stack,
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_call *mc,
+ const struct macro_expander *me,
const struct macro_expansion_stack *stack,
struct string_map *vars, bool *expand)
{
}
const struct substring var_name = p->token.string;
if (is_macro_keyword (var_name)
- || macro_find_parameter_by_name (mc->macro, var_name))
+ || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
{
macro_error (stack, p < end ? p : NULL,
_("Cannot use argument name or macro keyword "
p++;
char *value = macro_evaluate_expression (&p, end - p, nesting_countdown,
- macros, mc, stack, vars, expand);
+ macros, me, stack, vars, expand);
if (!value)
return 0;
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_call *mc,
+ const struct macro_expander *me,
const struct macro_expansion_stack *stack,
struct string_map *vars,
bool *expand, struct macro_tokens *exp)
}
const struct substring var_name = p->token.string;
if (is_macro_keyword (var_name)
- || macro_find_parameter_by_name (mc->macro, var_name))
+ || (me->macro && macro_find_parameter_by_name (me->macro, var_name)))
{
macro_error (stack, p, _("Cannot use argument name or macro "
"keyword as !DO variable."));
{
p++;
char *list = macro_evaluate_expression (&p, end - p, nesting_countdown,
- macros, mc, &next_stack, vars,
+ macros, me, &next_stack, vars,
expand);
if (!list)
return 0;
struct macro_tokens items = { .n = 0 };
- macro_tokens_from_string__ (&items, ss_cstr (list), mc->segmenter_mode,
+ macro_tokens_from_string__ (&items, ss_cstr (list), me->segmenter_mode,
stack);
free (list);
bool break_ = false;
macro_expand (&inner, nesting_countdown, macros,
- mc, vars, &next_stack, expand, &break_, exp);
+ me, vars, &next_stack, expand, &break_, exp);
if (break_)
break;
}
p++;
double first;
if (!macro_evaluate_number (&p, end - p, nesting_countdown,
- macros, mc, &next_stack,
+ macros, me, &next_stack,
vars, expand, &first))
return 0;
double last;
if (!macro_evaluate_number (&p, end - p, nesting_countdown,
- macros, mc, &next_stack,
+ macros, me, &next_stack,
vars, expand, &last))
return 0;
{
p++;
if (!macro_evaluate_number (&p, end - p, nesting_countdown,
- macros, mc, &next_stack,
+ macros, me, &next_stack,
vars, expand, &by))
return 0;
bool break_ = false;
macro_expand (&inner, nesting_countdown,
- macros, mc, vars, &next_stack, expand, &break_,
+ macros, me, vars, &next_stack, expand, &break_,
exp);
if (break_)
break;
static void
macro_expand (const struct macro_tokens *mts, int nesting_countdown,
const struct macro_set *macros,
- const struct macro_call *mc, struct string_map *vars,
+ const struct macro_expander *me, struct string_map *vars,
const struct macro_expansion_stack *stack,
bool *expand, bool *break_, struct macro_tokens *exp)
{
{
const struct macro_token *mt = &mts->mts[i];
const struct token *token = &mt->token;
- if (token->type == T_MACRO_ID && mc)
+ if (token->type == T_MACRO_ID && me->macro)
{
const struct macro_param *param = macro_find_parameter_by_name (
- mc->macro, token->string);
+ me->macro, token->string);
if (param)
{
- const struct macro_tokens *arg = mc->args[param - mc->macro->params];
+ const struct macro_tokens *arg = me->args[param - me->macro->params];
if (*expand && param->expand_arg)
- macro_expand (arg, nesting_countdown,
- macros, NULL, NULL,
- &(struct macro_expansion_stack) {
- .name = param->name,
- .next = stack,
- }, expand, break_, exp);
+ {
+ struct macro_expander subme = {
+ .macros = me->macros,
+ .macro = NULL,
+ .args = NULL,
+ .segmenter_mode = me->segmenter_mode,
+ };
+ macro_expand (arg, nesting_countdown,
+ macros, &subme, NULL,
+ &(struct macro_expansion_stack) {
+ .name = param->name,
+ .next = stack,
+ }, expand, break_, exp);
+ }
else
for (size_t i = 0; i < arg->n; i++)
macro_tokens_add (exp, &arg->mts[i]);
if (is_bang_star (mts->mts, mts->n, i))
{
- for (size_t j = 0; j < mc->macro->n_params; j++)
+ for (size_t j = 0; j < me->macro->n_params; j++)
{
- const struct macro_param *param = &mc->macro->params[j];
+ const struct macro_param *param = &me->macro->params[j];
if (!param->positional)
break;
- const struct macro_tokens *arg = mc->args[j];
+ const struct macro_tokens *arg = me->args[j];
if (*expand && param->expand_arg)
- macro_expand (arg, nesting_countdown,
- macros, NULL, NULL,
- &(struct macro_expansion_stack) {
- .name = "!*",
- .next = stack,
- }, expand, break_, exp);
+ {
+ struct macro_expander subme = {
+ .macros = me->macros,
+ .macro = NULL,
+ .args = NULL,
+ .segmenter_mode = me->segmenter_mode,
+ };
+ macro_expand (arg, nesting_countdown,
+ macros, &subme, NULL,
+ &(struct macro_expansion_stack) {
+ .name = "!*",
+ .next = stack,
+ }, expand, break_, exp);
+ }
else
for (size_t k = 0; k < arg->n; k++)
macro_tokens_add (exp, &arg->mts[k]);
size_t n = macro_expand_if (&mts->mts[i], mts->n - i,
nesting_countdown,
- macros, mc, stack,
+ macros, me, stack,
vars, expand, break_, exp);
if (n > 0)
{
if (value)
{
macro_tokens_from_string__ (exp, ss_cstr (value),
- mc->segmenter_mode, stack);
+ me->segmenter_mode, stack);
continue;
}
}
if (*expand)
{
- struct macro_call *subme;
- int retval = macro_call_create (macros, token, &subme);
+ struct macro_call *submc;
+ int retval = macro_call_create (macros, token, &submc);
for (size_t j = 1; !retval; j++)
{
- const struct macro_token endcmd = { .token = { .type = T_ENDCMD } };
+ const struct macro_token endcmd
+ = { .token = { .type = T_ENDCMD } };
retval = macro_call_add (
- subme, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
+ submc, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
}
if (retval > 0)
{
i += retval - 1;
- macro_expand (&subme->macro->body, nesting_countdown - 1,
- macros, subme, NULL,
+ struct macro_expander subme = {
+ .macros = submc->macros,
+ .macro = submc->macro,
+ .args = submc->args,
+ .segmenter_mode = me->segmenter_mode,
+ };
+ macro_expand (&submc->macro->body, nesting_countdown - 1,
+ macros, &subme, NULL,
&(struct macro_expansion_stack) {
- .name = subme->macro->name,
- .file_name = subme->macro->file_name,
- .first_line = subme->macro->first_line,
- .last_line = subme->macro->last_line,
+ .name = submc->macro->name,
+ .file_name = submc->macro->file_name,
+ .first_line = submc->macro->first_line,
+ .last_line = submc->macro->last_line,
.next = stack,
}, expand, break_, exp);
- macro_call_destroy (subme);
+ macro_call_destroy (submc);
continue;
}
- macro_call_destroy (subme);
+ macro_call_destroy (submc);
}
if (token->type != T_MACRO_ID)
.n_input = mts->n - i,
.nesting_countdown = nesting_countdown,
.macros = macros,
- .mc = mc,
+ .me = me,
.stack = stack,
.vars = vars,
.expand = expand,
i += function_consumed - 1;
macro_tokens_from_string__ (exp, function_output.ss,
- mc->segmenter_mode, stack);
+ me->segmenter_mode, stack);
ds_destroy (&function_output);
continue;
size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
nesting_countdown,
- macros, mc, stack, vars, expand);
+ macros, me, stack, vars, expand);
if (n > 0)
{
i += n - 1;
}
n = macro_expand_do (&mts->mts[i], mts->n - i,
- nesting_countdown, macros, mc, stack,
+ nesting_countdown, macros, me, stack,
vars, expand, exp);
if (n > 0)
{
struct macro_tokens *exp)
{
assert (mc->state == MC_FINISHED);
- mc->segmenter_mode = segmenter_mode;
+
+ struct macro_expander me = {
+ .macros = mc->macros,
+ .macro = mc->macro,
+ .args = mc->args,
+ .segmenter_mode = segmenter_mode,
+ };
bool expand = true;
struct macro_expansion_stack stack = {
.last_line = mc->macro->last_line,
};
macro_expand (&mc->macro->body, settings_get_mnest (),
- mc->macros, mc, NULL, &stack, &expand, NULL, exp);
+ mc->macros, &me, NULL, &stack, &expand, NULL, exp);
}