Categorical value cache added
[pspp-builds.git] / src / lex-def.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 /* 
21    This file is concerned with the definition of the PSPP syntax, NOT the 
22    action of scanning/parsing code .
23 */
24
25 #include <config.h>
26 #include "lex-def.h"
27
28
29 #include <assert.h>
30 #include <string.h>
31
32
33 /* Table of keywords. */
34 const char *keywords[T_N_KEYWORDS + 1] = 
35   {
36     "AND", "OR", "NOT",
37     "EQ", "GE", "GT", "LE", "LT", "NE",
38     "ALL", "BY", "TO", "WITH",
39     NULL,
40   };
41
42
43
44 /* Comparing identifiers. */
45
46 /* Keywords match if one of the following is true: KW and TOK are
47    identical (except for differences in case), or TOK is at least 3
48    characters long and those characters are identical to KW.  KW_LEN
49    is the length of KW, TOK_LEN is the length of TOK. */
50 int
51 lex_id_match_len (const char *kw, size_t kw_len,
52                   const char *tok, size_t tok_len)
53 {
54   size_t i = 0;
55
56   assert (kw && tok);
57   for (;;)
58     {
59       if (i == kw_len && i == tok_len)
60         return 1;
61       else if (i == tok_len)
62         return i >= 3;
63       else if (i == kw_len)
64         return 0;
65       else if (toupper ((unsigned char) kw[i])
66                != toupper ((unsigned char) tok[i]))
67         return 0;
68
69       i++;
70     }
71 }
72
73 /* Same as lex_id_match_len() minus the need to pass in the lengths. */
74 int
75 lex_id_match (const char *kw, const char *tok)
76 {
77   return lex_id_match_len (kw, strlen (kw), tok, strlen (tok));
78 }
79
80
81
82 /* Returns the proper token type, either T_ID or a reserved keyword
83    enum, for ID[], which must contain LEN characters. */
84 int
85 lex_id_to_token (const char *id, size_t len)
86 {
87   const char **kwp;
88
89   if (len < 2 || len > 4)
90     return T_ID;
91   
92   for (kwp = keywords; *kwp; kwp++)
93     if (!strcasecmp (*kwp, id))
94       return T_FIRST_KEYWORD + (kwp - keywords);
95
96   return T_ID;
97 }
98 \f