aboutsummaryrefslogtreecommitdiff
path: root/tests/test/cpp/lex2.scm
blob: b80bcf370022a6fb6b6a94064d41013561c71d15 (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
(define-module (test cpp lex2)
  :use-module (srfi srfi-64)
  :use-module (srfi srfi-88)
  :use-module (ice-9 peg)
  :use-module (c lex2))


(test-equal "Integer literal"
  (list (lexeme type: 'preprocessing-token body: '(pp-number "10")))
  (lex "10"))

(test-equal "String literal"
  (list (lexeme type: 'preprocessing-token body: '(string-literal "Hello")))
  (lex "\"Hello\""))


(test-equal "Mulitple tokens, including whitespace"
  (list (lexeme type: 'whitespace body: " ")
        (lexeme type: 'preprocessing-token body: '(pp-number "10"))
        (lexeme type: 'whitespace body: " "))
  (lex " 10 "))

(test-equal "Char literal"
  (list (lexeme type: 'preprocessing-token body: '(character-constant "a")))
  (lex "'a'"))



(test-equal "Comment inside string"
  (list (lexeme type: 'preprocessing-token body: '(string-literal "Hel/*lo")))
  (lex "\"Hel/*lo\""))

(test-equal "#define line"
  (list
   (lexeme type: 'preprocessing-token body: '(punctuator "#"))
   (lexeme type: 'preprocessing-token body: '(identifier "define"))
   (lexeme type: 'whitespace body: " ")
   (lexeme type: 'preprocessing-token body: '(identifier "f"))
   (lexeme type: 'preprocessing-token body: '(punctuator "("))
   (lexeme type: 'preprocessing-token body: '(identifier "x"))
   (lexeme type: 'preprocessing-token body: '(punctuator ")"))
   (lexeme type: 'whitespace body: " ")
   (lexeme type: 'preprocessing-token body: '(pp-number "10")))
  (lex "#define f(x) 10"))



(test-equal "Nested parenthesis"
  (list
   (lexeme type: 'preprocessing-token body: '(identifier "f"))
   (lexeme type: 'preprocessing-token body: '(punctuator "("))
   (lexeme type: 'preprocessing-token body: '(pp-number "1"))
   (lexeme type: 'preprocessing-token body: '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (lexeme type: 'preprocessing-token body: '(punctuator "("))
   (lexeme type: 'preprocessing-token body: '(pp-number "2"))
   (lexeme type: 'preprocessing-token body: '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (lexeme type: 'preprocessing-token body: '(pp-number "3"))
   (lexeme type: 'preprocessing-token body: '(punctuator ")"))
   (lexeme type: 'preprocessing-token body: '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (lexeme type: 'preprocessing-token body: '(pp-number "4"))
   (lexeme type: 'preprocessing-token body: '(punctuator ")")))
  (lex "f(1, (2, 3), 4)"))



;; Generating a single lexeme
;; (whitespace "  ")
;; would also be ok
(test-equal "Grouped whitespace"
  (list (lexeme type: 'whitespace body: " ")
        (lexeme type: 'whitespace body: " "))
  (lex "  "))

(test-equal "Newlines get sepparate whitespace tokens"
  (list (lexeme type: 'whitespace body: " ")
        (lexeme type: 'whitespace body: " ")
        (lexeme type: 'whitespace body: "\n")
        (lexeme type: 'whitespace body: " "))
  (lex "  \n "))