Header And Logo

PostgreSQL
| The world's most advanced open source database.

c_keywords.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * c_keywords.c
00004  *    lexical token lookup for reserved words in postgres embedded SQL
00005  *
00006  * src/interfaces/ecpg/preproc/c_keywords.c
00007  *
00008  *-------------------------------------------------------------------------
00009  */
00010 #include "postgres_fe.h"
00011 
00012 #include <ctype.h>
00013 
00014 #include "extern.h"
00015 #include "preproc.h"
00016 
00017 /*
00018  * List of (keyword-name, keyword-token-value) pairs.
00019  *
00020  * !!WARNING!!: This list must be sorted, because binary
00021  *       search is used to locate entries.
00022  */
00023 static const ScanKeyword ScanCKeywords[] = {
00024     /* name, value, category */
00025 
00026     /*
00027      * category is not needed in ecpg, it is only here so we can share the
00028      * data structure with the backend
00029      */
00030     {"VARCHAR", VARCHAR, 0},
00031     {"auto", S_AUTO, 0},
00032     {"bool", SQL_BOOL, 0},
00033     {"char", CHAR_P, 0},
00034     {"const", S_CONST, 0},
00035     {"enum", ENUM_P, 0},
00036     {"extern", S_EXTERN, 0},
00037     {"float", FLOAT_P, 0},
00038     {"hour", HOUR_P, 0},
00039     {"int", INT_P, 0},
00040     {"long", SQL_LONG, 0},
00041     {"minute", MINUTE_P, 0},
00042     {"month", MONTH_P, 0},
00043     {"register", S_REGISTER, 0},
00044     {"second", SECOND_P, 0},
00045     {"short", SQL_SHORT, 0},
00046     {"signed", SQL_SIGNED, 0},
00047     {"static", S_STATIC, 0},
00048     {"struct", SQL_STRUCT, 0},
00049     {"to", TO, 0},
00050     {"typedef", S_TYPEDEF, 0},
00051     {"union", UNION, 0},
00052     {"unsigned", SQL_UNSIGNED, 0},
00053     {"varchar", VARCHAR, 0},
00054     {"volatile", S_VOLATILE, 0},
00055     {"year", YEAR_P, 0},
00056 };
00057 
00058 
00059 /*
00060  * Do a binary search using plain strcmp() comparison.  This is much like
00061  * ScanKeywordLookup(), except we want case-sensitive matching.
00062  */
00063 const ScanKeyword *
00064 ScanCKeywordLookup(const char *text)
00065 {
00066     const ScanKeyword *low = &ScanCKeywords[0];
00067     const ScanKeyword *high = &ScanCKeywords[lengthof(ScanCKeywords) - 1];
00068 
00069     while (low <= high)
00070     {
00071         const ScanKeyword *middle;
00072         int         difference;
00073 
00074         middle = low + (high - low) / 2;
00075         difference = strcmp(middle->name, text);
00076         if (difference == 0)
00077             return middle;
00078         else if (difference < 0)
00079             low = middle + 1;
00080         else
00081             high = middle - 1;
00082     }
00083 
00084     return NULL;
00085 }