#include "language/lexer/macro.h"
#include "language/lexer/scan.h"
#include "language/lexer/token.h"
+#include "libpspp/message.h"
#include "gl/xalloc.h"
struct substring s = lex_tokss (lexer);
struct string_lexer slex;
string_lexer_init (&slex, s.string, s.length, SEG_MODE_INTERACTIVE);
- struct token another_token;
+ struct token another_token = { .type = T_STOP };
if (!string_lexer_next (&slex, token)
|| string_lexer_next (&slex, &another_token))
{
settings_set_mprint (true);
while (lex_token (lexer) != T_STOP)
- lex_get (lexer);
+ {
+ if (!lex_next_is_from_macro (lexer, 0) && lex_token (lexer) != T_ENDCMD)
+ {
+ struct substring rep = lex_next_representation (lexer, 0, 0);
+ msg (MN, "unexpanded token \"%.*s\"", (int) rep.length, rep.string);
+ }
+ lex_get (lexer);
+ }
return CMD_SUCCESS;
}
token = &src->tokens[deque_push_front (&src->deque)];
token->token = (struct token) { .type = T_STOP };
+ token->from_macro = false;
return token;
}
return lex_source_get_syntax__ (lex_source__ (lexer), n0, n1);
}
+bool
+lex_next_is_from_macro (const struct lexer *lexer, int n)
+{
+ return lex_next__ (lexer, n)->from_macro;
+}
+
static bool
lex_tokens_match (const struct token *actual, const struct token *expected)
{
/* Token representation. */
struct substring lex_next_representation (const struct lexer *,
int n0, int n1);
+bool lex_next_is_from_macro (const struct lexer *, int n);
/* Current position. */
int lex_get_first_line_number (const struct lexer *, int n);
return me_finished (me);
else
{
- me->state = me->param->positional ? ME_ARG : ME_KEYWORD;
+ me->state = (!me->param->positional ? ME_KEYWORD
+ : me->param->arg_type == ARG_ENCLOSE ? ME_ENCLOSE
+ : ME_ARG);
return 0;
}
}
static int
me_add_arg (struct macro_expander *me, const struct macro_token *mt)
{
+ const struct macro_param *p = me->param;
+
const struct token *token = &mt->token;
- if (token->type == T_ENDCMD || token->type == T_STOP)
+ 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);
me->n_tokens++;
- const struct macro_param *p = me->param;
struct macro_tokens **argp = &me->args[p - me->macro->params];
if (!*argp)
*argp = xzalloc (sizeof **argp);
return 1;
else
{
- me->state = macro->params[0].positional ? ME_ARG : ME_KEYWORD;
+ me->state = (!macro->params[0].positional ? ME_KEYWORD
+ : macro->params[0].arg_type == ARG_ENCLOSE ? ME_ENCLOSE
+ : ME_ARG);
me->args = xcalloc (macro->n_params, sizeof *me->args);
me->param = macro->params;
return 0;
"x "" y".
!ENDDEFINE.
DEBUG EXPAND.
-!macro()
+!macro
])
AT_CHECK([pspp --testing-mode define.sps], [0], [dnl
a b c d e f g h.
AT_SETUP([macro expansion with arguments])
AT_DATA([define.sps], [dnl
-DEFINE !t1(!positional !tokens(1)) (!1) !ENDDEFINE.
-DEFINE !t2(!positional !tokens(2)) (!1) !ENDDEFINE.
-DEFINE !ce(!positional !charend('/')) (!1) !ENDDEFINE.
+DEFINE !title(!positional !tokens(1)) !1 !ENDDEFINE.
+DEFINE !t1(!positional !tokens(1)) t1 (!1) !ENDDEFINE.
+DEFINE !t2(!positional !tokens(2)) t2 (!1) !ENDDEFINE.
+
+DEFINE !ce(!positional !charend('/')) ce (!1) !ENDDEFINE.
+DEFINE !ce2(!positional !charend('(')
+ /!positional !charend(')'))
+ce2 (!1, !2)
+!ENDDEFINE.
+
+DEFINE !e(!positional !enclose('{','}')) e (!1) !ENDDEFINE.
+
+DEFINE !cmd(!positional !cmdend) cmd(!1) !ENDDEFINE.
+DEFINE !cmd2(!positional !cmdend
+ /!positional !tokens(1))
+cmd2(!1, !2)
+!ENDDEFINE.
+
DEBUG EXPAND.
+!title "!TOKENS(1) argument."
!t1 a.
!t1 b.
!t1 a b.
+!title "!TOKENS(2) argument."
!t2 a b.
!t2 b c d.
+!title "!CHAREND argument."
+!ce/.
+!ce x/.
+!ce x y/.
!ce x y z/.
+
+!title "Two !CHAREND arguments."
+!ce2 x(y).
+!ce2 1 2 3 4().
+
+!title "!ENCLOSE argument."
+!e {}.
+!e {a}.
+!e {a b}.
+
+!title "!CMDEND argument."
+!cmd 1 2 3 4.
+!cmd2 5 6.
+7.
])
AT_CHECK([pspp --testing-mode define.sps], [0], [dnl
-(a)
+"!TOKENS(1) argument."
+
+t1(a)
+
+t1(b)
+
+t1(a)
+
+note: unexpanded token "b"
+
+"!TOKENS(2) argument."
+
+t2(a b)
+
+t2(b c)
+
+note: unexpanded token "d"
+
+"!CHAREND argument."
+
+ce( )
+
+ce(x)
+
+ce(x y)
+
+ce(x y z)
+
+"Two !CHAREND arguments."
+
+ce2(x, y)
+
+ce2(1 2 3 4, )
+
+"!ENCLOSE argument."
+
+e( )
-(b)
+e(a)
-(a)
+e(a b)
-(a b)
+"!CMDEND argument."
-(b c)
+cmd(1 2 3 4)
-(x y z)
+cmd2(5 6, 7)
])
AT_CLEANUP