source: trunk/nomc/c/token.c@ 389

Last change on this file since 389 was 380, checked in by cinc, 17 years ago

Compiler for a new programming language for use with NOM (C like).

  • Property svn:executable set to *
File size: 13.2 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2* Version: CDDL 1.0/LGPL 2.1
3*
4* The contents of this file are subject to the COMMON DEVELOPMENT AND
5* DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6* this file except in compliance with the License. You may obtain a copy of
7* the License at http://www.sun.com/cddl/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is "NOM" Netlabs Object Model
15*
16* The Initial Developer of the Original Code is
17* netlabs.org: Chris Wohlgemuth <cinc-ml@netlabs.org>.
18* Portions created by the Initial Developer are Copyright (C) 2005-2008
19* the Initial Developer. All Rights Reserved.
20*
21* Contributor(s):
22*
23* Alternatively, the contents of this file may be used under the terms of
24* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
25* case the provisions of the LGPL are applicable instead of those above. If
26* you wish to allow use of your version of this file only under the terms of
27* the LGPL, and not to allow others to use your version of this file under
28* the terms of the CDDL, indicate your decision by deleting the provisions
29* above and replace them with the notice and other provisions required by the
30* LGPL. If you do not delete the provisions above, a recipient may use your
31* version of this file under the terms of any one of the CDDL or the LGPL.
32*
33* ***** END LICENSE BLOCK ***** */
34
35#ifdef __OS2__
36# include <os2.h>
37#endif /* __OS2__ */
38
39#include <stdio.h>
40#include <string.h>
41#include "nom.h"
42
43#include "parser.h"
44
45extern GScanner *gScanner;
46
47/*
48 We need this information during parsing to decide if an identifier
49 is e.g. a typespec or just a var name.
50 */
51void setCurSymbolInfo(void)
52{
53 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
54
55 switch(gScanner->token)
56 {
57 case G_TOKEN_IDENTIFIER:
58 /* Here we have to check identifiers if they are for example types, e.g. int */
59 break;
60 default:
61 pParseInfo->uiCurSymbolKind=KIND_UNKNOWN;
62 break;
63 }
64 if(gScanner->token==G_TOKEN_SYMBOL)
65 {
66 GTokenValue value;
67 PSYMBOL pCurSymbol;
68
69 value=gScanner->value;
70 pCurSymbol=value.v_symbol;
71
72 pParseInfo->uiCurSymbolKind=pCurSymbol->uiKind;
73 }
74}
75
76guint queryCurTokensKind(void)
77{
78 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
79
80 return pParseInfo->uiCurSymbolKind;
81}
82
83/* This token is not necessarily the current token! */
84static guint getKindOfNextToken()
85{
86
87 /* Load info into gScanner */
88 g_scanner_peek_next_token(gScanner);
89
90 if(gScanner->next_token==G_TOKEN_SYMBOL)
91 {
92 GTokenValue value;
93 PSYMBOL pCurSymbol;
94
95 value=gScanner->next_value;
96 pCurSymbol=value.v_symbol;
97
98 return pCurSymbol->uiKind;
99 }
100 switch(gScanner->next_token)
101 {
102 case G_TOKEN_IDENTIFIER:
103 {
104 /* Compare strings here. Yes, that's slow... */
105 break;
106 }
107 default:
108 break;
109 }
110
111 return KIND_UNKNOWN;
112}
113
114#if 0
115guint queryNextTokensKind(void)
116{
117 return getKindFromTokenType(gScanner->next_token);
118}
119#endif
120
121/* Well, the name says all... */
122void getNextToken(void)
123{
124 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
125
126 //g_scanner_get_next_token(gScanner);
127 getNextTokenFromStream();
128 setCurSymbolInfo();
129
130 printToFile(gScanner->token);
131}
132
133
134/* This is just a wrapper for g_scanner_get_next_token(). We use it
135 to do our private house keeping. */
136GTokenType getNextTokenFromStream(void)
137{
138 GTokenType token;
139 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
140
141 //g_printf("-> %d %d\n", gScanner->position, pParseInfo->uiCurPos);
142
143 token=g_scanner_get_next_token(gScanner);
144 pParseInfo->uiTokenPos=gScanner->position;
145 pParseInfo->uiTokenLine=gScanner->line;
146
147 //printToFile(token);
148 return token;
149}
150
151gboolean matchNextKind(guint uiKind)
152{
153 if(uiKind==getKindOfNextToken())
154 {
155 getNextToken();
156 return TRUE;
157 }
158 return FALSE;
159}
160
161/* Well, the name says all... */
162gboolean matchCur(GTokenType token)
163{
164
165 if(token==gScanner->token)
166 {
167 return TRUE;
168 }
169 return FALSE;
170}
171
172/* Well, the name says all...
173
174 Note that this function advances to the next token if the
175 tokens match.
176*/
177gboolean matchNext(GTokenType token)
178{
179 if(token==g_scanner_peek_next_token(gScanner))
180 {
181 getNextToken();
182 return TRUE;
183 }
184 return FALSE;
185}
186
187/*
188 Print current token info.
189 */
190void printToken(GTokenType token)
191{
192 GTokenValue value=gScanner->value;
193 guint uiCurLine=g_scanner_cur_line(gScanner);
194 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
195
196 //g_message("line correction: %d", pParseInfo->uiLineCorrection);
197 uiCurLine+=pParseInfo->uiLineCorrection;
198
199 //return;
200
201 switch(token)
202 {
203 case G_TOKEN_SYMBOL:
204 {
205 PSYMBOL pCurSymbol=value.v_symbol;
206
207 switch(pCurSymbol->uiSymbolToken)
208 {
209 case NOMC_SYMBOL_CLASS:
210 g_message("Token: %d (NOMC_SYMBOL_CLASS)\t\t\t (LINE %d)",
211 pCurSymbol->uiSymbolToken, uiCurLine);
212 break;
213 case IDL_SYMBOL_DEFINE:
214 g_message("Token: %d (IDL_SYMBOL_DEFINE)\t\t\t", pCurSymbol->uiSymbolToken);
215 break;
216 case IDL_SYMBOL_IFDEF:
217 g_message("Token: %d (IDL_SYMBOL_IFDEF)\t\t\t", pCurSymbol->uiSymbolToken);
218 break;
219 case IDL_SYMBOL_ENDIF:
220 g_message("Token: %d (IDL_SYMBOL_ENDIF)\t\t\t", pCurSymbol->uiSymbolToken);
221 break;
222 default:
223 {
224 g_message("Token: %d (%s)\t\t\t (LINE %d)", pCurSymbol->uiSymbolToken,
225 pCurSymbol->chrSymbolName, uiCurLine);
226
227 break;
228 }
229 }/* switch */
230
231 break;
232 }
233 case G_TOKEN_IDENTIFIER:
234 g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s (LINE %d)",
235 token, value.v_identifier, uiCurLine);
236 break;
237 case G_TOKEN_STRING:
238 g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string);
239 break;
240 case G_TOKEN_LEFT_PAREN:
241 g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t\t( (LINE %d)", token, uiCurLine);
242 break;
243 case G_TOKEN_RIGHT_PAREN:
244 g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t\t) (LINE %d)", token, uiCurLine);
245 break;
246 case G_TOKEN_LEFT_CURLY:
247 g_message("Token: %d (G_TOKEN_LEFT_CURLY)\t\t\t{ (LINE %d)", token, uiCurLine);
248 break;
249 case G_TOKEN_RIGHT_CURLY:
250 g_message("Token: %d (G_TOKEN_RIGHT_CURLY)\t\t\t} (LINE %d)", token, uiCurLine);
251 break;
252 case ':':
253 g_message("Token: %d (colon)\t\t:", token);
254 break;
255 case ';':
256 g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, uiCurLine);
257 break;
258 case '#':
259 g_message("Token: %d (hash)\t\t\t#", token);
260 break;
261 case '/':
262 g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment);
263 break;
264 case G_TOKEN_COMMA:
265 g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token);
266 break;
267 case G_TOKEN_INT:
268 g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int);
269 break;
270 default:
271 {
272 g_message("Token: %d (---)\t\t\t (LINE %d)", token, uiCurLine);
273 break;
274 } /* default */
275 } /* switch */
276}
277
278
279static void moveToPos(guint uiPos)
280{
281 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
282
283 //g_printf("-> %d %d %d\n", gScanner->position, pParseInfo->uiCurPos, uiPos);
284
285 while(0!=pParseInfo->uiTokenPos && uiPos > pParseInfo->uiCurPos)
286 {
287 g_print(" ");
288 pParseInfo->uiCurPos++;
289 }
290}
291
292void moveToLine(guint toLine)
293{
294 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
295 guint uiCurLine=toLine;
296
297 if(0==pParseInfo->uiCurFileLine)
298 {
299 pParseInfo->uiCurPos=1;
300 }
301 else
302 {
303 while(pParseInfo->uiCurFileLine < uiCurLine)
304 {
305 g_printf("\n");
306 pParseInfo->uiCurFileLine++;
307 pParseInfo->uiCurPos=1;
308 }
309 }
310 pParseInfo->uiCurFileLine=uiCurLine;
311}
312
313void printToFile(GTokenType token)
314{
315 GTokenValue value=gScanner->value;
316 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
317
318 if(!pParseInfo->fPrintToken)
319 return;
320
321 //g_printf("---> %d %d\n", gScanner->position, pParseInfo->uiCurPos);
322
323 moveToLine(pParseInfo->uiTokenLine);
324
325 //g_message("Token: %d ()\t\t:", token);
326
327 switch(token)
328 {
329 case G_TOKEN_SYMBOL:
330 {
331 PSYMBOL pCurSymbol=value.v_symbol;
332
333 moveToPos(g_scanner_cur_position(gScanner)-strlen(pCurSymbol->chrSymbolName)+1);
334 //g_printf("->%s<-", pCurSymbol->chrSymbolName);
335 g_printf("%s", pCurSymbol->chrSymbolName);
336 pParseInfo->uiCurPos+=strlen(pCurSymbol->chrSymbolName);
337 break;
338 }
339 case G_TOKEN_STRING:
340 moveToPos(g_scanner_cur_position(gScanner)-strlen(value.v_string)-1);
341
342 g_print("\"%s\"", value.v_string);
343 pParseInfo->uiCurPos+=strlen(value.v_string)+2;
344 break;
345 case G_TOKEN_INT:
346 {
347 gchar tmp[100];
348
349 g_snprintf(tmp, sizeof(tmp), "%d", value.v_int);
350
351 moveToPos(g_scanner_cur_position(gScanner)-strlen(tmp)+1);
352 g_print("%d", value.v_int);
353 pParseInfo->uiCurPos+=strlen(tmp);
354
355 break;
356 }
357 case G_TOKEN_IDENTIFIER:
358 moveToPos(g_scanner_cur_position(gScanner)-strlen(value.v_identifier)+1);
359
360 g_print("%s", value.v_identifier);
361 //g_print("-->%s<--", value.v_identifier);
362 pParseInfo->uiCurPos+=strlen(value.v_identifier);
363 break;
364
365 //case '.':
366 //g_message("Token: %d (.)\t\t:", token);
367
368 default:
369 {
370 moveToPos(g_scanner_cur_position(gScanner));
371 g_print("%c", token);
372 pParseInfo->uiCurPos++;
373 break;
374 } /* default */
375 } /* switch */
376}
377
378
379#if 0
380static void insertSpaces(guint uiPos)
381{
382 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
383
384 //g_printf("-> %d %d %d\n", gScanner->position, pParseInfo->uiCurPos, uiPos);
385
386 while(uiPos > pParseInfo->uiCurPos)
387 {
388 g_print(" ");
389 pParseInfo->uiCurPos++;
390 }
391}
392
393void printToFile(GTokenType token)
394{
395 GTokenValue value=gScanner->value;
396 guint uiCurLine=g_scanner_cur_line(gScanner);
397 PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
398
399 return;
400
401 if(!pParseInfo->fPrintToken)
402 return;
403
404 if(0==pParseInfo->uiCurFileLine)
405 {
406 pParseInfo->uiCurPos=1;
407 }
408 else
409 {
410 while(pParseInfo->uiCurFileLine < uiCurLine)
411 {
412 g_printf("\n");
413 pParseInfo->uiCurFileLine++;
414 pParseInfo->uiCurPos=1;
415 }
416 }
417 pParseInfo->uiCurFileLine=uiCurLine;
418
419 //g_printf("-> %d %d\n", gScanner->position, pParseInfo->uiCurPos);
420
421 //g_printf("-> %d %d\n", g_scanner_cur_position(gScanner), pParseInfo->uiCurPos);
422
423 // uiCurLine+=pParseInfo->uiLineCorrection;
424 //g_printf("-> %d", gScanner->position);
425
426 switch(token)
427 {
428 case G_TOKEN_SYMBOL:
429 {
430 PSYMBOL pCurSymbol=value.v_symbol;
431
432
433 insertSpaces(g_scanner_cur_position(gScanner)-strlen(pCurSymbol->chrSymbolName)+1);
434
435 g_printf("%s", pCurSymbol->chrSymbolName);
436
437 // pParseInfo->uiCurPos+=strlen(pCurSymbol->chrSymbolName);
438 break;
439 }
440 case G_TOKEN_STRING:
441 insertSpaces(g_scanner_cur_position(gScanner)-strlen(value.v_string)-1);
442
443 g_print("\"%s\"", value.v_string);
444 pParseInfo->uiCurPos+=strlen(value.v_string);
445 break;
446 case G_TOKEN_INT:
447 insertSpaces(g_scanner_cur_position(gScanner));
448 g_print("%d", value.v_int);
449 break;
450 case G_TOKEN_IDENTIFIER:
451 insertSpaces(g_scanner_cur_position(gScanner)-strlen(value.v_identifier)+1);
452
453 g_print("%s", value.v_identifier);
454 pParseInfo->uiCurPos+=strlen(value.v_identifier);
455 break;
456
457#if 0
458 case G_TOKEN_IDENTIFIER:
459 g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s (LINE %d)",
460 token, value.v_identifier, uiCurLine);
461 break;
462 case G_TOKEN_STRING:
463 g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string);
464 break;
465 case G_TOKEN_LEFT_PAREN:
466 g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t\t( (LINE %d)", token, uiCurLine);
467 break;
468 case G_TOKEN_RIGHT_PAREN:
469 g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t\t) (LINE %d)", token, uiCurLine);
470 break;
471 case G_TOKEN_LEFT_CURLY:
472 g_message("Token: %d (G_TOKEN_LEFT_CURLY)\t\t\t{ (LINE %d)", token, uiCurLine);
473 break;
474 case G_TOKEN_RIGHT_CURLY:
475 g_message("Token: %d (G_TOKEN_RIGHT_CURLY)\t\t\t} (LINE %d)", token, uiCurLine);
476 break;
477 case ':':
478 g_message("Token: %d (colon)\t\t:", token);
479 break;
480 case ';':
481 g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, uiCurLine);
482 break;
483 case '#':
484 g_message("Token: %d (hash)\t\t\t#", token);
485 break;
486 case '/':
487 g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment);
488 break;
489 case G_TOKEN_COMMA:
490 g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token);
491 break;
492 case G_TOKEN_INT:
493 g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int);
494 break;
495#endif
496 default:
497 {
498 insertSpaces(g_scanner_cur_position(gScanner));
499 g_print("%c", token);
500 pParseInfo->uiCurPos++;
501 break;
502 } /* default */
503
504 } /* switch */
505 //fprintf(fh, "#define %s_%s(nomSelf, ", pif->chrName, pm->chrName);
506}
507#endif
Note: See TracBrowser for help on using the repository browser.