1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/dataset.h"
20 #include "data/dictionary.h"
21 #include "data/mrset.h"
22 #include "language/command.h"
23 #include "language/lexer/format-parser.h"
24 #include "language/lexer/lexer.h"
25 #include "language/lexer/variable-parser.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/hmap.h"
28 #include "libpspp/message.h"
29 #include "output/pivot-table.h"
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) (msgid)
40 CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
41 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
42 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
43 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
47 ctables_vlabel_unique (enum ctables_vlabel vlabel)
49 /* This ensures that all of the values are unique. */
62 - unweighted summaries (U*)
63 - lower confidence limits (*.LCL)
64 - upper confidence limits (*.UCL)
65 - standard error (*.SE)
68 /* All variables. */ \
69 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
70 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
71 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
90 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
91 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
93 /* Scale variables, totals, and subtotals. */ \
94 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
103 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
104 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
105 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
108 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
109 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
110 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
115 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
118 /* Multiple response sets. */ \
119 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
120 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
139 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
140 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
142 enum ctables_summary_function
144 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
150 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
151 N_CTSF_FUNCTIONS = SUMMARIES
157 struct pivot_table_look *look;
159 /* If this is NULL, zeros are displayed using the normal print format.
160 Otherwise, this string is displayed. */
163 /* If this is NULL, missing values are displayed using the normal print
164 format. Otherwise, this string is displayed. */
167 /* Indexed by variable dictionary index. */
168 enum ctables_vlabel *vlabels;
170 bool mrsets_count_duplicates; /* MRSETS. */
171 bool smissing_listwise; /* SMISSING. */
172 struct variable *base_weight; /* WEIGHT. */
173 int hide_threshold; /* HIDESMALLCOUNTS. */
175 struct ctables_table *tables;
179 struct ctables_postcompute
181 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
182 const char *name; /* Name, without leading &. */
184 struct ctables_postcompute_expr *expr;
187 bool hide_source_cats;
190 struct ctables_postcompute_expr
192 enum ctables_postcompute_op
200 /* XXX SUBTOTAL and HSUBTOTAL */
213 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
218 XXX what about string ranges? */
221 double low; /* -DBL_MAX for LO. */
222 double high; /* DBL_MAX for HIGH. */
226 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
227 struct ctables_postcompute_expr *subs[2];
231 enum ctables_label_position
240 struct ctables_axis *axes[PIVOT_N_AXES];
242 enum pivot_axis_type slabels_position;
243 bool slabels_visible;
245 enum ctables_label_position row_labels;
246 enum ctables_label_position col_labels;
248 /* Indexed by variable dictionary index. */
249 struct ctables_categories **categories;
258 struct ctables_chisq *chisq;
259 struct ctables_pairwise *pairwise;
267 struct variable *var;
268 const struct mrset *mrset;
272 static const struct fmt_spec *
273 ctables_var_get_print_format (const struct ctables_var *var)
275 return (var->is_mrset
276 ? var_get_print_format (var->mrset->vars[0])
277 : var_get_print_format (var->var));
281 ctables_var_name (const struct ctables_var *var)
283 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
286 struct ctables_categories
290 /* Explicit categories. */
291 struct ctables_cat_value *values;
294 /* Implicit categories. */
296 bool include_missing;
297 enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
298 enum ctables_summary_function sort_func;
299 struct variable *sort_func_var;
307 /* Empty categories. */
311 struct ctables_cat_value
313 enum ctables_cat_value_type
327 double number; /* CCVT_NUMBER. */
328 char *string; /* CCVT_STRING. */
329 double range[2]; /* CCVT_RANGE. */
330 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
335 ctables_cat_value_uninit (struct ctables_cat_value *cv)
354 free (cv->subtotal_label);
359 ctables_categories_unref (struct ctables_categories *c)
364 assert (c->n_refs > 0);
368 for (size_t i = 0; i < c->n_values; i++)
369 ctables_cat_value_uninit (&c->values[i]);
371 free (c->total_label);
375 /* Chi-square test (SIGTEST). */
383 /* Pairwise comparison test (COMPARETEST). */
384 struct ctables_pairwise
386 enum { PROP, MEAN } type;
389 bool meansvariance_allcats;
391 enum { BONFERRONI = 1, BH } adjust;
415 struct ctables_var var;
417 struct ctables_summary *summaries;
419 size_t allocated_summaries;
423 struct ctables_axis *subs[2];
426 struct msg_location *loc;
429 static void ctables_axis_destroy (struct ctables_axis *);
438 enum ctables_function_availability
440 CTFA_ALL, /* Any variables. */
441 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
442 CTFA_MRSETS, /* Only multiple-response sets */
445 struct ctables_summary
447 enum ctables_summary_function function;
448 double percentile; /* CTSF_PTILE only. */
450 struct fmt_spec format; /* XXX extra CTABLES formats */
454 ctables_summary_uninit (struct ctables_summary *s)
461 parse_col_width (struct lexer *lexer, const char *name, double *width)
463 lex_match (lexer, T_EQUALS);
464 if (lex_match_id (lexer, "DEFAULT"))
466 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
468 *width = lex_number (lexer);
478 parse_bool (struct lexer *lexer, bool *b)
480 if (lex_match_id (lexer, "NO"))
482 else if (lex_match_id (lexer, "YES"))
486 lex_error_expecting (lexer, "YES", "NO");
492 static enum ctables_function_availability
493 ctables_function_availability (enum ctables_summary_function f)
495 static enum ctables_function_availability availability[] = {
496 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
501 return availability[f];
505 parse_ctables_summary_function (struct lexer *lexer,
506 enum ctables_summary_function *f)
510 enum ctables_summary_function function;
511 struct substring name;
513 static struct pair names[] = {
514 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
515 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
518 /* The .COUNT suffix may be omitted. */
519 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
520 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
521 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
522 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
523 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
524 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
525 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
529 if (!lex_force_id (lexer))
532 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
533 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
535 *f = names[i].function;
540 lex_error (lexer, _("Expecting summary function name."));
545 ctables_axis_destroy (struct ctables_axis *axis)
553 for (size_t i = 0; i < axis->n_summaries; i++)
554 ctables_summary_uninit (&axis->summaries[i]);
555 free (axis->summaries);
560 ctables_axis_destroy (axis->subs[0]);
561 ctables_axis_destroy (axis->subs[1]);
564 msg_location_destroy (axis->loc);
568 static struct ctables_axis *
569 ctables_axis_new_nonterminal (enum ctables_axis_op op,
570 struct ctables_axis *sub0,
571 struct ctables_axis *sub1,
572 struct lexer *lexer, int start_ofs)
574 struct ctables_axis *axis = xmalloc (sizeof *axis);
575 *axis = (struct ctables_axis) {
577 .subs = { sub0, sub1 },
578 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
583 struct ctables_axis_parse_ctx
586 struct dictionary *dict;
588 struct ctables_table *t;
591 static struct fmt_spec
592 ctables_summary_default_format (enum ctables_summary_function function,
593 const struct ctables_var *var)
595 static const enum ctables_format default_formats[] = {
596 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
600 switch (default_formats[function])
603 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
606 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
609 return *ctables_var_get_print_format (var);
617 ctables_summary_function_name (enum ctables_summary_function function)
619 static const char *names[] = {
620 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
624 return names[function];
628 add_summary (struct ctables_axis *axis,
629 enum ctables_summary_function function, double percentile,
630 const char *label, const struct fmt_spec *format,
631 const struct msg_location *loc)
633 if (axis->op == CTAO_VAR)
635 if (axis->n_summaries >= axis->allocated_summaries)
636 axis->summaries = x2nrealloc (axis->summaries,
637 &axis->allocated_summaries,
638 sizeof *axis->summaries);
640 const char *function_name = ctables_summary_function_name (function);
641 const char *var_name = ctables_var_name (&axis->var);
642 switch (ctables_function_availability (function))
645 if (!axis->var.is_mrset)
647 msg_at (SE, loc, _("Summary function %s applies only to multiple "
648 "response sets."), function_name);
649 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
659 _("Summary function %s applies only to scale variables."),
661 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
671 struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
672 *dst = (struct ctables_summary) {
673 .function = function,
674 .percentile = percentile,
675 .label = xstrdup (label),
676 .format = (format ? *format
677 : ctables_summary_default_format (function, &axis->var)),
683 for (size_t i = 0; i < 2; i++)
684 if (!add_summary (axis->subs[i], function, percentile, label, format,
691 static struct ctables_axis *ctables_axis_parse_stack (
692 struct ctables_axis_parse_ctx *);
695 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
696 struct ctables_var *var)
698 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
700 *var = (struct ctables_var) {
702 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
706 lex_error (lexer, _("'%s' does not name a multiple-response set "
707 "in the active file dictionary."),
708 lex_tokcstr (lexer));
716 *var = (struct ctables_var) {
718 .var = parse_variable (lexer, dict),
720 return var->var != NULL;
724 static struct ctables_axis *
725 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
727 if (lex_match (ctx->lexer, T_LPAREN))
729 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
730 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
732 ctables_axis_destroy (sub);
738 if (!lex_force_id (ctx->lexer))
741 int start_ofs = lex_ofs (ctx->lexer);
742 struct ctables_var var;
743 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
746 struct ctables_axis *axis = xmalloc (sizeof *axis);
747 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
749 /* XXX should figure out default measures by reading data */
750 axis->scale = (var.is_mrset ? false
751 : lex_match_phrase (ctx->lexer, "[S]") ? true
752 : lex_match_phrase (ctx->lexer, "[C]") ? false
753 : var_get_measure (var.var) == MEASURE_SCALE);
754 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
755 lex_ofs (ctx->lexer) - 1);
759 static struct ctables_axis *
760 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
762 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
763 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
768 int start_ofs = lex_ofs (ctx->lexer);
770 /* Parse function. */
771 enum ctables_summary_function function;
772 if (!parse_ctables_summary_function (ctx->lexer, &function))
775 /* Parse percentile. */
776 double percentile = 0;
777 if (function == CTSF_PTILE)
779 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
781 percentile = lex_number (ctx->lexer);
782 lex_get (ctx->lexer);
787 if (lex_is_string (ctx->lexer))
789 label = ss_xstrdup (lex_tokss (ctx->lexer));
790 lex_get (ctx->lexer);
792 else if (function == CTSF_PTILE)
793 label = xasprintf (_("Percentile %.2f"), percentile);
796 static const char *default_labels[] = {
797 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
801 label = xstrdup (gettext (default_labels[function]));
805 struct fmt_spec format;
806 const struct fmt_spec *formatp;
807 if (lex_token (ctx->lexer) == T_ID)
809 if (!parse_format_specifier (ctx->lexer, &format)
810 || !fmt_check_output (&format)
811 || !fmt_check_type_compat (&format, VAL_NUMERIC))
821 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
822 lex_ofs (ctx->lexer) - 1);
823 add_summary (sub, function, percentile, label, formatp, loc);
825 msg_location_destroy (loc);
827 lex_match (ctx->lexer, T_COMMA);
829 while (!lex_match (ctx->lexer, T_RBRACK));
834 ctables_axis_destroy (sub);
838 static const struct ctables_axis *
839 find_scale (const struct ctables_axis *axis)
843 else if (axis->op == CTAO_VAR)
847 assert (!axis->var.is_mrset);
855 for (size_t i = 0; i < 2; i++)
857 const struct ctables_axis *scale = find_scale (axis->subs[i]);
865 static const struct ctables_axis *
866 find_categorical_summary (const struct ctables_axis *axis)
870 else if (axis->op == CTAO_VAR)
871 return !axis->scale && axis->n_summaries ? axis : NULL;
874 for (size_t i = 0; i < 2; i++)
876 const struct ctables_axis *sum
877 = find_categorical_summary (axis->subs[i]);
885 static struct ctables_axis *
886 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
888 int start_ofs = lex_ofs (ctx->lexer);
889 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
893 while (lex_match (ctx->lexer, T_GT))
895 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
899 struct ctables_axis *nest = ctables_axis_new_nonterminal (
900 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
902 const struct ctables_axis *outer_scale = find_scale (lhs);
903 const struct ctables_axis *inner_scale = find_scale (rhs);
904 if (outer_scale && inner_scale)
906 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
907 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
908 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
909 ctables_axis_destroy (nest);
913 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
916 msg_at (SE, nest->loc,
917 _("Summaries may only be requested for categorical variables "
918 "at the innermost nesting level."));
919 msg_at (SN, outer_sum->loc,
920 _("This outer categorical variable has a summary."));
921 ctables_axis_destroy (nest);
931 static struct ctables_axis *
932 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
934 int start_ofs = lex_ofs (ctx->lexer);
935 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
939 while (lex_match (ctx->lexer, T_PLUS))
941 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
945 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
946 ctx->lexer, start_ofs);
953 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
954 struct ctables *ct, struct ctables_table *t,
955 enum pivot_axis_type a)
957 if (lex_token (lexer) == T_BY
958 || lex_token (lexer) == T_SLASH
959 || lex_token (lexer) == T_ENDCMD)
962 struct ctables_axis_parse_ctx ctx = {
968 t->axes[a] = ctables_axis_parse_stack (&ctx);
969 return t->axes[a] != NULL;
973 ctables_chisq_destroy (struct ctables_chisq *chisq)
979 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
985 ctables_table_uninit (struct ctables_table *t)
990 for (size_t i = 0; i < t->n_categories; i++)
991 ctables_categories_unref (t->categories[i]);
992 free (t->categories);
994 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
995 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
996 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1000 ctables_chisq_destroy (t->chisq);
1001 ctables_pairwise_destroy (t->pairwise);
1005 ctables_destroy (struct ctables *ct)
1010 pivot_table_look_unref (ct->look);
1014 for (size_t i = 0; i < ct->n_tables; i++)
1015 ctables_table_uninit (&ct->tables[i]);
1020 static struct ctables_cat_value
1021 ccvt_range (double low, double high)
1023 return (struct ctables_cat_value) {
1025 .range = { low, high }
1030 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1031 struct ctables_table *t)
1033 if (!lex_match_id (lexer, "VARIABLES"))
1035 lex_match (lexer, T_EQUALS);
1037 struct variable **vars;
1039 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1042 struct ctables_categories *c = xmalloc (sizeof *c);
1043 *c = (struct ctables_categories) { .n_refs = n_vars };
1044 for (size_t i = 0; i < n_vars; i++)
1046 struct ctables_categories **cp
1047 = &t->categories[var_get_dict_index (vars[i])];
1048 ctables_categories_unref (*cp);
1053 if (lex_match (lexer, T_LBRACK))
1055 size_t allocated_values = 0;
1058 if (c->n_values >= allocated_values)
1059 c->values = x2nrealloc (c->values, &allocated_values,
1062 struct ctables_cat_value *v = &c->values[c->n_values];
1063 if (lex_match_id (lexer, "OTHERNM"))
1064 v->type = CCVT_OTHERNM;
1065 else if (lex_match_id (lexer, "MISSING"))
1066 v->type = CCVT_MISSING;
1067 else if (lex_match_id (lexer, "SUBTOTAL"))
1068 *v = (struct ctables_cat_value)
1069 { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1070 else if (lex_match_id (lexer, "HSUBTOTAL"))
1071 *v = (struct ctables_cat_value)
1072 { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1073 else if (lex_match_id (lexer, "LO"))
1075 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1077 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1080 else if (lex_is_number (lexer))
1082 double number = lex_number (lexer);
1084 if (lex_match_id (lexer, "THRU"))
1086 v->type = CCVT_RANGE;
1087 v->range[0] = number;
1088 if (lex_match_id (lexer, "HI"))
1089 *v = ccvt_range (number, DBL_MAX);
1092 if (!lex_force_num (lexer))
1094 *v = ccvt_range (number, lex_number (lexer));
1099 *v = (struct ctables_cat_value) {
1100 .type = CCVT_NUMBER,
1104 else if (lex_is_string (lexer))
1106 *v = (struct ctables_cat_value) {
1107 .type = CCVT_STRING,
1108 .string = ss_xstrdup (lex_tokss (lexer)),
1114 lex_error (lexer, NULL);
1118 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1119 && lex_match (lexer, T_EQUALS))
1121 if (!lex_force_string (lexer))
1124 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1129 lex_match (lexer, T_COMMA);
1131 while (!lex_match (lexer, T_RBRACK));
1134 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1136 if (!c->n_values && lex_match_id (lexer, "ORDER"))
1138 lex_match (lexer, T_EQUALS);
1139 if (lex_match_id (lexer, "A"))
1140 c->sort_ascending = true;
1141 else if (lex_match_id (lexer, "D"))
1142 c->sort_ascending = false;
1145 lex_error_expecting (lexer, "A", "D");
1149 else if (!c->n_values && lex_match_id (lexer, "KEY"))
1151 lex_match (lexer, T_EQUALS);
1152 if (lex_match_id (lexer, "VALUE"))
1153 c->key = CTCS_VALUE;
1154 else if (lex_match_id (lexer, "LABEL"))
1155 c->key = CTCS_LABEL;
1158 c->key = CTCS_FUNCTION;
1159 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1162 if (lex_match (lexer, T_LPAREN))
1164 c->sort_func_var = parse_variable (lexer, dict);
1165 if (!c->sort_func_var)
1168 if (c->sort_func == CTSF_PTILE)
1170 lex_match (lexer, T_COMMA);
1171 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1173 c->percentile = lex_number (lexer);
1177 if (!lex_force_match (lexer, T_RPAREN))
1180 else if (ctables_function_availability (c->sort_func)
1183 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1188 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1190 lex_match (lexer, T_EQUALS);
1191 if (lex_match_id (lexer, "INCLUDE"))
1192 c->include_missing = true;
1193 else if (lex_match_id (lexer, "EXCLUDE"))
1194 c->include_missing = false;
1197 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1201 else if (lex_match_id (lexer, "TOTAL"))
1203 lex_match (lexer, T_EQUALS);
1204 if (!parse_bool (lexer, &c->show_totals))
1207 else if (lex_match_id (lexer, "LABEL"))
1209 lex_match (lexer, T_EQUALS);
1210 if (!lex_force_string (lexer))
1212 free (c->total_label);
1213 c->total_label = ss_xstrdup (lex_tokss (lexer));
1216 else if (lex_match_id (lexer, "POSITION"))
1218 lex_match (lexer, T_EQUALS);
1219 if (lex_match_id (lexer, "BEFORE"))
1220 c->totals_before = true;
1221 else if (lex_match_id (lexer, "AFTER"))
1222 c->totals_before = false;
1225 lex_error_expecting (lexer, "BEFORE", "AFTER");
1229 else if (lex_match_id (lexer, "EMPTY"))
1231 lex_match (lexer, T_EQUALS);
1232 if (lex_match_id (lexer, "INCLUDE"))
1233 c->show_empty = true;
1234 else if (lex_match_id (lexer, "EXCLUDE"))
1235 c->show_empty = false;
1238 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1245 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1246 "TOTAL", "LABEL", "POSITION", "EMPTY");
1248 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1256 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1258 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1259 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1260 for (size_t i = 0; i < n_vars; i++)
1261 vlabels[i] = CTVL_DEFAULT;
1263 struct ctables *ct = xmalloc (sizeof *ct);
1264 *ct = (struct ctables) {
1265 .look = pivot_table_look_unshare (pivot_table_look_ref (
1266 pivot_table_look_get_default ())),
1268 .hide_threshold = 5,
1271 if (!lex_force_match (lexer, T_SLASH))
1274 while (!lex_match_id (lexer, "TABLE"))
1276 if (lex_match_id (lexer, "FORMAT"))
1278 double widths[2] = { SYSMIS, SYSMIS };
1279 double units_per_inch = 72.0;
1281 while (lex_token (lexer) != T_SLASH)
1283 if (lex_match_id (lexer, "MINCOLWIDTH"))
1285 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1288 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1290 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1293 else if (lex_match_id (lexer, "UNITS"))
1295 lex_match (lexer, T_EQUALS);
1296 if (lex_match_id (lexer, "POINTS"))
1297 units_per_inch = 72.0;
1298 else if (lex_match_id (lexer, "INCHES"))
1299 units_per_inch = 1.0;
1300 else if (lex_match_id (lexer, "CM"))
1301 units_per_inch = 2.54;
1304 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1308 else if (lex_match_id (lexer, "EMPTY"))
1313 lex_match (lexer, T_EQUALS);
1314 if (lex_match_id (lexer, "ZERO"))
1316 /* Nothing to do. */
1318 else if (lex_match_id (lexer, "BLANK"))
1319 ct->zero = xstrdup ("");
1320 else if (lex_force_string (lexer))
1322 ct->zero = ss_xstrdup (lex_tokss (lexer));
1328 else if (lex_match_id (lexer, "MISSING"))
1330 lex_match (lexer, T_EQUALS);
1331 if (!lex_force_string (lexer))
1335 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1336 ? ss_xstrdup (lex_tokss (lexer))
1342 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1343 "UNITS", "EMPTY", "MISSING");
1348 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1349 && widths[0] > widths[1])
1351 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1355 for (size_t i = 0; i < 2; i++)
1356 if (widths[i] != SYSMIS)
1358 int *wr = ct->look->width_ranges[TABLE_HORZ];
1359 wr[i] = widths[i] / units_per_inch * 96.0;
1364 else if (lex_match_id (lexer, "VLABELS"))
1366 if (!lex_force_match_id (lexer, "VARIABLES"))
1368 lex_match (lexer, T_EQUALS);
1370 struct variable **vars;
1372 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1376 if (!lex_force_match_id (lexer, "DISPLAY"))
1381 lex_match (lexer, T_EQUALS);
1383 enum ctables_vlabel vlabel;
1384 if (lex_match_id (lexer, "DEFAULT"))
1385 vlabel = CTVL_DEFAULT;
1386 else if (lex_match_id (lexer, "NAME"))
1388 else if (lex_match_id (lexer, "LABEL"))
1389 vlabel = CTVL_LABEL;
1390 else if (lex_match_id (lexer, "BOTH"))
1392 else if (lex_match_id (lexer, "NONE"))
1396 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1402 for (size_t i = 0; i < n_vars; i++)
1403 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1406 else if (lex_match_id (lexer, "MRSETS"))
1408 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1410 lex_match (lexer, T_EQUALS);
1411 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1414 else if (lex_match_id (lexer, "SMISSING"))
1416 if (lex_match_id (lexer, "VARIABLE"))
1417 ct->smissing_listwise = false;
1418 else if (lex_match_id (lexer, "LISTWISE"))
1419 ct->smissing_listwise = true;
1422 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1427 else if (lex_match_id (lexer, "WEIGHT"))
1429 if (!lex_force_match_id (lexer, "VARIABLE"))
1431 lex_match (lexer, T_EQUALS);
1432 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1433 if (!ct->base_weight)
1436 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1438 if (!lex_force_match_id (lexer, "COUNT"))
1440 lex_match (lexer, T_EQUALS);
1441 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1443 ct->hide_threshold = lex_integer (lexer);
1448 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1449 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1450 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1454 if (!lex_force_match (lexer, T_SLASH))
1458 size_t allocated_tables = 0;
1461 if (ct->n_tables >= allocated_tables)
1462 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1463 sizeof *ct->tables);
1465 struct ctables_table *t = &ct->tables[ct->n_tables++];
1466 *t = (struct ctables_table) {
1467 .slabels_position = PIVOT_AXIS_COLUMN,
1468 .slabels_visible = true,
1469 .row_labels = CTLP_NORMAL,
1470 .col_labels = CTLP_NORMAL,
1471 .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1472 sizeof *t->categories),
1473 .n_categories = dict_get_n_vars (dataset_dict (ds)),
1477 lex_match (lexer, T_EQUALS);
1478 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1480 if (lex_match (lexer, T_BY))
1482 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1483 ct, t, PIVOT_AXIS_COLUMN))
1486 if (lex_match (lexer, T_BY))
1488 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1489 ct, t, PIVOT_AXIS_LAYER))
1494 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1495 && !t->axes[PIVOT_AXIS_LAYER])
1497 lex_error (lexer, _("At least one variable must be specified."));
1501 const struct ctables_axis *scales[PIVOT_N_AXES];
1502 size_t n_scales = 0;
1503 for (size_t i = 0; i < 3; i++)
1505 scales[i] = find_scale (t->axes[i]);
1511 msg (SE, _("Scale variables may appear only on one dimension."));
1512 if (scales[PIVOT_AXIS_ROW])
1513 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1514 _("This scale variable appears in the rows dimension."));
1515 if (scales[PIVOT_AXIS_COLUMN])
1516 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1517 _("This scale variable appears in the columns dimension."));
1518 if (scales[PIVOT_AXIS_LAYER])
1519 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1520 _("This scale variable appears in the layer dimension."));
1524 if (lex_token (lexer) == T_ENDCMD)
1526 if (!lex_force_match (lexer, T_SLASH))
1529 /* XXX Validate axes. */
1530 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1532 if (lex_match_id (lexer, "SLABELS"))
1534 while (lex_token (lexer) != T_SLASH)
1536 if (lex_match_id (lexer, "POSITION"))
1538 lex_match (lexer, T_EQUALS);
1539 if (lex_match_id (lexer, "COLUMN"))
1540 t->slabels_position = PIVOT_AXIS_COLUMN;
1541 else if (lex_match_id (lexer, "ROW"))
1542 t->slabels_position = PIVOT_AXIS_ROW;
1543 else if (lex_match_id (lexer, "LAYER"))
1544 t->slabels_position = PIVOT_AXIS_LAYER;
1547 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1551 else if (lex_match_id (lexer, "VISIBLE"))
1553 lex_match (lexer, T_EQUALS);
1554 if (!parse_bool (lexer, &t->slabels_visible))
1559 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1564 else if (lex_match_id (lexer, "CLABELS"))
1566 while (lex_token (lexer) != T_SLASH)
1568 if (lex_match_id (lexer, "AUTO"))
1569 t->row_labels = t->col_labels = CTLP_NORMAL;
1570 else if (lex_match_id (lexer, "ROWLABELS"))
1572 lex_match (lexer, T_EQUALS);
1573 if (lex_match_id (lexer, "OPPOSITE"))
1574 t->row_labels = CTLP_OPPOSITE;
1575 else if (lex_match_id (lexer, "LAYER"))
1576 t->row_labels = CTLP_LAYER;
1579 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1583 else if (lex_match_id (lexer, "COLLABELS"))
1585 lex_match (lexer, T_EQUALS);
1586 if (lex_match_id (lexer, "OPPOSITE"))
1587 t->col_labels = CTLP_OPPOSITE;
1588 else if (lex_match_id (lexer, "LAYER"))
1589 t->col_labels = CTLP_LAYER;
1592 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1598 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1604 else if (lex_match_id (lexer, "CRITERIA"))
1606 if (!lex_force_match_id (lexer, "CILEVEL"))
1608 lex_match (lexer, T_EQUALS);
1610 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1612 t->cilevel = lex_number (lexer);
1615 else if (lex_match_id (lexer, "CATEGORIES"))
1617 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1620 else if (lex_match_id (lexer, "TITLES"))
1625 if (lex_match_id (lexer, "CAPTION"))
1626 textp = &t->caption;
1627 else if (lex_match_id (lexer, "CORNER"))
1629 else if (lex_match_id (lexer, "TITLE"))
1633 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1636 lex_match (lexer, T_EQUALS);
1638 struct string s = DS_EMPTY_INITIALIZER;
1639 while (lex_is_string (lexer))
1641 if (!ds_is_empty (&s))
1642 ds_put_byte (&s, ' ');
1643 ds_put_substring (&s, lex_tokss (lexer));
1647 *textp = ds_steal_cstr (&s);
1649 while (lex_token (lexer) != T_SLASH
1650 && lex_token (lexer) != T_ENDCMD);
1652 else if (lex_match_id (lexer, "SIGTEST"))
1656 t->chisq = xmalloc (sizeof *t->chisq);
1657 *t->chisq = (struct ctables_chisq) {
1659 .include_mrsets = true,
1660 .all_visible = true,
1666 if (lex_match_id (lexer, "TYPE"))
1668 lex_match (lexer, T_EQUALS);
1669 if (!lex_force_match_id (lexer, "CHISQUARE"))
1672 else if (lex_match_id (lexer, "ALPHA"))
1674 lex_match (lexer, T_EQUALS);
1675 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1677 t->chisq->alpha = lex_number (lexer);
1680 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1682 lex_match (lexer, T_EQUALS);
1683 if (parse_bool (lexer, &t->chisq->include_mrsets))
1686 else if (lex_match_id (lexer, "CATEGORIES"))
1688 lex_match (lexer, T_EQUALS);
1689 if (lex_match_id (lexer, "ALLVISIBLE"))
1690 t->chisq->all_visible = true;
1691 else if (lex_match_id (lexer, "SUBTOTALS"))
1692 t->chisq->all_visible = false;
1695 lex_error_expecting (lexer,
1696 "ALLVISIBLE", "SUBTOTALS");
1702 lex_error_expecting (lexer, "TYPE", "ALPHA",
1703 "INCLUDEMRSETS", "CATEGORIES");
1707 while (lex_token (lexer) != T_SLASH
1708 && lex_token (lexer) != T_ENDCMD);
1710 else if (lex_match_id (lexer, "COMPARETEST"))
1714 t->pairwise = xmalloc (sizeof *t->pairwise);
1715 *t->pairwise = (struct ctables_pairwise) {
1717 .alpha = { .05, .05 },
1718 .adjust = BONFERRONI,
1719 .include_mrsets = true,
1720 .meansvariance_allcats = true,
1721 .all_visible = true,
1730 if (lex_match_id (lexer, "TYPE"))
1732 lex_match (lexer, T_EQUALS);
1733 if (lex_match_id (lexer, "PROP"))
1734 t->pairwise->type = PROP;
1735 else if (lex_match_id (lexer, "MEAN"))
1736 t->pairwise->type = MEAN;
1739 lex_error_expecting (lexer, "PROP", "MEAN");
1743 else if (lex_match_id (lexer, "ALPHA"))
1745 lex_match (lexer, T_EQUALS);
1747 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1749 double a0 = lex_number (lexer);
1752 lex_match (lexer, T_COMMA);
1753 if (lex_is_number (lexer))
1755 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1757 double a1 = lex_number (lexer);
1760 t->pairwise->alpha[0] = MIN (a0, a1);
1761 t->pairwise->alpha[1] = MAX (a0, a1);
1764 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
1766 else if (lex_match_id (lexer, "ADJUST"))
1768 lex_match (lexer, T_EQUALS);
1769 if (lex_match_id (lexer, "BONFERRONI"))
1770 t->pairwise->adjust = BONFERRONI;
1771 else if (lex_match_id (lexer, "BH"))
1772 t->pairwise->adjust = BH;
1773 else if (lex_match_id (lexer, "NONE"))
1774 t->pairwise->adjust = 0;
1777 lex_error_expecting (lexer, "BONFERRONI", "BH",
1782 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1784 lex_match (lexer, T_EQUALS);
1785 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
1788 else if (lex_match_id (lexer, "MEANSVARIANCE"))
1790 lex_match (lexer, T_EQUALS);
1791 if (lex_match_id (lexer, "ALLCATS"))
1792 t->pairwise->meansvariance_allcats = true;
1793 else if (lex_match_id (lexer, "TESTEDCATS"))
1794 t->pairwise->meansvariance_allcats = false;
1797 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
1801 else if (lex_match_id (lexer, "CATEGORIES"))
1803 lex_match (lexer, T_EQUALS);
1804 if (lex_match_id (lexer, "ALLVISIBLE"))
1805 t->pairwise->all_visible = true;
1806 else if (lex_match_id (lexer, "SUBTOTALS"))
1807 t->pairwise->all_visible = false;
1810 lex_error_expecting (lexer, "ALLVISIBLE",
1815 else if (lex_match_id (lexer, "MERGE"))
1817 lex_match (lexer, T_EQUALS);
1818 if (!parse_bool (lexer, &t->pairwise->merge))
1821 else if (lex_match_id (lexer, "STYLE"))
1823 lex_match (lexer, T_EQUALS);
1824 if (lex_match_id (lexer, "APA"))
1825 t->pairwise->apa_style = true;
1826 else if (lex_match_id (lexer, "SIMPLE"))
1827 t->pairwise->apa_style = false;
1830 lex_error_expecting (lexer, "APA", "SIMPLE");
1834 else if (lex_match_id (lexer, "SHOWSIG"))
1836 lex_match (lexer, T_EQUALS);
1837 if (!parse_bool (lexer, &t->pairwise->show_sig))
1842 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
1843 "INCLUDEMRSETS", "MEANSVARIANCE",
1844 "CATEGORIES", "MERGE", "STYLE",
1849 while (lex_token (lexer) != T_SLASH
1850 && lex_token (lexer) != T_ENDCMD);
1854 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
1855 "CRITERIA", "CATEGORIES", "TITLES",
1856 "SIGTEST", "COMPARETEST");
1861 while (lex_token (lexer) != T_ENDCMD);
1862 ctables_destroy (ct);
1866 ctables_destroy (ct);