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 | 1597 |
1 files changed, 992 insertions, 605 deletions
diff --git a/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty b/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty index 8d1f45803f9..f88c8fa4e15 100644 --- a/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty +++ b/Master/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty @@ -46,19 +46,63 @@ %% %% ----------------------------------------------------------------------- \RequirePackage{expl3} -\GetIdInfo$Id: l3regex.dtx 3200 2012-01-16 03:50:54Z bruno $ +\GetIdInfo$Id: l3regex.dtx 3259 2012-01-29 20:50:48Z bruno $ {L3 Experimental Regular Expressions} \ProvidesExplPackage {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription} \RequirePackage{l3str, l3tl-analysis, l3flag} \cs_new:Npn \regex_tmp:w { } -\tl_new:N \l_regex_tmpa_tl -\tl_new:N \l_regex_tmpb_tl -\tl_new:N \l_regex_tmpc_tl -\int_new:N \l_regex_tmpa_int -\int_new:N \l_regex_tmpb_int -\flag_new:N \l_regex_group_begin_flag -\flag_new:N \l_regex_group_end_flag +\tl_new:N \l_regex_internal_a_tl +\tl_new:N \l_regex_internal_b_tl +\int_new:N \l_regex_internal_a_int +\int_new:N \l_regex_internal_b_int +\int_new:N \l_regex_internal_c_int +\tl_new:N \g_regex_internal_tl +\tl_const:Nn \c_regex_d_tl + { \regex_item_range:nn \c_forty_eight { 57 } } % 0--9 +\tl_const:Nn \c_regex_h_tl + { + \regex_item_equal:n \c_thirty_two % space + \regex_item_equal:n \c_nine % tab + } +\tl_const:Nn \c_regex_s_tl + { + \regex_item_equal:n \c_thirty_two % space + \regex_item_equal:n \c_nine % tab + \regex_item_equal:n \c_ten % lf + \regex_item_equal:n \c_twelve % ff + \regex_item_equal:n \c_thirteen % cr + } +\tl_const:Nn \c_regex_v_tl + { \regex_item_range:nn \c_ten \c_thirteen } % lf, vtab, ff, cr +\tl_const:Nn \c_regex_w_tl + { + \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_D_tl + { \c_regex_d_tl \regex_break_point:TF { } \regex_break_true:w } +\tl_const:Nn \c_regex_H_tl + { \c_regex_h_tl \regex_break_point:TF { } \regex_break_true:w } +\tl_const:Nn \c_regex_S_tl + { \c_regex_s_tl \regex_break_point:TF { } \regex_break_true:w } +\tl_const:Nn \c_regex_V_tl + { \c_regex_v_tl \regex_break_point:TF { } \regex_break_true:w } +\tl_const:Nn \c_regex_W_tl + { \c_regex_w_tl \regex_break_point:TF { } \regex_break_true:w } +\tl_const:Nn \c_regex_N_tl + { + \regex_item_equal:n \c_ten + \regex_break_point:TF { } { \regex_break_true:w } + } +\tl_const:cn { c_regex_._tl } + { + \if_num:w \l_regex_current_char_int > - \c_two + \exp_after:wN \regex_break_true:w + \fi: + } \int_new:N \l_regex_build_mode_int \int_new:N \l_regex_max_state_int \int_new:N \l_regex_left_state_int @@ -87,105 +131,56 @@ \int_const:Nn \c_regex_catcode_A_int { "4000000 } \int_const:Nn \c_regex_catcodes_all_int { "5515155 } \bool_new:N \l_regex_catcodes_bool -\tl_new:N \l_regex_tmpa_regex_tl -\bool_new:N \l_regex_class_bool -\tl_new:N \l_regex_class_tl -\bool_new:N \l_regex_class_saved_bool -\tl_new:N \l_regex_class_saved_tl -\tl_const:Nn \c_regex_d_tl - { - \regex_item_range:nn { \c_forty_eight } { 57 } % 0--9 - } -\tl_const:Nn \c_regex_D_tl - { - \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 { \c_thirty_two } % space - \regex_item_equal:n { \c_nine } % tab - } -\tl_const:Nn \c_regex_H_tl - { - \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 { \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_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 { \c_ten } { \c_thirteen } % lf, vtab, ff, cr - } -\tl_const:Nn \c_regex_V_tl - { - \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 { \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 { \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_geq:n { \c_eleven } % > lf - \regex_item_range:nn { \c_zero } { \c_nine } % < lf - } +\bool_new:N \l_regex_tests_bool +\tl_new:N \l_regex_tests_tl +\bool_new:N \l_regex_tests_saved_bool +\tl_new:N \l_regex_tests_saved_tl +\int_new:N \l_regex_catcodes_saved_int +\int_new:N \l_regex_min_index_int +\int_new:N \l_regex_max_index_int \int_new:N \l_regex_nesting_int -\int_new:N \l_regex_min_step_int -\int_new:N \l_regex_max_step_int -\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_id_int +\int_new:N \l_regex_current_index_int +\int_new:N \l_regex_start_index_int +\int_new:N \l_regex_success_index_int \int_new:N \l_regex_current_char_int \int_new:N \l_regex_current_catcode_int -\tl_new:N \l_regex_current_token_tl +\tl_new:N \l_regex_current_token_tl \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 +\int_new:N \l_regex_step_int +\int_new:N \l_regex_max_active_int \tl_new:N \l_regex_every_match_tl +\bool_new:N \l_regex_fresh_thread_bool +\bool_new:N \l_regex_success_empty_bool +\cs_new_eq:NN \regex_if_two_empty_matches:F \use:n \bool_new:N \g_regex_success_bool \bool_new:N \l_regex_saved_success_bool \bool_new:N \l_regex_success_match_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_empty_bool -\bool_new:N \l_regex_fresh_thread_bool -\int_new:N \l_regex_replacement_int +\tl_const:Nn \c_regex_no_match_regex + { + \regex_nfa:Nw \c_regex_no_match_regex + \l_regex_max_state_int = \c_one + \l_regex_capturing_group_int = \c_zero + \tex_toks:D \c_zero { \s_stop } + \s_stop + } +\cs_new_protected:Npn \regex_new:N #1 + { + \chk_if_free_cs:N #1 + \cs_gset_eq:NN #1 \c_regex_no_match_regex + } +\regex_new:N \l_regex_internal_regex \int_new:N \l_regex_replacement_csnames_int +\int_new:N \l_regex_replacement_int \int_new:N \l_regex_submatch_int \int_new:N \l_regex_submatch_start_int \int_new:N \l_regex_match_count_int +\flag_new:n { regex_begin } +\flag_new:n { regex_end } \cs_new_protected:Npn \regex_toks_put_left:Nx #1#2 { \cs_set_nopar:Npx \regex_tmp:w \s_stop { \s_stop #2 } @@ -198,11 +193,22 @@ \tex_toks:D #1 \exp_after:wN { \tex_the:D \tex_toks:D \exp_after:wN #1 \regex_tmp:w } } -\cs_new:Npn \regex_query_substr:nn #1#2 +\cs_new:Npn \regex_toks_range:nn #1#2 + { + \exp_after:wN \regex_toks_range:ww + \int_use:N \int_eval:w #1 \exp_after:wN ; + \int_use:N \int_eval:w #2 ; + \prg_break_point:n { } + } +\cs_new:Npn \regex_toks_range:ww #1 ; #2 ; { - \if_num:w \int_eval:w #1 < \int_eval:w #2 \int_eval_end: - \str_aux_toks_range:nn {#1} {#2} + \if_num:w #1 < #2 \exp_stop_f: + \else: + \exp_after:wN \prg_map_break: \fi: + \tex_the:D \tex_toks:D #1 \exp_stop_f: + \exp_after:wN \regex_toks_range:ww + \int_use:N \int_eval:w #1 + \c_one ; #2 ; } \cs_new:Npn \regex_query_submatch:nn #1#2 { @@ -213,41 +219,63 @@ } \cs_new:Npn \regex_query_submatch:w #1 ; { - \regex_query_substr:nn + \regex_toks_range:nn { \tex_skip:D #1 } { \etex_gluestretch:D \tex_skip:D #1 } } \cs_new_protected:Npn \regex_seq_pop_int:NN #1#2 { - \seq_pop:NN #1 \l_regex_tmpa_tl - \int_set:Nn #2 \l_regex_tmpa_tl + \seq_pop:NN #1 \l_regex_internal_a_tl + \int_set:Nn #2 \l_regex_internal_a_tl } \cs_new_protected:Npn \regex_seq_get_int:NN #1#2 { - \seq_get:NN #1 \l_regex_tmpa_tl - \int_set:Nn #2 \l_regex_tmpa_tl + \seq_get:NN #1 \l_regex_internal_a_tl + \int_set:Nn #2 \l_regex_internal_a_tl } \cs_new_protected:Npn \regex_seq_push_int:NN #1#2 { \seq_push:No #1 { \int_use:N #2 } } \cs_new_protected:Npn \regex_seq_pop_use:N #1 { - \seq_pop:NN #1 \l_regex_tmpa_tl - \l_regex_tmpa_tl + \seq_pop:NN #1 \l_regex_internal_a_tl + \l_regex_internal_a_tl } \cs_new_protected:Npn \regex_seq_get_use:N #1 { - \seq_get:NN #1 \l_regex_tmpa_tl - \l_regex_tmpa_tl + \seq_get:NN #1 \l_regex_internal_a_tl + \l_regex_internal_a_tl } -\cs_new_protected:Npn \regex_break_true:w - #1 \regex_break_point:TF #2 #3 {#2} -\cs_new_protected:Npn \regex_break_point:TF #1 #2 { #2 } -\cs_new_protected_nopar:Npn \regex_item_dot: +\cs_new_protected:Npn \regex_get_digits:nw #1 { - \if_num:w \l_regex_current_char_int > - \c_two - \exp_after:wN \regex_break_true:w + \tex_afterassignment:D \regex_tmp:w + \cs_set_nopar:Npx \regex_tmp:w + { + \exp_not:n {#1} + { \if_false: } } \fi: + \regex_get_digits_aux:NN + } +\cs_new:Npn \regex_get_digits_aux:NN #1#2 + { + \if_meaning:w \regex_build_raw:N #1 + \if_num:w 9 < 1 \exp_not:N #2 \exp_stop_f: + #2 + \else: + \regex_get_digits_end:w #1 #2 + \fi: + \else: + \regex_get_digits_end:w #1 #2 \fi: + \regex_get_digits_aux:NN + } +\cs_new:Npn \regex_get_digits_end:w #1 \fi: #2 \regex_get_digits_aux:NN + { + \fi: #2 + \if_false: { { \fi: } } + #1 } +\cs_new_protected:Npn \regex_break_true:w + #1 \regex_break_point:TF #2 #3 {#2} +\cs_new_protected:Npn \regex_break_point:TF #1 #2 { #2 } \cs_new_protected:Npn \regex_item_caseful_equal:n #1 { \if_num:w #1 = \l_regex_current_char_int @@ -316,16 +344,19 @@ \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_item_catcode_aux: + { + " + \if_case:w \l_regex_current_catcode_int + 1 \or: 4 \or: 10 \or: 40 + \or: 100 \or: \or: 1000 \or: 4000 + \or: 10000 \or: \or: 100000 \or: 400000 + \or: 1000000 \or: 4000000 \else: 1*\c_zero + \fi: + } \cs_new_protected:Npn \regex_item_catcode:nT #1 { - \if_int_odd:w \int_eval:w #1 / " - \if_case:w \l_regex_current_catcode_int - 1 \or: 4 \or: 10 \or: 40 - \or: 100 \or: \or: 1000 \or: 4000 - \or: 10000 \or: \or: 100000 \or: 400000 - \or: 1000000 \or: 4000000 \else: 1*\c_zero - \fi: - \int_eval_end: + \if_int_odd:w \int_eval:w #1 / \regex_item_catcode_aux: \int_eval_end: \exp_after:wN \use:n \else: \exp_after:wN \use_none:n @@ -335,9 +366,9 @@ { \int_compare:nNnT \l_regex_current_catcode_int = \c_zero { - \tl_set:Nn \l_regex_tmpa_regex_tl {#1} + \tl_set:Nn \l_regex_internal_regex {#1} \bool_set_eq:NN \l_regex_saved_success_bool \g_regex_success_bool - \exp_args:NNx \regex_match:NnTF \l_regex_tmpa_regex_tl + \exp_args:NNx \regex_match:NnTF \l_regex_internal_regex { \exp_after:wN \exp_after:wN \exp_after:wN \cs_to_str:N \l_regex_current_token_tl @@ -349,43 +380,140 @@ { \bool_gset_eq:NN \g_regex_success_bool \l_regex_saved_success_bool } } } -\cs_new_protected:Npn \regex_get_digits:nw #1 +\cs_new_protected:Npn \regex_escape_use:nnnn #1#2#3#4 { - \tex_afterassignment:D \regex_tmp:w - \cs_set_nopar:Npx \regex_tmp:w + \group_begin: + \cs_set_nopar:Npn \regex_escape_unescaped:N ##1 { #1 } + \cs_set_nopar:Npn \regex_escape_escaped:N ##1 { #2 } + \cs_set_nopar:Npn \regex_escape_raw:N ##1 { #3 } + \int_set:Nn \tex_escapechar:D { 92 } + \str_gset_other:Nn \g_regex_internal_tl {#4} + \int_zero:N \l_regex_internal_a_int + \tl_set:Nx \l_regex_internal_a_tl + { + \exp_after:wN \regex_escape_loop:N \g_regex_internal_tl + { break } \prg_break_point:n { } + } + \use:x + { + \group_end: + \regex_toks_range:nn \c_zero \l_regex_internal_a_int + \exp_not:o \l_regex_internal_a_tl + } + } +\cs_new:Npn \regex_escape_loop:N #1 + { + \cs_if_exist_use:cF { regex_escape_\token_to_str:N #1:w } + { \regex_escape_unescaped:N #1 } + \regex_escape_loop:N + } +\cs_new_nopar:cpn { regex_escape_ \c_backslash_str :w } + \regex_escape_loop:N #1 + { + \cs_if_exist_use:cF { regex_escape_/\token_to_str:N #1:w } + { \regex_escape_escaped:N #1 } + \regex_escape_loop:N + } +\cs_new_eq:NN \regex_escape_unescaped:N ? +\cs_new_eq:NN \regex_escape_escaped:N ? +\cs_new_eq:NN \regex_escape_raw:N ? +\cs_new_eq:NN \regex_escape_break:w \prg_map_break: +\cs_new_nopar:cpn { regex_escape_/break:w } + { + \if_false: { \fi: } + \msg_kernel_error:nn { regex } { trailing-backslash } + \exp_after:wN \use_none:n \exp_after:wN { \if_false: } \fi: + } +\cs_new_nopar:cpn { regex_escape_~:w } { } +\cs_new_nopar:cpx { regex_escape_/a:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^G } +\cs_new_nopar:cpx { regex_escape_/t:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^I } +\cs_new_nopar:cpx { regex_escape_/n:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^J } +\cs_new_nopar:cpx { regex_escape_/f:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^L } +\cs_new_nopar:cpx { regex_escape_/r:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^M } +\cs_new_nopar:cpx { regex_escape_/e:w } + { \exp_not:N \regex_escape_raw:N \iow_char:N \^^[ } +\cs_new_nopar:cpn { regex_escape_/x:w } \regex_escape_loop:N + { + \if_false: { \fi: } + \tex_toks:D \l_regex_internal_a_int + \exp_after:wN { \l_regex_internal_a_tl } + \int_incr:N \l_regex_internal_a_int + \int_zero:N \l_regex_current_char_int + \regex_escape_x_test:N + } +\cs_new_protected:Npx \regex_escape_x_test:N #1 + { + \exp_not:N \token_if_eq_charcode:NNTF \c_space_token #1 + { \exp_not:N \regex_escape_x_test:N } { - \exp_not:n {#1} - { \if_false: } } \fi: - \regex_get_digits_aux:NN + \l_regex_current_char_int = "0 + \exp_not:N \token_if_eq_charcode:NNTF \c_lbrace_str #1 + { \exp_not:N \regex_escape_x_braced_loop:N } + { \exp_not:N \regex_escape_x_unbraced_i:N #1 } + } } -\cs_new:Npn \regex_get_digits_aux:NN #1#2 +\cs_new:Npn \regex_escape_x_unbraced_i:N #1 { - \if_meaning:w \regex_build_raw:N #1 - \if_charcode:w \c_space_token \exp_not:N #2 - \else: - \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: - \regex_get_digits_aux:NN + \str_aux_hexadecimal_use:NTF #1 + { \regex_escape_x_unbraced_ii:N } + { \exp_stop_f: \regex_escape_x_end: #1 } } -\cs_new:Npn \regex_get_digits_end:w #1 \fi: #2 \regex_get_digits_aux:NN +\cs_new:Npn \regex_escape_x_unbraced_ii:N #1 { - \fi: #2 - \if_false: { { \fi: } } - #1 + \token_if_eq_charcode:NNTF \c_space_token #1 + { \regex_escape_x_unbraced_ii:N } + { + \str_aux_hexadecimal_use:NTF #1 + { \exp_stop_f: \regex_escape_x_end: } + { \exp_stop_f: \regex_escape_x_end: #1 } + } + } +\cs_new:Npn \regex_escape_x_braced_loop:N #1 + { + \token_if_eq_charcode:NNTF \c_space_token #1 + { \regex_escape_x_braced_loop:N } + { + \str_aux_hexadecimal_use:NTF #1 + { \regex_escape_x_braced_loop:N } + { \exp_stop_f: \regex_escape_x_braced_end:N #1 } + } } -\prg_new_conditional:Npnn \regex_token_if_other_digit:N #1 { TF } +\cs_new_protected:Npx \regex_escape_x_braced_end:N #1 { - \if_num:w \c_nine < 1 \exp_not:N #1 \exp_stop_f: - \prg_return_true: \else: \prg_return_false: \fi: + \exp_not:N \token_if_eq_charcode:NNTF \c_rbrace_str #1 + { \exp_not:N \regex_escape_x_end: } + { + \msg_kernel_error:nnx { regex } { x-missing-rbrace } + { \int_use:N \l_regex_current_char_int } + \exp_not:N \regex_escape_x_end: #1 + } } -\prg_new_conditional:Npnn \regex_aux_char_if_special:N #1 { TF } +\group_begin: + \char_set_catcode_other:N \^^@ + \cs_new_protected_nopar:Npn \regex_escape_x_end: + { + \if_num:w \l_regex_current_char_int > \c_max_char_int + \msg_kernel_error:nnx { regex } { x-overflow } + { \int_use:N \l_regex_current_char_int } + \exp_after:wN \use:n + \else: + \tex_lccode:D \c_zero \l_regex_current_char_int + \exp_after:wN \tl_to_lowercase:n + \fi: + { + \tl_set:Nx \l_regex_internal_a_tl + { \if_false: } \fi: + \regex_escape_raw:N ^^@ + \regex_escape_loop:N + } + } +\group_end: +\prg_new_conditional:Npnn \regex_char_if_special:N #1 { TF } { \if_num:w `#1 < \c_ninety_one \if_num:w `#1 < \c_fifty_eight @@ -407,7 +535,7 @@ \fi: \fi: } -\prg_new_conditional:Npnn \regex_aux_char_if_alphanumeric:N #1 { TF } +\prg_new_conditional:Npnn \regex_char_if_alphanumeric:N #1 { TF } { \if_num:w `#1 < \c_ninety_one \if_num:w `#1 < \c_fifty_eight @@ -434,6 +562,18 @@ \exp_after:wN \use_ii:nn \fi: } +\cs_new_nopar:Npn \regex_build_if_assertions_forbidden:TF + { + \if_int_odd:w \l_regex_build_mode_int + \exp_after:wN \use_i:nn + \else: + \if_int_compare:w \l_regex_build_mode_int > \c_zero + \exp_after:wN \exp_after:wN \exp_after:wN \use_i:nn + \else: + \exp_after:wN \exp_after:wN \exp_after:wN \use_ii:nn + \fi: + \fi: + } \cs_new_protected_nopar:Npn \regex_build_new_state: { \tex_toks:D \l_regex_max_state_int { \s_stop } @@ -456,61 +596,35 @@ #3 { \int_eval:n { #4 - \l_regex_left_state_int } } } } -\cs_new_protected:Npn \regex_build:n #1 - { - \int_set_eq:NN \l_regex_build_mode_int \c_zero - \regex_build:w - \regex_build_new_state: - \regex_toks_put_right:Nx \l_regex_left_state_int - { \regex_action_start_wildcard: } - \regex_build_open_aux: - \str_escape_use:NNNn - \regex_build_i_unescaped:N - \regex_build_i_escaped:N - \regex_build_i_raw:N - { #1 } - \prg_do_nothing: \prg_do_nothing: - \prg_do_nothing: \prg_do_nothing: - \regex_build_end: - } -\cs_new:Npn \regex_build_i_unescaped:N #1 - { - \regex_aux_char_if_special:NTF #1 - { \exp_not:N \regex_build_special:N #1 } - { \exp_not:N \regex_build_raw:N #1 } - } -\cs_new:Npn \regex_build_i_escaped:N #1 - { - \regex_aux_char_if_alphanumeric:NTF #1 - { \exp_not:N \regex_build_escaped:N #1 } - { \exp_not:N \regex_build_raw:N #1 } - } -\cs_new:Npn \regex_build_i_raw:N #1 - { \exp_not:N \regex_build_raw:N #1 } -\cs_new_protected:Npn \regex_build_special:N #1 - { - \cs_if_exist_use:cF { regex_build_#1: } - { \regex_build_raw:N #1 } - } -\cs_new_protected:Npn \regex_build_escaped:N #1 - { - \cs_if_exist_use:cF { regex_build_/#1: } - { \regex_build_raw:N #1 } - } \cs_new_protected_nopar:Npn \regex_build:w { \int_set_eq:NN \l_regex_catcodes_default_int \c_regex_catcodes_all_int - \int_set_eq:NN \l_regex_capturing_group_int \c_zero + \int_set_eq:NN \l_regex_catcodes_int \l_regex_catcodes_default_int + \int_set_eq:NN \l_regex_capturing_group_int \c_zero + \tl_clear:N \l_regex_tests_tl + \bool_set_true:N \l_regex_tests_bool \int_zero:N \l_regex_max_state_int \regex_build_new_state: - \tl_clear:N \l_regex_class_tl - \bool_set_true:N \l_regex_class_bool - \int_set_eq:NN \l_regex_catcodes_int \l_regex_catcodes_default_int } \cs_new_protected_nopar:Npn \regex_build_end: { - \regex_seq_push_int:NN \l_regex_capturing_group_seq \c_zero - \regex_build_close_aux: \regex_build_group_: + \regex_build_exit_class: + \regex_build_exit_groups: + \regex_toks_put_right:Nx \l_regex_right_state_int + { \regex_action_success: } + \int_incr:N \l_regex_capturing_group_int + } +\cs_new_protected_nopar:Npn \regex_build_exit_class: + { + \regex_build_if_in_class:TF + { + \msg_kernel_error:nn { regex } { missing-rbrack } + \use:c { regex_build_]: } + } + { } + } +\cs_new_protected_nopar:Npn \regex_build_exit_groups: + { \seq_if_empty:NF \l_regex_capturing_group_seq { \msg_kernel_error:nnx { regex } { missing-rparen } @@ -519,23 +633,61 @@ { \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 - { \regex_action_success: } - \int_incr:N \l_regex_capturing_group_int + } +\cs_new_protected:Npn \regex_build:n #1 + { + \regex_build:w + \int_set_eq:NN \l_regex_build_mode_int \c_zero + \regex_build_new_state: + \regex_toks_put_right:Nx \l_regex_left_state_int + { \regex_action_start_wildcard: } + \regex_build_open_aux: + \regex_escape_use:nnnn + { + \regex_char_if_special:NTF ##1 + \regex_build_special:N \regex_build_raw:N ##1 + } + { + \regex_char_if_alphanumeric:NTF ##1 + \regex_build_escaped:N \regex_build_raw:N ##1 + } + { \regex_build_raw:N ##1 } + { #1 } + \prg_do_nothing: \prg_do_nothing: + \prg_do_nothing: \prg_do_nothing: + \int_compare:nNnT \l_regex_build_mode_int < \c_zero + { + \msg_kernel_error:nn { regex } { c-missing-rbrace } + \exp_after:wN \regex_build_special:N \c_rbrace_str + } + \seq_put_right:Nn \l_regex_capturing_group_seq {0} + \regex_build_close_aux: \regex_build_group_: + \regex_build_end: + } +\cs_new_protected:Npn \regex_build_special:N #1 + { + \cs_if_exist_use:cF { regex_build_#1: } + { \regex_build_raw:N #1 } + } +\cs_new_protected:Npn \regex_build_escaped:N #1 + { + \cs_if_exist_use:cF { regex_build_/#1: } + { \regex_build_raw:N #1 } } \cs_new_protected:Npn \regex_build_one:n #1 { \regex_build_one:x { \exp_not:n {#1} } } \cs_new_protected:Npn \regex_build_one:x #1 { - \tl_put_right:Nx \l_regex_class_tl + \tl_put_right:Nx \l_regex_tests_tl { - \if_num:w \l_regex_catcodes_int = \c_regex_catcodes_all_int - \exp_after:wN \use:n - \else: + \if_num:w \l_regex_catcodes_int < \c_regex_catcodes_all_int \regex_item_catcode:nT { \int_use:N \l_regex_catcodes_int } + \else: + \exp_after:wN \use:n \fi: {#1} } + \int_set_eq:NN \l_regex_catcodes_int \l_regex_catcodes_default_int \if_num:w \l_regex_build_mode_int = \c_two \l_regex_build_mode_int = \c_zero \else: @@ -547,28 +699,21 @@ \exp_after:wN \regex_build_one_quantifier: \fi: } -\cs_new:Npn \regex_build_tmp_class:n #1 +\cs_new:Npn \regex_tests_action_cost:n #1 { - \exp_not:o \l_regex_class_tl - \bool_if:NTF \l_regex_class_bool + \exp_not:o \l_regex_tests_tl + \bool_if:NTF \l_regex_tests_bool { \regex_break_point:TF { \regex_action_cost:n {#1} } { } } { \regex_break_point:TF { } { \regex_action_cost:n {#1} } } } -\cs_new_protected:Npn \regex_class_put:N #1 - { - \tl_put_right:Nx \l_regex_class_tl - { \regex_item_equal:n { \int_value:w `#1 ~ } } - } -\cs_new_protected:Npn \regex_class_put:NN #1#2 - { - \tl_put_right:Nx \l_regex_class_tl - { \regex_item_range:nn { \int_value:w `#1 ~ } { \int_value:w `#2 ~ } } - } -\cs_new_protected:Npn \regex_build_raw_alphanum_is_error:N #1 +\cs_new_protected:Npn \regex_build_raw_error:N #1 { - \regex_aux_char_if_alphanumeric:NTF #1 - { \msg_kernel_error:nnx { regex } { class-bad-escape } { #1 } } - { \regex_build_raw:N #1 } + \if_int_odd:w \l_regex_build_mode_int + \msg_kernel_error:nnx { regex } { class-bad-escape } {#1} + \else: + \msg_kernel_error:nnx { regex } { catcode-bad-escape } {#1} + \fi: + \regex_build_raw:N #1 } \cs_new_protected:Npn \regex_build_raw:N #1#2#3 { @@ -586,39 +731,45 @@ \token_if_eq_meaning:NNTF #2 \regex_build_escaped:N % [ { \use_i:nn } { \str_if_eq:nnTF { #2#3 } { \regex_build_special:N ] } } { - \msg_kernel_warning:nnx { regex } { end-range-missing } % [ - { #1 - \if_meaning:w #3 ] \else: \c_backslash_str \fi: #3 } - \regex_class_put:N #1 - \regex_class_put:N - + \msg_kernel_warning:nnxx { regex } { range-missing-end } % [ + {#1} { \token_if_eq_charcode:NNF #3 ] { \c_backslash_str } #3 } + \tl_put_right:Nx \l_regex_tests_tl + { + \regex_item_equal:n { \int_value:w `#1 ~ } + \regex_item_equal:n { \int_value:w `- ~ } + } #2#3 } { \if_num:w `#1 > `#3 \exp_stop_f: - \msg_kernel_error:nnxx { regex } { backwards-range } {#1} {#3} + \msg_kernel_error:nnxx { regex } { range-backwards } {#1} {#3} \else: - \if_num:w `#1 = `#3 \exp_stop_f: - \regex_class_put:N #3 - \else: - \regex_class_put:NN #1#3 - \fi: + \tl_put_right:Nx \l_regex_tests_tl + { + \if_num:w `#1 = `#3 \exp_stop_f: + \regex_item_equal:n + \else: + \regex_item_range:nn { \int_value:w `#1 ~ } + \fi: + { \int_value:w `#3 ~ } + } \fi: } } -\cs_new_protected_nopar:cpn { regex_build_.: } +\cs_new_protected_nopar:cpx { regex_build_.: } { - \regex_build_if_in_class:TF + \exp_not:N \regex_build_if_in_class:TF { \regex_build_raw:N . } - { \regex_build_one:x \regex_item_dot: } + { \regex_build_one:n \exp_not:c { c_regex_._tl } } } \tl_map_inline:nn { dDhHsSvVwWN } { \cs_new_protected_nopar:cpx { regex_build_/#1: } { \regex_build_one:n \exp_not:c { c_regex_#1_tl } } } -\cs_new_protected:Npn \regex_build_simple_assertion:Nn #1#2 +\cs_new_protected:Npn \regex_build_simple_assertion:nn #1#2 { - \regex_build_if_in_class:TF - { \regex_build_raw_alphanum_is_error:N #1 } + \regex_build_if_assertions_forbidden:TF {#1} { \regex_build_new_state: \regex_toks_put_right:Nx \l_regex_left_state_int @@ -632,47 +783,50 @@ } } } - %^^A \regex_assertion_quantifier: + %^^A todo: add \regex_assertion_quantifier: } } \cs_new_protected_nopar:cpn { regex_build_^: } { - \regex_build_simple_assertion:Nn ^ - { \int_compare:nNnT \l_regex_min_step_int = \l_regex_current_step_int } + \regex_build_simple_assertion:nn { \regex_build_raw:N ^ } + { \int_compare:nNnT \l_regex_min_index_int = \l_regex_current_index_int } } \cs_new_protected_nopar:cpn { regex_build_/A: } { - \regex_build_simple_assertion:Nn A - { \int_compare:nNnT \l_regex_min_step_int = \l_regex_current_step_int } + \regex_build_simple_assertion:nn { \regex_build_raw_error:N A } + { \int_compare:nNnT \l_regex_min_index_int = \l_regex_current_index_int } } \cs_new_protected_nopar:cpn { regex_build_/G: } { - \regex_build_simple_assertion:Nn G - { \int_compare:nNnT \l_regex_start_step_int = \l_regex_current_step_int } - } -\cs_new_protected_nopar:cpn { regex_build_$: } % $ - { - \regex_build_simple_assertion:Nn $ % $ - { \int_compare:nNnT \l_regex_current_char_int < \c_minus_one } + \regex_build_simple_assertion:nn { \regex_build_raw_error:N G } + { \int_compare:nNnT \l_regex_start_index_int = \l_regex_current_index_int } } +\group_begin: + \char_set_catcode_other:N \$ + \cs_new_protected_nopar:cpn { regex_build_$: } % $ + { + \regex_build_simple_assertion:nn { \regex_build_raw:N $ } % $ + { \int_compare:nNnT \l_regex_current_char_int < \c_minus_one } + } +\group_end: \cs_new_protected_nopar:cpn { regex_build_/Z: } { - \regex_build_simple_assertion:Nn Z + \regex_build_simple_assertion:nn { \regex_build_raw_error:N Z } { \int_compare:nNnT \l_regex_current_char_int < \c_minus_one } } \cs_new_protected_nopar:cpn { regex_build_/z: } { - \regex_build_simple_assertion:Nn z + \regex_build_simple_assertion:nn { \regex_build_raw_error:N z } { \int_compare:nNnT \l_regex_current_char_int < \c_minus_one } } \cs_new_protected_nopar:cpn { regex_build_/b: } { - \regex_build_simple_assertion:Nn b - { \regex_if_word_boundary:TF \use:n \use_none:n } + \regex_build_simple_assertion:nn { \regex_build_raw_error:N b } + { \regex_if_word_boundary:T } } \cs_new_protected_nopar:cpn { regex_build_/B: } { - \regex_build_simple_assertion:Nn B + \regex_build_simple_assertion:nn { \regex_build_raw_error:N B } { \regex_if_word_boundary:TF { } } } \cs_new_protected_nopar:Npn \regex_if_word_boundary:TF @@ -685,6 +839,8 @@ { \group_end: \c_regex_w_tl } \regex_break_point:TF } +\cs_new_protected_nopar:Npn \regex_if_word_boundary:T + { \regex_if_word_boundary:TF \use:n \use_none:n } \cs_new_protected_nopar:cpn { regex_build_[: } { \regex_build_if_in_class:TF @@ -696,21 +852,22 @@ \regex_build_if_in_class:TF { \if_num:w \l_regex_build_mode_int > \c_sixteen - \tl_set:Nx \l_regex_class_tl + \tl_set:Nx \l_regex_tests_tl { - \exp_not:o \l_regex_class_saved_tl - \if_num:w \l_regex_catcodes_int < \c_regex_catcodes_all_int - \regex_item_catcode:nT { \int_use:N \l_regex_catcodes_int } + \exp_not:o \l_regex_tests_saved_tl + \if_num:w \l_regex_catcodes_saved_int < \c_regex_catcodes_all_int + \regex_item_catcode:nT + { \int_use:N \l_regex_catcodes_saved_int } \else: \exp_after:wN \use:n \fi: { - \exp_not:o \l_regex_class_tl - \bool_if:NF \l_regex_class_bool - { \regex_break_point:TF { } { \regex_break_true:w } } + \exp_not:o \l_regex_tests_tl + \bool_if:NF \l_regex_tests_bool + { \regex_break_point:TF { } \regex_break_true:w } } } - \bool_set_eq:NN \l_regex_class_bool \l_regex_class_saved_bool + \bool_set_eq:NN \l_regex_tests_bool \l_regex_tests_saved_bool \fi: \tex_advance:D \l_regex_build_mode_int - \c_fifteen \tex_divide:D \l_regex_build_mode_int \c_thirteen @@ -724,14 +881,16 @@ { \l_regex_build_mode_int = \int_value:w \l_regex_build_mode_int 3 ~ % \if_num:w \l_regex_build_mode_int > \c_sixteen - \tl_set_eq:NN \l_regex_class_saved_tl \l_regex_class_tl - \bool_set_eq:NN \l_regex_class_saved_bool \l_regex_class_bool + \tl_set_eq:NN \l_regex_tests_saved_tl \l_regex_tests_tl + \bool_set_eq:NN \l_regex_tests_saved_bool \l_regex_tests_bool + \int_set_eq:NN \l_regex_catcodes_saved_int \l_regex_catcodes_int + \int_set_eq:NN \l_regex_catcodes_int \c_regex_catcodes_all_int \fi: \token_if_eq_meaning:NNTF #1 \regex_build_special:N { \token_if_eq_charcode:NNTF #2 ^ { - \bool_set_false:N \l_regex_class_bool % [ + \bool_set_false:N \l_regex_tests_bool % [ \str_if_eq:nnTF {#3#4} { \regex_build_special:N ] } { \regex_build_raw:N } } @@ -766,8 +925,7 @@ } { \cs_if_exist_use:cF { regex_build_c_#3: } } { - \msg_kernel_error:nnxx { regex } { c-command } - { regular~expression } { #3 } + \msg_kernel_error:nnx { regex } { c-missing-category } {#3} #2 #3 } } @@ -808,8 +966,7 @@ { \regex_build_c_lbrack_end: } } { - \msg_kernel_error:nnxx { regex } { c-command } - { regular-expression } { #2 } + \msg_kernel_error:nnx { regex } { c-missing-rbrack } {#2} \regex_build_c_lbrack_end: } } @@ -830,18 +987,21 @@ \fi: } \cs_new_protected:cpn { regex_build_ \c_rbrace_str : } - { %^^A incorrect recovery for \c{...(...[...} with missing ) or ]. + { \int_compare:nNnTF \l_regex_build_mode_int < \c_zero { - \use:c { regex_build_/Z: } - \regex_toks_put_right:Nx \l_regex_right_state_int - { \regex_action_success: } - \int_incr:N \l_regex_capturing_group_int + \regex_build_exit_class: + \regex_build_exit_groups: + \regex_build_escaped:N Z + \regex_build_end: \use:x { \group_end: \regex_build_one:n - { \regex_item_cs:n { \regex_set_aux:N ? } } + { + \regex_item_cs:n + { \regex_set_aux:N \l_regex_internal_regex } + } } } { \exp_after:wN \regex_build_raw:N \c_rbrace_str } @@ -913,7 +1073,7 @@ \tl_if_empty:nTF {#2} { \regex_build_quantifier_aux:nnNN { {#1} } { n* } } { \regex_build_quantifier_aux:nnNN { {#1} {#2} } { nn } } - } %^^A todo: catch the case m>n + } { \regex_build_quantifier_end:nn { } { } \use:x @@ -928,82 +1088,101 @@ \cs_new_protected:Npn \regex_build_quantifier_end:nn #1#2 { \use:c { regex_build_ \l_regex_one_or_group_tl _ #1 : } #2 - \tl_clear:N \l_regex_class_tl - \bool_set_true:N \l_regex_class_bool - \int_set_eq:NN \l_regex_catcodes_int \l_regex_catcodes_default_int + \tl_clear:N \l_regex_tests_tl + \bool_set_true:N \l_regex_tests_bool } \cs_new_protected_nopar:Npn \regex_build_one_quantifier: { \tl_set:Nx \l_regex_one_or_group_tl { one } \regex_build_quantifier:w } -\cs_new_protected_nopar:Npn \regex_build_one_: - { - \regex_build_transition:NN - \regex_build_tmp_class:n \l_regex_right_state_int - } -\cs_new_protected_nopar:cpn { regex_build_one_?: } - { - \regex_build_transitions:NNNN - \regex_build_tmp_class:n \l_regex_right_state_int - \regex_action_free:n \l_regex_right_state_int - } -\cs_new_protected_nopar:cpn { regex_build_one_??: } - { - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_right_state_int - \regex_build_tmp_class:n \l_regex_right_state_int - } -\cs_new_protected_nopar:cpn { regex_build_one_*: } - { - \regex_build_transitions:NNNN - \regex_build_tmp_class:n \l_regex_left_state_int - \regex_action_free:n \l_regex_right_state_int - } -\cs_new_protected_nopar:cpn { regex_build_one_*?: } +\cs_new_protected:Npn \regex_build_one_n: #1 { - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_right_state_int - \regex_build_tmp_class:n \l_regex_left_state_int + \prg_replicate:nn {#1} + { + \regex_build_transition:NN + \regex_tests_action_cost:n \l_regex_right_state_int + } } -\cs_new_protected_nopar:cpn { regex_build_one_+: } +\cs_new_eq:cN { regex_build_one_n?: } \regex_build_one_n: +\cs_new_protected_nopar:Npn \regex_build_one_: + { \regex_build_one_n: \c_one } +\cs_new_protected:cpn { regex_build_one_n*: } #1 { - \regex_build_one_: - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_tmpa_int - \regex_action_free:n \l_regex_right_state_int + \int_compare:nNnTF {#1} = \c_zero + { + \regex_build_transitions:NNNN + \regex_tests_action_cost:n \l_regex_left_state_int + \regex_action_free:n \l_regex_right_state_int + } + { + \regex_build_one_n: {#1} + \int_set_eq:NN \l_regex_internal_a_int \l_regex_left_state_int + \regex_build_transitions:NNNN + \regex_action_free:n \l_regex_internal_a_int + \regex_action_free:n \l_regex_right_state_int + } } -\cs_new_protected_nopar:cpn { regex_build_one_+?: } +\cs_new_protected:cpn { regex_build_one_n*?: } #1 { - \regex_build_one_: - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_right_state_int - \regex_action_free:n \l_regex_tmpa_int + \int_compare:nNnTF {#1} = \c_zero + { + \regex_build_transitions:NNNN + \regex_action_free:n \l_regex_right_state_int + \regex_tests_action_cost:n \l_regex_left_state_int + } + { + \regex_build_one_n: {#1} + \int_set_eq:NN \l_regex_internal_a_int \l_regex_left_state_int + \regex_build_transitions:NNNN + \regex_action_free:n \l_regex_right_state_int + \regex_action_free:n \l_regex_internal_a_int + } } -\cs_new_protected:Npn \regex_build_one_n: #1 - { \prg_replicate:nn {#1} { \regex_build_one_: } } -\cs_new_eq:cN { regex_build_one_n?: } \regex_build_one_n: -\cs_new_protected:cpx { regex_build_one_n*: } #1 - { - \exp_not:N \prg_replicate:nn {#1} { \exp_not:N \regex_build_one_: } - \exp_not:c { regex_build_one_*: } +\cs_new_protected_nopar:cpx { regex_build_one_*: } + { \exp_not:c { regex_build_one_n*: } \c_zero } +\cs_new_protected_nopar:cpx { regex_build_one_*?: } + { \exp_not:c { regex_build_one_n*?: } \c_zero } +\cs_new_protected_nopar:cpx { regex_build_one_+: } + { \exp_not:c { regex_build_one_n*: } \c_one } +\cs_new_protected_nopar:cpx { regex_build_one_+?: } + { \exp_not:c { regex_build_one_n*?: } \c_one } +\cs_new_protected_nopar:Npn \regex_build_one_nn: + { + \regex_build_one_nn_aux:nnnn { } + { + \regex_build_transitions:NNNN + \regex_tests_action_cost:n \l_regex_right_state_int + \regex_action_free:n \l_regex_internal_a_int + } } -\cs_new_protected:cpx { regex_build_one_n*?: } #1 +\cs_new_protected_nopar:cpn { regex_build_one_nn?: } { - \exp_not:N \prg_replicate:nn {#1} { \exp_not:N \regex_build_one_: } - \exp_not:c { regex_build_one_*?: } + \regex_build_one_nn_aux:nnnn {?} + { + \regex_build_transitions:NNNN + \regex_action_free:n \l_regex_internal_a_int + \regex_tests_action_cost:n \l_regex_right_state_int + } } -\cs_new_protected:Npn \regex_build_one_nn_aux:Nnn #1#2#3 +\cs_new_protected:Npn \regex_build_one_nn_aux:nnnn #1#2#3#4 { - \prg_replicate:nn {#2} { \regex_build_one_: } - \prg_replicate:nn {#3-#2} {#1} + \regex_build_one_n: {#3} + \int_compare:nNnTF {#3} > {#4} + { + \msg_kernel_error:nnxxx + { regex } { quantifier-backwards } {#3} {#4} {#1} + } + { + \int_set:Nn \l_regex_internal_a_int + { \l_regex_right_state_int + #4 - #3 } + \prg_replicate:nn { #4 - #3 } { #2 } + } } -\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_one_?: } + { \regex_build_one_nn: \c_zero \c_one } +\cs_new_protected_nopar:cpx { regex_build_one_??: } + { \exp_not:c { regex_build_one_nn?: } \c_zero \c_one } \cs_new_protected:cpn { regex_build_(: } #1#2 { \regex_build_if_in_class:TF @@ -1060,7 +1239,7 @@ \regex_seq_pop_int:NN \l_regex_left_state_seq \l_regex_left_state_int \regex_seq_pop_int:NN \l_regex_right_state_seq \l_regex_right_state_int \regex_seq_pop_use:N \l_regex_end_group_seq - \seq_pop:NN \l_regex_end_alternation_seq \l_regex_tmpa_tl + \seq_pop:NN \l_regex_end_alternation_seq \l_regex_internal_a_tl } \cs_new_protected_nopar:Npn \regex_build_begin_alternation: { @@ -1093,7 +1272,7 @@ { \cs_if_exist_use:cF { regex_build_special_group_\token_to_str:N #2 : } { - \msg_kernel_error:nnx { regex } { unsupported } + \msg_kernel_error:nnx { regex } { special-group-unknown } { (? \token_to_str:N #2 } %) \regex_build_special:N ( % ) \regex_build_raw:N ? @@ -1149,7 +1328,7 @@ \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 } } + { \msg_kernel_error:nnx { regex } { unknown-option } { #3 } } \regex_build_options:NNN #1 } { @@ -1158,7 +1337,7 @@ { ) } { } { - } { \regex_build_options:NNN - } } - { \msg_error:nnx { regex } { invalid-in-option } { #3 } } + { \msg_kernel_error:nnx { regex } { invalid-in-option } { #3 } } } } \cs_new_protected_nopar:cpn { regex_build_option_+i: } @@ -1176,53 +1355,52 @@ } \cs_new_protected:Npn \regex_build_group_submatches:NN #1#2 { - \seq_pop:NN \l_regex_capturing_group_seq \l_regex_tmpa_tl - \int_compare:nNnF { \l_regex_tmpa_tl } < \c_zero + \seq_pop:NN \l_regex_capturing_group_seq \l_regex_internal_a_tl + \int_compare:nNnF { \l_regex_internal_a_tl } < \c_zero { \regex_toks_put_left:Nx #1 - { \regex_action_submatch:n { \l_regex_tmpa_tl < } } + { \regex_action_submatch:n { \l_regex_internal_a_tl < } } \regex_toks_put_left:Nx #2 - { \regex_action_submatch:n { \l_regex_tmpa_tl > } } + { \regex_action_submatch:n { \l_regex_internal_a_tl > } } } } -\cs_new_protected_nopar:Npn \regex_build_group_: - { - \regex_build_group_submatches:NN - \l_regex_left_state_int \l_regex_right_state_int - \regex_build_transition:NN - \regex_action_free:n \l_regex_right_state_int - } -\cs_new_protected:Npn \regex_build_group_shift:N #1 +\cs_new_protected_nopar:Npn \regex_build_group_qs_aux: { - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int + \int_set_eq:NN \l_regex_internal_a_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 + \tex_toks:D \l_regex_right_state_int = \tex_toks:D \l_regex_internal_a_int \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! + \int_sub:Nn \l_regex_current_state_int + { + \int_eval:n % ^^A here we lie! + { \l_regex_right_state_int - \l_regex_internal_a_int } + } } - \cs_set:Npx \regex_tmp:w + \use:x { - \tex_toks:D \l_regex_tmpa_int + \tex_toks:D \l_regex_internal_a_int { \s_stop - #1 { \int_eval:n { \l_regex_right_state_int - \l_regex_tmpa_int } } + \regex_action_free:n + { + \int_eval:n + { \l_regex_right_state_int - \l_regex_internal_a_int } + } } } - \regex_tmp:w \regex_build_group_submatches:NN \l_regex_right_state_int \l_regex_left_state_int } \cs_new_protected:Npn \regex_build_group_qs_aux:NN #1#2 { - \regex_build_group_shift:N \regex_action_free:n + \regex_build_group_qs_aux: \int_set_eq:NN \l_regex_right_state_int \l_regex_left_state_int \regex_build_transition:NN \regex_action_free:n #1 - #2 \l_regex_tmpa_int + #2 \l_regex_internal_a_int { \regex_action_free:n - { \int_eval:n { \l_regex_right_state_int - \l_regex_tmpa_int } } + { \int_eval:n { \l_regex_right_state_int - \l_regex_internal_a_int } } } } \cs_new_protected_nopar:cpn { regex_build_group_?: } @@ -1235,99 +1413,241 @@ \regex_build_group_qs_aux:NN \l_regex_right_state_int \regex_toks_put_left:Nx } -\cs_new_protected_nopar:cpn { regex_build_group_*: } +\cs_new_protected:Npn \regex_build_group_transition_right_max: { - \regex_build_group_qs_aux:NN - \l_regex_tmpa_int \regex_toks_put_right:Nx + \regex_toks_put_right:Nx \l_regex_right_state_int + { + \regex_action_free:n + { \int_eval:n { \l_regex_max_state_int - \l_regex_right_state_int } } + } } -\cs_new_protected_nopar:cpn { regex_build_group_*?: } +\cs_new_protected:Npn \regex_build_group_n_aux:n #1 { - \regex_build_group_qs_aux:NN - \l_regex_tmpa_int \regex_toks_put_left:Nx + \int_set_eq:NN \l_regex_internal_a_int \l_regex_left_state_int + \int_set_eq:NN \l_regex_internal_b_int \l_regex_max_state_int + \int_add:Nn \l_regex_right_state_int + { ( #1 - 1 ) * ( \l_regex_max_state_int - \l_regex_left_state_int ) } + \int_set:Nn \l_regex_max_state_int + { + \l_regex_left_state_int + + (#1) * ( \l_regex_max_state_int - \l_regex_left_state_int ) + } + \int_while_do:nNnn \l_regex_internal_b_int < \l_regex_max_state_int + { + \tex_toks:D \l_regex_internal_b_int + = \tex_toks:D \l_regex_internal_a_int + \int_incr:N \l_regex_internal_a_int + \int_incr:N \l_regex_internal_b_int + } + } +\cs_new_protected:Npn \regex_build_group_n: #1 + { + \regex_build_group_transition_right_max: + \regex_build_group_n_aux:n {#1} + \regex_build_new_state: + \int_compare:nNnF {#1} = \c_zero + { + \regex_build_group_submatches:NN + \l_regex_internal_a_int \l_regex_left_state_int + } } -\cs_new_protected_nopar:cpn { regex_build_group_+: } +\cs_new_eq:cN { regex_build_group_n?: } \regex_build_group_n: +\cs_new_protected_nopar:Npn \regex_build_group_: + { \regex_build_group_n: \c_one } +\cs_new_protected_nopar:cpn { regex_build_group_n*: } { - \regex_build_group_submatches:NN - \l_regex_left_state_int \l_regex_right_state_int - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_tmpa_int - \regex_action_free:n \l_regex_right_state_int + \regex_build_group_n_star_aux:NNn + \regex_toks_put_right:Nx \regex_toks_put_left:Nx } -\cs_new_protected_nopar:cpn { regex_build_group_+?: } +\cs_new_protected_nopar:cpn { regex_build_group_n*?: } { - \regex_build_group_submatches:NN - \l_regex_left_state_int \l_regex_right_state_int - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_right_state_int - \regex_action_free:n \l_regex_tmpa_int + \regex_build_group_n_star_aux:NNn + \regex_toks_put_left:Nx \regex_toks_put_right:Nx } -\cs_new_protected:Npn \regex_build_group_n_aux:n #1 +\cs_new_protected:Npn \regex_build_group_n_star_aux:NNn #1#2#3 { - \regex_toks_put_right:Nx \l_regex_right_state_int + \int_compare:nNnTF {#3} = \c_zero + { \regex_build_group_qs_aux:NN \l_regex_internal_a_int #1 } { - \regex_action_free:n + \regex_build_group_transition_right_max: + \regex_build_group_n_aux:n {#3} + \regex_build_new_state: + #2 \l_regex_left_state_int { - \int_eval:n %^^A todo: document why that value. - { \l_regex_max_state_int - \c_one - \l_regex_left_state_int } + \regex_action_free:n + { + \int_eval:n + { \l_regex_internal_a_int - \l_regex_left_state_int } + } } + \regex_build_group_submatches:NN + \l_regex_internal_a_int \l_regex_left_state_int } - \int_set_eq:NN \l_regex_tmpa_int \l_regex_left_state_int - \int_set_eq:NN \l_regex_tmpb_int \l_regex_max_state_int - \int_set:Nn \l_regex_max_state_int + } +\cs_new_protected_nopar:cpx { regex_build_group_*: } + { \exp_not:c { regex_build_group_n*: } \c_zero } +\cs_new_protected_nopar:cpx { regex_build_group_*?: } + { \exp_not:c { regex_build_group_n*?: } \c_zero } +\cs_new_protected_nopar:cpx { regex_build_group_+: } + { \exp_not:c { regex_build_group_n*: } \c_one } +\cs_new_protected_nopar:cpx { regex_build_group_+?: } + { \exp_not:c { regex_build_group_n*?: } \c_one } +\cs_new_protected_nopar:Npn \regex_build_group_nn: + { + \regex_build_group_nn_aux:nnnn { } + \regex_build_group_transition_right_max: + } +\cs_new_protected_nopar:cpn { regex_build_group_nn?: } + { + \regex_build_group_nn_aux:nnnn { } { - \l_regex_left_state_int - + #1 * ( \l_regex_max_state_int - \l_regex_left_state_int ) + \use:x + { + \tex_toks:D \l_regex_right_state_int + { + \exp_after:wN \regex_build_group_nn_aux_ii:w + \tex_the:D \tex_toks:D \l_regex_right_state_int + } + } } - \int_while_do:nNnn \l_regex_tmpb_int < \l_regex_max_state_int + } +\cs_new_protected:Npn \regex_build_group_nn_aux:nnnn #1#2#3#4 + { + \int_compare:nNnTF {#3} > {#4} { - \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 + \msg_kernel_error:nnxxx + { regex } { quantifier-backwards } {#3} {#4} {#1} + } + { + \int_compare:nNnTF {#3} = \c_zero + { + \regex_build_group_qs_aux: + \int_set_eq:NN \l_regex_right_state_int \l_regex_left_state_int + \int_set_eq:NN \l_regex_left_state_int \l_regex_internal_a_int + \regex_build_group_transition_right_max: + \regex_build_group_n_aux:n { #4 - #3 } + \int_set_eq:NN \l_regex_internal_c_int \l_regex_right_state_int + \int_set_eq:NN \l_regex_right_state_int \l_regex_left_state_int + \prg_replicate:nn { #4 - #3 } + { + #2 + \int_add:Nn \l_regex_right_state_int + { \l_regex_internal_b_int - \l_regex_internal_a_int } + } + \int_set_eq:NN \l_regex_right_state_int \l_regex_internal_c_int + \int_set_eq:NN \l_regex_left_state_int \l_regex_internal_a_int + \regex_build_new_state: + } + { + \regex_build_group_transition_right_max: + \regex_build_group_n_aux:n {#3} + \int_set_eq:NN \l_regex_left_state_int \l_regex_internal_a_int + \regex_build_group_submatches:NN + \l_regex_left_state_int \l_regex_right_state_int + \regex_build_group_n_aux:n { #4 - #3 + \c_one } + \int_sub:Nn \l_regex_right_state_int + { \l_regex_internal_a_int - \l_regex_left_state_int } + \prg_replicate:nn { #4 - #3 } + { + #2 + \int_add:Nn \l_regex_right_state_int + { \l_regex_internal_b_int - \l_regex_internal_a_int } + } + \int_set_eq:NN \l_regex_left_state_int \l_regex_internal_a_int + \regex_build_new_state: + } } } -\cs_new_protected:Npn \regex_build_group_n: #1 - { % ^^A todo: catch case #1 <= 0. - \regex_build_group_n_aux:n {#1} - \regex_build_transition: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_protected:Npn \regex_build_group_nn_aux_ii:w #1 \regex_action_free:n + { + #1 + \regex_action_free:n + { + \int_eval:n + { \l_regex_max_state_int - \l_regex_right_state_int } + } + \regex_action_free:n } -\cs_new_eq:cN { regex_build_group_n?: } \regex_build_group_n: -\cs_new_protected:cpn { regex_build_group_n*: } #1 - { % ^^A todo: catch case #1 <= 0. - \regex_build_group_n_aux:n {#1} - \regex_build_transitions:NNNN - \regex_action_free:n \l_regex_tmpa_int - \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:cpn { regex_build_/u: } #1#2 + { + \regex_build_if_assertions_forbidden:TF + { \regex_build_raw_error:N u #1#2 } + { + \str_if_eq:xxTF {#1#2} { \regex_build_special:N \c_lbrace_str } + { + \tl_set:Nx \l_regex_internal_a_tl { \if_false: } \fi: + \regex_build_u_loop:NN + } + { + \msg_kernel_error:nn { regex } { u-missing-lbrace } + #1#2 + } + } } -\cs_new_protected:cpn { regex_build_group_n*?: } #1 - { % ^^A todo: catch case #1 <= 0. - \regex_build_group_n_aux:n {#1} - \regex_build_transitions: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:Npn \regex_build_u_loop:NN #1#2 + { + \token_if_eq_meaning:NNTF #1 \regex_build_raw:N + { #2 \regex_build_u_loop:NN } + { + \token_if_eq_meaning:NNTF #1 \regex_build_special:N + { + \exp_after:wN \token_if_eq_charcode:NNTF \c_rbrace_str #2 + { \if_false: { \fi: } \regex_build_u_end: } + { #2 \regex_build_u_loop:NN } + } + { + \if_false: { \fi: } + \msg_kernel_error:nnx { regex } { u-missing-rbrace } {#2} + \regex_build_u_end: + #1#2 + } + } + } +\cs_new_protected:Npn \regex_build_u_end: + { + \tl_set:Nv \l_regex_internal_a_tl \l_regex_internal_a_tl + \if_num:w \l_regex_build_mode_int = \c_zero + \regex_build_u_not_cs: + \else: + \regex_build_u_in_cs: + \fi: + \tl_clear:N \l_regex_tests_tl } -\cs_new_protected: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:Npn \regex_build_u_in_cs: + { + \exp_args:NNo \str_gset_other:Nn \g_regex_internal_tl + { \l_regex_internal_a_tl } + \tl_map_inline:Nn \g_regex_internal_tl + { + \tl_set:Nx \l_regex_tests_tl { \regex_item_equal:n { `##1 } } + \regex_build_one_: + } } -\cs_new_protected: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_build_u_not_cs: + { + \exp_args:No \tl_analysis_map_inline:nn { \l_regex_internal_a_tl } + { + \tl_set:Nx \l_regex_tests_tl + { + \exp_not:N \int_compare:nNnT + \l_regex_current_catcode_int = { \int_value:w "##2 } + { + \int_compare:nNnTF { "##2 } = \c_zero + { + \exp_not:n + { \str_if_eq:xxT { \l_regex_current_token_tl } {##1} } + { \regex_break_true:w } + } + { \regex_item_equal:n {##3} } + } + } + \regex_build_one_: + } } \cs_new_protected:Npn \regex_store_state:n #1 { - \int_incr:N \l_regex_max_index_int - \tex_skip:D \l_regex_max_index_int #1 sp \scan_stop: + \int_incr:N \l_regex_max_active_int + \tex_skip:D \l_regex_max_active_int #1 sp \scan_stop: \regex_store_submatches:n {#1} } \cs_new_protected_nopar:Npn \regex_state_use_with_submatches: @@ -1336,10 +1656,9 @@ { \regex_state_use_aux:n { \exp_after:wN \use_none_delimit_by_s_stop:w } } \cs_new_protected:Npn \regex_state_use_aux:n #1 { - \if_num:w \tex_dimen:D \l_regex_current_state_int - < \l_regex_unique_id_int + \if_num:w \tex_dimen:D \l_regex_current_state_int < \l_regex_step_int \tex_dimen:D \l_regex_current_state_int - = \l_regex_unique_id_int sp \scan_stop: + = \l_regex_step_int sp \scan_stop: #1 \tex_the:D \tex_toks:D \exp_after:wN \l_regex_current_state_int \fi: \scan_stop: @@ -1374,68 +1693,55 @@ { \exp_after:wN \c_zero \exp_after:wN \regex_state_submatches:nn \exp_after:wN - { - \int_value:w \int_eval:w - \l_regex_unique_id_int + \c_one - \exp_after:wN - } + { \int_value:w \int_eval:w \l_regex_step_int + \c_one \exp_after:wN } \exp_after:wN { \l_regex_current_submatches_prop } \regex_state_submatches:nn {#1} {#2} \s_stop } \cs_new_protected:Npn \regex_state_submatches:nn #1#2 { - \if_num:w #1 = \l_regex_unique_id_int + \if_num:w #1 = \l_regex_step_int \tl_set:Nn \l_regex_current_submatches_prop { #2 } \fi: } \cs_new_protected:Npn \regex_match:n #1 { - \tl_set_analysis:Nn \l_regex_tmpa_tl {#1} - \int_zero:N \l_regex_nesting_int - \int_set_eq:NN \l_regex_current_step_int \l_regex_max_state_int + \int_set_eq:NN \l_regex_current_index_int \l_regex_max_state_int \regex_query_set:nnn { } { -1 } { -2 } - \int_set_eq:NN \l_regex_min_step_int \l_regex_current_step_int - \exp_after:wN \regex_query_set_loop:ww - \l_regex_tmpa_tl - \s_tl { ? \prg_map_break: } \s_tl - \prg_break_point:n { } - \int_set_eq:NN \l_regex_max_step_int \l_regex_current_step_int + \int_set_eq:NN \l_regex_min_index_int \l_regex_current_index_int + \tl_analysis_map_inline:nn {#1} + { + \regex_query_set:nnn {##1} {"##2} {##3} + \if_case:w "##2 \exp_stop_f: + \or: \int_incr:N \l_regex_nesting_int + \or: \int_decr:N \l_regex_nesting_int + \fi: + } + \int_set_eq:NN \l_regex_max_index_int \l_regex_current_index_int \regex_query_set:nnn { } { -1 } { -2 } - \regex_match_initial_setup: \regex_match_once: } -\cs_new:Npn \regex_query_set_loop:ww #1 \s_tl #2#3 \s_tl - { - \use_none:n #2 - \regex_query_set:nnn {#1} {"#2} {#3} - \if_case:w "#2 \exp_stop_f: - \or: \int_incr:N \l_regex_nesting_int - \or: \int_decr:N \l_regex_nesting_int - \fi: - \regex_query_set_loop:ww - } \cs_new_protected:Npn \regex_query_set:nnn #1#2#3 { - \tex_muskip:D \l_regex_current_step_int + \tex_muskip:D \l_regex_current_index_int = \etex_gluetomu:D #3 sp plus #2 sp minus \l_regex_nesting_int sp \scan_stop: - \tex_toks:D \l_regex_current_step_int {#1} - \int_incr:N \l_regex_current_step_int + \tex_toks:D \l_regex_current_index_int {#1} + \int_incr:N \l_regex_current_index_int } \cs_new_protected_nopar:Npn \regex_query_get: { \tl_set:Nx \l_regex_current_token_tl - { \tex_the:D \tex_toks:D \l_regex_current_step_int } + { \tex_the:D \tex_toks:D \l_regex_current_index_int } \l_regex_current_char_int - = \etex_mutoglue:D \tex_muskip:D \l_regex_current_step_int + = \etex_mutoglue:D \tex_muskip:D \l_regex_current_index_int \l_regex_current_catcode_int = \etex_gluestretch:D - \etex_mutoglue:D \tex_muskip:D \l_regex_current_step_int + \etex_mutoglue:D \tex_muskip:D \l_regex_current_index_int } \cs_new_protected_nopar:Npn \regex_match_once: { @@ -1451,13 +1757,14 @@ } \cs_new_protected_nopar:Npn \regex_match_initial_setup: { - \prg_stepwise_inline:nnnn {0} {1} { \l_regex_max_state_int - \c_one } - { \tex_dimen:D ##1 \c_minus_one sp \scan_stop: } - \int_set_eq:NN \l_regex_unique_id_int \c_minus_one - \int_set:Nn \l_regex_start_step_int - { \l_regex_min_step_int - \c_one } - \int_set_eq:NN \l_regex_current_step_int \l_regex_min_step_int - \int_set_eq:NN \l_regex_success_step_int \l_regex_min_step_int + \int_set_eq:NN \l_regex_step_int \c_minus_one + \prg_stepwise_inline:nnnn + \c_zero \c_one { \l_regex_max_state_int - \c_one } + { \tex_dimen:D ##1 \l_regex_step_int sp \scan_stop: } + \int_set:Nn \l_regex_start_index_int + { \l_regex_min_index_int - \c_one } + \int_set_eq:NN \l_regex_current_index_int \l_regex_min_index_int + \int_set_eq:NN \l_regex_success_index_int \l_regex_min_index_int \int_set_eq:NN \l_regex_submatch_int \l_regex_max_state_int \bool_set_false:N \l_regex_success_empty_bool \bool_gset_false:N \g_regex_success_bool @@ -1466,49 +1773,48 @@ { \prop_clear:N \l_regex_current_submatches_prop \bool_if:NTF \l_regex_success_empty_bool - { \cs_set_eq:NN \regex_last_match_empty:F \regex_last_match_empty_yes:F } - { \cs_set_eq:NN \regex_last_match_empty:F \regex_last_match_empty_no:F } - \int_set_eq:NN \l_regex_start_step_int \l_regex_success_step_int - \int_set:Nn \l_regex_current_step_int - { \l_regex_start_step_int - \c_one } + { \cs_set_eq:NN \regex_if_two_empty_matches:F \regex_if_match_empty:F } + { \cs_set_eq:NN \regex_if_two_empty_matches:F \use:n } + \int_set_eq:NN \l_regex_start_index_int \l_regex_success_index_int + \int_set:Nn \l_regex_current_index_int + { \l_regex_start_index_int - \c_one } \bool_set_false:N \l_regex_success_match_bool - \int_zero:N \l_regex_max_index_int + \int_zero:N \l_regex_max_active_int \regex_store_state:n {0} %^^A _state_int! } \cs_new_protected_nopar:Npn \regex_match_loop: { - \int_incr:N \l_regex_current_step_int - \int_incr:N \l_regex_unique_id_int + \int_incr:N \l_regex_current_index_int + \int_incr:N \l_regex_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 \regex_query_get: \regex_match_loop_case_hook: - \cs_set_nopar:Npx \regex_tmp:w + \use:x { - \int_zero:N \l_regex_max_index_int - \regex_match_one_index:w 1 ; \prg_break_point:n { } + \int_zero:N \l_regex_max_active_int + \regex_match_one_active:w 1 ; \prg_break_point:n { } \exp_not:N \prg_break_point:n { } } - \regex_tmp:w \if_num:w \l_regex_current_char_int < \c_minus_one \exp_after:wN \prg_map_break: \fi: - \if_num:w \l_regex_max_index_int = \c_zero + \if_num:w \l_regex_max_active_int = \c_zero \exp_after:wN \prg_map_break: \fi: \regex_match_loop: } -\cs_new:Npn \regex_match_one_index:w #1; +\cs_new:Npn \regex_match_one_active:w #1; { - \if_num:w #1 > \l_regex_max_index_int + \if_num:w #1 > \l_regex_max_active_int \exp_after:wN \prg_map_break: \fi: - \regex_match_one_index_aux:n + \regex_match_one_active_aux:n { \int_value:w \tex_skip:D #1 } - \exp_after:wN \regex_match_one_index:w + \exp_after:wN \regex_match_one_active:w \int_use:N \int_eval:w #1 + \c_one ; } -\cs_new_protected:Npn \regex_match_one_index_aux:n #1 +\cs_new_protected:Npn \regex_match_one_active_aux:n #1 { \int_set:Nn \l_regex_current_state_int {#1} \prop_clear:N \l_regex_current_submatches_prop @@ -1546,12 +1852,12 @@ } \cs_new_protected_nopar:Npn \regex_action_success: { - \regex_last_match_empty:F + \regex_if_two_empty_matches:F { \bool_set_true:N \l_regex_success_match_bool \bool_set_eq:NN \l_regex_success_empty_bool \l_regex_fresh_thread_bool - \int_set_eq:NN \l_regex_success_step_int \l_regex_current_step_int + \int_set_eq:NN \l_regex_success_index_int \l_regex_current_index_int \prop_set_eq:NN \l_regex_success_submatches_prop \l_regex_current_submatches_prop \prg_map_break: @@ -1559,7 +1865,7 @@ } \cs_new_protected:Npn \regex_action_free:n #1 { - \cs_set_nopar:Npx \regex_tmp:w + \use:x { \int_add:Nn \l_regex_current_state_int {#1} \regex_state_use: @@ -1568,13 +1874,16 @@ \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:Npn \regex_action_submatch:n #1 { \prop_put:Nno \l_regex_current_submatches_prop {#1} - { \int_use:N \l_regex_current_step_int } + { \int_use:N \l_regex_current_index_int } } +\cs_new:Npn \regex_if_match_empty:T + { \int_compare:nNnT \l_regex_start_index_int = \l_regex_current_index_int } +\cs_new:Npn \regex_if_match_empty:F + { \int_compare:nNnF \l_regex_start_index_int = \l_regex_current_index_int } \cs_new_protected:Npn \regex_submatch_nesting_aux:n #1 { + \etex_glueshrink:D \etex_mutoglue:D \etex_muexpr:D @@ -1589,10 +1898,10 @@ \int_zero:N \l_regex_replacement_int \int_zero:N \l_regex_nesting_int \tl_clear:N \l_regex_nesting_tl - \str_escape_use:NNNn - \regex_replacement_unescaped:N - \regex_replacement_escaped:N - \regex_replacement_raw:N + \regex_escape_use:nnnn + { \regex_replacement_unescaped:N ##1 } + { \regex_replacement_escaped:N ##1 } + { \regex_replacement_raw:N ##1 } {#1} \prg_do_nothing: \prg_do_nothing: \cs_set:Npx \regex_nesting:n ##1 @@ -1604,9 +1913,9 @@ \use:x { \exp_not:n { \cs_set:Npn \regex_replacement_tl:n ##1 } - { \str_aux_toks_range:nn \c_zero \l_regex_replacement_int } + { \regex_toks_range:nn \c_zero \l_regex_replacement_int } } - % ^^A rename! Careful with \cP\#. + % ^^A rename! } \cs_new_protected:Npn \regex_replacement_raw:N #1 { \regex_replacement_put:n {#1} } @@ -1629,19 +1938,14 @@ } \cs_new_protected:Npn \regex_replacement_escaped:N #1 { - \if_charcode:w c #1 - \exp_after:wN \regex_replacement_c:w - \else: - \if_charcode:w g #1 - \exp_after:wN \exp_after:wN \exp_after:wN \regex_replacement_g:w - \else: + \cs_if_exist_use:cF { regex_replacement_#1:w } + { \if_num:w \c_one < 1#1 \exp_stop_f: \regex_replacement_put_submatch:n {#1} \else: \regex_replacement_put:n #1 \fi: - \fi: - \fi: + } } \cs_new_protected:Npn \regex_replacement_put_submatch:n #1 { @@ -1668,7 +1972,7 @@ { \exp_not:n { #1#2 } } { \regex_replacement_unescaped:N \c_lbrace_str } { - \int_zero:N \l_regex_tmpa_int + \int_zero:N \l_regex_internal_a_int \regex_replacement_g_digits:NN } { \regex_replacement_error:NNN g #1 #2 } @@ -1678,13 +1982,13 @@ \token_if_eq_meaning:NNTF #1 \regex_replacement_unescaped:N { \if_num:w \c_one < 1#2 \exp_stop_f: - \int_set:Nn \l_regex_tmpa_int { \c_ten * \l_regex_tmpa_int + #2 } + \int_set:Nn \l_regex_internal_a_int { \c_ten * \l_regex_internal_a_int + #2 } \exp_after:wN \use_i:nnn \exp_after:wN \regex_replacement_g_digits:NN \else: \if_charcode:w \c_rbrace_str #2 \exp_args:No \regex_replacement_put_submatch:n - { \int_use:N \l_regex_tmpa_int } + { \int_use:N \l_regex_internal_a_int } \exp_after:wN \exp_after:wN \exp_after:wN \use_none:nn \else: \exp_after:wN \exp_after:wN @@ -1696,6 +2000,16 @@ { \regex_replacement_error:NNN g } #1 #2 } +\cs_new_protected:Npn \regex_replacement_u:w #1#2 + { + \str_if_eq:xxTF { #1#2 } { \regex_replacement_unescaped:N \c_lbrace_str } + { + \int_incr:N \l_regex_replacement_csnames_int + \regex_replacement_put:n + { \exp_not:n { \exp_after:wN \exp_not:V \cs:w } } + } + { \regex_replacement_error:NNN u #1#2 } + } \cs_new_protected:Npn \regex_replacement_c:w #1#2 { \token_if_eq_meaning:NNTF #1 \regex_replacement_unescaped:N @@ -1711,8 +2025,6 @@ \regex_replacement_put:n { \exp_not:n { \exp_after:wN \regex_replacement_exp_not:N \cs:w } } } -\cs_new_eq:cc { regex_replacement_c_C:w } - { regex_replacement_c_ \c_lbrace_str :w } \cs_new:Npn \regex_replacement_exp_not:N #1 { \exp_not:n {#1} } \group_begin: @@ -1790,11 +2102,11 @@ { \group_begin: \regex_build:n {#3} - \cs_set_nopar:Npx \regex_tmp:w - { #1 \exp_not:N #2 { \regex_set_aux:N #2 } } - \exp_after:wN - \group_end: - \regex_tmp:w + \use:x + { + \group_end: + #1 \exp_not:N #2 { \regex_set_aux:N #2 } + } } \cs_new:Npn \regex_set_aux:N #1 { @@ -1811,7 +2123,7 @@ \regex_match_loop_caseless_hook: } \prg_stepwise_function:nnnN - {0} {1} {\l_regex_max_state_int - \c_one } + \c_zero \c_one {\l_regex_max_state_int - \c_one } \regex_set_aux:n \s_stop } @@ -1824,18 +2136,6 @@ } \cs_new_protected_nopar:Npn \regex_use:N { \exp_last_unbraced:No \use_none:nn } -\cs_new:Npn \regex_to_str:N #1 - { - \exp_after:wN \regex_to_str_aux:Nw #1 - \tex_toks:D \q_stop \prg_map_break: \tex_toks:D - \prg_break_point:n { } - } -\cs_new:Npn \regex_to_str_aux:Nw #1#2\tex_toks:D - { - \use_none_delimit_by_q_stop:w #2 \q_stop - \tl_to_str:n {#1#2} \iow_newline: - \regex_to_str_aux:Nw \tex_toks:D - } \cs_new_protected_nopar:Npn \regex_aux_return: { \if_meaning:w \c_true_bool \g_regex_success_bool @@ -1921,9 +2221,9 @@ \cs_new_protected:Npn \regex_group_end_extract_seq:N #1 { \cs_set_eq:NN \seq_item:n \scan_stop: - \flag_lower:N \l_regex_group_begin_flag - \flag_lower:N \l_regex_group_end_flag - \tl_set:Nx \l_regex_tmpa_tl + \flag_clear:n { regex_begin } + \flag_clear:n { regex_end } + \tl_set:Nx \l_regex_internal_a_tl { \prg_stepwise_function:nnnN \l_regex_max_state_int @@ -1931,12 +2231,13 @@ { \l_regex_submatch_int - \c_one } \regex_extract_seq_aux:n } - \flag_test:NF \l_regex_group_begin_flag - { \flag_test:NF \l_regex_group_end_flag { \use_none:nnn } } - \msg_kernel_error:nn { regex } { sequence-unbalanced } - \tl_set:Nx \l_regex_tmpa_tl { \l_regex_tmpa_tl } + \int_compare:nNnF + { \flag_height:n { regex_begin } + \flag_height:n { regex_end } } + = \c_zero + { \msg_kernel_error:nn { regex } { sequence-unbalanced } } + \tl_set:Nx \l_regex_internal_a_tl { \l_regex_internal_a_tl } \exp_args:NNNo \group_end: - \tl_set:Nn #1 \l_regex_tmpa_tl + \tl_set:Nn #1 \l_regex_internal_a_tl } \cs_new:Npn \regex_extract_seq_aux:n #1 { @@ -1947,14 +2248,14 @@ } } \cs_new:Npn \regex_extract_seq_aux:ww #1; #2; - { %^^A todo: use flags + { \if_num:w #1 < \c_zero - \flag_raise:N \l_regex_group_end_flag + \flag_raise:n { regex_end } \prg_replicate:nn {-#1} { \exp_not:n { { \if_false: } \fi: } } \fi: \regex_query_submatch:w #2; \if_num:w #1 > \c_zero - \flag_raise:N \l_regex_group_begin_flag + \flag_raise:n { regex_begin } \prg_replicate:nn {#1} { \exp_not:n { \if_false: { \fi: } } } \fi: } @@ -2016,20 +2317,20 @@ \group_begin: \tl_set:Nn \l_regex_every_match_tl { - \if_num:w \l_regex_start_step_int < \l_regex_success_step_int + \if_num:w \l_regex_start_index_int < \l_regex_success_index_int \regex_extract: \tex_skip:D \l_regex_submatch_start_int - = \l_regex_start_step_int sp + = \l_regex_start_index_int sp plus \tex_skip:D \l_regex_submatch_start_int \scan_stop: \fi: \regex_match_once: } #1 {#2} {#3} \tex_skip:D \l_regex_submatch_int - = \l_regex_start_step_int sp - plus \l_regex_current_step_int sp \scan_stop: + = \l_regex_start_index_int sp + plus \l_regex_current_index_int sp \scan_stop: \int_incr:N \l_regex_submatch_int - \if_num:w \l_regex_start_step_int = \l_regex_current_step_int + \if_num:w \l_regex_start_index_int = \l_regex_current_index_int \if_meaning:w \c_true_bool \l_regex_success_empty_bool \int_decr:N \l_regex_submatch_int \fi: @@ -2044,34 +2345,34 @@ \exp_args:Nno #1 {#2} #4 \if_meaning:w \c_true_bool \g_regex_success_bool \regex_extract: - \int_set:Nn \l_regex_tmpa_int + \int_set:Nn \l_regex_internal_a_int { \regex_nesting:n { \l_regex_submatch_start_int } } - \if_num:w \l_regex_tmpa_int = \c_zero + \if_num:w \l_regex_internal_a_int = \c_zero \else: \msg_kernel_error:nnx { regex } { replace-unbalanced } - { \l_regex_tmpa_int } + { \l_regex_internal_a_int } \fi: - \tl_set:Nx \l_regex_tmpa_tl + \tl_set:Nx \l_regex_internal_a_tl { - \if_num:w \l_regex_tmpa_int < \c_zero - \prg_replicate:nn { - \l_regex_tmpa_int } + \if_num:w \l_regex_internal_a_int < \c_zero + \prg_replicate:nn { - \l_regex_internal_a_int } { \exp_not:n { { \if_false: } \fi: } } \fi: - \regex_query_substr:nn - { \l_regex_min_step_int } + \regex_toks_range:nn + { \l_regex_min_index_int } { \tex_skip:D \l_regex_submatch_start_int } \regex_replacement_tl:n { \l_regex_submatch_start_int } - \regex_query_substr:nn + \regex_toks_range:nn { \etex_gluestretch:D \tex_skip:D \l_regex_submatch_start_int } - { \l_regex_max_step_int } - \if_num:w \l_regex_tmpa_int > \c_zero - \prg_replicate:nn { \l_regex_tmpa_int } + { \l_regex_max_index_int } + \if_num:w \l_regex_internal_a_int > \c_zero + \prg_replicate:nn { \l_regex_internal_a_int } { \exp_not:n { \if_false: { \fi: } } } \fi: } - \tl_set:Nx \l_regex_tmpa_tl { \l_regex_tmpa_tl } + \tl_set:Nx \l_regex_internal_a_tl { \l_regex_internal_a_tl } \exp_args:NNNo \group_end: - \tl_set:Nn #4 \l_regex_tmpa_tl + \tl_set:Nn #4 \l_regex_internal_a_tl \else: \group_end: \fi: @@ -2087,12 +2388,12 @@ \regex_extract: \tex_skip:D \l_regex_submatch_start_int = \tex_the:D \tex_skip:D \l_regex_submatch_start_int - minus \l_regex_start_step_int sp \scan_stop: + minus \l_regex_start_index_int sp \scan_stop: \regex_match_once: } \regex_replacement:n {#3} \exp_args:Nno #1 {#2} #4 - \int_set:Nn \l_regex_tmpa_int + \int_set:Nn \l_regex_internal_a_int { 0 \prg_stepwise_function:nnnN @@ -2101,15 +2402,15 @@ { \l_regex_submatch_int - \c_one } \regex_nesting:n } - \if_num:w \l_regex_tmpa_int = \c_zero + \if_num:w \l_regex_internal_a_int = \c_zero \else: \msg_kernel_error:nnx { regex } { replace-unbalanced } - { \l_regex_tmpa_int } + { \l_regex_internal_a_int } \fi: - \tl_set:Nx \l_regex_tmpa_tl + \tl_set:Nx \l_regex_internal_a_tl { - \if_num:w \l_regex_tmpa_int < \c_zero - \prg_replicate:nn { - \l_regex_tmpa_int } + \if_num:w \l_regex_internal_a_int < \c_zero + \prg_replicate:nn { - \l_regex_internal_a_int } { \exp_not:n { { \if_false: } \fi: } } \fi: \prg_stepwise_function:nnnN @@ -2117,30 +2418,144 @@ \l_regex_capturing_group_int { \l_regex_submatch_int - \c_one } \regex_replace_all_aux:n - \regex_query_substr:nn - \l_regex_start_step_int \l_regex_max_step_int - \if_num:w \l_regex_tmpa_int > \c_zero - \prg_replicate:nn { \l_regex_tmpa_int } + \regex_toks_range:nn + \l_regex_start_index_int \l_regex_max_index_int + \if_num:w \l_regex_internal_a_int > \c_zero + \prg_replicate:nn { \l_regex_internal_a_int } { \exp_not:n { \if_false: { \fi: } } } \fi: } - \tl_set:Nx \l_regex_tmpa_tl { \l_regex_tmpa_tl } + \tl_set:Nx \l_regex_internal_a_tl { \l_regex_internal_a_tl } \exp_args:NNNo \group_end: - \tl_set:Nn #4 \l_regex_tmpa_tl + \tl_set:Nn #4 \l_regex_internal_a_tl } \cs_new:Npn \regex_replace_all_aux:n #1 { - \regex_query_substr:nn + \regex_toks_range:nn { \etex_glueshrink:D \tex_skip:D #1 } { \tex_skip:D #1 } \regex_replacement_tl:n {#1} } +\msg_kernel_new:nnnn { regex } { trailing-backslash } + { Trailing~escape~character~\iow_char:N\\. } + { + A~regular~expression~or~its~replacement~text~ends~with~ + the~escape~character~\iow_char:N\\.~It~will~be~ignored. + } +\msg_kernel_new:nnnn { regex } { x-missing-rbrace } + { Missing~closing~brace~in~\iow_char:N\\x~hexadecimal~sequence. } + { + You~wrote~something~like~ + `\iow_char:N\\x\{\int_to_hexadecimal:n{#1}'.~ + The~closing~brace~is~missing. + } +\msg_kernel_new:nnnn { regex } { x-overflow } + { Character~code~'#1'~too~large~in~\iow_char:N\\x~hexadecimal~sequence. } + { + You~wrote~something~like~ + `\iow_char:N\\x\{\int_to_hexadecimal:n{#1}\}'.~ + The~character~code~'#1'~is~larger~than~\int_use:N \c_max_char_int. + } +\msg_kernel_new:nnnn { regex } { missing-rbrack } + { Missing~right~bracket~inserted~in~regular~expression. } + { + LaTeX~was~given~a~regular~expression~where~a~character~class~ + was~started~with~'[',~but~the~matching~']'~is~missing. + } +\msg_kernel_new:nnnn { regex } { missing-rparen } + { + Missing~right~parenthes\int_compare:nTF{#1=1}{i}{e}s~ + inserted~in~regular~expression. + } + { + LaTeX~was~given~a~regular~expression~with~\int_eval:n{#1}~ + more~left~parenthes\int_compare:nTF{#1=1}{i}{e}s~than~right~ + parenthes\int_compare:nTF{#1=1}{i}{e}s. + } +\msg_kernel_new:nnnn { regex } { extra-rparen } + { Extra~right~parenthesis~ignored~in~regular~expression. } + { + LaTeX~came~across~a~closing~parenthesis~when~no~submatch~group~ + was~open.~The~parenthesis~will~be~ignored. + } +\msg_kernel_new:nnnn { regex } { class-bad-escape } + { Invalid~escape~\c_backslash_str #1~in~character~class. } + { + The~escape~sequence~\iow_char:N\\#1~may~not~appear~within~ + a~character~class.~For~instance,~assertions~(which~match~zero~ + characters)~would~not~make~sense~there. + } +\msg_kernel_new:nnnn { regex } { catcode-bad-escape } + { Invalid~escape~\c_backslash_str #1~following~category~test. } + { + The~escape~sequence~\iow_char:N\\#1~may~not~appear~following~ + a~category~test~such~as~\iow_char:N\\cL~or~ + \iow_char:N\\c[\iow_char:N\^BE]. + } +\msg_kernel_new:nnnn { regex } { range-missing-end } + { Invalid~end-point~for~range~'#1-#2'~in~character~class. } + { + The~end-point~'#2'~of~the~range~'#1-#2'~may~not~serve~as~an~ + end-point~for~a~range:~alphanumeric~characters~should~not~be~ + escaped,~and~non-alphanumeric~characters~should~be~escaped. + } +\msg_kernel_new:nnnn { regex } { range-backwards } + { Range~[#1-#2]~out~of~order~in~character~class. } + { + In~ranges~of~characters~[x-y]~appearing~in~character~classes,~ + the~first~character~code~must~not~be~larger~than~the~second.~ + Here,~#1~has~character~code~\int_eval:n {`#1},~while~#2~has~ + character~code~\int_eval:n {`#2}. + } +\msg_kernel_new:nnnn { regex } { c-bad-mode } + { Invalid~nested~\iow_char:N\\c~escape~in~regular~expression. } + { + The~\iow_char:N\\c~escape~cannot~be~used~within~ + a~control~sequence~test~'\iow_char:N\\c{...}'.~ + To~combine~several~category~tests,~use~'\iow_char:N\\c[...]'. + } +\msg_kernel_new:nnnn { regex } { c-missing-rbrace } + { Missing~right~brace~inserted~for~\iow_char:N\\c~escape. } + { + LaTeX~was~given~a~regular~expression~where~a~ + '\iow_char:N\\c\iow_char:N\{...'~construction~was~not~ended~ + with~a~closing~brace~'\iow_char:N\}'. + } +\msg_kernel_new:nnnn { regex } { c-missing-rbrack } + { Missing~right~bracket~inserted~for~\iow_char:N\\c~escape. } + { + A~construction~'\iow_char:N\\c[...'~appears~in~a~ + regular~expression,~but~the~closing~']'~is~not~present. + } +\msg_kernel_new:nnnn { regex } { c-missing-category } + { Invalid~character~'#1'~following~\iow_char\\c~escape. } + { + In~regular~expressions,~the~\iow_char:N\\c~escape~sequence~ + may~only~be~followed~by~a~left~brace,~a~left~bracket,~or~a~ + capital~letter~representing~a~character~category,~namely~ + one~of~ABCDELMOPSTU. + } +\msg_kernel_new:nnnn { regex } { u-missing-lbrace } + { Missing~left~brace~following~\iow_char:N\\u~escape. } + { + The~\iow_char:N\\u~escape~sequence~must~be~followed~by~ + a~brace~group~with~the~name~of~the~variable~to~use. + } +\msg_kernel_new:nnnn { regex } { u-missing-rbrace } + { Missing~right~brace~inserted~for~\iow_char:N\\u~escape. } + { + LaTeX~ + \tl_if_empty:xTF {#2} + { reached~the~end~of~the~string~ } + { encountered~an~escaped~alphanumeric~character '\iow_char:N\\#2'~ } + when~parsing~the~argument~of~an~'\iow_char:N\\u\iow_char:N\{...\}'~escape. + } \msg_kernel_new:nnnn { regex } { sequence-unbalanced } { Missing~ - \flag_test:NTF \l_regex_group_end_flag + \flag_if_raised:nTF { regex_end } { left~ - \flag_test:NTF \l_regex_group_begin_flag + \flag_if_raised:nTF { regex_begin } { and~right~braces } { brace } } { right~brace } @@ -2170,34 +2585,6 @@ } \ inserted. } -\msg_kernel_new:nnnn { regex } { missing-rparen } - { Missing~right~parenthesis~added~in~regular~expression. } - { - LaTeX~was~given~a~regular~expression~with~\int_eval:n{#1}~ - more~left~parenthes\int_compare:nTF{#1=1}{i}{e}s~than~right~ - parenthes\int_compare:nTF{#1=1}{i}{e}s. - } -\msg_kernel_new:nnnn { regex } { extra-rparen } - { Extra~right~parenthesis~ignored~in~regular~expression. } - { - LaTeX~came~across~a~closing~parenthesis~when~no~submatch~group~ - was~open.~The~parenthesis~will~be~ignored. - } -\msg_kernel_new:nnnn { regex } { backwards-range } - { Range~[#1-#2]~out~of~order~in~character~class. } - { - In~ranges~of~characters~[x-y]~appearing~in~character~classes,~ - the~first~character~code~must~not~be~larger~than~the~second.~ - Here,~#1~has~character~code~\int_eval:n {`#1},~while~#2~has~ - character~code~\int_eval:n {`#2}. - } -\msg_kernel_new:nnnn { regex } { unsupported } - { Unsupported~construction~`#1'. } - { - The~construction~`#1'~is~not~supported~by~the~LaTeX~ - regular~expression~module.~Perhaps~some~character~should~ - have~been~escaped? - } \msg_kernel_new:nnnn { regex } { not-nfa } { This~is~not~a~regular~expression~variable. } { @@ -2206,6 +2593,8 @@ is~\\\\ \ \ \ \ \token_to_str:N #1 = \token_to_meaning:N #1 . } +\msg_kernel_new:nnn { regex } { nfa-misused } + { Automaton~#1 used~incorrectly. } \msg_kernel_new:nnnn { regex } { c-command } { Misused~\iow_char:N\\c~or\iow_char:N\\C~command~in~a~#1. } { @@ -2232,8 +2621,6 @@ submatches~are~represented~either~as~\iow_char:N \\g{dd..d},~ or~\\d,~where~`d'~are~single~digits.~Here,~a~brace~is~missing. } -\msg_kernel_new:nnn { regex } { nfa-misused } - { Automaton~#1 used~incorrectly. } %% %% %% End of file `l3regex.sty'. |