12 int GetToken(tParser *File);
18 } csaReservedWords[] = {
19 {TOK_RWD_FUNCTION, "function"},
20 {TOK_RWD_INTEGER, "integer"},
21 {TOK_RWD_REAL, "string"}
26 * \brief Read a token from a buffer
27 * \param File Parser state
29 int GetToken(tParser *File)
33 if( File->NextToken != -1 ) {
34 File->Token = File->NextToken;
35 File->TokenStr = File->NextTokenStr;
36 File->TokenLen = File->NextTokenLen;
41 // Clear whitespace (including comments)
45 while( isspace( *File->CurPos ) )
49 if( *File->CurPos == '#' ) {
50 while( *File->CurPos && *File->CurPos != '\n' )
55 // C-Style Line Comments
56 if( *File->CurPos == '/' && File->CurPos[1] == '/' ) {
57 while( *File->CurPos && *File->CurPos != '\n' )
62 // C-Style Block Comments
63 if( *File->CurPos == '/' && File->CurPos[1] == '*' ) {
65 while( *File->CurPos && !(File->CurPos[-1] == '*' && *File->CurPos == '/') )
70 // No more "whitespace"
74 // Save previous tokens (speeds up PutBack and LookAhead)
75 File->LastToken = File->Token;
76 File->LastTokenStr = File->TokenStr;
77 File->LastTokenLen = File->TokenLen;
80 File->TokenStr = File->CurPos;
81 switch( *File->CurPos++ )
84 case '/': ret = TOK_DIV; break;
85 case '*': ret = TOK_MUL; break;
86 case '+': ret = TOK_PLUS; break;
88 if( *File->CurPos == '>' ) {
99 while( *File->CurPos && !(*File->CurPos == '"' && *File->CurPos != '\\') )
105 case '(': ret = TOK_PAREN_OPEN; break;
106 case ')': ret = TOK_PAREN_CLOSE; break;
107 case '{': ret = TOK_BRACE_OPEN; break;
108 case '}': ret = TOK_BRACE_CLOSE; break;
109 case '[': ret = TOK_SQUARE_OPEN; break;
110 case ']': ret = TOK_SQUARE_CLOSE; break;
113 case ';': ret = TOK_SEMICOLON; break;
114 case '.': ret = TOK_SCOPE; break;
119 if( *File->CurPos == '=' ) {
129 // \$[0-9]+ or \$[_a-zA-Z][_a-zA-Z0-9]*
133 if( isdigit( *File->CurPos ) ) {
134 while( isdigit(*File->CurPos) )
139 while( is_ident(*File->CurPos) )
145 // Default (Numbers and Identifiers)
148 if( isdigit(*File->CurPos) )
150 while( isdigit(*File->CurPos) )
157 if( is_ident(*File->CurPos) )
160 while( is_ident(*File->CurPos) || isdigit(*File->CurPos) )
172 File->TokenLen = File->CurPos - File->TokenStr;
176 void PutBack(tParser *File)
178 if( File->LastToken == -1 ) {
183 File->NextToken = File->Token;
184 File->NextTokenStr = File->TokenStr;
185 File->NextTokenLen = File->TokenLen;
187 File->Token = File->LastToken;
188 File->TokenStr = File->LastTokenStr;
189 File->TokenLen = File->LastTokenLen;
190 File->CurPos = File->NextTokenStr;
192 File->LastToken = -1;
195 int LookAhead(tParser *File)
197 int ret = GetToken(File);
204 * \brief Check for ident characters
205 * \note Matches Regex [a-zA-Z_]
207 int is_ident(char ch)
209 if('a' <= ch && ch <= 'z') return 1;
210 if('Z' <= ch && ch <= 'Z') return 1;
211 if(ch == '_') return 1;
218 if('0' <= ch && ch <= '9') return 1;
224 if(' ' == ch) return 1;
225 if('\t' == ch) return 1;
226 if('\b' == ch) return 1;
227 if('\n' == ch) return 1;
228 if('\r' == ch) return 1;