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
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
// Copyright 2018 Ethan Pailes.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/*!
This crate provides a library for writing maintainable regular expressions.
When regex are small, their terse syntax is nice because it allows you
to say a lot with very little. As regex grow in size, this terse syntax
quickly becomes a liability, interfering with maintainability.
The [regex crate][regexcrate] provides an
[extended mode][emode] which allows users to write regex containing
insignificant whitespace and add comments to their regex. This crate
takes that idea a step further by allowing you to factor your regex
into different named pieces, then recombine the pieces later using
familiar syntax.

The actual rust API surface of this crate is intentionally small.
Most of the functionality provided comes as part of the remake
language.

# Example: A mostly-wrong URI validator
```rust
use remake::Remake;
let web_uri_re = Remake::compile(r#"
    let scheme = /https?:/ . '//';
    let auth = /[\w\.\-_]+/;
    let path = ('/' . /[\w\-_]+/)*;
    let query_body = (/[\w\.\-_?]/ | '/')*;
    let frag_body = cap query_body as frag;

      /^/
    . scheme . auth . path
    . ('?' . query_body)?
    . ('#' . frag_body)?
    . /$/
    "#).unwrap();

assert!(web_uri_re.is_match("https://www.rust-lang.org"));
assert!(web_uri_re.is_match("https://github.com/ethanpailes/remake"));

assert_eq!(
    web_uri_re
        .captures("https://tools.ietf.org/html/rfc3986#section-1.1.3").unwrap()
        .name("frag").unwrap().as_str(),
        "section-1.1.3");
```

# Regular Expression Literals

Remake is all about manipulating regular expressions, so regex
literals are one of the key features. In remake, we denote a
regex literal by bracketing a regex in slashes. This is similar
to the approach taken by languages like javascript.

```rust
use remake::Remake;
let re = Remake::compile(r" /foo|bar/ ").unwrap();

assert!(re.is_match("foo"));
```

If you want to include a forward slash in a regex literal, you will
have to escape it with a backslash.

```rust
use remake::Remake;
let re = Remake::compile(r" /a regex with a \/ slash/ ").unwrap();

assert!(re.is_match("a regex with a / slash"));
```

A common issue when writing a regex is not knowing if a particular
sigil has special meaning in a regex. Even if you know that '+' is
special, it can be easy to forget to escape it, especially
as your regex grows in complexity. To help with these situations,
remake provides a second type of regex literal using single quotes.
In a single-quote regex literal, there are no special characters.

```rust
use remake::Remake;
let re = Remake::compile(r" 'foo|bar' ").unwrap();

assert!(!re.is_match("foo"));
assert!(re.is_match("foo|bar"));
```

# Combining Regex

The ability to pull regex apart into separate literals is not that
useful without the ability to put them back together. Remake provides
a number of operators for combining regex, corresponding very closely to
ordinary regex operators.

## Composition

We use the `.` operator to indicate regex composition, also known as regex
concatenation. There is no syntax for composition in ordinary regex, instead
expressions written next to each other are simply composed automatically.
In remake, we are more explicit.

```rust
use remake::Remake;
let re = Remake::compile(r" 'f+oo' . /a*b/ ").unwrap();

assert!(re.is_match("f+oob"));
assert!(re.is_match("f+ooaaaaaaaaaaaaaaab"));
```

## Choice

Just like in standard regex syntax, we use the pipe operator to indicate
choice between two different remake expressions.

```rust
use remake::Remake;
let re = Remake::compile(r" 'foo' | 'bar' ").unwrap();

assert!(re.is_match("foo"));
```

## Kleene Star

Just like in standard regex syntax, we use a unary postfix `*` operator to
ask for zero or more repetitions of an expression.

```rust
use remake::Remake;
let re = Remake::compile(r" 'a'* . 'b' ").unwrap();

assert!(re.is_match("aaaaab"));
assert!(re.is_match("b"));
```

remake supports lazy Kleene star as well

```rust
use remake::Remake;
let re = Remake::compile(r" 'a'*? ").unwrap();

assert_eq!(re.find("aaaaa").unwrap().as_str(), "");
```

## Kleene Plus

As you might expect, remake also has syntax for repeating
an expression one or more times.

```rust
use remake::Remake;
let re = Remake::compile(r" 'a'+ . 'b' ").unwrap();
assert!(re.is_match("aaaaab"));
assert!(!re.is_match("b"));
```

and there is a lazy variant

```rust
use remake::Remake;
let re = Remake::compile(r" 'a'+? ").unwrap();

assert_eq!(re.find("aaaaa").unwrap().as_str(), "a");
```

## Counted Repetition

[regex][regexcrate] supports a couple of different ways to
ask for a counted repetition. Remake supports them all.

```rust
use remake::Remake;
let re_a_1 = Remake::compile(r" 'a'{1} ").unwrap();
let re_a_1ormore = Remake::compile(r" 'a'{1,} ").unwrap();
let re_between_2_and_5 = Remake::compile(r" 'a'{2,5} ").unwrap();

assert!(re_a_1.is_match("a"));
assert!(re_a_1ormore.is_match("aaaaaa"));
assert!(re_between_2_and_5.is_match("aaaaa"));

let re_a_1_lazy = Remake::compile(r" 'a'{1}? ").unwrap();
let re_a_1ormore_lazy = Remake::compile(r" 'a'{1,}? ").unwrap();
let re_between_2_and_5_lazy = Remake::compile(r" 'a'{2,5}? ").unwrap();

assert!(re_a_1_lazy.is_match("a"));
assert!(re_a_1ormore_lazy.is_match("aaaaaa"));
assert!(re_between_2_and_5_lazy.is_match("aaaaa"));
```

## Capture Groups

Regex can be annotated with parentheses to ask the engine to
take note of where particular sub-expressions occur in a match.
You can access these sub-expressions by invoking the
[`Regex::captures`](struct.Regex.html#method.captures)
method. Remake already uses parentheses to control precedence, so
it would be confusing to also use them as capturing syntax. Instead,
we introduce the `cap` keyword. You can ask for an unnamed capture
group by writing `cap <expr>`

```rust
use remake::Remake;
let re = Remake::compile(r" 'a' . cap 'b' . 'c' ").unwrap();

assert_eq!(&re.captures("abc").unwrap()[1], "b");
```

or give it a name with `cap <expr> as <name>`

```rust
use remake::Remake;
let re = Remake::compile(r" 'a' . cap 'b' as group . 'c' ").unwrap();
assert_eq!(re.captures("abc").unwrap().name("group").unwrap().as_str(), "b");
```

capture groups compose well with capture groups defined in a regex
literal

```rust
use remake::Remake;
let re = Remake::compile(r" /foo(bar)/ . cap 'baz' ").unwrap();

let caps = re.captures("foobarbaz").unwrap();
assert_eq!(&caps[1], "bar");
assert_eq!(&caps[2], "baz");
```

Note that the index of unnamed capture groups depends on their
order in the final expression that is generated, not where they
are defined in a particular piece of remake source. The process of
index assignment is not particularly implicit, so it is good practice
to use named capture groups as regex grow large.

# Block Expressions

Just like rust, remake supports block expressions to introduce
new scopes. A block starts with an open curly brace (`{`), contains
zero or more statements followed by an expression, and ends with
a closing curly brace (`}`).

```rust
use remake::Remake;
let re = Remake::compile(r#"
    {
        let foo_re = 'foo';
        foo_re | /bar/
    }
    "#).unwrap();

assert!(re.is_match("bar"));
```

For convenience, the top level of a piece of remake source is
automatically treated as the inside of a block.

```rust
use remake::Remake;
let re = Remake::compile(r#"
    let foo_re = 'foo';
    foo_re | /bar/
    "#).unwrap();

assert!(re.is_match("bar"));
```

# Let Statements

As shown above, the `let` keyword can be used to bind expressions
to a name in the current scope. 

# Error Messages

Nice error messages are key to developer productivity, so remake
endeavors to provide human friendly error messages. Remake error
messages implement a `Debug` instance written with the expectation
that the most common way to use remake is
`Remake::compile(...).unwrap()`. This means that the error messages
automatically show up with nice formatting in the middle of the
output from `rustc`. Good error messages are important, so if you
find one confusing and have an idea about how to improve it, please
[share your idea](https://github.com/ethanpailes/remake/issues).

## Example: A Bad Literal

The code

```rust,should_panic
use remake::Remake;
let _re = Remake::compile(r" /unclosed literal ").unwrap();
```

will fail with the error message

```text
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
remake parse error:
    at line 1, col 2:
    0001 >  /unclosed literal
            ^
Invalid token.
```

# Comments

remake has C-style comments like rust

```rust
use remake::Remake;
let re = Remake::compile(r#"
    // this is a line comment
    let foo_re = 'foo';
    /* and this is a 
     * block comment
     */
    foo_re | /bar/
    /* just like /* in rust */ block comments can be nested */
    "#).unwrap();

assert!(re.is_match("bar"));
```

[emode]: https://github.com/rust-lang/regex#usage
[regexcrate]: https://github.com/rust-lang/regex
*/

extern crate failure;
extern crate lalrpop_util;
pub extern crate regex;
extern crate regex_syntax;

mod ast;
mod error;
mod lex;
mod operators;
mod parse;
mod util;

//
// I want the eventual interface to look something like:
//
// ```
// let r = Remake::new(r"/foo/")?;
// let wrap_parens = Remake::new(r"fn (re) { '(' + . re . ')' }")?;
// let re: Regex = wrap_parens.apply(r)?.eval()?;
// ```
//
// The idea is that remake expressions can be parsed and then passed
// around within rust as opaque expressions. They can then be combined
// through function application.
//
// This design would work well with an ML-style all-functions-have-
// one-argument sort of approach, but I want remake to semantically
// be basically dynamically typed rust with a strong focus on regular
// expression manipulation. A possible solution is some light trait
// abuse where we define a sealed (using the sealed trait pattern)
// RemakeArgument trait and then impl it for Remake, tuples of RemakeArgument,
// (and Vecs of RemakeArgument, IntoIters of RemakeArgument,
//  and HashMaps of RemakeArgument once corresponding data structures
//  have been added to the language). I think the user would mostly not
// need to know about the RemakeArgument trait.
//

use error::InternalError;
use std::fmt;

/// A remake expression, which can be compiled into a regex.
#[derive(Clone)]
pub struct Remake {
    /// The parsed remake expression.
    expr: ast::Expr,
    /// The source used to construct this remake expression.
    ///
    /// Required to interpret spans.
    src: String,
}

impl Remake {
    /// Evaluate some remake source to produce a regular expression.
    ///
    /// # Example: A mostly-wrong URI validator
    /// ```rust
    /// use remake::Remake;
    /// let web_uri_re = Remake::compile(
    ///     r#"
    ///     let scheme = /https?:/ . '//';
    ///     let auth = /[\w\.\-_]+/;
    ///     let path = ('/' . /[\w\-_]+/)*;
    ///     let query_body = (/[\w\.\-_?]/ | '/')*;
    ///     let frag_body = cap query_body as frag;
    ///
    ///       /^/
    ///     . scheme . auth . path
    ///     . ('?' . query_body)?
    ///     . ('#' . frag_body)?
    ///     . /$/
    ///     "#,
    /// ).unwrap();
    ///
    /// assert!(web_uri_re.is_match("https://www.rust-lang.org"));
    /// assert!(web_uri_re.is_match("https://github.com/ethanpailes/remake"));
    ///
    /// assert_eq!(
    ///     web_uri_re
    ///         .captures("https://tools.ietf.org/html/rfc3986#section-1.1.3")
    ///         .unwrap()
    ///         .name("frag")
    ///         .unwrap()
    ///         .as_str(),
    ///     "section-1.1.3"
    /// );
    /// ```
    ///
    /// # Errors:
    ///
    /// Calling `compile` on some remake source might result in an
    /// [`Error`](enum.Error.html).
    pub fn compile(src: &str) -> Result<regex::Regex, Error> {
        Self::new(String::from(src))?.eval()
    }

    /// Construct a remake expression which can be evaluated
    /// at a later time.
    ///
    /// To go directly from a remake expression to a `Regex`,
    /// see `Remake::compile`.
    ///
    /// # Example:
    /// ```rust
    /// # use remake::Remake;
    /// # fn ex_main() -> Result<(), remake::Error> {
    /// let remake_expr = Remake::new(r" 'a literal' ".to_string())?;
    /// # Ok(())
    /// # }
    /// # fn main() { ex_main().unwrap() }
    /// ```
    ///
    /// # Errors:
    ///
    /// Calling `new` on some remake source might result in a
    /// [`Error::ParseError`](enum.Error.html).
    pub fn new(src: String) -> Result<Remake, Error> {
        let mut remake = Remake {
            expr: ast::Expr::new(
                ast::ExprKind::ExprPoison,
                ast::Span {
                    start: 0,
                    end: 0,
                },
            ),
            src: src,
        };

        {
            let parser = parse::BlockBodyParser::new();
            let lexer = lex::Lexer::new(&remake.src);
            remake.expr = match parser.parse(lexer) {
                Ok(expr) => expr,
                Err(err) => {
                    return Err(Error::ParseError(match err {
                        lalrpop_util::ParseError::User { error } => {
                            format!("{}", error.overlay(&remake.src))
                        }

                        lalrpop_util::ParseError::InvalidToken { location } => {
                            format!(
                                "{}",
                                InternalError::new(
                                    error::ErrorKind::InvalidToken,
                                    ast::Span {
                                        start: location,
                                        end: location + 1,
                                    }
                                ).overlay(&remake.src)
                            )
                        }

                        lalrpop_util::ParseError::UnrecognizedToken {
                            token: Some((l, tok, r)),
                            expected,
                        } => format!(
                            "{}",
                            InternalError::new(
                                error::ErrorKind::UnrecognizedToken {
                                    token: tok.to_string(),
                                    expected: expected,
                                },
                                ast::Span {
                                    start: l,
                                    end: r
                                }
                            ).overlay(&remake.src)
                        ),

                        err => format!("{}", err),
                    }));
                }
            };
        }

        Ok(remake)
    }

    /// Evaluate a remake expression.
    ///
    /// # Example:
    /// ```rust
    /// # use remake::Remake;
    /// # fn ex_main() -> Result<(), remake::Error> {
    /// let remake_expr = Remake::new(r" 'a [li[teral' ".to_string())?;
    /// let re = remake_expr.eval()?;
    ///
    /// assert!(re.is_match("a [li[teral"));
    /// # Ok(())
    /// # }
    /// # fn main() { ex_main().unwrap() }
    /// ```
    ///
    /// # Errors:
    ///
    /// Calling `eval` on a remake expression might result in a
    /// [`Error::RuntimeError`](enum.Error.html).
    pub fn eval(self) -> Result<regex::Regex, Error> {
        match self.expr.eval() {
            Ok(ast) => Ok(regex::Regex::new(&ast.to_string()).unwrap()),
            Err(err) => Err(Error::RuntimeError(format!(
                "{}",
                err.overlay(&self.src)
            ))),
        }
    }
}

/// A remake error with a descriptive human-readable message explaining
/// what went wrong.
#[derive(Clone)]
pub enum Error {
    /// A parse error occurred.
    ParseError(String),

    /// A runtime error occurred.
    RuntimeError(String),
}

impl failure::Fail for Error {}

// The debug formatter already provides a user-facing error so
// that .unwrap() will result in quick feedback.
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Error::*;

        match self {
            &ParseError(ref err) => {
                writeln!(f, "\nremake parse error:")?;
                writeln!(f, "{}", err)?;
            }

            &RuntimeError(ref err) => {
                writeln!(f, "\nremake evaluation error:")?;
                writeln!(f, "{}", err)?;
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! mat {
        ($test_name:ident, $remake_src:expr, $input:expr) => {
            #[test]
            fn $test_name() {
                let re = Remake::compile($remake_src).unwrap();
                assert!(
                    re.is_match($input),
                    format!("/{:?}/ does not match {:?}.", re, $input)
                );
            }
        };
    }

    macro_rules! captures {
        ($test_name:ident,
         $remake_src:expr,
         $input:expr,
         $( $group:expr ),*) => {
            #[test]
            fn $test_name() {
                let re = Remake::compile($remake_src)
                            .expect("The regex to compile.");
                let expected = vec![$($group),*];
                let actual = re.captures($input)
                               .expect("The regex to match.")
                               .iter()
                               .map(|mat| mat.map(|g| g.as_str()))
                               .collect::<Vec<_>>();

                assert_eq!(&expected[..], &actual[1..],
                    "Captures did not match. re={:?}", re);
            }
        }
    }

    macro_rules! captures_named {
        ($test_name:ident,
         $remake_src:expr,
         $input:expr,
         $( $group:expr ),*) => {
            #[test]
            fn $test_name() {
                use std::str::FromStr;

                let re = Remake::compile($remake_src)
                            .expect("The regex to compile.");
                let expected_caps = vec![$($group),*];
                let num_name_re =
                    Remake::compile("'_' . cap /[0-9]+/").unwrap();

                let caps = re.captures($input).expect("The regex to match.");

                for &(name, result) in expected_caps.iter() {
                    let cap = match num_name_re.captures(name) {
                        Some(c) =>
                            caps.get(usize::from_str(&c[1]).unwrap())
                                .map(|m| m.as_str()),
                        None => caps.name(name).map(|m| m.as_str()),
                    };

                    assert_eq!(cap, result, "Group '{}' did not match", name);
                }
            }
        }
    }

    macro_rules! no_mat {
        ($test_name:ident, $remake_src:expr, $input:expr) => {
            #[test]
            fn $test_name() {
                let re = Remake::compile($remake_src).unwrap();
                assert!(
                    !re.is_match($input),
                    format!("/{:?}/ matches {:?}.", re, $input)
                );
            }
        };
    }

    macro_rules! error_pre {
        ($test_name:ident, $remake_src:expr, $expected_err_str:expr) => {
            #[test]
            fn $test_name() {
                let result = Remake::compile($remake_src);
                match &result {
                    &Err(ref err) => {
                        let err_msg = format!("{}", err);
                        // When copying the right output into the test
                        // case uncommenting this can help debug.
                        //
                        // assert_eq!($expected_err_str, err_msg);

                        if !err_msg.starts_with($expected_err_str) {
                            // We unwrap rather than asserting or something
                            // a little more reasonable so that we can see
                            // the error output as the user sees it.
                            result.clone().unwrap();
                        }
                    }
                    &Ok(ref res) => panic!("Should not eval. res={:?}", res),
                }
            }
        };
    }

    //
    // remake literals
    //

    mat!(lit_1, r"/a/", "a");
    mat!(lit_2, r"'a'", "a");
    mat!(lit_3, r"/\p{Currency_Symbol}/", r"$");
    mat!(
        lit_4,
        r"'\p{Currency_Symbol}'",
        r"\p{Currency_Symbol}"
    );
    mat!(
        lit_5,
        r"'\u{Currency_Symbol}'",
        r"\u{Currency_Symbol}"
    );

    //
    // remake parse errors
    //

    error_pre!(
        unmatched_tick_1_,
        r"'a",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > 'a
           ^^
remake lexical error:
Unclosed raw regex literal."#
    );

    error_pre!(
        unmatched_tick_2_,
        r"a'",
        r#"
remake parse error:
    at line 1, col 2:
    0001 > a'
            ^
remake lexical error:
Unclosed raw regex literal."#
    );

    error_pre!(
        unmatched_slash_1_,
        r"/a",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > /a
           ^^
remake lexical error:
Unclosed regex literal."#
    );

    error_pre!(
        unmatched_slash_2_,
        r"a/",
        r#"
remake parse error:
    at line 1, col 2:
    0001 > a/
            ^
remake lexical error:
Unclosed regex literal."#
    );

    error_pre!(
        unmatched_tick_slash_1_,
        r"'a/",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > 'a/
           ^^^
remake lexical error:
Unclosed raw regex literal."#
    );

    error_pre!(
        unmatched_tick_slash_2_,
        r"/a'",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > /a'
           ^^^
remake lexical error:
Unclosed regex literal."#
    );

    //
    // parse errors that bubble up from the regex crate
    //

    error_pre!(
        re_parse_err_1_,
        r"/a[/",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > /a[/
           ^^^^
Error parsing the regex literal: /a[/
    regex parse error:
        a[
         ^
    error: unclosed character class"#
    );

    error_pre!(
        re_parse_err_2_,
        r"/a[]/",
        r#"
remake parse error:
    at line 1, col 1:
    0001 > /a[]/
           ^^^^^
Error parsing the regex literal: /a[]/
    regex parse error:
        a[]
         ^^
    error: unclosed character class"#
    );

    error_pre!(
        re_multiline_parse_err_1_,
        r#"

            /a[/

        "#,
        r#"
remake parse error:
    at line 3, col 13:
    0002 > 
    0003 >             /a[/
                       ^^^^
    0004 > 
Error parsing the regex literal: /a[/
    regex parse error:
        a[
         ^
    error: unclosed character class"#
    );

    error_pre!(
        unrecognized_token_1_,
        r"/foo/ /foo/",
        r#"
remake parse error:
    at line 1, col 7:
    0001 > /foo/ /foo/
                 ^^^^^
Unexpected token '/foo/'. Expected one of:"#
    );

    error_pre!(
        binary_plus_1_,
        r"/foo/ + /bar/",
        r#"
remake parse error:
    at line 1, col 9:
    0001 > /foo/ + /bar/
                   ^^^^^
Unexpected token '/bar/'. Expected one of:"#
    );

    error_pre!(
        binary_plus_multiline_1_,
        r#"/foo/ +
        /bar
        /"#,
        r#"
remake parse error:
    starting at line 2, col 9 and ending at line 3, col 10:
    0001  > /foo/ +
    0002  >         /bar
    start >         ^
    0003  >         /
    end   >         ^
Unexpected token '/bar
        /'. Expected one of:"#
    );

    error_pre!(
        binary_plus_multiline_2_,
        r#"
        /foo/ + /
        
        bar



        /"#,
        r#"
remake parse error:
    starting at line 2, col 17 and ending at line 8, col 10:
    0001  > 
    0002  >         /foo/ + /
    start >                 ^
    0003  >         
    ...
    0007  > 
    0008  >         /
    end   >         ^
Unexpected token"#
    );

    //
    // Remake expressions.
    //

    mat!(concat_1_, r"/foo/ . 'bar'", "foobar");
    mat!(concat_2_, r"/foo/ . 'b[r'", "foob[r");
    mat!(alt_1_, r"/foo/ | /bar/", "foo");

    mat!(greedy_star_1_, r"/a/ *", "aaaaaaa");
    mat!(greedy_star_2_, r"/a/ * . 'b'", "aaaaaaab");
    mat!(greedy_star_3_, r"/a/ * . 'b'", "b");

    mat!(lazy_star_1_, r"/a/ *?", "aaaaaaa");
    mat!(lazy_star_2_, r"/a/ *? . 'b'", "aaaaaaab");
    mat!(lazy_star_3_, r"/a/ *? . 'b'", "b");

    mat!(greedy_plus_1_, r"/a/ +", "aaaaaaa");
    mat!(greedy_plus_2_, r"/a/ + . 'b'", "aaaaaaab");
    no_mat!(greedy_plus_3_, r"/a/ + . 'b'", "b");

    mat!(lazy_plus_1_, r"/a/ +?", "aaaaaaa");
    mat!(lazy_plus_2_, r"/a/ +? . 'b'", "aaaaaaab");
    no_mat!(lazy_plus_3_, r"/a/ +? . 'b'", "b");

    mat!(greedy_question_1_, r"/a/ ?", "a");
    mat!(greedy_question_2_, r"/a/ ? . 'b'", "ab");
    mat!(greedy_question_3_, r"/a/ ? . 'b'", "b");

    mat!(lazy_question_1_, r"/a/ ??", "a");
    mat!(lazy_question_2_, r"/a/ ?? . 'b'", "ab");
    mat!(lazy_question_3_, r"/a/ ?? . 'b'", "b");

    mat!(greedy_exact_1_, r"/a/ {3}", "aaa");
    mat!(greedy_exact_2_, r"/a/ { 3} . 'b'", "aaab");

    // lazyness does not matter for an exact repetition
    no_mat!(lazy_exact_1_, r"/a/ {3}?", "a");
    no_mat!(lazy_exact_2_, r"/a/ {3 }? . 'b'", "ab");
    mat!(lazy_exact_3_, r"/a/ {3}?", "aaa");
    mat!(lazy_exact_4_, r"/a/ {3 }? . 'b'", "aaab");

    mat!(greedy_atleast_1_, r"/a/ {3,}", "aaaaaaaa");
    mat!(
        greedy_atleast_2_,
        r"/a/ { 3 , } . 'b'",
        "aaaaaab"
    );
    no_mat!(greedy_atleast_3_, r"/a/ { 3 , } . 'b'", "ab");

    mat!(lazy_atleast_1_, r"/a/ {3,}?", "aaaaaaaa");
    mat!(
        lazy_atleast_2_,
        r"/a/ { 3 , }? . 'b'",
        "aaaaaab"
    );
    no_mat!(lazy_atleast_3_, r"/a/ { 3 , }? . 'b'", "ab");

    captures!(cap_basic_1_, r"/a(b)a/", "aba", Some("b"));

    captures!(
        cap_remake_1_,
        r"/a/ . cap /b/ . /a/",
        "aba",
        Some("b")
    );
    captures!(
        cap_remake_2_,
        r"cap (/a/ . cap /b/ . /a/)",
        "aba",
        Some("aba"),
        Some("b")
    );

    captures!(
        cap_remake_mixed_1_,
        r"cap (/a/ . /(b)/ . /a/)",
        "aba",
        Some("aba"),
        Some("b")
    );
    captures!(
        cap_remake_mixed_2_,
        r"/(a)/ . cap (/a/ . /(b)/ . /a/)",
        "aaba",
        Some("a"),
        Some("aba"),
        Some("b")
    );
    captures!(
        cap_remake_mixed_3_,
        r"cap /blah/ . (/(a)/ | (/b/ . cap /c/))",
        "blahbc",
        Some("blah"),
        None,
        Some("c")
    );
    captures!(
        cap_remake_mixed_4_,
        r"/(a)(b)(c)/ . cap /blah/",
        "abcblah",
        Some("a"),
        Some("b"),
        Some("c"),
        Some("blah")
    );

    captures_named!(
        cap_remake_named_1_,
        r"/a/ . cap /b/ as foo . /a/",
        "aba",
        ("foo", Some("b"))
    );
    captures_named!(
        cap_remake_named_2_,
        r"cap cap /foo/ as blah",
        "foo",
        ("_1", Some("foo")),
        ("blah", Some("foo"))
    );

    mat!(block_basic_1_, r"{ /a/ }", "a");

    mat!(block_repeat_1_, r"{ /a/{2} }", "aa");
    no_mat!(block_repeat_2_, r"{ /a/{3,} }", "aa");

    mat!(
        block_unused_let_1_,
        r#"{
        let foo = /a/;
        /b/
    }"#,
        "b"
    );

    mat!(
        toplevel_unused_let_1_,
        r#"
        let foo = /a/;
        /b/
    "#,
        "b"
    );

    mat!(
        toplevel_let_1_,
        r#"
        let foo = /a/;
        foo
    "#,
        "a"
    );

    mat!(
        toplevel_let_2_,
        r#"
        let foo = /a/;
        foo . /bar/
    "#,
        "abar"
    );

    error_pre!(
        name_error_1_,
        r#"
        foo
    "#,
        r#"
remake evaluation error:
    at line 2, col 9:
    0001 > 
    0002 >         foo
                   ^^^
    0003 >     
NameError: unknown variable 'foo'."#
    );

    error_pre!(num_too_long_1_,
        r"'a'{11111111111111111111111111111111111111111111111111111111}",
        r#"
remake parse error:
    at line 1, col 5:
    0001 > 'a'{11111111111111111111111111111111111111111111111111111111}
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
remake lexical error:
Error parsing '11111111111111111111111111111111111111111111111111111111' as a number:"#);

    mat!(alt_merge_1_, r"/a|b/ | /c/", "c");
    mat!(alt_merge_2_, r"/c/ | /a|b/", "c");
    mat!(alt_merge_3_, r"/c|d/ | /a|b/", "d");

    mat!(
        block_1_,
        r#"
        let gen_delims = ':' | '/' | '?' | '[' | ']' | '@';
        'foo'
    "#,
        "foo"
    );

    mat!(
        repeated_concat_1_,
        r"/^/ . ('/' . /[\w\-_]+/)* . /$/",
        ""
    );
    mat!(
        repeated_concat_2_,
        r"/^/ . ('/' . /[\w\-_]+/)* . /$/",
        "/a/b/c"
    );

    mat!(
        repeated_concat_3_,
        r"/^/ . ('/' . /[\w\-_]+?/)* . /$/",
        ""
    );
    mat!(
        repeated_concat_4_,
        r"/^/ . ('/' . /[\w\-_]+?/)* . /$/",
        "/a/b/c"
    );

    mat!(
        repeated_concat_5_,
        r"/^/ . ('/' . /[\w\-_]+/)+ . /$/",
        "/a/b/c"
    );
    mat!(
        repeated_concat_6_,
        r"/^/ . ('/' . /[\w\-_]+/)+? . /$/",
        "/a/b/c"
    );

    mat!(
        repeated_concat_7_,
        r"/^/ . ('/' . /[\w\-_]+/){3} . /$/",
        "/a/b/c"
    );
    mat!(
        repeated_concat_8_,
        r"/^/ . ('/' . /[\w\-_]+/){3}? . /$/",
        "/a/b/c"
    );

    mat!(
        repeated_concat_9_,
        r"/^/ . ('/' . /[\w\-_]+/){1,} . /$/",
        "/a/b/c"
    );
    mat!(
        repeated_concat_10_,
        r"/^/ . ('/' . /[\w\-_]+/){1,}? . /$/",
        "/a/b/c"
    );

    mat!(
        repeated_concat_11_,
        r"/^/ . ('/' . /[\w\-_]+/){1,5} . /$/",
        "/a/b/c"
    );
    mat!(
        repeated_concat_12_,
        r"/^/ . ('/' . /[\w\-_]+/){1,5}? . /$/",
        "/a/b/c"
    );
}