]> git.sur5r.net Git - cc65/blob - src/da65/scanner.c
Added capability to conver o65 object files by using the new co65 utility
[cc65] / src / da65 / scanner.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.c                                 */
4 /*                                                                           */
5 /*           Configuration file scanner for the da65 disassembler            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <ctype.h>
41
42 /* common */
43 #include "chartype.h"
44 #include "xsprintf.h"
45
46 /* ld65 */
47 #include "global.h"
48 #include "error.h"
49 #include "scanner.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Current token and attributes */
60 unsigned        CfgTok;
61 char            CfgSVal [CFG_MAX_IDENT_LEN+1];
62 long            CfgIVal;
63
64 /* Error location */
65 unsigned                CfgErrorLine;
66 unsigned                CfgErrorCol;
67
68 /* Input sources for the configuration */
69 static const char*      CfgFile         = 0;
70 static const char*      CfgBuf          = 0;
71
72 /* Other input stuff */
73 static int              C               = ' ';
74 static unsigned         InputLine       = 1;
75 static unsigned         InputCol        = 0;
76 static FILE*            InputFile       = 0;
77
78
79
80 /*****************************************************************************/
81 /*                              Error handling                               */
82 /*****************************************************************************/
83
84
85
86 void CfgWarning (const char* Format, ...)
87 /* Print a warning message adding file name and line number of the config file */
88 {
89     char Buf [512];
90     va_list ap;
91
92     va_start (ap, Format);
93     xvsprintf (Buf, sizeof (Buf), Format, ap);
94     va_end (ap);
95
96     Warning ("%s(%u): %s", CfgFile, CfgErrorLine, Buf);
97 }
98
99
100
101 void CfgError (const char* Format, ...)
102 /* Print an error message adding file name and line number of the config file */
103 {
104     char Buf [512];
105     va_list ap;
106
107     va_start (ap, Format);
108     xvsprintf (Buf, sizeof (Buf), Format, ap);
109     va_end (ap);
110
111     Error ("%s(%u): %s", CfgFile, CfgErrorLine, Buf);
112 }
113
114
115
116 /*****************************************************************************/
117 /*                                   Code                                    */
118 /*****************************************************************************/
119
120
121
122 static void NextChar (void)
123 /* Read the next character from the input file */
124 {
125     if (CfgBuf) {
126         /* Read from buffer */
127         C = (unsigned char)(*CfgBuf);
128         if (C == 0) {
129             C = EOF;
130         } else {
131             ++CfgBuf;
132         }
133     } else {
134         /* Read from the file */
135         C = getc (InputFile);
136     }
137
138     /* Count columns */
139     if (C != EOF) {
140         ++InputCol;
141     }
142
143     /* Count lines */
144     if (C == '\n') {
145         ++InputLine;
146         InputCol = 0;
147     }
148 }
149
150
151
152 static unsigned DigitVal (int C)
153 /* Return the value for a numeric digit */
154 {
155     if (isdigit (C)) {
156         return C - '0';
157     } else {
158         return toupper (C) - 'A' + 10;
159     }
160 }
161
162
163
164 void CfgNextTok (void)
165 /* Read the next token from the input stream */
166 {
167     unsigned I;
168
169
170 Again:
171     /* Skip whitespace */
172     while (isspace (C)) {
173         NextChar ();
174     }
175
176     /* Remember the current position */
177     CfgErrorLine = InputLine;
178     CfgErrorCol  = InputCol;
179
180     /* Identifier? */
181     if (C == '_' || IsAlpha (C)) {
182
183         /* Read the identifier */
184         I = 0;
185         while (C == '_' || IsAlNum (C)) {
186             if (I < CFG_MAX_IDENT_LEN) {
187                 CfgSVal [I++] = C;
188             }
189             NextChar ();
190         }
191         CfgSVal [I] = '\0';
192         CfgTok = CFGTOK_IDENT;
193         return;
194     }
195
196     /* Hex number? */
197     if (C == '$') {
198         NextChar ();
199         if (!isxdigit (C)) {
200             CfgError ("Hex digit expected");
201         }
202         CfgIVal = 0;
203         while (isxdigit (C)) {
204             CfgIVal = CfgIVal * 16 + DigitVal (C);
205             NextChar ();
206         }
207         CfgTok = CFGTOK_INTCON;
208         return;
209     }
210
211     /* Decimal number? */
212     if (isdigit (C)) {
213         CfgIVal = 0;
214         while (isdigit (C)) {
215             CfgIVal = CfgIVal * 10 + DigitVal (C);
216             NextChar ();
217         }
218         CfgTok = CFGTOK_INTCON;
219         return;
220     }
221
222     /* Other characters */
223     switch (C) {
224
225         case '{':
226             NextChar ();
227             CfgTok = CFGTOK_LCURLY;
228             break;
229
230         case '}':
231             NextChar ();
232             CfgTok = CFGTOK_RCURLY;
233             break;
234
235         case ';':
236             NextChar ();
237             CfgTok = CFGTOK_SEMI;
238             break;
239
240         case '.':
241             NextChar ();
242             CfgTok = CFGTOK_DOT;
243             break;
244
245         case ',':
246             NextChar ();
247             CfgTok = CFGTOK_COMMA;
248             break;
249
250         case '=':
251             NextChar ();
252             CfgTok = CFGTOK_EQ;
253             break;
254
255         case ':':
256             NextChar ();
257             CfgTok = CFGTOK_COLON;
258             break;
259
260         case '\"':
261             NextChar ();
262             I = 0;
263             while (C != '\"') {
264                 if (C == EOF || C == '\n') {
265                     CfgError ("Unterminated string");
266                 }
267                 if (I < CFG_MAX_IDENT_LEN) {
268                     CfgSVal [I++] = C;
269                 }
270                 NextChar ();
271             }
272             NextChar ();
273             CfgSVal [I] = '\0';
274             CfgTok = CFGTOK_STRCON;
275             break;
276
277         case '#':
278             /* Comment */
279             while (C != '\n' && C != EOF) {
280                 NextChar ();
281             }
282             if (C != EOF) {
283                 goto Again;
284             }
285             CfgTok = CFGTOK_EOF;
286             break;
287
288         case EOF:
289             CfgTok = CFGTOK_EOF;
290             break;
291
292         default:
293             CfgError ("Invalid character `%c'", C);
294
295     }
296 }
297
298
299
300 void CfgConsume (unsigned T, const char* Msg)
301 /* Skip a token, print an error message if not found */
302 {
303     if (CfgTok != T) {
304         CfgError (Msg);
305     }
306     CfgNextTok ();
307 }
308
309
310
311 void CfgConsumeLCurly (void)
312 /* Consume a left curly brace */
313 {
314     CfgConsume (CFGTOK_LCURLY, "`{' expected");
315 }
316
317
318
319 void CfgConsumeRCurly (void)
320 /* Consume a right curly brace */
321 {
322     CfgConsume (CFGTOK_RCURLY, "`}' expected");
323 }
324
325
326
327 void CfgConsumeSemi (void)
328 /* Consume a semicolon */
329 {
330     CfgConsume (CFGTOK_SEMI, "`;' expected");
331 }
332
333
334
335 void CfgConsumeColon (void)
336 /* Consume a colon */
337 {
338     CfgConsume (CFGTOK_COLON, "`:' expected");
339 }
340
341
342
343 void CfgOptionalComma (void)
344 /* Consume a comma if there is one */
345 {
346     if (CfgTok == CFGTOK_COMMA) {
347         CfgNextTok ();
348     }
349 }
350
351
352
353 void CfgOptionalAssign (void)
354 /* Consume an equal sign if there is one */
355 {
356     if (CfgTok == CFGTOK_EQ) {
357         CfgNextTok ();
358     }
359 }
360
361
362
363 void CfgAssureInt (void)
364 /* Make sure the next token is an integer */
365 {
366     if (CfgTok != CFGTOK_INTCON) {
367         CfgError ("Integer constant expected");
368     }
369 }
370
371
372
373 void CfgAssureStr (void)
374 /* Make sure the next token is a string constant */
375 {
376     if (CfgTok != CFGTOK_STRCON) {
377         CfgError ("String constant expected");
378     }
379 }
380
381
382
383 void CfgAssureIdent (void)
384 /* Make sure the next token is an identifier */
385 {
386     if (CfgTok != CFGTOK_IDENT) {
387         CfgError ("Identifier expected");
388     }
389 }
390
391
392
393 void CfgRangeCheck (long Lo, long Hi)
394 /* Check the range of CfgIVal */
395 {
396     if (CfgIVal < Lo || CfgIVal > Hi) {
397         CfgError ("Range error");
398     }
399 }
400
401
402
403 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
404 /* Map an identifier to one of the special tokens in the table */
405 {
406     unsigned I;
407
408     /* We need an identifier */
409     if (CfgTok == CFGTOK_IDENT) {
410
411         /* Make it upper case */
412         I = 0;
413         while (CfgSVal [I]) {
414             CfgSVal [I] = toupper (CfgSVal [I]);
415             ++I;
416         }
417
418         /* Linear search */
419         for (I = 0; I < Size; ++I) {
420             if (strcmp (CfgSVal, Table [I].Ident) == 0) {
421                 CfgTok = Table [I].Tok;
422                 return;
423             }
424         }
425
426     }
427
428     /* Not found or no identifier */
429     CfgError ("%s expected", Name);
430 }
431
432
433
434 void CfgBoolToken (void)
435 /* Map an identifier or integer to a boolean token */
436 {
437     static const IdentTok Booleans [] = {
438         {   "YES",      CFGTOK_TRUE     },
439         {   "NO",       CFGTOK_FALSE    },
440         {   "TRUE",     CFGTOK_TRUE     },
441         {   "FALSE",    CFGTOK_FALSE    },
442     };
443
444     /* If we have an identifier, map it to a boolean token */
445     if (CfgTok == CFGTOK_IDENT) {
446         CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
447     } else {
448         /* We expected an integer here */
449         if (CfgTok != CFGTOK_INTCON) {
450             CfgError ("Boolean value expected");
451         }
452         CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
453     }
454 }
455
456
457
458 void CfgSetName (const char* Name)
459 /* Set a name for a config file */
460 {
461     CfgFile = Name;
462 }
463
464
465
466 const char* CfgGetName (void)
467 /* Get the name of the config file */
468 {
469     return CfgFile? CfgFile : "";
470 }
471
472
473
474 void CfgSetBuf (const char* Buf)
475 /* Set a memory buffer for the config */
476 {
477     CfgBuf = Buf;
478 }
479
480
481
482 int CfgAvail (void)
483 /* Return true if we have a configuration available */
484 {
485     return CfgFile != 0 || CfgBuf != 0;
486 }
487
488
489
490 void CfgOpenInput (void)
491 /* Open the input file if we have one */
492 {
493     /* If we have a config name given, open the file, otherwise we will read
494      * from a buffer.
495      */
496     if (!CfgBuf) {
497
498         /* Open the file */
499         InputFile = fopen (CfgFile, "r");
500         if (InputFile == 0) {
501             Error ("Cannot open `%s': %s", CfgFile, strerror (errno));
502         }
503
504     }
505
506     /* Initialize variables */
507     C         = ' ';
508     InputLine = 1;
509     InputCol  = 0;
510
511     /* Start the ball rolling ... */
512     CfgNextTok ();
513 }
514
515
516
517 void CfgCloseInput (void)
518 /* Close the input file if we have one */
519 {
520     /* Close the input file if we had one */
521     if (InputFile) {
522         (void) fclose (InputFile);
523         InputFile = 0;
524     }
525 }
526
527
528
529