DEFINE: New command.
[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 /* A token along with the syntax that was tokenized to produce it.  The syntax
29    allows the token to be turned back into syntax accurately. */
30 struct macro_token
31   {
32     struct token token;
33     struct substring syntax;
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_syntax (struct macro_token *, struct string *);
40
41 bool is_macro_keyword (struct substring);
42 \f
43 /* A dynamic array of macro tokens.
44
45    The syntax for the tokens doesn't include white space, etc. between them. */
46 struct macro_tokens
47   {
48     struct macro_token *mts;
49     size_t n;
50     size_t allocated;
51   };
52
53 void macro_tokens_copy (struct macro_tokens *, const struct macro_tokens *);
54 void macro_tokens_uninit (struct macro_tokens *);
55 struct macro_token *macro_tokens_add_uninit (struct macro_tokens *);
56 void macro_tokens_add (struct macro_tokens *, const struct macro_token *);
57
58 void macro_tokens_from_string (struct macro_tokens *, const struct substring,
59                                enum segmenter_mode);
60
61 void macro_tokens_to_syntax (struct macro_tokens *, struct string *,
62                              size_t *ofs, size_t *len);
63
64 void macro_tokens_print (const struct macro_tokens *, FILE *);
65 \f
66 /* A parameter to a macro. */
67 struct macro_param
68   {
69     bool positional;            /* Is this a positional parameter? */
70     char *name;                 /* "!1" or "!name". */
71     struct macro_tokens def;    /* Default expansion. */
72     bool expand_arg;            /* Macro-expand the argument? */
73
74     enum
75       {
76         ARG_N_TOKENS,
77         ARG_CHAREND,
78         ARG_ENCLOSE,
79         ARG_CMDEND
80       }
81     arg_type;
82     union
83       {
84         int n_tokens;            /* ARG_N_TOKENS. */
85         struct token charend;    /* ARG_CHAREND. */
86         struct token enclose[2]; /* ARG_ENCLOSE. */
87       };
88   };
89
90 /* A macro. */
91 struct macro
92   {
93     struct hmap_node hmap_node; /* Indexed by 'name'. */
94     char *name;
95
96     /* Source code location of macro definition, for error reporting. */
97     char *file_name;
98     int first_line;
99     int last_line;
100
101     /* Parameters. */
102     struct macro_param *params;
103     size_t n_params;
104
105     /* Body. */
106     struct macro_tokens body;
107   };
108
109 void macro_destroy (struct macro *);
110 \f
111 /* A collection of macros. */
112 struct macro_set
113   {
114     struct hmap macros;
115   };
116
117 struct macro_set *macro_set_create (void);
118 void macro_set_destroy (struct macro_set *);
119 const struct macro *macro_set_find (const struct macro_set *,
120                                     const char *);
121 void macro_set_add (struct macro_set *, struct macro *);
122
123 static inline bool
124 macro_set_is_empty (const struct macro_set *set)
125 {
126   return hmap_is_empty (&set->macros);
127 }
128 \f
129 /* Parsing and expanding macro calls. */
130
131 struct macro_call;
132
133 int macro_call_create (const struct macro_set *, const struct token *,
134                       struct macro_call **);
135 int macro_call_add (struct macro_call *, const struct macro_token *);
136
137 void macro_call_expand (struct macro_call *, enum segmenter_mode segmenter_mode,
138                         struct macro_tokens *);
139
140 void macro_call_destroy (struct macro_call *);
141
142 #endif /* macro.h */