+ size_t n_vars = dict_get_n_vars (dataset_dict (ds));
+ enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
+ for (size_t i = 0; n_vars; i++)
+ vlabels[i] = CTVL_DEFAULT;
+
+ struct ctables *ct = xmalloc (sizeof *ct);
+ *ct = (struct ctables) {
+ .look = pivot_table_look_unshare (pivot_table_look_ref (
+ pivot_table_look_get_default ())),
+ .vlabels = vlabels,
+ .hide_threshold = 5,
+ };
+
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+
+ while (!lex_match_id (lexer, "TABLE"))
+ {
+ if (lex_match_id (lexer, "FORMAT"))
+ {
+ double widths[2] = { SYSMIS, SYSMIS };
+ double units_per_inch = 72.0;
+
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "MINCOLWIDTH"))
+ {
+ if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MAXCOLWIDTH"))
+ {
+ if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "UNITS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "POINTS"))
+ units_per_inch = 72.0;
+ else if (lex_match_id (lexer, "INCHES"))
+ units_per_inch = 1.0;
+ else if (lex_match_id (lexer, "CM"))
+ units_per_inch = 2.54;
+ else
+ {
+ lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "EMPTY"))
+ {
+ free (ct->zero);
+ ct->zero = NULL;
+
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ZERO"))
+ {
+ /* Nothing to do. */
+ }
+ else if (lex_match_id (lexer, "BLANK"))
+ ct->zero = xstrdup ("");
+ else if (lex_force_string (lexer))
+ {
+ ct->zero = ss_xstrdup (lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ else
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MISSING"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_string (lexer))
+ goto error;
+
+ free (ct->missing);
+ ct->missing = (strcmp (lex_tokcstr (lexer), ".")
+ ? ss_xstrdup (lex_tokss (lexer))
+ : NULL);
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
+ "UNITS", "EMPTY", "MISSING");
+ goto error;
+ }
+ }
+
+ if (widths[0] != SYSMIS && widths[1] != SYSMIS
+ && widths[0] > widths[1])
+ {
+ msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
+ goto error;
+ }
+
+ for (size_t i = 0; i < 2; i++)
+ if (widths[i] != SYSMIS)
+ {
+ int *wr = ct->look->width_ranges[TABLE_HORZ];
+ wr[i] = widths[i] / units_per_inch * 96.0;
+ if (wr[0] > wr[1])
+ wr[!i] = wr[i];
+ }
+ }
+ else if (lex_match_id (lexer, "VLABELS"))
+ {
+ if (!lex_force_match_id (lexer, "VARIABLES"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ struct variable **vars;
+ size_t n_vars;
+ if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
+ PV_NO_SCRATCH))
+ goto error;
+
+ if (!lex_force_match_id (lexer, "DISPLAY"))
+ {
+ free (vars);
+ goto error;
+ }
+ lex_match (lexer, T_EQUALS);
+
+ enum ctables_vlabel vlabel;
+ if (lex_match_id (lexer, "DEFAULT"))
+ vlabel = CTVL_DEFAULT;
+ else if (lex_match_id (lexer, "NAME"))
+ vlabel = CTVL_NAME;
+ else if (lex_match_id (lexer, "LABEL"))
+ vlabel = CTVL_LABEL;
+ else if (lex_match_id (lexer, "BOTH"))
+ vlabel = CTVL_BOTH;
+ else if (lex_match_id (lexer, "NONE"))
+ vlabel = CTVL_NONE;
+ else
+ {
+ lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
+ "BOTH", "NONE");
+ free (vars);
+ goto error;
+ }
+
+ for (size_t i = 0; i < n_vars; i++)
+ ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
+ free (vars);
+ }
+ else if (lex_match_id (lexer, "MRSETS"))
+ {
+ if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "SMISSING"))
+ {
+ if (lex_match_id (lexer, "VARIABLE"))
+ ct->smissing_listwise = false;
+ else if (lex_match_id (lexer, "LISTWISE"))
+ ct->smissing_listwise = true;
+ else
+ {
+ lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
+ goto error;
+ }
+ }
+ /* XXX PCOMPUTE */
+ else if (lex_match_id (lexer, "WEIGHT"))
+ {
+ if (!lex_force_match_id (lexer, "VARIABLE"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ ct->base_weight = parse_variable (lexer, dataset_dict (ds));
+ if (!ct->base_weight)
+ goto error;
+ }
+ else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
+ {
+ if (!lex_force_match_id (lexer, "COUNT"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
+ goto error;
+ ct->hide_threshold = lex_integer (lexer);
+ lex_get (lexer);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
+ "SMISSING", "PCOMPUTE", "PPROPERTIES",
+ "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
+ goto error;
+ }
+
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+ }
+
+ size_t allocated_tables = 0;
+ do
+ {
+ if (ct->n_tables >= allocated_tables)
+ ct->tables = x2nrealloc (ct->tables, &allocated_tables,
+ sizeof *ct->tables);
+
+ struct ctables_table *t = &ct->tables[ct->n_tables++];
+ *t = (struct ctables_table) {
+ .slabels_position = PIVOT_AXIS_COLUMN,
+ .slabels_visible = true,
+ .row_labels = CTLP_NORMAL,
+ .col_labels = CTLP_NORMAL,
+ .cilevel = 95,
+ };
+
+ lex_match (lexer, T_EQUALS);
+ if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
+ goto error;
+
+ if (lex_match (lexer, T_BY))
+ {
+ if (!ctables_axis_parse (lexer, dataset_dict (ds),
+ ct, t, PIVOT_AXIS_COLUMN))
+ goto error;
+
+ if (lex_match (lexer, T_BY))
+ {
+ if (!ctables_axis_parse (lexer, dataset_dict (ds),
+ ct, t, PIVOT_AXIS_LAYER))
+ goto error;
+ }
+ }
+ if (!lex_force_match (lexer, T_SLASH))
+ goto error;
+
+ /* XXX Validate axes. */
+ while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
+ {
+ if (lex_match_id (lexer, "SLABELS"))
+ {
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "POSITION"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "COLUMN"))
+ t->slabels_position = PIVOT_AXIS_COLUMN;
+ else if (lex_match_id (lexer, "ROW"))
+ t->slabels_position = PIVOT_AXIS_ROW;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->slabels_position = PIVOT_AXIS_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "COLUMN", "ROW",
+ "LAYER");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "VISIBLE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->slabels_visible))
+ goto error;
+ }
+ else
+ {
+ lex_error_expecting (lexer, "POSITION", "VISIBLE");
+ goto error;
+ }
+ }
+ }
+ else if (lex_match_id (lexer, "CLABELS"))
+ {
+ while (lex_token (lexer) != T_SLASH)
+ {
+ if (lex_match_id (lexer, "AUTO"))
+ t->row_labels = t->col_labels = CTLP_NORMAL;
+ else if (lex_match_id (lexer, "ROWLABELS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "OPPOSITE"))
+ t->row_labels = CTLP_OPPOSITE;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->row_labels = CTLP_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "COLLABELS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "OPPOSITE"))
+ t->col_labels = CTLP_OPPOSITE;
+ else if (lex_match_id (lexer, "LAYER"))
+ t->col_labels = CTLP_LAYER;
+ else
+ {
+ lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+ goto error;
+ }
+ }
+ else
+ {
+ lex_error_expecting (lexer, "AUTO", "ROWLABELS",
+ "COLLABELS");
+ goto error;
+ }
+ }
+ }
+ else if (lex_match_id (lexer, "CRITERIA"))
+ {
+ if (!lex_force_match_id (lexer, "CILEVEL"))
+ goto error;
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
+ goto error;
+ t->cilevel = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "TITLES"))
+ {
+ do
+ {
+ char **textp;
+ if (lex_match_id (lexer, "CAPTION"))
+ textp = &t->caption;
+ else if (lex_match_id (lexer, "CORNER"))
+ textp = &t->corner;
+ else if (lex_match_id (lexer, "TITLE"))
+ textp = &t->title;
+ else
+ {
+ lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
+ goto error;
+ }
+ lex_match (lexer, T_EQUALS);
+
+ struct string s = DS_EMPTY_INITIALIZER;
+ while (lex_is_string (lexer))
+ {
+ if (!ds_is_empty (&s))
+ ds_put_byte (&s, ' ');
+ ds_put_substring (&s, lex_tokss (lexer));
+ lex_get (lexer);
+ }
+ free (*textp);
+ *textp = ds_steal_cstr (&s);
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else if (lex_match_id (lexer, "SIGTEST"))
+ {
+ if (!t->chisq)
+ {
+ t->chisq = xmalloc (sizeof *t->chisq);
+ *t->chisq = (struct ctables_chisq) {
+ .alpha = .05,
+ .include_mrsets = true,
+ .all_visible = true,
+ };
+ }
+
+ do
+ {
+ if (lex_match_id (lexer, "TYPE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_match_id (lexer, "CHISQUARE"))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "ALPHA"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
+ goto error;
+ t->chisq->alpha = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (parse_bool (lexer, &t->chisq->include_mrsets))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "CATEGORIES"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLVISIBLE"))
+ t->chisq->all_visible = true;
+ else if (lex_match_id (lexer, "SUBTOTALS"))
+ t->chisq->all_visible = false;
+ else
+ {
+ lex_error_expecting (lexer,
+ "ALLVISIBLE", "SUBTOTALS");
+ goto error;
+ }
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TYPE", "ALPHA",
+ "INCLUDEMRSETS", "CATEGORIES");
+ goto error;
+ }
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else if (lex_match_id (lexer, "COMPARETEST"))
+ {
+ if (!t->pairwise)
+ {
+ t->pairwise = xmalloc (sizeof *t->pairwise);
+ *t->pairwise = (struct ctables_pairwise) {
+ .type = PROP,
+ .alpha = { .05, .05 },
+ .adjust = BONFERRONI,
+ .include_mrsets = true,
+ .meansvariance_allcats = true,
+ .all_visible = true,
+ .merge = false,
+ .apa_style = true,
+ .show_sig = false,
+ };
+ }
+
+ do
+ {
+ if (lex_match_id (lexer, "TYPE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "PROP"))
+ t->pairwise->type = PROP;
+ else if (lex_match_id (lexer, "MEAN"))
+ t->pairwise->type = MEAN;
+ else
+ {
+ lex_error_expecting (lexer, "PROP", "MEAN");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "ALPHA"))
+ {
+ lex_match (lexer, T_EQUALS);
+
+ if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+ goto error;
+ double a0 = lex_number (lexer);
+ lex_get (lexer);
+
+ lex_match (lexer, T_COMMA);
+ if (lex_is_number (lexer))
+ {
+ if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+ goto error;
+ double a1 = lex_number (lexer);
+ lex_get (lexer);
+
+ t->pairwise->alpha[0] = MIN (a0, a1);
+ t->pairwise->alpha[1] = MAX (a0, a1);
+ }
+ else
+ t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
+ }
+ else if (lex_match_id (lexer, "ADJUST"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "BONFERRONI"))
+ t->pairwise->adjust = BONFERRONI;
+ else if (lex_match_id (lexer, "BH"))
+ t->pairwise->adjust = BH;
+ else if (lex_match_id (lexer, "NONE"))
+ t->pairwise->adjust = 0;
+ else
+ {
+ lex_error_expecting (lexer, "BONFERRONI", "BH",
+ "NONE");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->include_mrsets))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "MEANSVARIANCE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLCATS"))
+ t->pairwise->meansvariance_allcats = true;
+ else if (lex_match_id (lexer, "TESTEDCATS"))
+ t->pairwise->meansvariance_allcats = false;
+ else
+ {
+ lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "CATEGORIES"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "ALLVISIBLE"))
+ t->pairwise->all_visible = true;
+ else if (lex_match_id (lexer, "SUBTOTALS"))
+ t->pairwise->all_visible = false;
+ else
+ {
+ lex_error_expecting (lexer, "ALLVISIBLE",
+ "SUBTOTALS");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "MERGE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->merge))
+ goto error;
+ }
+ else if (lex_match_id (lexer, "STYLE"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "APA"))
+ t->pairwise->apa_style = true;
+ else if (lex_match_id (lexer, "SIMPLE"))
+ t->pairwise->apa_style = false;
+ else
+ {
+ lex_error_expecting (lexer, "APA", "SIMPLE");
+ goto error;
+ }
+ }
+ else if (lex_match_id (lexer, "SHOWSIG"))
+ {
+ lex_match (lexer, T_EQUALS);
+ if (!parse_bool (lexer, &t->pairwise->show_sig))
+ goto error;
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
+ "INCLUDEMRSETS", "MEANSVARIANCE",
+ "CATEGORIES", "MERGE", "STYLE",
+ "SHOWSIG");
+ goto error;
+ }
+ }
+ while (lex_token (lexer) != T_SLASH
+ && lex_token (lexer) != T_ENDCMD);
+ }
+ else
+ {
+ lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
+ "CRITERIA", "CATEGORIES", "TITLES",
+ "SIGTEST", "COMPARETEST");
+ goto error;
+ }
+ }
+ }
+ while (lex_token (lexer) != T_ENDCMD);
+
+ return CMD_SUCCESS;