-
Notifications
You must be signed in to change notification settings - Fork 789
/
TokenizerTests.fs
225 lines (202 loc) · 10.4 KB
/
TokenizerTests.fs
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
module FSharp.Compiler.Service.Tests.TokenizerTests
open FSharp.Compiler.Tokenization
open FSharp.Test
open Xunit
let sourceTok = FSharpSourceTokenizer([], Some "C:\\test.fsx", None, None)
let rec parseLine(line: string, state: FSharpTokenizerLexState ref, tokenizer: FSharpLineTokenizer) = seq {
match tokenizer.ScanToken(state.Value) with
| Some(tok), nstate ->
let str = line.Substring(tok.LeftColumn, tok.RightColumn - tok.LeftColumn + 1)
yield str, tok
state.Value <- nstate
yield! parseLine(line, state, tokenizer)
| None, nstate ->
state.Value <- nstate }
let tokenizeLines (lines:string[]) =
[ let state = ref FSharpTokenizerLexState.Initial
for n, line in lines |> Seq.zip [ 0 .. lines.Length-1 ] do
let tokenizer = sourceTok.CreateLineTokenizer(line)
yield n, parseLine(line, state, tokenizer) |> List.ofSeq ]
[<Fact>]
let ``Tokenizer test - simple let with string``() =
let tokenizedLines =
tokenizeLines
[| "// Sets the hello world variable"
"let hello = \"Hello world\" " |]
let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("LINE_COMMENT", "//"); ("LINE_COMMENT", " "); ("LINE_COMMENT", "Sets");
("LINE_COMMENT", " "); ("LINE_COMMENT", "the"); ("LINE_COMMENT", " ");
("LINE_COMMENT", "hello"); ("LINE_COMMENT", " ");
("LINE_COMMENT", "world"); ("LINE_COMMENT", " ");
("LINE_COMMENT", "variable")]);
(1,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
("STRING_TEXT", "world"); ("STRING", "\""); ("WHITESPACE", " ")])]
if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
actual |> Assert.shouldBeEqualWith expected (sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)
[<Fact>]
let ``Tokenizer test 2 - single line non-nested string interpolation``() =
let tokenizedLines =
tokenizeLines
[| "// Tests tokenizing string interpolation"
"let hello0 = $\"\""
"let hello1 = $\"Hello world\" "
"let hello2 = $\"Hello world {1+1} = {2}\" "
"let hello0v = @$\"\""
"let hello1v = @$\"Hello world\" "
"let hello2v = @$\"Hello world {1+1} = {2}\" " |]
let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("LINE_COMMENT", "//"); ("LINE_COMMENT", " "); ("LINE_COMMENT", "Tests");
("LINE_COMMENT", " "); ("LINE_COMMENT", "tokenizing"); ("LINE_COMMENT", " ");
("LINE_COMMENT", "string"); ("LINE_COMMENT", " ");
("LINE_COMMENT", "interpolation")]);
(1,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello0"); ("WHITESPACE", " ");
("EQUALS", "="); ("WHITESPACE", " "); ("STRING_TEXT", "$\"");
("INTERP_STRING_BEGIN_END", "\"")]);
(2,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1"); ("WHITESPACE", " ");
("EQUALS", "="); ("WHITESPACE", " "); ("STRING_TEXT", "$\"");
("STRING_TEXT", "Hello"); ("STRING_TEXT", " "); ("STRING_TEXT", "world");
("INTERP_STRING_BEGIN_END", "\""); ("WHITESPACE", " ")]);
(3,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello2"); ("WHITESPACE", " ");
("EQUALS", "="); ("WHITESPACE", " "); ("STRING_TEXT", "$\"");
("STRING_TEXT", "Hello"); ("STRING_TEXT", " "); ("STRING_TEXT", "world");
("STRING_TEXT", " "); ("INTERP_STRING_BEGIN_PART", "{"); ("INT32", "1");
("PLUS_MINUS_OP", "+"); ("INT32", "1"); ("STRING_TEXT", "}");
("STRING_TEXT", " "); ("STRING_TEXT", "="); ("STRING_TEXT", " ");
("INTERP_STRING_PART", "{"); ("INT32", "2"); ("STRING_TEXT", "}");
("INTERP_STRING_END", "\""); ("WHITESPACE", " ")]);
(4,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello0v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("INTERP_STRING_BEGIN_END", "\"")]);
(5,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
("STRING_TEXT", "world"); ("INTERP_STRING_BEGIN_END", "\"");
("WHITESPACE", " ")]);
(6,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello2v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
("STRING_TEXT", "world"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{"); ("INT32", "1"); ("PLUS_MINUS_OP", "+");
("INT32", "1"); ("STRING_TEXT", "}"); ("STRING_TEXT", " ");
("STRING_TEXT", "="); ("STRING_TEXT", " "); ("INTERP_STRING_PART", "{");
("INT32", "2"); ("STRING_TEXT", "}"); ("INTERP_STRING_END", "\"");
("WHITESPACE", " ")]);]
if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
actual |> Assert.shouldBeEqualWith expected (sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)
[<Fact>]
let ``Tokenizer test - multiline non-nested string interpolation``() =
let tokenizedLines =
tokenizeLines
[| "let hello1t = $\"\"\"abc {1+"
" 1} def\"\"\"" |]
let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1t");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "$\"\"\""); ("STRING_TEXT", "abc"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{"); ("INT32", "1"); ("PLUS_MINUS_OP", "+")]);
(1,
[("WHITESPACE", " "); ("INT32", "1"); ("STRING_TEXT", "}");
("STRING_TEXT", " "); ("STRING_TEXT", "def"); ("INTERP_STRING_END", "\"\"\"")])]
if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
actual |> Assert.shouldBeEqualWith expected (sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)
[<Fact>]
// checks nested '{' and nested single-quote strings
let ``Tokenizer test - multi-line nested string interpolation``() =
let tokenizedLines =
tokenizeLines
[| "let hello1t = $\"\"\"abc {\"a\" + "
" { "
" contents = \"b\" "
" }.contents "
" } def\"\"\"" |]
let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1t");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "$\"\"\""); ("STRING_TEXT", "abc"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{"); ("STRING_TEXT", "\""); ("STRING_TEXT", "a");
("STRING", "\""); ("WHITESPACE", " "); ("PLUS_MINUS_OP", "+");
("WHITESPACE", " ")]);
(1,
[("WHITESPACE", " "); ("LBRACE", "{");
("WHITESPACE", " ")]);
(2,
[("WHITESPACE", " "); ("IDENT", "contents");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "\""); ("STRING_TEXT", "b"); ("STRING", "\"");
("WHITESPACE", " ")]);
(3,
[("WHITESPACE", " "); ("RBRACE", "}"); ("DOT", ".");
("IDENT", "contents"); ("WHITESPACE", " ")]);
(4,
[("WHITESPACE", " "); ("STRING_TEXT", "}");
("STRING_TEXT", " "); ("STRING_TEXT", "def"); ("INTERP_STRING_END", "\"\"\"")])]
if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
actual |> Assert.shouldBeEqualWith expected (sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)
[<Fact>]
let ``Tokenizer test - single-line nested string interpolation``() =
let tokenizedLines =
tokenizeLines
[| " $\"abc { { contents = 1 } }\" " |]
let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("WHITESPACE", " "); ("STRING_TEXT", "$\""); ("STRING_TEXT", "abc");
("STRING_TEXT", " "); ("INTERP_STRING_BEGIN_PART", "{"); ("WHITESPACE", " ");
("LBRACE", "{"); ("WHITESPACE", " "); ("IDENT", "contents");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " "); ("INT32", "1");
("WHITESPACE", " "); ("RBRACE", "}"); ("WHITESPACE", " ");
("STRING_TEXT", "}"); ("INTERP_STRING_END", "\""); ("WHITESPACE", " ")])]
if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
actual |> Assert.shouldBeEqualWith expected (sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)
[<Fact>]
let ``Unfinished idents``() =
let tokenizedLines =
tokenizeLines
[| "`"; "``"; "``a"; "``a`"; "```" |]
let actual =
[ for lineTokens in tokenizedLines |> List.map snd do
[ for str, info in lineTokens -> info.TokenName, str ] ]
let expected =
[["IDENT", "`"]
["IDENT", "``"]
["IDENT", "``a"]
["IDENT", "``a`"]
["IDENT", "```"]]
actual |> Assert.shouldBe expected