| 1 |
use snapbox::prelude::*; |
| 2 |
use snapbox::str; |
| 3 |
|
| 4 |
use crate::ascii::dec_uint; |
| 5 |
use crate::ascii::digit0; |
| 6 |
use crate::combinator::dispatch; |
| 7 |
use crate::combinator::empty; |
| 8 |
use crate::combinator::fail; |
| 9 |
use crate::combinator::seq; |
| 10 |
use crate::prelude::*; |
| 11 |
use crate::token::any; |
| 12 |
|
| 13 |
#[test] |
| 14 |
fn dispatch_basics() { |
| 15 |
fn escape_seq_char<'i>(input: &mut &'i str) -> TestResult<&'i str, char> { |
| 16 |
dispatch! {any; |
| 17 |
'b' => empty.value('\u{8}'), |
| 18 |
'f' => empty.value('\u{c}'), |
| 19 |
'n' => empty.value('\n'), |
| 20 |
'r' => empty.value('\r'), |
| 21 |
't' => empty.value('\t'), |
| 22 |
'\\' => empty.value('\\'), |
| 23 |
'"' => empty.value('"'), |
| 24 |
_ => fail::<_, char, _>, |
| 25 |
} |
| 26 |
.parse_next(input) |
| 27 |
} |
| 28 |
assert_parse!( |
| 29 |
escape_seq_char.parse_peek("b123"), |
| 30 |
str![[r#" |
| 31 |
Ok( |
| 32 |
( |
| 33 |
"123", |
| 34 |
'\u{8}', |
| 35 |
), |
| 36 |
) |
| 37 |
|
| 38 |
"#]] |
| 39 |
.raw() |
| 40 |
); |
| 41 |
assert_parse!( |
| 42 |
escape_seq_char.parse_peek("error"), |
| 43 |
str![[r#" |
| 44 |
Err( |
| 45 |
Backtrack( |
| 46 |
InputError { |
| 47 |
input: "rror", |
| 48 |
}, |
| 49 |
), |
| 50 |
) |
| 51 |
|
| 52 |
"#]] |
| 53 |
.raw() |
| 54 |
); |
| 55 |
assert_parse!( |
| 56 |
escape_seq_char.parse_peek(""), |
| 57 |
str![[r#" |
| 58 |
Err( |
| 59 |
Backtrack( |
| 60 |
InputError { |
| 61 |
input: "", |
| 62 |
}, |
| 63 |
), |
| 64 |
) |
| 65 |
|
| 66 |
"#]] |
| 67 |
.raw() |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn seq_struct_basics() { |
| 73 |
#[derive(Debug, PartialEq)] |
| 74 |
struct Point { |
| 75 |
x: u32, |
| 76 |
y: u32, |
| 77 |
} |
| 78 |
|
| 79 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 80 |
seq! { |
| 81 |
Point { |
| 82 |
x: dec_uint, |
| 83 |
_: ',', |
| 84 |
y: dec_uint, |
| 85 |
} |
| 86 |
} |
| 87 |
.parse_next(input) |
| 88 |
} |
| 89 |
assert_parse!( |
| 90 |
parser.parse_peek("123,4 remaining"), |
| 91 |
str![[r#" |
| 92 |
Ok( |
| 93 |
( |
| 94 |
" remaining", |
| 95 |
Point { |
| 96 |
x: 123, |
| 97 |
y: 4, |
| 98 |
}, |
| 99 |
), |
| 100 |
) |
| 101 |
|
| 102 |
"#]] |
| 103 |
.raw() |
| 104 |
); |
| 105 |
assert_parse!( |
| 106 |
parser.parse_peek("123, remaining"), |
| 107 |
str![[r#" |
| 108 |
Err( |
| 109 |
Backtrack( |
| 110 |
InputError { |
| 111 |
input: " remaining", |
| 112 |
}, |
| 113 |
), |
| 114 |
) |
| 115 |
|
| 116 |
"#]] |
| 117 |
.raw() |
| 118 |
); |
| 119 |
assert_parse!( |
| 120 |
parser.parse_peek(""), |
| 121 |
str![[r#" |
| 122 |
Err( |
| 123 |
Backtrack( |
| 124 |
InputError { |
| 125 |
input: "", |
| 126 |
}, |
| 127 |
), |
| 128 |
) |
| 129 |
|
| 130 |
"#]] |
| 131 |
.raw() |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn seq_struct_default_init() { |
| 137 |
#[derive(Debug, PartialEq, Default)] |
| 138 |
struct Point { |
| 139 |
x: u32, |
| 140 |
y: u32, |
| 141 |
z: u32, |
| 142 |
} |
| 143 |
|
| 144 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 145 |
seq! { |
| 146 |
Point { |
| 147 |
x: dec_uint, |
| 148 |
_: ',', |
| 149 |
y: dec_uint, |
| 150 |
..Default::default() |
| 151 |
} |
| 152 |
} |
| 153 |
.parse_next(input) |
| 154 |
} |
| 155 |
assert_parse!( |
| 156 |
parser.parse_peek("123,4 remaining"), |
| 157 |
str![[r#" |
| 158 |
Ok( |
| 159 |
( |
| 160 |
" remaining", |
| 161 |
Point { |
| 162 |
x: 123, |
| 163 |
y: 4, |
| 164 |
z: 0, |
| 165 |
}, |
| 166 |
), |
| 167 |
) |
| 168 |
|
| 169 |
"#]] |
| 170 |
.raw() |
| 171 |
); |
| 172 |
assert_parse!( |
| 173 |
parser.parse_peek("123, remaining"), |
| 174 |
str![[r#" |
| 175 |
Err( |
| 176 |
Backtrack( |
| 177 |
InputError { |
| 178 |
input: " remaining", |
| 179 |
}, |
| 180 |
), |
| 181 |
) |
| 182 |
|
| 183 |
"#]] |
| 184 |
.raw() |
| 185 |
); |
| 186 |
assert_parse!( |
| 187 |
parser.parse_peek(""), |
| 188 |
str![[r#" |
| 189 |
Err( |
| 190 |
Backtrack( |
| 191 |
InputError { |
| 192 |
input: "", |
| 193 |
}, |
| 194 |
), |
| 195 |
) |
| 196 |
|
| 197 |
"#]] |
| 198 |
.raw() |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn seq_struct_trailing_comma_elided() { |
| 204 |
#![allow(dead_code)] |
| 205 |
|
| 206 |
#[derive(Debug, PartialEq)] |
| 207 |
struct Point { |
| 208 |
x: u32, |
| 209 |
y: u32, |
| 210 |
} |
| 211 |
|
| 212 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 213 |
seq! { |
| 214 |
Point { |
| 215 |
x: dec_uint, |
| 216 |
_: ',', |
| 217 |
y: dec_uint, |
| 218 |
_: empty, |
| 219 |
} |
| 220 |
} |
| 221 |
.parse_next(input) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn seq_struct_no_trailing_comma() { |
| 227 |
#![allow(dead_code)] |
| 228 |
|
| 229 |
#[derive(Debug, PartialEq)] |
| 230 |
struct Point { |
| 231 |
x: u32, |
| 232 |
y: u32, |
| 233 |
} |
| 234 |
|
| 235 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 236 |
seq! { |
| 237 |
Point { |
| 238 |
x: dec_uint, |
| 239 |
_: ',', |
| 240 |
y: dec_uint |
| 241 |
} |
| 242 |
} |
| 243 |
.parse_next(input) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn seq_struct_no_trailing_comma_elided() { |
| 249 |
#![allow(dead_code)] |
| 250 |
|
| 251 |
#[derive(Debug, PartialEq)] |
| 252 |
struct Point { |
| 253 |
x: u32, |
| 254 |
y: u32, |
| 255 |
} |
| 256 |
|
| 257 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 258 |
seq! { |
| 259 |
Point { |
| 260 |
x: dec_uint, |
| 261 |
_: ',', |
| 262 |
y: dec_uint, |
| 263 |
_: empty |
| 264 |
} |
| 265 |
} |
| 266 |
.parse_next(input) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn seq_enum_struct_variant() { |
| 272 |
#[derive(Debug, PartialEq, Eq)] |
| 273 |
enum Expr { |
| 274 |
Add { lhs: u32, rhs: u32 }, |
| 275 |
Mul(u32, u32), |
| 276 |
} |
| 277 |
|
| 278 |
fn add<'i>(input: &mut &'i [u8]) -> TestResult<&'i [u8], Expr> { |
| 279 |
seq! {Expr::Add { |
| 280 |
lhs: dec_uint::<_, u32, _>, |
| 281 |
_: b" + ", |
| 282 |
rhs: dec_uint::<_, u32, _>, |
| 283 |
}} |
| 284 |
.parse_next(input) |
| 285 |
} |
| 286 |
|
| 287 |
fn mul<'i>(input: &mut &'i [u8]) -> TestResult<&'i [u8], Expr> { |
| 288 |
seq!(Expr::Mul( |
| 289 |
dec_uint::<_, u32, _>, |
| 290 |
_: b" * ", |
| 291 |
dec_uint::<_, u32, _>, |
| 292 |
)) |
| 293 |
.parse_next(input) |
| 294 |
} |
| 295 |
|
| 296 |
assert_parse!( |
| 297 |
add.parse_peek(&b"1 + 2"[..]), |
| 298 |
str![[r#" |
| 299 |
Ok( |
| 300 |
( |
| 301 |
[], |
| 302 |
Add { |
| 303 |
lhs: 1, |
| 304 |
rhs: 2, |
| 305 |
}, |
| 306 |
), |
| 307 |
) |
| 308 |
|
| 309 |
"#]] |
| 310 |
.raw() |
| 311 |
); |
| 312 |
|
| 313 |
assert_parse!( |
| 314 |
mul.parse_peek(&b"3 * 4"[..]), |
| 315 |
str![[r#" |
| 316 |
Ok( |
| 317 |
( |
| 318 |
[], |
| 319 |
Mul( |
| 320 |
3, |
| 321 |
4, |
| 322 |
), |
| 323 |
), |
| 324 |
) |
| 325 |
|
| 326 |
"#]] |
| 327 |
.raw() |
| 328 |
); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn seq_struct_borrow() { |
| 333 |
#![allow(dead_code)] |
| 334 |
|
| 335 |
#[derive(Debug, PartialEq)] |
| 336 |
struct Point { |
| 337 |
x: u32, |
| 338 |
y: u32, |
| 339 |
} |
| 340 |
|
| 341 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 342 |
let mut dec_uint = digit0.parse_to(); |
| 343 |
seq! { |
| 344 |
Point { |
| 345 |
x: dec_uint, |
| 346 |
_: ',', |
| 347 |
y: dec_uint, |
| 348 |
_: empty |
| 349 |
} |
| 350 |
} |
| 351 |
.parse_next(input) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn seq_tuple_struct_basics() { |
| 357 |
#[derive(Debug, PartialEq)] |
| 358 |
struct Point(u32, u32); |
| 359 |
|
| 360 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 361 |
seq! { |
| 362 |
Point( |
| 363 |
dec_uint, |
| 364 |
_: ',', |
| 365 |
dec_uint, |
| 366 |
) |
| 367 |
} |
| 368 |
.parse_next(input) |
| 369 |
} |
| 370 |
assert_parse!( |
| 371 |
parser.parse_peek("123,4 remaining"), |
| 372 |
str![[r#" |
| 373 |
Ok( |
| 374 |
( |
| 375 |
" remaining", |
| 376 |
Point( |
| 377 |
123, |
| 378 |
4, |
| 379 |
), |
| 380 |
), |
| 381 |
) |
| 382 |
|
| 383 |
"#]] |
| 384 |
.raw() |
| 385 |
); |
| 386 |
assert_parse!( |
| 387 |
parser.parse_peek("123, remaining"), |
| 388 |
str![[r#" |
| 389 |
Err( |
| 390 |
Backtrack( |
| 391 |
InputError { |
| 392 |
input: " remaining", |
| 393 |
}, |
| 394 |
), |
| 395 |
) |
| 396 |
|
| 397 |
"#]] |
| 398 |
.raw() |
| 399 |
); |
| 400 |
assert_parse!( |
| 401 |
parser.parse_peek(""), |
| 402 |
str![[r#" |
| 403 |
Err( |
| 404 |
Backtrack( |
| 405 |
InputError { |
| 406 |
input: "", |
| 407 |
}, |
| 408 |
), |
| 409 |
) |
| 410 |
|
| 411 |
"#]] |
| 412 |
.raw() |
| 413 |
); |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn seq_tuple_struct_trailing_comma_elided() { |
| 418 |
#![allow(dead_code)] |
| 419 |
|
| 420 |
#[derive(Debug, PartialEq)] |
| 421 |
struct Point(u32, u32); |
| 422 |
|
| 423 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 424 |
seq! { |
| 425 |
Point( |
| 426 |
dec_uint, |
| 427 |
_: ',', |
| 428 |
dec_uint, |
| 429 |
_: empty, |
| 430 |
) |
| 431 |
} |
| 432 |
.parse_next(input) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
#[test] |
| 437 |
fn seq_tuple_struct_no_trailing_comma() { |
| 438 |
#![allow(dead_code)] |
| 439 |
|
| 440 |
#[derive(Debug, PartialEq)] |
| 441 |
struct Point(u32, u32); |
| 442 |
|
| 443 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 444 |
seq! { |
| 445 |
Point( |
| 446 |
dec_uint, |
| 447 |
_: ',', |
| 448 |
dec_uint |
| 449 |
) |
| 450 |
} |
| 451 |
.parse_next(input) |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
#[test] |
| 456 |
fn seq_tuple_struct_no_trailing_comma_elided() { |
| 457 |
#![allow(dead_code)] |
| 458 |
|
| 459 |
#[derive(Debug, PartialEq)] |
| 460 |
struct Point(u32, u32); |
| 461 |
|
| 462 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 463 |
seq! { |
| 464 |
Point( |
| 465 |
dec_uint, |
| 466 |
_: ',', |
| 467 |
dec_uint, |
| 468 |
_: empty |
| 469 |
) |
| 470 |
} |
| 471 |
.parse_next(input) |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn seq_tuple_basics() { |
| 477 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, (u32, u32)> { |
| 478 |
seq! { |
| 479 |
( |
| 480 |
dec_uint, |
| 481 |
_: ',', |
| 482 |
dec_uint, |
| 483 |
) |
| 484 |
} |
| 485 |
.parse_next(input) |
| 486 |
} |
| 487 |
assert_parse!( |
| 488 |
parser.parse_peek("123,4 remaining"), |
| 489 |
str![[r#" |
| 490 |
Ok( |
| 491 |
( |
| 492 |
" remaining", |
| 493 |
( |
| 494 |
123, |
| 495 |
4, |
| 496 |
), |
| 497 |
), |
| 498 |
) |
| 499 |
|
| 500 |
"#]] |
| 501 |
.raw() |
| 502 |
); |
| 503 |
assert_parse!( |
| 504 |
parser.parse_peek("123, remaining"), |
| 505 |
str![[r#" |
| 506 |
Err( |
| 507 |
Backtrack( |
| 508 |
InputError { |
| 509 |
input: " remaining", |
| 510 |
}, |
| 511 |
), |
| 512 |
) |
| 513 |
|
| 514 |
"#]] |
| 515 |
.raw() |
| 516 |
); |
| 517 |
assert_parse!( |
| 518 |
parser.parse_peek(""), |
| 519 |
str![[r#" |
| 520 |
Err( |
| 521 |
Backtrack( |
| 522 |
InputError { |
| 523 |
input: "", |
| 524 |
}, |
| 525 |
), |
| 526 |
) |
| 527 |
|
| 528 |
"#]] |
| 529 |
.raw() |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn seq_tuple_trailing_comma_elided() { |
| 535 |
#![allow(dead_code)] |
| 536 |
|
| 537 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, (u32, u32)> { |
| 538 |
seq! { |
| 539 |
( |
| 540 |
dec_uint, |
| 541 |
_: ',', |
| 542 |
dec_uint, |
| 543 |
_: empty, |
| 544 |
) |
| 545 |
} |
| 546 |
.parse_next(input) |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
#[test] |
| 551 |
fn seq_tuple_no_trailing_comma() { |
| 552 |
#![allow(dead_code)] |
| 553 |
|
| 554 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, (u32, u32)> { |
| 555 |
seq! { |
| 556 |
( |
| 557 |
dec_uint, |
| 558 |
_: ',', |
| 559 |
dec_uint |
| 560 |
) |
| 561 |
} |
| 562 |
.parse_next(input) |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn seq_tuple_no_trailing_comma_elided() { |
| 568 |
#![allow(dead_code)] |
| 569 |
|
| 570 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, (u32, u32)> { |
| 571 |
seq! { |
| 572 |
( |
| 573 |
dec_uint, |
| 574 |
_: ',', |
| 575 |
dec_uint, |
| 576 |
_: empty |
| 577 |
) |
| 578 |
} |
| 579 |
.parse_next(input) |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
#[test] |
| 584 |
fn seq_tuple_no_parens() { |
| 585 |
#![allow(dead_code)] |
| 586 |
|
| 587 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, (u32, u32)> { |
| 588 |
seq! ( |
| 589 |
dec_uint, |
| 590 |
_: ',', |
| 591 |
dec_uint, |
| 592 |
) |
| 593 |
.parse_next(input) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
#[test] |
| 598 |
fn seq_tuple_borrow() { |
| 599 |
#![allow(dead_code)] |
| 600 |
|
| 601 |
#[derive(Debug, PartialEq)] |
| 602 |
struct Point(u32, u32); |
| 603 |
|
| 604 |
fn parser<'i>(input: &mut &'i str) -> TestResult<&'i str, Point> { |
| 605 |
let mut dec_uint = digit0.parse_to(); |
| 606 |
seq! { |
| 607 |
Point( |
| 608 |
dec_uint, |
| 609 |
_: ',', |
| 610 |
dec_uint, |
| 611 |
_: empty |
| 612 |
) |
| 613 |
} |
| 614 |
.parse_next(input) |
| 615 |
} |
| 616 |
} |