| ... | ... | @@ -359,63 +359,6 @@ pub const Tokenizer = struct { |
| 359 | 359 | }; |
| 360 | 360 | }; |
| 361 | 361 | |
| 362 | | export fn stage2_DepTokenizer_init(input: [*]const u8, len: usize) stage2_DepTokenizer { |
| 363 | | const t = std.heap.c_allocator.create(Tokenizer) catch @panic("failed to create .d tokenizer"); |
| 364 | | t.* = Tokenizer.init(std.heap.c_allocator, input[0..len]); |
| 365 | | return stage2_DepTokenizer{ |
| 366 | | .handle = t, |
| 367 | | }; |
| 368 | | } |
| 369 | | |
| 370 | | export fn stage2_DepTokenizer_deinit(self: *stage2_DepTokenizer) void { |
| 371 | | self.handle.deinit(); |
| 372 | | } |
| 373 | | |
| 374 | | export fn stage2_DepTokenizer_next(self: *stage2_DepTokenizer) stage2_DepNextResult { |
| 375 | | const otoken = self.handle.next() catch { |
| 376 | | const textz = std.Buffer.init(&self.handle.arena.allocator, self.handle.error_text) catch @panic("failed to create .d tokenizer error text"); |
| 377 | | return stage2_DepNextResult{ |
| 378 | | .type_id = .error_, |
| 379 | | .textz = textz.toSlice().ptr, |
| 380 | | }; |
| 381 | | }; |
| 382 | | const token = otoken orelse { |
| 383 | | return stage2_DepNextResult{ |
| 384 | | .type_id = .null_, |
| 385 | | .textz = undefined, |
| 386 | | }; |
| 387 | | }; |
| 388 | | const textz = std.Buffer.init(&self.handle.arena.allocator, token.bytes) catch @panic("failed to create .d tokenizer token text"); |
| 389 | | return stage2_DepNextResult{ |
| 390 | | .type_id = switch (token.id) { |
| 391 | | .target => stage2_DepNextResult.TypeId.target, |
| 392 | | .prereq => stage2_DepNextResult.TypeId.prereq, |
| 393 | | }, |
| 394 | | .textz = textz.toSlice().ptr, |
| 395 | | }; |
| 396 | | } |
| 397 | | |
| 398 | | export const stage2_DepTokenizer = extern struct { |
| 399 | | handle: *Tokenizer, |
| 400 | | }; |
| 401 | | |
| 402 | | export const stage2_DepNextResult = extern struct { |
| 403 | | type_id: TypeId, |
| 404 | | |
| 405 | | // when type_id == error --> error text |
| 406 | | // when type_id == null --> undefined |
| 407 | | // when type_id == target --> target pathname |
| 408 | | // when type_id == prereq --> prereq pathname |
| 409 | | textz: [*]const u8, |
| 410 | | |
| 411 | | export const TypeId = extern enum { |
| 412 | | error_, |
| 413 | | null_, |
| 414 | | target, |
| 415 | | prereq, |
| 416 | | }; |
| 417 | | }; |
| 418 | | |
| 419 | 362 | test "empty file" { |
| 420 | 363 | try depTokenizer("", ""); |
| 421 | 364 | } |
| ... | ... | @@ -469,78 +412,54 @@ test "empty target linefeeds" { |
| 469 | 412 | const expect = "target = {foo.o}"; |
| 470 | 413 | try depTokenizer( |
| 471 | 414 | \\foo.o: |
| 472 | | , |
| 473 | | expect |
| 474 | | ); |
| 415 | , expect); |
| 475 | 416 | try depTokenizer( |
| 476 | 417 | \\foo.o: |
| 477 | 418 | \\ |
| 478 | | , |
| 479 | | expect |
| 480 | | ); |
| 419 | , expect); |
| 481 | 420 | try depTokenizer( |
| 482 | 421 | \\foo.o: |
| 483 | | , |
| 484 | | expect |
| 485 | | ); |
| 422 | , expect); |
| 486 | 423 | try depTokenizer( |
| 487 | 424 | \\foo.o: |
| 488 | 425 | \\ |
| 489 | | , |
| 490 | | expect |
| 491 | | ); |
| 426 | , expect); |
| 492 | 427 | } |
| 493 | 428 | |
| 494 | 429 | test "empty target linefeeds + continuations" { |
| 495 | 430 | const expect = "target = {foo.o}"; |
| 496 | 431 | try depTokenizer( |
| 497 | 432 | \\foo.o:\ |
| 498 | | , |
| 499 | | expect |
| 500 | | ); |
| 433 | , expect); |
| 501 | 434 | try depTokenizer( |
| 502 | 435 | \\foo.o:\ |
| 503 | 436 | \\ |
| 504 | | , |
| 505 | | expect |
| 506 | | ); |
| 437 | , expect); |
| 507 | 438 | try depTokenizer( |
| 508 | 439 | \\foo.o:\ |
| 509 | | , |
| 510 | | expect |
| 511 | | ); |
| 440 | , expect); |
| 512 | 441 | try depTokenizer( |
| 513 | 442 | \\foo.o:\ |
| 514 | 443 | \\ |
| 515 | | , |
| 516 | | expect |
| 517 | | ); |
| 444 | , expect); |
| 518 | 445 | } |
| 519 | 446 | |
| 520 | 447 | test "empty target linefeeds + hspace + continuations" { |
| 521 | 448 | const expect = "target = {foo.o}"; |
| 522 | 449 | try depTokenizer( |
| 523 | 450 | \\foo.o: \ |
| 524 | | , |
| 525 | | expect |
| 526 | | ); |
| 451 | , expect); |
| 527 | 452 | try depTokenizer( |
| 528 | 453 | \\foo.o: \ |
| 529 | 454 | \\ |
| 530 | | , |
| 531 | | expect |
| 532 | | ); |
| 455 | , expect); |
| 533 | 456 | try depTokenizer( |
| 534 | 457 | \\foo.o: \ |
| 535 | | , |
| 536 | | expect |
| 537 | | ); |
| 458 | , expect); |
| 538 | 459 | try depTokenizer( |
| 539 | 460 | \\foo.o: \ |
| 540 | 461 | \\ |
| 541 | | , |
| 542 | | expect |
| 543 | | ); |
| 462 | , expect); |
| 544 | 463 | } |
| 545 | 464 | |
| 546 | 465 | test "prereq" { |
| ... | ... | @@ -572,15 +491,11 @@ test "prereq continuation" { |
| 572 | 491 | try depTokenizer( |
| 573 | 492 | \\foo.o: foo.h\ |
| 574 | 493 | \\bar.h |
| 575 | | , |
| 576 | | expect |
| 577 | | ); |
| 494 | , expect); |
| 578 | 495 | try depTokenizer( |
| 579 | 496 | \\foo.o: foo.h\ |
| 580 | 497 | \\bar.h |
| 581 | | , |
| 582 | | expect |
| 583 | | ); |
| 498 | , expect); |
| 584 | 499 | } |
| 585 | 500 | |
| 586 | 501 | test "multiple prereqs" { |
| ... | ... | @@ -907,14 +822,14 @@ test "error target - continuation expecting end-of-line" { |
| 907 | 822 | \\target = {foo.o} |
| 908 | 823 | \\ERROR: illegal char 'x' at position 8: continuation expecting end-of-line |
| 909 | 824 | ); |
| 910 | | try depTokenizer("foo.o: \\
x", |
| 825 | try depTokenizer("foo.o: \\\x0dx", |
| 911 | 826 | \\target = {foo.o} |
| 912 | 827 | \\ERROR: illegal char 'x' at position 9: continuation expecting end-of-line |
| 913 | 828 | ); |
| 914 | 829 | } |
| 915 | 830 | |
| 916 | 831 | test "error prereq - continuation expecting end-of-line" { |
| 917 | | try depTokenizer("foo.o: foo.h\\
x", |
| 832 | try depTokenizer("foo.o: foo.h\\\x0dx", |
| 918 | 833 | \\target = {foo.o} |
| 919 | 834 | \\ERROR: illegal char 'x' at position 14: continuation expecting end-of-line |
| 920 | 835 | ); |