~ubuntu-branches/ubuntu/lucid/gauche-c-wrapper/lucid

« back to all changes in this revision

Viewing changes to lib/c-wrapper/c-lex.scm

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2008-04-07 09:15:03 UTC
  • Revision ID: james.westby@ubuntu.com-20080407091503-wu0h414koe95kj4i
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;;
 
2
;;; C-LEX for C-PARSE
 
3
;;;
 
4
 
 
5
(define (buf-read-line)
 
6
  (let ((line (read-line (input-port) #t)))
 
7
    (cond
 
8
     ((eof-object? line)
 
9
      (let ((chars (circular-list line)))
 
10
        (rest-chars chars)
 
11
        chars))
 
12
     ((eq? (string-size line) 0)
 
13
      (lineno (+ (lineno) 1))
 
14
      (buf-read-line))
 
15
     ((string-incomplete? line)
 
16
      (lineno (+ (lineno) 1))
 
17
      (buf-read-line))
 
18
     ((eq? (string-byte-ref line 0) 35) ;; '#' = 35 (ASCII)
 
19
      (rxmatch-cond
 
20
        ((#/^\#\s+(\d+)\s+"(\S+)"/ line) (_ num file)
 
21
         (lineno (- (string->number num) 1))
 
22
         (filename file)
 
23
         (buf-read-line))
 
24
        (test (#/^\#\s*define\s(([^ \x28]+)(?:\(([^\x29]+)\))?)/ line) 
 
25
              => (lambda (match)
 
26
                   (lineno (+ (lineno) 1))
 
27
                   (enqueue! (macro-queue)
 
28
                             (list* (match 1) ;; foo(x)
 
29
                                    (cons (filename) (lineno)) ;; ("foo.h" . 123)
 
30
                                    (match 2) ;; foo
 
31
                                    (and-let* ((args (match 3)))
 
32
                                      (map string->symbol
 
33
                                           (string-split args #[\s,]))))) ;; (x)
 
34
                   (buf-read-line)))
 
35
        (else
 
36
         (lineno (+ (lineno) 1))
 
37
         (buf-read-line))))
 
38
     (else
 
39
      (lineno (+ (lineno) 1))
 
40
      (let ((chars (string->list (string-append line "\n"))))
 
41
        (rest-chars chars)
 
42
        chars)))))
 
43
 
 
44
(define (buf-read-char)
 
45
  (let ((rest (rest-chars)))
 
46
    (when (null? rest)
 
47
      (set! rest (buf-read-line)))
 
48
    (rest-chars (cdr rest))
 
49
    (car rest)))
 
50
 
 
51
(define (buf-peek-char)
 
52
  (let ((rest (rest-chars)))
 
53
    (when (null? rest)
 
54
      (set! rest (buf-read-line)))
 
55
    (car rest)))
 
56
 
 
57
(define (buf-next-char)
 
58
  (rest-chars (cdr (rest-chars))))
 
59
 
 
60
;;
 
61
(define c-keywords
 
62
  '((auto        AUTO)
 
63
    (break       BREAK)         
 
64
    (case        CASE)          
 
65
    (const       CONST)         
 
66
    (__const     CONST)         
 
67
    (continue    CONTINUE)      
 
68
    (default     DEFAULT)       
 
69
    (do          DO)            
 
70
    (else        ELSE)          
 
71
    (enum        ENUM)          
 
72
    (extern      EXTERN)        
 
73
    (for         FOR)           
 
74
    (goto        GOTO)          
 
75
    (if          IF)            
 
76
    (register    REGISTER)      
 
77
    (return      RETURN)        
 
78
    (signed      SIGNED)
 
79
    (__signed    SIGNED)
 
80
    (sizeof      SIZEOF)        
 
81
    (static      STATIC)        
 
82
    (struct      STRUCT)        
 
83
    (switch      SWITCH)        
 
84
    (typedef     TYPEDEF)       
 
85
    (union       UNION)         
 
86
    (unsigned    UNSIGNED)
 
87
    (__unsigned  UNSIGNED)
 
88
    (volatile    VOLATILE)      
 
89
    (__volatile  VOLATILE)
 
90
    (__volatile__ VOLATILE)
 
91
    (while       WHILE)
 
92
    (inline  INLINE)
 
93
    (__inline    INLINE)
 
94
    (__inline__  INLINE)
 
95
    (asm         ASM)
 
96
    (__asm       ASM)
 
97
    (__asm__     ASM)
 
98
    (__restrict  RESTRICT)
 
99
    (__restrict__  RESTRICT)
 
100
    (__extension__ EXTENSION)
 
101
    (__attribute__ ATTRIBUTE) ; ignore
 
102
    (@interface AT_INTERFACE)
 
103
    (@implementation AT_IMPLEMENTATION)
 
104
    (@end AT_END)
 
105
    (@selector AT_SELECTOR)
 
106
    (@defs AT_DEFS)
 
107
    (@encode AT_ENCODE)
 
108
    (@public AT_PUBLIC)
 
109
    (@private AT_PRIVATE)
 
110
    (@protected AT_PROTECTED)
 
111
    (@protocol AT_PROTOCOL)
 
112
    (@class AT_CLASS)
 
113
    (@compatibility_alias AT_ALIAS)
 
114
    (@throw AT_THROW)
 
115
    (@try AT_TRY)
 
116
    (@catch AT_CATCH)
 
117
    (@finally AT_FINALLY)
 
118
    (@synchronized AT_SYNCHRONIZED)
 
119
    ))
 
120
 
 
121
(define c-operators
 
122
  '((>>=         RIGHT_ASSIGN)
 
123
    (<<=         LEFT_ASSIGN)
 
124
    (+=          ADD_ASSIGN)
 
125
    (-=          SUB_ASSIGN)
 
126
    (*=          MUL_ASSIGN)
 
127
    (/=          DIV_ASSIGN)
 
128
    (%=          MOD_ASSIGN)
 
129
    (&=          AND_ASSIGN)
 
130
    (^=          XOR_ASSIGN)
 
131
    ;;(|=          OR_ASSIGN)
 
132
    (>>          RIGHT_OP)
 
133
    (<<          LEFT_OP)
 
134
    (>           >)
 
135
    (<           <)
 
136
    (++          INC_OP)
 
137
    (--          DEC_OP)
 
138
    (->          PTR_OP)
 
139
    (&&          AND_OP)
 
140
    ;;(||          OR_OP)
 
141
    (<=          LE_OP)
 
142
    (>=          GE_OP)
 
143
    (==          EQ_OP)
 
144
    (!=          NE_OP)
 
145
    (*           *)
 
146
    (/           /)
 
147
    (+           +)
 
148
    (-           -)
 
149
    (%           %)
 
150
    (&           &)
 
151
    (^           ^)
 
152
    (=           =)
 
153
    (?           ?)
 
154
    (!           !)
 
155
    (~           ~)
 
156
    ))
 
157
 
 
158
;;;
 
159
;;;
 
160
;;;
 
161
(define %keyword-tbl (let ((tbl (make-hash-table)))
 
162
                       (for-each (lambda (lst)
 
163
                                   (hash-table-put! tbl (car lst) lst))
 
164
                                 c-keywords)
 
165
                       tbl))
 
166
 
 
167
(define %operator-tbl (let ((tbl (make-hash-table)))
 
168
                        (for-each (lambda (lst)
 
169
                                    (hash-table-put! tbl (car lst) lst))
 
170
                                  c-operators)
 
171
                        tbl))
 
172
 
 
173
(define (get-keyword-val symbol) (hash-table-get %keyword-tbl symbol #f))
 
174
 
 
175
(define (get-operator-val symbol) (hash-table-get %operator-tbl symbol #f))
 
176
 
 
177
;;;
 
178
;;;
 
179
;;;
 
180
;; gcc of GCC allows dollar sign
 
181
;;(define initial-identifier-charset #[A-Za-z_])
 
182
;;(define identifier-charset         #[A-Za-z_0-9])  
 
183
(define initial-identifier-charset #[A-Za-z_$@])
 
184
(define identifier-charset #[A-Za-z_$0-9])  
 
185
 
 
186
(define operator-charset #[*~!+\-/\^&%=?<>])
 
187
(define special-operator-charset #[|.]) ; cant be in scheme symbol
 
188
 
 
189
(define scheme-token-charset 
 
190
  (char-set    #\.                      ; DOT
 
191
               #\:                      ; COLON
 
192
               #\(                      ; LPAREN
 
193
               #\)                      ; RPAREN
 
194
               #\{                      ; LCBRA
 
195
               #\}                      ; RCBRA
 
196
               #\[                      ; LSBRA
 
197
               #\]                      ; RSBRA
 
198
               #\|                      ; OR
 
199
               ))
 
200
 
 
201
(define (char-digit? c)         (char-set-contains? #[0-9] c))
 
202
(define (char-nonzero-digit? c) (char-set-contains? #[1-9] c))
 
203
(define (char-hex-digit? c)     (char-set-contains? #[0-9A-Fa-f] c))
 
204
(define (char-octal-digit? c)   (char-set-contains? #[0-7] c))
 
205
 
 
206
(define skip-spaces
 
207
  (let ((space-char-list '(#\space #\tab #\x0b #\page)))
 
208
    (lambda ()
 
209
      (let loop ((c (buf-peek-char)))
 
210
        (when (memq c space-char-list)
 
211
          (buf-next-char)
 
212
          (loop (buf-peek-char)))))))
 
213
 
 
214
(define (skip-line)
 
215
  (let loop ((c (buf-peek-char)))
 
216
    (if (and (not (eof-object? c))
 
217
             (not (char=? c #\newline)))
 
218
        (begin
 
219
          (buf-next-char)
 
220
          (loop (buf-peek-char))))))
 
221
 
 
222
(define (l->symbol l)
 
223
  (string->symbol (list->string (reverse l))))
 
224
 
 
225
(define (read-identifier l)
 
226
  (let ((c (buf-peek-char)))
 
227
    (if (char-set-contains? identifier-charset c)
 
228
        (begin
 
229
          (buf-next-char)
 
230
          (read-identifier (cons c l)))
 
231
        (let ((s (l->symbol l)))
 
232
          (cond
 
233
           ((get-keyword-val s) => (lambda (k)
 
234
                                     ;; skip __attribute__ keyword
 
235
                                     (if (eq? (cadr k) 'ATTRIBUTE)
 
236
                                         (c-scan)
 
237
                                         (cadr k))))
 
238
           ((memq s '(int long double char short float))
 
239
            (cons 'TYPENAME s))
 
240
           ((and (pair? (last-token)) (eq? (car (last-token)) 'TYPENAME))
 
241
            (cons 'IDENTIFIER s))
 
242
           ((or (eq? (last-token) 'STRUCT) (eq? (last-token) 'UNION))
 
243
            (cons 'IDENTIFIER s))
 
244
           ((eq? (last-token) 'AT_INTERFACE)
 
245
            (install-type s)
 
246
            (cons 'IDENTIFIER s))
 
247
           ((typedefed? s)
 
248
            (cons 'TYPENAME s))
 
249
           (else
 
250
            (cons 'IDENTIFIER s)))))))
 
251
 
 
252
(define (read-operator c)
 
253
  (define (l->opval l)
 
254
    (get-operator-val (l->symbol l)))
 
255
  (let lp ((cand (l->opval (list c)))
 
256
           (l (list c)))
 
257
    (let* ((c (buf-peek-char))
 
258
           (k (l->opval (cons c l))))
 
259
      (if k
 
260
          (begin
 
261
            (buf-next-char)
 
262
            (lp k (cons c l)))
 
263
          (cadr cand)))))
 
264
 
 
265
 
 
266
;;;
 
267
;;;
 
268
;;;
 
269
(define (hexchar->integer c)
 
270
  (cond ((char=? c #\0) 0)
 
271
        ((char=? c #\1) 1)
 
272
        ((char=? c #\2) 2)
 
273
        ((char=? c #\3) 3)
 
274
        ((char=? c #\4) 4)
 
275
        ((char=? c #\5) 5)
 
276
        ((char=? c #\6) 6)
 
277
        ((char=? c #\7) 7)
 
278
        ((char=? c #\8) 8)
 
279
        ((char=? c #\9) 9)
 
280
        ((char-ci=? c #\a) 10)
 
281
        ((char-ci=? c #\b) 11)
 
282
        ((char-ci=? c #\c) 12)
 
283
        ((char-ci=? c #\d) 13)
 
284
        ((char-ci=? c #\e) 14)
 
285
        ((char-ci=? c #\f) 15)
 
286
        (else
 
287
         (error "can not be, there must be a bug in lexer"))))
 
288
 
 
289
(define (backslash c)
 
290
  (define (readn c n v)
 
291
    (if (and (> n 0) (char-octal-digit? c))
 
292
        (begin
 
293
          (buf-next-char)
 
294
          (readn (buf-peek-char) 
 
295
                 (- n 1) 
 
296
                 (+ (* v 8) (hexchar->integer c))))
 
297
        (integer->char v)))
 
298
  (define (readx c)
 
299
    (let lp ((c c) 
 
300
             (v 0))
 
301
      (if  (char-hex-digit? c)
 
302
           (begin
 
303
             (buf-next-char)
 
304
             (lp (buf-peek-char)
 
305
                 (+ (* v 16) (hexchar->integer c))))
 
306
           (integer->char v))))
 
307
  (define (readu c)
 
308
    (define (range-check v) 
 
309
      ;; CARM: Sec 2.9, p41
 
310
      (or (= v #x24)
 
311
          (= v #x40)
 
312
          (= v #x60)
 
313
          (not (or (< v #xa0)
 
314
                   (and (<= #xd800 v) (<= v #xdfff))))))
 
315
    (receive (n v)
 
316
        (let lp ((c c) 
 
317
                 (n 0) 
 
318
                 (v 0))
 
319
          (if (char-hex-digit? c)
 
320
              (begin
 
321
                (buf-read-char)
 
322
                (lp (buf-peek-char) 
 
323
                    (+ n 1)
 
324
                    (+ (* v 16) (hexchar->integer c))))
 
325
              (values n v)))
 
326
      (cond
 
327
       ((not (or (= n 4) (= n 8)))
 
328
        (error "Universal character name must be 4 or 8 hex-digit"))
 
329
       ((not (range-check v))
 
330
        (errorf "\\u~4,'0x is not a valid universal character" v))
 
331
       (else
 
332
        (ucs->char v)))))
 
333
 
 
334
  (if (char=? c #\\)
 
335
      (let ((c (buf-read-char)))
 
336
        (cond                
 
337
         ((char=? c #\n) #\newline)     ; NL   0A  ^J
 
338
         ((char=? c #\x)                ; not char-ci=?,  cf. CARM p. 35
 
339
          (if (char-hex-digit? (buf-peek-char))
 
340
              (readx (buf-peek-char))
 
341
              (error "\\x must be followed by hex-digit" (buf-peek-char))))
 
342
         ((char=? c #\t) #\tab)         ; HT   09  ^I
 
343
         ((char-ci=? c #\u)
 
344
          (if (char-hex-digit? (buf-peek-char))
 
345
              (readu (buf-peek-char))
 
346
              (error "\\u must be followed by hex-digit" (buf-peek-char))))
 
347
         ((char-octal-digit? c)
 
348
          (readn (buf-peek-char) 3 (hexchar->integer c)))
 
349
         ((char-set-contains? #[?\'\"\\] c) c) ; first two backslash 
 
350
                                               ; are placed to fool emacs
 
351
         ((char=? c #\p)
 
352
          ;; TODO: pascal string
 
353
          c)
 
354
         ((char=? c #\a) #\x07)         ; BEL  07  ^G
 
355
         ((char=? c #\b) #\x08)         ; BS   08  ^H
 
356
         ((char=? c #\v) #\x0b)         ; VT   0B  ^K
 
357
         ((char=? c #\f) #\page)        ; NP   0C  ^L
 
358
         ((char=? c #\r) #\return)      ; CR   0D  ^M
 
359
         ((char-set-contains? #[a-z] c)
 
360
          (warning "Unknown lower case escape character is used: ~a" c)
 
361
          c)
 
362
         ((char-set-contains? #[A-Z] c)
 
363
          (warning "No upper case espace character is defined: ~a" c)
 
364
          c)
 
365
         (else
 
366
          (buf-read-char))))
 
367
      c))
 
368
 
 
369
(define (read-string-literal)
 
370
  (let lp ((c (buf-read-char))
 
371
           (s '()))
 
372
    (if (eof-object? c)
 
373
        (error "missing double quote")
 
374
        (if (char=? c #\")
 
375
            (apply string (reverse s))
 
376
            (let ((cc (backslash c)))
 
377
              (lp (buf-read-char) (cons cc s)))))))
 
378
 
 
379
(define (read-character-constant)
 
380
  (let lp ((c (buf-read-char))
 
381
           (s 0))
 
382
    (if (eof-object? c)
 
383
        (error "missing quote")
 
384
        (if (char=? c  #\')
 
385
            s
 
386
            (let ((cc (backslash c)))
 
387
              ;; Meaning of Multicharacter constant is implementation
 
388
              ;; dependent.  Here we implement a convention with left-to-right
 
389
              ;; packing, which is  described in CARM pp. 31--32.
 
390
              (lp (buf-read-char) 
 
391
                  (+ (* 256 s) 
 
392
                     (char->integer cc))))))))
 
393
;;;
 
394
;;; read-hexadecimal, octal.
 
395
;;;
 
396
(define (read-octal-or-flonum l)
 
397
  (define (rl->n l)
 
398
    (for-each (lambda (c) 
 
399
                (if (not (char-set-contains? #[0-7] c))
 
400
                    (error "invalid char in octal" c)))
 
401
              l)
 
402
    (strtoll (apply string (reverse l))))
 
403
 
 
404
  (let ((c (buf-peek-char)))
 
405
    (cond 
 
406
     ((char-set-contains? #[0-9] c)
 
407
      (buf-next-char)
 
408
      (read-octal-or-flonum (cons c l)))
 
409
     ((char=? #\. c)
 
410
      (buf-next-char)
 
411
      (read-flonum (cons c l) #[0-9] 10 #[Ee]))
 
412
     ((char-set-contains? #[Ee] c) 
 
413
      (buf-next-char)
 
414
      (read-expnum (cons c l) 10))
 
415
     ((char-set-contains? #[ULul] c)
 
416
      (buf-next-char)
 
417
      (list (integer-suffix c)
 
418
            (rl->n l)))
 
419
     (else
 
420
      (list 'int (rl->n l))))))
 
421
  
 
422
;;;
 
423
;;; read decimal integer or floating point
 
424
;;;
 
425
(define (read-number-constant l ics radix ecs)
 
426
 
 
427
  (define (rl->n l)
 
428
    (strtoll (apply string (reverse l))))
 
429
 
 
430
  (let ((c (buf-peek-char)))
 
431
    (cond 
 
432
     ((char-set-contains? ics c)
 
433
      (buf-next-char)
 
434
      (read-number-constant (cons c l) ics radix ecs))
 
435
     ((char=? c #\.)
 
436
      (buf-next-char)
 
437
      (read-flonum (cons c l) ics radix ecs))
 
438
     ((char-set-contains? ecs c) 
 
439
      (buf-next-char)
 
440
      (read-expnum (cons c l) radix))
 
441
     ((char-set-contains? #[ULul] c)
 
442
      (buf-next-char)
 
443
      (list (integer-suffix c)
 
444
            (rl->n l)))
 
445
     (else
 
446
      (list 'int (rl->n l))))))
 
447
 
 
448
(define (read-decimal l)
 
449
  (read-number-constant l #[0-9] 10 #[Ee]))
 
450
(define (read-hexadecimal l) 
 
451
  (read-number-constant l #[0-9A-Fa-f] 16 #[Pp]))
 
452
 
 
453
(define (integer-suffix c)
 
454
  ;; ugly...
 
455
  (cond 
 
456
   ((char-ci=? c #\u)
 
457
    (let ((c (buf-peek-char)))
 
458
      (cond ((char=? c #\l)
 
459
             (buf-next-char)
 
460
             (let ((c (buf-peek-char)))
 
461
               (if (char=? c #\l)
 
462
                   (begin
 
463
                     (buf-next-char)
 
464
                     'unsigned-long-long)
 
465
                   'unsigned-long)))
 
466
            ((char=? c #\L)
 
467
             (buf-next-char)
 
468
             (let ((c (buf-peek-char)))
 
469
               (if (char=? c #\L)
 
470
                   (begin
 
471
                     (buf-next-char)
 
472
                     'unsigned-long-long)
 
473
                   'unsigned-long)))
 
474
            (else
 
475
             'unsigned-int))))
 
476
 
 
477
   ((char=? c #\l)
 
478
    (let ((c (buf-peek-char)))
 
479
      (cond ((char=? c #\l)
 
480
             (begin
 
481
               (buf-next-char)
 
482
               (let ((c (buf-peek-char)))
 
483
                 (if (char-ci=? c #\u)
 
484
                     (begin (buf-next-char)
 
485
                            'unsigned-long-long)
 
486
                     'long-long))))
 
487
            ((char-ci=? c #\u)
 
488
             (buf-next-char)
 
489
             'unsinged-long)
 
490
            (else  'long))))
 
491
 
 
492
   ((char=? c #\L)
 
493
    (let ((c (buf-peek-char)))
 
494
      (cond ((char=? c #\L)
 
495
             (begin
 
496
               (buf-next-char)
 
497
               (let ((c (buf-peek-char)))
 
498
                 (if (char-ci=? c #\u)
 
499
                     (begin (buf-next-char)
 
500
                            'unsigned-long-long)
 
501
                     'long-long))))
 
502
            ((char-ci=? c #\u)
 
503
             (buf-next-char)
 
504
             'unsinged-long)
 
505
            (else  'long))))
 
506
   (else
 
507
    (error "there is a bug in lexer"))))
 
508
 
 
509
(define (read-flonum l ics radix ecs)
 
510
  (define (rl->n l)
 
511
    (strtod (apply string (reverse l))))
 
512
 
 
513
  (define (error-if-hex)
 
514
    (if (= radix 16)
 
515
        (error "hexadecimal floating constants require an exponent")))
 
516
 
 
517
  (let ((c (buf-peek-char)))
 
518
    (cond
 
519
     ((char-set-contains? ics c)
 
520
      (buf-next-char)
 
521
      (read-flonum (cons c l) ics radix ecs))
 
522
     ((char-set-contains? ecs c) 
 
523
      (buf-next-char)
 
524
      (read-expnum (cons c l) radix))
 
525
     ((char-ci=? c #\f)
 
526
      (buf-next-char)
 
527
      (error-if-hex)
 
528
      (list 'float (rl->n l)))
 
529
     ((char-ci=? c #\l)
 
530
      (buf-next-char)
 
531
      (error-if-hex)
 
532
      (list 'long-double (rl->n l)))
 
533
     (else
 
534
      (error-if-hex)
 
535
      (list 'double (rl->n l))))))
 
536
 
 
537
;;;
 
538
;;;
 
539
;;;
 
540
(define (read-expnum l radix)
 
541
 
 
542
  (define (rl->n l) (strtod (apply string (reverse l))))
 
543
 
 
544
  (define (exp1 c l)
 
545
    (cond
 
546
     ((char-numeric? c)
 
547
      (buf-next-char)
 
548
      (exp1 (buf-peek-char) (cons c l)))
 
549
     ((char-ci=? c #\f)
 
550
      (buf-next-char)
 
551
      (list 'float (rl->n l)))
 
552
     ((char-ci=? c #\l)
 
553
      (buf-next-char)
 
554
      (list 'long-double (rl->n l)))
 
555
     (else
 
556
      (list 'double (rl->n l)))))
 
557
 
 
558
  (if (char-set-contains? #[0-9\-\+] (buf-peek-char))
 
559
      (let ((c (buf-read-char)))
 
560
        (exp1 (buf-peek-char) (cons c l)))
 
561
      (error "malformed floating point expression")))
 
562
 
 
563
;;;
 
564
;;;
 
565
;;;
 
566
(define (follow expect ifyes ifno)
 
567
  (let ((c (buf-peek-char)))
 
568
    (if (char=? c expect)
 
569
        (begin (buf-next-char)
 
570
               ifyes)
 
571
        ifno)))
 
572
 
 
573
;;;
 
574
;;;
 
575
;;;
 
576
 
 
577
(define sc-ignore         skip-line)
 
578
(define sc-do-sharp-space skip-line)
 
579
 
 
580
(define sharp-commands
 
581
  `(pragma  ,sc-ignore)
 
582
  )
 
583
 
 
584
(define (do-sharp-command)
 
585
  (if (char=? (buf-peek-char) #\space)
 
586
      (begin
 
587
        (buf-next-char)
 
588
        (sc-do-sharp-space))
 
589
      (let lp ((c (buf-peek-char))
 
590
               (l '()))
 
591
        (cond ((char-alphabetic? c)
 
592
               (buf-next-char)
 
593
               (lp (buf-peek-char) (cons c l)))
 
594
              (else
 
595
               (let ((cmd (assq (l->symbol l) sharp-commands)))
 
596
                 (if cmd 
 
597
                     ((cadr cmd))
 
598
                     (sc-ignore))))))))
 
599
 
 
600
;;;
 
601
;;; Changes from c-lex.scm:
 
602
;;;
 
603
;;;   STRING        -> STRING_LITERAL
 
604
;;;   OROR          -> OR_OP
 
605
;;;   (ASSIGN . OR) -> OR_ASSIGN
 
606
;;;
 
607
(define (c-scan)
 
608
  (let ((token (%c-scan)))
 
609
    (last-token token)
 
610
    token))
 
611
 
 
612
(define (%c-scan)
 
613
  (skip-spaces)
 
614
  (let loop ((c (buf-read-char)))
 
615
    (cond
 
616
     ((eof-object? c)  '*eoi*)
 
617
     ((char=? c #\newline) 
 
618
      (skip-spaces)
 
619
      (loop (buf-read-char)))
 
620
     ((char=? c #\0)
 
621
      (cond
 
622
       ((char-ci=? (buf-peek-char) #\x)
 
623
        (begin (buf-next-char)
 
624
               (cons 'CONSTANT (read-hexadecimal '(#\x #\0)))))
 
625
       (else
 
626
        (cons 'CONSTANT (read-octal-or-flonum (list c))))))
 
627
 
 
628
     ((char-numeric? c)
 
629
      (cons 'CONSTANT (read-decimal (list c))))
 
630
 
 
631
     ((and (char=? c #\.) (char-numeric? (buf-peek-char)))
 
632
      (cons 'CONSTANT (read-flonum (list c) #[0-9] 10 #[Ee])))
 
633
 
 
634
     ((char=? c #\.)  
 
635
      (if (char=? (buf-peek-char) #\.)
 
636
          (begin
 
637
            (buf-next-char)
 
638
            (if (char=? (buf-peek-char) #\.)
 
639
                (begin
 
640
                  (buf-next-char)
 
641
                  'ELLIPSIS)
 
642
                'UNKNOWN))
 
643
          'DOT))
 
644
 
 
645
     ((and (char=? c #\L) (char=? (buf-peek-char) #\"))
 
646
      (buf-next-char)                       ; L
 
647
      (cons 'STRING   (read-string-literal)))
 
648
     ((and (char=? c #\L) (char=? (buf-peek-char) #\'))
 
649
      (buf-next-char)                       ; L
 
650
      (cons 'CONSTANT (list 'wchar (read-character-constant))))
 
651
     ((and (char=? c #\@) (char=? (buf-peek-char) #\"))
 
652
      (buf-next-char)                       ; @
 
653
      (cons 'OBJC_STRING (read-string-literal)))
 
654
     ((char=? c #\")
 
655
      (cons 'STRING (read-string-literal)))
 
656
     ((char=? c #\')
 
657
      (cons 'CONSTANT (list 'int (read-character-constant))))
 
658
 
 
659
     ((char-set-contains? initial-identifier-charset c)
 
660
      (read-identifier (list c)))
 
661
 
 
662
     ((char-set-contains? operator-charset c)
 
663
      (read-operator c))
 
664
 
 
665
     ((char=? c #\,)   'COMMA)
 
666
     ((char=? c #\:)   'COLON)
 
667
     ((char=? c #\;)   'SEMICOLON)
 
668
     ((char=? c #\()   'LPAREN)
 
669
     ((char=? c #\))   'RPAREN)
 
670
     ((char=? c #\{)   'LCBRA)
 
671
     ((char=? c #\})   'RCBRA)
 
672
     ((char=? c #\[)   'LSBRA)
 
673
     ((char=? c #\])   'RSBRA)
 
674
 
 
675
     ;; special case for c-operator due to Scheme
 
676
     ((char=? c #\|)   (or (follow #\| 'OR_OP     #f)   ; ||
 
677
                           (follow #\= 'OR_ASSIGN #f)   ; |=
 
678
                           'OR))                        ; |
 
679
     (else
 
680
      (error "waring: illegal character: " c)
 
681
      (skip-spaces)
 
682
      (loop (buf-read-char))))))
 
683
;;;;;;
 
684
;;;;;; END OF LEXER PART
 
685