aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/preproc/c_keywords.c
blob: 9a8b94bd256d0b0311f2a6ffcc83602f3d19b5e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*-------------------------------------------------------------------------
 *
 * keywords.c
 *	  lexical token lookup for reserved words in postgres embedded SQL
 *
 *-------------------------------------------------------------------------
 */
#include <ctype.h>
#include <string.h>

#include "postgres.h"
#include "extern.h"
#include "preproc.h"

/*
 * List of (keyword-name, keyword-token-value) pairs.
 *
 * !!WARNING!!: This list must be sorted, because binary
 *		 search is used to locate entries.
 */
static ScanKeyword ScanKeywords[] = {
	/* name					value			*/
	{"VARCHAR", S_VARCHAR},
	{"auto", S_AUTO},
	{"bool", S_BOOL},
	{"char", S_CHAR},
	{"const", S_CONST},
	{"double", S_DOUBLE},
	{"enum", S_ENUM},
	{"extern", S_EXTERN},
	{"float", S_FLOAT},
	{"int", S_INT},
	{"long", S_LONG},
	{"register", S_REGISTER},
	{"short", S_SHORT},
	{"signed", S_SIGNED},
	{"static", S_STATIC},
	{"struct", S_STRUCT},
	{"union", S_UNION},
	{"unsigned", S_UNSIGNED},
	{"varchar", S_VARCHAR},
};

ScanKeyword *
ScanCKeywordLookup(char *text)
{
	ScanKeyword *low = &ScanKeywords[0];
	ScanKeyword *high = endof(ScanKeywords) - 1;
	ScanKeyword *middle;
	int			difference;

	while (low <= high)
	{
		middle = low + (high - low) / 2;
		difference = strcmp(middle->name, text);
		if (difference == 0)
			return middle;
		else if (difference < 0)
			low = middle + 1;
		else
			high = middle - 1;
	}

	return NULL;
}