1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #include "libpspp/hmap.h"
24 #include "libpspp/str.h"
25 #include "language/lexer/segment.h"
26 #include "language/lexer/token.h"
28 struct macro_expander;
30 /* A token along with the syntax that was tokenized to produce it. The syntax
31 allows the token to be turned back into syntax accurately. */
35 struct substring representation;
38 void macro_token_copy (struct macro_token *, const struct macro_token *);
39 void macro_token_uninit (struct macro_token *);
41 void macro_token_to_representation (struct macro_token *, struct string *);
43 bool is_macro_keyword (struct substring);
45 /* A dynamic array of macro tokens.
47 The syntax for the tokens doesn't include white space, etc. between them. */
50 struct macro_token *mts;
55 void macro_tokens_copy (struct macro_tokens *, const struct macro_tokens *);
56 void macro_tokens_uninit (struct macro_tokens *);
57 struct macro_token *macro_tokens_add_uninit (struct macro_tokens *);
58 void macro_tokens_add (struct macro_tokens *, const struct macro_token *);
60 void macro_tokens_from_string (struct macro_tokens *, const struct substring,
63 void macro_tokens_to_representation (struct macro_tokens *, struct string *,
64 size_t *ofs, size_t *len);
66 void macro_tokens_print (const struct macro_tokens *, FILE *);
68 /* A parameter to a macro. */
71 bool positional; /* Is this a positional parameter? */
72 char *name; /* "!1" or "!name". */
73 struct macro_tokens def; /* Default expansion. */
74 bool expand_arg; /* Macro-expand the argument? */
86 int n_tokens; /* ARG_N_TOKENS. */
87 struct token charend; /* ARG_CHAREND. */
88 struct token enclose[2]; /* ARG_ENCLOSE. */
95 struct hmap_node hmap_node; /* Indexed by 'name'. */
98 /* Source code location of macro definition, for error reporting. */
104 struct macro_param *params;
108 struct macro_tokens body;
111 void macro_destroy (struct macro *);
113 /* A collection of macros. */
119 struct macro_set *macro_set_create (void);
120 void macro_set_destroy (struct macro_set *);
121 const struct macro *macro_set_find (const struct macro_set *,
123 void macro_set_add (struct macro_set *, struct macro *);
126 macro_set_is_empty (const struct macro_set *set)
128 return hmap_is_empty (&set->macros);
131 /* Macro expansion. */
133 int macro_expander_create (const struct macro_set *,
134 const struct token *,
135 struct macro_expander **);
136 void macro_expander_destroy (struct macro_expander *);
138 int macro_expander_add (struct macro_expander *, const struct macro_token *);
140 void macro_expander_get_expansion (struct macro_expander *,
141 enum segmenter_mode segmenter_mode,
142 struct macro_tokens *);