From: John Darrington Date: Mon, 6 Oct 2008 03:02:49 +0000 (+0800) Subject: Allow hyphens in {sub}command names. X-Git-Tag: v0.7.1~50^2~40 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff46735d4c81b49fa17c8bd26f3f5dd7bde9a15d;p=pspp-builds.git Allow hyphens in {sub}command names. Extend the lexer so as to allow hypens to appear in the identifiers which of subcommands. --- diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 1c9542d7..5b24522f 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -1304,3 +1304,28 @@ lex_tokstr (const struct lexer *lexer) { return &lexer->tokstr; } + +/* If the lexer is positioned at the (pseudo)identifier S, which + may contain a hyphen ('-'), skips it and returns true. Each + half of the identifier may be abbreviated to its first three + letters. + Otherwise, returns false. */ +bool +lex_match_hyphenated_word (struct lexer *lexer, const char *s) +{ + const char *hyphen = strchr (s, '-'); + if (hyphen == NULL) + return lex_match_id (lexer, s); + else if (lexer->token != T_ID + || !lex_id_match (ss_buffer (s, hyphen - s), ss_cstr (lexer->tokid)) + || lex_look_ahead (lexer) != '-') + return false; + else + { + lex_get (lexer); + lex_force_match (lexer, '-'); + lex_force_match_id (lexer, hyphen + 1); + return true; + } +} + diff --git a/src/language/lexer/lexer.h b/src/language/lexer/lexer.h index 53732b49..9e5d09ae 100644 --- a/src/language/lexer/lexer.h +++ b/src/language/lexer/lexer.h @@ -55,6 +55,8 @@ bool lex_match (struct lexer *, int); bool lex_match_id (struct lexer *, const char *); bool lex_match_id_n (struct lexer *, const char *, size_t n); bool lex_match_int (struct lexer *, int); +bool lex_match_hyphenated_word (struct lexer *lexer, const char *s); + /* Forcible matching functions. */ bool lex_force_match (struct lexer *, int); diff --git a/src/language/lexer/q2c.c b/src/language/lexer/q2c.c index d14c69d0..3e23390f 100644 --- a/src/language/lexer/q2c.c +++ b/src/language/lexer/q2c.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2008 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 @@ -357,6 +357,44 @@ dump_token (void) } #endif /* DUMP_TOKENS */ + +const char hyphen_proxy = '_'; + +static void +id_cpy (char **cp) +{ + char *dest = tokstr; + char *src = *cp; + + while (*src == '_' || *src == '-' || isalnum ((unsigned char) *src)) + { + *dest++ = *src == '-' ? hyphen_proxy :toupper ((unsigned char) (*src)); + src++; + } + + *cp = src; + *dest++ = '\0'; +} + +static const char * +unmunge (const char *s) +{ + char *dest = xmalloc (strlen (s)); + char *d = dest; + + while (*s) + { + if (*s == hyphen_proxy) + *d = '-'; + else + *d = *s; + s++; + d++; + } + + return dest; +} + /* Reads a token from the input file. */ static int lex_get (void) @@ -398,9 +436,8 @@ lex_get (void) { char *dest = tokstr; token = T_ID; - while (*cp == '_' || isalnum ((unsigned char) *cp)) - *dest++ = toupper ((unsigned char) (*cp++)); - *dest++ = '\0'; + + id_cpy (&cp); } else token = *cp++; @@ -1374,7 +1411,11 @@ make_match (const char *t) else if (isdigit ((unsigned char) t[0])) sprintf (s, "lex_match_int (lexer, %s)", t); else - sprintf (s, "lex_match_id (lexer, \"%s\")", t); + { + char *c = unmunge (t); + sprintf (s, "lex_match_hyphenated_word (lexer, \"%s\")", c); + free (c); + } return s; }