From 48ec61d954cc032c773daf96851cf3b78f27b112 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 28 Oct 2012 10:16:01 +0100 Subject: [PATCH] Move function parse_design_interaction into variable-parser.c This function is (or will be) used by several procedures, so make it public. --- src/language/lexer/variable-parser.c | 62 ++++++++++++++++++++++++++++ src/language/lexer/variable-parser.h | 17 ++++++++ src/language/stats/examine.c | 19 --------- src/language/stats/glm.c | 60 --------------------------- 4 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/language/lexer/variable-parser.c b/src/language/lexer/variable-parser.c index 49d6a5deb9..c1e1c28cdf 100644 --- a/src/language/lexer/variable-parser.c +++ b/src/language/lexer/variable-parser.c @@ -37,6 +37,8 @@ #include "libpspp/str.h" #include "libpspp/stringi-set.h" +#include "math/interaction.h" + #include "gl/c-ctype.h" #include "gl/xalloc.h" @@ -873,3 +875,63 @@ var_set_create_from_array (struct variable *const *var, size_t var_cnt) return vs; } + +/* Match a variable. + If the match succeeds, the variable will be placed in VAR. + Returns true if successful */ +bool +lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var) +{ + if (lex_token (lexer) != T_ID) + return false; + + *var = parse_variable_const (lexer, dict); + + if ( *var == NULL) + return false; + return true; +} + +/* An interaction is a variable followed by {*, BY} followed by an interaction */ +bool +parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, 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, dict, &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)) + { + return parse_design_interaction (lexer, dict, iact); + } + + return true; +} + diff --git a/src/language/lexer/variable-parser.h b/src/language/lexer/variable-parser.h index 7abea0a14b..798851d1a7 100644 --- a/src/language/lexer/variable-parser.h +++ b/src/language/lexer/variable-parser.h @@ -125,5 +125,22 @@ const_var_set_destroy (struct const_var_set *vs) var_set_destroy ( (struct var_set *) vs); } +/* Match a variable. + If the match succeeds, the variable will be placed in VAR. + Returns true if successful */ +bool +lex_match_variable (struct lexer *lexer, const struct dictionary *dict, const struct variable **var); + +struct interaction; + +/* Parse an interaction. + If not successfull return false. + Otherwise, a newly created interaction will be placed in IACT. + It is the caller's responsibility to destroy this interaction. + */ +bool +parse_design_interaction (struct lexer *lexer, const struct dictionary *dict, struct interaction **iact); + + #endif /* variable-parser.h */ diff --git a/src/language/stats/examine.c b/src/language/stats/examine.c index 0d9c68585a..5d308f11ab 100644 --- a/src/language/stats/examine.c +++ b/src/language/stats/examine.c @@ -1461,25 +1461,6 @@ summary_report (const struct examine *cmd, int iact_idx) tab_submit (t); } - -/* 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 dictionary *dict, const struct variable **var) -{ - if (lex_token (lexer) != T_ID) - - return false; - - *var = parse_variable_const (lexer, dict); - - if ( *var == NULL) - return false; - return true; -} - /* Attempt to parse an interaction from LEXER */ static struct interaction * parse_interaction (struct lexer *lexer, struct examine *ex) diff --git a/src/language/stats/glm.c b/src/language/stats/glm.c index d2b3a37e86..1ded71e9f1 100644 --- a/src/language/stats/glm.c +++ b/src/language/stats/glm.c @@ -883,66 +883,6 @@ dump_matrix (const gsl_matrix * m) - -/* 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 dictionary *dict, const struct variable **var) -{ - if (lex_token (lexer) != T_ID) - return false; - - *var = parse_variable_const (lexer, 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, const struct dictionary *dict, 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, dict, &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)) - { - return parse_design_interaction (lexer, dict, iact); - } - - return true; -} - static bool parse_nested_variable (struct lexer *lexer, struct glm_spec *glm) { -- 2.30.2