PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
c_keywords.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * c_keywords.c
4  * lexical token lookup for reserved words in postgres embedded SQL
5  *
6  * src/interfaces/ecpg/preproc/c_keywords.c
7  *
8  *-------------------------------------------------------------------------
9  */
10 #include "postgres_fe.h"
11 
12 #include <ctype.h>
13 
14 #include "extern.h"
15 #include "preproc.h"
16 
17 /*
18  * List of (keyword-name, keyword-token-value) pairs.
19  *
20  * !!WARNING!!: This list must be sorted, because binary
21  * search is used to locate entries.
22  */
23 static const ScanKeyword ScanCKeywords[] = {
24  /* name, value, category */
25 
26  /*
27  * category is not needed in ecpg, it is only here so we can share the
28  * data structure with the backend
29  */
30  {"VARCHAR", VARCHAR, 0},
31  {"auto", S_AUTO, 0},
32  {"bool", SQL_BOOL, 0},
33  {"char", CHAR_P, 0},
34  {"const", S_CONST, 0},
35  {"enum", ENUM_P, 0},
36  {"extern", S_EXTERN, 0},
37  {"float", FLOAT_P, 0},
38  {"hour", HOUR_P, 0},
39  {"int", INT_P, 0},
40  {"long", SQL_LONG, 0},
41  {"minute", MINUTE_P, 0},
42  {"month", MONTH_P, 0},
43  {"register", S_REGISTER, 0},
44  {"second", SECOND_P, 0},
45  {"short", SQL_SHORT, 0},
46  {"signed", SQL_SIGNED, 0},
47  {"static", S_STATIC, 0},
48  {"struct", SQL_STRUCT, 0},
49  {"to", TO, 0},
50  {"typedef", S_TYPEDEF, 0},
51  {"union", UNION, 0},
52  {"unsigned", SQL_UNSIGNED, 0},
53  {"varchar", VARCHAR, 0},
54  {"volatile", S_VOLATILE, 0},
55  {"year", YEAR_P, 0},
56 };
57 
58 
59 /*
60  * Do a binary search using plain strcmp() comparison. This is much like
61  * ScanKeywordLookup(), except we want case-sensitive matching.
62  */
63 const ScanKeyword *
65 {
66  const ScanKeyword *low = &ScanCKeywords[0];
67  const ScanKeyword *high = &ScanCKeywords[lengthof(ScanCKeywords) - 1];
68 
69  while (low <= high)
70  {
71  const ScanKeyword *middle;
72  int difference;
73 
74  middle = low + (high - low) / 2;
75  difference = strcmp(middle->name, text);
76  if (difference == 0)
77  return middle;
78  else if (difference < 0)
79  low = middle + 1;
80  else
81  high = middle - 1;
82  }
83 
84  return NULL;
85 }
#define lengthof(array)
Definition: c.h:558
static const ScanKeyword ScanCKeywords[]
Definition: c_keywords.c:23
const char * name
Definition: keywords.h:26
const ScanKeyword * ScanCKeywordLookup(const char *text)
Definition: c_keywords.c:64
Datum difference(PG_FUNCTION_ARGS)
#define NULL
Definition: c.h:226
Definition: c.h:435