#include "libpspp/message.h"
#include "libpspp/str.h"
#include "libpspp/string-array.h"
+#include "libpspp/string-map.h"
+#include "libpspp/stringi-set.h"
#include "gl/c-ctype.h"
+#include "gl/ftoastr.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
ds_put_substring (s, mt->representation);
}
+bool
+is_macro_keyword (struct substring s)
+{
+ static struct stringi_set keywords = STRINGI_SET_INITIALIZER (keywords);
+ if (stringi_set_is_empty (&keywords))
+ {
+ static const char *kws[] = {
+ "BREAK",
+ "CHAREND",
+ "CMDEND",
+ "DEFAULT",
+ "DO",
+ "DOEND",
+ "ELSE",
+ "ENCLOSE",
+ "ENDDEFINE",
+ "IF",
+ "IFEND",
+ "IN",
+ "LET",
+ "NOEXPAND",
+ "OFFEXPAND",
+ "ONEXPAND",
+ "POSITIONAL",
+ "THEN",
+ "TOKENS",
+ };
+ for (size_t i = 0; i < sizeof kws / sizeof *kws; i++)
+ stringi_set_insert (&keywords, kws[i]);
+ }
+
+ ss_ltrim (&s, ss_cstr ("!"));
+ return stringi_set_contains_len (&keywords, s.string, s.length);
+}
+
void
macro_tokens_copy (struct macro_tokens *dst, const struct macro_tokens *src)
{
};
struct state state = {
- .segmenter = SEGMENTER_INIT (mode),
+ .segmenter = segmenter_init (mode, true),
.body = src,
};
struct state saved = state;
}
void
-macro_tokens_to_representation (struct macro_tokens *mts, struct string *s)
+macro_tokens_to_representation (struct macro_tokens *mts, struct string *s,
+ size_t *ofs, size_t *len)
{
+ assert ((ofs != NULL) == (len != NULL));
+
if (!mts->n)
return;
- macro_token_to_representation (&mts->mts[0], s);
- for (size_t i = 1; i < mts->n; i++)
+ for (size_t i = 0; i < mts->n; i++)
{
- enum token_type prev = mts->mts[i - 1].token.type;
- enum token_type next = mts->mts[i].token.type;
-
- if (prev == T_ENDCMD)
- ds_put_byte (s, '\n');
- else
+ if (i > 0)
{
- enum token_class pc = classify_token (prev);
- enum token_class nc = classify_token (next);
- if (needs_space (pc, nc))
- ds_put_byte (s, ' ');
+ enum token_type prev = mts->mts[i - 1].token.type;
+ enum token_type next = mts->mts[i].token.type;
+
+ if (prev == T_ENDCMD)
+ ds_put_byte (s, '\n');
+ else
+ {
+ enum token_class pc = classify_token (prev);
+ enum token_class nc = classify_token (next);
+ if (needs_space (pc, nc))
+ ds_put_byte (s, ' ');
+ }
}
+ if (ofs)
+ ofs[i] = s->ss.length;
macro_token_to_representation (&mts->mts[i], s);
+ if (len)
+ len[i] = s->ss.length - ofs[i];
}
}
static const struct macro_param *
macro_find_parameter_by_name (const struct macro *m, struct substring name)
{
- if (ss_first (name) == '!')
- ss_advance (&name, 1);
+ ss_ltrim (&name, ss_cstr ("!"));
for (size_t i = 0; i < m->n_params; i++)
{
int nesting_countdown;
const struct macro_set *macros;
const struct macro_expander *me;
+ struct string_map *vars;
bool *expand;
};
static void
macro_expand (const struct macro_tokens *,
int nesting_countdown, const struct macro_set *,
- const struct macro_expander *, bool *expand, struct macro_tokens *exp);
+ const struct macro_expander *, struct string_map *vars,
+ bool *expand, bool *break_, struct macro_tokens *exp);
static bool
expand_macro_function (struct parse_macro_function_ctx *ctx,
return 2;
}
+ if (ctx->vars)
+ {
+ const char *value = string_map_find__ (ctx->vars,
+ token->string.string,
+ token->string.length);
+ if (value)
+ {
+ ds_put_cstr (farg, value);
+ return 1;
+ }
+ }
+
struct parse_macro_function_ctx subctx = {
.input = &ctx->input[i],
.n_input = ctx->n_input - i,
.nesting_countdown = ctx->nesting_countdown,
.macros = ctx->macros,
.me = ctx->me,
+ .vars = ctx->vars,
.expand = ctx->expand,
};
size_t subinput_consumed;
if (!n_tokens
|| tokens[0].token.type != T_MACRO_ID
- || !ss_equals_case (tokens[0].token.string, function))
+ || !ss_equals_case (tokens[0].token.string, function)) /* XXX abbrevs allowed */
return false;
if (n_tokens < 2 || tokens[1].token.type != T_LPAREN)
unquote_string (const char *s, struct string *content)
{
struct string_lexer slex;
- string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE /* XXX */);
+ string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE /* XXX */,
+ true);
struct token token1;
if (!string_lexer_next (&slex, &token1))
if (mts.n > 1)
{
struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
- macro_tokens_to_representation (&tail, output);
+ macro_tokens_to_representation (&tail, output, NULL, NULL);
}
macro_tokens_uninit (&mts);
ds_destroy (&tmp);
macro_tokens_from_string (&mts, ss_cstr (args.strings[0]),
SEG_MODE_INTERACTIVE /* XXX */);
struct macro_tokens exp = { .n = 0 };
- macro_expand (&mts, ctx->nesting_countdown - 1, ctx->macros,
- ctx->me, ctx->expand, &exp);
- macro_tokens_to_representation (&exp, output);
+ macro_expand (&mts, ctx->nesting_countdown - 1, ctx->macros, ctx->me,
+ ctx->vars, ctx->expand, NULL, &exp);
+ macro_tokens_to_representation (&exp, output, NULL, NULL);
macro_tokens_uninit (&exp);
macro_tokens_uninit (&mts);
}
int nesting_countdown;
const struct macro_set *macros;
const struct macro_expander *me;
+ struct string_map *vars;
bool *expand;
};
.nesting_countdown = ctx->nesting_countdown,
.macros = ctx->macros,
.me = ctx->me,
+ .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), &unquoted))
+ {
+ ds_swap (&function_output, &unquoted);
+ ds_destroy (&unquoted);
+ }
*tokens = p + function_consumed;
return ds_steal_cstr (&function_output);
}
static char *
macro_evaluate_relational (const struct expr_context *ctx,
- const struct macro_token **tokens,
- const struct macro_token *end)
+ const struct macro_token **tokens,
+ const struct macro_token *end)
{
const struct macro_token *p = *tokens;
char *lhs = macro_evaluate_literal (ctx, &p, end);
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, bool *expand)
+ const struct macro_expander *me, struct string_map *vars,
+ bool *expand)
{
const struct expr_context ctx = {
.nesting_countdown = nesting_countdown,
.macros = macros,
.me = me,
+ .vars = vars,
.expand = expand,
};
return macro_evaluate_or (&ctx, tokens, *tokens + n_tokens);
}
+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, struct string_map *vars,
+ bool *expand, double *number)
+{
+ char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown,
+ macros, me, vars, expand);
+ if (!s)
+ return false;
+
+ struct macro_tokens mts = { .n = 0 };
+ macro_tokens_from_string (&mts, ss_cstr (s), SEG_MODE_INTERACTIVE /* XXX */);
+ if (mts.n != 1 || !token_is_number (&mts.mts[0].token))
+ {
+ macro_tokens_print (&mts, stdout);
+ printf ("expression must evaluate to a number (not %s)\n", s);
+ free (s);
+ macro_tokens_uninit (&mts);
+ return false;
+ }
+
+ *number = token_number (&mts.mts[0].token);
+ free (s);
+ macro_tokens_uninit (&mts);
+ return true;
+}
+
static const struct macro_token *
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, bool *expand,
- struct macro_tokens *exp)
+ const struct macro_expander *me, struct string_map *vars,
+ bool *expand, bool *break_, struct macro_tokens *exp)
{
const struct macro_token *p = tokens;
const struct macro_token *end = tokens + n_tokens;
p++;
char *result = macro_evaluate_expression (&p, end - p,
- nesting_countdown, macros, me, expand);
+ nesting_countdown, macros, me, vars,
+ expand);
if (!result)
return 0;
bool b = strcmp (result, "0");
.mts = CONST_CAST (struct macro_token *, start),
.n = n,
};
- macro_expand (&mts, nesting_countdown, macros, me, expand, exp);
+ macro_expand (&mts, nesting_countdown, macros, me, vars, expand,
+ break_, exp);
}
return (end_if + 1) - 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, struct string_map *vars,
+ bool *expand)
+{
+ const struct macro_token *p = tokens;
+ const struct macro_token *end = tokens + n_tokens;
+
+ if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!LET")))
+ return 0;
+ p++;
+
+ if (p >= end || p->token.type != T_MACRO_ID)
+ {
+ printf ("expected macro variable name following !LET\n");
+ return 0;
+ }
+ const struct substring var_name = p->token.string;
+ if (is_macro_keyword (var_name)
+ || macro_find_parameter_by_name (me->macro, var_name))
+ {
+ printf ("cannot use argument name or macro keyword as !LET variable\n");
+ return 0;
+ }
+ p++;
+
+ if (p >= end || p->token.type != T_EQUALS)
+ {
+ printf ("expected = following !LET\n");
+ return 0;
+ }
+ p++;
+
+ char *value = macro_evaluate_expression (&p, end - p,
+ nesting_countdown, macros, me, vars,
+ expand);
+ if (!value)
+ return 0;
+
+ string_map_replace_nocopy (vars, ss_xstrdup (var_name), value);
+ return p - tokens;
+}
+
+static const struct macro_token *
+find_doend (const struct macro_token *p, const struct macro_token *end)
+{
+ size_t nesting = 0;
+ for (; p < end; p++)
+ {
+ if (p->token.type != T_MACRO_ID)
+ continue;
+
+ if (ss_equals_case (p->token.string, ss_cstr ("!DO")))
+ nesting++;
+ else if (ss_equals_case (p->token.string, ss_cstr ("!DOEND")))
+ {
+ if (!nesting)
+ return p;
+ nesting--;
+ }
+ }
+ printf ("missing !DOEND\n");
+ return NULL;
+}
+
+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, struct string_map *vars,
+ bool *expand, struct macro_tokens *exp)
+{
+ const struct macro_token *p = tokens;
+ const struct macro_token *end = tokens + n_tokens;
+
+ if (p >= end || !ss_equals_case (p->token.string, ss_cstr ("!DO")))
+ return 0;
+ p++;
+
+ if (p >= end || p->token.type != T_MACRO_ID)
+ {
+ printf ("expected macro variable name following !DO\n");
+ return 0;
+ }
+ const struct substring var_name = p->token.string;
+ if (is_macro_keyword (var_name)
+ || macro_find_parameter_by_name (me->macro, var_name))
+ {
+ printf ("cannot use argument name or macro keyword as !DO variable\n");
+ return 0;
+ }
+ p++;
+
+ int miterate = settings_get_miterate ();
+ if (p < end && p->token.type == T_MACRO_ID
+ && ss_equals_case (p->token.string, ss_cstr ("!IN")))
+ {
+ p++;
+ char *list = macro_evaluate_expression (&p, end - p,
+ nesting_countdown, macros, me, vars,
+ expand);
+ if (!list)
+ return 0;
+
+ struct macro_tokens items = { .n = 0 };
+ macro_tokens_from_string (&items, ss_cstr (list),
+ SEG_MODE_INTERACTIVE /* XXX */);
+ free (list);
+
+ const struct macro_token *do_end = find_doend (p, end);
+ if (!do_end)
+ {
+ macro_tokens_uninit (&items);
+ return 0;
+ }
+
+ const struct macro_tokens inner = {
+ .mts = CONST_CAST (struct macro_token *, p),
+ .n = do_end - p
+ };
+ for (size_t i = 0; i < items.n; i++)
+ {
+ if (i >= miterate)
+ {
+ printf ("exceeded maximum number of iterations %d\n", miterate);
+ break;
+ }
+ string_map_replace_nocopy (vars, ss_xstrdup (var_name),
+ ss_xstrdup (items.mts[i].representation));
+
+ bool break_ = false;
+ macro_expand (&inner, nesting_countdown, macros,
+ me, vars, expand, &break_, exp);
+ if (break_)
+ break;
+ }
+ return do_end - tokens + 1;
+ }
+ else if (p < end && p->token.type == T_EQUALS)
+ {
+ p++;
+ double first;
+ if (!macro_evaluate_number (&p, end - p, nesting_countdown, macros, me,
+ vars, expand, &first))
+ return 0;
+
+ if (p >= end || p->token.type != T_MACRO_ID
+ || !ss_equals_case (p->token.string, ss_cstr ("!TO")))
+ {
+ printf ("expecting !TO\n");
+ return 0;
+ }
+ p++;
+
+ double last;
+ if (!macro_evaluate_number (&p, end - p, nesting_countdown, macros, me,
+ vars, expand, &last))
+ return 0;
+
+ double by = 1.0;
+ if (p < end && p->token.type == T_MACRO_ID
+ && ss_equals_case (p->token.string, ss_cstr ("!BY")))
+ {
+ p++;
+ if (!macro_evaluate_number (&p, end - p, nesting_countdown, macros, me,
+ vars, expand, &by))
+ return 0;
+
+ if (by == 0.0)
+ {
+ printf ("!BY value cannot be zero\n");
+ return 0;
+ }
+ }
+
+ const struct macro_token *do_end = find_doend (p, end);
+ if (!do_end)
+ return 0;
+ const struct macro_tokens inner = {
+ .mts = CONST_CAST (struct macro_token *, p),
+ .n = do_end - p
+ };
+
+ if ((by > 0 && first <= last) || (by < 0 && first >= last))
+ {
+ int i = 0;
+ for (double index = first;
+ by > 0 ? (index <= last) : (index >= last);
+ index += by)
+ {
+ if (i++ > miterate)
+ {
+ printf ("exceeded maximum number of iterations %d\n",
+ miterate);
+ break;
+ }
+
+ char index_s[DBL_BUFSIZE_BOUND];
+ c_dtoastr (index_s, sizeof index_s, 0, 0, index);
+ string_map_replace_nocopy (vars, ss_xstrdup (var_name),
+ xstrdup (index_s));
+
+ bool break_ = false;
+ macro_expand (&inner, nesting_countdown, macros,
+ me, vars, expand, &break_, exp);
+ if (break_)
+ break;
+ }
+ }
+
+ return do_end - tokens + 1;
+ }
+ else
+ {
+ printf ("expecting = or !IN in !DO loop\n");
+ return 0;
+ }
+}
+
static void
macro_expand (const struct macro_tokens *mts,
int nesting_countdown, const struct macro_set *macros,
- const struct macro_expander *me, bool *expand,
- struct macro_tokens *exp)
+ const struct macro_expander *me, struct string_map *vars,
+ bool *expand, bool *break_, struct macro_tokens *exp)
{
if (nesting_countdown <= 0)
{
return;
}
- for (size_t i = 0; i < mts->n; i++)
+ struct string_map own_vars = STRING_MAP_INITIALIZER (own_vars);
+ if (!vars)
+ vars = &own_vars;
+
+ for (size_t i = 0; i < mts->n && (!break_ || !*break_); i++)
{
const struct macro_token *mt = &mts->mts[i];
const struct token *token = &mt->token;
const struct macro_tokens *arg = me->args[param - me->macro->params];
//macro_tokens_print (arg, stdout);
if (*expand && param->expand_arg)
- macro_expand (arg, nesting_countdown, macros, NULL, expand, exp);
+ macro_expand (arg, nesting_countdown, macros, NULL, NULL,
+ expand, break_, exp);
else
for (size_t i = 0; i < arg->n; i++)
macro_tokens_add (exp, &arg->mts[i]);
const struct macro_tokens *arg = me->args[j];
if (*expand && param->expand_arg)
- macro_expand (arg, nesting_countdown, macros, NULL, expand, exp);
+ macro_expand (arg, nesting_countdown, macros, NULL, NULL,
+ 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, me, expand,
- exp);
+ nesting_countdown, macros, me, vars,
+ expand, break_, exp);
if (n > 0)
{
i += n - 1;
}
}
+ if (token->type == T_MACRO_ID && vars)
+ {
+ const char *value = string_map_find__ (vars, token->string.string,
+ token->string.length);
+ if (value)
+ {
+ macro_tokens_from_string (exp, ss_cstr (value),
+ SEG_MODE_INTERACTIVE /* XXX */);
+ continue;
+ }
+ }
+
if (*expand)
{
struct macro_expander *subme;
{
i += retval - 1;
macro_expand (&subme->macro->body, nesting_countdown - 1, macros,
- subme, expand, exp);
+ subme, NULL, expand, break_, exp);
macro_expander_destroy (subme);
continue;
}
continue;
}
+ if (ss_equals_case (token->string, ss_cstr ("!break")))
+ {
+ if (!break_)
+ printf ("!BREAK outside !DO\n");
+ else
+ {
+ *break_ = true;
+ break;
+ }
+ }
+
struct parse_macro_function_ctx ctx = {
.input = &mts->mts[i],
.n_input = mts->n - i,
.nesting_countdown = nesting_countdown,
.macros = macros,
.me = me,
+ .vars = vars,
.expand = expand,
};
struct string function_output = DS_EMPTY_INITIALIZER;
continue;
}
+ size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
+ nesting_countdown, macros, me, vars,
+ expand);
+ if (n > 0)
+ {
+ i += n - 1;
+ continue;
+ }
+
+ n = macro_expand_do (&mts->mts[i], mts->n - i,
+ nesting_countdown, macros, me, vars,
+ expand, exp);
+ if (n > 0)
+ {
+ i += n - 1;
+ continue;
+ }
+
if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
*expand = true;
else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
else
macro_tokens_add (exp, mt);
}
+ if (vars == &own_vars)
+ string_map_destroy (&own_vars);
}
void
bool expand = true;
macro_expand (&me->macro->body, settings_get_mnest (),
- me->macros, me, &expand, exp);
+ me->macros, me, NULL, &expand, NULL, exp);
#if 0
printf ("expansion:\n");