aboutsummaryrefslogtreecommitdiff
path: root/module/c/lex2.scm
blob: 652aa6c111880fb46ecb4140f171e7794c9e095b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(define-module (c lex2)
  :use-module (ice-9 peg)
  :use-module (ice-9 match)
  :use-module (hnh util object)
  :use-module (hnh util type)
  :use-module (srfi srfi-88)
  :export (lex
           lexeme lexeme?
           placemaker
           (type . lexeme-type)
           (body . lexeme-body)
           (noexpand . lexeme-noexpand)))

;;; A.1 Lexical grammar
;;; A.1.1 Lexical elements

;; (6.4)
(define-peg-pattern token all
  (or keyword
      identifier
      constant
      string-literal
      punctuator
      ))

;; (6.4)
(define-peg-pattern preprocessing-token all
  ;; string literal moved before header-name since string literals
  ;; otherwise became q-strings
  (or string-literal
      header-name
      identifier
      pp-number
      character-constant
      punctuator
      ;; Each non-white-space character that cannot be one of the above
      ))

;;; A.1.2 Keywords

;; (6.4.1)
(define-peg-pattern keyword all
  (or "auto" "break" "case" "char" "const" "continue" "default"
      "do" "double" "else" "enum" "extern" "float" "for" "goto"
      "if" "inline" "int" "long" "register" "restrict" "return"
      "short" "signed" "sizeof" "static" "struct" "switch"
      "typedef" "union" "unsigned" "void" "volatile" "while"
      "_Alignas" "_Alignof" "_Atomic" "_Bool" "_Complex"
      "_Generic" "_Imaginary" "_Noreturn" "_Static_assert"
      "_Thread_local"))

;;; A.1.3 Identifiers

;; (6.4.2.1)
(define-peg-pattern identifier all
  (and identifier-nondigit (* (or identifier-nondigit digit))))

;; (6.4.2.1)
(define-peg-pattern identifier-nondigit body
  (or nondigit
      universal-character-name
      ;; TODO other implementation-defined characters
      ))

;; (6.4.2.1)
(define-peg-pattern nondigit body
  (or "_"
      (range #\A #\Z)
      (range #\a #\z)))

;; (6.4.2.1)
(define-peg-pattern digit body
  (range #\0 #\9))

;;; A.1.4 Universal character names

;; (6.4.3)
(define-peg-pattern universal-character-name all
  (or (and "\\u" hex-quad)
      (and "\\U" hex-quad hex-quad)))

;; (6.4.3)
(define-peg-pattern hex-quad body
  (and hexadecimal-digit hexadecimal-digit
       hexadecimal-digit hexadecimal-digit))

;;; A.1.5 Constants

;; (6.4.4)
(define-peg-pattern constant all
  ;; Int and float swapped from standard since we need to try parsing
  ;; the floats beforehand
  (or floating-constant
      integer-constant
      enumeration-constant
      character-constant))

;; (6.4.4.1)
(define-peg-pattern integer-constant all
  (and (or decimal-constant
           octal-constant
           hexadecimal-constant)
       integer-suffix))

;; (6.4.4.1)
(define-peg-pattern decimal-constant all
  (and nonzero-digit
       (+ digit)))

;; (6.4.4.1)
(define-peg-pattern octal-constant all
  (+ octal-digit))

;; (6.4.4.1)
(define-peg-pattern hexadecimal-constant all
  (and hexadecimal-prefix (+ hexadecimal-digit)))

;; (6.4.4.1)
(define-peg-pattern hexadecimal-prefix none
  (or "0x" "0X"))

;; (6.4.4.1)
(define-peg-pattern nonzero-digit body
  (range #\1 #\9))

;; (6.4.4.1)
(define-peg-pattern octal-digit body
  (range #\0 #\7))

;; (6.4.4.1)
(define-peg-pattern hexadecimal-digit body
  (or (range #\0 #\9)
      (range #\a #\f)
      (range #\A #\F)))

;; (6.4.4.1)
(define-peg-pattern integer-suffix all
  (or (and unsigned-suffix (? long-suffix))
      (and long-suffix (? unsigned-suffix))))

;; (6.4.4.1)
;; This is a merger of long-suffix and long-long-suffix
(define-peg-pattern long-suffix body
  (or "l" "L" "ll" "LL"))

;; (6.4.4.1)
(define-peg-pattern unsigned-suffix body
  (or "u" "U"))

;; (6.4.4.2)
(define-peg-pattern floating-constant all
  (or decimal-floating-constant
      hexadecimal-floating-constant))

;; (6.4.4.2)
(define-peg-pattern decimal-floating-constant all
  (or (and fractional-constant (? exponent-part) (? floating-suffix))
      (and digit-sequence exponent-part (? floating-suffix))))

;; (6.4.4.2)
(define-peg-pattern hexadecimal-floating-constant all
  (and hexadecimal-prefix
       (or hexadecimal-fractional-constant
           hexadecimal-digit-sequence)
       binary-exponent-part
       (? floating-suffix)))

;; (6.4.4.2)
(define-peg-pattern fractional-constant all
  (or (and (? digit-sequence) "." digit-sequence)
      (and digit-sequence ".")))

;; (6.4.4.2)
(define-peg-pattern exponent-part all
  (and (or "e" "E") (? sign) digit-sequence))

;; (6.4.4.2)
(define-peg-pattern sign all
  (or "+" "-"))

;; (6.4.4.2)
(define-peg-pattern digit-sequence body
  (+ digit))

;; (6.4.4.2)
(define-peg-pattern hexadecimal-fractional-constant all
  (or (and (? hexadecimal-digit-sequence) "." hexadecimal-digit-sequence)
      (and    hexadecimal-digit-sequence  ".")))

;; (6.4.4.2)
(define-peg-pattern binary-exponent-part all
  (and (or "p" "P")
       (? sign)
       digit-sequence))

;; (6.4.4.2)
(define-peg-pattern hexadecimal-digit-sequence body
  (+ hexadecimal-digit))

;; (6.4.4.2)
(define-peg-pattern floating-suffix all
  (or "f" "l" "F" "L"))

;; (6.4.4.3)
(define-peg-pattern enumeration-constant all
  identifier)

(define-peg-pattern character-prefix all
  (or "L" "u" "U"))

;; (6.4.4.4)
(define-peg-pattern character-constant all
  (and (? character-prefix)
       (ignore "'")
       (+ c-char)
       (ignore "'")))

;; (6.4.4.4)
(define-peg-pattern c-char body
  (or (and (not-followed-by (or "'" "\\" "\n"))  peg-any)
      escape-sequence))

;; (6.4.4.4)
(define-peg-pattern escape-sequence all
  (or simple-escape-sequence
      octal-escape-sequence
      hexadecimal-escape-sequence
      universal-character-name))

;; (6.4.4.4)
(define-peg-pattern simple-escape-sequence all
  (and (ignore "\\") (or "'" "\"" "?" "\\"
                         "a" "b" "f" "n" "r" "t" "v")))

;; (6.4.4.4)
(define-peg-pattern octal-escape-sequence all
  (and (ignore "\\") octal-digit (? octal-digit) (? octal-digit)))

;; (6.4.4.4)
(define-peg-pattern hexadecimal-escape-sequence all
  (and (ignore "\\x") (+ hexadecimal-digit)))

;; A.1.6 String literals

;; (6.4.5)
(define-peg-pattern string-literal all
  (and (? encoding-prefix)
       (ignore "\"")
       (* s-char)
       (ignore "\"")))

;; (6.4.5)
(define-peg-pattern encoding-prefix all
  (or "u8" "u" "U" "L"))

;; (6.4.5)
(define-peg-pattern s-char body
  (or (and (not-followed-by (or "\"" "\\" "\n"))  peg-any)
      escape-sequence))

;;; A.1.7

;; (6.4.6)
(define-peg-pattern punctuator all
  (or "[" "]" "(" ")" "{" "}"
      "..."                             ; Moved to be before "."
      "." "->"
      "++" "--" "&" "*" "+" "-" "~" "!"
      "/" "%" "<<" ">>" "<" ">" "<=" ">=" "==" "!=" "^" "|" "&&" "||"
      "?" ":" ";"
      "=" "*=" "/=" "%=" "+=" "-=" "<<=" ">>=" "&=" "^=" "|="
      "," "##" "#" ; # and ## flipped
      "<:" ":>" "<%" "%>" "%:%:" "%:" ; %: and %:%: flipped
      ))

;;; A.1.8 Header names

(define-peg-pattern h-string all (+ h-char))
(define-peg-pattern q-string all (+ q-char))

;; (6.4.7)
(define-peg-pattern header-name all
  (or (and (ignore "<")  h-string (ignore ">"))
      (and (ignore "\"") q-string (ignore "\""))))

;; (6.4.7)
(define-peg-pattern h-char body
  (or (and (not-followed-by (or ">" "\n")) peg-any)
      escape-sequence))

;; (6.4.7)
(define-peg-pattern q-char body
  (or (and (not-followed-by (or "\"" "\n")) peg-any)
      escape-sequence))

;;; A.1.9 Preprocessing numbers

;; (6.4.8)
(define-peg-pattern pp-number all
  (and (? ".") digit
       (* (or digit
              identifier-nondigit
              (and (or "e" "E" "p" "P")
                   sign)
              "."))))



(define-peg-pattern whitespace all
  (or "\t" "\n" "\v" "\f" " "
      ;; "\r"
      ))

(define-peg-pattern block-comment body
  (and (ignore "/*")
       (* (and (not-followed-by "*/")
               peg-any))
       (ignore "*/")))

(define-peg-pattern line-comment body
  (and (ignore "//")
       (* (and (not-followed-by "\n")
               peg-any))))

(define-peg-pattern comment all
  (or line-comment block-comment))

(define-peg-pattern preprocessing-tokens all
  (* (or whitespace
         comment
         preprocessing-token)))



(define-type (lexeme)
  (type type: (memv '(whitespace comment preprocessing-token placemaker)))
  (body type: (or string? list?))
  (noexpand type: (list-of string?)
            default: '()))

(define (placemaker)
  (lexeme type: 'placemaker body: '()))

(define (lex-output->lexeme-object x)
  (match x
    (`(whitespace ,body)
     (lexeme body: body type: 'whitespace ))
    (`(comment ,body)
     (lexeme body: body type: 'comment ))
    (`(preprocessing-token ,body)
     (case body
       ;; "unflatten"
       ((string-literal)
        (lexeme body: '(string-literal "") type: 'preprocessing-token))
       (else
        (lexeme body: body type: 'preprocessing-token))))
    ;; "unflatten"
    ('comment (lexeme body: "" type: 'comment))))



;; At a number of places I chose token depending on the order of the rule. The
;; standard however says that the longest possible choice should be used.
;; 6.4 p. 4

;; returns a list of lexemes
(define (lex string)
  (if (string-null? string)
      '()
      (map lex-output->lexeme-object
           (cdr (peg:tree (match-pattern preprocessing-tokens string))))))