diff options
Diffstat (limited to 'Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty')
-rw-r--r-- | Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty | 1053 |
1 files changed, 606 insertions, 447 deletions
diff --git a/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty b/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty index 5da00fdd3d0..c57f415bc47 100644 --- a/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty +++ b/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty @@ -46,113 +46,121 @@ %% %% ----------------------------------------------------------------------- \RequirePackage{expl3} -\GetIdInfo$Id: l3regex.dtx 2895 2011-10-09 15:58:19Z joseph $ +\GetIdInfo$Id: l3regex.dtx 2943 2011-10-30 15:59:43Z bruno $ {L3 Experimental Regular Expressions} \ProvidesExplPackage {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription} \RequirePackage{l3str} +\int_const:Nn \c_forty_eight { 48 } +\int_const:Nn \c_fifty_eight { 58 } +\int_const:Nn \c_sixty_five { 65 } +\int_const:Nn \c_ninety_one { 91 } +\int_const:Nn \c_ninety_seven { 97 } +\int_const:Nn \c_one_hundred_twenty_three { 123 } +\int_const:Nn \c_one_hundred_twenty_seven { 127 } \cs_new:Npn \regex_tmp:w { } \tl_new:N \g_regex_tmpa_tl \tl_new:N \l_regex_tmpa_tl \tl_new:N \l_regex_tmpb_tl \int_new:N \l_regex_tmpa_int -\tl_new:N \l_regex_pattern_str +\int_new:N \l_regex_tmpb_int \int_new:N \l_regex_max_state_int \int_new:N \l_regex_left_state_int \int_new:N \l_regex_right_state_int -\seq_new:N \l_regex_left_state_seq -\seq_new:N \l_regex_right_state_seq +\seq_new:N \l_regex_group_begin_seq +\seq_new:N \l_regex_group_end_seq \int_new:N \l_regex_capturing_group_int \seq_new:N \l_regex_capturing_group_seq \tl_new:N \l_regex_one_or_group_tl -\int_new:N \l_regex_repetition_int -\bool_new:N \l_regex_look_behind_bool -\tl_new:N \l_regex_look_behind_str \cs_new_eq:NN \regex_build_tmp_class:n \use_none:n \bool_new:N \l_regex_class_bool \tl_new:N \l_regex_class_tl \tl_const:Nn \c_regex_d_tl { - \regex_item_range:nn {48} {57} % 0--9 + \regex_item_range:nn { \c_forty_eight } { 57 } % 0--9 } \tl_const:Nn \c_regex_D_tl { - \regex_item_more:n {57} % 9 - \regex_item_range:nn {0} {47} % 0 + \regex_item_geq:n { \c_fifty_eight } % > `9 + \regex_item_range:nn { \c_zero } { 47 } % 0 } \tl_const:Nn \c_regex_h_tl { - \regex_item_equal:n {32} % space - \regex_item_equal:n {9} % tab + \regex_item_equal:n { \c_thirty_two } % space + \regex_item_equal:n { \c_nine } % tab } \tl_const:Nn \c_regex_H_tl { - \regex_item_neq:n {32} % space - \regex_item_neq:n {9} % tab - \regex_break_true:w + \regex_item_geq:n { 33 } % > space + \regex_item_range:nn { \c_ten } { 31 } % tab < ... < space + \regex_item_range:nn { \c_zero } { \c_eight } % < tab } \tl_const:Nn \c_regex_s_tl { - \regex_item_equal:n {32} % space - \regex_item_neq:n {11} % vtab - \regex_item_range:nn {9} {13} % tab, lf, vtab, ff, cr + \regex_item_equal:n { \c_thirty_two } % space + \regex_item_range:nn { \c_nine } { \c_ten } % tab, lf + \regex_item_range:nn { \c_twelve } { \c_thirteen } % ff, cr } \tl_const:Nn \c_regex_S_tl { - \regex_item_more:n {32} % > space - \regex_item_range:nn {14} {31} % tab < ... < space - \regex_item_range:nn {0} {8} % < tab - \regex_item_equal:n {11} % vtab + \regex_item_geq:n { 33 } % > space + \regex_item_range:nn { \c_fourteen } { 31 } % tab < ... < space + \regex_item_range:nn { \c_zero } { \c_eight } % < tab + \regex_item_equal:n { \c_eleven } % vtab } \tl_const:Nn \c_regex_v_tl { - \regex_item_range:nn {10} {13} % lf, vtab, ff, cr + \regex_item_range:nn { \c_ten } { \c_thirteen } % lf, vtab, ff, cr } \tl_const:Nn \c_regex_V_tl { - \regex_item_more:n {13} % cr - \regex_item_range:nn {0} {9} % < lf + \regex_item_geq:n { \c_fourteen } % >cr + \regex_item_range:nn { \c_zero } { \c_nine } % < lf } \tl_const:Nn \c_regex_w_tl { - \regex_item_range:nn {97} {122} % a--z - \regex_item_range:nn {65} {90} % A--Z - \regex_item_range:nn {48} {57} % 0--9 - \regex_item_equal:n {95} % _ + \regex_item_range:nn { \c_ninety_seven } { 122 } % a--z + \regex_item_range:nn { \c_sixty_five } { 90 } % A--Z + \regex_item_range:nn { \c_forty_eight } { 57 } % 0--9 + \regex_item_equal:n { 95 } % _ } \tl_const:Nn \c_regex_W_tl { - \regex_item_range:nn {0} {47} % <`0 - \regex_item_range:nn {58} {64} % (`9+1)--(`A-1) - \regex_item_range:nn {91} {94} % (`Z+1)--(`_-1) - \regex_item_equal:n {96} % ` - \regex_item_more:n {122} % z + \regex_item_range:nn { \c_zero } { 47 } % <`0 + \regex_item_range:nn { \c_fifty_eight } { 64 } % (`9+1)--(`A-1) + \regex_item_range:nn { \c_ninety_one } { 94 } % (`Z+1)--(`_-1) + \regex_item_equal:n { 96 } % ` + \regex_item_geq:n { \c_one_hundred_twenty_three } % z } \tl_const:Nn \c_regex_N_tl { - \regex_item_neq:n {10} % lf - \regex_break_true:w + \regex_item_geq:n { \c_eleven } % > lf + \regex_item_range:nn { \c_zero } { \c_nine } % < lf } -\tl_new:N \l_regex_query_str -\int_new:N \l_regex_start_step_int -\int_new:N \l_regex_current_char_int +\tl_new:N \l_regex_query_other_str \int_new:N \l_regex_current_step_int +\int_new:N \l_regex_start_step_int +\int_new:N \l_regex_success_step_int \int_new:N \l_regex_unique_step_int +\int_new:N \l_regex_current_char_int +\int_new:N \l_regex_last_char_int +\int_new:N \l_regex_case_changed_char_int +\bool_new:N \l_regex_caseless_bool \int_new:N \l_regex_current_state_int \prop_new:N \l_regex_current_submatches_prop +\prop_new:N \l_regex_success_submatches_prop \int_new:N \l_regex_max_index_int \tl_new:N \l_regex_every_match_tl +\bool_new:N \l_regex_success_bool \cs_new_protected:Npn \regex_last_match_empty_no:F #1 {#1} \cs_new_protected:Npn \regex_last_match_empty_yes:F { \int_compare:nNnF \l_regex_start_step_int = \l_regex_current_step_int } \cs_new_eq:NN \regex_last_match_empty:F \regex_last_match_empty_no:F -\bool_new:N \l_regex_success_bool \bool_new:N \l_regex_success_empty_bool -\int_new:N \l_regex_success_step_int -\prop_new:N \l_regex_success_submatches_prop \bool_new:N \l_regex_fresh_thread_bool \seq_new:N \g_regex_submatches_seq \int_new:N \g_regex_match_count_int +\seq_new:N \g_regex_extract_all_seq \seq_new:N \g_regex_split_seq \tl_new:N \l_regex_replacement_tl \tl_new:N \g_regex_replaced_str @@ -169,40 +177,43 @@ \tex_toks:D #1 \exp_after:wN { \tex_the:D \tex_toks:D \exp_after:wN #1 \regex_tmp:w } } -\cs_new_eq:NN \regex_if_tail_stop:N \quark_if_recursion_tail_stop:N -\cs_new_protected_nopar:Npn \regex_if_tail_error:Nn #1#2 - { \quark_if_recursion_tail_stop_do:Nn #1 { \regex_build_error:n {#2} } } -\cs_new_protected_nopar:Npn \regex_build_error:n #1 +\cs_new_nopar:Npn \regex_query_substr:NN #1#2 { - \msg_error:nnxx { regex } { build-error } {\int_eval:n{#1}} - { - \prg_case_int:nnn {#1} - { - {-999} {File~not~found} - {-998} {Unsupported~construct} - {-997} {The~regular~expression~is~too~large~(32768~states).} - {1} {\iow_char:N\\~at~end~of~pattern } - {4} {Numbers~out~of~order~in~\iow_char:N\{\iow_char\}~quantifier.} - {6} {Missing~terminating~\iow_char:N\]~for~character~class } - {7} {Invalid~escape~sequence~in~character~class} - {8} {Range~out~of~order~in~character~class} - {22} {Mismatched~parentheses} - {34} {Character~value~in~\iow_char:N\\x{...}~sequence~is~too~large} - } - { Internal~bug. } - } + \exp_after:wN \regex_query_substr_aux:NN + \exp_after:wN #1 + \exp_after:wN #2 + \l_regex_query_other_str + \q_stop + } +\cs_new:Npn \regex_query_substr_aux:NN #1#2 + { + \str_skip_do:nn {#1} + { \str_collect_do:nn { #2 - #1 } { \use_i_delimit_by_q_stop:nw } } + } +\cs_new_protected_nopar:Npn \regex_seq_pop:N #1 + { + \seq_pop:NN #1 \l_regex_tmpa_tl + \l_regex_tmpa_tl + } +\cs_new_protected_nopar:Npn \regex_seq_get:N #1 + { + \seq_get:NN #1 \l_regex_tmpa_tl + \l_regex_tmpa_tl } -\msg_new:nnn { regex } { build-error } { (error~#1): ~ #2 } -\cs_new_nopar:Npn \regex_break_true:w #1 \regex_break_point:TF #2 #3 {#2} -\cs_new_nopar:Npn \regex_break_false:w #1 \regex_break_point:TF #2 #3 {#3} -\cs_new_eq:NN \regex_break_point:TF \use_ii:nn -\cs_new_nopar:Npn \regex_item_equal:n #1 +\cs_new_protected_nopar:Npn \regex_item_dot:T + { \int_compare:nNnF \l_regex_current_char_int = \c_minus_one } +\cs_new_protected_nopar:Npn \regex_break_true:w + #1 \regex_break_point:TF #2 #3 {#2} +\cs_new_protected_nopar:Npn \regex_break_false:w + #1 \regex_break_point:TF #2 #3 {#3} +\cs_new_protected_nopar:Npn \regex_break_point:TF #1 #2 { #2 } +\cs_new_protected_nopar:Npn \regex_item_caseful_equal:n #1 { \if_num:w #1 = \l_regex_current_char_int \exp_after:wN \regex_break_true:w \fi: } -\cs_new_nopar:Npn \regex_item_range:nn #1 #2 +\cs_new_protected_nopar:Npn \regex_item_caseful_range:nn #1 #2 { \reverse_if:N \if_num:w #1 > \l_regex_current_char_int \reverse_if:N \if_num:w #2 < \l_regex_current_char_int @@ -210,24 +221,60 @@ \fi: \fi: } -\cs_new_nopar:Npn \regex_item_less:n #1 +\cs_new_protected_nopar:Npn \regex_item_caseful_geq:n #1 { - \if_num:w #1 > \l_regex_current_char_int + \reverse_if:N \if_num:w #1 > \l_regex_current_char_int \exp_after:wN \regex_break_true:w \fi: } -\cs_new_nopar:Npn \regex_item_more:n #1 +\cs_new_protected_nopar:Npn \regex_item_caseless_equal:n #1 { - \if_num:w #1 < \l_regex_current_char_int + \if_num:w #1 = \l_regex_current_char_int + \exp_after:wN \regex_break_true:w + \fi: + \if_num:w #1 = \l_regex_case_changed_char_int \exp_after:wN \regex_break_true:w \fi: } -\cs_new_nopar:Npn \regex_item_neq:n #1 +\cs_new_protected_nopar:Npn \regex_item_caseless_range:nn #1 #2 { - \if_num:w #1 = \l_regex_current_char_int - \exp_after:wN \regex_break_false:w + \reverse_if:N \if_num:w #1 > \l_regex_current_char_int + \reverse_if:N \if_num:w #2 < \l_regex_current_char_int + \exp_after:wN \exp_after:wN \exp_after:wN \regex_break_true:w + \fi: + \fi: + \reverse_if:N \if_num:w #1 > \l_regex_case_changed_char_int + \reverse_if:N \if_num:w #2 < \l_regex_case_changed_char_int + \exp_after:wN \exp_after:wN \exp_after:wN \regex_break_true:w + \fi: \fi: } +\cs_new_protected_nopar:Npn \regex_item_caseless_geq:n #1 + { + \reverse_if:N \if_num:w #1 > \l_regex_current_char_int + \exp_after:wN \regex_break_true:w + \fi: + \reverse_if:N \if_num:w #1 > \l_regex_case_changed_char_int + \exp_after:wN \regex_break_true:w + \fi: + } +\cs_new:Npn \regex_item_equal:n { \regex_item_caseful_equal:n } +\cs_new:Npn \regex_item_range:nn { \regex_item_caseful_range:nn } +\cs_new:Npn \regex_item_geq:n { \regex_item_caseful_geq:n } +\cs_new_protected_nopar:Npn \regex_build_caseless: + { + \bool_set_true:N \l_regex_caseless_bool + \cs_set:Npn \regex_item_equal:n { \regex_item_caseless_equal:n } + \cs_set:Npn \regex_item_range:nn { \regex_item_caseless_range:nn } + \cs_set:Npn \regex_item_geq:n { \regex_item_caseless_geq:n } + } +\cs_new_protected_nopar:Npn \regex_build_caseful: + { + \bool_set_false:N \l_regex_caseless_bool + \cs_set:Npn \regex_item_equal:n { \regex_item_caseful_equal:n } + \cs_set:Npn \regex_item_range:nn { \regex_item_caseful_range:nn } + \cs_set:Npn \regex_item_geq:n { \regex_item_caseful_geq:n } + } \cs_new_protected:Npn \regex_get_digits:nw #1 { \tex_afterassignment:D \regex_tmp:w @@ -235,65 +282,71 @@ { \exp_not:n {#1} { \if_false: } } \fi: - \regex_get_digits_aux:N + \regex_get_digits_aux:NN } -\cs_new_nopar:Npn \regex_get_digits_aux:N #1 +\cs_new_nopar:Npn \regex_get_digits_aux:NN #1#2 { - \if_num:w 9 < 1 \exp_not:N #1 \exp_stop_f: - \else: - \if_charcode:w \c_space_token \exp_not:N #1 + \if_meaning:w \regex_build_raw:N #1 + \if_charcode:w \c_space_token \exp_not:N #2 \else: - \regex_get_digits_end:w + \if_num:w 9 < 1 \exp_not:N #2 \exp_stop_f: + #2 + \else: + \regex_get_digits_end:w #1 #2 + \fi: \fi: + \else: + \regex_get_digits_end:w #1 #2 \fi: - #1 \regex_get_digits_aux:N + \regex_get_digits_aux:NN } -\cs_new_nopar:Npn \regex_get_digits_end:w - \fi: \fi: #1 \regex_get_digits_aux:N +\cs_new_nopar:Npn \regex_get_digits_end:w #1 \fi: #2 \regex_get_digits_aux:NN { - \fi: \fi: + \fi: #2 \if_false: { { \fi: } } #1 } +\prg_new_conditional:Npnn \regex_token_if_other_digit:N #1 { TF } + { + \if_num:w \c_nine < 1 \exp_not:N #1 \exp_stop_f: + \prg_return_true: \else: \prg_return_false: \fi: + } \prg_new_conditional:Npnn \regex_aux_char_if_special:N #1 { TF } { - \if_num:w `#1 < 97 \exp_stop_f: - \if_num:w `#1 < 58 \exp_stop_f: - \if_num:w \int_eval:w (`#1 - \c_eight)/\c_sixteen = \c_two - \prg_return_true: - \else: - \prg_return_false: - \fi: + \if_num:w `#1 < \c_ninety_one + \if_num:w `#1 < \c_fifty_eight + \if_num:w `#1 < \c_forty_eight + \if_num:w `#1 < \c_thirty_two + \prg_return_false: \else: \prg_return_true: \fi: + \else: \prg_return_false: \fi: \else: - \if_num:w \int_eval:w `#1 / 26 = \c_three - \prg_return_false: - \else: - \prg_return_true: - \fi: + \if_num:w `#1 < \c_sixty_five + \prg_return_true: \else: \prg_return_false: \fi: \fi: \else: - \if_num:w \int_eval:w `#1 / \c_five = 25 \exp_stop_f: - \prg_return_true: + \if_num:w `#1 < \c_one_hundred_twenty_three + \if_num:w `#1 < \c_ninety_seven + \prg_return_true: \else: \prg_return_false: \fi: \else: - \prg_return_false: + \if_num:w `#1 < \c_one_hundred_twenty_seven + \prg_return_true: \else: \prg_return_false: \fi: \fi: \fi: } \prg_new_conditional:Npnn \regex_aux_char_if_alphanumeric:N #1 { TF } { - \if_num:w `#1 < 91 \exp_stop_f: - \if_num:w `#1 < 65 \exp_stop_f: - \if_num:w \c_nine < 1 #1 \exp_stop_f: - \prg_return_true: - \else: - \prg_return_false: - \fi: + \if_num:w `#1 < \c_ninety_one + \if_num:w `#1 < \c_fifty_eight + \if_num:w `#1 < \c_forty_eight + \prg_return_false: \else: \prg_return_true: \fi: \else: - \prg_return_true: + \if_num:w `#1 < \c_sixty_five + \prg_return_false: \else: \prg_return_true: \fi: \fi: \else: - \if_num:w \int_eval:w (`#1-\c_six)/26 = \c_four - \prg_return_true: + \if_num:w `#1 < \c_one_hundred_twenty_three + \if_num:w `#1 < \c_ninety_seven + \prg_return_false: \else: \prg_return_true: \fi: \else: \prg_return_false: \fi: @@ -301,8 +354,8 @@ } \cs_new_protected_nopar:Npn \regex_build_new_state: { - \int_compare:nNnTF \l_regex_max_state_int > {32766} - { \regex_build_error:n {-997} } + \int_compare:nNnTF \l_regex_max_state_int > { 32766 } + { \msg_error:nn { regex } { -997 } } { \int_incr:N \l_regex_max_state_int \tex_toks:D \l_regex_max_state_int { \s_regex_toks } @@ -314,15 +367,15 @@ { \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int - { #1 { \int_use:N #2 } } + { #1 { \int_eval:n { #2 - \l_regex_left_state_int } } } } \cs_new_protected_nopar:Npn \regex_build_transitions_aux:NNNN #1#2#3#4 { \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int { - #1 { \int_use:N #2 } - #3 { \int_use:N #4 } + #1 { \int_eval:n { #2 - \l_regex_left_state_int } } + #3 { \int_eval:n { #4 - \l_regex_left_state_int } } } } \cs_new_protected:Npn \regex_build:n #1 @@ -363,25 +416,19 @@ \cs_set_eq:NN \regex_build_control:N \regex_build_default_control:N \cs_set_eq:NN \regex_build_raw:N \regex_build_default_raw:N \int_set_eq:NN \l_regex_capturing_group_int \c_zero - \int_zero:N \l_regex_left_state_int - \int_zero:N \l_regex_right_state_int \int_zero:N \l_regex_max_state_int \regex_build_new_state: \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int - { - \regex_action_start_wildcard:nn - { \int_use:N \l_regex_left_state_int } - { \int_use:N \l_regex_right_state_int } - } + { \regex_action_start_wildcard:nn {0} {1} } } \cs_new_protected_nopar:Npn \regex_build_end: { \seq_if_empty:NF \l_regex_capturing_group_seq { - \regex_build_error:n {22} + \msg_error:nn { regex } {22} \prg_replicate:nn - { \seq_length:N \l_regex_capturing_group_seq } % ( + { \seq_length:N \l_regex_capturing_group_seq } { \regex_build_close_aux: \regex_build_group_: } } \regex_toks_put_right:Nx \l_regex_right_state_int @@ -399,7 +446,13 @@ \regex_toks_put_right:Nx \l_regex_left_state_int { \exp_not:N \int_compare:nNnT {#1} = \l_regex_current_step_int - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } } } \cs_new_protected_nopar:cpn { regex_build_$: } % $ @@ -409,71 +462,72 @@ { \exp_not:N \int_compare:nNnT \c_minus_one = \l_regex_current_char_int - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } } } \cs_new_eq:Nc \regex_build_Z: { regex_build_$: } %$ \cs_new_eq:Nc \regex_build_z: { regex_build_$: } %$ \cs_new_protected_nopar:Npn \regex_build_b: { - \bool_set_true:N \l_regex_look_behind_bool \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int { \exp_not:N \regex_if_word_boundary:TF - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } { } } } \cs_new_protected_nopar:Npn \regex_build_B: { - \bool_set_true:N \l_regex_look_behind_bool \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int { \exp_not:N \regex_if_word_boundary:TF { } - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } } } \cs_new_protected_nopar:Npn \regex_if_word_boundary:TF { - \tl_if_empty:NTF \l_regex_look_behind_str - { \c_regex_w_tl } - { - \group_begin: - \cs_set_nopar:Npx \regex_tmp:w - { - \int_set:Nn \l_regex_current_char_int - { ` \str_head:N \l_regex_look_behind_str } - } - \regex_tmp:w - \c_regex_w_tl - \regex_break_point:TF - { \group_end: \c_regex_W_tl } - { \group_end: \c_regex_w_tl } - } + \group_begin: + \int_set_eq:NN \l_regex_current_char_int \l_regex_last_char_int + \c_regex_w_tl + \regex_break_point:TF + { \group_end: \c_regex_W_tl \regex_item_equal:n { -1 } } + { \group_end: \c_regex_w_tl } \regex_break_point:TF } \cs_new_protected_nopar:Npn \regex_build_default_raw:N #1 { \cs_set:Npx \regex_build_tmp_class:n ##1 { - \exp_not:n { \exp_not:N \if_num:w } - \int_value:w `#1 = \l_regex_current_char_int - \regex_action_cost:n { ##1 } - \exp_not:n { \exp_not:N \fi: } + \regex_item_equal:n { \int_value:w `#1 } + \regex_break_point:TF { \regex_action_cost:n { ##1 } } { } } \regex_build_one_quantifier: } \cs_new_protected_nopar:cpn { regex_build_.: } { \cs_set:Npn \regex_build_tmp_class:n ##1 - { - \exp_not:N \if_num:w \c_minus_one < \l_regex_current_char_int - \regex_action_cost:n {##1} - \exp_not:N \fi: - } + { \regex_item_dot:T { \regex_action_cost:n {##1} } } \regex_build_one_quantifier: } \cs_new_protected_nopar:Npn \regex_build_char_type:N #1 @@ -481,9 +535,7 @@ \cs_set:Npn \regex_build_tmp_class:n ##1 { \exp_not:N #1 - \exp_not:N \regex_break_point:TF - { \regex_action_cost:n {##1} } - { } + \regex_break_point:TF { \regex_action_cost:n {##1} } { } } \regex_build_one_quantifier: } @@ -518,16 +570,8 @@ { \exp_not:o \l_regex_class_tl \bool_if:NTF \l_regex_class_bool - { - \exp_not:N \regex_break_point:TF - { \regex_action_cost:n {##1} } - { } - } - { - \exp_not:N \regex_break_point:TF - { } - { \regex_action_cost:n {##1} } - } + { \regex_break_point:TF { \regex_action_cost:n {##1} } { } } + { \regex_break_point:TF { } { \regex_action_cost:n {##1} } } } \regex_build_one_quantifier: } @@ -553,7 +597,7 @@ \cs_new_protected_nopar:Npn \regex_class_single:N #1 { \tl_put_right:Nx \l_regex_class_tl - { \exp_not:N \regex_item_equal:n { \int_value:w `#1 } } + { \regex_item_equal:n { \int_value:w `#1 } } } \cs_new_protected_nopar:Npn \regex_class_range:Nw #1#2#3 { @@ -572,14 +616,14 @@ \cs_new_protected_nopar:Npn \regex_class_range_put:NN #1#2 { \if_num:w `#1 > `#2 \exp_stop_f: - \regex_build_error:n {8} + \msg_error:nn { regex } {8} \else: \tl_put_right:Nx \l_regex_class_tl { \if_num:w `#1 = `#2 \exp_stop_f: - \exp_not:N \regex_item_equal:n + \regex_item_equal:n \else: - \exp_not:N \regex_item_range:nn { \int_value:w `#1 } + \regex_item_range:nn { \int_value:w `#1 } \fi: { \int_value:w `#2 } } @@ -651,26 +695,29 @@ \exp_not:N \regex_build_raw:N \c_lbrace_str #1#2 } } -\cs_new_protected_nopar:Npn \regex_build_quantifier_lbrace:nnw #1#2#3 +\cs_new_protected_nopar:Npn \regex_build_quantifier_lbrace:nnw #1#2#3#4 { - \str_if_eq:xxTF { \exp_not:N #3 } { \c_rbrace_str } + \str_if_eq:xxTF + { \exp_not:n {#3#4} } + { \exp_not:N \regex_build_control:N \c_rbrace_str } { \tl_if_empty:nTF {#2} - { \regex_build_quantifier_aux:nnN { {#1} {\c_max_int} } {nn} } + { \regex_build_quantifier_aux:nnNN { {#1} } { n* } } { \int_compare:nNnT {#1} > {#2} - { \regex_build_error:n {4} } - \regex_build_quantifier_aux:nnN { {#1} {#2} } {nn} + { \msg_error:nn { regex } {4} } + \regex_build_quantifier_aux:nnNN { {#1} {#2} } { nn } } } { \regex_build_quantifier_end:n { } \use:x { - \exp_not:n { \exp_args:No \tl_map_function:nN } - { \c_lbrace_str #1 #2 , } - \regex_build_raw:N + \exp_args:No \tl_map_function:nN + { \c_lbrace_str #1 , #2 } + \regex_build_raw:N } + #3 #4 } } \cs_new_protected_nopar:Npn \regex_build_quantifier_end:n #1 @@ -726,71 +773,27 @@ \regex_action_free:n \l_regex_tmpa_int } \cs_new_protected_nopar:Npn \regex_build_one_n: #1 - { - \int_set_eq:NN \l_regex_tmpa_int \l_regex_right_state_int - \regex_build_new_state: - \regex_build_new_state: - \regex_toks_put_right:Nx \l_regex_tmpa_int - { %^^A safe \if nesting? - \exp_not:N \if_num:w #1 > \l_regex_repetition_int - \regex_action_repeat_move:n - { \int_use:N \l_regex_left_state_int } - \exp_not:N \else: - \regex_action_no_repeat_move:n - { \int_use:N \l_regex_right_state_int } - \exp_not:N \fi: - } - \regex_toks_put_right:Nx \l_regex_left_state_int - { - \regex_build_tmp_class:n - { \int_use:N \l_regex_tmpa_int } - } - } + { \prg_replicate:nn {#1} { \regex_build_one_: } } \cs_new_eq:cN { regex_build_one_n?: } \regex_build_one_n: -\cs_new_protected_nopar:Npn \regex_build_one_nn: #1#2 +\cs_new_protected_nopar:cpx { regex_build_one_n*: } #1 { - \regex_build_one_nn_aux:nn {#1} - { - \exp_not:N \if_num:w #2 > \l_regex_repetition_int - \regex_action_repeat_copy:n - { \int_use:N \l_regex_left_state_int } - \exp_not:N \fi: - \regex_action_no_repeat_move:n - { \int_use:N \l_regex_right_state_int } - } + \exp_not:N \prg_replicate:nn {#1} { \exp_not:N \regex_build_one_: } + \exp_not:c { regex_build_one_*: } } -\cs_new_protected_nopar:cpn { regex_build_one_nn?: } #1#2 +\cs_new_protected_nopar:cpx { regex_build_one_n*?: } #1 { - \regex_build_one_nn_aux:nn {#1} - { - \regex_action_no_repeat_copy:n - { \int_use:N \l_regex_right_state_int } - \exp_not:N \if_num:w #2 > \l_regex_repetition_int - \regex_action_repeat_move:n - { \int_use:N \l_regex_left_state_int } - \exp_not:N \fi: - } + \exp_not:N \prg_replicate:nn {#1} { \exp_not:N \regex_build_one_: } + \exp_not:c { regex_build_one_*?: } } -\cs_new_protected_nopar:Npn \regex_build_one_nn_aux:nn #1#2 +\cs_new_protected_nopar:Npn \regex_build_one_nn_aux:Nnn #1#2#3 { - \int_set_eq:NN \l_regex_tmpa_int \l_regex_right_state_int - \regex_build_new_state: - \regex_build_new_state: - \regex_toks_put_right:Nx \l_regex_tmpa_int - { - \exp_not:N \if_num:w #1 > \l_regex_repetition_int - \regex_action_repeat_move:n - { \int_use:N \l_regex_left_state_int } - \exp_not:N \else: - #2 - \exp_not:N \fi: - } - \regex_toks_put_right:Nx \l_regex_left_state_int - { - \regex_build_tmp_class:n - { \int_use:N \l_regex_tmpa_int } - } + \prg_replicate:nn {#2} { \regex_build_one_: } + \prg_replicate:nn {#3-#2} {#1} } +\cs_new_protected_nopar:Npx \regex_build_one_nn: + { \regex_build_one_nn_aux:Nnn \exp_not:c { regex_build_one_?: } } +\cs_new_protected_nopar:cpx { regex_build_one_nn?: } + { \regex_build_one_nn_aux:Nnn \exp_not:c { regex_build_one_??: } } \cs_new_protected_nopar:cpn { regex_build_(: } #1#2 { \str_if_eq:nnTF { #1 #2 } { \regex_build_control:N ? } @@ -806,10 +809,19 @@ \cs_new_protected_nopar:Npn \regex_build_open_aux: { \regex_build_new_state: - \seq_push:Nx \l_regex_left_state_seq - { \int_use:N \l_regex_left_state_int } - \seq_push:Nx \l_regex_right_state_seq - { \int_use:N \l_regex_right_state_int } + \seq_push:Nx \l_regex_group_begin_seq + { + \int_set:Nn \l_regex_left_state_int + { \int_use:N \l_regex_left_state_int } + } + \bool_if:NTF \l_regex_caseless_bool + { \seq_push:Nn \l_regex_group_end_seq { \regex_build_caseless: } } + { \seq_push:Nn \l_regex_group_end_seq { \regex_build_caseful: } } + \seq_push:Nx \l_regex_group_end_seq + { + \int_set:Nn \l_regex_right_state_int + { \int_use:N \l_regex_right_state_int } + } \regex_build_begin_alternation: } \cs_new_protected_nopar:cpn { regex_build_|: } @@ -820,7 +832,7 @@ \cs_new_protected_nopar:cpn { regex_build_): } { \seq_if_empty:NTF \l_regex_capturing_group_seq - { \regex_build_error:n {22} } + { \msg_error:nn { regex } { 22 } } { \regex_build_close_aux: \regex_build_group_quantifier: @@ -829,30 +841,41 @@ \cs_new_protected_nopar:Npn \regex_build_close_aux: { \regex_build_end_alternation: - \seq_pop:NN \l_regex_left_state_seq \l_regex_tmpa_tl - \int_set:Nn \l_regex_left_state_int \l_regex_tmpa_tl - \seq_pop:NN \l_regex_right_state_seq \l_regex_tmpa_tl - \int_set:Nn \l_regex_right_state_int \l_regex_tmpa_tl + \regex_seq_pop:N \l_regex_group_begin_seq + \regex_seq_pop:N \l_regex_group_end_seq + \regex_seq_pop:N \l_regex_group_end_seq } \cs_new_protected_nopar:Npn \regex_build_begin_alternation: { \regex_build_new_state: - \seq_get:NN \l_regex_left_state_seq \l_regex_tmpa_tl - \int_set:Nn \l_regex_left_state_int \l_regex_tmpa_tl + \regex_seq_get:N \l_regex_group_begin_seq \regex_toks_put_right:Nx \l_regex_left_state_int - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } } \cs_new_protected_nopar:Npn \regex_build_end_alternation: { - \seq_get:NN \l_regex_right_state_seq \l_regex_tmpa_tl - \regex_toks_put_right:Nx \l_regex_right_state_int - { \regex_action_free:n { \l_regex_tmpa_tl } } + \int_set_eq:NN \l_regex_left_state_int \l_regex_right_state_int + \regex_seq_get:N \l_regex_group_end_seq + \regex_toks_put_right:Nx \l_regex_left_state_int + { + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_left_state_int } + } + } } \cs_new_protected_nopar:Npn \regex_build_special_group:NN #1#2 { \cs_if_exist_use:cF { regex_build_special_group_\token_to_str:N #2 : } { - \regex_build_error:n { -998 } + \msg_error:nn { regex } { -998 } \regex_build_control:N ( % ) \regex_build_raw:N ? #1 #2 @@ -863,6 +886,40 @@ \seq_push:Nx \l_regex_capturing_group_seq { * } \regex_build_open_aux: } +\cs_new_protected_nopar:Npn \regex_build_special_group_i: + { + \regex_build_options:NNN + + \regex_build_raw:N i + } +\cs_new_protected_nopar:cpn { regex_build_special_group_-: } + { + \regex_build_options:NNN - + } +\cs_new_protected_nopar:Npn \regex_build_options:NNN #1#2#3 + { + \token_if_eq_meaning:NNTF \regex_build_raw:N #2 + { + \cs_if_exist_use:cF { regex_build_option_#1#3: } + { \msg_error:nnx { regex } { unknown-option } { #3 } } + \regex_build_options:NNN #1 + } + { + \prg_case_str:nnn { #3 } + { % ( + { ) } { } + { - } { \regex_build_options:NNN - } + } + { \msg_error:nnx { regex } { invalid-in-option } { #3 } } + } + } +\cs_new_protected_nopar:cpn { regex_build_option_+i: } + { + \regex_build_caseless: + \cs_set_eq:NN \regex_match_loop_case_hook: + \regex_match_loop_caseless_hook: + } +\cs_new_protected_nopar:cpn { regex_build_option_-i: } + { \regex_build_caseful: } \cs_new_protected_nopar:Npn \regex_build_group_quantifier: { \tl_set:Nn \l_regex_one_or_group_tl { group } @@ -891,21 +948,33 @@ \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int \regex_build_new_state: \tex_toks:D \l_regex_right_state_int = \tex_toks:D \l_regex_tmpa_int - \use:x + \regex_toks_put_left:Nx \l_regex_right_state_int + { + \int_set:Nn \l_regex_current_state_int + { \int_use:N \l_regex_tmpa_int } % ^^A here we lie! + } + \cs_set:Npx \regex_tmp:w { \tex_toks:D \l_regex_tmpa_int - { \s_regex_toks #1 { \int_use:N \l_regex_right_state_int } } + { + \s_regex_toks + #1 { \int_eval:n { \l_regex_right_state_int - \l_regex_tmpa_int } } + } } + \regex_tmp:w \regex_build_group_submatches:NN \l_regex_right_state_int \l_regex_left_state_int - \int_set_eq:NN \l_regex_right_state_int \l_regex_left_state_int } \cs_new_protected_nopar:Npn \regex_build_group_qs_aux:NN #1#2 { \regex_build_group_shift:N \regex_action_free:n + \int_set_eq:NN \l_regex_right_state_int \l_regex_left_state_int \regex_build_transition_aux:NN \regex_action_free:n #1 #2 \l_regex_tmpa_int - { \regex_action_free:n { \int_use:N \l_regex_right_state_int } } + { + \regex_action_free:n + { \int_eval:n { \l_regex_right_state_int - \l_regex_tmpa_int } } + } } \cs_new_protected_nopar:cpn { regex_build_group_?: } { @@ -945,77 +1014,63 @@ \regex_action_free:n \l_regex_right_state_int \regex_action_free:n \l_regex_tmpa_int } -\cs_new_protected_nopar:Npn \regex_build_group_n: #1 +\cs_new_protected_nopar:Npn \regex_build_group_n_aux:n #1 { - \regex_build_group_shift:N \regex_action_repeat_move:n - \regex_build_transition_aux:NN - \regex_action_free:n \l_regex_tmpa_int - \use:x + \regex_toks_put_right:Nx \l_regex_right_state_int { - \tex_toks:D \l_regex_tmpa_int - { - \s_regex_toks - \exp_not:N \if_num:w #1 > \l_regex_repetition_int - \tex_the:D \tex_toks:D \l_regex_tmpa_int - \exp_not:N \else: - \regex_action_no_repeat_move:n - { \int_use:N \l_regex_right_state_int } - \exp_not:N \fi: - } + \regex_action_free:n + { \int_eval:n { \l_regex_max_state_int - \l_regex_left_state_int } } } - } -\cs_new_eq:cN { regex_build_group_n?: } \regex_build_group_n: -\cs_new_protected_nopar:Npn \regex_build_group_nn: #1#2 - { - \regex_build_group_shift:N \regex_action_repeat_move:n - \regex_build_transition_aux:NN - \regex_action_free:n \l_regex_tmpa_int - \use:x + \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int + \int_set:Nn \l_regex_tmpb_int { \l_regex_max_state_int + \c_one } + \int_set:Nn \l_regex_max_state_int { - \tex_toks:D \l_regex_tmpa_int - { - \s_regex_toks - \exp_not:N \if_num:w #1 > \l_regex_repetition_int - \tex_the:D \tex_toks:D \l_regex_tmpa_int - \exp_not:N \else: - \exp_not:N \if_num:w #2 > \l_regex_repetition_int - \tex_the:D \tex_toks:D \l_regex_tmpa_int - \exp_not:N \fi: - \regex_action_no_repeat_copy:n - { \int_use:N \l_regex_right_state_int } - \exp_not:N \fi: - } + \l_regex_left_state_int - \c_one + + #1 * ( \l_regex_max_state_int - \l_regex_left_state_int + \c_one ) + } + \int_until_do:nNnn \l_regex_tmpb_int > \l_regex_max_state_int + { + \tex_toks:D \l_regex_tmpb_int = \tex_toks:D \l_regex_tmpa_int + \int_incr:N \l_regex_tmpa_int + \int_incr:N \l_regex_tmpb_int } } -\cs_new_protected_nopar:cpn { regex_build_group_nn?: } #1#2 - { - \regex_build_group_shift:N \regex_action_repeat_move:n +\cs_new_protected_nopar:Npn \regex_build_group_n: #1 + { % ^^A todo: catch case #1 <= 0. + \regex_build_group_n_aux:n {#1} \regex_build_transition_aux:NN + \regex_action_free:n \l_regex_right_state_int + \regex_build_group_submatches:NN + \l_regex_tmpa_int \l_regex_left_state_int + } +\cs_new_eq:cN { regex_build_group_n?: } \regex_build_group_n: +\cs_new_protected_nopar:cpn { regex_build_group_n*: } #1 + { % ^^A todo: catch case #1 <= 0. + \regex_build_group_n_aux:n {#1} + \regex_build_transitions_aux:NNNN \regex_action_free:n \l_regex_tmpa_int - \use:x - { - \tex_toks:D \l_regex_tmpa_int - { - \s_regex_toks - \exp_not:N \if_num:w #1 > \l_regex_repetition_int - \tex_the:D \tex_toks:D \l_regex_tmpa_int - \exp_not:N \else: - \regex_action_no_repeat_copy:n - { \int_use:N \l_regex_right_state_int } - \exp_not:N \if_num:w #2 > \l_regex_repetition_int - \tex_the:D \tex_toks:D \l_regex_tmpa_int - \exp_not:N \fi: - \exp_not:N \fi: - } - } + \regex_action_free:n \l_regex_right_state_int + \regex_build_group_submatches:NN + \l_regex_tmpa_int \l_regex_left_state_int } -\cs_new_protected:Npn \regex_if_state_free:nT #1 - { - \if_num:w \tex_dimen:D #1 = \l_regex_unique_step_int - \exp_after:wN \use_none:n - \else: - \exp_after:wN \use:n - \fi: +\cs_new_protected_nopar:cpn { regex_build_group_n*?: } #1 + { % ^^A todo: catch case #1 <= 0. + \regex_build_group_n_aux:n {#1} + \regex_build_transitions_aux:NNNN + \regex_action_free:n \l_regex_right_state_int + \regex_action_free:n \l_regex_tmpa_int + \regex_build_group_submatches:NN + \l_regex_tmpa_int \l_regex_left_state_int + } +\cs_new_protected_nopar:Npn \regex_build_group_nn: #1#2 + { % ^^A Not Implemented Yet! + \msg_expandable_error:n { Quantifier~{m,n}~not~implemented~yet } + \use:c { regex_build_group_n*: } {#1} + } +\cs_new_protected_nopar:cpn { regex_build_group_nn?: } #1#2 + { % ^^A Not Implemented Yet! + \msg_expandable_error:n { Quantifier~{m,n}~not~implemented~yet } + \use:c { regex_build_group_n*?: } {#1} } \cs_new_protected:Npn \regex_store_state:n #1 { @@ -1023,23 +1078,24 @@ \tex_skip:D \l_regex_max_index_int #1 sp \scan_stop: \regex_store_submatches:n {#1} } -\cs_new_protected_nopar:Npn \regex_state_use_submatches: +\cs_new_protected_nopar:Npn \regex_state_use_with_submatches: { \regex_state_use_aux:n { } } \cs_new_protected_nopar:Npn \regex_state_use: { \regex_state_use_aux:n { \exp_after:wN \regex_state_use_aux_ii:w } } -\cs_new:Npn \regex_state_use_aux_ii:w #1 \s_regex_toks { } -\cs_new_protected:Npn \regex_state_use_aux:n #1 - { - \regex_if_state_free:nT { \l_regex_current_state_int } - { - \tex_dimen:D \l_regex_current_state_int - = \l_regex_unique_step_int sp \scan_stop: - #1 \tex_the:D \tex_toks:D \l_regex_current_state_int \scan_stop: - } +\cs_new_nopar:Npn \regex_state_use_aux_ii:w #1 \s_regex_toks { } +\cs_new_protected_nopar:Npn \regex_state_use_aux:n #1 + { + \if_num:w \tex_dimen:D \l_regex_current_state_int + < \l_regex_unique_step_int + \tex_dimen:D \l_regex_current_state_int + = \l_regex_unique_step_int sp \scan_stop: + #1 \tex_the:D \tex_toks:D \exp_after:wN \l_regex_current_state_int + \fi: + \scan_stop: } \cs_new_protected_nopar:Npn \regex_disable_submatches: { - \cs_set_eq:NN \regex_state_use_submatches: \regex_state_use: + \cs_set_eq:NN \regex_state_use_with_submatches: \regex_state_use: \cs_set_eq:NN \regex_store_submatches:n \regex_protected_use_none:n \cs_set_eq:NN \regex_action_submatches:n @@ -1048,55 +1104,73 @@ \cs_new_protected:Npn \regex_protected_use_none:n #1 { } \cs_new_protected:Npn \regex_store_submatches:n #1 { - \exp_after:wN \regex_store_submatches_aux:Nw - \exp_after:wN \l_regex_current_submatches_prop - \tex_the:D \tex_toks:D #1 \scan_stop: \q_stop - { #1 } + \tex_toks:D #1 \exp_after:wN + { + \tex_romannumeral:D + \exp_after:wN \regex_store_submatches_aux:w + \tex_the:D \tex_toks:D #1 + } } -\cs_new_protected:Npn \regex_store_submatches_aux:Nw #1 #2 \s_regex_toks +\cs_new_protected:Npn \regex_store_submatches_aux:w #1 \s_regex_toks { - \exp_args:No \regex_store_submatches_aux_ii:wnnwwn { #1 } - #2 - \regex_state_submatches:nn { \c_minus_one } { \q_prop } + \regex_store_submatches_aux_ii:Nnnw + #1 + \regex_state_submatches:nn \c_minus_one \q_prop \s_regex_toks } -\cs_new_protected:Npn \regex_store_submatches_aux_ii:wnnwwn - #1 \regex_state_submatches:nn #2#3 #4 \s_regex_toks #5 \q_stop #6 +\cs_new_protected:Npn \regex_store_submatches_aux_ii:Nnnw + \regex_state_submatches:nn #1 #2 #3 \s_regex_toks { - \tex_toks:D #6 \exp_after:wN + \exp_after:wN \c_zero + \exp_after:wN \regex_state_submatches:nn \exp_after:wN { - \exp_after:wN \regex_state_submatches:nn \exp_after:wN - { \int_value:w \int_eval:w \l_regex_unique_step_int + \c_one } - { #1 } - \regex_state_submatches:nn {#2} {#3} - \s_regex_toks - #5 + \int_value:w \int_eval:w + \l_regex_unique_step_int + \c_one + \exp_after:wN } + \exp_after:wN { \l_regex_current_submatches_prop } + \regex_state_submatches:nn {#1} {#2} + \s_regex_toks } \cs_new_protected:Npn \regex_state_submatches:nn #1#2 { \if_num:w #1 = \l_regex_unique_step_int - \tl_set:Nn \l_regex_current_submatches_prop {#2} + \tl_set:Nn \l_regex_current_submatches_prop { #2 } \fi: } \cs_new_protected:Npn \regex_match:n #1 { - \tl_set:Nx \l_regex_query_str { \tl_to_other_str:n {#1} } + \tl_set:Nx \l_regex_query_other_str { \tl_to_other_str:n {#1} } \regex_match_initial_setup: \regex_match_once: } \cs_new_protected_nopar:Npn \regex_match_once: { \regex_match_setup: - \exp_after:wN \regex_match_once_aux: \l_regex_query_str + \exp_after:wN \regex_match_once_aux: \l_regex_query_other_str \q_recursion_tail \q_recursion_stop \bool_if:NT \l_regex_success_bool { \l_regex_every_match_tl } } \cs_new_protected_nopar:Npn \regex_match_once_aux: - { \str_skip_do:nn { \l_regex_start_step_int } { \regex_match_loop:N } } + { + \int_compare:nNnTF \l_regex_start_step_int = \c_zero + { + \int_set_eq:NN \l_regex_current_char_int \c_minus_one + \regex_match_loop:N + } + { + \str_skip_do:nn + { \l_regex_start_step_int - \c_one } + { \regex_match_once_aux_ii:N } + } + } +\cs_new_protected_nopar:Npn \regex_match_once_aux_ii:N #1 + { + \int_set:Nn \l_regex_current_char_int { `#1 } + \regex_match_loop:N + } \cs_new_protected_nopar:Npn \regex_match_initial_setup: { - \tl_clear:N \l_regex_look_behind_str \prg_stepwise_inline:nnnn {1} {1} { \l_regex_max_state_int } { \tex_dimen:D ##1 \c_minus_one sp \scan_stop: } \int_set_eq:NN \l_regex_unique_step_int \c_minus_one @@ -1118,20 +1192,11 @@ \int_zero:N \l_regex_max_index_int \regex_store_state:n {1} } -\cs_new_protected_nopar:Npn \regex_match_loop_setup: - { - \int_incr:N \l_regex_current_step_int - \int_incr:N \l_regex_unique_step_int - \bool_set_false:N \l_regex_fresh_thread_bool - } \cs_new_eq:NN \s_regex_step \scan_stop: \cs_new:Npn \regex_break_step:w #1 \s_regex_step { } \cs_new_protected_nopar:Npn \regex_match_loop:N #1 { - \regex_match_loop_setup: - \token_if_eq_meaning:NNTF #1 \q_recursion_tail - { \int_set_eq:NN \l_regex_current_char_int \c_minus_one } - { \int_set:Nn \l_regex_current_char_int {`#1} } + \regex_match_loop_setup:N #1 \cs_set_nopar:Npx \regex_tmp:w { \int_zero:N \l_regex_max_index_int @@ -1145,19 +1210,49 @@ \exp_after:wN \use_none_delimit_by_q_recursion_stop:w \fi: \quark_if_recursion_tail_stop:N #1 - \bool_if:NT \l_regex_look_behind_bool - { \tl_put_left:Nx \l_regex_look_behind_str {#1} } \regex_match_loop:N } \cs_new_nopar:Npn \regex_match_one_index:n #1 { - \regex_match_one_index_aux:n { \int_value:w \tex_skip:D #1 } + \regex_match_one_index_aux:n + { \int_value:w \tex_skip:D #1 } } \cs_new_protected_nopar:Npn \regex_match_one_index_aux:n #1 { \int_set:Nn \l_regex_current_state_int {#1} \prop_clear:N \l_regex_current_submatches_prop - \regex_state_use_submatches: + \regex_state_use_with_submatches: + } +\cs_new_protected_nopar:Npn \regex_match_loop_setup:N #1 + { + \int_incr:N \l_regex_current_step_int + \int_incr:N \l_regex_unique_step_int + \bool_set_false:N \l_regex_fresh_thread_bool + \int_set_eq:NN \l_regex_last_char_int \l_regex_current_char_int + \if_meaning:w #1 \q_recursion_tail + \int_set_eq:NN \l_regex_current_char_int \c_minus_one + \else: + \int_set:Nn \l_regex_current_char_int {`#1} + \fi: + \regex_match_loop_case_hook: + } +\cs_new_protected_nopar:Npn \regex_match_loop_case_hook: { } +\cs_new_protected_nopar:Npn \regex_match_loop_caseless_hook: + { + \int_set_eq:NN \l_regex_case_changed_char_int \l_regex_current_char_int + \if_num:w \l_regex_current_char_int < \c_ninety_one + \if_num:w \l_regex_current_char_int < \c_sixty_five + \else: + \int_add:Nn \l_regex_case_changed_char_int { \c_thirty_two } + \fi: + \else: + \if_num:w \l_regex_current_char_int < \c_one_hundred_twenty_three + \if_num:w \l_regex_current_char_int < \c_ninety_seven + \else: + \int_sub:Nn \l_regex_case_changed_char_int { \c_thirty_two } + \fi: + \fi: + \fi: } \cs_new_protected_nopar:Npn \regex_action_start_wildcard:nn #1#2 { @@ -1167,7 +1262,10 @@ \regex_action_cost:n {#1} } \cs_new_protected_nopar:Npn \regex_action_cost:n #1 - { \regex_store_state:n {#1} } + { + \exp_args:Nf \regex_store_state:n %^^A optimize! + { \int_eval:n { \l_regex_current_state_int + #1 } } + } \cs_new_protected_nopar:Npn \regex_action_success: { \regex_last_match_empty:F @@ -1184,30 +1282,22 @@ } \cs_new_protected_nopar:Npn \regex_action_free:n #1 { - \regex_if_state_free:nT {#1} + \cs_set_nopar:Npx \regex_tmp:w { - \use:x - { - \int_set:Nn \l_regex_current_state_int {#1} - \regex_store_submatches:n { \l_regex_current_state_int } - \regex_state_use: - \int_set:Nn \l_regex_current_state_int - { \int_use:N \l_regex_current_state_int } - \tl_set:Nn \exp_not:N \l_regex_current_submatches_prop - { \exp_not:o \l_regex_current_submatches_prop } - } + \int_add:Nn \l_regex_current_state_int {#1} + \regex_state_use: + \int_set:Nn \l_regex_current_state_int + { \int_use:N \l_regex_current_state_int } + \tl_set:Nn \exp_not:N \l_regex_current_submatches_prop + { \exp_not:o \l_regex_current_submatches_prop } } + \regex_tmp:w } \cs_new_protected_nopar:Npn \regex_action_submatch:n #1 { \prop_put:Nno \l_regex_current_submatches_prop {#1} { \int_use:N \l_regex_current_step_int } - \regex_store_submatches:n { \l_regex_current_state_int } } -\cs_new_protected_nopar:Npn \regex_action_repeat_copy:n { \ERROR } -\cs_new_protected_nopar:Npn \regex_action_repeat_move:n { \ERROR } -\cs_new_protected_nopar:Npn \regex_action_no_repeat_copy:n { \ERROR } -\cs_new_protected_nopar:Npn \regex_action_no_repeat_move:n { \ERROR } \cs_new_protected_nopar:Npn \regex_extract: { \seq_gclear:N \g_regex_submatches_seq @@ -1217,11 +1307,7 @@ \regex_extract_aux:nTF { ##1 } { \seq_gput_right:Nx \g_regex_submatches_seq - { - \str_from_to:Nnn \l_regex_query_str - { \l_regex_tmpa_tl } - { \l_regex_tmpb_tl } - } + { \regex_query_substr:NN \l_regex_tmpa_tl \l_regex_tmpb_tl } } { \seq_gput_right:Nn \g_regex_submatches_seq { } } } @@ -1253,9 +1339,13 @@ = \int_use:N \l_regex_max_state_int \l_regex_capturing_group_int = \int_use:N \l_regex_capturing_group_int - \bool_if:NTF \l_regex_look_behind_bool - { \bool_set_true:N \l_regex_look_behind_bool } - { \bool_set_false:N \l_regex_look_behind_bool } + \token_if_eq_meaning:NNT + \regex_match_loop_case_hook: + \regex_match_loop_caseless_hook: + { + \cs_set_eq:NN \regex_match_loop_case_hook: + \regex_match_loop_caseless_hook: + } \prg_stepwise_function:nnnN {1} {1} {\l_regex_max_state_int} \regex_set_aux:n @@ -1269,7 +1359,7 @@ \cs_new:Npn \regex_nfa:Nw #1 #2 \regex_nfa_end: { \msg_expandable_error:n { Automaton~#1 used~incorrectly. } } \cs_new_eq:NN \regex_nfa_end: \scan_stop: -\prg_new_protected_conditional:Npnn \regex_check_nfa:N #1 { TF } +\prg_new_protected_conditional:Npnn \regex_check_nfa:N #1 { T , TF } { \exp_after:wN \regex_check_nfa_aux:Nw #1 \q_stop } \cs_new:Npn \regex_check_nfa_aux:Nw #1 #2 \q_stop { @@ -1280,11 +1370,6 @@ \prg_return_false: \fi: } -\msg_new:nnn { regex } { not-nfa } - { - I~was~expecting~a~regular~expression~variable.\\ - Instead,~I~got~#1. - } \cs_new_protected_nopar:Npn \regex_use:N #1 { \exp_after:wN \use_none:nn #1 } \cs_new_protected:Npn \regex_user_aux:n #1 @@ -1296,7 +1381,7 @@ } \cs_new_protected_nopar:Npn \regex_return_after_group: { - \if_bool:N \l_regex_success_bool + \if_meaning:w \c_true_bool \l_regex_success_bool \group_insert_after:N \prg_return_true: \else: \group_insert_after:N \prg_return_false: @@ -1304,7 +1389,7 @@ } \cs_new_protected_nopar:Npn \regex_extract_after_group:N #1 { - \if_bool:N \l_regex_success_bool + \if_meaning:w \c_true_bool \l_regex_success_bool \regex_extract: \group_insert_after:N \seq_set_eq:NN \group_insert_after:N #1 @@ -1359,7 +1444,7 @@ } \cs_new_protected:Npn \regex_count:NnN #1#2#3 { - \regex_check_nfa:NTF #1 + \regex_check_nfa:NT #1 { \regex_user_aux:n { @@ -1375,9 +1460,8 @@ \regex_count_after_group:N #3 } } - { } } -\cs_new_protected:Npn \regex_extract:nnN #1#2#3 +\cs_new_protected:Npn \regex_extract_once:nnN #1#2#3 { \regex_user_aux:n { @@ -1386,7 +1470,8 @@ \regex_extract_after_group:N #3 } } -\prg_new_protected_conditional:Npnn \regex_extract:nnN #1#2#3 { T , F , TF } +\prg_new_protected_conditional:Npnn \regex_extract_once:nnN #1#2#3 + { T , F , TF } { \regex_user_aux:n { @@ -1396,9 +1481,9 @@ \regex_return_after_group: } } -\cs_new_protected:Npn \regex_extract:NnN #1#2#3 +\cs_new_protected:Npn \regex_extract_once:NnN #1#2#3 { - \regex_check_nfa:NTF #1 + \regex_check_nfa:NT #1 { \regex_user_aux:n { @@ -1407,9 +1492,9 @@ \regex_extract_after_group:N #3 } } - { } } -\prg_new_protected_conditional:Npnn \regex_extract:NnN #1#2#3 { T , F , TF } +\prg_new_protected_conditional:Npnn \regex_extract_once:NnN #1#2#3 + { T , F , TF } { \regex_check_nfa:NTF #1 { @@ -1423,6 +1508,48 @@ } { \prg_return_false: } } +\cs_new_protected:Npn \regex_extract_all:nnN #1#2#3 + { + \regex_user_aux:n + { + \seq_gclear:N \g_regex_extract_all_seq + \tl_set:Nn \l_regex_every_match_tl { \regex_extract_all_aux: } + \regex_build:n {#1} + \regex_match:n {#2} + \regex_extract_all_after_group:N #3 + } + } +\cs_new_protected:Npn \regex_extract_all:NnN #1#2#3 + { + \regex_check_nfa:NT #1 + { + \regex_user_aux:n + { + \seq_gclear:N \g_regex_extract_all_seq + \tl_set:Nn \l_regex_every_match_tl { \regex_extract_all_aux: } + \regex_use:N #1 + \regex_match:n {#2} + \regex_extract_all_after_group:N #3 + } + } + } +\cs_new_protected_nopar:Npn \regex_extract_all_aux: + { + \regex_extract: + \seq_gconcat:NNN \g_regex_extract_all_seq + \g_regex_extract_all_seq \g_regex_submatches_seq + \regex_match_once: + } +\cs_new_protected_nopar:Npn \regex_extract_all_after_group:N #1 + { + \seq_if_empty:NF \g_regex_extract_all_seq + { + \regex_extract: + \group_insert_after:N \seq_set_eq:NN + \group_insert_after:N #1 + \group_insert_after:N \g_regex_extract_all_seq + } + } \cs_new_protected:Npn \regex_split:nnN #1#2#3 { \regex_user_aux:n @@ -1436,7 +1563,7 @@ } \cs_new_protected:Npn \regex_split:NnN #1#2#3 { - \regex_check_nfa:NTF #1 + \regex_check_nfa:NT #1 { \regex_user_aux:n { @@ -1447,7 +1574,6 @@ \regex_split_after_group:N #3 } } - { } } \cs_new_protected_nopar:Npn \regex_split_aux: { @@ -1459,9 +1585,8 @@ { \seq_gput_left:Nx \g_regex_submatches_seq { - \str_from_to:Nnn \l_regex_query_str - { \int_use:N \l_regex_start_step_int } - { \l_regex_tmpa_tl } + \regex_query_substr:NN + \l_regex_start_step_int \l_regex_tmpa_tl } } { \msg_error:nn { regex } { internal } } @@ -1480,9 +1605,8 @@ { \seq_gput_right:Nx \g_regex_split_seq { - \str_from_to:Nnn \l_regex_query_str - { \int_use:N \l_regex_start_step_int } - { \int_use:N \l_regex_current_step_int } + \regex_query_substr:NN + \l_regex_start_step_int \l_regex_current_step_int } } \group_insert_after:N \seq_set_eq:NN @@ -1494,40 +1618,57 @@ \str_aux_escape:NNNn \prg_do_nothing: \regex_replacement_escaped:N - \regex_replacement_raw:N + \prg_do_nothing: {#1} \tl_set_eq:NN \l_regex_replacement_tl \g_str_result_tl \tl_set:Nx \l_regex_replacement_tl { \l_regex_replacement_tl \prg_do_nothing: } } -\cs_new_nopar:Npn \regex_replacement_raw:N { \exp_not:N \prg_do_nothing: } \cs_new_nopar:Npn \regex_replacement_escaped:N #1 { - \if_num:w 9 < 1 #1 \exp_stop_f: - \exp_not:N \regex_replacement_escaped_ii:nN {#1} - \else: - #1 - \fi: + \regex_token_if_other_digit:NTF #1 + { + \exp_not:n + { \exp_not:n { \seq_item:Nn \g_regex_submatches_seq } } {#1} + } + { + \token_if_eq_charcode:NNTF g #1 + { \exp_not:N \regex_replacement_submatch:w } + { #1 } + } } -\cs_new_nopar:Npn \regex_replacement_escaped_ii:nN #1#2 +\cs_new_nopar:Npn \regex_replacement_submatch:w #1 { - \regex_token_if_other_digit:NTF #2 - { \regex_replacement_escaped_ii:nN {#1#2} } - { \exp_not:N \seq_item:Nn \exp_not:N \g_regex_submatches_seq {#1} #2 } + \exp_after:wN \token_if_eq_meaning:NNTF \c_lbrace_str #1 + { \regex_replacement_submatch_aux:nN { } } + { + \regex_token_if_other_digit:NTF #1 + { \exp_not:n { \seq_item:Nn \g_regex_submatches_seq } } + { \msg_expandable_error:n { \g misused~in~replacement~text. } } + #1 + } } -\prg_new_conditional:Npnn \regex_token_if_other_digit:N #1 { TF } +\cs_new_nopar:Npn \regex_replacement_submatch_aux:nN #1 #2 { - \if_num:w 9 < 1 \exp_not:N #1 \exp_stop_f: - \prg_return_true: - \else: - \prg_return_false: - \fi: + \regex_token_if_other_digit:NTF #2 + { \regex_replacement_submatch_aux:nN {#1#2} } + { + \exp_not:n { \seq_item:Nn \g_regex_submatches_seq } {#1} + \exp_after:wN \token_if_eq_meaning:NNF \c_rbrace_str #2 + { + \msg_expandable_error:n { \g misused~in~replacement~text. } + #2 + } + } } \cs_new_protected_nopar:Npn \regex_replace_after_group:N #1 { - \group_insert_after:N \tl_set_eq:NN + \group_insert_after:N \tl_set:Nx \group_insert_after:N #1 - \group_insert_after:N \g_regex_replaced_str + \group_insert_after:N { + \group_insert_after:N \tl_to_str:N + \group_insert_after:N \g_regex_replaced_str + \group_insert_after:N } } \cs_new_protected:Npn \regex_replace_once_aux:Nn #1#2 { @@ -1539,15 +1680,16 @@ \regex_extract: \regex_extract_aux:nTF {0} { + \cs_set_nopar:Npn \regex_tmp:w + { \str_skip_do:nn { \l_regex_tmpb_tl } { } } \tl_gset:Nx \g_regex_replaced_str { - \str_from_to:Nnn \l_regex_query_str {0} { \l_regex_tmpa_tl } + \regex_query_substr:NN \c_zero \l_regex_tmpa_tl \l_regex_replacement_tl - \str_from_to:Nnn \l_regex_query_str - { \l_regex_tmpb_tl } { \c_max_int } + \exp_after:wN \regex_tmp:w \l_regex_query_other_str } } - { \tl_gset_eq:NN \g_regex_replaced_str \l_regex_query_str } + { \tl_gset_eq:NN \g_regex_replaced_str \l_regex_query_other_str } \group_end: } \cs_new_protected:Npn \regex_replace_once:nnN #1#2#3 @@ -1592,12 +1734,10 @@ \tl_gclear:N \g_regex_replaced_str #2 \exp_args:No \regex_match:n {#1} + \cs_set_nopar:Npn \regex_tmp:w + { \str_skip_do:nn { \l_regex_start_step_int } { } } \tl_gput_right:Nx \g_regex_replaced_str - { - \str_from_to:Nnn \l_regex_query_str - { \int_use:N \l_regex_start_step_int } - { \c_max_int } - } + { \exp_after:wN \regex_tmp:w \l_regex_query_other_str } \group_end: } \cs_new_protected_nopar:Npn \regex_replace_all_aux: @@ -1607,9 +1747,8 @@ { \tl_gput_right:Nx \g_regex_replaced_str { - \str_from_to:Nnn \l_regex_query_str - { \int_use:N \l_regex_start_step_int } - { \l_regex_tmpa_tl } + \regex_query_substr:NN + \l_regex_start_step_int \l_regex_tmpa_tl \l_regex_replacement_tl } } @@ -1632,6 +1771,26 @@ \regex_replacement:n {#2} } } +\msg_new:nnn { regex } { -998 } { Unsupported~construct } +\msg_new:nnn { regex } { -997 } + { The~regular~expression~is~too~large~(32768~states). } +\msg_new:nnn { regex } { 1 } { \iow_char:N\\~at~end~of~pattern } +\msg_new:nnn { regex } { 4 } + { Numbers~out~of~order~in~\iow_char:N\{\iow_char\}~quantifier. } +\msg_new:nnn { regex } { 6 } + { Missing~terminating~\iow_char:N\]~for~character~class } +\msg_new:nnn { regex } { 7 } + { Invalid~escape~sequence~in~character~class } +\msg_new:nnn { regex } { 8 } + { Range~out~of~order~in~character~class } +\msg_new:nnn { regex } { 22 } { Mismatched~parentheses } +\msg_new:nnn { regex } { 34 } + { Character~value~in~\iow_char:N\\x{...}~sequence~is~too~large } +\msg_new:nnn { regex } { not-nfa } + { + LaTeX~was~expecting~a~regular~expression~variable.\\ + Instead,~LaTeX~found~'#1'. + } %% %% %% End of file `l3regex.sty'. |