improve macro error messages
[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/segment.h"
26 #include "language/lexer/token.h"
27
28 struct macro_expander;
29
30 struct macro_token
31   {
32     struct token token;
33     struct substring representation;
34   };
35
36 void macro_token_copy (struct macro_token *, const struct macro_token *);
37 void macro_token_uninit (struct macro_token *);
38
39 void macro_token_to_representation (struct macro_token *, struct string *);
40
41 bool is_macro_keyword (struct substring);
42
43 struct macro_tokens
44   {
45     struct macro_token *mts;
46     size_t n;
47     size_t allocated;
48   };
49
50 void macro_tokens_copy (struct macro_tokens *, const struct macro_tokens *);
51 void macro_tokens_uninit (struct macro_tokens *);
52 struct macro_token *macro_tokens_add_uninit (struct macro_tokens *);
53 void macro_tokens_add (struct macro_tokens *, const struct macro_token *);
54
55 void macro_tokens_from_string (struct macro_tokens *, const struct substring,
56                                enum segmenter_mode);
57
58 void macro_tokens_to_representation (struct macro_tokens *, struct string *,
59                                      size_t *ofs, size_t *len);
60
61 void macro_tokens_print (const struct macro_tokens *, FILE *);
62
63 struct macro_param
64   {
65     bool positional;            /* Is this a positional parameter? */
66     char *name;                 /* "!1" or "!name". */
67     struct macro_tokens def;    /* Default expansion. */
68     bool expand_arg;            /* Macro-expand the argument? */
69
70     enum
71       {
72         ARG_N_TOKENS,
73         ARG_CHAREND,
74         ARG_ENCLOSE,
75         ARG_CMDEND
76       }
77     arg_type;
78     union
79       {
80         int n_tokens;
81         struct token charend;
82         struct token enclose[2];
83       };
84   };
85
86 struct macro
87   {
88     struct hmap_node hmap_node; /* Indexed by 'name'. */
89     char *name;
90
91     struct macro_param *params;
92     size_t n_params;
93
94     struct macro_tokens body;
95   };
96
97 void macro_destroy (struct macro *);
98
99 struct macro_set
100   {
101     struct hmap macros;
102   };
103
104 struct macro_set *macro_set_create (void);
105 void macro_set_destroy (struct macro_set *);
106 const struct macro *macro_set_find (const struct macro_set *,
107                                     const char *);
108 void macro_set_add (struct macro_set *, struct macro *);
109
110 static inline bool
111 macro_set_is_empty (const struct macro_set *set)
112 {
113   return hmap_is_empty (&set->macros);
114 }
115 \f
116 /* Macro expansion. */
117
118 int macro_expander_create (const struct macro_set *,
119                            const struct token *,
120                            struct macro_expander **);
121 void macro_expander_destroy (struct macro_expander *);
122
123 int macro_expander_add (struct macro_expander *, const struct macro_token *);
124
125 void macro_expander_get_expansion (struct macro_expander *, struct macro_tokens *);
126
127 #endif /* macro.h */