ba0f1fe735df4f36a8f989b6d6b94443ec6e0c02
[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_expander;
28
29 struct macro_param
30   {
31     bool positional;            /* Is this a positional parameter? */
32     char *name;                 /* "!1" or "!name". */
33     struct tokens def;          /* Default expansion. */
34     bool expand_arg;            /* Macro-expand the argument? */
35
36     enum
37       {
38         ARG_N_TOKENS,
39         ARG_CHAREND,
40         ARG_ENCLOSE,
41         ARG_CMDEND
42       }
43     arg_type;
44     union
45       {
46         int n_tokens;
47         struct token charend;
48         struct token enclose[2];
49       };
50   };
51
52 struct macro
53   {
54     struct hmap_node hmap_node; /* Indexed by 'name'. */
55     char *name;
56
57     struct macro_param *params;
58     size_t n_params;
59
60     struct substring body;
61     struct tokens body_tokens;
62   };
63
64 void macro_destroy (struct macro *);
65
66 struct macro_set
67   {
68     struct hmap macros;
69   };
70
71 struct macro_set *macro_set_create (void);
72 void macro_set_destroy (struct macro_set *);
73 const struct macro *macro_set_find (const struct macro_set *,
74                                     const char *);
75 void macro_set_add (struct macro_set *, struct macro *);
76
77 static inline bool
78 macro_set_is_empty (const struct macro_set *set)
79 {
80   return hmap_is_empty (&set->macros);
81 }
82 \f
83 /* Macro expansion. */
84
85 int macro_expander_create (const struct macro_set *,
86                            const struct token *,
87                            struct macro_expander **);
88 void macro_expander_destroy (struct macro_expander *);
89
90 int macro_expander_add (struct macro_expander *, const struct token *);
91
92 void macro_expander_get_expansion (struct macro_expander *, struct tokens *);
93
94 #endif /* macro.h */