category filtering works
[pspp] / src / language / stats / ctables.c
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 #include <config.h>
18
19 #include <math.h>
20
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/mrset.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/message.h"
34 #include "libpspp/string-array.h"
35 #include "math/moments.h"
36 #include "output/pivot-table.h"
37
38 #include "gl/minmax.h"
39 #include "gl/xalloc.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) (msgid)
44
45 enum ctables_vlabel
46   {
47     CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
48     CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
49     CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
50     CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
51   };
52
53 /* XXX:
54    - unweighted summaries (U*)
55    - lower confidence limits (*.LCL)
56    - upper confidence limits (*.UCL)
57    - standard error (*.SE)
58  */
59 #define SUMMARIES                                                       \
60     /* All variables. */                                                \
61     S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL)            \
62     S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
63     S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
64     S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
65     S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
66     S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
67     S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
68     S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
69     S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
70     S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
71     S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
72     S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
73     S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
74     S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
75     S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
76     S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
77     S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
78     S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
79     S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
80     S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
81     S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
82     S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
83     S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
84                                                                         \
85     /* Scale variables, totals, and subtotals. */                       \
86     S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE)  \
87     S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE)           \
88     S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE)     \
89     S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE)  \
90     S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE)  \
91     S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE)           \
92     S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE)   \
93     S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE)        \
94     S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
95     S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
96     S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE)              \
97     S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE)      \
98     S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
99     S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE)      \
100     S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
101     S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
102     S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
103     S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
104     S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
105     S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
106     S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
107     S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
108     S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
109                                                                         \
110     /* Multiple response sets. */                                       \
111   S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
112     S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
113     S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114     S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115     S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116     S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117     S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118     S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119     S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
120     S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121     S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122     S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123     S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124     S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125     S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126     S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
127     S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128     S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129     S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130     S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131     S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
132     S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
133
134 enum ctables_summary_function
135   {
136 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
137     SUMMARIES
138 #undef S
139   };
140
141 enum {
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
143   N_CTSF_FUNCTIONS = SUMMARIES
144 #undef S
145 };
146
147 enum ctables_domain_type
148   {
149     /* Within a section, where stacked variables divide one section from
150        another. */
151     CTDT_TABLE,                  /* All layers of a whole section. */
152     CTDT_LAYER,                  /* One layer within a section. */
153     CTDT_LAYERROW,               /* Row in one layer within a section. */
154     CTDT_LAYERCOL,               /* Column in one layer within a section. */
155
156     /* Within a subtable, where a subtable pairs an innermost row variable with
157        an innermost column variable within a single layer.  */
158     CTDT_SUBTABLE,               /* Whole subtable. */
159     CTDT_ROW,                    /* Row within a subtable. */
160     CTDT_COL,                    /* Column within a subtable. */
161 #define N_CTDTS 7
162   };
163
164 struct ctables_domain
165   {
166     struct hmap_node node;
167
168     const struct ctables_freq *example;
169
170     double valid;
171     double missing;
172   };
173
174 struct ctables_freq
175   {
176     /* In struct ctables's 'ft' hmap.  Indexed by all the values in all the
177        axes (except the scalar variable, if any). */
178     struct hmap_node node;
179
180     /* The domains that contains this cell. */
181     struct ctables_domain *domains[N_CTDTS];
182
183     struct
184       {
185         size_t vaa_idx;
186         union value *values;
187         int leaf;
188       }
189     axes[PIVOT_N_AXES];
190
191     union ctables_summary *summaries;
192   };
193
194 struct ctables
195   {
196     struct pivot_table_look *look;
197
198     /* If this is NULL, zeros are displayed using the normal print format.
199        Otherwise, this string is displayed. */
200     char *zero;
201
202     /* If this is NULL, missing values are displayed using the normal print
203        format.  Otherwise, this string is displayed. */
204     char *missing;
205
206     /* Indexed by variable dictionary index. */
207     enum ctables_vlabel *vlabels;
208
209     bool mrsets_count_duplicates; /* MRSETS. */
210     bool smissing_listwise;       /* SMISSING. */
211     struct variable *base_weight; /* WEIGHT. */
212     int hide_threshold;           /* HIDESMALLCOUNTS. */
213
214     struct ctables_table **tables;
215     size_t n_tables;
216   };
217
218 struct ctables_postcompute
219   {
220     struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
221     const char *name;           /* Name, without leading &. */
222
223     struct ctables_postcompute_expr *expr;
224     char *label;
225     /* XXX FORMAT */
226     bool hide_source_cats;
227   };
228
229 struct ctables_postcompute_expr
230   {
231     enum ctables_postcompute_op
232       {
233         /* Terminals. */
234         CTPO_CAT_NUMBER,
235         CTPO_CAT_STRING,
236         CTPO_CAT_RANGE,
237         CTPO_CAT_MISSING,
238         /* XXX OTHERNM */
239         /* XXX SUBTOTAL and HSUBTOTAL */
240
241         /* Nonterminals. */
242         CTPO_ADD,
243         CTPO_SUB,
244         CTPO_MUL,
245         CTPO_DIV,
246         CTPO_POW,
247       }
248     op;
249
250     union
251       {
252         /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
253         double number;
254
255         /* CTPO_CAT_RANGE.
256
257            XXX what about string ranges? */
258         double range[2];
259
260         /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
261         struct ctables_postcompute_expr *subs[2];
262       };
263   };
264
265 enum ctables_label_position
266   {
267     CTLP_NORMAL,
268     CTLP_OPPOSITE,
269     CTLP_LAYER,
270   };
271
272 struct var_array
273   {
274     struct variable **vars;
275     size_t n;
276     size_t scale_idx;
277     size_t *domains[N_CTDTS];
278     size_t n_domains[N_CTDTS];
279
280     struct ctables_summary_spec *summaries;
281     size_t n_summaries;
282     struct variable *summary_var;
283   };
284
285 struct var_array2
286   {
287     struct var_array *vas;
288     size_t n;
289   };
290
291 struct ctables_table
292   {
293     struct ctables_axis *axes[PIVOT_N_AXES];
294     struct var_array2 vaas[PIVOT_N_AXES];
295     enum pivot_axis_type summary_axis;
296     struct hmap ft;
297     struct hmap domains[N_CTDTS];
298
299     enum pivot_axis_type slabels_position;
300     bool slabels_visible;
301
302     enum ctables_label_position row_labels;
303     enum ctables_label_position col_labels;
304
305     /* Indexed by variable dictionary index. */
306     struct ctables_categories **categories;
307     size_t n_categories;
308
309     double cilevel;
310
311     char *caption;
312     char *corner;
313     char *title;
314
315     struct ctables_chisq *chisq;
316     struct ctables_pairwise *pairwise;
317   };
318
319 struct ctables_var
320   {
321     bool is_mrset;
322     union
323       {
324         struct variable *var;
325         const struct mrset *mrset;
326       };
327   };
328
329 static const struct fmt_spec *
330 ctables_var_get_print_format (const struct ctables_var *var)
331 {
332   return (var->is_mrset
333           ? var_get_print_format (var->mrset->vars[0])
334           : var_get_print_format (var->var));
335 }
336
337 static const char *
338 ctables_var_name (const struct ctables_var *var)
339 {
340   return var->is_mrset ? var->mrset->name : var_get_name (var->var);
341 }
342
343 struct ctables_categories
344   {
345     size_t n_refs;
346
347     /* Explicit categories. */
348     struct ctables_cat_value *values;
349     size_t n_values;
350
351     /* Implicit categories. */
352     bool sort_ascending;
353     bool include_missing;
354     enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
355     enum ctables_summary_function sort_func;
356     struct variable *sort_func_var;
357     double percentile;
358
359     /* Totals. */
360     bool show_totals;
361     bool totals_before;
362     char *total_label;
363
364     /* Empty categories. */
365     bool show_empty;
366   };
367
368 struct ctables_cat_value
369   {
370     enum ctables_cat_value_type
371       {
372         CCVT_NUMBER,
373         CCVT_STRING,
374         CCVT_RANGE,
375         CCVT_MISSING,
376         CCVT_OTHERNM,
377         CCVT_SUBTOTAL,
378         CCVT_HSUBTOTAL,
379       }
380     type;
381
382     union
383       {
384         double number;          /* CCVT_NUMBER. */
385         char *string;           /* CCVT_STRING. */
386         double range[2];        /* CCVT_RANGE. */
387         char *subtotal_label;   /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
388       };
389   };
390
391 static void
392 ctables_cat_value_uninit (struct ctables_cat_value *cv)
393 {
394   if (!cv)
395     return;
396
397   switch (cv->type)
398     {
399     case CCVT_NUMBER:
400     case CCVT_RANGE:
401     case CCVT_MISSING:
402     case CCVT_OTHERNM:
403       break;
404
405     case CCVT_STRING:
406       free (cv->string);
407       break;
408
409     case CCVT_SUBTOTAL:
410     case CCVT_HSUBTOTAL:
411       free (cv->subtotal_label);
412     }
413 }
414
415 static void
416 ctables_categories_unref (struct ctables_categories *c)
417 {
418   if (!c)
419     return;
420
421   assert (c->n_refs > 0);
422   if (--c->n_refs)
423     return;
424
425   for (size_t i = 0; i < c->n_values; i++)
426     ctables_cat_value_uninit (&c->values[i]);
427   free (c->values);
428   free (c->total_label);
429   free (c);
430 }
431
432 /* Chi-square test (SIGTEST). */
433 struct ctables_chisq
434   {
435     double alpha;
436     bool include_mrsets;
437     bool all_visible;
438   };
439
440 /* Pairwise comparison test (COMPARETEST). */
441 struct ctables_pairwise
442   {
443     enum { PROP, MEAN } type;
444     double alpha[2];
445     bool include_mrsets;
446     bool meansvariance_allcats;
447     bool all_visible;
448     enum { BONFERRONI = 1, BH } adjust;
449     bool merge;
450     bool apa_style;
451     bool show_sig;
452   };
453
454 struct ctables_axis
455   {
456     enum ctables_axis_op
457       {
458         /* Terminals. */
459         CTAO_VAR,
460
461         /* Nonterminals. */
462         CTAO_STACK,             /* + */
463         CTAO_NEST,              /* > */
464       }
465     op;
466
467     union
468       {
469         /* Terminals. */
470         struct
471           {
472             struct ctables_var var;
473             bool scale;
474             struct ctables_summary_spec *summaries;
475             size_t n_summaries;
476             size_t allocated_summaries;
477           };
478
479         /* Nonterminals. */
480         struct ctables_axis *subs[2];
481       };
482
483     struct msg_location *loc;
484   };
485
486 static void ctables_axis_destroy (struct ctables_axis *);
487
488 enum ctables_format
489   {
490     CTF_COUNT,
491     CTF_PERCENT,
492     CTF_GENERAL
493   };
494
495 enum ctables_function_availability
496   {
497     CTFA_ALL,                /* Any variables. */
498     CTFA_SCALE,              /* Only scale variables, totals, and subtotals. */
499     CTFA_MRSETS,             /* Only multiple-response sets */
500   };
501
502 struct ctables_summary_spec
503   {
504     enum ctables_summary_function function;
505     double percentile;          /* CTSF_PTILE only. */
506     char *label;
507     struct fmt_spec format;     /* XXX extra CTABLES formats */
508   };
509
510 static void
511 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
512 {
513   if (s)
514     free (s->label);
515 }
516
517 static bool
518 parse_col_width (struct lexer *lexer, const char *name, double *width)
519 {
520   lex_match (lexer, T_EQUALS);
521   if (lex_match_id (lexer, "DEFAULT"))
522     *width = SYSMIS;
523   else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
524     {
525       *width = lex_number (lexer);
526       lex_get (lexer);
527     }
528   else
529     return false;
530
531   return true;
532 }
533
534 static bool
535 parse_bool (struct lexer *lexer, bool *b)
536 {
537   if (lex_match_id (lexer, "NO"))
538     *b = false;
539   else if (lex_match_id (lexer, "YES"))
540     *b = true;
541   else
542     {
543       lex_error_expecting (lexer, "YES", "NO");
544       return false;
545     }
546   return true;
547 }
548
549 static enum ctables_function_availability
550 ctables_function_availability (enum ctables_summary_function f)
551 {
552   static enum ctables_function_availability availability[] = {
553 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
554     SUMMARIES
555 #undef S
556   };
557
558   return availability[f];
559 }
560
561 static bool
562 parse_ctables_summary_function (struct lexer *lexer,
563                                 enum ctables_summary_function *f)
564 {
565   struct pair
566     {
567       enum ctables_summary_function function;
568       struct substring name;
569     };
570   static struct pair names[] = {
571 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
572     { ENUM, SS_LITERAL_INITIALIZER (NAME) },
573     SUMMARIES
574
575     /* The .COUNT suffix may be omitted. */
576     S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
577     S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
578     S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
579     S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
580     S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
581     S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
582     S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
583 #undef S
584   };
585
586   if (!lex_force_id (lexer))
587     return false;
588
589   for (size_t i = 0; i < sizeof names / sizeof *names; i++)
590     if (ss_equals_case (names[i].name, lex_tokss (lexer)))
591       {
592         *f = names[i].function;
593         lex_get (lexer);
594         return true;
595       }
596
597   lex_error (lexer, _("Expecting summary function name."));
598   return false;
599 }
600
601 static void
602 ctables_axis_destroy (struct ctables_axis *axis)
603 {
604   if (!axis)
605     return;
606
607   switch (axis->op)
608     {
609     case CTAO_VAR:
610       for (size_t i = 0; i < axis->n_summaries; i++)
611         ctables_summary_spec_uninit (&axis->summaries[i]);
612       free (axis->summaries);
613       break;
614
615     case CTAO_STACK:
616     case CTAO_NEST:
617       ctables_axis_destroy (axis->subs[0]);
618       ctables_axis_destroy (axis->subs[1]);
619       break;
620     }
621   msg_location_destroy (axis->loc);
622   free (axis);
623 }
624
625 static struct ctables_axis *
626 ctables_axis_new_nonterminal (enum ctables_axis_op op,
627                               struct ctables_axis *sub0,
628                               struct ctables_axis *sub1,
629                               struct lexer *lexer, int start_ofs)
630 {
631   struct ctables_axis *axis = xmalloc (sizeof *axis);
632   *axis = (struct ctables_axis) {
633     .op = op,
634     .subs = { sub0, sub1 },
635     .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
636   };
637   return axis;
638 }
639
640 struct ctables_axis_parse_ctx
641   {
642     struct lexer *lexer;
643     struct dictionary *dict;
644     struct ctables *ct;
645     struct ctables_table *t;
646   };
647
648 static struct fmt_spec
649 ctables_summary_default_format (enum ctables_summary_function function,
650                                 const struct ctables_var *var)
651 {
652   static const enum ctables_format default_formats[] = {
653 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
654     SUMMARIES
655 #undef S
656   };
657   switch (default_formats[function])
658     {
659     case CTF_COUNT:
660       return (struct fmt_spec) { .type = FMT_F, .w = 40 };
661
662     case CTF_PERCENT:
663       return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
664
665     case CTF_GENERAL:
666       return *ctables_var_get_print_format (var);
667
668     default:
669       NOT_REACHED ();
670     }
671 }
672
673 static char *
674 ctables_summary_default_label (enum ctables_summary_function function,
675                                double percentile)
676 {
677   static const char *default_labels[] = {
678 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
679     SUMMARIES
680 #undef S
681   };
682
683   return (function == CTSF_PTILE
684           ? xasprintf (_("Percentile %.2f"), percentile)
685           : xstrdup (gettext (default_labels[function])));
686 }
687
688 static const char *
689 ctables_summary_function_name (enum ctables_summary_function function)
690 {
691   static const char *names[] = {
692 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
693     SUMMARIES
694 #undef S
695   };
696   return names[function];
697 }
698
699 static bool
700 add_summary_spec (struct ctables_axis *axis,
701                   enum ctables_summary_function function, double percentile,
702                   const char *label, const struct fmt_spec *format,
703                   const struct msg_location *loc)
704 {
705   if (axis->op == CTAO_VAR)
706     {
707       if (axis->n_summaries >= axis->allocated_summaries)
708         axis->summaries = x2nrealloc (axis->summaries,
709                                       &axis->allocated_summaries,
710                                       sizeof *axis->summaries);
711
712       const char *function_name = ctables_summary_function_name (function);
713       const char *var_name = ctables_var_name (&axis->var);
714       switch (ctables_function_availability (function))
715         {
716         case CTFA_MRSETS:
717           if (!axis->var.is_mrset)
718             {
719               msg_at (SE, loc, _("Summary function %s applies only to multiple "
720                                  "response sets."), function_name);
721               msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
722                       var_name);
723               return false;
724             }
725           break;
726
727         case CTFA_SCALE:
728           if (!axis->scale)
729             {
730               msg_at (SE, loc,
731                       _("Summary function %s applies only to scale variables."),
732                       function_name);
733               msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
734                       var_name);
735               return false;
736             }
737           break;
738
739         case CTFA_ALL:
740           break;
741         }
742
743       struct ctables_summary_spec *dst = &axis->summaries[axis->n_summaries++];
744       *dst = (struct ctables_summary_spec) {
745         .function = function,
746         .percentile = percentile,
747         .label = xstrdup (label),
748         .format = (format ? *format
749                    : ctables_summary_default_format (function, &axis->var)),
750       };
751       return true;
752     }
753   else
754     {
755       for (size_t i = 0; i < 2; i++)
756         if (!add_summary_spec (axis->subs[i], function, percentile, label,
757                                format, loc))
758           return false;
759       return true;
760     }
761 }
762
763 static struct ctables_axis *ctables_axis_parse_stack (
764   struct ctables_axis_parse_ctx *);
765
766 static bool
767 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
768                    struct ctables_var *var)
769 {
770   if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
771     {
772       *var = (struct ctables_var) {
773         .is_mrset = true,
774         .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
775       };
776       if (!var->mrset)
777         {
778           lex_error (lexer, _("'%s' does not name a multiple-response set "
779                               "in the active file dictionary."),
780                      lex_tokcstr (lexer));
781           return false;
782         }
783       lex_get (lexer);
784       return true;
785     }
786   else
787     {
788       *var = (struct ctables_var) {
789         .is_mrset = false,
790         .var = parse_variable (lexer, dict),
791       };
792       return var->var != NULL;
793     }
794 }
795
796 static struct ctables_axis *
797 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
798 {
799   if (lex_match (ctx->lexer, T_LPAREN))
800     {
801       struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
802       if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
803         {
804           ctables_axis_destroy (sub);
805           return NULL;
806         }
807       return sub;
808     }
809
810   if (!lex_force_id (ctx->lexer))
811     return NULL;
812
813   int start_ofs = lex_ofs (ctx->lexer);
814   struct ctables_var var;
815   if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
816     return NULL;
817
818   struct ctables_axis *axis = xmalloc (sizeof *axis);
819   *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
820
821   /* XXX should figure out default measures by reading data */
822   axis->scale = (var.is_mrset ? false
823                  : lex_match_phrase (ctx->lexer, "[S]") ? true
824                  : lex_match_phrase (ctx->lexer, "[C]") ? false
825                  : var_get_measure (var.var) == MEASURE_SCALE);
826   axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
827                                 lex_ofs (ctx->lexer) - 1);
828   return axis;
829 }
830
831 static struct ctables_axis *
832 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
833 {
834   struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
835   if (!sub || !lex_match (ctx->lexer, T_LBRACK))
836     return sub;
837
838   do
839     {
840       int start_ofs = lex_ofs (ctx->lexer);
841
842       /* Parse function. */
843       enum ctables_summary_function function;
844       if (!parse_ctables_summary_function (ctx->lexer, &function))
845         goto error;
846
847       /* Parse percentile. */
848       double percentile = 0;
849       if (function == CTSF_PTILE)
850         {
851           if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
852             goto error;
853           percentile = lex_number (ctx->lexer);
854           lex_get (ctx->lexer);
855         }
856
857       /* Parse label. */
858       char *label;
859       if (lex_is_string (ctx->lexer))
860         {
861           label = ss_xstrdup (lex_tokss (ctx->lexer));
862           lex_get (ctx->lexer);
863         }
864       else
865         label = ctables_summary_default_label (function, percentile);
866
867       /* Parse format. */
868       struct fmt_spec format;
869       const struct fmt_spec *formatp;
870       if (lex_token (ctx->lexer) == T_ID)
871         {
872           if (!parse_format_specifier (ctx->lexer, &format)
873               || !fmt_check_output (&format)
874               || !fmt_check_type_compat (&format, VAL_NUMERIC))
875             {
876               free (label);
877               goto error;
878             }
879           formatp = &format;
880         }
881       else
882         formatp = NULL;
883
884       struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
885                                                    lex_ofs (ctx->lexer) - 1);
886       add_summary_spec (sub, function, percentile, label, formatp, loc);
887       free (label);
888       msg_location_destroy (loc);
889
890       lex_match (ctx->lexer, T_COMMA);
891     }
892   while (!lex_match (ctx->lexer, T_RBRACK));
893
894   return sub;
895
896 error:
897   ctables_axis_destroy (sub);
898   return NULL;
899 }
900
901 static const struct ctables_axis *
902 find_scale (const struct ctables_axis *axis)
903 {
904   if (!axis)
905     return NULL;
906   else if (axis->op == CTAO_VAR)
907     {
908       if (axis->scale)
909         {
910           assert (!axis->var.is_mrset);
911           return axis;
912         }
913       else
914         return NULL;
915     }
916   else
917     {
918       for (size_t i = 0; i < 2; i++)
919         {
920           const struct ctables_axis *scale = find_scale (axis->subs[i]);
921           if (scale)
922             return scale;
923         }
924       return NULL;
925     }
926 }
927
928 static const struct ctables_axis *
929 find_categorical_summary_spec (const struct ctables_axis *axis)
930 {
931   if (!axis)
932     return NULL;
933   else if (axis->op == CTAO_VAR)
934     return !axis->scale && axis->n_summaries ? axis : NULL;
935   else
936     {
937       for (size_t i = 0; i < 2; i++)
938         {
939           const struct ctables_axis *sum
940             = find_categorical_summary_spec (axis->subs[i]);
941           if (sum)
942             return sum;
943         }
944       return NULL;
945     }
946 }
947
948 static struct ctables_axis *
949 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
950 {
951   int start_ofs = lex_ofs (ctx->lexer);
952   struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
953   if (!lhs)
954     return NULL;
955
956   while (lex_match (ctx->lexer, T_GT))
957     {
958       struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
959       if (!rhs)
960         return NULL;
961
962       struct ctables_axis *nest = ctables_axis_new_nonterminal (
963         CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
964
965       const struct ctables_axis *outer_scale = find_scale (lhs);
966       const struct ctables_axis *inner_scale = find_scale (rhs);
967       if (outer_scale && inner_scale)
968         {
969           msg_at (SE, nest->loc, _("Cannot nest scale variables."));
970           msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
971           msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
972           ctables_axis_destroy (nest);
973           return NULL;
974         }
975
976       const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
977       if (outer_sum)
978         {
979           msg_at (SE, nest->loc,
980                   _("Summaries may only be requested for categorical variables "
981                     "at the innermost nesting level."));
982           msg_at (SN, outer_sum->loc,
983                   _("This outer categorical variable has a summary."));
984           ctables_axis_destroy (nest);
985           return NULL;
986         }
987
988       lhs = nest;
989     }
990
991   return lhs;
992 }
993
994 static struct ctables_axis *
995 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
996 {
997   int start_ofs = lex_ofs (ctx->lexer);
998   struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
999   if (!lhs)
1000     return NULL;
1001
1002   while (lex_match (ctx->lexer, T_PLUS))
1003     {
1004       struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1005       if (!rhs)
1006         return NULL;
1007
1008       lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1009                                           ctx->lexer, start_ofs);
1010     }
1011
1012   return lhs;
1013 }
1014
1015 static bool
1016 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1017                     struct ctables *ct, struct ctables_table *t,
1018                     enum pivot_axis_type a)
1019 {
1020   if (lex_token (lexer) == T_BY
1021       || lex_token (lexer) == T_SLASH
1022       || lex_token (lexer) == T_ENDCMD)
1023     return true;
1024
1025   struct ctables_axis_parse_ctx ctx = {
1026     .lexer = lexer,
1027     .dict = dict,
1028     .ct = ct,
1029     .t = t
1030   };
1031   t->axes[a] = ctables_axis_parse_stack (&ctx);
1032   return t->axes[a] != NULL;
1033 }
1034
1035 static void
1036 ctables_chisq_destroy (struct ctables_chisq *chisq)
1037 {
1038   free (chisq);
1039 }
1040
1041 static void
1042 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1043 {
1044   free (pairwise);
1045 }
1046
1047 static void
1048 ctables_table_destroy (struct ctables_table *t)
1049 {
1050   if (!t)
1051     return;
1052
1053   for (size_t i = 0; i < t->n_categories; i++)
1054     ctables_categories_unref (t->categories[i]);
1055   free (t->categories);
1056
1057   ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1058   ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1059   ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1060   free (t->caption);
1061   free (t->corner);
1062   free (t->title);
1063   ctables_chisq_destroy (t->chisq);
1064   ctables_pairwise_destroy (t->pairwise);
1065   free (t);
1066 }
1067
1068 static void
1069 ctables_destroy (struct ctables *ct)
1070 {
1071   if (!ct)
1072     return;
1073
1074   pivot_table_look_unref (ct->look);
1075   free (ct->zero);
1076   free (ct->missing);
1077   free (ct->vlabels);
1078   for (size_t i = 0; i < ct->n_tables; i++)
1079     ctables_table_destroy (ct->tables[i]);
1080   free (ct->tables);
1081   free (ct);
1082 }
1083
1084 static struct ctables_cat_value
1085 ccvt_range (double low, double high)
1086 {
1087   return (struct ctables_cat_value) {
1088     .type = CCVT_RANGE,
1089     .range = { low, high }
1090   };
1091 }
1092
1093 static bool
1094 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1095                                 struct ctables_table *t)
1096 {
1097   if (!lex_match_id (lexer, "VARIABLES"))
1098     return false;
1099   lex_match (lexer, T_EQUALS);
1100
1101   struct variable **vars;
1102   size_t n_vars;
1103   if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1104     return false;
1105
1106   struct ctables_categories *c = xmalloc (sizeof *c);
1107   *c = (struct ctables_categories) { .n_refs = n_vars };
1108   for (size_t i = 0; i < n_vars; i++)
1109     {
1110       struct ctables_categories **cp
1111         = &t->categories[var_get_dict_index (vars[i])];
1112       ctables_categories_unref (*cp);
1113       *cp = c;
1114     }
1115   free (vars);
1116
1117   if (lex_match (lexer, T_LBRACK))
1118     {
1119       size_t allocated_values = 0;
1120       do
1121         {
1122           if (c->n_values >= allocated_values)
1123             c->values = x2nrealloc (c->values, &allocated_values,
1124                                     sizeof *c->values);
1125
1126           struct ctables_cat_value *v = &c->values[c->n_values];
1127           if (lex_match_id (lexer, "OTHERNM"))
1128             v->type = CCVT_OTHERNM;
1129           else if (lex_match_id (lexer, "MISSING"))
1130             v->type = CCVT_MISSING;
1131           else if (lex_match_id (lexer, "SUBTOTAL"))
1132             *v = (struct ctables_cat_value)
1133               { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1134           else if (lex_match_id (lexer, "HSUBTOTAL"))
1135             *v = (struct ctables_cat_value)
1136               { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1137           else if (lex_match_id (lexer, "LO"))
1138             {
1139               if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1140                 return false;
1141               *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1142               lex_get (lexer);
1143             }
1144           else if (lex_is_number (lexer))
1145             {
1146               double number = lex_number (lexer);
1147               lex_get (lexer);
1148               if (lex_match_id (lexer, "THRU"))
1149                 {
1150                   v->type = CCVT_RANGE;
1151                   v->range[0] = number;
1152                   if (lex_match_id (lexer, "HI"))
1153                     *v = ccvt_range (number, DBL_MAX);
1154                   else
1155                     {
1156                       if (!lex_force_num (lexer))
1157                         return false;
1158                       *v = ccvt_range (number, lex_number (lexer));
1159                       lex_get (lexer);
1160                     }
1161                 }
1162               else
1163                 *v = (struct ctables_cat_value) {
1164                   .type = CCVT_NUMBER,
1165                   .number = number
1166                 };
1167             }
1168           else if (lex_is_string (lexer))
1169             {
1170               *v = (struct ctables_cat_value) {
1171                 .type = CCVT_STRING,
1172                 .string = ss_xstrdup (lex_tokss (lexer)),
1173               };
1174               lex_get (lexer);
1175             }
1176           else
1177             {
1178               lex_error (lexer, NULL);
1179               return false;
1180             }
1181
1182           if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1183               && lex_match (lexer, T_EQUALS))
1184             {
1185               if (!lex_force_string (lexer))
1186                 return false;
1187
1188               v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1189               lex_get (lexer);
1190             }
1191
1192           c->n_values++;
1193           lex_match (lexer, T_COMMA);
1194         }
1195       while (!lex_match (lexer, T_RBRACK));
1196     }
1197
1198   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1199     {
1200       if (!c->n_values && lex_match_id (lexer, "ORDER"))
1201         {
1202           lex_match (lexer, T_EQUALS);
1203           if (lex_match_id (lexer, "A"))
1204             c->sort_ascending = true;
1205           else if (lex_match_id (lexer, "D"))
1206             c->sort_ascending = false;
1207           else
1208             {
1209               lex_error_expecting (lexer, "A", "D");
1210               return false;
1211             }
1212         }
1213       else if (!c->n_values && lex_match_id (lexer, "KEY"))
1214         {
1215           lex_match (lexer, T_EQUALS);
1216           if (lex_match_id (lexer, "VALUE"))
1217             c->key = CTCS_VALUE;
1218           else if (lex_match_id (lexer, "LABEL"))
1219             c->key = CTCS_LABEL;
1220           else
1221             {
1222               c->key = CTCS_FUNCTION;
1223               if (!parse_ctables_summary_function (lexer, &c->sort_func))
1224                 return false;
1225
1226               if (lex_match (lexer, T_LPAREN))
1227                 {
1228                   c->sort_func_var = parse_variable (lexer, dict);
1229                   if (!c->sort_func_var)
1230                     return false;
1231
1232                   if (c->sort_func == CTSF_PTILE)
1233                     {
1234                       lex_match (lexer, T_COMMA);
1235                       if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1236                         return false;
1237                       c->percentile = lex_number (lexer);
1238                       lex_get (lexer);
1239                     }
1240
1241                   if (!lex_force_match (lexer, T_RPAREN))
1242                     return false;
1243                 }
1244               else if (ctables_function_availability (c->sort_func)
1245                        == CTFA_SCALE)
1246                 {
1247                   bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1248                   return false;
1249                 }
1250             }
1251         }
1252       else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1253         {
1254           lex_match (lexer, T_EQUALS);
1255           if (lex_match_id (lexer, "INCLUDE"))
1256             c->include_missing = true;
1257           else if (lex_match_id (lexer, "EXCLUDE"))
1258             c->include_missing = false;
1259           else
1260             {
1261               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1262               return false;
1263             }
1264         }
1265       else if (lex_match_id (lexer, "TOTAL"))
1266         {
1267           lex_match (lexer, T_EQUALS);
1268           if (!parse_bool (lexer, &c->show_totals))
1269             return false;
1270         }
1271       else if (lex_match_id (lexer, "LABEL"))
1272         {
1273           lex_match (lexer, T_EQUALS);
1274           if (!lex_force_string (lexer))
1275             return false;
1276           free (c->total_label);
1277           c->total_label = ss_xstrdup (lex_tokss (lexer));
1278           lex_get (lexer);
1279         }
1280       else if (lex_match_id (lexer, "POSITION"))
1281         {
1282           lex_match (lexer, T_EQUALS);
1283           if (lex_match_id (lexer, "BEFORE"))
1284             c->totals_before = true;
1285           else if (lex_match_id (lexer, "AFTER"))
1286             c->totals_before = false;
1287           else
1288             {
1289               lex_error_expecting (lexer, "BEFORE", "AFTER");
1290               return false;
1291             }
1292         }
1293       else if (lex_match_id (lexer, "EMPTY"))
1294         {
1295           lex_match (lexer, T_EQUALS);
1296           if (lex_match_id (lexer, "INCLUDE"))
1297             c->show_empty = true;
1298           else if (lex_match_id (lexer, "EXCLUDE"))
1299             c->show_empty = false;
1300           else
1301             {
1302               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1303               return false;
1304             }
1305         }
1306       else
1307         {
1308           if (!c->n_values)
1309             lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1310                                  "TOTAL", "LABEL", "POSITION", "EMPTY");
1311           else
1312             lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1313           return false;
1314         }
1315     }
1316   return true;
1317 }
1318
1319 static void
1320 var_array_uninit (struct var_array *va)
1321 {
1322   if (va)
1323     free (va->vars);
1324 }
1325
1326 static void
1327 var_array2_uninit (struct var_array2 *vaa)
1328 {
1329   if (vaa)
1330     {
1331       for (size_t i = 0; i < vaa->n; i++)
1332         var_array_uninit (&vaa->vas[i]);
1333       free (vaa->vas);
1334     }
1335 }
1336
1337 static struct var_array2
1338 nest_fts (struct var_array2 va0, struct var_array2 va1)
1339 {
1340   if (!va0.n)
1341     return va1;
1342   else if (!va1.n)
1343     return va0;
1344
1345   struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
1346   for (size_t i = 0; i < va0.n; i++)
1347     for (size_t j = 0; j < va1.n; j++)
1348       {
1349         const struct var_array *a = &va0.vas[i];
1350         const struct var_array *b = &va1.vas[j];
1351
1352         size_t allocate = a->n + b->n;
1353         struct variable **vars = xnmalloc (allocate, sizeof *vars);
1354         enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1355         size_t n = 0;
1356         for (size_t k = 0; k < a->n; k++)
1357           vars[n++] = a->vars[k];
1358         for (size_t k = 0; k < b->n; k++)
1359           vars[n++] = b->vars[k];
1360         assert (n == allocate);
1361
1362         const struct var_array *summary_src;
1363         if (!a->summary_var)
1364           summary_src = b;
1365         else if (!b->summary_var)
1366           summary_src = a;
1367         else
1368           NOT_REACHED ();
1369         vaa.vas[vaa.n++] = (struct var_array) {
1370           .vars = vars,
1371           .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1372                         : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1373                         : SIZE_MAX),
1374           .n = n,
1375           .summaries = summary_src->summaries,
1376           .n_summaries = summary_src->n_summaries,
1377           .summary_var = summary_src->summary_var,
1378         };
1379       }
1380   var_array2_uninit (&va0);
1381   var_array2_uninit (&va1);
1382   return vaa;
1383 }
1384
1385 static struct var_array2
1386 stack_fts (struct var_array2 va0, struct var_array2 va1)
1387 {
1388   struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
1389   for (size_t i = 0; i < va0.n; i++)
1390     vaa.vas[vaa.n++] = va0.vas[i];
1391   for (size_t i = 0; i < va1.n; i++)
1392     vaa.vas[vaa.n++] = va1.vas[i];
1393   assert (vaa.n == va0.n + va1.n);
1394   free (va0.vas);
1395   free (va1.vas);
1396   return vaa;
1397 }
1398
1399 static struct var_array2
1400 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1401 {
1402   if (!a)
1403     return (struct var_array2) { .n = 0 };
1404
1405   switch (a->op)
1406     {
1407     case CTAO_VAR:
1408       assert (!a->var.is_mrset);
1409
1410       struct variable **vars = xmalloc (sizeof *vars);
1411       *vars = a->var.var;
1412
1413       struct var_array *va = xmalloc (sizeof *va);
1414       *va = (struct var_array) {
1415         .vars = vars,
1416         .n = 1,
1417         .scale_idx = a->scale ? 0 : SIZE_MAX,
1418       };
1419       if (a->n_summaries || a->scale)
1420         {
1421           va->summaries = a->summaries;
1422           va->n_summaries = a->n_summaries;
1423           va->summary_var = a->var.var;
1424         }
1425       return (struct var_array2) { .vas = va, .n = 1 };
1426
1427     case CTAO_STACK:
1428       return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1429                         enumerate_fts (axis_type, a->subs[1]));
1430
1431     case CTAO_NEST:
1432       return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1433                        enumerate_fts (axis_type, a->subs[1]));
1434     }
1435
1436   NOT_REACHED ();
1437 }
1438
1439 union ctables_summary
1440   {
1441     /* COUNT, VALIDN, TOTALN. */
1442     struct
1443       {
1444         double valid;
1445         double missing;
1446       };
1447
1448     /* MINIMUM, MAXIMUM, RANGE. */
1449     struct
1450       {
1451         double min;
1452         double max;
1453       };
1454
1455     /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1456     struct moments1 *moments;
1457
1458     /* XXX percentiles, median, mode, multiple response */
1459   };
1460
1461 static void
1462 ctables_summary_init (union ctables_summary *s,
1463                       const struct ctables_summary_spec *ss)
1464 {
1465   switch (ss->function)
1466     {
1467     case CTSF_COUNT:
1468     case CTSF_ECOUNT:
1469     case CTSF_ROWPCT_COUNT:
1470     case CTSF_COLPCT_COUNT:
1471     case CTSF_TABLEPCT_COUNT:
1472     case CTSF_SUBTABLEPCT_COUNT:
1473     case CTSF_LAYERPCT_COUNT:
1474     case CTSF_LAYERROWPCT_COUNT:
1475     case CTSF_LAYERCOLPCT_COUNT:
1476     case CTSF_ROWPCT_VALIDN:
1477     case CTSF_COLPCT_VALIDN:
1478     case CTSF_TABLEPCT_VALIDN:
1479     case CTSF_SUBTABLEPCT_VALIDN:
1480     case CTSF_LAYERPCT_VALIDN:
1481     case CTSF_LAYERROWPCT_VALIDN:
1482     case CTSF_LAYERCOLPCT_VALIDN:
1483     case CTSF_ROWPCT_TOTALN:
1484     case CTSF_COLPCT_TOTALN:
1485     case CTSF_TABLEPCT_TOTALN:
1486     case CTSF_SUBTABLEPCT_TOTALN:
1487     case CTSF_LAYERPCT_TOTALN:
1488     case CTSF_LAYERROWPCT_TOTALN:
1489     case CTSF_LAYERCOLPCT_TOTALN:
1490     case CSTF_TOTALN:
1491     case CTSF_ETOTALN:
1492     case CTSF_VALIDN:
1493     case CTSF_EVALIDN:
1494       s->missing = s->valid = 0;
1495       break;
1496
1497     case CTSF_MAXIMUM:
1498     case CTSF_MINIMUM:
1499     case CTSF_RANGE:
1500       s->min = s->max = SYSMIS;
1501       break;
1502
1503     case CTSF_MEAN:
1504     case CTSF_SEMEAN:
1505     case CTSF_STDDEV:
1506     case CTSF_SUM:
1507     case CTSF_VARIANCE:
1508     case CTSF_ROWPCT_SUM:
1509     case CTSF_COLPCT_SUM:
1510     case CTSF_TABLEPCT_SUM:
1511     case CTSF_SUBTABLEPCT_SUM:
1512     case CTSF_LAYERPCT_SUM:
1513     case CTSF_LAYERROWPCT_SUM:
1514     case CTSF_LAYERCOLPCT_SUM:
1515       s->moments = moments1_create (MOMENT_VARIANCE);
1516       break;
1517
1518     case CTSF_MEDIAN:
1519     case CTSF_MISSING:
1520     case CTSF_MODE:
1521     case CTSF_PTILE:
1522       NOT_REACHED ();
1523
1524     case CTSF_RESPONSES:
1525     case CTSF_ROWPCT_RESPONSES:
1526     case CTSF_COLPCT_RESPONSES:
1527     case CTSF_TABLEPCT_RESPONSES:
1528     case CTSF_SUBTABLEPCT_RESPONSES:
1529     case CTSF_LAYERPCT_RESPONSES:
1530     case CTSF_LAYERROWPCT_RESPONSES:
1531     case CTSF_LAYERCOLPCT_RESPONSES:
1532     case CTSF_ROWPCT_RESPONSES_COUNT:
1533     case CTSF_COLPCT_RESPONSES_COUNT:
1534     case CTSF_TABLEPCT_RESPONSES_COUNT:
1535     case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1536     case CTSF_LAYERPCT_RESPONSES_COUNT:
1537     case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1538     case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1539     case CTSF_ROWPCT_COUNT_RESPONSES:
1540     case CTSF_COLPCT_COUNT_RESPONSES:
1541     case CTSF_TABLEPCT_COUNT_RESPONSES:
1542     case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1543     case CTSF_LAYERPCT_COUNT_RESPONSES:
1544     case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1545     case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1546       NOT_REACHED ();
1547     }
1548 }
1549
1550 static void UNUSED
1551 ctables_summary_uninit (union ctables_summary *s,
1552                         const struct ctables_summary_spec *ss)
1553 {
1554   switch (ss->function)
1555     {
1556     case CTSF_COUNT:
1557     case CTSF_ECOUNT:
1558     case CTSF_ROWPCT_COUNT:
1559     case CTSF_COLPCT_COUNT:
1560     case CTSF_TABLEPCT_COUNT:
1561     case CTSF_SUBTABLEPCT_COUNT:
1562     case CTSF_LAYERPCT_COUNT:
1563     case CTSF_LAYERROWPCT_COUNT:
1564     case CTSF_LAYERCOLPCT_COUNT:
1565     case CTSF_ROWPCT_VALIDN:
1566     case CTSF_COLPCT_VALIDN:
1567     case CTSF_TABLEPCT_VALIDN:
1568     case CTSF_SUBTABLEPCT_VALIDN:
1569     case CTSF_LAYERPCT_VALIDN:
1570     case CTSF_LAYERROWPCT_VALIDN:
1571     case CTSF_LAYERCOLPCT_VALIDN:
1572     case CTSF_ROWPCT_TOTALN:
1573     case CTSF_COLPCT_TOTALN:
1574     case CTSF_TABLEPCT_TOTALN:
1575     case CTSF_SUBTABLEPCT_TOTALN:
1576     case CTSF_LAYERPCT_TOTALN:
1577     case CTSF_LAYERROWPCT_TOTALN:
1578     case CTSF_LAYERCOLPCT_TOTALN:
1579     case CSTF_TOTALN:
1580     case CTSF_ETOTALN:
1581     case CTSF_VALIDN:
1582     case CTSF_EVALIDN:
1583       break;
1584
1585     case CTSF_MAXIMUM:
1586     case CTSF_MINIMUM:
1587     case CTSF_RANGE:
1588       break;
1589
1590     case CTSF_MEAN:
1591     case CTSF_SEMEAN:
1592     case CTSF_STDDEV:
1593     case CTSF_SUM:
1594     case CTSF_VARIANCE:
1595     case CTSF_ROWPCT_SUM:
1596     case CTSF_COLPCT_SUM:
1597     case CTSF_TABLEPCT_SUM:
1598     case CTSF_SUBTABLEPCT_SUM:
1599     case CTSF_LAYERPCT_SUM:
1600     case CTSF_LAYERROWPCT_SUM:
1601     case CTSF_LAYERCOLPCT_SUM:
1602       moments1_destroy (s->moments);
1603       break;
1604
1605     case CTSF_MEDIAN:
1606     case CTSF_MISSING:
1607     case CTSF_MODE:
1608     case CTSF_PTILE:
1609       NOT_REACHED ();
1610
1611     case CTSF_RESPONSES:
1612     case CTSF_ROWPCT_RESPONSES:
1613     case CTSF_COLPCT_RESPONSES:
1614     case CTSF_TABLEPCT_RESPONSES:
1615     case CTSF_SUBTABLEPCT_RESPONSES:
1616     case CTSF_LAYERPCT_RESPONSES:
1617     case CTSF_LAYERROWPCT_RESPONSES:
1618     case CTSF_LAYERCOLPCT_RESPONSES:
1619     case CTSF_ROWPCT_RESPONSES_COUNT:
1620     case CTSF_COLPCT_RESPONSES_COUNT:
1621     case CTSF_TABLEPCT_RESPONSES_COUNT:
1622     case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1623     case CTSF_LAYERPCT_RESPONSES_COUNT:
1624     case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1625     case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1626     case CTSF_ROWPCT_COUNT_RESPONSES:
1627     case CTSF_COLPCT_COUNT_RESPONSES:
1628     case CTSF_TABLEPCT_COUNT_RESPONSES:
1629     case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1630     case CTSF_LAYERPCT_COUNT_RESPONSES:
1631     case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1632     case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1633       NOT_REACHED ();
1634     }
1635 }
1636
1637 static void
1638 ctables_summary_add (union ctables_summary *s,
1639                      const struct ctables_summary_spec *ss,
1640                      const struct variable *var, const union value *value,
1641                      double weight)
1642 {
1643   switch (ss->function)
1644     {
1645     case CTSF_COUNT:
1646     case CTSF_ECOUNT:
1647     case CTSF_ROWPCT_COUNT:
1648     case CTSF_COLPCT_COUNT:
1649     case CTSF_TABLEPCT_COUNT:
1650     case CTSF_SUBTABLEPCT_COUNT:
1651     case CTSF_LAYERPCT_COUNT:
1652     case CTSF_LAYERROWPCT_COUNT:
1653     case CTSF_LAYERCOLPCT_COUNT:
1654     case CTSF_ROWPCT_VALIDN:
1655     case CTSF_COLPCT_VALIDN:
1656     case CTSF_TABLEPCT_VALIDN:
1657     case CTSF_SUBTABLEPCT_VALIDN:
1658     case CTSF_LAYERPCT_VALIDN:
1659     case CTSF_LAYERROWPCT_VALIDN:
1660     case CTSF_LAYERCOLPCT_VALIDN:
1661     case CTSF_ROWPCT_TOTALN:
1662     case CTSF_COLPCT_TOTALN:
1663     case CTSF_TABLEPCT_TOTALN:
1664     case CTSF_SUBTABLEPCT_TOTALN:
1665     case CTSF_LAYERPCT_TOTALN:
1666     case CTSF_LAYERROWPCT_TOTALN:
1667     case CTSF_LAYERCOLPCT_TOTALN:
1668     case CSTF_TOTALN:
1669     case CTSF_ETOTALN:
1670     case CTSF_VALIDN:
1671     case CTSF_EVALIDN:
1672       if (var_is_value_missing (var, value))
1673         s->missing += weight;
1674       else
1675         s->valid += weight;
1676       break;
1677
1678     case CTSF_MAXIMUM:
1679     case CTSF_MINIMUM:
1680     case CTSF_RANGE:
1681       if (!var_is_value_missing (var, value))
1682         {
1683           assert (!var_is_alpha (var)); /* XXX? */
1684           if (s->min == SYSMIS || value->f < s->min)
1685             s->min = value->f;
1686           if (s->max == SYSMIS || value->f > s->max)
1687             s->max = value->f;
1688         }
1689       break;
1690
1691     case CTSF_MEAN:
1692     case CTSF_SEMEAN:
1693     case CTSF_STDDEV:
1694     case CTSF_SUM:
1695     case CTSF_VARIANCE:
1696     case CTSF_ROWPCT_SUM:
1697     case CTSF_COLPCT_SUM:
1698     case CTSF_TABLEPCT_SUM:
1699     case CTSF_SUBTABLEPCT_SUM:
1700     case CTSF_LAYERPCT_SUM:
1701     case CTSF_LAYERROWPCT_SUM:
1702     case CTSF_LAYERCOLPCT_SUM:
1703       moments1_add (s->moments, value->f, weight);
1704       break;
1705
1706     case CTSF_MEDIAN:
1707     case CTSF_MISSING:
1708     case CTSF_MODE:
1709     case CTSF_PTILE:
1710       NOT_REACHED ();
1711
1712     case CTSF_RESPONSES:
1713     case CTSF_ROWPCT_RESPONSES:
1714     case CTSF_COLPCT_RESPONSES:
1715     case CTSF_TABLEPCT_RESPONSES:
1716     case CTSF_SUBTABLEPCT_RESPONSES:
1717     case CTSF_LAYERPCT_RESPONSES:
1718     case CTSF_LAYERROWPCT_RESPONSES:
1719     case CTSF_LAYERCOLPCT_RESPONSES:
1720     case CTSF_ROWPCT_RESPONSES_COUNT:
1721     case CTSF_COLPCT_RESPONSES_COUNT:
1722     case CTSF_TABLEPCT_RESPONSES_COUNT:
1723     case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1724     case CTSF_LAYERPCT_RESPONSES_COUNT:
1725     case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1726     case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1727     case CTSF_ROWPCT_COUNT_RESPONSES:
1728     case CTSF_COLPCT_COUNT_RESPONSES:
1729     case CTSF_TABLEPCT_COUNT_RESPONSES:
1730     case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1731     case CTSF_LAYERPCT_COUNT_RESPONSES:
1732     case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1733     case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1734       NOT_REACHED ();
1735     }
1736 }
1737
1738 static double
1739 ctables_summary_value (const struct ctables_freq *f,
1740                        union ctables_summary *s,
1741                        const struct ctables_summary_spec *ss)
1742 {
1743   switch (ss->function)
1744     {
1745     case CTSF_COUNT:
1746     case CTSF_ECOUNT:
1747       return s->valid;
1748
1749     case CTSF_SUBTABLEPCT_COUNT:
1750       return f->domains[CTDT_SUBTABLE]->valid ? s->valid / f->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1751
1752     case CTSF_ROWPCT_COUNT:
1753       return f->domains[CTDT_ROW]->valid ? s->valid / f->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1754
1755     case CTSF_COLPCT_COUNT:
1756       return f->domains[CTDT_COL]->valid ? s->valid / f->domains[CTDT_COL]->valid * 100 : SYSMIS;
1757
1758     case CTSF_TABLEPCT_COUNT:
1759       return f->domains[CTDT_TABLE]->valid ? s->valid / f->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1760
1761     case CTSF_LAYERPCT_COUNT:
1762       return f->domains[CTDT_LAYER]->valid ? s->valid / f->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1763
1764     case CTSF_LAYERROWPCT_COUNT:
1765       return f->domains[CTDT_LAYERROW]->valid ? s->valid / f->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1766
1767     case CTSF_LAYERCOLPCT_COUNT:
1768       return f->domains[CTDT_LAYERCOL]->valid ? s->valid / f->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1769
1770     case CTSF_ROWPCT_VALIDN:
1771     case CTSF_COLPCT_VALIDN:
1772     case CTSF_TABLEPCT_VALIDN:
1773     case CTSF_SUBTABLEPCT_VALIDN:
1774     case CTSF_LAYERPCT_VALIDN:
1775     case CTSF_LAYERROWPCT_VALIDN:
1776     case CTSF_LAYERCOLPCT_VALIDN:
1777     case CTSF_ROWPCT_TOTALN:
1778     case CTSF_COLPCT_TOTALN:
1779     case CTSF_TABLEPCT_TOTALN:
1780     case CTSF_SUBTABLEPCT_TOTALN:
1781     case CTSF_LAYERPCT_TOTALN:
1782     case CTSF_LAYERROWPCT_TOTALN:
1783     case CTSF_LAYERCOLPCT_TOTALN:
1784       NOT_REACHED ();
1785
1786     case CSTF_TOTALN:
1787     case CTSF_ETOTALN:
1788       return s->valid + s->missing;
1789
1790     case CTSF_VALIDN:
1791     case CTSF_EVALIDN:
1792       return s->valid;
1793
1794     case CTSF_MAXIMUM:
1795       return s->max;
1796
1797     case CTSF_MINIMUM:
1798       return s->min;
1799
1800     case CTSF_RANGE:
1801       return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
1802
1803     case CTSF_MEAN:
1804       {
1805         double mean;
1806         moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
1807         return mean;
1808       }
1809
1810     case CTSF_SEMEAN:
1811       {
1812         double weight, variance;
1813         moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
1814         return calc_semean (variance, weight);
1815       }
1816
1817     case CTSF_STDDEV:
1818       {
1819         double variance;
1820         moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1821         return variance != SYSMIS ? sqrt (variance) : SYSMIS;
1822       }
1823
1824     case CTSF_SUM:
1825       {
1826         double weight, mean;
1827         moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
1828         return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
1829       }
1830
1831     case CTSF_VARIANCE:
1832       {
1833         double variance;
1834         moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
1835         return variance;
1836       }
1837
1838     case CTSF_ROWPCT_SUM:
1839     case CTSF_COLPCT_SUM:
1840     case CTSF_TABLEPCT_SUM:
1841     case CTSF_SUBTABLEPCT_SUM:
1842     case CTSF_LAYERPCT_SUM:
1843     case CTSF_LAYERROWPCT_SUM:
1844     case CTSF_LAYERCOLPCT_SUM:
1845       NOT_REACHED ();
1846
1847     case CTSF_MEDIAN:
1848     case CTSF_MISSING:
1849     case CTSF_MODE:
1850     case CTSF_PTILE:
1851       NOT_REACHED ();
1852
1853     case CTSF_RESPONSES:
1854     case CTSF_ROWPCT_RESPONSES:
1855     case CTSF_COLPCT_RESPONSES:
1856     case CTSF_TABLEPCT_RESPONSES:
1857     case CTSF_SUBTABLEPCT_RESPONSES:
1858     case CTSF_LAYERPCT_RESPONSES:
1859     case CTSF_LAYERROWPCT_RESPONSES:
1860     case CTSF_LAYERCOLPCT_RESPONSES:
1861     case CTSF_ROWPCT_RESPONSES_COUNT:
1862     case CTSF_COLPCT_RESPONSES_COUNT:
1863     case CTSF_TABLEPCT_RESPONSES_COUNT:
1864     case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1865     case CTSF_LAYERPCT_RESPONSES_COUNT:
1866     case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1867     case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1868     case CTSF_ROWPCT_COUNT_RESPONSES:
1869     case CTSF_COLPCT_COUNT_RESPONSES:
1870     case CTSF_TABLEPCT_COUNT_RESPONSES:
1871     case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1872     case CTSF_LAYERPCT_COUNT_RESPONSES:
1873     case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1874     case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1875       NOT_REACHED ();
1876     }
1877
1878   NOT_REACHED ();
1879 }
1880
1881 struct ctables_freq_sort_aux
1882   {
1883     const struct ctables_table *t;
1884     enum pivot_axis_type a;
1885   };
1886
1887 static int
1888 ctables_freq_compare_3way (const void *a_, const void *b_, const void *aux_)
1889 {
1890   const struct ctables_freq_sort_aux *aux = aux_;
1891   struct ctables_freq *const *ap = a_;
1892   struct ctables_freq *const *bp = b_;
1893   const struct ctables_freq *a = *ap;
1894   const struct ctables_freq *b = *bp;
1895
1896   size_t a_idx = a->axes[aux->a].vaa_idx;
1897   size_t b_idx = b->axes[aux->a].vaa_idx;
1898   if (a_idx != b_idx)
1899     return a_idx < b_idx ? -1 : 1;
1900
1901   const struct var_array *va = &aux->t->vaas[aux->a].vas[a_idx];
1902   for (size_t i = 0; i < va->n; i++)
1903     if (i != va->scale_idx)
1904       {
1905         int cmp = value_compare_3way (&a->axes[aux->a].values[i],
1906                                       &b->axes[aux->a].values[i],
1907                                       var_get_width (va->vars[i]));
1908         if (cmp)
1909           return cmp;
1910       }
1911   return 0;
1912 }
1913
1914 /* Algorithm:
1915
1916    For each row:
1917        For each ctables_table:
1918            For each combination of row vars:
1919                For each combination of column vars:
1920                    For each combination of layer vars:
1921                        Add entry
1922    Make a table of row values:
1923        Sort entries by row values
1924        Assign a 0-based index to each actual value
1925        Construct a dimension
1926    Make a table of column values
1927    Make a table of layer values
1928    For each entry:
1929        Fill the table entry using the indexes from before.
1930  */
1931
1932 static struct ctables_domain *
1933 ctables_domain_insert (struct ctables_table *t, struct ctables_freq *f,
1934                        enum ctables_domain_type domain)
1935 {
1936   size_t hash = 0;
1937   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1938     {
1939       size_t idx = f->axes[a].vaa_idx;
1940       const struct var_array *va = &t->vaas[a].vas[idx];
1941       hash = hash_int (idx, hash);
1942       for (size_t i = 0; i < va->n_domains[domain]; i++)
1943         {
1944           size_t v_idx = va->domains[domain][i];
1945           hash = value_hash (&f->axes[a].values[v_idx],
1946                              var_get_width (va->vars[v_idx]), hash);
1947         }
1948     }
1949
1950   struct ctables_domain *d;
1951   HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
1952     {
1953       const struct ctables_freq *df = d->example;
1954       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
1955         {
1956           size_t idx = f->axes[a].vaa_idx;
1957           if (idx != df->axes[a].vaa_idx)
1958             goto not_equal;
1959
1960           const struct var_array *va = &t->vaas[a].vas[idx];
1961           for (size_t i = 0; i < va->n_domains[domain]; i++)
1962             {
1963               size_t v_idx = va->domains[domain][i];
1964               if (!value_equal (&df->axes[a].values[v_idx],
1965                                 &f->axes[a].values[v_idx],
1966                                 var_get_width (va->vars[v_idx])))
1967                 goto not_equal;
1968             }
1969         }
1970       return d;
1971
1972     not_equal: ;
1973     }
1974
1975   d = xmalloc (sizeof *d);
1976   *d = (struct ctables_domain) { .example = f };
1977   hmap_insert (&t->domains[domain], &d->node, hash);
1978   return d;
1979 }
1980
1981 static const struct ctables_cat_value *
1982 ctables_categories_match (const struct ctables_categories *cats,
1983                           const union value *v, const struct variable *var)
1984 {
1985   const struct ctables_cat_value *othernm = NULL;
1986   for (size_t i = cats->n_values; i-- > 0; )
1987     {
1988       const struct ctables_cat_value *cv = &cats->values[i];
1989       switch (cv->type)
1990         {
1991         case CCVT_NUMBER:
1992           if (cv->number == v->f)
1993             return cv;
1994           break;
1995
1996         case CCVT_STRING:
1997           NOT_REACHED ();
1998
1999         case CCVT_RANGE:
2000           if ((cv->range[0] == -DBL_MAX || v->f >= cv->range[0])
2001               && (cv->range[1] == DBL_MAX || v->f <= cv->range[1]))
2002             return cv;
2003           break;
2004
2005         case CCVT_MISSING:
2006           if (var_is_value_missing (var, v))
2007             return cv;
2008           break;
2009
2010         case CCVT_OTHERNM:
2011           if (!othernm)
2012             othernm = cv;
2013           break;
2014
2015         case CCVT_SUBTOTAL:
2016         case CCVT_HSUBTOTAL:
2017           break;
2018         }
2019     }
2020
2021   return var_is_value_missing (var, v) ? NULL : othernm;
2022 }
2023
2024 static void
2025 ctables_freqtab_insert (struct ctables_table *t,
2026                         const struct ccase *c,
2027                         size_t ir, size_t ic, size_t il,
2028                         double weight)
2029 {
2030   size_t ix[PIVOT_N_AXES] = {
2031     [PIVOT_AXIS_ROW] = ir,
2032     [PIVOT_AXIS_COLUMN] = ic,
2033     [PIVOT_AXIS_LAYER] = il,
2034   };
2035   const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
2036
2037   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2038     {
2039       const struct var_array *va = &t->vaas[a].vas[ix[a]];
2040       for (size_t i = 0; i < va->n; i++)
2041         {
2042           if (i == va->scale_idx)
2043             continue;
2044
2045           const struct ctables_categories *cats = t->categories[var_get_dict_index (va->vars[i])];
2046           if (!cats || !cats->n_values)
2047             continue;
2048
2049           if (!ctables_categories_match (cats, case_data (c, va->vars[i]), va->vars[i]))
2050             return;
2051         }
2052     }
2053
2054   size_t hash = 0;
2055   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2056     {
2057       const struct var_array *va = &t->vaas[a].vas[ix[a]];
2058       hash = hash_int (ix[a], hash);
2059       for (size_t i = 0; i < va->n; i++)
2060         if (i != va->scale_idx)
2061           hash = value_hash (case_data (c, va->vars[i]),
2062                              var_get_width (va->vars[i]), hash);
2063     }
2064
2065   struct ctables_freq *f;
2066   HMAP_FOR_EACH_WITH_HASH (f, struct ctables_freq, node, hash, &t->ft)
2067     {
2068       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2069         {
2070           const struct var_array *va = &t->vaas[a].vas[ix[a]];
2071           if (f->axes[a].vaa_idx != ix[a])
2072             goto not_equal;
2073           for (size_t i = 0; i < va->n; i++)
2074             if (i != va->scale_idx
2075                 && !value_equal (case_data (c, va->vars[i]),
2076                                  &f->axes[a].values[i],
2077                                  var_get_width (va->vars[i])))
2078                 goto not_equal;
2079         }
2080
2081       goto summarize;
2082
2083     not_equal: ;
2084     }
2085
2086   f = xmalloc (sizeof *f);
2087   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2088     {
2089       const struct var_array *va = &t->vaas[a].vas[ix[a]];
2090       f->axes[a].vaa_idx = ix[a];
2091       f->axes[a].values = (va->n
2092                            ? xnmalloc (va->n, sizeof *f->axes[a].values)
2093                            : NULL);
2094       for (size_t i = 0; i < va->n; i++)
2095         value_clone (&f->axes[a].values[i], case_data (c, va->vars[i]),
2096                      var_get_width (va->vars[i]));
2097     }
2098   f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
2099   for (size_t i = 0; i < ss->n_summaries; i++)
2100     ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
2101   for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2102     f->domains[dt] = ctables_domain_insert (t, f, dt);
2103   hmap_insert (&t->ft, &f->node, hash);
2104
2105 summarize:
2106   for (size_t i = 0; i < ss->n_summaries; i++)
2107     ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
2108                          case_data (c, ss->summary_var), weight);
2109   for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2110     f->domains[dt]->valid += weight;
2111 }
2112
2113 static bool
2114 ctables_execute (struct dataset *ds, struct ctables *ct)
2115 {
2116   for (size_t i = 0; i < ct->n_tables; i++)
2117     {
2118       struct ctables_table *t = ct->tables[i];
2119       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2120         if (t->axes[a])
2121           {
2122             t->vaas[a] = enumerate_fts (a, t->axes[a]);
2123
2124             for (size_t j = 0; j < t->vaas[a].n; j++)
2125               {
2126                 struct var_array *va = &t->vaas[a].vas[j];
2127                 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2128                   {
2129                     va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
2130                     va->n_domains[dt] = 0;
2131
2132                     for (size_t k = 0; k < va->n; k++)
2133                       {
2134                         if (k == va->scale_idx)
2135                           continue;
2136
2137                         switch (dt)
2138                           {
2139                           case CTDT_TABLE:
2140                             continue;
2141
2142                           case CTDT_LAYER:
2143                             if (a != PIVOT_AXIS_LAYER)
2144                               continue;
2145                             break;
2146
2147                           case CTDT_SUBTABLE:
2148                           case CTDT_ROW:
2149                           case CTDT_COL:
2150                             if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2151                                 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2152                                 : a == PIVOT_AXIS_ROW)
2153                               {
2154                                 if (k == va->n - 1
2155                                     || (va->scale_idx == va->n - 1
2156                                         && k == va->n - 2))
2157                                   continue;
2158                               }
2159                             break;
2160
2161                           case CTDT_LAYERROW:
2162                             if (a == PIVOT_AXIS_COLUMN)
2163                               continue;
2164                             break;
2165
2166                           case CTDT_LAYERCOL:
2167                             if (a == PIVOT_AXIS_ROW)
2168                               continue;
2169                             break;
2170                           }
2171
2172                         va->domains[dt][va->n_domains[dt]++] = k;
2173                       }
2174                   }
2175               }
2176           }
2177         else
2178           {
2179             struct var_array *va = xmalloc (sizeof *va);
2180             *va = (struct var_array) { .n = 0 };
2181             t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
2182           }
2183
2184       for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
2185         {
2186           struct var_array *va = &t->vaas[t->summary_axis].vas[i];
2187           if (!va->n_summaries)
2188             {
2189               va->summaries = xmalloc (sizeof *va->summaries);
2190               va->n_summaries = 1;
2191
2192               enum ctables_summary_function function
2193                 = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
2194               struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
2195
2196               *va->summaries = (struct ctables_summary_spec) {
2197                 .function = function,
2198                 .format = ctables_summary_default_format (function, &var),
2199                 .label = ctables_summary_default_label (function, 0),
2200               };
2201               if (!va->summary_var)
2202                 va->summary_var = va->vars[0];
2203             }
2204         }
2205     }
2206
2207   struct casereader *input = casereader_create_filter_weight (proc_open (ds),
2208                                                               dataset_dict (ds),
2209                                                               NULL, NULL);
2210   bool warn_on_invalid = true;
2211   double total_weight = 0;
2212   for (struct ccase *c = casereader_read (input); c;
2213        case_unref (c), c = casereader_read (input))
2214     {
2215       double weight = dict_get_case_weight (dataset_dict (ds), c,
2216                                             &warn_on_invalid);
2217       total_weight += weight;
2218
2219       for (size_t i = 0; i < ct->n_tables; i++)
2220         {
2221           struct ctables_table *t = ct->tables[i];
2222
2223           for (size_t ir = 0; ir < t->vaas[PIVOT_AXIS_ROW].n; ir++)
2224             for (size_t ic = 0; ic < t->vaas[PIVOT_AXIS_COLUMN].n; ic++)
2225               for (size_t il = 0; il < t->vaas[PIVOT_AXIS_LAYER].n; il++)
2226                 ctables_freqtab_insert (t, c, ir, ic, il, weight);
2227         }
2228     }
2229   casereader_destroy (input);
2230
2231   for (size_t i = 0; i < ct->n_tables; i++)
2232     {
2233       struct ctables_table *t = ct->tables[i];
2234
2235       struct pivot_table *pt = pivot_table_create__ (
2236         (t->title
2237          ? pivot_value_new_user_text (t->title, SIZE_MAX)
2238          : pivot_value_new_text (N_("Custom Tables"))),
2239         NULL);
2240       if (t->caption)
2241         pivot_table_set_caption (
2242           pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2243       if (t->corner)
2244         pivot_table_set_caption (
2245           pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2246
2247       pivot_table_set_look (pt, ct->look);
2248       struct pivot_dimension *d[PIVOT_N_AXES];
2249       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2250         {
2251           static const char *names[] = {
2252             [PIVOT_AXIS_ROW] = N_("Rows"),
2253             [PIVOT_AXIS_COLUMN] = N_("Columns"),
2254             [PIVOT_AXIS_LAYER] = N_("Layers"),
2255           };
2256           d[a] = (t->axes[a] || a == t->summary_axis
2257                   ? pivot_dimension_create (pt, a, names[a])
2258                   : NULL);
2259           if (!d[a])
2260             continue;
2261
2262           assert (t->axes[a]);
2263
2264           struct ctables_freq **sorted = xnmalloc (t->ft.count, sizeof *sorted);
2265
2266           struct ctables_freq *f;
2267           size_t n = 0;
2268           HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2269             sorted[n++] = f;
2270           assert (n == t->ft.count);
2271
2272           struct ctables_freq_sort_aux aux = { .t = t, .a = a };
2273           sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
2274
2275           size_t max_depth = 0;
2276           for (size_t j = 0; j < t->vaas[a].n; j++)
2277             if (t->vaas[a].vas[j].n > max_depth)
2278               max_depth = t->vaas[a].vas[j].n;
2279
2280           struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2281           struct pivot_category *top = NULL;
2282           int prev_leaf = 0;
2283           for (size_t j = 0; j < n; j++)
2284             {
2285               struct ctables_freq *f = sorted[j];
2286               const struct var_array *va = &t->vaas[a].vas[f->axes[a].vaa_idx];
2287
2288               size_t n_common = 0;
2289               bool new_subtable = false;
2290               if (j > 0)
2291                 {
2292                   struct ctables_freq *prev = sorted[j - 1];
2293                   if (prev->axes[a].vaa_idx == f->axes[a].vaa_idx)
2294                     {
2295                       for (; n_common < va->n; n_common++)
2296                         if (n_common != va->scale_idx
2297                             && !value_equal (&prev->axes[a].values[n_common],
2298                                              &f->axes[a].values[n_common],
2299                                              var_get_type (va->vars[n_common])))
2300                           break;
2301                     }
2302                   else
2303                     new_subtable = true;
2304                 }
2305               else
2306                 new_subtable = true;
2307
2308               if (new_subtable)
2309                 {
2310                   enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
2311                   top = d[a]->root;
2312                   if (vlabel != CTVL_NONE)
2313                     top = pivot_category_create_group__ (
2314                       top, pivot_value_new_variable (va->vars[0]));
2315                 }
2316               if (n_common == va->n)
2317                 {
2318                   f->axes[a].leaf = prev_leaf;
2319                   continue;
2320                 }
2321
2322               for (size_t k = n_common; k < va->n; k++)
2323                 {
2324                   struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2325
2326                   struct pivot_value *label
2327                     = (k != va->scale_idx
2328                        ? pivot_value_new_var_value (va->vars[k],
2329                                                     &f->axes[a].values[k])
2330                        : NULL);
2331                   if (k == va->n - 1)
2332                     {
2333                       if (a == t->summary_axis)
2334                         {
2335                           if (label)
2336                             parent = pivot_category_create_group__ (parent, label);
2337                           for (size_t m = 0; m < va->n_summaries; m++)
2338                             {
2339                               int leaf = pivot_category_create_leaf (
2340                                 parent, pivot_value_new_text (va->summaries[m].label));
2341                               if (m == 0)
2342                                 prev_leaf = leaf;
2343                             }
2344                         }
2345                       else
2346                         {
2347                           /* This assertion is true as long as the summary axis
2348                              is the axis where the summaries are displayed. */
2349                           assert (label);
2350
2351                           prev_leaf = pivot_category_create_leaf (parent, label);
2352                         }
2353                       break;
2354                     }
2355
2356                   if (label)
2357                     parent = pivot_category_create_group__ (parent, label);
2358
2359                   enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
2360                   if (vlabel != CTVL_NONE)
2361                     parent = pivot_category_create_group__ (
2362                       parent, pivot_value_new_variable (va->vars[k + 1]));
2363                   groups[k] = parent;
2364                 }
2365
2366               f->axes[a].leaf = prev_leaf;
2367             }
2368           free (sorted);
2369           free (groups);
2370         }
2371       struct ctables_freq *f;
2372       HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
2373         {
2374           const struct var_array *ss = &t->vaas[t->summary_axis].vas[f->axes[t->summary_axis].vaa_idx];
2375           for (size_t j = 0; j < ss->n_summaries; j++)
2376             {
2377               size_t dindexes[3];
2378               size_t n_dindexes = 0;
2379
2380               for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2381                 if (d[a])
2382                   {
2383                     int leaf = f->axes[a].leaf;
2384                     if (a == t->summary_axis)
2385                       leaf += j;
2386                     dindexes[n_dindexes++] = leaf;
2387                   }
2388
2389               double d = ctables_summary_value (f, &f->summaries[j], &ss->summaries[j]);
2390               struct pivot_value *value = pivot_value_new_number (d);
2391               value->numeric.format = ss->summaries[j].format;
2392               pivot_table_put (pt, dindexes, n_dindexes, value);
2393             }
2394         }
2395
2396       pivot_table_submit (pt);
2397     }
2398
2399 #if 0
2400   for (size_t i = 0; i < ct->n_tables; i++)
2401     {
2402       struct ctables_table *t = ct->tables[i];
2403
2404       for (size_t j = 0; j < t->n_fts; j++)
2405         {
2406           struct ctables_freqtab *ft = t->fts[j];
2407           struct ctables_freq *f, *next;
2408           HMAP_FOR_EACH_SAFE (f, next, struct ctables_freq, node, &ft->data)
2409             {
2410               hmap_delete (&ft->data, &f->node);
2411               for (size_t k = 0; k < ft->n_summaries; k++)
2412                 ctables_summary_uninit (&f->summaries[k], &ft->summaries[k]);
2413               free (f->summaries);
2414               for (size_t k = 0; k < ft->vars.n; k++)
2415                 {
2416                   const struct variable *var = ft->vars.vars[k];
2417                   value_destroy (&f->values[k], var_get_width (var));
2418                 }
2419               free (f);
2420             }
2421           hmap_destroy (&ft->data);
2422           var_array_uninit (&ft->vars);
2423           free (ft);
2424         }
2425       free (t->fts);
2426     }
2427 #endif
2428   
2429   return proc_commit (ds);
2430 }
2431
2432 int
2433 cmd_ctables (struct lexer *lexer, struct dataset *ds)
2434 {
2435   size_t n_vars = dict_get_n_vars (dataset_dict (ds));
2436   enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
2437   enum settings_value_show tvars = settings_get_show_variables ();
2438   for (size_t i = 0; i < n_vars; i++)
2439     vlabels[i] = (enum ctables_vlabel) tvars;
2440
2441   struct ctables *ct = xmalloc (sizeof *ct);
2442   *ct = (struct ctables) {
2443     .look = pivot_table_look_unshare (pivot_table_look_ref (
2444                                         pivot_table_look_get_default ())),
2445     .vlabels = vlabels,
2446     .hide_threshold = 5,
2447   };
2448   ct->look->omit_empty = false;
2449
2450   if (!lex_force_match (lexer, T_SLASH))
2451     goto error;
2452
2453   while (!lex_match_id (lexer, "TABLE"))
2454     {
2455       if (lex_match_id (lexer, "FORMAT"))
2456         {
2457           double widths[2] = { SYSMIS, SYSMIS };
2458           double units_per_inch = 72.0;
2459
2460           while (lex_token (lexer) != T_SLASH)
2461             {
2462               if (lex_match_id (lexer, "MINCOLWIDTH"))
2463                 {
2464                   if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
2465                     goto error;
2466                 }
2467               else if (lex_match_id (lexer, "MAXCOLWIDTH"))
2468                 {
2469                   if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
2470                     goto error;
2471                 }
2472               else if (lex_match_id (lexer, "UNITS"))
2473                 {
2474                   lex_match (lexer, T_EQUALS);
2475                   if (lex_match_id (lexer, "POINTS"))
2476                     units_per_inch = 72.0;
2477                   else if (lex_match_id (lexer, "INCHES"))
2478                     units_per_inch = 1.0;
2479                   else if (lex_match_id (lexer, "CM"))
2480                     units_per_inch = 2.54;
2481                   else
2482                     {
2483                       lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
2484                       goto error;
2485                     }
2486                 }
2487               else if (lex_match_id (lexer, "EMPTY"))
2488                 {
2489                   free (ct->zero);
2490                   ct->zero = NULL;
2491
2492                   lex_match (lexer, T_EQUALS);
2493                   if (lex_match_id (lexer, "ZERO"))
2494                     {
2495                       /* Nothing to do. */
2496                     }
2497                   else if (lex_match_id (lexer, "BLANK"))
2498                     ct->zero = xstrdup ("");
2499                   else if (lex_force_string (lexer))
2500                     {
2501                       ct->zero = ss_xstrdup (lex_tokss (lexer));
2502                       lex_get (lexer);
2503                     }
2504                   else
2505                     goto error;
2506                 }
2507               else if (lex_match_id (lexer, "MISSING"))
2508                 {
2509                   lex_match (lexer, T_EQUALS);
2510                   if (!lex_force_string (lexer))
2511                     goto error;
2512
2513                   free (ct->missing);
2514                   ct->missing = (strcmp (lex_tokcstr (lexer), ".")
2515                                  ? ss_xstrdup (lex_tokss (lexer))
2516                                  : NULL);
2517                   lex_get (lexer);
2518                 }
2519               else
2520                 {
2521                   lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
2522                                        "UNITS", "EMPTY", "MISSING");
2523                   goto error;
2524                 }
2525             }
2526
2527           if (widths[0] != SYSMIS && widths[1] != SYSMIS
2528               && widths[0] > widths[1])
2529             {
2530               msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
2531               goto error;
2532             }
2533
2534           for (size_t i = 0; i < 2; i++)
2535             if (widths[i] != SYSMIS)
2536               {
2537                 int *wr = ct->look->width_ranges[TABLE_HORZ];
2538                 wr[i] = widths[i] / units_per_inch * 96.0;
2539                 if (wr[0] > wr[1])
2540                   wr[!i] = wr[i];
2541               }
2542         }
2543       else if (lex_match_id (lexer, "VLABELS"))
2544         {
2545           if (!lex_force_match_id (lexer, "VARIABLES"))
2546             goto error;
2547           lex_match (lexer, T_EQUALS);
2548
2549           struct variable **vars;
2550           size_t n_vars;
2551           if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
2552                                 PV_NO_SCRATCH))
2553             goto error;
2554
2555           if (!lex_force_match_id (lexer, "DISPLAY"))
2556             {
2557               free (vars);
2558               goto error;
2559             }
2560           lex_match (lexer, T_EQUALS);
2561
2562           enum ctables_vlabel vlabel;
2563           if (lex_match_id (lexer, "DEFAULT"))
2564             vlabel = (enum ctables_vlabel) settings_get_show_variables ();
2565           else if (lex_match_id (lexer, "NAME"))
2566             vlabel = CTVL_NAME;
2567           else if (lex_match_id (lexer, "LABEL"))
2568             vlabel = CTVL_LABEL;
2569           else if (lex_match_id (lexer, "BOTH"))
2570             vlabel = CTVL_BOTH;
2571           else if (lex_match_id (lexer, "NONE"))
2572             vlabel = CTVL_NONE;
2573           else
2574             {
2575               lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
2576                                    "BOTH", "NONE");
2577               free (vars);
2578               goto error;
2579             }
2580
2581           for (size_t i = 0; i < n_vars; i++)
2582             ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
2583           free (vars);
2584         }
2585       else if (lex_match_id (lexer, "MRSETS"))
2586         {
2587           if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
2588             goto error;
2589           lex_match (lexer, T_EQUALS);
2590           if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
2591             goto error;
2592         }
2593       else if (lex_match_id (lexer, "SMISSING"))
2594         {
2595           if (lex_match_id (lexer, "VARIABLE"))
2596             ct->smissing_listwise = false;
2597           else if (lex_match_id (lexer, "LISTWISE"))
2598             ct->smissing_listwise = true;
2599           else
2600             {
2601               lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
2602               goto error;
2603             }
2604         }
2605       /* XXX PCOMPUTE */
2606       else if (lex_match_id (lexer, "WEIGHT"))
2607         {
2608           if (!lex_force_match_id (lexer, "VARIABLE"))
2609             goto error;
2610           lex_match (lexer, T_EQUALS);
2611           ct->base_weight = parse_variable (lexer, dataset_dict (ds));
2612           if (!ct->base_weight)
2613             goto error;
2614         }
2615       else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
2616         {
2617           if (!lex_force_match_id (lexer, "COUNT"))
2618             goto error;
2619           lex_match (lexer, T_EQUALS);
2620           if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
2621             goto error;
2622           ct->hide_threshold = lex_integer (lexer);
2623           lex_get (lexer);
2624         }
2625       else
2626         {
2627           lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
2628                                "SMISSING", "PCOMPUTE", "PPROPERTIES",
2629                                "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
2630           goto error;
2631         }
2632
2633       if (!lex_force_match (lexer, T_SLASH))
2634         goto error;
2635     }
2636
2637   size_t allocated_tables = 0;
2638   do
2639     {
2640       if (ct->n_tables >= allocated_tables)
2641         ct->tables = x2nrealloc (ct->tables, &allocated_tables,
2642                                  sizeof *ct->tables);
2643
2644       struct ctables_table *t = xmalloc (sizeof *t);
2645       *t = (struct ctables_table) {
2646         .ft = HMAP_INITIALIZER (t->ft),
2647         .slabels_position = PIVOT_AXIS_COLUMN,
2648         .slabels_visible = true,
2649         .row_labels = CTLP_NORMAL,
2650         .col_labels = CTLP_NORMAL,
2651         .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
2652                                sizeof *t->categories),
2653         .n_categories = dict_get_n_vars (dataset_dict (ds)),
2654         .cilevel = 95,
2655       };
2656       for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2657         hmap_init (&t->domains[dt]);
2658       ct->tables[ct->n_tables++] = t;
2659
2660       lex_match (lexer, T_EQUALS);
2661       if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
2662         goto error;
2663       if (lex_match (lexer, T_BY))
2664         {
2665           if (!ctables_axis_parse (lexer, dataset_dict (ds),
2666                                    ct, t, PIVOT_AXIS_COLUMN))
2667             goto error;
2668
2669           if (lex_match (lexer, T_BY))
2670             {
2671               if (!ctables_axis_parse (lexer, dataset_dict (ds),
2672                                        ct, t, PIVOT_AXIS_LAYER))
2673                 goto error;
2674             }
2675         }
2676
2677       if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
2678           && !t->axes[PIVOT_AXIS_LAYER])
2679         {
2680           lex_error (lexer, _("At least one variable must be specified."));
2681           goto error;
2682         }
2683
2684       const struct ctables_axis *scales[PIVOT_N_AXES];
2685       size_t n_scales = 0;
2686       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2687         {
2688           scales[a] = find_scale (t->axes[a]);
2689           if (scales[a])
2690             n_scales++;
2691         }
2692       if (n_scales > 1)
2693         {
2694           msg (SE, _("Scale variables may appear only on one axis."));
2695           if (scales[PIVOT_AXIS_ROW])
2696             msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
2697                     _("This scale variable appears on the rows axis."));
2698           if (scales[PIVOT_AXIS_COLUMN])
2699             msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
2700                     _("This scale variable appears on the columns axis."));
2701           if (scales[PIVOT_AXIS_LAYER])
2702             msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
2703                     _("This scale variable appears on the layer axis."));
2704           goto error;
2705         }
2706
2707       const struct ctables_axis *summaries[PIVOT_N_AXES];
2708       size_t n_summaries = 0;
2709       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2710         {
2711           summaries[a] = (scales[a]
2712                           ? scales[a]
2713                           : find_categorical_summary_spec (t->axes[a]));
2714           if (summaries[a])
2715             n_summaries++;
2716         }
2717       if (n_summaries > 1)
2718         {
2719           msg (SE, _("Summaries may appear only on one axis."));
2720           if (summaries[PIVOT_AXIS_ROW])
2721             msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
2722                     _("This variable on the rows axis has a summary."));
2723           if (summaries[PIVOT_AXIS_COLUMN])
2724             msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
2725                     _("This variable on the columns axis has a summary."));
2726           if (summaries[PIVOT_AXIS_LAYER])
2727             msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
2728                     _("This variable on the layers axis has a summary."));
2729           goto error;
2730         }
2731       for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2732         if (n_summaries ? summaries[a] : t->axes[a])
2733           {
2734             t->summary_axis = a;
2735             break;
2736           }
2737
2738       if (lex_token (lexer) == T_ENDCMD)
2739         break;
2740       if (!lex_force_match (lexer, T_SLASH))
2741         break;
2742
2743       while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
2744         {
2745           if (lex_match_id (lexer, "SLABELS"))
2746             {
2747               while (lex_token (lexer) != T_SLASH)
2748                 {
2749                   if (lex_match_id (lexer, "POSITION"))
2750                     {
2751                       lex_match (lexer, T_EQUALS);
2752                       if (lex_match_id (lexer, "COLUMN"))
2753                         t->slabels_position = PIVOT_AXIS_COLUMN;
2754                       else if (lex_match_id (lexer, "ROW"))
2755                         t->slabels_position = PIVOT_AXIS_ROW;
2756                       else if (lex_match_id (lexer, "LAYER"))
2757                         t->slabels_position = PIVOT_AXIS_LAYER;
2758                       else
2759                         {
2760                           lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
2761                           goto error;
2762                         }
2763                     }
2764                   else if (lex_match_id (lexer, "VISIBLE"))
2765                     {
2766                       lex_match (lexer, T_EQUALS);
2767                       if (!parse_bool (lexer, &t->slabels_visible))
2768                         goto error;
2769                     }
2770                   else
2771                     {
2772                       lex_error_expecting (lexer, "POSITION", "VISIBLE");
2773                       goto error;
2774                     }
2775                 }
2776             }
2777           else if (lex_match_id (lexer, "CLABELS"))
2778             {
2779               while (lex_token (lexer) != T_SLASH)
2780                 {
2781                   if (lex_match_id (lexer, "AUTO"))
2782                     t->row_labels = t->col_labels = CTLP_NORMAL;
2783                   else if (lex_match_id (lexer, "ROWLABELS"))
2784                     {
2785                       lex_match (lexer, T_EQUALS);
2786                       if (lex_match_id (lexer, "OPPOSITE"))
2787                         t->row_labels = CTLP_OPPOSITE;
2788                       else if (lex_match_id (lexer, "LAYER"))
2789                         t->row_labels = CTLP_LAYER;
2790                       else
2791                         {
2792                           lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2793                           goto error;
2794                         }
2795                     }
2796                   else if (lex_match_id (lexer, "COLLABELS"))
2797                     {
2798                       lex_match (lexer, T_EQUALS);
2799                       if (lex_match_id (lexer, "OPPOSITE"))
2800                         t->col_labels = CTLP_OPPOSITE;
2801                       else if (lex_match_id (lexer, "LAYER"))
2802                         t->col_labels = CTLP_LAYER;
2803                       else
2804                         {
2805                           lex_error_expecting (lexer, "OPPOSITE", "LAYER");
2806                           goto error;
2807                         }
2808                     }
2809                   else
2810                     {
2811                       lex_error_expecting (lexer, "AUTO", "ROWLABELS",
2812                                            "COLLABELS");
2813                       goto error;
2814                     }
2815                 }
2816             }
2817           else if (lex_match_id (lexer, "CRITERIA"))
2818             {
2819               if (!lex_force_match_id (lexer, "CILEVEL"))
2820                 goto error;
2821               lex_match (lexer, T_EQUALS);
2822
2823               if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
2824                 goto error;
2825               t->cilevel = lex_number (lexer);
2826               lex_get (lexer);
2827             }
2828           else if (lex_match_id (lexer, "CATEGORIES"))
2829             {
2830               if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
2831                 goto error;
2832             }
2833           else if (lex_match_id (lexer, "TITLES"))
2834             {
2835               do
2836                 {
2837                   char **textp;
2838                   if (lex_match_id (lexer, "CAPTION"))
2839                     textp = &t->caption;
2840                   else if (lex_match_id (lexer, "CORNER"))
2841                     textp = &t->corner;
2842                   else if (lex_match_id (lexer, "TITLE"))
2843                     textp = &t->title;
2844                   else
2845                     {
2846                       lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
2847                       goto error;
2848                     }
2849                   lex_match (lexer, T_EQUALS);
2850
2851                   struct string s = DS_EMPTY_INITIALIZER;
2852                   while (lex_is_string (lexer))
2853                     {
2854                       if (!ds_is_empty (&s))
2855                         ds_put_byte (&s, ' ');
2856                       ds_put_substring (&s, lex_tokss (lexer));
2857                       lex_get (lexer);
2858                     }
2859                   free (*textp);
2860                   *textp = ds_steal_cstr (&s);
2861                 }
2862               while (lex_token (lexer) != T_SLASH
2863                      && lex_token (lexer) != T_ENDCMD);
2864             }
2865           else if (lex_match_id (lexer, "SIGTEST"))
2866             {
2867               if (!t->chisq)
2868                 {
2869                   t->chisq = xmalloc (sizeof *t->chisq);
2870                   *t->chisq = (struct ctables_chisq) {
2871                     .alpha = .05,
2872                     .include_mrsets = true,
2873                     .all_visible = true,
2874                   };
2875                 }
2876
2877               do
2878                 {
2879                   if (lex_match_id (lexer, "TYPE"))
2880                     {
2881                       lex_match (lexer, T_EQUALS);
2882                       if (!lex_force_match_id (lexer, "CHISQUARE"))
2883                         goto error;
2884                     }
2885                   else if (lex_match_id (lexer, "ALPHA"))
2886                     {
2887                       lex_match (lexer, T_EQUALS);
2888                       if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
2889                         goto error;
2890                       t->chisq->alpha = lex_number (lexer);
2891                       lex_get (lexer);
2892                     }
2893                   else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2894                     {
2895                       lex_match (lexer, T_EQUALS);
2896                       if (parse_bool (lexer, &t->chisq->include_mrsets))
2897                         goto error;
2898                     }
2899                   else if (lex_match_id (lexer, "CATEGORIES"))
2900                     {
2901                       lex_match (lexer, T_EQUALS);
2902                       if (lex_match_id (lexer, "ALLVISIBLE"))
2903                         t->chisq->all_visible = true;
2904                       else if (lex_match_id (lexer, "SUBTOTALS"))
2905                         t->chisq->all_visible = false;
2906                       else
2907                         {
2908                           lex_error_expecting (lexer,
2909                                                "ALLVISIBLE", "SUBTOTALS");
2910                           goto error;
2911                         }
2912                     }
2913                   else
2914                     {
2915                       lex_error_expecting (lexer, "TYPE", "ALPHA",
2916                                            "INCLUDEMRSETS", "CATEGORIES");
2917                       goto error;
2918                     }
2919                 }
2920               while (lex_token (lexer) != T_SLASH
2921                      && lex_token (lexer) != T_ENDCMD);
2922             }
2923           else if (lex_match_id (lexer, "COMPARETEST"))
2924             {
2925               if (!t->pairwise)
2926                 {
2927                   t->pairwise = xmalloc (sizeof *t->pairwise);
2928                   *t->pairwise = (struct ctables_pairwise) {
2929                     .type = PROP,
2930                     .alpha = { .05, .05 },
2931                     .adjust = BONFERRONI,
2932                     .include_mrsets = true,
2933                     .meansvariance_allcats = true,
2934                     .all_visible = true,
2935                     .merge = false,
2936                     .apa_style = true,
2937                     .show_sig = false,
2938                   };
2939                 }
2940
2941               do
2942                 {
2943                   if (lex_match_id (lexer, "TYPE"))
2944                     {
2945                       lex_match (lexer, T_EQUALS);
2946                       if (lex_match_id (lexer, "PROP"))
2947                         t->pairwise->type = PROP;
2948                       else if (lex_match_id (lexer, "MEAN"))
2949                         t->pairwise->type = MEAN;
2950                       else
2951                         {
2952                           lex_error_expecting (lexer, "PROP", "MEAN");
2953                           goto error;
2954                         }
2955                     }
2956                   else if (lex_match_id (lexer, "ALPHA"))
2957                     {
2958                       lex_match (lexer, T_EQUALS);
2959
2960                       if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2961                         goto error;
2962                       double a0 = lex_number (lexer);
2963                       lex_get (lexer);
2964
2965                       lex_match (lexer, T_COMMA);
2966                       if (lex_is_number (lexer))
2967                         {
2968                           if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
2969                             goto error;
2970                           double a1 = lex_number (lexer);
2971                           lex_get (lexer);
2972
2973                           t->pairwise->alpha[0] = MIN (a0, a1);
2974                           t->pairwise->alpha[1] = MAX (a0, a1);
2975                         }
2976                       else
2977                         t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
2978                     }
2979                   else if (lex_match_id (lexer, "ADJUST"))
2980                     {
2981                       lex_match (lexer, T_EQUALS);
2982                       if (lex_match_id (lexer, "BONFERRONI"))
2983                         t->pairwise->adjust = BONFERRONI;
2984                       else if (lex_match_id (lexer, "BH"))
2985                         t->pairwise->adjust = BH;
2986                       else if (lex_match_id (lexer, "NONE"))
2987                         t->pairwise->adjust = 0;
2988                       else
2989                         {
2990                           lex_error_expecting (lexer, "BONFERRONI", "BH",
2991                                                "NONE");
2992                           goto error;
2993                         }
2994                     }
2995                   else if (lex_match_id (lexer, "INCLUDEMRSETS"))
2996                     {
2997                       lex_match (lexer, T_EQUALS);
2998                       if (!parse_bool (lexer, &t->pairwise->include_mrsets))
2999                         goto error;
3000                     }
3001                   else if (lex_match_id (lexer, "MEANSVARIANCE"))
3002                     {
3003                       lex_match (lexer, T_EQUALS);
3004                       if (lex_match_id (lexer, "ALLCATS"))
3005                         t->pairwise->meansvariance_allcats = true;
3006                       else if (lex_match_id (lexer, "TESTEDCATS"))
3007                         t->pairwise->meansvariance_allcats = false;
3008                       else
3009                         {
3010                           lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3011                           goto error;
3012                         }
3013                     }
3014                   else if (lex_match_id (lexer, "CATEGORIES"))
3015                     {
3016                       lex_match (lexer, T_EQUALS);
3017                       if (lex_match_id (lexer, "ALLVISIBLE"))
3018                         t->pairwise->all_visible = true;
3019                       else if (lex_match_id (lexer, "SUBTOTALS"))
3020                         t->pairwise->all_visible = false;
3021                       else
3022                         {
3023                           lex_error_expecting (lexer, "ALLVISIBLE",
3024                                                "SUBTOTALS");
3025                           goto error;
3026                         }
3027                     }
3028                   else if (lex_match_id (lexer, "MERGE"))
3029                     {
3030                       lex_match (lexer, T_EQUALS);
3031                       if (!parse_bool (lexer, &t->pairwise->merge))
3032                         goto error;
3033                     }
3034                   else if (lex_match_id (lexer, "STYLE"))
3035                     {
3036                       lex_match (lexer, T_EQUALS);
3037                       if (lex_match_id (lexer, "APA"))
3038                         t->pairwise->apa_style = true;
3039                       else if (lex_match_id (lexer, "SIMPLE"))
3040                         t->pairwise->apa_style = false;
3041                       else
3042                         {
3043                           lex_error_expecting (lexer, "APA", "SIMPLE");
3044                           goto error;
3045                         }
3046                     }
3047                   else if (lex_match_id (lexer, "SHOWSIG"))
3048                     {
3049                       lex_match (lexer, T_EQUALS);
3050                       if (!parse_bool (lexer, &t->pairwise->show_sig))
3051                         goto error;
3052                     }
3053                   else
3054                     {
3055                       lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3056                                            "INCLUDEMRSETS", "MEANSVARIANCE",
3057                                            "CATEGORIES", "MERGE", "STYLE",
3058                                            "SHOWSIG");
3059                       goto error;
3060                     }
3061                 }
3062               while (lex_token (lexer) != T_SLASH
3063                      && lex_token (lexer) != T_ENDCMD);
3064             }
3065           else
3066             {
3067               lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3068                                    "CRITERIA", "CATEGORIES", "TITLES",
3069                                    "SIGTEST", "COMPARETEST");
3070               goto error;
3071             }
3072         }
3073
3074       if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
3075         {
3076           msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3077           goto error;
3078         }
3079
3080     }
3081   while (lex_token (lexer) != T_ENDCMD);
3082
3083   bool ok = ctables_execute (ds, ct);
3084   ctables_destroy (ct);
3085   return ok ? CMD_SUCCESS : CMD_FAILURE;
3086
3087 error:
3088   ctables_destroy (ct);
3089   return CMD_FAILURE;
3090 }
3091