Skip to content
Snippets Groups Projects
keywords.c 6.61 KiB
Newer Older
/*-------------------------------------------------------------------------
 *
 *	  lexical token lookup for reserved words in postgres SQL
Bruce Momjian's avatar
Bruce Momjian committed
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
 *	  $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.87 2001/01/05 06:34:19 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <ctype.h>

Bruce Momjian's avatar
Bruce Momjian committed
#include "postgres.h"
#include "nodes/parsenodes.h"
Bruce Momjian's avatar
Bruce Momjian committed
#include "nodes/pg_list.h"
#include "parser/keywords.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[] = {
	{"abort", ABORT_TRANS},
	{"absolute", ABSOLUTE},
	{"add", ADD},
	{"after", AFTER},
	{"aggregate", AGGREGATE},
	{"all", ALL},
	{"alter", ALTER},
	{"analyse", ANALYSE}, /* British spelling */
	{"analyze", ANALYZE},
	{"and", AND},
	{"any", ANY},
	{"backward", BACKWARD},
	{"before", BEFORE},
	{"begin", BEGIN_TRANS},
	{"between", BETWEEN},
	{"binary", BINARY},
	{"both", BOTH},
	{"by", BY},
	{"case", CASE},
	{"character", CHARACTER},
	{"characteristics", CHARACTERISTICS},
	{"checkpoint", CHECKPOINT},
	{"close", CLOSE},
	{"cluster", CLUSTER},
	{"coalesce", COALESCE},
	{"comment", COMMENT},
	{"constraint", CONSTRAINT},
	{"constraints", CONSTRAINTS},
	{"copy", COPY},
	{"create", CREATE},
	{"createdb", CREATEDB},
	{"createuser", CREATEUSER},
	{"current_date", CURRENT_DATE},
	{"current_time", CURRENT_TIME},
	{"current_timestamp", CURRENT_TIMESTAMP},
	{"current_user", CURRENT_USER},
	{"database", DATABASE},
	{"dec", DEC},
	{"decimal", DECIMAL},
	{"declare", DECLARE},
	{"default", DEFAULT},
	{"deferrable", DEFERRABLE},
	{"deferred", DEFERRED},
	{"delete", DELETE},
	{"delimiters", DELIMITERS},
	{"desc", DESC},
	{"distinct", DISTINCT},
	{"do", DO},
	{"double", DOUBLE},
	{"else", ELSE},
Marc G. Fournier's avatar
 
Marc G. Fournier committed
	{"encoding", ENCODING},
Bruce Momjian's avatar
Bruce Momjian committed
	{"except", EXCEPT},
	{"execute", EXECUTE},
	{"exists", EXISTS},
	{"explain", EXPLAIN},
	{"extend", EXTEND},
	{"extract", EXTRACT},
Hiroshi Inoue's avatar
Hiroshi Inoue committed
	{"force", FORCE},
	{"forward", FORWARD},
	{"from", FROM},
	{"full", FULL},
	{"function", FUNCTION},
	{"grant", GRANT},
	{"group", GROUP},
	{"index", INDEX},
	{"inherits", INHERITS},
Bruce Momjian's avatar
Bruce Momjian committed
	{"insert", INSERT},
Bruce Momjian's avatar
Bruce Momjian committed
	{"intersect", INTERSECT},
	{"interval", INTERVAL},
	{"into", INTO},
	{"is", IS},
	{"isnull", ISNULL},
	{"isolation", ISOLATION},
	{"lancompiler", LANCOMPILER},
	{"language", LANGUAGE},
	{"leading", LEADING},
	{"left", LEFT},
	{"level", LEVEL},
	{"listen", LISTEN},
	{"load", LOAD},
	{"local", LOCAL},
	{"location", LOCATION},
Marc G. Fournier's avatar
 
Marc G. Fournier committed
	{"names", NAMES},
	{"nocreatedb", NOCREATEDB},
	{"nocreateuser", NOCREATEUSER},
	{"none", NONE},
	{"not", NOT},
	{"nothing", NOTHING},
	{"notify", NOTIFY},
	{"notnull", NOTNULL},
	{"nullif", NULLIF},
	{"numeric", NUMERIC},
	{"operator", OPERATOR},
	{"option", OPTION},
	{"or", OR},
	{"order", ORDER},
	{"owner", OWNER},
	{"password", PASSWORD},
	{"precision", PRECISION},
	{"privileges", PRIVILEGES},
	{"procedure", PROCEDURE},
	{"public", PUBLIC},
	{"references", REFERENCES},
Hiroshi Inoue's avatar
Hiroshi Inoue committed
	{"reindex", REINDEX},
	{"relative", RELATIVE},
	{"rename", RENAME},
	{"reset", RESET},
	{"returns", RETURNS},
	{"revoke", REVOKE},
	{"right", RIGHT},
	{"rollback", ROLLBACK},
	{"select", SELECT},
	{"sequence", SEQUENCE},
	{"serializable", SERIALIZABLE},
	{"session_user", SESSION_USER},
	{"set", SET},
	{"setof", SETOF},
	{"statement", STATEMENT},
	{"stdin", STDIN},
	{"stdout", STDOUT},
	{"substring", SUBSTRING},
	{"sysid", SYSID},
Bruce Momjian's avatar
Bruce Momjian committed
	{"temporary", TEMPORARY},
	{"then", THEN},
	{"timezone_hour", TIMEZONE_HOUR},
	{"timezone_minute", TIMEZONE_MINUTE},
Jan Wieck's avatar
Jan Wieck committed
	{"toast", TOAST},
	{"transaction", TRANSACTION},
	{"trigger", TRIGGER},
	{"trim", TRIM},
	{"union", UNION},
	{"unique", UNIQUE},
Marc G. Fournier's avatar
 
Marc G. Fournier committed
	{"unlisten", UNLISTEN},
	{"using", USING},
	{"vacuum", VACUUM},
	{"varying", VARYING},
	{"verbose", VERBOSE},
	{"version", VERSION},
	{"view", VIEW},
	{"when", WHEN},
	{"where", WHERE},
	{"with", WITH},
ScanKeywordLookup(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;