@@ -1275,10 +1275,20 @@ static bool_t can_character_follow_identifier_(char ch) {
1275
1275
1276
1276
static bool_t stream__write_text (
1277
1277
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
1279
1279
) {
1280
1280
size_t i = 0 ;
1281
+ size_t mc = 0 ;
1281
1282
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
+ }
1282
1292
while (i < len ) {
1283
1293
if (str [i ] == '\0' ) break ;
1284
1294
if (match_eol (str , i , len , & i )) {
@@ -1324,15 +1334,21 @@ static bool_t stream__write_text(
1324
1334
c = str [j ];
1325
1335
if (!(c >= '0' && c <= '9' )) break ;
1326
1336
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 */
1328
1338
j ++ ;
1329
1339
}
1330
1340
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
+ }
1336
1352
}
1337
1353
}
1338
1354
else if (c == '{' ) { /* ${...} */
@@ -1475,7 +1491,7 @@ static bool_t stream__write_text(
1475
1491
1476
1492
static bool_t stream__write_code (
1477
1493
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
1479
1495
) {
1480
1496
bool_t b = TRUE; /* TRUE if succeeding leading spaces in a line */
1481
1497
size_t h = 0 ;
@@ -1486,7 +1502,7 @@ static bool_t stream__write_code(
1486
1502
if (!dircpp && b && match_directive_c (str , i , len , & k )) {
1487
1503
{
1488
1504
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 );
1490
1506
}
1491
1507
{
1492
1508
size_t md = VOID_VALUE ; /* the minimum indent length of the successive lines */
@@ -1502,29 +1518,29 @@ static bool_t stream__write_code(
1502
1518
}
1503
1519
}
1504
1520
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);
1506
1522
}
1507
1523
h = i = k ;
1508
1524
/* b == TRUE */
1509
1525
}
1510
1526
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 );
1513
1529
h = i = k ;
1514
1530
b = TRUE;
1515
1531
}
1516
1532
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 );
1519
1535
h = i = k ;
1520
1536
/* do not change `b` because a C-style comment is dealt as a white space */
1521
1537
}
1522
1538
else if (
1523
1539
match_quotation (str , i , len , '\'' , & k ) ||
1524
1540
match_quotation (str , i , len , '\"' , & k )
1525
1541
) {
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 );
1528
1544
h = i = k ;
1529
1545
b = FALSE;
1530
1546
}
@@ -1538,12 +1554,12 @@ static bool_t stream__write_code(
1538
1554
}
1539
1555
}
1540
1556
}
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 );
1542
1558
}
1543
1559
1544
1560
static void stream__write_code_block (
1545
1561
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
1547
1563
) {
1548
1564
size_t m = VOID_VALUE ; /* the minimum indent length except for the start line of the code block */
1549
1565
size_t b = TRUE; /* TRUE if the code starts at the first line */
@@ -1584,7 +1600,7 @@ static void stream__write_code_block(
1584
1600
assert (b || d >= m );
1585
1601
stream__write_characters (obj , ' ' , indent + (b ? 0 : d - m ));
1586
1602
}
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);
1588
1604
stream__putc (obj , '\n' );
1589
1605
if (obj -> line != VOID_VALUE )
1590
1606
stream__write_line_directive (obj , obj -> path , obj -> line + 1 );
@@ -1593,7 +1609,7 @@ static void stream__write_code_block(
1593
1609
static void stream__write_footer (
1594
1610
stream_t * obj , const char * str , size_t len , const char * path , size_t lineno , const subst_map_t * subst
1595
1611
) {
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 );
1597
1613
}
1598
1614
1599
1615
static char * get_home_directory (void ) {
@@ -4590,7 +4606,7 @@ static bool_t generate(context_t *ctx) {
4590
4606
for (i = 0 ; i < ctx -> eheader .n ; i ++ ) {
4591
4607
stream__write_code_block (
4592
4608
& 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 )
4594
4610
);
4595
4611
stream__puts (& hstream , "\n" );
4596
4612
}
@@ -4607,7 +4623,7 @@ static bool_t generate(context_t *ctx) {
4607
4623
for (i = 0 ; i < ctx -> header .n ; i ++ ) {
4608
4624
stream__write_code_block (
4609
4625
& 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 )
4611
4627
);
4612
4628
stream__puts (& hstream , "\n" );
4613
4629
}
@@ -4619,7 +4635,7 @@ static bool_t generate(context_t *ctx) {
4619
4635
for (i = 0 ; i < ctx -> esource .n ; i ++ ) {
4620
4636
stream__write_code_block (
4621
4637
& 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 )
4623
4639
);
4624
4640
stream__puts (& sstream , "\n" );
4625
4641
}
@@ -4667,7 +4683,7 @@ static bool_t generate(context_t *ctx) {
4667
4683
for (i = 0 ; i < ctx -> source .n ; i ++ ) {
4668
4684
stream__write_code_block (
4669
4685
& 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 )
4671
4687
);
4672
4688
stream__puts (& sstream , "\n" );
4673
4689
}
@@ -6423,7 +6439,7 @@ static bool_t generate(context_t *ctx) {
6423
6439
}
6424
6440
stream__write_code_block (
6425
6441
& 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 )
6427
6443
);
6428
6444
k = c -> n ;
6429
6445
while (k > 0 ) {
@@ -6537,7 +6553,7 @@ static bool_t generate(context_t *ctx) {
6537
6553
}
6538
6554
stream__write_code_block (
6539
6555
& 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 )
6541
6557
);
6542
6558
k = c -> n ;
6543
6559
while (k > 0 ) {
0 commit comments