X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdictionary%2Fnumeric.c;h=4bf1ef94a1be3be59375314ecab70f74aad04458;hb=dee5f07052d915c97330a2f9371d44e0e5333fa8;hp=41f3c79a67b8bcc3ea5b1194f770a93535f86713;hpb=f550aee00a62fe1d8baf62d83cd7efef6cc2ee92;p=pspp diff --git a/src/language/dictionary/numeric.c b/src/language/dictionary/numeric.c index 41f3c79a67..4bf1ef94a1 100644 --- a/src/language/dictionary/numeric.c +++ b/src/language/dictionary/numeric.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2010 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,17 +18,17 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "data/dataset.h" +#include "data/dictionary.h" +#include "data/variable.h" +#include "data/format.h" +#include "language/command.h" +#include "language/lexer/format-parser.h" +#include "language/lexer/lexer.h" +#include "language/lexer/variable-parser.h" +#include "libpspp/assertion.h" +#include "libpspp/message.h" +#include "libpspp/str.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -37,142 +37,132 @@ int cmd_numeric (struct lexer *lexer, struct dataset *ds) { - size_t i; - - /* Names of variables to create. */ - char **v; - size_t nv; - do { - /* Format spec for variables to create. f.type==-1 if default is to - be used. */ - struct fmt_spec f; - - if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE)) + char **v; + size_t nv; + int vars_start = lex_ofs (lexer); + if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds), + &v, &nv, PV_NO_DUPLICATE)) return CMD_FAILURE; + int vars_end = lex_ofs (lexer) - 1; + + bool ok = false; /* Get the optional format specification. */ - if (lex_match (lexer, '(')) + struct fmt_spec f = var_default_formats (0); + if (lex_match (lexer, T_LPAREN)) { if (!parse_format_specifier (lexer, &f)) - goto fail; + goto done; - if ( ! fmt_check_output (&f)) - goto fail; + char *error = fmt_check_output__ (&f); + if (error) + { + lex_next_error (lexer, -1, -1, "%s", error); + free (error); + goto done; + } if (fmt_is_string (f.type)) { char str[FMT_STRING_LEN_MAX + 1]; - msg (SE, _("Format type %s may not be used with a numeric " - "variable."), fmt_to_string (&f, str)); - goto fail; + lex_next_error (lexer, -1, -1, + _("Format type %s may not be used with a numeric " + "variable."), fmt_to_string (&f, str)); + goto done; } - if (!lex_match (lexer, ')')) + if (!lex_match (lexer, T_RPAREN)) { - msg (SE, _("`)' expected after output format.")); - goto fail; + lex_error_expecting (lexer, "`)'"); + goto done; } } - else - f.type = -1; /* Create each variable. */ - for (i = 0; i < nv; i++) + for (size_t i = 0; i < nv; i++) { - struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0); + struct variable *new_var = dict_create_var (dataset_dict (ds), + v[i], 0); if (!new_var) - msg (SE, _("There is already a variable named %s."), v[i]); + lex_ofs_error (lexer, vars_start, vars_end, + _("There is already a variable named %s."), v[i]); else - { - if (f.type != -1) - var_set_both_formats (new_var, &f); - } + var_set_both_formats (new_var, &f); } + ok = true; - /* Clean up. */ - for (i = 0; i < nv; i++) + done: + for (size_t i = 0; i < nv; i++) free (v[i]); free (v); + if (!ok) + return CMD_FAILURE; } - while (lex_match (lexer, '/')); + while (lex_match (lexer, T_SLASH)); - return lex_end_of_command (lexer); - - /* If we have an error at a point where cleanup is required, - flow-of-control comes here. */ -fail: - for (i = 0; i < nv; i++) - free (v[i]); - free (v); - return CMD_FAILURE; + return CMD_SUCCESS; } /* Parses the STRING command. */ int cmd_string (struct lexer *lexer, struct dataset *ds) { - size_t i; - - /* Names of variables to create. */ - char **v; - size_t nv; - - /* Format spec for variables to create. */ - struct fmt_spec f; - - /* Width of variables to create. */ - int width; - do { - if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE)) + char **v; + size_t nv; + int vars_start = lex_ofs (lexer); + if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds), + &v, &nv, PV_NO_DUPLICATE)) return CMD_FAILURE; + int vars_end = lex_ofs (lexer) - 1; - if (!lex_force_match (lexer, '(') - || !parse_format_specifier (lexer, &f) - || !lex_force_match (lexer, ')')) - goto fail; - if (!fmt_is_string (f.type)) - { - char str[FMT_STRING_LEN_MAX + 1]; - msg (SE, _("Format type %s may not be used with a string " - "variable."), fmt_to_string (&f, str)); - goto fail; - } - if (!fmt_check_output (&f)) - goto fail; + bool ok = false; - width = fmt_var_width (&f); + struct fmt_spec f; + if (!lex_force_match (lexer, T_LPAREN) + || !parse_format_specifier (lexer, &f)) + goto done; + + char *error = fmt_check_type_compat__ (&f, NULL, VAL_STRING); + if (!error) + error = fmt_check_output__ (&f); + if (error) + { + lex_next_error (lexer, -1, -1, "%s", error); + free (error); + goto done; + } + + if (!lex_force_match (lexer, T_RPAREN)) + goto done; /* Create each variable. */ - for (i = 0; i < nv; i++) + int width = fmt_var_width (&f); + for (size_t i = 0; i < nv; i++) { struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], width); if (!new_var) - msg (SE, _("There is already a variable named %s."), v[i]); + lex_ofs_error (lexer, vars_start, vars_end, + _("There is already a variable named %s."), v[i]); else var_set_both_formats (new_var, &f); } + ok = true; - /* Clean up. */ - for (i = 0; i < nv; i++) + done: + for (size_t i = 0; i < nv; i++) free (v[i]); free (v); + if (!ok) + return CMD_FAILURE; } - while (lex_match (lexer, '/')); - - return lex_end_of_command (lexer); + while (lex_match (lexer, T_SLASH)); - /* If we have an error at a point where cleanup is required, - flow-of-control comes here. */ -fail: - for (i = 0; i < nv; i++) - free (v[i]); - free (v); - return CMD_FAILURE; + return CMD_SUCCESS; } /* Parses the LEAVE command. */ @@ -182,13 +172,11 @@ cmd_leave (struct lexer *lexer, struct dataset *ds) struct variable **v; size_t nv; - size_t i; - if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE)) return CMD_CASCADING_FAILURE; - for (i = 0; i < nv; i++) + for (size_t i = 0; i < nv; i++) var_set_leave (v[i], true); free (v); - return lex_end_of_command (lexer); + return CMD_SUCCESS; }