work on macro calls
[pspp] / src / language / lexer / macro.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef MACRO_H
18 #define MACRO_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22
23 #include "libpspp/hmap.h"
24 #include "libpspp/str.h"
25 #include "language/lexer/token.h"
26
27 struct macro_param
28   {
29     char *name;                 /* NULL for a positional parameter. */
30     struct tokens def;          /* Default expansion. */
31     bool expand_arg;            /* Macro-expand the argument? */
32
33     enum
34       {
35         ARG_N_TOKENS,
36         ARG_CHAREND,
37         ARG_ENCLOSE,
38         ARG_CMDEND
39       }
40     arg_type;
41     union
42       {
43         int n_tokens;
44         struct token charend;
45         struct token enclose[2];
46       };
47   };
48
49 struct macro
50   {
51     struct hmap_node hmap_node; /* Indexed by 'name'. */
52     char *name;
53
54     struct macro_param *params;
55     size_t n_params;
56
57     char **body;
58     size_t n_body;
59   };
60
61 void macro_destroy (struct macro *);
62
63 struct macro_set
64   {
65     struct hmap macros;
66   };
67
68 const struct macro *macro_set_find (const struct macro_set *,
69                                     const char *);
70
71 struct macro_expander *macro_expander_create (const struct macro_set *);
72 void macro_expander_destroy (struct macro_expander *);
73
74 /* Add one token to the input to macro-expand.  Returns:
75
76    -1: Advance one token without change.
77     0: Needs more tokens.
78    >0: Expand the given number of tokens.
79 */
80 int macro_expander_add (struct macro_expander *, const struct token *);
81 int macro_expander_add_eof (struct macro_expander *);
82
83 void macro_expander_get_expansion (struct macro_expander *,
84                                    struct token **tokens, size_t *n);
85
86 #endif /* macro.h */