aboutsummaryrefslogtreecommitdiff
path: root/module/c/unlex.scm
blob: 18e800d9c1f2be05913b92b1fd98742306554d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(define-module (c unlex)
  :use-module (hnh util type)
  :use-module (ice-9 match)
  :use-module (c lex2)
  :use-module (c cpp-types)
  :use-module (c cpp-util)
  :export (unlex
           stringify-token
           stringify-tokens))

(define (unlex tokens)
  (typecheck tokens (list-of lexeme?))
  (string-concatenate
   (map (lambda (x) (cond (x preprocessing-token? => stringify-token)
                     ((whitespace-token? x) (lexeme-body x))))
        tokens)))

;; takes a list of preprocessing-token's, and return a "source" string
(define (unlex-aggressive tokens)
  (typecheck tokens (list-of lexeme?))
  (string-concatenate
   (map (lambda (x)
          (cond ((preprocessing-token? x) (stringify-token x))
                ((whitespace-token? x) " ")))
        (squeeze-whitespace tokens))))

;; Returns the "source" of the token, as a preprocessing string literal token
(define (stringify-token preprocessing-token)
  (match (lexeme-body preprocessing-token)
    (`(string-literal ,s)
     (format #f "~s" s))
    (`(header-name (q-string ,s))
     (format #f "~s" s))
    (`(header-name (h-string ,s))
     (format #f "<~a>" s))
    (`(identifier ,id) id)
    (`(pp-number ,n) n)
    (`(character-constant ,c)
     (format #f "'~a'" c))
    (`(punctuator ,p) p)))

;; takes a token list, and return a single string literal token
(define (stringify-tokens tokens)
  (lexeme type: 'preprocessing-token
          body: `(string-literal ,(unlex-aggressive tokens))))