--- /dev/null
+/* 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 "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/mrset.h"
+#include "language/command.h"
+#include "language/lexer/format-parser.h"
+#include "language/lexer/lexer.h"
+#include "language/lexer/variable-parser.h"
+#include "libpspp/assertion.h"
+#include "libpspp/hmap.h"
+#include "libpspp/message.h"
+#include "output/pivot-table.h"
+
+#include "gl/minmax.h"
+#include "gl/xalloc.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) (msgid)
+
+enum ctables_vlabel
+ {
+ CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
+ CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
+ CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
+ CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
+ CTVL_NONE,
+ };
+static void UNUSED
+ctables_vlabel_unique (enum ctables_vlabel vlabel)
+{
+ /* This ensures that all of the values are unique. */
+ switch (vlabel)
+ {
+ case CTVL_DEFAULT:
+ case CTVL_NAME:
+ case CTVL_LABEL:
+ case CTVL_BOTH:
+ case CTVL_NONE:
+ abort ();
+ }
+}
+
+/* XXX:
+ - unweighted summaries (U*)
+ - lower confidence limits (*.LCL)
+ - upper confidence limits (*.UCL)
+ - standard error (*.SE)
+ */
+#define SUMMARIES \
+ /* All variables. */ \
+ S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
+ S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
+ S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
+ S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
+ \
+ /* Scale variables, totals, and subtotals. */ \
+ S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
+ S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
+ S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
+ S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
+ S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
+ S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
+ S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
+ \
+ /* Multiple response sets. */ \
+ S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
+ S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+ S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
+
+enum ctables_summary_function
+ {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
+ SUMMARIES
+#undef S
+ };
+
+enum {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
+ N_CTSF_FUNCTIONS = SUMMARIES
+#undef S
+};
+
+struct ctables
+ {
+ struct pivot_table_look *look;
+
+ /* If this is NULL, zeros are displayed using the normal print format.
+ Otherwise, this string is displayed. */
+ char *zero;
+
+ /* If this is NULL, missing values are displayed using the normal print
+ format. Otherwise, this string is displayed. */
+ char *missing;
+
+ /* Indexed by variable dictionary index. */
+ enum ctables_vlabel *vlabels;
+
+ bool mrsets_count_duplicates; /* MRSETS. */
+ bool smissing_listwise; /* SMISSING. */
+ struct variable *base_weight; /* WEIGHT. */
+ int hide_threshold; /* HIDESMALLCOUNTS. */
+
+ struct ctables_table *tables;
+ size_t n_tables;
+ };
+
+struct ctables_postcompute
+ {
+ struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
+ const char *name; /* Name, without leading &. */
+
+ struct ctables_postcompute_expr *expr;
+ char *label;
+ /* XXX FORMAT */
+ bool hide_source_cats;
+ };
+
+struct ctables_postcompute_expr
+ {
+ enum ctables_postcompute_op
+ {
+ /* Terminals. */
+ CTPO_CAT_NUMBER,
+ CTPO_CAT_STRING,
+ CTPO_CAT_RANGE,
+ CTPO_CAT_MISSING,
+ /* XXX OTHERNM */
+ /* XXX SUBTOTAL and HSUBTOTAL */
+
+ /* Nonterminals. */
+ CTPO_ADD,
+ CTPO_SUB,
+ CTPO_MUL,
+ CTPO_DIV,
+ CTPO_POW,
+ }
+ op;
+
+ union
+ {
+ /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
+ double number;
+
+ /* CTPO_CAT_RANGE.
+
+ XXX what about string ranges? */
+ struct
+ {
+ double low; /* -DBL_MAX for LO. */
+ double high; /* DBL_MAX for HIGH. */
+ }
+ range;
+
+ /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
+ struct ctables_postcompute_expr *subs[2];
+ };
+ };
+
+enum ctables_label_position
+ {
+ CTLP_NORMAL,
+ CTLP_OPPOSITE,
+ CTLP_LAYER,
+ };
+
+struct ctables_table
+ {
+ struct ctables_axis *axes[PIVOT_N_AXES];
+
+ enum pivot_axis_type slabels_position;
+ bool slabels_visible;
+
+ enum ctables_label_position row_labels;
+ enum ctables_label_position col_labels;
+
+ /* Indexed by variable dictionary index. */
+ struct ctables_categories **categories;
+ size_t n_categories;
+
+ double cilevel;
+
+ char *caption;
+ char *corner;
+ char *title;
+
+ struct ctables_chisq *chisq;
+ struct ctables_pairwise *pairwise;
+ };
+
+struct ctables_var
+ {
+ bool is_mrset;
+ union
+ {
+ struct variable *var;
+ const struct mrset *mrset;
+ };
+ };
+
+static const struct fmt_spec *
+ctables_var_get_print_format (const struct ctables_var *var)
+{
+ return (var->is_mrset
+ ? var_get_print_format (var->mrset->vars[0])
+ : var_get_print_format (var->var));
+}
+
+struct ctables_categories
+ {
+ size_t n_refs;
+
+ /* Explicit categories. */
+ struct ctables_cat_value *values;
+ size_t n_values;
+
+ /* Implicit categories. */
+ bool sort_ascending;
+ bool include_missing;
+ enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
+ enum ctables_summary_function sort_func;
+ struct variable *sort_func_var;
+ double percentile;
+
+ /* Totals. */
+ bool show_totals;
+ bool totals_before;
+ char *total_label;
+
+ /* Empty categories. */
+ bool show_empty;
+ };
+
+struct ctables_cat_value
+ {
+ enum ctables_cat_value_type
+ {
+ CCVT_NUMBER,
+ CCVT_STRING,
+ CCVT_RANGE,
+ CCVT_MISSING,
+ CCVT_OTHERNM,
+ CCVT_SUBTOTAL,
+ CCVT_HSUBTOTAL,
+ }
+ type;
+
+ union
+ {
+ double number; /* CCVT_NUMBER. */
+ char *string; /* CCVT_STRING. */
+ double range[2]; /* CCVT_RANGE. */
+ char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
+ };
+ };
+
+static void
+ctables_cat_value_uninit (struct ctables_cat_value *cv)
+{
+ if (!cv)
+ return;
+
+ switch (cv->type)
+ {
+ case CCVT_NUMBER:
+ case CCVT_RANGE:
+ case CCVT_MISSING:
+ case CCVT_OTHERNM:
+ break;
+
+ case CCVT_STRING:
+ free (cv->string);
+ break;
+
+ case CCVT_SUBTOTAL:
+ case CCVT_HSUBTOTAL:
+ free (cv->subtotal_label);
+ }
+}
+
+static void
+ctables_categories_unref (struct ctables_categories *c)
+{
+ if (!c)
+ return;
+
+ assert (c->n_refs > 0);
+ if (--c->n_refs)
+ return;
+
+ for (size_t i = 0; i < c->n_values; i++)
+ ctables_cat_value_uninit (&c->values[i]);
+ free (c->values);
+ free (c->total_label);
+ free (c);
+}
+
+/* Chi-square test (SIGTEST). */
+struct ctables_chisq
+ {
+ double alpha;
+ bool include_mrsets;
+ bool all_visible;
+ };
+
+/* Pairwise comparison test (COMPARETEST). */
+struct ctables_pairwise
+ {
+ enum { PROP, MEAN } type;
+ double alpha[2];
+ bool include_mrsets;
+ bool meansvariance_allcats;
+ bool all_visible;
+ enum { BONFERRONI = 1, BH } adjust;
+ bool merge;
+ bool apa_style;
+ bool show_sig;
+ };
+
+struct ctables_axis
+ {
+ enum ctables_axis_op
+ {
+ /* Terminals. */
+ CTAO_VAR,
+
+ /* Nonterminals. */
+ CTAO_STACK, /* + */
+ CTAO_NEST, /* > */
+ }
+ op;
+
+ union
+ {
+ /* Terminals. */
+ struct
+ {
+ struct ctables_var var;
+ bool scale;
+ struct ctables_summary *summaries;
+ size_t n_summaries;
+ };
+
+ /* Nonterminals. */
+ struct ctables_axis *subs[2];
+ };
+ };
+
+static void ctables_axis_destroy (struct ctables_axis *);
+
+enum ctables_format
+ {
+ CTF_COUNT,
+ CTF_PERCENT,
+ CTF_GENERAL
+ };
+
+enum ctables_function_availability
+ {
+ CTFA_ALL, /* Any variables. */
+ CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
+ CTFA_MRSETS, /* Only multiple-response sets */
+ };
+
+struct ctables_summary
+ {
+ enum ctables_summary_function function;
+ double percentile; /* CTSF_PTILE only. */
+ char *label;
+ struct fmt_spec format; /* XXX extra CTABLES formats */
+ };
+
+static void
+ctables_summary_uninit (struct ctables_summary *s)
+{
+ if (s)
+ free (s->label);
+}
+
+static bool
+parse_col_width (struct lexer *lexer, const char *name, double *width)
+{
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "DEFAULT"))
+ *width = SYSMIS;
+ else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
+ {
+ *width = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else
+ return false;
+
+ return true;
+}
+
+static bool
+parse_bool (struct lexer *lexer, bool *b)
+{
+ if (lex_match_id (lexer, "NO"))
+ *b = false;
+ else if (lex_match_id (lexer, "YES"))
+ *b = true;
+ else
+ {
+ lex_error_expecting (lexer, "YES", "NO");
+ return false;
+ }
+ return true;
+}
+
+static enum ctables_function_availability
+ctables_function_availability (enum ctables_summary_function f)
+{
+ static enum ctables_function_availability availability[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
+ SUMMARIES
+#undef S
+ };
+
+ return availability[f];
+}
+
+static bool
+parse_ctables_summary_function (struct lexer *lexer,
+ enum ctables_summary_function *f)
+{
+ struct pair
+ {
+ enum ctables_summary_function function;
+ struct substring name;
+ };
+ static struct pair names[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
+ { ENUM, SS_LITERAL_INITIALIZER (NAME) },
+ SUMMARIES
+
+ /* The .COUNT suffix may be omitted. */
+ S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
+ S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
+ S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
+ S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
+ S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
+ S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
+ S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
+#undef S
+ };
+
+ if (!lex_force_id (lexer))
+ return false;
+
+ for (size_t i = 0; i < sizeof names / sizeof *names; i++)
+ if (ss_equals_case (names[i].name, lex_tokss (lexer)))
+ {
+ *f = names[i].function;
+ return true;
+ }
+
+ lex_error (lexer, _("Expecting summary function name."));
+ return false;
+}
+
+static void
+ctables_axis_destroy (struct ctables_axis *axis)
+{
+ if (!axis)
+ return;
+
+ switch (axis->op)
+ {
+ case CTAO_VAR:
+ for (size_t i = 0; i < axis->n_summaries; i++)
+ ctables_summary_uninit (&axis->summaries[i]);
+ free (axis->summaries);
+ break;
+
+ case CTAO_STACK:
+ case CTAO_NEST:
+ ctables_axis_destroy (axis->subs[0]);
+ ctables_axis_destroy (axis->subs[1]);
+ break;
+ }
+ free (axis);
+}
+
+static struct ctables_axis *
+ctables_axis_new_nonterminal (enum ctables_axis_op op,
+ struct ctables_axis *sub0,
+ struct ctables_axis *sub1)
+{
+ struct ctables_axis *axis = xmalloc (sizeof *axis);
+ *axis = (struct ctables_axis) { .op = op, .subs = { sub0, sub1 } };
+ return axis;
+}
+
+struct ctables_axis_parse_ctx
+ {
+ struct lexer *lexer;
+ struct dictionary *dict;
+ struct ctables *ct;
+ struct ctables_table *t;
+ };
+
+static struct ctables_summary *
+add_summary (struct ctables_axis *axis, enum ctables_summary_function function,
+ double percentile, size_t *allocated_summaries)
+{
+ if (axis->n_summaries >= *allocated_summaries)
+ axis->summaries = x2nrealloc (axis->summaries, allocated_summaries,
+ sizeof *axis->summaries);
+
+ static const char *default_labels[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
+ SUMMARIES
+#undef S
+ };
+ char *label = (function == CTSF_PTILE
+ ? xasprintf (_("Percentile %.2f"), percentile)
+ : xstrdup (gettext (default_labels[function])));
+
+ static const enum ctables_format default_formats[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
+ SUMMARIES
+#undef S
+ };
+ struct fmt_spec format;
+ switch (default_formats[function])
+ {
+ case CTF_COUNT:
+ format = (struct fmt_spec) { .type = FMT_F, .w = 40 };
+ break;
+
+ case CTF_PERCENT:
+ format = (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
+ break;
+
+ case CTF_GENERAL:
+ format = *ctables_var_get_print_format (&axis->var);
+ break;
+
+ default:
+ NOT_REACHED ();
+ }
+
+ struct ctables_summary *s = &axis->summaries[axis->n_summaries++];
+ *s = (struct ctables_summary) {
+ .function = function,
+ .percentile = percentile,
+ .label = label,
+ .format = format,
+ };
+ return s;
+}
+
+static struct ctables_axis *ctables_axis_parse_stack (
+ struct ctables_axis_parse_ctx *);
+
+static bool
+ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
+ struct ctables_var *var)
+{
+ if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
+ {
+ *var = (struct ctables_var) {
+ .is_mrset = true,
+ .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
+ };
+ if (!var->mrset)
+ {
+ lex_error (lexer, _("'%s' does not name a multiple-response set "
+ "in the active file dictionary."),
+ lex_tokcstr (lexer));
+ return false;
+ }
+ lex_get (lexer);
+ return true;
+ }
+ else
+ {
+ *var = (struct ctables_var) {
+ .is_mrset = false,
+ .var = parse_variable (lexer, dict),
+ };
+ return var->var != NULL;
+ }
+}
+
+static struct ctables_axis *
+ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
+{
+ if (lex_match (ctx->lexer, T_LPAREN))
+ {
+ struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
+ if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
+ {
+ ctables_axis_destroy (sub);
+ return NULL;
+ }
+ return sub;
+ }
+
+ if (!lex_force_id (ctx->lexer))
+ return NULL;
+
+ struct ctables_var var;
+ if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
+ return NULL;
+
+ struct ctables_axis *axis = xmalloc (sizeof *axis);
+ *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
+
+ /* XXX should figure out default measures by reading data */
+ axis->scale = (var.is_mrset ? false
+ : lex_match_phrase (ctx->lexer, "[S]") ? true
+ : lex_match_phrase (ctx->lexer, "[C]") ? false
+ : var_get_measure (var.var) == MEASURE_SCALE);
+
+ size_t allocated_summaries = 0;
+ if (lex_match (ctx->lexer, T_LBRACK))
+ {
+ do
+ {
+ enum ctables_summary_function function;
+ if (!parse_ctables_summary_function (ctx->lexer, &function))
+ goto error;
+
+ double percentile = 0;
+ if (function == CTSF_PTILE)
+ {
+ if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
+ goto error;
+ percentile = lex_number (ctx->lexer);
+ lex_get (ctx->lexer);
+ }
+
+ struct ctables_summary *s = add_summary (axis, function, percentile,
+ &allocated_summaries);
+ if (lex_is_string (ctx->lexer))
+ {
+ free (s->label);
+ s->label = ss_xstrdup (lex_tokss (ctx->lexer));
+ lex_get (ctx->lexer);
+ }
+ if (lex_token (ctx->lexer) == T_ID)
+ {
+ if (!parse_format_specifier (ctx->lexer, &s->format)
+ || !fmt_check_output (&s->format)
+ || !fmt_check_type_compat (&s->format, VAL_NUMERIC))
+ goto error;
+ }
+ lex_match (ctx->lexer, T_COMMA);
+ }
+ while (!lex_match (ctx->lexer, T_RBRACK));
+ }
+ else
+ add_summary (axis, axis->scale ? CTSF_MEAN : CTSF_COUNT, 0,
+ &allocated_summaries);
+ return axis;
+
+error:
+ ctables_axis_destroy (axis);
+ return NULL;
+}
+
+static struct ctables_axis *
+ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
+{
+ struct ctables_axis *lhs = ctables_axis_parse_primary (ctx);
+ if (!lhs)
+ return NULL;
+
+ while (lex_match (ctx->lexer, T_GT))
+ {
+ struct ctables_axis *rhs = ctables_axis_parse_primary (ctx);
+ if (!rhs)
+ return NULL;
+
+ lhs = ctables_axis_new_nonterminal (CTAO_NEST, lhs, rhs);
+ }
+
+ return lhs;
+}
+
+static struct ctables_axis *
+ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
+{
+ struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
+ if (!lhs)
+ return NULL;
+
+ while (lex_match (ctx->lexer, T_PLUS))
+ {
+ struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
+ if (!rhs)
+ return NULL;
+
+ lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs);
+ }
+
+ return lhs;
+}
+
+static bool
+ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
+ struct ctables *ct, struct ctables_table *t,
+ enum pivot_axis_type a)
+{
+ if (lex_token (lexer) == T_BY
+ || lex_token (lexer) == T_SLASH
+ || lex_token (lexer) == T_ENDCMD)
+ return true;
+
+ struct ctables_axis_parse_ctx ctx = {
+ .lexer = lexer,
+ .dict = dict,
+ .ct = ct,
+ .t = t
+ };
+ t->axes[a] = ctables_axis_parse_stack (&ctx);
+ return t->axes[a] != NULL;
+}
+
+static void
+ctables_chisq_destroy (struct ctables_chisq *chisq)
+{
+ free (chisq);
+}
+
+static void
+ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
+{
+ free (pairwise);
+}
+
+static void
+ctables_table_uninit (struct ctables_table *t)
+{
+ if (!t)
+ return;
+
+ for (size_t i = 0; i < t->n_categories; i++)
+ ctables_categories_unref (t->categories[i]);
+ free (t->categories);
+
+ ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
+ ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
+ ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
+ free (t->caption);
+ free (t->corner);
+ free (t->title);
+ ctables_chisq_destroy (t->chisq);
+ ctables_pairwise_destroy (t->pairwise);
+}
+
+static void
+ctables_destroy (struct ctables *ct)
+{
+ if (!ct)
+ return;
+
+ pivot_table_look_unref (ct->look);
+ free (ct->zero);
+ free (ct->missing);
+ free (ct->vlabels);
+ for (size_t i = 0; i < ct->n_tables; i++)
+ ctables_table_uninit (&ct->tables[i]);
+ free (ct->tables);
+ free (ct);
+}
+
+static struct ctables_cat_value
+ccvt_range (double low, double high)
+{
+ return (struct ctables_cat_value) {
+ .type = CCVT_RANGE,
+ .range = { low, high }
+ };
+}
+
+static bool
+ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
+ struct ctables_table *t)
+{
+ if (!lex_match_id (lexer, "VARIABLES"))
+ return false;
+ lex_match (lexer, T_EQUALS);
+
+ struct variable **vars;
+ size_t n_vars;
+ if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
+ return false;
+
+ struct ctables_categories *c = xmalloc (sizeof *c);
+ *c = (struct ctables_categories) { .n_refs = n_vars };
+ for (size_t i = 0; i < n_vars; i++)
+ {
+ struct ctables_categories **cp
+ = &t->categories[var_get_dict_index (vars[i])];
+ ctables_categories_unref (*cp);
+ *cp = c;
+ }
+ free (vars);
+
+ if (lex_match (lexer, T_LBRACK))
+ {
+ size_t allocated_values = 0;
+ do
+ {
+ if (c->n_values >= allocated_values)
+ c->values = x2nrealloc (c->values, &allocated_values,
+ sizeof *c->values);
+
+ struct ctables_cat_value *v = &c->values[c->n_values];
+ if (lex_match_id (lexer, "OTHERNM"))
+ v->type = CCVT_OTHERNM;
+ else if (lex_match_id (lexer, "MISSING"))
+ v->type = CCVT_MISSING;
+ else if (lex_match_id (lexer, "SUBTOTAL"))
+ *v = (struct ctables_cat_value)
+ { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
+ else if (lex_match_id (lexer, "HSUBTOTAL"))
+ *v = (struct ctables_cat_value)
+ { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
+ else if (lex_match_id (lexer, "LO"))
+ {
+ if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
+ return false;
+ *v = ccvt_range (-DBL_MAX, lex_number (lexer));
+ lex_get (lexer);
+ }
+ else if (lex_is_number (lexer))
+ {
+ double number = lex_number (lexer);
+ lex_get (lexer);
+ if (lex_match_id (lexer, "THRU"))
+ {
+ v->type = CCVT_RANGE;
+ v->range[0] = number;
+ if (lex_match_id (lexer, "HI"))
+ *v = ccvt_range (number, DBL_MAX);
+ else
+ {
+ if (!lex_force_num (lexer))
+ return false;
+ *v = ccvt_range (number, lex_number (lexer));
+ lex_get (lexer);
+ }
+ }
+ else
+ *v = (struct ctables_cat_value) {
+ .type = CCVT_NUMBER,
+ .number = number
+ };
+ }
+ else if (lex_is_string (lexer))
+ {
+ *v = (struct ctables_cat_value) {
+ .type = CCVT_STRING,
+ .string = ss_xstrdup (lex_tokss (lexer)),
+ };
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error (lexer, NULL);
+ return false;
+ }
+
+ if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
+ && lex_match (lexer, T_EQUALS))
+ {
+ if (!lex_force_string (lexer))
+ return false;
+
+ v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+
+ c->n_values++;
+ lex_match (lexer, T_COMMA);
+ }
+ while (!lex_match (lexer, T_RBRACK));
+ }
+
+ while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
+ {
+ if (!c->n_values && lex_match_id (lexer, "ORDER"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "A"))
+ c->sort_ascending = true;
+ else if (lex_match_id (lexer, "D"))
+ c->sort_ascending = false;
+ else
+ {
+ lex_error_expecting (lexer, "A", "D");
+ return false;
+ }
+ }
+ else if (!c->n_values && lex_match_id (lexer, "KEY"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "VALUE"))
+ c->key = CTCS_VALUE;
+ else if (lex_match_id (lexer, "LABEL"))
+ c->key = CTCS_LABEL;
+ else
+ {
+ c->key = CTCS_FUNCTION;
+ if (!parse_ctables_summary_function (lexer, &c->sort_func))
+ return false;
+
+ if (lex_match (lexer, T_LPAREN))
+ {
+ c->sort_func_var = parse_variable (lexer, dict);
+ if (!c->sort_func_var)
+ return false;
+
+ if (c->sort_func == CTSF_PTILE)
+ {
+ lex_match (lexer, T_COMMA);
+ if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
+ return false;
+ c->percentile = lex_number (lexer);
+ lex_get (lexer);
+ }
+
+ if (!lex_force_match (lexer, T_RPAREN))
+ return false;
+ }
+ else if (ctables_function_availability (c->sort_func)
+ == CTFA_SCALE)
+ {
+ bool UNUSED b = lex_force_match (lexer, T_LPAREN);
+ return false;
+ }
+ }
+ }
+ else if (!c->n_values && lex_match_id (lexer, "MISSING"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "INCLUDE"))
+ c->include_missing = true;
+ else if (lex_match_id (lexer, "EXCLUDE"))
+ c->include_missing = false;
+ else
+ {
+ lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
+ return false;
+ }
+ }
+ else if (lex_match_id (lexer, "TOTAL"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &c->show_totals))
+ return false;
+ }
+ else if (lex_match_id (lexer, "LABEL"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_string (lexer))
+ return false;
+ free (c->total_label);
+ c->total_label = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "POSITION"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "BEFORE"))
+ c->totals_before = true;
+ else if (lex_match_id (lexer, "AFTER"))
+ c->totals_before = false;
+ else
+ {
+ lex_error_expecting (lexer, "BEFORE", "AFTER");
+ return false;
+ }
+ }
+ else if (lex_match_id (lexer, "EMPTY"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "INCLUDE"))
+ c->show_empty = true;
+ else if (lex_match_id (lexer, "EXCLUDE"))
+ c->show_empty = false;
+ else
+ {
+ lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
+ return false;
+ }
+ }
+ else
+ {
+ if (!c->n_values)
+ lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
+ "TOTAL", "LABEL", "POSITION", "EMPTY");
+ else
+ lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
+ return false;
+ }
+ }
+ return true;
+}
+
+int
+cmd_ctables (struct lexer *lexer, struct dataset *ds)
+{
+ size_t n_vars = dict_get_n_vars (dataset_dict (ds));
+ enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
+ for (size_t i = 0; i < n_vars; i++)
+ vlabels[i] = CTVL_DEFAULT;
+
+ struct ctables *ct = xmalloc (sizeof *ct);
+ *ct = (struct ctables) {
+ .look = pivot_table_look_unshare (pivot_table_look_ref (
+ pivot_table_look_get_default ())),
+ .vlabels = vlabels,
+ .hide_threshold = 5,
+ };
+
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+
+ while (!lex_match_id (lexer, "TABLE"))
+ {
+ if (lex_match_id (lexer, "FORMAT"))
+ {
+ double widths[2] = { SYSMIS, SYSMIS };
+ double units_per_inch = 72.0;
+
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "MINCOLWIDTH"))
+ {
+ if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MAXCOLWIDTH"))
+ {
+ if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "UNITS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "POINTS"))
+ units_per_inch = 72.0;
+ else if (lex_match_id (lexer, "INCHES"))
+ units_per_inch = 1.0;
+ else if (lex_match_id (lexer, "CM"))
+ units_per_inch = 2.54;
+ else
+ {
+ lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "EMPTY"))
+ {
+ free (ct->zero);
+ ct->zero = NULL;
+
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ZERO"))
+ {
+ /* Nothing to do. */
+ }
+ else if (lex_match_id (lexer, "BLANK"))
+ ct->zero = xstrdup ("");
+ else if (lex_force_string (lexer))
+ {
+ ct->zero = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ else
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MISSING"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_string (lexer))
+ goto error;
+
+ free (ct->missing);
+ ct->missing = (strcmp (lex_tokcstr (lexer), ".")
+ ? ss_xstrdup (lex_tokss (lexer))
+ : NULL);
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
+ "UNITS", "EMPTY", "MISSING");
+ goto error;
+ }
+ }
+
+ if (widths[0] != SYSMIS && widths[1] != SYSMIS
+ && widths[0] > widths[1])
+ {
+ msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
+ goto error;
+ }
+
+ for (size_t i = 0; i < 2; i++)
+ if (widths[i] != SYSMIS)
+ {
+ int *wr = ct->look->width_ranges[TABLE_HORZ];
+ wr[i] = widths[i] / units_per_inch * 96.0;
+ if (wr[0] > wr[1])
+ wr[!i] = wr[i];
+ }
+ }
+ else if (lex_match_id (lexer, "VLABELS"))
+ {
+ if (!lex_force_match_id (lexer, "VARIABLES"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ struct variable **vars;
+ size_t n_vars;
+ if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
+ PV_NO_SCRATCH))
+ goto error;
+
+ if (!lex_force_match_id (lexer, "DISPLAY"))
+ {
+ free (vars);
+ goto error;
+ }
+ lex_match (lexer, T_EQUALS);
+
+ enum ctables_vlabel vlabel;
+ if (lex_match_id (lexer, "DEFAULT"))
+ vlabel = CTVL_DEFAULT;
+ else if (lex_match_id (lexer, "NAME"))
+ vlabel = CTVL_NAME;
+ else if (lex_match_id (lexer, "LABEL"))
+ vlabel = CTVL_LABEL;
+ else if (lex_match_id (lexer, "BOTH"))
+ vlabel = CTVL_BOTH;
+ else if (lex_match_id (lexer, "NONE"))
+ vlabel = CTVL_NONE;
+ else
+ {
+ lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
+ "BOTH", "NONE");
+ free (vars);
+ goto error;
+ }
+
+ for (size_t i = 0; i < n_vars; i++)
+ ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
+ free (vars);
+ }
+ else if (lex_match_id (lexer, "MRSETS"))
+ {
+ if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "SMISSING"))
+ {
+ if (lex_match_id (lexer, "VARIABLE"))
+ ct->smissing_listwise = false;
+ else if (lex_match_id (lexer, "LISTWISE"))
+ ct->smissing_listwise = true;
+ else
+ {
+ lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
+ goto error;
+ }
+ }
+ /* XXX PCOMPUTE */
+ else if (lex_match_id (lexer, "WEIGHT"))
+ {
+ if (!lex_force_match_id (lexer, "VARIABLE"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ ct->base_weight = parse_variable (lexer, dataset_dict (ds));
+ if (!ct->base_weight)
+ goto error;
+ }
+ else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
+ {
+ if (!lex_force_match_id (lexer, "COUNT"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
+ goto error;
+ ct->hide_threshold = lex_integer (lexer);
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
+ "SMISSING", "PCOMPUTE", "PPROPERTIES",
+ "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
+ goto error;
+ }
+
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+ }
+
+ size_t allocated_tables = 0;
+ do
+ {
+ if (ct->n_tables >= allocated_tables)
+ ct->tables = x2nrealloc (ct->tables, &allocated_tables,
+ sizeof *ct->tables);
+
+ struct ctables_table *t = &ct->tables[ct->n_tables++];
+ *t = (struct ctables_table) {
+ .slabels_position = PIVOT_AXIS_COLUMN,
+ .slabels_visible = true,
+ .row_labels = CTLP_NORMAL,
+ .col_labels = CTLP_NORMAL,
+ .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
+ sizeof *t->categories),
+ .n_categories = dict_get_n_vars (dataset_dict (ds)),
+ .cilevel = 95,
+ };
+
+ lex_match (lexer, T_EQUALS);
+ if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
+ goto error;
+
+ if (lex_match (lexer, T_BY))
+ {
+ if (!ctables_axis_parse (lexer, dataset_dict (ds),
+ ct, t, PIVOT_AXIS_COLUMN))
+ goto error;
+
+ if (lex_match (lexer, T_BY))
+ {
+ if (!ctables_axis_parse (lexer, dataset_dict (ds),
+ ct, t, PIVOT_AXIS_LAYER))
+ goto error;
+ }
+ }
+ if (lex_token (lexer) == T_ENDCMD)
+ break;
+ if (!lex_force_match (lexer, T_SLASH))
+ break;
+
+ /* XXX Validate axes. */
+ while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
+ {
+ if (lex_match_id (lexer, "SLABELS"))
+ {
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "POSITION"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "COLUMN"))
+ t->slabels_position = PIVOT_AXIS_COLUMN;
+ else if (lex_match_id (lexer, "ROW"))
+ t->slabels_position = PIVOT_AXIS_ROW;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->slabels_position = PIVOT_AXIS_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "COLUMN", "ROW",
+ "LAYER");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "VISIBLE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->slabels_visible))
+ goto error;
+ }
+ else
+ {
+ lex_error_expecting (lexer, "POSITION", "VISIBLE");
+ goto error;
+ }
+ }
+ }
+ else if (lex_match_id (lexer, "CLABELS"))
+ {
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "AUTO"))
+ t->row_labels = t->col_labels = CTLP_NORMAL;
+ else if (lex_match_id (lexer, "ROWLABELS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "OPPOSITE"))
+ t->row_labels = CTLP_OPPOSITE;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->row_labels = CTLP_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "COLLABELS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "OPPOSITE"))
+ t->col_labels = CTLP_OPPOSITE;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->col_labels = CTLP_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+ goto error;
+ }
+ }
+ else
+ {
+ lex_error_expecting (lexer, "AUTO", "ROWLABELS",
+ "COLLABELS");
+ goto error;
+ }
+ }
+ }
+ else if (lex_match_id (lexer, "CRITERIA"))
+ {
+ if (!lex_force_match_id (lexer, "CILEVEL"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
+ goto error;
+ t->cilevel = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "CATEGORIES"))
+ {
+ if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "TITLES"))
+ {
+ do
+ {
+ char **textp;
+ if (lex_match_id (lexer, "CAPTION"))
+ textp = &t->caption;
+ else if (lex_match_id (lexer, "CORNER"))
+ textp = &t->corner;
+ else if (lex_match_id (lexer, "TITLE"))
+ textp = &t->title;
+ else
+ {
+ lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
+ goto error;
+ }
+ lex_match (lexer, T_EQUALS);
+
+ struct string s = DS_EMPTY_INITIALIZER;
+ while (lex_is_string (lexer))
+ {
+ if (!ds_is_empty (&s))
+ ds_put_byte (&s, ' ');
+ ds_put_substring (&s, lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ free (*textp);
+ *textp = ds_steal_cstr (&s);
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else if (lex_match_id (lexer, "SIGTEST"))
+ {
+ if (!t->chisq)
+ {
+ t->chisq = xmalloc (sizeof *t->chisq);
+ *t->chisq = (struct ctables_chisq) {
+ .alpha = .05,
+ .include_mrsets = true,
+ .all_visible = true,
+ };
+ }
+
+ do
+ {
+ if (lex_match_id (lexer, "TYPE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_match_id (lexer, "CHISQUARE"))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "ALPHA"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
+ goto error;
+ t->chisq->alpha = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (parse_bool (lexer, &t->chisq->include_mrsets))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "CATEGORIES"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLVISIBLE"))
+ t->chisq->all_visible = true;
+ else if (lex_match_id (lexer, "SUBTOTALS"))
+ t->chisq->all_visible = false;
+ else
+ {
+ lex_error_expecting (lexer,
+ "ALLVISIBLE", "SUBTOTALS");
+ goto error;
+ }
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TYPE", "ALPHA",
+ "INCLUDEMRSETS", "CATEGORIES");
+ goto error;
+ }
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else if (lex_match_id (lexer, "COMPARETEST"))
+ {
+ if (!t->pairwise)
+ {
+ t->pairwise = xmalloc (sizeof *t->pairwise);
+ *t->pairwise = (struct ctables_pairwise) {
+ .type = PROP,
+ .alpha = { .05, .05 },
+ .adjust = BONFERRONI,
+ .include_mrsets = true,
+ .meansvariance_allcats = true,
+ .all_visible = true,
+ .merge = false,
+ .apa_style = true,
+ .show_sig = false,
+ };
+ }
+
+ do
+ {
+ if (lex_match_id (lexer, "TYPE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "PROP"))
+ t->pairwise->type = PROP;
+ else if (lex_match_id (lexer, "MEAN"))
+ t->pairwise->type = MEAN;
+ else
+ {
+ lex_error_expecting (lexer, "PROP", "MEAN");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "ALPHA"))
+ {
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+ goto error;
+ double a0 = lex_number (lexer);
+ lex_get (lexer);
+
+ lex_match (lexer, T_COMMA);
+ if (lex_is_number (lexer))
+ {
+ if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+ goto error;
+ double a1 = lex_number (lexer);
+ lex_get (lexer);
+
+ t->pairwise->alpha[0] = MIN (a0, a1);
+ t->pairwise->alpha[1] = MAX (a0, a1);
+ }
+ else
+ t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
+ }
+ else if (lex_match_id (lexer, "ADJUST"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "BONFERRONI"))
+ t->pairwise->adjust = BONFERRONI;
+ else if (lex_match_id (lexer, "BH"))
+ t->pairwise->adjust = BH;
+ else if (lex_match_id (lexer, "NONE"))
+ t->pairwise->adjust = 0;
+ else
+ {
+ lex_error_expecting (lexer, "BONFERRONI", "BH",
+ "NONE");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->include_mrsets))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MEANSVARIANCE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLCATS"))
+ t->pairwise->meansvariance_allcats = true;
+ else if (lex_match_id (lexer, "TESTEDCATS"))
+ t->pairwise->meansvariance_allcats = false;
+ else
+ {
+ lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "CATEGORIES"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLVISIBLE"))
+ t->pairwise->all_visible = true;
+ else if (lex_match_id (lexer, "SUBTOTALS"))
+ t->pairwise->all_visible = false;
+ else
+ {
+ lex_error_expecting (lexer, "ALLVISIBLE",
+ "SUBTOTALS");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "MERGE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->merge))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "STYLE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "APA"))
+ t->pairwise->apa_style = true;
+ else if (lex_match_id (lexer, "SIMPLE"))
+ t->pairwise->apa_style = false;
+ else
+ {
+ lex_error_expecting (lexer, "APA", "SIMPLE");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "SHOWSIG"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->show_sig))
+ goto error;
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
+ "INCLUDEMRSETS", "MEANSVARIANCE",
+ "CATEGORIES", "MERGE", "STYLE",
+ "SHOWSIG");
+ goto error;
+ }
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
+ "CRITERIA", "CATEGORIES", "TITLES",
+ "SIGTEST", "COMPARETEST");
+ goto error;
+ }
+ }
+ }
+ while (lex_token (lexer) != T_ENDCMD);
+ ctables_destroy (ct);
+ return CMD_SUCCESS;
+
+error:
+ ctables_destroy (ct);
+ return CMD_FAILURE;
+}
+