1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
50 typedef struct _HashEntry
54 struct _HashEntry* next;
59 HashEntry keyword_storage[100];
61 #define HASHTABLE_SIZE 17
62 HashEntry* keyword_table[HASHTABLE_SIZE];
63 static int hash_init = 0;
65 const char* keywords[] =
67 "break", "case", "char", "const",
68 "continue", "default", "do", "double",
69 "else", "enum", "extern", "float",
70 "for", "goto", "if", "int",
71 "long", "register", "return", "short",
72 "signed", "sizeof", "static", "struct",
73 "switch", "typedef", "union", "unsigned",
74 "void", "volatile", "while", 0
78 inline unsigned calc_hash( const char* text, int len )
82 for( j = 0; j < len; j++ )
85 if( shift >= 32 ) shift -= 32;
86 hash ^= ((unsigned char*)text)[j] << shift;
92 void InitLexer( Lexer* lexer, const char* text )
100 int count[HASHTABLE_SIZE];
101 memset( count, 0, sizeof(count));
102 memset( keyword_table, 0, sizeof( keyword_table));
104 for( i = 0; keywords[i] != 0; i++ )
107 keyword_storage[i].len = len = strlen( keywords[i] );
108 keyword_storage[i].hash = calc_hash( keywords[i], len );
109 keyword_storage[i].str = keywords[i];
110 idx = keyword_storage[i].hash % HASHTABLE_SIZE;
111 keyword_storage[i].next = keyword_table[idx];
112 keyword_table[idx] = keyword_storage + i;
120 HashEntry* find_text( const char* str, int len )
122 unsigned hash = calc_hash( str, len );
123 int idx = hash % HASHTABLE_SIZE;
124 HashEntry* entry = keyword_table[idx];
128 if( entry->hash == hash &&
130 !strncmp( entry->str, str, len )) break;
137 void GetToken( Lexer* lexer, Token* token )
139 const char* text = lexer->text;
140 int pos = lexer->pos;
141 token->type = TOKEN_NORMAL;
143 while( isspace(text[pos])) pos++;
151 case '/': /* end-line comment */
152 token->type = TOKEN_COMMENT;
153 ++pos; while( text[pos] != '\n' && text[pos] != '\0' ) pos++;
156 token->type = TOKEN_COMMENT;
158 while( text[pos] != '\0' )
160 if( text[pos] == '*' && text[pos+1] == '/')
170 case '\0': token->type = TOKEN_END;
173 case '\"': token->type = TOKEN_STRING;
177 if( text[pos] == '\0' || text[pos] == '\"' || text[pos] == '\n' )
179 if( text[pos] == '\\' )
182 if( text[pos] == '\n' ) pos++;
189 if( text[pos] == '\"') pos++;
192 case '\'': token->type = TOKEN_STRING;
196 if( text[pos] == '\0' || text[pos] == '\'' || text[pos] == '\n' )
198 pos += text[pos] == '\\' ? 2 : 1;
200 if( text[pos] == '\'') pos++;
203 if( isalpha( text[pos] ) || text[pos] == '_' )
206 while( isalnum( text[pos] ) || text[pos] == '_' ) pos++;
208 if( find_text( text + token->start, pos - token->start ))
210 token->type = TOKEN_KEYWORD;
213 else if( isdigit(text[pos]) || (text[pos] == '.' && isdigit(text[pos+1])))
216 token->type = TOKEN_NUMBER;
217 pos++; while( isalnum( text[pos])) pos++;
218 if( (text[pos] == '+' || text[pos] == '-') && text[pos-1] == 'e')
220 while( isdigit(text[pos1]) || text[pos1] == '.') pos1++;
221 if( pos1 == pos - 1 )
224 while( isdigit(text[pos])) pos++;