| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package lexer | 5 package lexer |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "strings" | 8 "strings" |
| 9 "testing" | 9 "testing" |
| 10 ) | 10 ) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 {"10e+5", FloatConst}, | 73 {"10e+5", FloatConst}, |
| 74 {"10e-5", FloatConst}, | 74 {"10e-5", FloatConst}, |
| 75 {"3.14E5", FloatConst}, | 75 {"3.14E5", FloatConst}, |
| 76 {"3.14e5", FloatConst}, | 76 {"3.14e5", FloatConst}, |
| 77 {"3.14E+55", FloatConst}, | 77 {"3.14E+55", FloatConst}, |
| 78 {"3.14e+55", FloatConst}, | 78 {"3.14e+55", FloatConst}, |
| 79 {"3.14E-55", FloatConst}, | 79 {"3.14E-55", FloatConst}, |
| 80 {"3.14e-55", FloatConst}, | 80 {"3.14e-55", FloatConst}, |
| 81 {"\"hello world\"", StringLiteral}, | 81 {"\"hello world\"", StringLiteral}, |
| 82 {"\"hello \\\"real\\\" world\"", StringLiteral}, | 82 {"\"hello \\\"real\\\" world\"", StringLiteral}, |
| 83 {"// hello comment world", SingleLineComment}, |
| 84 {"/* hello \n comment */", MultiLineComment}, |
| 83 } | 85 } |
| 84 | 86 |
| 85 for i := range testData { | 87 for i := range testData { |
| 86 l := lexer{source: testData[i].source, tokens: make(chan Token)} | 88 l := lexer{source: testData[i].source, tokens: make(chan Token)} |
| 87 go l.run() | 89 go l.run() |
| 88 tokens := pumpTokens(l.tokens) | 90 tokens := pumpTokens(l.tokens) |
| 89 | 91 |
| 90 if len(tokens) != 1 { | 92 if len(tokens) != 1 { |
| 91 t.Fatalf("Source('%v'): Expected 1 token but got %v inst
ead: %v", | 93 t.Fatalf("Source('%v'): Expected 1 token but got %v inst
ead: %v", |
| 92 testData[i].source, len(tokens), tokens) | 94 testData[i].source, len(tokens), tokens) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) | 181 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) |
| 180 } | 182 } |
| 181 | 183 |
| 182 // TestUnterminatedStringLiteralEol tests that the correct error is emitted if | 184 // TestUnterminatedStringLiteralEol tests that the correct error is emitted if |
| 183 // a quoted string is closed on a subsequent line. | 185 // a quoted string is closed on a subsequent line. |
| 184 func TestUnterminatedStringLiteralEol(t *testing.T) { | 186 func TestUnterminatedStringLiteralEol(t *testing.T) { |
| 185 ts := Tokenize("\"hello\n world\"") | 187 ts := Tokenize("\"hello\n world\"") |
| 186 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) | 188 checkEq(t, ErrorUnterminatedStringLiteral, ts.PeekNext().Kind) |
| 187 } | 189 } |
| 188 | 190 |
| 191 // TestSingleLineCommentNoSkip tests that single line comments are correctly lex
ed. |
| 192 func TestSingleLineCommentNoSkip(t *testing.T) { |
| 193 ts := tokenizeUnfiltered("( // some stuff\n)") |
| 194 checkEq(t, LParen, ts.PeekNext().Kind) |
| 195 ts.ConsumeNext() |
| 196 checkEq(t, SingleLineComment, ts.PeekNext().Kind) |
| 197 checkEq(t, "// some stuff", ts.PeekNext().Text) |
| 198 ts.ConsumeNext() |
| 199 checkEq(t, RParen, ts.PeekNext().Kind) |
| 200 } |
| 201 |
| 202 // TestMultiLineCommentNoSkip tests that multi line comments are correctly lexed
. |
| 203 func TestMultiLineCommentNoSkip(t *testing.T) { |
| 204 ts := tokenizeUnfiltered("( /* hello world/ * *\n */)") |
| 205 checkEq(t, LParen, ts.PeekNext().Kind) |
| 206 ts.ConsumeNext() |
| 207 checkEq(t, MultiLineComment, ts.PeekNext().Kind) |
| 208 checkEq(t, "/* hello world/ * *\n */", ts.PeekNext().Text) |
| 209 ts.ConsumeNext() |
| 210 checkEq(t, RParen, ts.PeekNext().Kind) |
| 211 } |
| 212 |
| 189 // TestSingleLineComment tests that single line comments are correctly skipped. | 213 // TestSingleLineComment tests that single line comments are correctly skipped. |
| 190 func TestSingleLineComment(t *testing.T) { | 214 func TestSingleLineComment(t *testing.T) { |
| 191 ts := Tokenize("( // some stuff\n)") | 215 ts := Tokenize("( // some stuff\n)") |
| 192 checkEq(t, LParen, ts.PeekNext().Kind) | 216 checkEq(t, LParen, ts.PeekNext().Kind) |
| 193 ts.ConsumeNext() | 217 ts.ConsumeNext() |
| 194 checkEq(t, RParen, ts.PeekNext().Kind) | 218 checkEq(t, RParen, ts.PeekNext().Kind) |
| 195 } | 219 } |
| 196 | 220 |
| 197 // TestMultiLineComment tests that multi line comments are correctly skipped. | 221 // TestMultiLineComment tests that multi line comments are correctly skipped. |
| 198 func TestMultiLineComment(t *testing.T) { | 222 func TestMultiLineComment(t *testing.T) { |
| 199 ts := Tokenize("( /* hello world/ * *\n */)") | 223 ts := Tokenize("( /* hello world/ * *\n */)") |
| 200 checkEq(t, LParen, ts.PeekNext().Kind) | 224 checkEq(t, LParen, ts.PeekNext().Kind) |
| 201 ts.ConsumeNext() | 225 ts.ConsumeNext() |
| 202 checkEq(t, RParen, ts.PeekNext().Kind) | 226 checkEq(t, RParen, ts.PeekNext().Kind) |
| 203 } | 227 } |
| 204 | 228 |
| 229 // TestCommentsOnly tests that source made only of comments is correctly process
ed. |
| 230 func TestCommentsOnly(t *testing.T) { |
| 231 ts := Tokenize("/* hello world */\n // hello world") |
| 232 checkEq(t, EOF, ts.PeekNext().Kind) |
| 233 } |
| 234 |
| 205 // TestUnterminatedMultiLineComment tests that unterminated multiline comments | 235 // TestUnterminatedMultiLineComment tests that unterminated multiline comments |
| 206 // emit the correct error. | 236 // emit the correct error. |
| 207 func TestUnterminatedMultiLineComment(t *testing.T) { | 237 func TestUnterminatedMultiLineComment(t *testing.T) { |
| 208 ts := Tokenize("( /* hello world/ * *\n )") | 238 ts := Tokenize("( /* hello world/ * *\n )") |
| 209 checkEq(t, LParen, ts.PeekNext().Kind) | 239 checkEq(t, LParen, ts.PeekNext().Kind) |
| 210 ts.ConsumeNext() | 240 ts.ConsumeNext() |
| 211 checkEq(t, ErrorUnterminatedComment, ts.PeekNext().Kind) | 241 checkEq(t, ErrorUnterminatedComment, ts.PeekNext().Kind) |
| 212 } | 242 } |
| 213 | 243 |
| 214 // TestUnterminatedMultiLineCommentAtStar tests that if the string ends at a * | 244 // TestUnterminatedMultiLineCommentAtStar tests that if the string ends at a * |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 296 |
| 267 // TestTokenSnippetLongPrelude tests snippet generation in the presence of a | 297 // TestTokenSnippetLongPrelude tests snippet generation in the presence of a |
| 268 // long line with many characters preceeding the token. | 298 // long line with many characters preceeding the token. |
| 269 func TestTokenSnippetLongPrelude(t *testing.T) { | 299 func TestTokenSnippetLongPrelude(t *testing.T) { |
| 270 source := strings.Repeat(" ", 70) + "hello world" | 300 source := strings.Repeat(" ", 70) + "hello world" |
| 271 ts := Tokenize(source) | 301 ts := Tokenize(source) |
| 272 expected := strings.Repeat(" ", 58) + "hello world\n" | 302 expected := strings.Repeat(" ", 58) + "hello world\n" |
| 273 expected += strings.Repeat(" ", 58) + "^^^^^" | 303 expected += strings.Repeat(" ", 58) + "^^^^^" |
| 274 checkEq(t, expected, ts.PeekNext().Snippet(source, false)) | 304 checkEq(t, expected, ts.PeekNext().Snippet(source, false)) |
| 275 } | 305 } |
| OLD | NEW |