+#endif
+
+
+\f
+
+/* Match a variable.
+ If the match succeeds, the variable will be placed in VAR.
+ Returns true if successful */
+static bool
+lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var)
+{
+ if (lex_token (lexer) != T_ID)
+ return false;
+
+ *var = parse_variable_const (lexer, glm->dict);
+
+ if ( *var == NULL)
+ return false;
+ return true;
+}
+
+/* An interaction is a variable followed by {*, BY} followed by an interaction */
+static bool
+parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact)
+{
+ const struct variable *v = NULL;
+ assert (iact);
+
+ switch (lex_next_token (lexer, 1))
+ {
+ case T_ENDCMD:
+ case T_SLASH:
+ case T_COMMA:
+ case T_ID:
+ case T_BY:
+ case T_ASTERISK:
+ break;
+ default:
+ return false;
+ break;
+ }
+
+ if (! lex_match_variable (lexer, glm, &v))
+ {
+ interaction_destroy (*iact);
+ *iact = NULL;
+ return false;
+ }
+
+ assert (v);
+
+ if ( *iact == NULL)
+ *iact = interaction_create (v);
+ else
+ interaction_add_variable (*iact, v);
+
+ if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY))
+ {
+ lex_error (lexer, "Interactions are not yet implemented"); return false;
+ return parse_design_interaction (lexer, glm, iact);
+ }
+
+ return true;
+}
+
+static bool
+parse_nested_variable (struct lexer *lexer, struct glm_spec *glm)
+{
+ const struct variable *v = NULL;
+ if ( ! lex_match_variable (lexer, glm, &v))
+ return false;
+
+ if (lex_match (lexer, T_LPAREN))
+ {
+ if ( ! parse_nested_variable (lexer, glm))
+ return false;
+
+ if ( ! lex_force_match (lexer, T_RPAREN))
+ return false;
+ }
+
+ lex_error (lexer, "Nested variables are not yet implemented"); return false;
+ return true;
+}
+
+/* A design term is an interaction OR a nested variable */
+static bool
+parse_design_term (struct lexer *lexer, struct glm_spec *glm)
+{
+ struct interaction *iact = NULL;
+ if (parse_design_interaction (lexer, glm, &iact))
+ {
+ /* Interaction parsing successful. Add to list of interactions */
+ glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);
+ glm->interactions[glm->n_interactions - 1] = iact;
+ return true;
+ }
+
+ if ( parse_nested_variable (lexer, glm))
+ return true;
+
+ return false;
+}
+
+
+
+/* Parse a complete DESIGN specification.
+ A design spec is a design term, optionally followed by a comma,
+ and another design spec.
+*/
+static bool
+parse_design_spec (struct lexer *lexer, struct glm_spec *glm)
+{
+ if (lex_token (lexer) == T_ENDCMD || lex_token (lexer) == T_SLASH)
+ return true;
+
+ if ( ! parse_design_term (lexer, glm))
+ return false;
+
+ lex_match (lexer, T_COMMA);
+
+ return parse_design_spec (lexer, glm);
+}
+