Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
*/
bool scoped() const noexcept
{
return scoped_;
}
//---------------------------------------------------------------
/** @brief determines if children are mutually exclusive alternatives */
group& exclusive(bool yes) {
exclusive_ = yes;
return *this;
}
/** @brief returns if children are mutually exclusive alternatives */
bool exclusive() const noexcept {
return exclusive_;
}
//---------------------------------------------------------------
/** @brief returns true, if any child is required to match */
bool any_required() const
{
return std::any_of(children_.begin(), children_.end(),
[](const child& n){ return n.required(); });
}
/** @brief returns true, if all children are required to match */
bool all_required() const
{
return std::all_of(children_.begin(), children_.end(),
[](const child& n){ return n.required(); });
}
//---------------------------------------------------------------
/** @brief returns true if any child is optional (=non-required) */
bool any_optional() const {
return !all_required();
}
/** @brief returns true if all children are optional (=non-required) */
bool all_optional() const {
return !any_required();
}
//---------------------------------------------------------------
/** @brief returns if the entire group is blocking / positional */
bool blocking() const noexcept {
return token<group>::blocking() || (exclusive() && all_blocking());
}
//-----------------------------------------------------
/** @brief determines if the entire group is blocking / positional */
group& blocking(bool yes) {
return token<group>::blocking(yes);
}
//---------------------------------------------------------------
/** @brief returns true if any child is blocking */
bool any_blocking() const
{
return std::any_of(children_.begin(), children_.end(),
[](const child& n){ return n.blocking(); });
}
//---------------------------------------------------------------
/** @brief returns true if all children is blocking */
bool all_blocking() const
{
return std::all_of(children_.begin(), children_.end(),
[](const child& n){ return n.blocking(); });
}
//---------------------------------------------------------------
/** @brief returns if any child is a value parameter (recursive) */
bool any_flagless() const
{
return std::any_of(children_.begin(), children_.end(),
[](const child& p){
return p.is_param() && p.as_param().flags().empty();
});
}
/** @brief returns if all children are value parameters (recursive) */
bool all_flagless() const
{
return std::all_of(children_.begin(), children_.end(),
[](const child& p){
return p.is_param() && p.as_param().flags().empty();
});
}
//---------------------------------------------------------------
/** @brief adds child parameter at the end */
group&
push_back(const parameter& v) {
children_.emplace_back(v);
return *this;
}
//-----------------------------------------------------
/** @brief adds child parameter at the end */
group&
push_back(parameter&& v) {
children_.emplace_back(std::move(v));
return *this;
}
//-----------------------------------------------------
/** @brief adds child group at the end */
group&
push_back(const group& g) {
children_.emplace_back(g);
return *this;
}
//-----------------------------------------------------
/** @brief adds child group at the end */
group&
push_back(group&& g) {
children_.emplace_back(std::move(g));
return *this;
}
//-----------------------------------------------------
/** @brief adds children (groups and/or parameters) */
template<class Param1, class Param2, class... Params>
group&
push_back(Param1&& param1, Param2&& param2, Params&&... params)
{
children_.reserve(children_.size() + 2 + sizeof...(params));
push_back(std::forward<Param1>(param1));
push_back(std::forward<Param2>(param2), std::forward<Params>(params)...);
return *this;
}
//---------------------------------------------------------------
/** @brief adds child parameter at the beginning */
group&
push_front(const parameter& v) {
children_.emplace(children_.begin(), v);
return *this;
}
//-----------------------------------------------------
/** @brief adds child parameter at the beginning */
group&
push_front(parameter&& v) {
children_.emplace(children_.begin(), std::move(v));
return *this;
}
//-----------------------------------------------------
/** @brief adds child group at the beginning */
group&
push_front(const group& g) {
children_.emplace(children_.begin(), g);
return *this;
}
//-----------------------------------------------------
/** @brief adds child group at the beginning */
group&
push_front(group&& g) {
children_.emplace(children_.begin(), std::move(g));
return *this;
}
//---------------------------------------------------------------
/** @brief adds all children of other group at the end */
group&
merge(group&& g)
{
children_.insert(children_.end(),
std::make_move_iterator(g.begin()),
std::make_move_iterator(g.end()));
return *this;
}
//-----------------------------------------------------
/** @brief adds all children of several other groups at the end */
template<class... Groups>
group&
merge(group&& g1, group&& g2, Groups&&... gs)
{
merge(std::move(g1));
merge(std::move(g2), std::forward<Groups>(gs)...);
return *this;
}
//---------------------------------------------------------------
/** @brief indexed, nutable access to child */
child& operator [] (size_type index) noexcept {
return children_[index];
}
/** @brief indexed, non-nutable access to child */
const child& operator [] (size_type index) const noexcept {
return children_[index];
}
//---------------------------------------------------------------
/** @brief mutable access to first child */
child& front() noexcept { return children_.front(); }
/** @brief non-mutable access to first child */
const child& front() const noexcept { return children_.front(); }
//-----------------------------------------------------
/** @brief mutable access to last child */
child& back() noexcept { return children_.back(); }
/** @brief non-mutable access to last child */
const child& back() const noexcept { return children_.back(); }
//---------------------------------------------------------------
/** @brief returns true, if group has no children, false otherwise */
bool empty() const noexcept { return children_.empty(); }
/** @brief returns number of children */
size_type size() const noexcept { return children_.size(); }
/** @brief returns number of nested levels; 1 for a flat group */
size_type depth() const {
size_type n = 0;
for(const auto& c : children_) {
auto l = 1 + c.depth();
if(l > n) n = l;
}
return n;
}
//---------------------------------------------------------------
/** @brief returns mutating iterator to position of first element */
iterator begin() noexcept { return children_.begin(); }
/** @brief returns non-mutating iterator to position of first element */
const_iterator begin() const noexcept { return children_.begin(); }
/** @brief returns non-mutating iterator to position of first element */
const_iterator cbegin() const noexcept { return children_.begin(); }
/** @brief returns mutating iterator to position one past the last element */
iterator end() noexcept { return children_.end(); }
/** @brief returns non-mutating iterator to position one past the last element */
const_iterator end() const noexcept { return children_.end(); }
/** @brief returns non-mutating iterator to position one past the last element */
const_iterator cend() const noexcept { return children_.end(); }
//---------------------------------------------------------------
/** @brief returns augmented iterator for depth first searches
* @details taverser knows end of iteration and can skip over children
*/
depth_first_traverser
begin_dfs() const noexcept {
return depth_first_traverser{*this};
}
//---------------------------------------------------------------
/** @brief returns recursive parameter count */
size_type param_count() const {
size_type c = 0;
for(const auto& n : children_) {
c += n.param_count();
}
return c;
}
//---------------------------------------------------------------
/** @brief returns range of all flags (recursive) */
arg_list all_flags() const
{
std::vector<arg_string> all;
gather_flags(children_, all);
return all;
}
/** @brief returns true, if no flag occurs as true
* prefix of any other flag (identical flags will be ignored) */
bool flags_are_prefix_free() const
{
const auto fs = all_flags();
using std::begin; using std::end;
for(auto i = begin(fs), e = end(fs); i != e; ++i) {
if(!i->empty()) {
for(auto j = i+1; j != e; ++j) {
if(!j->empty() && *i != *j) {
if(i->find(*j) == 0) return false;
if(j->find(*i) == 0) return false;
}
}
}
}
return true;
}
//---------------------------------------------------------------
/** @brief returns longest common prefix of all flags */
arg_string common_flag_prefix() const
{
arg_list prefixes;
gather_prefixes(children_, prefixes);
return str::longest_common_prefix(prefixes);
}
private:
//---------------------------------------------------------------
static void
gather_flags(const children_store& nodes, arg_list& all)
{
for(const auto& p : nodes) {
if(p.is_group()) {
gather_flags(p.as_group().children_, all);
}
else {
const auto& pf = p.as_param().flags();
using std::begin;
using std::end;
if(!pf.empty()) all.insert(end(all), begin(pf), end(pf));
}
}
}
//---------------------------------------------------------------
static void
gather_prefixes(const children_store& nodes, arg_list& all)
{
for(const auto& p : nodes) {
if(p.is_group()) {
gather_prefixes(p.as_group().children_, all);
}
else if(!p.as_param().flags().empty()) {
auto pfx = str::longest_common_prefix(p.as_param().flags());
if(!pfx.empty()) all.push_back(std::move(pfx));
}
}
}
//---------------------------------------------------------------
children_store children_;
bool exclusive_ = false;
bool joinable_ = false;
bool scoped_ = false;
};
/*************************************************************************//**
*
* @brief group or parameter
*
*****************************************************************************/
using pattern = group::child;
/*************************************************************************//**
*
* @brief makes a group of parameters and/or groups
*
*****************************************************************************/
inline group
operator , (parameter a, parameter b)
{
return group{std::move(a), std::move(b)}.scoped(false);
}
//---------------------------------------------------------
inline group
operator , (parameter a, group b)
{
return !b.scoped() && !b.blocking() && !b.exclusive() && !b.repeatable()
&& !b.joinable() && (b.doc().empty() || b.doc() == a.doc())
? b.push_front(std::move(a))
: group{std::move(a), std::move(b)}.scoped(false);
}
//---------------------------------------------------------
inline group
operator , (group a, parameter b)
{
return !a.scoped() && !a.blocking() && !a.exclusive() && !a.repeatable()
&& !a.joinable() && (a.doc().empty() || a.doc() == b.doc())
? a.push_back(std::move(b))
: group{std::move(a), std::move(b)}.scoped(false);
}
//---------------------------------------------------------
inline group
operator , (group a, group b)
{
return !a.scoped() && !a.blocking() && !a.exclusive() && !a.repeatable()
&& !a.joinable() && (a.doc().empty() || a.doc() == b.doc())
? a.push_back(std::move(b))
: group{std::move(a), std::move(b)}.scoped(false);
}
/*************************************************************************//**
*
* @brief makes a group of alternative parameters or groups
*
*****************************************************************************/
template<class Param, class... Params>
inline group
one_of(Param param, Params... params)
{
return group{std::move(param), std::move(params)...}.exclusive(true);
}
/*************************************************************************//**
*
* @brief makes a group of alternative parameters or groups
*
*****************************************************************************/
inline group
operator | (parameter a, parameter b)
{
return group{std::move(a), std::move(b)}.scoped(false).exclusive(true);
}
//-------------------------------------------------------------------
inline group
operator | (parameter a, group b)
{
return !b.scoped() && !b.blocking() && b.exclusive() && !b.repeatable()
&& !b.joinable()
&& (b.doc().empty() || b.doc() == a.doc())
? b.push_front(std::move(a))
: group{std::move(a), std::move(b)}.scoped(false).exclusive(true);
}
//-------------------------------------------------------------------
inline group
operator | (group a, parameter b)
{
return !a.scoped() && a.exclusive() && !a.repeatable() && !a.joinable()
&& a.blocking() == b.blocking()
&& (a.doc().empty() || a.doc() == b.doc())
? a.push_back(std::move(b))
: group{std::move(a), std::move(b)}.scoped(false).exclusive(true);
}
inline group
operator | (group a, group b)
{
return !a.scoped() && a.exclusive() &&!a.repeatable() && !a.joinable()
&& a.blocking() == b.blocking()
&& (a.doc().empty() || a.doc() == b.doc())
? a.push_back(std::move(b))
: group{std::move(a), std::move(b)}.scoped(false).exclusive(true);
}
namespace detail {
inline void set_blocking(bool) {}
template<class P, class... Ps>
void set_blocking(bool yes, P& p, Ps&... ps) {
p.blocking(yes);
set_blocking(yes, ps...);
}
} // namespace detail
/*************************************************************************//**
*
* @brief makes a parameter/group sequence by making all input objects blocking
*
*****************************************************************************/
template<class Param, class... Params>
inline group
in_sequence(Param param, Params... params)
{
detail::set_blocking(true, param, params...);
return group{std::move(param), std::move(params)...}.scoped(true);
}
/*************************************************************************//**
*
* @brief makes a parameter/group sequence by making all input objects blocking
*
*****************************************************************************/
inline group
operator & (parameter a, parameter b)
{
a.blocking(true);
b.blocking(true);
return group{std::move(a), std::move(b)}.scoped(true);
}
//---------------------------------------------------------
inline group
operator & (parameter a, group b)
{
a.blocking(true);
return group{std::move(a), std::move(b)}.scoped(true);
}
//---------------------------------------------------------
inline group
operator & (group a, parameter b)
{
b.blocking(true);
if(a.all_blocking() && !a.exclusive() && !a.repeatable() && !a.joinable()
&& (a.doc().empty() || a.doc() == b.doc()))
{
return a.push_back(std::move(b));
}
else {
if(!a.all_blocking()) a.blocking(true);
return group{std::move(a), std::move(b)}.scoped(true);
}
}
inline group
operator & (group a, group b)
{
if(!b.all_blocking()) b.blocking(true);
if(a.all_blocking() && !a.exclusive() && !a.repeatable()
&& !a.joinable() && (a.doc().empty() || a.doc() == b.doc()))
{
return a.push_back(std::move(b));
}
else {
if(!a.all_blocking()) a.blocking(true);
return group{std::move(a), std::move(b)}.scoped(true);
}
}
/*************************************************************************//**
*
* @brief makes a group of parameters and/or groups
* where all single char flag params ("-a", "b", ...) are joinable
*
*****************************************************************************/
inline group&
joinable(group& param) {
return param.joinable(true);
}
inline group&&
joinable(group&& param) {
return std::move(param.joinable(true));
}
//-------------------------------------------------------------------
template<class... Params>
inline group
joinable(parameter param, Params... params)
{
return group{std::move(param), std::move(params)...}.joinable(true);
}
template<class P2, class... Ps>
inline group
joinable(group p1, P2 p2, Ps... ps)
{
return group{std::move(p1), std::move(p2), std::move(ps)...}.joinable(true);
}
template<class Param, class... Params>
inline group
joinable(doc_string docstr, Param param, Params... params)
{
return group{std::move(param), std::move(params)...}
.joinable(true).doc(std::move(docstr));
}
/*************************************************************************//**
*
* @brief makes a repeatable copy of a parameter
*
*****************************************************************************/
inline parameter
repeatable(parameter p) {
return p.repeatable(true);
}
/*************************************************************************//**
*
* @brief makes a repeatable copy of a group
*
*****************************************************************************/
inline group
repeatable(group g) {
return g.repeatable(true);
}
/*************************************************************************//**
*
* @brief makes a group of parameters and/or groups
* that is repeatable as a whole
* Note that a repeatable group consisting entirely of non-blocking
* children is equivalent to a non-repeatable group of
* repeatable children.
*
*****************************************************************************/
template<class P2, class... Ps>
inline group
repeatable(parameter p1, P2 p2, Ps... ps)
{
return group{std::move(p1), std::move(p2),
std::move(ps)...}.repeatable(true);
}
template<class P2, class... Ps>
inline group
repeatable(group p1, P2 p2, Ps... ps)
{
return group{std::move(p1), std::move(p2),
std::move(ps)...}.repeatable(true);
}
/*************************************************************************//**
*
* @brief recursively prepends a prefix to all flags
*
*****************************************************************************/
inline parameter&&
with_prefix(const arg_string& prefix, parameter&& p) {
return std::move(with_prefix(prefix, p));
}
//-------------------------------------------------------------------
inline group&
with_prefix(const arg_string& prefix, group& params)
{
for(auto& p : params) {
if(p.is_group()) {
with_prefix(prefix, p.as_group());
} else {
with_prefix(prefix, p.as_param());
}
}
return params;
}
inline group&&
with_prefix(const arg_string& prefix, group&& params)
{
return std::move(with_prefix(prefix, params));
}
template<class Param, class... Params>
inline group
with_prefix(arg_string prefix, Param&& param, Params&&... params)
{
return with_prefix(prefix, group{std::forward<Param>(param),
std::forward<Params>(params)...});
}
/*************************************************************************//**
*
* @brief recursively prepends a prefix to all flags
*
* @param shortpfx : used for single-letter flags
* @param longpfx : used for flags with length > 1
*
*****************************************************************************/
inline parameter&&
with_prefixes_short_long(const arg_string& shortpfx, const arg_string& longpfx,
parameter&& p)
{
return std::move(with_prefixes_short_long(shortpfx, longpfx, p));
}
//-------------------------------------------------------------------
inline group&
with_prefixes_short_long(const arg_string& shortFlagPrefix,
const arg_string& longFlagPrefix,
group& params)
{
for(auto& p : params) {
if(p.is_group()) {
with_prefixes_short_long(shortFlagPrefix, longFlagPrefix, p.as_group());
} else {
with_prefixes_short_long(shortFlagPrefix, longFlagPrefix, p.as_param());
}
}
return params;
}
inline group&&
with_prefixes_short_long(const arg_string& shortFlagPrefix,
const arg_string& longFlagPrefix,
group&& params)
{
return std::move(with_prefixes_short_long(shortFlagPrefix, longFlagPrefix,
params));
}
template<class Param, class... Params>
inline group
with_prefixes_short_long(const arg_string& shortFlagPrefix,
const arg_string& longFlagPrefix,
Param&& param, Params&&... params)
{
return with_prefixes_short_long(shortFlagPrefix, longFlagPrefix,
group{std::forward<Param>(param),
std::forward<Params>(params)...});
}
/*************************************************************************//**
*
* @brief parsing implementation details
*
*****************************************************************************/
namespace detail {
/*************************************************************************//**
*
* @brief DFS traverser that keeps track of 'scopes'
* scope = all parameters that are either bounded by
* two blocking parameters on the same depth level
* or the beginning/end of the outermost group
*
*****************************************************************************/
class scoped_dfs_traverser
{
public:
using dfs_traverser = group::depth_first_traverser;
scoped_dfs_traverser() = default;
explicit
scoped_dfs_traverser(const group& g):
pos_{g}, lastMatch_{}, posAfterLastMatch_{}, scopes_{},
curMatched_{false}, ignoreBlocks_{false},
repeatGroupStarted_{false}, repeatGroupContinues_{false}
{}
const dfs_traverser& base() const noexcept { return pos_; }
const dfs_traverser& last_match() const noexcept { return lastMatch_; }
const group& parent() const noexcept { return pos_.parent(); }
const group* repeat_group() const noexcept { return pos_.repeat_group(); }
const group* join_group() const noexcept { return pos_.join_group(); }
const pattern* operator ->() const noexcept { return pos_.operator->(); }
const pattern& operator *() const noexcept { return *pos_; }
const pattern* ptr() const noexcept { return pos_.operator->(); }
explicit operator bool() const noexcept { return bool(pos_); }
bool joinable() const noexcept { return pos_.joinable(); }
arg_string common_flag_prefix() const { return pos_.common_flag_prefix(); }
void ignore_blocking(bool yes) { ignoreBlocks_ = yes; }
void invalidate() { pos_.invalidate(); curMatched_ = false; }
bool matched() const noexcept { return curMatched_; }
bool start_of_repeat_group() const noexcept { return repeatGroupStarted_; }
//-----------------------------------------------------
scoped_dfs_traverser&
next_sibling() { pos_.next_sibling(); return *this; }
scoped_dfs_traverser&
next_alternative() { pos_.next_alternative(); return *this; }
scoped_dfs_traverser&
next_after_siblings() { pos_.next_after_siblings(); return *this; }
//-----------------------------------------------------
scoped_dfs_traverser&
operator ++ ()
{
if(!pos_) return *this;
if(pos_.is_last_in_path()) {
return_to_outermost_scope();
return *this;
}
//current pattern can block if it didn't match already
if(!ignoreBlocks_ && !matched()) {
//current group can block if we didn't have any match in it
if(pos_.is_last_in_group() && pos_.parent().blocking()
&& (!posAfterLastMatch_ || &(posAfterLastMatch_.parent()) != &(pos_.parent())))
{
//ascend to parent's level
++pos_;
//skip all siblings of parent group
pos_.next_after_siblings();
if(!pos_) return_to_outermost_scope();
}
else if(pos_->blocking() && !pos_->is_group()) {
if(pos_.parent().exclusive()) { //is_alternative(pos_.level())) {
pos_.next_alternative();
} else {
//no match => skip siblings of blocking param
pos_.next_after_siblings();
}
if(!pos_) return_to_outermost_scope();
} else {
++pos_;
}
} else {
++pos_;
}
check_left_scope();
return *this;
}
//-----------------------------------------------------
void next_after_match(scoped_dfs_traverser match)
{
if(!match || ignoreBlocks_) return;
check_repeat_group_start(match);
lastMatch_ = match.base();
if(!match->blocking() && match.base().parent().blocking()) {
match.pos_.back_to_parent();
}
//if match is not in current position & current position is blocking
//=> current position has to be advanced by one so that it is
//no longer reachable within current scope
//(can happen for repeatable, blocking parameters)
if(match.base() != pos_ && pos_->blocking()) pos_.next_sibling();
if(match->blocking()) {
if(match.pos_.is_alternative()) {
//discard other alternatives
match.pos_.skip_alternatives();
}
if(is_last_in_current_scope(match.pos_)) {
//if current param is not repeatable -> back to previous scope
if(!match->repeatable() && !match->is_group()) {
curMatched_ = false;
pos_ = std::move(match.pos_);
if(!scopes_.empty()) pos_.undo(scopes_.top());
}
else { //stay at match position
curMatched_ = true;
pos_ = std::move(match.pos_);
}
}
else { //not last in current group
//if current param is not repeatable, go directly to next
if(!match->repeatable() && !match->is_group()) {
curMatched_ = false;
++match.pos_;
} else {
curMatched_ = true;
}
if(match.pos_.level() > pos_.level()) {
scopes_.push(pos_.undo_point());
pos_ = std::move(match.pos_);
}
else if(match.pos_.level() < pos_.level()) {
return_to_level(match.pos_.level());
}
else {
pos_ = std::move(match.pos_);
}
}
posAfterLastMatch_ = pos_;
}
else {
if(match.pos_.level() < pos_.level()) {
return_to_level(match.pos_.level());
}
posAfterLastMatch_ = pos_;
}
repeatGroupContinues_ = repeat_group_continues();
}
private:
//-----------------------------------------------------
bool is_last_in_current_scope(const dfs_traverser& pos)
{
if(scopes_.empty()) return pos.is_last_in_path();
//check if we would leave the current scope on ++
auto p = pos;
++p;
return p.level() < scopes_.top().level();
}
//-----------------------------------------------------
void check_repeat_group_start(const scoped_dfs_traverser& newMatch)
{
const auto newrg = newMatch.repeat_group();
if(!newrg) {
repeatGroupStarted_ = false;
}
else if(lastMatch_.repeat_group() != newrg) {
repeatGroupStarted_ = true;
}
else if(!repeatGroupContinues_ || !newMatch.repeatGroupContinues_) {
repeatGroupStarted_ = true;
}
else {
//special case: repeat group is outermost group
//=> we can never really 'leave' and 'reenter' it
//but if the current scope is the first element, then we are
//conceptually at a position 'before' the group
repeatGroupStarted_ = scopes_.empty() || (
newrg == pos_.root() &&
scopes_.top().param() == &(*pos_.root()->begin()) );
}
repeatGroupContinues_ = repeatGroupStarted_;
}
//-----------------------------------------------------
bool repeat_group_continues()
{
if(!repeatGroupContinues_) return false;
const auto curRepGroup = pos_.repeat_group();
if(!curRepGroup) return false;
if(curRepGroup != lastMatch_.repeat_group()) return false;
if(!posAfterLastMatch_) return false;
return true;
}
//-----------------------------------------------------
void check_left_scope()
{
if(posAfterLastMatch_) {
if(pos_.level() < posAfterLastMatch_.level()) {
while(!scopes_.empty() && scopes_.top().level() >= pos_.level()) {
pos_.undo(scopes_.top());
scopes_.pop();
}
posAfterLastMatch_.invalidate();
}
}
while(!scopes_.empty() && scopes_.top().level() > pos_.level()) {
pos_.undo(scopes_.top());
scopes_.pop();
}
repeatGroupContinues_ = repeat_group_continues();
}
//-----------------------------------------------------
void return_to_outermost_scope()
{
posAfterLastMatch_.invalidate();
if(scopes_.empty()) {
pos_.invalidate();
repeatGroupContinues_ = false;
return;
}
while(!scopes_.empty() && (!pos_ || pos_.level() >= 1)) {
pos_.undo(scopes_.top());
scopes_.pop();
}
while(!scopes_.empty()) scopes_.pop();
repeatGroupContinues_ = repeat_group_continues();
}
//-----------------------------------------------------
void return_to_level(int level)
{
if(pos_.level() <= level) return;
while(!scopes_.empty() && pos_.level() > level) {
pos_.undo(scopes_.top());
scopes_.pop();
}
};
dfs_traverser pos_;