aboutsummaryrefslogtreecommitdiff
path: root/module/c/cpp-types.scm
blob: 6dad061e950e896e7417b84dbb4e9505c2bb89f4 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(define-module (c cpp-types)
  :use-module (c lex2)
  :use-module (ice-9 match)
  :use-module (c cpp-util)
  :use-module (hnh util type)
  :export (whitespace-token?
           comment-token?
           preprocessing-token?
           other-token?
           placemaker-token?
           newline-token?
           identifier-token?
           punctuator-token?
           pp-number?
           string-token?
           h-string-token?
           q-string-token?
           character-constant?
           comment->whitespace
           comments->whitespace
           make-string-literal
           ))

(define (whitespace-token? x)
  (and (lexeme? x)
       (eq? 'whitespace (lexeme-type x))))

(define (comment-token? x)
  (and (lexeme? x)
       (eq? 'comment (lexeme-type x))))

(define (preprocessing-token? x)
  (and (lexeme? x)
       (eq? 'preprocessing-token (lexeme-type x))))

(define (other-token? x)
  (and (lexeme? x)
       (eq? 'other (lexeme-type x))))

(define (placemaker-token? x)
  (and (lexeme? x)
       (eq? 'placemaker (lexeme-type x))))

(define (newline-token? x)
  (and (whitespace-token? x)
       (string=? "\n" (lexeme-body x))))

(define (identifier-token? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (`(identifier ,id) id)
         (_ #f))))

(define (punctuator-token? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (`(punctuator ,x) x)
         (_ #f))))

(define (pp-number? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (`(pp-number ,x) x)
         (_ #f))))

;; TODO rename to string-literal-token?
(define (string-token? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (('string-literal x ...) (apply values x))
         (_ #f))))

(define (character-constant? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (('character-constant x ...) (apply values x))
         (_ #f))))


(define (h-string-token? token)
  (and (preprocessing-token? token)
       (match (lexeme-body token)
         (`(header-name (h-string ,x)) x)
         (_ #f))))

;; NOTE q-string tokens are never produced by the lexer,
;; since they instead are treated as regular strings
(define (q-string-token? token)
  (string-token? token))

(define (make-string-literal parts)
  (typecheck parts (list-of (or string? list?)))
  (lexeme type: 'preprocessing-token
          body: (cons 'string-literal parts)))