@@ -1275,10 +1275,20 @@ static bool_t can_character_follow_identifier_(char ch) {
12751275
12761276static bool_t stream__write_text (
12771277 stream_t * obj , const char * str , size_t len , bool_t lhead , size_t dedent , size_t indent ,
1278- code_block_kind_t kind , size_t ncapts , const string_array_t * mvars , const subst_map_t * subst
1278+ code_block_kind_t kind , const node_const_array_t * capts , const string_array_t * mvars , const subst_map_t * subst
12791279) {
12801280 size_t i = 0 ;
1281+ size_t mc = 0 ;
12811282 if (len == VOID_VALUE ) return lhead ; /* for safety */
1283+ if (capts ) {
1284+ size_t ic ;
1285+ for (ic = 0 ; ic < capts -> n ; ic ++ ) {
1286+ size_t kc ;
1287+ assert (capts -> p [ic ]-> type == NODE_CAPTURE );
1288+ kc = capts -> p [ic ]-> data .capture .index + 1 ;
1289+ if (mc < kc ) mc = kc ;
1290+ }
1291+ }
12821292 while (i < len ) {
12831293 if (str [i ] == '\0' ) break ;
12841294 if (match_eol (str , i , len , & i )) {
@@ -1324,15 +1334,21 @@ static bool_t stream__write_text(
13241334 c = str [j ];
13251335 if (!(c >= '0' && c <= '9' )) break ;
13261336 k = (c - '0' ) + 10 * k ;
1327- if (k > ncapts ) k = ncapts + 1 ; /* to avoid overflow */
1337+ if (k > mc ) k = mc + 1 ; /* to avoid overflow */
13281338 j ++ ;
13291339 }
13301340 if (j < len && (str [j ] == 's' || str [j ] == 'e' )) j ++ ;
1331- if ((j >= len || can_character_follow_identifier_ (str [j ])) && k <= ncapts ) {
1332- stream__puts (obj , VARNAME_CAPTURE_PREFIX );
1333- i ++ ;
1334- while (i < j ) stream__putc (obj , str [i ++ ]);
1335- b = TRUE;
1341+ if (j >= len || can_character_follow_identifier_ (str [j ])) {
1342+ size_t ic ;
1343+ for (ic = 0 ; ic < capts -> n ; ic ++ ) {
1344+ if (k == capts -> p [ic ]-> data .capture .index + 1 ) break ;
1345+ }
1346+ if (ic < capts -> n ) {
1347+ stream__puts (obj , VARNAME_CAPTURE_PREFIX );
1348+ i ++ ;
1349+ while (i < j ) stream__putc (obj , str [i ++ ]);
1350+ b = TRUE;
1351+ }
13361352 }
13371353 }
13381354 else if (c == '{' ) { /* ${...} */
@@ -1475,7 +1491,7 @@ static bool_t stream__write_text(
14751491
14761492static bool_t stream__write_code (
14771493 stream_t * obj , const char * str , size_t len , bool_t lhead , size_t dedent , size_t indent ,
1478- code_block_kind_t kind , size_t ncapts , const string_array_t * mvars , const subst_map_t * subst , bool_t dircpp
1494+ code_block_kind_t kind , const node_const_array_t * capts , const string_array_t * mvars , const subst_map_t * subst , bool_t dircpp
14791495) {
14801496 bool_t b = TRUE; /* TRUE if succeeding leading spaces in a line */
14811497 size_t h = 0 ;
@@ -1486,7 +1502,7 @@ static bool_t stream__write_code(
14861502 if (!dircpp && b && match_directive_c (str , i , len , & k )) {
14871503 {
14881504 const size_t j = find_back_preceding_spaces_in_line (str , i );
1489- stream__write_text (obj , str + h , j - h , lhead , dedent , indent , kind , ncapts , mvars , subst );
1505+ stream__write_text (obj , str + h , j - h , lhead , dedent , indent , kind , capts , mvars , subst );
14901506 }
14911507 {
14921508 size_t md = VOID_VALUE ; /* the minimum indent length of the successive lines */
@@ -1502,29 +1518,29 @@ static bool_t stream__write_code(
15021518 }
15031519 }
15041520 if (md == VOID_VALUE ) md = 0 ;
1505- lhead = stream__write_code (obj , str + i , k - i , FALSE, md , INDENT_UNIT , kind , ncapts , mvars , subst , TRUE);
1521+ lhead = stream__write_code (obj , str + i , k - i , FALSE, md , INDENT_UNIT , kind , capts , mvars , subst , TRUE);
15061522 }
15071523 h = i = k ;
15081524 /* b == TRUE */
15091525 }
15101526 else if (match_comment_cxx (str , i , len , & k )) {
1511- lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , ncapts , mvars , subst );
1512- lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , ncapts , mvars , subst );
1527+ lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , capts , mvars , subst );
1528+ lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , capts , mvars , subst );
15131529 h = i = k ;
15141530 b = TRUE;
15151531 }
15161532 else if (match_comment_c (str , i , len , & k )) {
1517- lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , ncapts , mvars , subst );
1518- lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , ncapts , mvars , subst );
1533+ lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , capts , mvars , subst );
1534+ lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , capts , mvars , subst );
15191535 h = i = k ;
15201536 /* do not change `b` because a C-style comment is dealt as a white space */
15211537 }
15221538 else if (
15231539 match_quotation (str , i , len , '\'' , & k ) ||
15241540 match_quotation (str , i , len , '\"' , & k )
15251541 ) {
1526- lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , ncapts , mvars , subst );
1527- lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , ncapts , mvars , subst );
1542+ lhead = stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , capts , mvars , subst );
1543+ lhead = stream__write_text (obj , str + i , k - i , lhead , dedent , indent , CODE_BLOCK_KIND__NONE , capts , mvars , subst );
15281544 h = i = k ;
15291545 b = FALSE;
15301546 }
@@ -1538,12 +1554,12 @@ static bool_t stream__write_code(
15381554 }
15391555 }
15401556 }
1541- return stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , ncapts , mvars , subst );
1557+ return stream__write_text (obj , str + h , i - h , lhead , dedent , indent , kind , capts , mvars , subst );
15421558}
15431559
15441560static void stream__write_code_block (
15451561 stream_t * obj , const char * str , size_t len , size_t indent , const char * path , size_t lineno ,
1546- code_block_kind_t kind , size_t ncapts , const string_array_t * mvars , const subst_map_t * subst
1562+ code_block_kind_t kind , const node_const_array_t * capts , const string_array_t * mvars , const subst_map_t * subst
15471563) {
15481564 size_t m = VOID_VALUE ; /* the minimum indent length except for the start line of the code block */
15491565 size_t b = TRUE; /* TRUE if the code starts at the first line */
@@ -1584,7 +1600,7 @@ static void stream__write_code_block(
15841600 assert (b || d >= m );
15851601 stream__write_characters (obj , ' ' , indent + (b ? 0 : d - m ));
15861602 }
1587- stream__write_code (obj , str + h , len - h , FALSE, m , indent , kind , ncapts , mvars , subst , FALSE);
1603+ stream__write_code (obj , str + h , len - h , FALSE, m , indent , kind , capts , mvars , subst , FALSE);
15881604 stream__putc (obj , '\n' );
15891605 if (obj -> line != VOID_VALUE )
15901606 stream__write_line_directive (obj , obj -> path , obj -> line + 1 );
@@ -1593,7 +1609,7 @@ static void stream__write_code_block(
15931609static void stream__write_footer (
15941610 stream_t * obj , const char * str , size_t len , const char * path , size_t lineno , const subst_map_t * subst
15951611) {
1596- stream__write_code_block (obj , str , len , 0 , path , lineno , CODE_BLOCK_KIND__NORMAL , 0 , NULL , subst );
1612+ stream__write_code_block (obj , str , len , 0 , path , lineno , CODE_BLOCK_KIND__NORMAL , NULL , NULL , subst );
15971613}
15981614
15991615static char * get_home_directory (void ) {
@@ -4590,7 +4606,7 @@ static bool_t generate(context_t *ctx) {
45904606 for (i = 0 ; i < ctx -> eheader .n ; i ++ ) {
45914607 stream__write_code_block (
45924608 & hstream , ctx -> eheader .p [i ].text , ctx -> eheader .p [i ].len , 0 ,
4593- ctx -> eheader .p [i ].fpos .path , ctx -> eheader .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , 0 , NULL , & (ctx -> subst )
4609+ ctx -> eheader .p [i ].fpos .path , ctx -> eheader .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , NULL , NULL , & (ctx -> subst )
45944610 );
45954611 stream__puts (& hstream , "\n" );
45964612 }
@@ -4607,7 +4623,7 @@ static bool_t generate(context_t *ctx) {
46074623 for (i = 0 ; i < ctx -> header .n ; i ++ ) {
46084624 stream__write_code_block (
46094625 & hstream , ctx -> header .p [i ].text , ctx -> header .p [i ].len , 0 ,
4610- ctx -> header .p [i ].fpos .path , ctx -> header .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , 0 , NULL , & (ctx -> subst )
4626+ ctx -> header .p [i ].fpos .path , ctx -> header .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , NULL , NULL , & (ctx -> subst )
46114627 );
46124628 stream__puts (& hstream , "\n" );
46134629 }
@@ -4619,7 +4635,7 @@ static bool_t generate(context_t *ctx) {
46194635 for (i = 0 ; i < ctx -> esource .n ; i ++ ) {
46204636 stream__write_code_block (
46214637 & sstream , ctx -> esource .p [i ].text , ctx -> esource .p [i ].len , 0 ,
4622- ctx -> esource .p [i ].fpos .path , ctx -> esource .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , 0 , NULL , & (ctx -> subst )
4638+ ctx -> esource .p [i ].fpos .path , ctx -> esource .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , NULL , NULL , & (ctx -> subst )
46234639 );
46244640 stream__puts (& sstream , "\n" );
46254641 }
@@ -4667,7 +4683,7 @@ static bool_t generate(context_t *ctx) {
46674683 for (i = 0 ; i < ctx -> source .n ; i ++ ) {
46684684 stream__write_code_block (
46694685 & sstream , ctx -> source .p [i ].text , ctx -> source .p [i ].len , 0 ,
4670- ctx -> source .p [i ].fpos .path , ctx -> source .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , 0 , NULL , & (ctx -> subst )
4686+ ctx -> source .p [i ].fpos .path , ctx -> source .p [i ].fpos .line , CODE_BLOCK_KIND__NORMAL , NULL , NULL , & (ctx -> subst )
46714687 );
46724688 stream__puts (& sstream , "\n" );
46734689 }
@@ -6423,7 +6439,7 @@ static bool_t generate(context_t *ctx) {
64236439 }
64246440 stream__write_code_block (
64256441 & sstream , b -> text , b -> len , INDENT_UNIT , b -> fpos .path , b -> fpos .line ,
6426- CODE_BLOCK_KIND__PROGPRED , c -> n , & (ctx -> mvars ), & (ctx -> subst )
6442+ CODE_BLOCK_KIND__PROGPRED , c , & (ctx -> mvars ), & (ctx -> subst )
64276443 );
64286444 k = c -> n ;
64296445 while (k > 0 ) {
@@ -6537,7 +6553,7 @@ static bool_t generate(context_t *ctx) {
65376553 }
65386554 stream__write_code_block (
65396555 & sstream , b -> text , b -> len , INDENT_UNIT , b -> fpos .path , b -> fpos .line ,
6540- CODE_BLOCK_KIND__ACTION , c -> n , & (ctx -> mvars ), & (ctx -> subst )
6556+ CODE_BLOCK_KIND__ACTION , c , & (ctx -> mvars ), & (ctx -> subst )
65416557 );
65426558 k = c -> n ;
65436559 while (k > 0 ) {
0 commit comments