Break macro structure definition into new file.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 18 Apr 2021 20:48:53 +0000 (13:48 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 30 May 2021 20:31:45 +0000 (13:31 -0700)
src/language/control/define.c
src/language/lexer/automake.mk
src/language/lexer/macro.c [new file with mode: 0644]
src/language/lexer/macro.h [new file with mode: 0644]

index b718ae44202cebc2c10b848a40f24d1de4cd2abc..958ae5035cdb8890c6b4c6038fee43f693727132 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "language/command.h"
 #include "language/lexer/lexer.h"
+#include "language/lexer/macro.h"
 #include "language/lexer/scan.h"
 #include "language/lexer/token.h"
 
@@ -49,47 +50,6 @@ match_macro_id (struct lexer *lexer, const char *id)
     return false;
 }
 
-struct tokens
-  {
-    struct token *tokens;
-    size_t n;
-  };
-
-struct macro_param
-  {
-    char *name;                 /* NULL for a positional parameter. */
-    struct tokens def;          /* Default expansion. */
-    bool expand_arg;            /* Macro-expand the argument? */
-
-    enum
-      {
-        ARG_N_TOKENS,
-        ARG_CHAREND,
-        ARG_ENCLOSE,
-        ARG_CMDEND
-      }
-    arg_type;
-    union
-      {
-        int n_tokens;
-        struct token charend;
-        struct token enclose[2];
-      };
-  };
-
-struct macro
-  {
-    char *name;
-
-    struct macro_param *params;
-    size_t n_params;
-
-    char **body;
-    size_t n_body;
-  };
-
-static void macro_destroy (struct macro *);
-
 static bool
 parse_quoted_token (struct lexer *lexer, struct token *token)
 {
@@ -242,51 +202,3 @@ error:
   macro_destroy (m);
   return CMD_FAILURE;
 }
-
-static void
-tokens_uninit (struct tokens *tokens)
-{
-  for (size_t i = 0; i < tokens->n; i++)
-    token_destroy (&tokens->tokens[i]);
-  free (tokens->tokens);
-}
-
-static void
-macro_destroy (struct macro *m)
-{
-  if (!m)
-    return;
-
-  free (m->name);
-  for (size_t i = 0; i < m->n_params; i++)
-    {
-      struct macro_param *p = &m->params[i];
-      free (p->name);
-
-      tokens_uninit (&p->def);
-
-      switch (p->arg_type)
-        {
-        case ARG_N_TOKENS:
-          break;
-
-        case ARG_CHAREND:
-          token_destroy (&p->charend);
-          break;
-
-        case ARG_ENCLOSE:
-          token_destroy (&p->enclose[0]);
-          token_destroy (&p->enclose[1]);
-          break;
-
-        case ARG_CMDEND:
-          break;
-        }
-    }
-  free (m->params);
-  for (size_t i = 0; i < m->n_body; i++)
-    free (m->body[i]);
-  free (m->body);
-  free (m);
-}
-
index 4387c3dd223b77e879a57b99bfc3541100ee7475..01b3df49c6cb62745a2df2902110a9f652766777 100644 (file)
@@ -24,6 +24,8 @@ language_lexer_sources = \
        src/language/lexer/include-path.h \
        src/language/lexer/lexer.c \
        src/language/lexer/lexer.h \
+       src/language/lexer/macro.c \
+       src/language/lexer/macro.h \
        src/language/lexer/format-parser.c \
        src/language/lexer/format-parser.h \
        src/language/lexer/scan.c \
diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c
new file mode 100644 (file)
index 0000000..ca086fc
--- /dev/null
@@ -0,0 +1,68 @@
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "language/lexer/macro.h"
+
+#include <stdlib.h>
+
+void
+macro_tokens_uninit (struct macro_tokens *tokens)
+{
+  for (size_t i = 0; i < tokens->n; i++)
+    token_destroy (&tokens->tokens[i]);
+  free (tokens->tokens);
+}
+
+void
+macro_destroy (struct macro *m)
+{
+  if (!m)
+    return;
+
+  free (m->name);
+  for (size_t i = 0; i < m->n_params; i++)
+    {
+      struct macro_param *p = &m->params[i];
+      free (p->name);
+
+      macro_tokens_uninit (&p->def);
+
+      switch (p->arg_type)
+        {
+        case ARG_N_TOKENS:
+          break;
+
+        case ARG_CHAREND:
+          token_destroy (&p->charend);
+          break;
+
+        case ARG_ENCLOSE:
+          token_destroy (&p->enclose[0]);
+          token_destroy (&p->enclose[1]);
+          break;
+
+        case ARG_CMDEND:
+          break;
+        }
+    }
+  free (m->params);
+  for (size_t i = 0; i < m->n_body; i++)
+    free (m->body[i]);
+  free (m->body);
+  free (m);
+}
diff --git a/src/language/lexer/macro.h b/src/language/lexer/macro.h
new file mode 100644 (file)
index 0000000..2264ad6
--- /dev/null
@@ -0,0 +1,68 @@
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef MACRO_H
+#define MACRO_H 1
+
+#include <stdbool.h>
+#include <stddef.h>
+
+#include "language/lexer/token.h"
+
+struct macro_tokens
+  {
+    struct token *tokens;
+    size_t n;
+  };
+
+void macro_tokens_uninit (struct macro_tokens *);
+
+struct macro_param
+  {
+    char *name;                 /* NULL for a positional parameter. */
+    struct macro_tokens def;    /* Default expansion. */
+    bool expand_arg;            /* Macro-expand the argument? */
+
+    enum
+      {
+        ARG_N_TOKENS,
+        ARG_CHAREND,
+        ARG_ENCLOSE,
+        ARG_CMDEND
+      }
+    arg_type;
+    union
+      {
+        int n_tokens;
+        struct token charend;
+        struct token enclose[2];
+      };
+  };
+
+struct macro
+  {
+    char *name;
+
+    struct macro_param *params;
+    size_t n_params;
+
+    char **body;
+    size_t n_body;
+  };
+
+void macro_destroy (struct macro *);
+
+#endif /* macro.h */