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

(define (l s)  (lexeme type: 'preprocessing-token body: s))

(define (ls . xs)
  (map l xs))

;; See comment on h-string-compound in (c lex2)
(test-expect-fail "H-string looking as argument to macro")

(test-equal "Integer literal"
  (ls '(pp-number "10"))
  (lex "10"))

(test-equal "String literal"
  (ls `(string-literal (encoding-prefix) "Hello"))
  (lex "\"Hello\""))


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

(test-equal "Char literal"
  (ls `(character-constant (character-prefix) "a"))
  (lex "'a'"))



(test-equal "Comment inside string"
  (ls `(string-literal (encoding-prefix) "Hel/*lo"))
  (lex "\"Hel/*lo\""))

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



(test-equal "Nested parenthesis"
  (list
   (l '(identifier "f"))
   (l '(punctuator "("))
   (l '(pp-number "1"))
   (l '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (l '(punctuator "("))
   (l '(pp-number "2"))
   (l '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (l '(pp-number "3"))
   (l '(punctuator ")"))
   (l '(punctuator ","))
   (lexeme type: 'whitespace body: " ")
   (l '(pp-number "4"))
   (l '(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 "))


;; Refer to 6.4 p.1 for the syntax requirement
;; 6.10.9 p. 2 for the sample string
(test-equal "each non-white-space character that cannot be one of the above"
  (list (l '(punctuator "."))
        (l '(punctuator "."))
        (lexeme type: 'other body: "\\")  ; <- Interesting part
        (l '(identifier "listing"))
        (l '(punctuator "."))
        (l '(identifier "dir")))
  (lex "..\\listing.dir"))

(test-group "h-strings"

  ;; A hstring is the part enclosed by pointy brackets in
  ;; #include <header.h>
  ;; (e.g. "header.h")
  ;; Something with the form <chars> anywhere else should be
  ;; treated as (token <) (token chars) (token >)

 (test-equal "Propper H-string"
   (list (l '(punctuator "#"))
         (l '(identifier "include"))
         ;; (lexeme type: 'whitespace body: " ")
         (l '(h-string "a")))
   (lex "#include <a>"))

 (test-equal "Not a H string"
   (ls '(punctuator "<")
       '(identifier "a")
       '(punctuator ">"))
   (lex "<a>"))

 ;; Assume that s is defined as
 ;; #define s(x) #x
 ;; then the following expression would expand to
 ;; "#include<a>"
 (test-equal "H-string looking as argument to macro"
   (ls '(identifier "s")
       '(punctuator "(")
       '(punctuator "#")
       '(identifier "include")
       '(punctuator "<")
       '(identifier "a")
       '(punctuator ">")
       '(punctuator ")"))
   (lex "s(#include<a>)"))

 ;; The standard says this case is undefined
 ;; 6.4.7 p. 3
 (test-equal "Quotation mark inside h-string"
   (ls '(punctuator "#")
       '(identifier "include")
       '(h-string "a\"b"))
   (lex "#include<a\"b>")))

(test-equal "Q-strings are lexed as regular strings"
  (list (l '(punctuator "#"))
        (l '(identifier "include"))
        (lexeme type: 'whitespace body: " ")
        (l '(string-literal (encoding-prefix) "test")))
  ;; # include here, since generated tokens could possible depend on that context,
  ;; and the reason regular strings are returned is since the lexer doesn't check
  ;; that context
  (lex "#include \"test\"")
  )



(test-group "Unicode"
  (test-equal "In string literals"
    (ls '(string-literal (encoding-prefix) "åäö"))
    (lex "\"åäö\""))

  (test-equal "Outside string literals"
    (list (lexeme type: 'other body: "å")
          (lexeme type: 'other body: "ä")
          (lexeme type: 'other body: "ö"))
    (lex "åäö")))




(test-group "Characters with prefixes"
  (test-equal (ls '(character-constant (character-prefix . "u")
                                       "a"))
    (lex "u'a'"))
  (test-equal (ls '(character-constant (character-prefix . "U")
                                       "a"))
    (lex "U'a'"))
  (test-equal (ls '(character-constant (character-prefix . "L")
                                       "a"))
    (lex "L'a'")))

;; Note that these strings have 0 "data" components
(test-group "Strings with prefixes"
  (test-equal (ls '(string-literal (encoding-prefix . "u8")))
    (lex "u8\"\""))
  (test-equal (ls '(string-literal (encoding-prefix . "u")))
    (lex "u\"\""))
  (test-equal (ls '(string-literal (encoding-prefix . "U")))
    (lex "U\"\""))
  (test-equal (ls '(string-literal (encoding-prefix . "L")))
    (lex "L\"\"")))