summaryrefslogtreecommitdiff
path: root/gettext-tools/src/x-perl.c
blob: ea903dcc3837f092b40e0fe63476aebdc0bfc3cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
/* xgettext Perl backend.
   Copyright (C) 2002-2010, 2015-2016 Free Software Foundation, Inc.

   This file was written by Guido Flohr <guido@imperia.net>, 2002-2010.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* Specification.  */
#include "x-perl.h"

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "message.h"
#include "xgettext.h"
#include "error.h"
#include "error-progname.h"
#include "xalloc.h"
#include "po-charset.h"
#include "unistr.h"
#include "uniname.h"
#include "gettext.h"

#define _(s) gettext(s)

/* The Perl syntax is defined in perlsyn.pod.  Try the command
   "man perlsyn" or "perldoc perlsyn".
   Also, the syntax after the 'sub' keyword is specified in perlsub.pod.
   Try the command "man perlsub" or "perldoc perlsub".
   Perl 5.10 has new operators '//' and '//=', see
   <http://perldoc.perl.org/perldelta.html#Defined-or-operator>.  */

#define DEBUG_PERL 0


/* ====================== Keyword set customization.  ====================== */

/* If true extract all strings.  */
static bool extract_all = false;

static hash_table keywords;
static bool default_keywords = true;


void
x_perl_extract_all ()
{
  extract_all = true;
}


void
x_perl_keyword (const char *name)
{
  if (name == NULL)
    default_keywords = false;
  else
    {
      const char *end;
      struct callshape shape;
      const char *colon;

      if (keywords.table == NULL)
        hash_init (&keywords, 100);

      split_keywordspec (name, &end, &shape);

      /* The characters between name and end should form a valid C identifier.
         A colon means an invalid parse in split_keywordspec().  */
      colon = strchr (name, ':');
      if (colon == NULL || colon >= end)
        insert_keyword_callshape (&keywords, name, end - name, &shape);
    }
}

/* Finish initializing the keywords hash table.
   Called after argument processing, before each file is processed.  */
static void
init_keywords ()
{
  if (default_keywords)
    {
      /* When adding new keywords here, also update the documentation in
         xgettext.texi!  */
      x_perl_keyword ("gettext");
      x_perl_keyword ("%gettext");
      x_perl_keyword ("$gettext");
      x_perl_keyword ("dgettext:2");
      x_perl_keyword ("dcgettext:2");
      x_perl_keyword ("ngettext:1,2");
      x_perl_keyword ("dngettext:2,3");
      x_perl_keyword ("dcngettext:2,3");
      x_perl_keyword ("gettext_noop");
#if 0
      x_perl_keyword ("__");
      x_perl_keyword ("$__");
      x_perl_keyword ("%__");
      x_perl_keyword ("__x");
      x_perl_keyword ("__n:1,2");
      x_perl_keyword ("__nx:1,2");
      x_perl_keyword ("__xn:1,2");
      x_perl_keyword ("N__");
#endif
      default_keywords = false;
    }
}

void
init_flag_table_perl ()
{
  xgettext_record_flag ("gettext:1:pass-perl-format");
  xgettext_record_flag ("gettext:1:pass-perl-brace-format");
  xgettext_record_flag ("%gettext:1:pass-perl-format");
  xgettext_record_flag ("%gettext:1:pass-perl-brace-format");
  xgettext_record_flag ("$gettext:1:pass-perl-format");
  xgettext_record_flag ("$gettext:1:pass-perl-brace-format");
  xgettext_record_flag ("dgettext:2:pass-perl-format");
  xgettext_record_flag ("dgettext:2:pass-perl-brace-format");
  xgettext_record_flag ("dcgettext:2:pass-perl-format");
  xgettext_record_flag ("dcgettext:2:pass-perl-brace-format");
  xgettext_record_flag ("ngettext:1:pass-perl-format");
  xgettext_record_flag ("ngettext:2:pass-perl-format");
  xgettext_record_flag ("ngettext:1:pass-perl-brace-format");
  xgettext_record_flag ("ngettext:2:pass-perl-brace-format");
  xgettext_record_flag ("dngettext:2:pass-perl-format");
  xgettext_record_flag ("dngettext:3:pass-perl-format");
  xgettext_record_flag ("dngettext:2:pass-perl-brace-format");
  xgettext_record_flag ("dngettext:3:pass-perl-brace-format");
  xgettext_record_flag ("dcngettext:2:pass-perl-format");
  xgettext_record_flag ("dcngettext:3:pass-perl-format");
  xgettext_record_flag ("dcngettext:2:pass-perl-brace-format");
  xgettext_record_flag ("dcngettext:3:pass-perl-brace-format");
  xgettext_record_flag ("gettext_noop:1:pass-perl-format");
  xgettext_record_flag ("gettext_noop:1:pass-perl-brace-format");
  xgettext_record_flag ("printf:1:perl-format"); /* argument 1 or 2 ?? */
  xgettext_record_flag ("sprintf:1:perl-format");
#if 0
  xgettext_record_flag ("__:1:pass-perl-format");
  xgettext_record_flag ("__:1:pass-perl-brace-format");
  xgettext_record_flag ("%__:1:pass-perl-format");
  xgettext_record_flag ("%__:1:pass-perl-brace-format");
  xgettext_record_flag ("$__:1:pass-perl-format");
  xgettext_record_flag ("$__:1:pass-perl-brace-format");
  xgettext_record_flag ("__x:1:perl-brace-format");
  xgettext_record_flag ("__n:1:pass-perl-format");
  xgettext_record_flag ("__n:2:pass-perl-format");
  xgettext_record_flag ("__n:1:pass-perl-brace-format");
  xgettext_record_flag ("__n:2:pass-perl-brace-format");
  xgettext_record_flag ("__nx:1:perl-brace-format");
  xgettext_record_flag ("__nx:2:perl-brace-format");
  xgettext_record_flag ("__xn:1:perl-brace-format");
  xgettext_record_flag ("__xn:2:perl-brace-format");
  xgettext_record_flag ("N__:1:pass-perl-format");
  xgettext_record_flag ("N__:1:pass-perl-brace-format");
#endif
}


/* ======================== Reading of characters.  ======================== */

/* Real filename, used in error messages about the input file.  */
static const char *real_file_name;

/* Logical filename and line number, used to label the extracted messages.  */
static char *logical_file_name;
static int line_number;

/* The input file stream.  */
static FILE *fp;

/* The current line buffer.  */
static char *linebuf;

/* The size of the current line.  */
static int linesize;

/* The position in the current line.  */
static int linepos;

/* The size of the input buffer.  */
static size_t linebuf_size;

/* Number of lines eaten for here documents.  */
static int eaten_here;

/* Paranoia: EOF marker for __END__ or __DATA__.  */
static bool end_of_file;


/* 1. line_number handling.  */

/* Returns the next character from the input stream or EOF.  */
static int
phase1_getc ()
{
  line_number += eaten_here;
  eaten_here = 0;

  if (end_of_file)
    return EOF;

  if (linepos >= linesize)
    {
      linesize = getline (&linebuf, &linebuf_size, fp);

      if (linesize < 0)
        {
          if (ferror (fp))
            error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
                   real_file_name);
          end_of_file = true;
          return EOF;
        }

      linepos = 0;
      ++line_number;

      /* Undosify.  This is important for catching the end of <<EOF and
         <<'EOF'.  We could rely on stdio doing this for us but
         it is not uncommon to to come across Perl scripts with CRLF
         newline conventions on systems that do not follow this
         convention.  */
      if (linesize >= 2 && linebuf[linesize - 1] == '\n'
          && linebuf[linesize - 2] == '\r')
        {
          linebuf[linesize - 2] = '\n';
          linebuf[linesize - 1] = '\0';
          --linesize;
        }
    }

  return linebuf[linepos++];
}

/* Supports only one pushback character.  */
static void
phase1_ungetc (int c)
{
  if (c != EOF)
    {
      if (linepos == 0)
        /* Attempt to ungetc across line boundary.  Shouldn't happen.
           No two phase1_ungetc calls are permitted in a row.  */
        abort ();

      --linepos;
    }
}

/* Read a here document and return its contents.
   The delimiter is an UTF-8 encoded string; the resulting string is UTF-8
   encoded as well.  */

static char *
get_here_document (const char *delimiter)
{
  /* Accumulator for the entire here document, including a NUL byte
     at the end.  */
  static char *buffer;
  static size_t bufmax = 0;
  size_t bufpos = 0;
  /* Current line being appended.  */
  static char *my_linebuf = NULL;
  static size_t my_linebuf_size = 0;

  /* Allocate the initial buffer.  Later on, bufmax > 0.  */
  if (bufmax == 0)
    {
      buffer = XNMALLOC (1, char);
      buffer[0] = '\0';
      bufmax = 1;
    }

  for (;;)
    {
      int read_bytes = getline (&my_linebuf, &my_linebuf_size, fp);
      char *my_line_utf8;
      bool chomp;

      if (read_bytes < 0)
        {
          if (ferror (fp))
            {
              error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
                     real_file_name);
            }
          else
            {
              error_with_progname = false;
              error (EXIT_SUCCESS, 0, _("\
%s:%d: can't find string terminator \"%s\" anywhere before EOF"),
                     real_file_name, line_number, delimiter);
              error_with_progname = true;

              break;
            }
        }

      ++eaten_here;

      /* Convert to UTF-8.  */
      my_line_utf8 =
        from_current_source_encoding (my_linebuf, lc_string, logical_file_name,
                                      line_number + eaten_here);
      if (my_line_utf8 != my_linebuf)
        {
          if (strlen (my_line_utf8) >= my_linebuf_size)
            {
              my_linebuf_size = strlen (my_line_utf8) + 1;
              my_linebuf = xrealloc (my_linebuf, my_linebuf_size);
            }
          strcpy (my_linebuf, my_line_utf8);
          free (my_line_utf8);
        }

      /* Undosify.  This is important for catching the end of <<EOF and
         <<'EOF'.  We could rely on stdio doing this for us but you
         it is not uncommon to to come across Perl scripts with CRLF
         newline conventions on systems that do not follow this
         convention.  */
      if (read_bytes >= 2 && my_linebuf[read_bytes - 1] == '\n'
          && my_linebuf[read_bytes - 2] == '\r')
        {
          my_linebuf[read_bytes - 2] = '\n';
          my_linebuf[read_bytes - 1] = '\0';
          --read_bytes;
        }

      /* Temporarily remove the trailing newline from my_linebuf.  */
      chomp = false;
      if (read_bytes >= 1 && my_linebuf[read_bytes - 1] == '\n')
        {
          chomp = true;
          my_linebuf[read_bytes - 1] = '\0';
        }

      /* See whether this line terminates the here document.  */
      if (strcmp (my_linebuf, delimiter) == 0)
        break;

      /* Add back the trailing newline to my_linebuf.  */
      if (chomp)
        my_linebuf[read_bytes - 1] = '\n';

      /* Ensure room for read_bytes + 1 bytes.  */
      if (bufpos + read_bytes >= bufmax)
        {
          do
            bufmax = 2 * bufmax + 10;
          while (bufpos + read_bytes >= bufmax);
          buffer = xrealloc (buffer, bufmax);
        }
      /* Append this line to the accumulator.  */
      strcpy (buffer + bufpos, my_linebuf);
      bufpos += read_bytes;
    }

  /* Done accumulating the here document.  */
  return xstrdup (buffer);
}

/* Skips pod sections.  */
static void
skip_pod ()
{
  line_number += eaten_here;
  eaten_here = 0;
  linepos = 0;

  for (;;)
    {
      linesize = getline (&linebuf, &linebuf_size, fp);

      if (linesize < 0)
        {
          if (ferror (fp))
            error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
                   real_file_name);
          return;
        }

      ++line_number;

      if (strncmp ("=cut", linebuf, 4) == 0)
        {
          /* Force reading of a new line on next call to phase1_getc().  */
          linepos = linesize;
          return;
        }
    }
}


/* These are for tracking whether comments count as immediately before
   keyword.  */
static int last_comment_line;
static int last_non_comment_line;


/* 2. Replace each comment that is not inside a string literal or regular
   expression with a newline character.  We need to remember the comment
   for later, because it may be attached to a keyword string.  */

static int
phase2_getc ()
{
  static char *buffer;
  static size_t bufmax;
  size_t buflen;
  int lineno;
  int c;
  char *utf8_string;

  c = phase1_getc ();
  if (c == '#')
    {
      buflen = 0;
      lineno = line_number;
      /* Skip leading whitespace.  */
      for (;;)
        {
          c = phase1_getc ();
          if (c == EOF)
            break;
          if (c != ' ' && c != '\t' && c != '\r' && c != '\f')
            {
              phase1_ungetc (c);
              break;
            }
        }
      /* Accumulate the comment.  */
      for (;;)
        {
          c = phase1_getc ();
          if (c == '\n' || c == EOF)
            break;
          if (buflen >= bufmax)
            {
              bufmax = 2 * bufmax + 10;
              buffer = xrealloc (buffer, bufmax);
            }
          buffer[buflen++] = c;
        }
      if (buflen >= bufmax)
        {
          bufmax = 2 * bufmax + 10;
          buffer = xrealloc (buffer, bufmax);
        }
      buffer[buflen] = '\0';
      /* Convert it to UTF-8.  */
      utf8_string =
        from_current_source_encoding (buffer, lc_comment, logical_file_name,
                                      lineno);
      /* Save it until we encounter the corresponding string.  */
      savable_comment_add (utf8_string);
      last_comment_line = lineno;
    }
  return c;
}

/* Supports only one pushback character.  */
static void
phase2_ungetc (int c)
{
  if (c != EOF)
    phase1_ungetc (c);
}

/* Whitespace recognition.  */

#define case_whitespace \
  case ' ': case '\t': case '\r': case '\n': case '\f'

static inline bool
is_whitespace (int c)
{
  return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f');
}


/* ========================== Reading of tokens.  ========================== */


enum token_type_ty
{
  token_type_eof,
  token_type_lparen,            /* ( */
  token_type_rparen,            /* ) */
  token_type_comma,             /* , */
  token_type_fat_comma,         /* => */
  token_type_dereference,       /* -> */
  token_type_semicolon,         /* ; */
  token_type_lbrace,            /* { */
  token_type_rbrace,            /* } */
  token_type_lbracket,          /* [ */
  token_type_rbracket,          /* ] */
  token_type_string,            /* quote-like */
  token_type_number,            /* starting with a digit o dot */
  token_type_named_op,          /* if, unless, while, ... */
  token_type_variable,          /* $... */
  token_type_object,            /* A dereferenced variable, maybe a blessed
                                   object.  */
  token_type_symbol,            /* symbol, number */
  token_type_regex_op,          /* s, tr, y, m.  */
  token_type_dot,               /* . */
  token_type_other,             /* regexp, misc. operator */
  /* The following are not really token types, but variants used by
     the parser.  */
  token_type_keyword_symbol     /* keyword symbol */
};
typedef enum token_type_ty token_type_ty;

/* Subtypes for strings, important for interpolation.  */
enum string_type_ty
{
  string_type_verbatim,     /* "<<'EOF'", "m'...'", "s'...''...'",
                               "tr/.../.../", "y/.../.../".  */
  string_type_q,            /* "'..'", "q/.../".  */
  string_type_qq,           /* '"..."', "`...`", "qq/.../", "qx/.../",
                               "<file*glob>".  */
  string_type_qr            /* Not supported.  */
};

/* Subtypes for symbols, important for dollar interpretation.  */
enum symbol_type_ty
{
  symbol_type_none,         /* Nothing special.  */
  symbol_type_sub,          /* 'sub'.  */
  symbol_type_function      /* Function name after 'sub'.  */
};

typedef struct token_ty token_ty;
struct token_ty
{
  token_type_ty type;
  token_type_ty last_type;
  int sub_type;                 /* for token_type_string, token_type_symbol */
  char *string;                 /* for:                 in encoding:
                                   token_type_named_op  ASCII
                                   token_type_string    UTF-8
                                   token_type_symbol    ASCII
                                   token_type_variable  global_source_encoding
                                   token_type_object    global_source_encoding
                                 */
  refcounted_string_list_ty *comment; /* for token_type_string */
  int line_number;
};

#if DEBUG_PERL
static const char *
token2string (const token_ty *token)
{
  switch (token->type)
    {
    case token_type_eof:
      return "token_type_eof";
    case token_type_lparen:
      return "token_type_lparen";
    case token_type_rparen:
      return "token_type_rparen";
    case token_type_comma:
      return "token_type_comma";
    case token_type_fat_comma:
      return "token_type_fat_comma";
    case token_type_dereference:
      return "token_type_dereference";
    case token_type_semicolon:
      return "token_type_semicolon";
    case token_type_lbrace:
      return "token_type_lbrace";
    case token_type_rbrace:
      return "token_type_rbrace";
    case token_type_lbracket:
      return "token_type_lbracket";
    case token_type_rbracket:
      return "token_type_rbracket";
    case token_type_string:
      return "token_type_string";
    case token_type_number:
      return "token type number";
    case token_type_named_op:
      return "token_type_named_op";
    case token_type_variable:
      return "token_type_variable";
    case token_type_object:
      return "token_type_object";
    case token_type_symbol:
      return "token_type_symbol";
    case token_type_regex_op:
      return "token_type_regex_op";
    case token_type_dot:
      return "token_type_dot";
    case token_type_other:
      return "token_type_other";
    default:
      return "unknown";
    }
}
#endif

/* Free the memory pointed to by a 'struct token_ty'.  */
static inline void
free_token (token_ty *tp)
{
  switch (tp->type)
    {
    case token_type_named_op:
    case token_type_string:
    case token_type_symbol:
    case token_type_variable:
    case token_type_object:
      free (tp->string);
      break;
    default:
      break;
    }
  if (tp->type == token_type_string)
    drop_reference (tp->comment);
  free (tp);
}

/* Pass 1 of extracting quotes: Find the end of the string, regardless
   of the semantics of the construct.  Return the complete string,
   including the starting and the trailing delimiter, with backslashes
   removed where appropriate.  */
static char *
extract_quotelike_pass1 (int delim)
{
  /* This function is called recursively.  No way to allocate stuff
     statically.  Also alloca() is inappropriate due to limited stack
     size on some platforms.  So we use malloc().  */
  int bufmax = 10;
  char *buffer = XNMALLOC (bufmax, char);
  int bufpos = 0;
  bool nested = true;
  int counter_delim;

  buffer[bufpos++] = delim;

  /* Find the closing delimiter.  */
  switch (delim)
    {
    case '(':
      counter_delim = ')';
      break;
    case '{':
      counter_delim = '}';
      break;
    case '[':
      counter_delim = ']';
      break;
    case '<':
      counter_delim = '>';
      break;
    default: /* "..." or '...' or |...| etc. */
      nested = false;
      counter_delim = delim;
      break;
    }

  for (;;)
    {
      int c = phase1_getc ();

      /* This round can produce 1 or 2 bytes.  Ensure room for 2 bytes.  */
      if (bufpos + 2 > bufmax)
        {
          bufmax = 2 * bufmax + 10;
          buffer = xrealloc (buffer, bufmax);
        }

      if (c == counter_delim || c == EOF)
        {
          buffer[bufpos++] = counter_delim; /* will be stripped off later */
          buffer[bufpos++] = '\0';
#if DEBUG_PERL
          fprintf (stderr, "PASS1: %s\n", buffer);
#endif
          return buffer;
        }

      if (nested && c == delim)
        {
          char *inner = extract_quotelike_pass1 (delim);
          size_t len = strlen (inner);

          /* Ensure room for len + 1 bytes.  */
          if (bufpos + len >= bufmax)
            {
              do
                bufmax = 2 * bufmax + 10;
              while (bufpos + len >= bufmax);
              buffer = xrealloc (buffer, bufmax);
            }
          strcpy (buffer + bufpos, inner);
          free (inner);
          bufpos += len;
        }
      else if (c == '\\')
        {
          c = phase1_getc ();
          if (c == '\\')
            {
              buffer[bufpos++] = '\\';
              buffer[bufpos++] = '\\';
            }
          else if (c == delim || c == counter_delim)
            {
              /* This is pass2 in Perl.  */
              buffer[bufpos++] = c;
            }
          else
            {
              buffer[bufpos++] = '\\';
              phase1_ungetc (c);
            }
        }
      else
        {
          buffer[bufpos++] = c;
        }
    }
}

/* Like extract_quotelike_pass1, but return the complete string in UTF-8
   encoding.  */
static char *
extract_quotelike_pass1_utf8 (int delim)
{
  char *string = extract_quotelike_pass1 (delim);
  char *utf8_string =
    from_current_source_encoding (string, lc_string, logical_file_name,
                                  line_number);
  if (utf8_string != string)
    free (string);
  return utf8_string;
}


/* ========= Reading of tokens and commands.  Extracting strings.  ========= */


/* Context lookup table.  */
static flag_context_list_table_ty *flag_context_list_table;


/* Forward declaration of local functions.  */
static void interpolate_keywords (message_list_ty *mlp, const char *string,
                                  int lineno);
static token_ty *x_perl_lex (message_list_ty *mlp);
static void x_perl_unlex (token_ty *tp);
static bool extract_balanced (message_list_ty *mlp,
                              token_type_ty delim, bool eat_delim,
                              bool comma_delim,
                              flag_context_ty outer_context,
                              flag_context_list_iterator_ty context_iter,
                              int arg, struct arglist_parser *argparser);


/* Extract an unsigned hexadecimal number from STRING, considering at
   most LEN bytes and place the result in *RESULT.  Returns a pointer
   to the first character past the hexadecimal number.  */
static const char *
extract_hex (const char *string, size_t len, unsigned int *result)
{
  size_t i;

  *result = 0;

  for (i = 0; i < len; i++)
    {
      char c = string[i];
      int number;

      if (c >= 'A' && c <= 'F')
        number = c - 'A' + 10;
      else if (c >= 'a' && c <= 'f')
        number = c - 'a' + 10;
      else if (c >= '0' && c <= '9')
        number = c - '0';
      else
        break;

      *result <<= 4;
      *result |= number;
    }

  return string + i;
}

/* Extract an unsigned octal number from STRING, considering at
   most LEN bytes and place the result in *RESULT.  Returns a pointer
   to the first character past the octal number.  */
static const char *
extract_oct (const char *string, size_t len, unsigned int *result)
{
  size_t i;

  *result = 0;

  for (i = 0; i < len; i++)
    {
      char c = string[i];
      int number;

      if (c >= '0' && c <= '7')
        number = c - '0';
      else
        break;

      *result <<= 3;
      *result |= number;
    }

  return string + i;
}

/* Extract the various quotelike constructs except for <<EOF.  See the
   section "Gory details of parsing quoted constructs" in perlop.pod.
   Return the resulting token in *tp; tp->type == token_type_string.  */
static void
extract_quotelike (token_ty *tp, int delim)
{
  char *string = extract_quotelike_pass1_utf8 (delim);
  size_t len = strlen (string);

  tp->type = token_type_string;
  /* Take the string without the delimiters at the start and at the end.  */
  if (!(len >= 2))
    abort ();
  string[len - 1] = '\0';
  tp->string = xstrdup (string + 1);
  free (string);
  tp->comment = add_reference (savable_comment);
}

/* Extract the quotelike constructs with double delimiters, like
   s/[SEARCH]/[REPLACE]/.  This function does not eat up trailing
   modifiers (left to the caller).
   Return the resulting token in *tp; tp->type == token_type_regex_op.  */
static void
extract_triple_quotelike (message_list_ty *mlp, token_ty *tp, int delim,
                          bool interpolate)
{
  char *string;

  tp->type = token_type_regex_op;

  string = extract_quotelike_pass1_utf8 (delim);
  if (interpolate)
    interpolate_keywords (mlp, string, line_number);
  free (string);

  if (delim == '(' || delim == '<' || delim == '{' || delim == '[')
    {
      /* The delimiter for the second string can be different, e.g.
         s{SEARCH}{REPLACE} or s{SEARCH}/REPLACE/.  See "man perlrequick".  */
      delim = phase1_getc ();
      while (is_whitespace (delim))
        {
          /* The hash-sign is not a valid delimiter after whitespace, ergo
             use phase2_getc() and not phase1_getc() now.  */
          delim = phase2_getc ();
        }
    }
  string = extract_quotelike_pass1_utf8 (delim);
  if (interpolate)
    interpolate_keywords (mlp, string, line_number);
  free (string);
}

/* Perform pass 3 of quotelike extraction (interpolation).
   *tp is a token of type token_type_string.
   This function replaces tp->string.
   This function does not access tp->comment.  */
/* FIXME: Currently may writes null-bytes into the string.  */
static void
extract_quotelike_pass3 (token_ty *tp, int error_level)
{
  static char *buffer;
  static int bufmax = 0;
  int bufpos = 0;
  const char *crs;
  bool uppercase;
  bool lowercase;
  bool quotemeta;

#if DEBUG_PERL
  switch (tp->sub_type)
    {
    case string_type_verbatim:
      fprintf (stderr, "Interpolating string_type_verbatim:\n");
      break;
    case string_type_q:
      fprintf (stderr, "Interpolating string_type_q:\n");
      break;
    case string_type_qq:
      fprintf (stderr, "Interpolating string_type_qq:\n");
      break;
    case string_type_qr:
      fprintf (stderr, "Interpolating string_type_qr:\n");
      break;
    }
  fprintf (stderr, "%s\n", tp->string);
  if (tp->sub_type == string_type_verbatim)
    fprintf (stderr, "---> %s\n", tp->string);
#endif

  if (tp->sub_type == string_type_verbatim)
    return;

  /* Loop over tp->string, accumulating the expansion in buffer.  */
  crs = tp->string;
  uppercase = false;
  lowercase = false;
  quotemeta = false;
  while (*crs)
    {
      bool backslashed;

      /* Ensure room for 7 bytes, 6 (multi-)bytes plus a leading backslash
         if \Q modifier is present.  */
      if (bufpos + 7 > bufmax)
        {
          bufmax = 2 * bufmax + 10;
          buffer = xrealloc (buffer, bufmax);
        }

      if (tp->sub_type == string_type_q)
        {
          switch (*crs)
            {
            case '\\':
              if (crs[1] == '\\')
                {
                  crs += 2;
                  buffer[bufpos++] = '\\';
                  break;
                }
              /* FALLTHROUGH */
            default:
              buffer[bufpos++] = *crs++;
              break;
            }
          continue;
        }

      /* We only get here for double-quoted strings or regular expressions.
         Unescape escape sequences.  */
      if (*crs == '\\')
        {
          switch (crs[1])
            {
            case 't':
              crs += 2;
              buffer[bufpos++] = '\t';
              continue;
            case 'n':
              crs += 2;
              buffer[bufpos++] = '\n';
              continue;
            case 'r':
              crs += 2;
              buffer[bufpos++] = '\r';
              continue;
            case 'f':
              crs += 2;
              buffer[bufpos++] = '\f';
              continue;
            case 'b':
              crs += 2;
              buffer[bufpos++] = '\b';
              continue;
            case 'a':
              crs += 2;
              buffer[bufpos++] = '\a';
              continue;
            case 'e':
              crs += 2;
              buffer[bufpos++] = 0x1b;
              continue;
            case '0': case '1': case '2': case '3':
            case '4': case '5': case '6': case '7':
              {
                unsigned int oct_number;
                int length;

                crs = extract_oct (crs + 1, 3, &oct_number);

                /* FIXME: If one of the variables UPPERCASE or LOWERCASE is
                   true, the character should be converted to its uppercase
                   resp. lowercase equivalent.  I don't know if the necessary
                   facilities are already included in gettext.  For US-Ascii
                   the conversion can be already be done, however.  */
                if (uppercase && oct_number >= 'a' && oct_number <= 'z')
                  {
                    oct_number = oct_number - 'a' + 'A';
                  }
                else if (lowercase && oct_number >= 'A' && oct_number <= 'Z')
                  {
                    oct_number = oct_number - 'A' + 'a';
                  }


                /* Yes, octal escape sequences in the range 0x100..0x1ff are
                   valid.  */
                length = u8_uctomb ((unsigned char *) (buffer + bufpos),
                                    oct_number, 2);
                if (length > 0)
                  bufpos += length;
              }
              continue;
            case 'x':
              {
                unsigned int hex_number = 0;
                int length;

                crs += 2;
                if (*crs == '{')
                  {
                    const char *end = strchr (crs, '}');
                    if (end == NULL)
                      {
                        error_with_progname = false;
                        error (error_level, 0, _("\
%s:%d: missing right brace on \\x{HEXNUMBER}"), real_file_name, line_number);
                        error_with_progname = true;
                        ++crs;
                        continue;
                      }
                    else
                      {
                        ++crs;
                        (void) extract_hex (crs, end - crs, &hex_number);
                        crs = end + 1;
                      }
                  }
                else
                  {
                    crs = extract_hex (crs, 2, &hex_number);
                  }

                /* FIXME: If one of the variables UPPERCASE or LOWERCASE is
                   true, the character should be converted to its uppercase
                   resp. lowercase equivalent.  I don't know if the necessary
                   facilities are already included in gettext.  For US-Ascii
                   the conversion can be already be done, however.  */
                if (uppercase && hex_number >= 'a' && hex_number <= 'z')
                  {
                    hex_number = hex_number - 'a' + 'A';
                  }
                else if (lowercase && hex_number >= 'A' && hex_number <= 'Z')
                  {
                    hex_number = hex_number - 'A' + 'a';
                  }

                length = u8_uctomb ((unsigned char *) (buffer + bufpos),
                                    hex_number, 6);

                if (length > 0)
                  bufpos += length;
              }
              continue;
            case 'c':
              /* Perl's notion of control characters.  */
              crs += 2;
              if (*crs)
                {
                  int the_char = (unsigned char) *crs;
                  if (the_char >= 'a' && the_char <= 'z')
                    the_char = the_char - 'a' + 'A';
                  buffer[bufpos++] = the_char ^ 0x40;
                }
              continue;
            case 'N':
              crs += 2;
              if (*crs == '{')
                {
                  const char *end = strchr (crs + 1, '}');
                  if (end != NULL)
                    {
                      char *name;
                      unsigned int unicode;

                      name = XNMALLOC (end - (crs + 1) + 1, char);
                      memcpy (name, crs + 1, end - (crs + 1));
                      name[end - (crs + 1)] = '\0';

                      unicode = unicode_name_character (name);
                      if (unicode != UNINAME_INVALID)
                        {
                          /* FIXME: Convert to upper/lowercase if the
                             corresponding flag is set to true.  */
                          int length =
                            u8_uctomb ((unsigned char *) (buffer + bufpos),
                                       unicode, 6);
                          if (length > 0)
                            bufpos += length;
                        }

                      free (name);

                      crs = end + 1;
                    }
                }
              continue;
            }
        }

      /* No escape sequence, go on.  */
      if (*crs == '\\')
        {
          ++crs;
          switch (*crs)
            {
            case 'E':
              uppercase = false;
              lowercase = false;
              quotemeta = false;
              ++crs;
              continue;
            case 'L':
              uppercase = false;
              lowercase = true;
              ++crs;
              continue;
            case 'U':
              uppercase = true;
              lowercase = false;
              ++crs;
              continue;
            case 'Q':
              quotemeta = true;
              ++crs;
              continue;
            case 'l':
              ++crs;
              if (*crs >= 'A' && *crs <= 'Z')
                {
                  buffer[bufpos++] = *crs - 'A' + 'a';
                }
              else if ((unsigned char) *crs >= 0x80)
                {
                  error_with_progname = false;
                  error (error_level, 0, _("\
%s:%d: invalid interpolation (\"\\l\") of 8bit character \"%c\""),
                         real_file_name, line_number, *crs);
                  error_with_progname = true;
                }
              else
                {
                  buffer[bufpos++] = *crs;
                }
              ++crs;
              continue;
            case 'u':
              ++crs;
              if (*crs >= 'a' && *crs <= 'z')
                {
                  buffer[bufpos++] = *crs - 'a' + 'A';
                }
              else if ((unsigned char) *crs >= 0x80)
                {
                  error_with_progname = false;
                  error (error_level, 0, _("\
%s:%d: invalid interpolation (\"\\u\") of 8bit character \"%c\""),
                         real_file_name, line_number, *crs);
                  error_with_progname = true;
                }
              else
                {
                  buffer[bufpos++] = *crs;
                }
              ++crs;
              continue;
            case '\\':
              buffer[bufpos++] = *crs;
              ++crs;
              continue;
            default:
              backslashed = true;
              break;
            }
        }
      else
        backslashed = false;

      if (quotemeta
          && !((*crs >= 'A' && *crs <= 'Z') || (*crs >= 'A' && *crs <= 'z')
               || (*crs >= '0' && *crs <= '9') || *crs == '_'))
        {
          buffer[bufpos++] = '\\';
          backslashed = true;
        }

      if (!backslashed && !extract_all && (*crs == '$' || *crs == '@'))
        {
          error_with_progname = false;
          error (error_level, 0, _("\
%s:%d: invalid variable interpolation at \"%c\""),
                 real_file_name, line_number, *crs);
          error_with_progname = true;
          ++crs;
        }
      else if (lowercase)
        {
          if (*crs >= 'A' && *crs <= 'Z')
            buffer[bufpos++] = *crs - 'A' + 'a';
          else if ((unsigned char) *crs >= 0x80)
            {
              error_with_progname = false;
              error (error_level, 0, _("\
%s:%d: invalid interpolation (\"\\L\") of 8bit character \"%c\""),
                     real_file_name, line_number, *crs);
              error_with_progname = true;
              buffer[bufpos++] = *crs;
            }
          else
            buffer[bufpos++] = *crs;
          ++crs;
        }
      else if (uppercase)
        {
          if (*crs >= 'a' && *crs <= 'z')
            buffer[bufpos++] = *crs - 'a' + 'A';
          else if ((unsigned char) *crs >= 0x80)
            {
              error_with_progname = false;
              error (error_level, 0, _("\
%s:%d: invalid interpolation (\"\\U\") of 8bit character \"%c\""),
                     real_file_name, line_number, *crs);
              error_with_progname = true;
              buffer[bufpos++] = *crs;
            }
          else
            buffer[bufpos++] = *crs;
          ++crs;
        }
      else
        {
          buffer[bufpos++] = *crs++;
        }
    }

  /* Ensure room for 1 more byte.  */
  if (bufpos >= bufmax)
    {
      bufmax = 2 * bufmax + 10;
      buffer = xrealloc (buffer, bufmax);
    }

  buffer[bufpos++] = '\0';

#if DEBUG_PERL
  fprintf (stderr, "---> %s\n", buffer);
#endif

  /* Replace tp->string.  */
  free (tp->string);
  tp->string = xstrdup (buffer);
}

/* Parse a variable.  This is done in several steps:
     1) Consume all leading occurencies of '$', '@', '%', and '*'.
     2) Determine the name of the variable from the following input.
     3) Parse possible following hash keys or array indexes.
 */
static void
extract_variable (message_list_ty *mlp, token_ty *tp, int first)
{
  static char *buffer;
  static int bufmax = 0;
  int bufpos = 0;
  int c = first;
  size_t varbody_length = 0;
  bool maybe_hash_deref = false;
  bool maybe_hash_value = false;

  tp->type = token_type_variable;

#if DEBUG_PERL
  fprintf (stderr, "%s:%d: extracting variable type '%c'\n",
           real_file_name, line_number, first);
#endif

  /*
   * 1) Consume dollars and so on (not euros ...).  Unconditionally
   *    accepting the hash sign (#) will maybe lead to inaccurate
   *    results.  FIXME!
   */
  while (c == '$' || c == '*' || c == '#' || c == '@' || c == '%')
    {
      if (bufpos >= bufmax)
        {
          bufmax = 2 * bufmax + 10;
          buffer = xrealloc (buffer, bufmax);
        }
      buffer[bufpos++] = c;
      c = phase1_getc ();
    }

  if (c == EOF)
    {
      tp->type = token_type_eof;
      return;
    }

  /* Hash references are treated in a special way, when looking for
     our keywords.  */
  if (buffer[0] == '$')
    {
      if (bufpos == 1)
        maybe_hash_value = true;
      else if (bufpos == 2 && buffer[1] == '$')
        {
          if (!(c == '{'
                || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
                || (c >= '0' && c <= '9')
                || c == '_' || c == ':' || c == '\'' || c >= 0x80))
            {
              /* Special variable $$ for pid.  */
              if (bufpos >= bufmax)
                {
                  bufmax = 2 * bufmax + 10;
                  buffer = xrealloc (buffer, bufmax);
                }
              buffer[bufpos++] = '\0';
              tp->string = xstrdup (buffer);
#if DEBUG_PERL
              fprintf (stderr, "%s:%d: is PID ($$)\n",
                       real_file_name, line_number);
#endif

              phase1_ungetc (c);
              return;
            }

          maybe_hash_deref = true;
          bufpos = 1;
        }
    }

  /*
   * 2) Get the name of the variable.  The first character is practically
   *    arbitrary.  Punctuation and numbers automagically put a variable
   *    in the global namespace but that subtle difference is not interesting
   *    for us.
   */
  if (bufpos >= bufmax)
    {
      bufmax = 2 * bufmax + 10;
      buffer = xrealloc (buffer, bufmax);
    }
  if (c == '{')
    {
      /* Yuck, we cannot accept ${gettext} as a keyword...  Except for
       * debugging purposes it is also harmless, that we suppress the
       * real name of the variable.
       */
#if DEBUG_PERL
      fprintf (stderr, "%s:%d: braced {variable_name}\n",
               real_file_name, line_number);
#endif

      if (extract_balanced (mlp, token_type_rbrace, true, false,
                            null_context, null_context_list_iterator,
                            1, arglist_parser_alloc (mlp, NULL)))
        {
          tp->type = token_type_eof;
          return;
        }
      buffer[bufpos++] = c;
    }
  else
    {
      while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
             || (c >= '0' && c <= '9')
             || c == '_' || c == ':' || c == '\'' || c >= 0x80)
        {
          ++varbody_length;
          if (bufpos >= bufmax)
            {
              bufmax = 2 * bufmax + 10;
              buffer = xrealloc (buffer, bufmax);
            }
          buffer[bufpos++] = c;
          c = phase1_getc ();
        }
      phase1_ungetc (c);
    }

  /* Probably some strange Perl variable like $`.  */
  if (varbody_length == 0)
    {
      c = phase1_getc ();
      if (c == EOF || is_whitespace (c))
        phase1_ungetc (c);  /* Loser.  */
      else
        {
          if (bufpos >= bufmax)
            {
              bufmax = 2 * bufmax + 10;
              buffer = xrealloc (buffer, bufmax);
            }
          buffer[bufpos++] = c;
        }
    }

  if (bufpos >= bufmax)
    {
      bufmax = 2 * bufmax + 10;
      buffer = xrealloc (buffer, bufmax);
    }
  buffer[bufpos++] = '\0';

  tp->string = xstrdup (buffer);

#if DEBUG_PERL
  fprintf (stderr, "%s:%d: complete variable name: %s\n",
           real_file_name, line_number, tp->string);
#endif

  /*
   * 3) If the following looks strange to you, this is valid Perl syntax:
   *
   *      $var = $$hashref    # We can place a
   *                          # comment here and then ...
   *             {key_into_hashref};
   *
   *    POD sections are not allowed but we leave complaints about
   *    that to the compiler/interpreter.
   */
  /* We only extract strings from the first hash key (if present).  */

  if (maybe_hash_deref || maybe_hash_value)
    {
      bool is_dereference = false;
      int c;

      do
        c = phase2_getc ();
      while (is_whitespace (c));

      if (c == '-')
        {
          int c2 = phase1_getc ();

          if (c2 == '>')
            {
              is_dereference = true;

              do
                c = phase2_getc ();
              while (is_whitespace (c));
            }
          else if (c2 != '\n')
            {
              /* Discarding the newline is harmless here.  The only
                 special character recognized after a minus is greater-than
                 for dereference.  However, the sequence "-\n>" that we
                 treat incorrectly here, is a syntax error.  */
              phase1_ungetc (c2);
            }
        }

      if (maybe_hash_value && is_dereference)
        {
          tp->type = token_type_object;
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: first keys preceded by \"->\"\n",
                   real_file_name, line_number);
#endif
        }
      else if (maybe_hash_value)
        {
          /* Fake it into a hash.  */
          tp->string[0] = '%';
        }

      /* Do NOT change that into else if (see above).  */
      if ((maybe_hash_value || maybe_hash_deref) && c == '{')
        {
          void *keyword_value;

#if DEBUG_PERL
          fprintf (stderr, "%s:%d: first keys preceded by '{'\n",
                   real_file_name, line_number);
#endif

          if (hash_find_entry (&keywords, tp->string, strlen (tp->string),
                               &keyword_value) == 0)
            {
              /* TODO: Shouldn't we use the shapes of the keyword, instead
                 of hardwiring argnum1 = 1 ?
              const struct callshapes *shapes =
                (const struct callshapes *) keyword_value;
              */
              struct callshapes shapes;
              shapes.keyword = tp->string; /* XXX storage duration? */
              shapes.keyword_len = strlen (tp->string);
              shapes.nshapes = 1;
              shapes.shapes[0].argnum1 = 1;
              shapes.shapes[0].argnum2 = 0;
              shapes.shapes[0].argnumc = 0;
              shapes.shapes[0].argnum1_glib_context = false;
              shapes.shapes[0].argnum2_glib_context = false;
              shapes.shapes[0].argtotal = 0;
              string_list_init (&shapes.shapes[0].xcomments);

              {
                /* Extract a possible string from the key.  Before proceeding
                   we check whether the open curly is followed by a symbol and
                   then by a right curly.  */
                flag_context_list_iterator_ty context_iter =
                  flag_context_list_iterator (
                    flag_context_list_table_lookup (
                      flag_context_list_table,
                      tp->string, strlen (tp->string)));
                token_ty *t1 = x_perl_lex (mlp);

#if DEBUG_PERL
                fprintf (stderr, "%s:%d: extracting string key\n",
                         real_file_name, line_number);
#endif

                if (t1->type == token_type_symbol
                    || t1->type == token_type_named_op)
                  {
                    token_ty *t2 = x_perl_lex (mlp);
                    if (t2->type == token_type_rbrace)
                      {
                        flag_context_ty context;
                        lex_pos_ty pos;

                        context =
                          inherited_context (null_context,
                                             flag_context_list_iterator_advance (
                                               &context_iter));

                        pos.line_number = line_number;
                        pos.file_name = logical_file_name;

                        xgettext_current_source_encoding = po_charset_utf8;
                        remember_a_message (mlp, NULL, xstrdup (t1->string),
                                            context, &pos, NULL, savable_comment);
                        xgettext_current_source_encoding = xgettext_global_source_encoding;
                        free_token (t2);
                        free_token (t1);
                      }
                    else
                      {
                        x_perl_unlex (t2);
                      }
                  }
                else
                  {
                    x_perl_unlex (t1);
                    if (extract_balanced (mlp, token_type_rbrace, true, false,
                                          null_context, context_iter,
                                          1, arglist_parser_alloc (mlp, &shapes)))
                      return;
                  }
              }
            }
          else
            {
              phase2_ungetc (c);
            }
        }
      else
        {
          phase2_ungetc (c);
        }
    }

  /* Now consume "->", "[...]", and "{...}".  */
  for (;;)
    {
      int c = phase2_getc ();
      int c2;

      switch (c)
        {
        case '{':
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: extracting balanced '{' after varname\n",
                   real_file_name, line_number);
#endif
          extract_balanced (mlp, token_type_rbrace, true, false,
                            null_context, null_context_list_iterator,
                            1, arglist_parser_alloc (mlp, NULL));
          break;

        case '[':
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: extracting balanced '[' after varname\n",
                   real_file_name, line_number);
#endif
          extract_balanced (mlp, token_type_rbracket, true, false,
                            null_context, null_context_list_iterator,
                            1, arglist_parser_alloc (mlp, NULL));
          break;

        case '-':
          c2 = phase1_getc ();
          if (c2 == '>')
            {
#if DEBUG_PERL
              fprintf (stderr, "%s:%d: another \"->\" after varname\n",
                       real_file_name, line_number);
#endif
              break;
            }
          else if (c2 != '\n')
            {
              /* Discarding the newline is harmless here.  The only
                 special character recognized after a minus is greater-than
                 for dereference.  However, the sequence "-\n>" that we
                 treat incorrectly here, is a syntax error.  */
              phase1_ungetc (c2);
            }
          /* FALLTHROUGH */

        default:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: variable finished\n",
                   real_file_name, line_number);
#endif
          phase2_ungetc (c);
          return;
        }
    }
}

/* Actually a simplified version of extract_variable().  It searches for
   variables inside a double-quoted string that may interpolate to
   some keyword hash (reference).  The string is UTF-8 encoded.  */
static void
interpolate_keywords (message_list_ty *mlp, const char *string, int lineno)
{
  static char *buffer;
  static int bufmax = 0;
  int bufpos = 0;
  flag_context_ty context;
  int c;
  bool maybe_hash_deref = false;
  enum parser_state
    {
      initial,
      one_dollar,
      two_dollars,
      identifier,
      minus,
      wait_lbrace,
      wait_quote,
      dquote,
      squote,
      barekey,
      wait_rbrace
    } state;
  token_ty token;

  lex_pos_ty pos;

  /* States are:
   *
   * initial:      initial
   * one_dollar:   dollar sign seen in state INITIAL
   * two_dollars:  another dollar-sign has been seen in state ONE_DOLLAR
   * identifier:   a valid identifier character has been seen in state
   *               ONE_DOLLAR or TWO_DOLLARS
   * minus:        a minus-sign has been seen in state IDENTIFIER
   * wait_lbrace:  a greater-than has been seen in state MINUS
   * wait_quote:   a left brace has been seen in state IDENTIFIER or in
   *               state WAIT_LBRACE
   * dquote:       a double-quote has been seen in state WAIT_QUOTE
   * squote:       a single-quote has been seen in state WAIT_QUOTE
   * barekey:      an bareword character has been seen in state WAIT_QUOTE
   * wait_rbrace:  closing quote has been seen in state DQUOTE or SQUOTE
   *
   * In the states initial...identifier the context is null_context; in the
   * states minus...wait_rbrace the context is the one suitable for the first
   * argument of the last seen identifier.
   */
  state = initial;
  context = null_context;

  token.type = token_type_string;
  token.sub_type = string_type_qq;
  token.line_number = line_number;
  /* No need for  token.comment = add_reference (savable_comment);  here.
     We can let token.comment uninitialized here, and use savable_comment
     directly, because this function only parses the given string and does
     not call phase2_getc.  */
  pos.file_name = logical_file_name;
  pos.line_number = lineno;

  while ((c = (unsigned char) *string++) != '\0')
    {
      void *keyword_value;

      if (state == initial)
        bufpos = 0;

      if (c == '\n')
        lineno++;

      if (bufpos + 1 >= bufmax)
        {
          bufmax = 2 * bufmax + 10;
          buffer = xrealloc (buffer, bufmax);
        }

      switch (state)
        {
        case initial:
          switch (c)
            {
            case '\\':
              c = (unsigned char) *string++;
              if (c == '\0')
                return;
              break;
            case '$':
              buffer[bufpos++] = '$';
              maybe_hash_deref = false;
              state = one_dollar;
              break;
            default:
              break;
            }
          break;
        case one_dollar:
          switch (c)
            {
            case '$':
              /*
               * This is enough to make us believe later that we dereference
               * a hash reference.
               */
              maybe_hash_deref = true;
              state = two_dollars;
              break;
            default:
              if (c == '_' || c == ':' || c == '\'' || c >= 0x80
                  || (c >= 'A' && c <= 'Z')
                  || (c >= 'a' && c <= 'z')
                  || (c >= '0' && c <= '9'))
                {
                  buffer[bufpos++] = c;
                  state = identifier;
                }
              else
                state = initial;
              break;
            }
          break;
        case two_dollars:
          if (c == '_' || c == ':' || c == '\'' || c >= 0x80
              || (c >= 'A' && c <= 'Z')
              || (c >= 'a' && c <= 'z')
              || (c >= '0' && c <= '9'))
            {
              buffer[bufpos++] = c;
              state = identifier;
            }
          else
            state = initial;
          break;
        case identifier:
          switch (c)
            {
            case '-':
              if (hash_find_entry (&keywords, buffer, bufpos, &keyword_value)
                  == 0)
                {
                  flag_context_list_iterator_ty context_iter =
                    flag_context_list_iterator (
                      flag_context_list_table_lookup (
                        flag_context_list_table,
                        buffer, bufpos));
                  context =
                    inherited_context (null_context,
                                       flag_context_list_iterator_advance (
                                         &context_iter));
                  state = minus;
                }
              else
                state = initial;
              break;
            case '{':
              if (!maybe_hash_deref)
                buffer[0] = '%';
              if (hash_find_entry (&keywords, buffer, bufpos, &keyword_value)
                  == 0)
                {
                  flag_context_list_iterator_ty context_iter =
                    flag_context_list_iterator (
                      flag_context_list_table_lookup (
                        flag_context_list_table,
                        buffer, bufpos));
                  context =
                    inherited_context (null_context,
                                       flag_context_list_iterator_advance (
                                         &context_iter));
                  state = wait_quote;
                }
              else
                state = initial;
              break;
            default:
              if (c == '_' || c == ':' || c == '\'' || c >= 0x80
                  || (c >= 'A' && c <= 'Z')
                  || (c >= 'a' && c <= 'z')
                  || (c >= '0' && c <= '9'))
                {
                  buffer[bufpos++] = c;
                }
              else
                state = initial;
              break;
            }
          break;
        case minus:
          switch (c)
            {
            case '>':
              state = wait_lbrace;
              break;
            default:
              context = null_context;
              state = initial;
              break;
            }
          break;
        case wait_lbrace:
          switch (c)
            {
            case '{':
              state = wait_quote;
              break;
            default:
              context = null_context;
              state = initial;
              break;
            }
          break;
        case wait_quote:
          switch (c)
            {
            case_whitespace:
              break;
            case '\'':
              pos.line_number = lineno;
              bufpos = 0;
              state = squote;
              break;
            case '"':
              pos.line_number = lineno;
              bufpos = 0;
              state = dquote;
              break;
            default:
              if (c == '_' || (c >= '0' && c <= '9') || c >= 0x80
                  || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                {
                  pos.line_number = lineno;
                  bufpos = 0;
                  buffer[bufpos++] = c;
                  state = barekey;
                }
              else
                {
                  context = null_context;
                  state = initial;
                }
              break;
            }
          break;
        case dquote:
          switch (c)
            {
            case '"':
              /* The resulting string has to be interpolated twice.  */
              buffer[bufpos] = '\0';
              token.string = xstrdup (buffer);
              extract_quotelike_pass3 (&token, EXIT_FAILURE);
              /* The string can only shrink with interpolation (because
                 we ignore \Q).  */
              if (!(strlen (token.string) <= bufpos))
                abort ();
              strcpy (buffer, token.string);
              free (token.string);
              state = wait_rbrace;
              break;
            case '\\':
              if (string[0] == '\"')
                {
                  buffer[bufpos++] = string++[0];
                }
              else if (string[0])
                {
                  buffer[bufpos++] = '\\';
                  buffer[bufpos++] = string++[0];
                }
              else
                {
                  context = null_context;
                  state = initial;
                }
              break;
            default:
              buffer[bufpos++] = c;
              break;
            }
          break;
        case squote:
          switch (c)
            {
            case '\'':
              state = wait_rbrace;
              break;
            case '\\':
              if (string[0] == '\'')
                {
                  buffer[bufpos++] = string++[0];
                }
              else if (string[0])
                {
                  buffer[bufpos++] = '\\';
                  buffer[bufpos++] = string++[0];
                }
              else
                {
                  context = null_context;
                  state = initial;
                }
              break;
            default:
              buffer[bufpos++] = c;
              break;
            }
          break;
        case barekey:
          if (c == '_' || (c >= '0' && c <= '9') || c >= 0x80
              || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
            {
              buffer[bufpos++] = c;
              break;
            }
          else if (is_whitespace (c))
            {
              state = wait_rbrace;
              break;
            }
          else if (c != '}')
            {
              context = null_context;
              state = initial;
              break;
            }
          /* Must be right brace.  */
          /* FALLTHROUGH */
        case wait_rbrace:
          switch (c)
            {
            case_whitespace:
              break;
            case '}':
              buffer[bufpos] = '\0';
              token.string = xstrdup (buffer);
              extract_quotelike_pass3 (&token, EXIT_FAILURE);
              xgettext_current_source_encoding = po_charset_utf8;
              remember_a_message (mlp, NULL, token.string, context, &pos,
                                  NULL, savable_comment);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              /* FALLTHROUGH */
            default:
              context = null_context;
              state = initial;
              break;
            }
          break;
        }
    }
}

/* There is an ambiguity about '/' and '?': They can start an operator
   (division operator '/' or '/=' or the conditional operator '?'), or they can
   start a regular expression.  The distinction is important because inside
   regular expressions, '#' loses its special meaning.  This function helps
   making the decision (a heuristic).  See the documentation for details.  */
static bool
prefer_regexp_over_division (token_type_ty type)
{
  bool retval = true;

  switch (type)
    {
      case token_type_eof:
        retval = true;
        break;
      case token_type_lparen:
        retval = true;
        break;
      case token_type_rparen:
        retval = false;
        break;
      case token_type_comma:
        retval = true;
        break;
      case token_type_fat_comma:
        retval = true;
        break;
      case token_type_dereference:
        retval = true;
        break;
      case token_type_semicolon:
        retval = true;
        break;
      case token_type_lbrace:
        retval = true;
        break;
      case token_type_rbrace:
        retval = false;
        break;
      case token_type_lbracket:
        retval = true;
        break;
      case token_type_rbracket:
        retval = false;
        break;
      case token_type_string:
        retval = false;
        break;
      case token_type_number:
        retval = false;
        break;
      case token_type_named_op:
        retval = true;
        break;
      case token_type_variable:
        retval = false;
        break;
      case token_type_object:
        retval = false;
        break;
      case token_type_symbol:
      case token_type_keyword_symbol:
        retval = true;
        break;
      case token_type_regex_op:
        retval = false;
        break;
      case token_type_dot:
        retval = true;
        break;
      case token_type_other:
        retval = true;
        break;
  }

#if DEBUG_PERL
  token_ty ty;
  ty.type = type;
  fprintf (stderr, "Prefer regexp over division after %s: %s\n",
           token2string (&ty), retval ? "true" : "false");
#endif

  return retval;
}

/* Last token type seen in the stream.  Important for the interpretation
   of slash and question mark.  */
static token_type_ty last_token_type;

/* Combine characters into tokens.  Discard whitespace.  */

static void
x_perl_prelex (message_list_ty *mlp, token_ty *tp)
{
  static char *buffer;
  static int bufmax;
  int bufpos;
  int c;

  for (;;)
    {
      c = phase2_getc ();
      tp->line_number = line_number;
      tp->last_type = last_token_type;

      switch (c)
        {
        case EOF:
          tp->type = token_type_eof;
          return;

        case '\n':
          if (last_non_comment_line > last_comment_line)
            savable_comment_reset ();
          /* FALLTHROUGH */
        case '\t':
        case ' ':
          /* Ignore whitespace.  */
          continue;

        case '%':
        case '@':
        case '*':
        case '$':
          if (!extract_all)
            {
              extract_variable (mlp, tp, c);
              return;
            }
          break;
        }

      last_non_comment_line = tp->line_number;

      switch (c)
        {
        case '.':
          {
            int c2 = phase1_getc ();
            phase1_ungetc (c2);
            if (c2 == '.')
              {
                tp->type = token_type_other;
                return;
              }
            else if (!(c2 >= '0' && c2 <= '9'))
              {
                tp->type = token_type_dot;
                return;
              }
          }
          /* FALLTHROUGH */
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case '_':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
          /* Symbol, or part of a number.  */
          bufpos = 0;
          for (;;)
            {
              if (bufpos >= bufmax)
                {
                  bufmax = 2 * bufmax + 10;
                  buffer = xrealloc (buffer, bufmax);
                }
              buffer[bufpos++] = c;
              c = phase1_getc ();
              switch (c)
                {
                case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
                case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
                case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
                case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
                case 'Y': case 'Z':
                case '_':
                case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
                case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
                case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
                case 's': case 't': case 'u': case 'v': case 'w': case 'x':
                case 'y': case 'z':
                case '0': case '1': case '2': case '3': case '4':
                case '5': case '6': case '7': case '8': case '9':
                  continue;

                default:
                  phase1_ungetc (c);
                  break;
                }
              break;
            }
          if (bufpos >= bufmax)
            {
              bufmax = 2 * bufmax + 10;
              buffer = xrealloc (buffer, bufmax);
            }
          buffer[bufpos] = '\0';

          if (strcmp (buffer, "__END__") == 0
              || strcmp (buffer, "__DATA__") == 0)
            {
              end_of_file = true;
              tp->type = token_type_eof;
              return;
            }
          else if (strcmp (buffer, "and") == 0
                   || strcmp (buffer, "cmp") == 0
                   || strcmp (buffer, "eq") == 0
                   || strcmp (buffer, "if") == 0
                   || strcmp (buffer, "ge") == 0
                   || strcmp (buffer, "gt") == 0
                   || strcmp (buffer, "le") == 0
                   || strcmp (buffer, "lt") == 0
                   || strcmp (buffer, "ne") == 0
                   || strcmp (buffer, "not") == 0
                   || strcmp (buffer, "or") == 0
                   || strcmp (buffer, "unless") == 0
                   || strcmp (buffer, "while") == 0
                   || strcmp (buffer, "xor") == 0)
            {
              tp->type = token_type_named_op;
              tp->string = xstrdup (buffer);
              return;
            }
          else if (strcmp (buffer, "s") == 0
                 || strcmp (buffer, "y") == 0
                 || strcmp (buffer, "tr") == 0)
            {
              int delim = phase1_getc ();

              while (is_whitespace (delim))
                delim = phase2_getc ();

              if (delim == EOF)
                {
                  tp->type = token_type_eof;
                  return;
                }
              if ((delim >= '0' && delim <= '9')
                  || (delim >= 'A' && delim <= 'Z')
                  || (delim >= 'a' && delim <= 'z'))
                {
                  /* False positive.  */
                  phase2_ungetc (delim);
                  tp->type = token_type_symbol;
                  tp->sub_type = symbol_type_none;
                  tp->string = xstrdup (buffer);
                  return;
                }
              extract_triple_quotelike (mlp, tp, delim,
                                        buffer[0] == 's' && delim != '\'');

              /* Eat the following modifiers.  */
              do
                c = phase1_getc ();
              while (c >= 'a' && c <= 'z');
              phase1_ungetc (c);
              return;
            }
          else if (strcmp (buffer, "m") == 0)
            {
              int delim = phase1_getc ();

              while (is_whitespace (delim))
                delim = phase2_getc ();

              if (delim == EOF)
                {
                  tp->type = token_type_eof;
                  return;
                }
              if ((delim >= '0' && delim <= '9')
                  || (delim >= 'A' && delim <= 'Z')
                  || (delim >= 'a' && delim <= 'z'))
                {
                  /* False positive.  */
                  phase2_ungetc (delim);
                  tp->type = token_type_symbol;
                  tp->sub_type = symbol_type_none;
                  tp->string = xstrdup (buffer);
                  return;
                }
              extract_quotelike (tp, delim);
              if (delim != '\'')
                interpolate_keywords (mlp, tp->string, line_number);
              free (tp->string);
              drop_reference (tp->comment);
              tp->type = token_type_regex_op;

              /* Eat the following modifiers.  */
              do
                c = phase1_getc ();
              while (c >= 'a' && c <= 'z');
              phase1_ungetc (c);
              return;
            }
          else if (strcmp (buffer, "qq") == 0
                   || strcmp (buffer, "q") == 0
                   || strcmp (buffer, "qx") == 0
                   || strcmp (buffer, "qw") == 0
                   || strcmp (buffer, "qr") == 0)
            {
              /* The qw (...) construct is not really a string but we
                 can treat in the same manner and then pretend it is
                 a symbol.  Rationale: Saying "qw (foo bar)" is the
                 same as "my @list = ('foo', 'bar'); @list;".  */

              int delim = phase1_getc ();

              while (is_whitespace (delim))
                delim = phase2_getc ();

              if (delim == EOF)
                {
                  tp->type = token_type_eof;
                  return;
                }

              if ((delim >= '0' && delim <= '9')
                  || (delim >= 'A' && delim <= 'Z')
                  || (delim >= 'a' && delim <= 'z'))
                {
                  /* False positive.  */
                  phase2_ungetc (delim);
                  tp->type = token_type_symbol;
                  tp->sub_type = symbol_type_none;
                  tp->string = xstrdup (buffer);
                  return;
                }

              extract_quotelike (tp, delim);

              switch (buffer[1])
                {
                case 'q':
                case 'x':
                  tp->type = token_type_string;
                  tp->sub_type = string_type_qq;
                  interpolate_keywords (mlp, tp->string, line_number);
                  break;
                case 'r':
                  drop_reference (tp->comment);
                  tp->type = token_type_regex_op;
                  break;
                case 'w':
                  drop_reference (tp->comment);
                  tp->type = token_type_symbol;
                  tp->sub_type = symbol_type_none;
                  break;
                case '\0':
                  tp->type = token_type_string;
                  tp->sub_type = string_type_q;
                  break;
                default:
                  abort ();
                }
              return;
            }
          else if ((buffer[0] >= '0' && buffer[0] <= '9') || buffer[0] == '.')
            {
              tp->type = token_type_number;
              return;
            }
          tp->type = token_type_symbol;
          tp->sub_type = (strcmp (buffer, "sub") == 0
                          ? symbol_type_sub
                          : symbol_type_none);
          tp->string = xstrdup (buffer);
          return;

        case '"':
          extract_quotelike (tp, c);
          tp->sub_type = string_type_qq;
          interpolate_keywords (mlp, tp->string, line_number);
          return;

        case '`':
          extract_quotelike (tp, c);
          tp->sub_type = string_type_qq;
          interpolate_keywords (mlp, tp->string, line_number);
          return;

        case '\'':
          extract_quotelike (tp, c);
          tp->sub_type = string_type_q;
          return;

        case '(':
          c = phase2_getc ();
          if (c == ')')
            /* Ignore empty list.  */
            continue;
          else
            phase2_ungetc (c);
          tp->type = token_type_lparen;
          return;

        case ')':
          tp->type = token_type_rparen;
          return;

        case '{':
          tp->type = token_type_lbrace;
          return;

        case '}':
          tp->type = token_type_rbrace;
          return;

        case '[':
          tp->type = token_type_lbracket;
          return;

        case ']':
          tp->type = token_type_rbracket;
          return;

        case ';':
          tp->type = token_type_semicolon;
          return;

        case ',':
          tp->type = token_type_comma;
          return;

        case '=':
          /* Check for fat comma.  */
          c = phase1_getc ();
          if (c == '>')
            {
              tp->type = token_type_fat_comma;
              return;
            }
          else if (linepos == 2
                   && (last_token_type == token_type_semicolon
                       || last_token_type == token_type_rbrace)
                   && ((c >= 'A' && c <='Z')
                       || (c >= 'a' && c <= 'z')))
            {
#if DEBUG_PERL
              fprintf (stderr, "%s:%d: start pod section\n",
                       real_file_name, line_number);
#endif
              skip_pod ();
#if DEBUG_PERL
              fprintf (stderr, "%s:%d: end pod section\n",
                       real_file_name, line_number);
#endif
              continue;
            }
          phase1_ungetc (c);
          tp->type = token_type_other;
          return;

        case '<':
          /* Check for <<EOF and friends.  */
          c = phase1_getc ();
          if (c == '<')
            {
              c = phase1_getc ();
              if (c == '\'')
                {
                  char *string;
                  extract_quotelike (tp, c);
                  string = get_here_document (tp->string);
                  free (tp->string);
                  tp->string = string;
                  tp->type = token_type_string;
                  tp->sub_type = string_type_verbatim;
                  tp->line_number = line_number + 1;
                  return;
                }
              else if (c == '"')
                {
                  char *string;
                  extract_quotelike (tp, c);
                  string = get_here_document (tp->string);
                  free (tp->string);
                  tp->string = string;
                  tp->type = token_type_string;
                  tp->sub_type = string_type_qq;
                  tp->line_number = line_number + 1;
                  interpolate_keywords (mlp, tp->string, tp->line_number);
                  return;
                }
              else if ((c >= 'A' && c <= 'Z')
                       || (c >= 'a' && c <= 'z')
                       || c == '_')
                {
                  bufpos = 0;
                  while ((c >= 'A' && c <= 'Z')
                         || (c >= 'a' && c <= 'z')
                         || (c >= '0' && c <= '9')
                         || c == '_' || c >= 0x80)
                    {
                      if (bufpos >= bufmax)
                        {
                          bufmax = 2 * bufmax + 10;
                          buffer = xrealloc (buffer, bufmax);
                        }
                      buffer[bufpos++] = c;
                      c = phase1_getc ();
                    }
                  if (c == EOF)
                    {
                      tp->type = token_type_eof;
                      return;
                    }
                  else
                    {
                      char *string;
                      phase1_ungetc (c);
                      if (bufpos >= bufmax)
                        {
                          bufmax = 2 * bufmax + 10;
                          buffer = xrealloc (buffer, bufmax);
                        }
                      buffer[bufpos++] = '\0';
                      string = get_here_document (buffer);
                      tp->string = string;
                      tp->type = token_type_string;
                      tp->sub_type = string_type_qq;
                      tp->comment = add_reference (savable_comment);
                      tp->line_number = line_number + 1;
                      interpolate_keywords (mlp, tp->string, tp->line_number);
                      return;
                    }
                }
              else
                {
                  tp->type = token_type_other;
                  return;
                }
            }
          else
            {
              phase1_ungetc (c);
              tp->type = token_type_other;
            }
          return;  /* End of case '>'.  */

        case '-':
          /* Check for dereferencing operator.  */
          c = phase1_getc ();
          if (c == '>')
            {
              tp->type = token_type_dereference;
              return;
            }
          else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
            {
              /* One of the -X (filetest) functions.  We play safe
                 and accept all alphabetical characters here.  */
              tp->type = token_type_other;
              return;
            }
          phase1_ungetc (c);
          tp->type = token_type_other;
          return;

        case '/':
        case '?':
          if (prefer_regexp_over_division (tp->last_type))
            {
              extract_quotelike (tp, c);
              interpolate_keywords (mlp, tp->string, line_number);
              free (tp->string);
              drop_reference (tp->comment);
              tp->type = token_type_regex_op;
              /* Eat the following modifiers.  */
              do
                c = phase1_getc ();
              while (c >= 'a' && c <= 'z');
              phase1_ungetc (c);
              return;
            }
          /* Recognize operator '//'.  */
          if (c == '/')
            {
              c = phase1_getc ();
              if (c != '/')
                phase1_ungetc (c);
            }
          /* FALLTHROUGH */

        default:
          /* We could carefully recognize each of the 2 and 3 character
             operators, but it is not necessary, except for the '//' operator,
             as we only need to recognize gettext invocations.  Don't
             bother.  */
          tp->type = token_type_other;
          return;
        }
    }
}


/* A token stack used as a lookahead buffer.  */

typedef struct token_stack_ty token_stack_ty;
struct token_stack_ty
{
  token_ty **items;
  size_t nitems;
  size_t nitems_max;
};

static struct token_stack_ty token_stack;

#if DEBUG_PERL
/* Dumps all resources allocated by stack STACK.  */
static int
token_stack_dump (token_stack_ty *stack)
{
  size_t i;

  fprintf (stderr, "BEGIN STACK DUMP\n");
  for (i = 0; i < stack->nitems; i++)
    {
      token_ty *token = stack->items[i];
      fprintf (stderr, "  [%s]\n", token2string (token));
      switch (token->type)
        {
        case token_type_named_op:
        case token_type_string:
        case token_type_symbol:
        case token_type_variable:
          fprintf (stderr, "    string: %s\n", token->string);
          break;
        case token_type_object:
          fprintf (stderr, "    string: %s->\n", token->string);
        default:
          break;
        }
    }
  fprintf (stderr, "END STACK DUMP\n");
  return 0;
}
#endif

/* Pushes the token TOKEN onto the stack STACK.  */
static inline void
token_stack_push (token_stack_ty *stack, token_ty *token)
{
  if (stack->nitems >= stack->nitems_max)
    {
      size_t nbytes;

      stack->nitems_max = 2 * stack->nitems_max + 4;
      nbytes = stack->nitems_max * sizeof (token_ty *);
      stack->items = xrealloc (stack->items, nbytes);
    }
  stack->items[stack->nitems++] = token;
}

/* Pops the most recently pushed token from the stack STACK and returns it.
   Returns NULL if the stack is empty.  */
static inline token_ty *
token_stack_pop (token_stack_ty *stack)
{
  if (stack->nitems > 0)
    return stack->items[--(stack->nitems)];
  else
    return NULL;
}

/* Return the top of the stack without removing it from the stack, or
   NULL if the stack is empty.  */
static inline token_ty *
token_stack_peek (const token_stack_ty *stack)
{
  if (stack->nitems > 0)
    return stack->items[stack->nitems - 1];
  else
    return NULL;
}

/* Frees all resources allocated by stack STACK.  */
static inline void
token_stack_free (token_stack_ty *stack)
{
  size_t i;

  for (i = 0; i < stack->nitems; i++)
    free_token (stack->items[i]);
  free (stack->items);
}


static token_ty *
x_perl_lex (message_list_ty *mlp)
{
#if DEBUG_PERL
  int dummy = token_stack_dump (&token_stack);
#endif
  token_ty *tp = token_stack_pop (&token_stack);

  if (!tp)
    {
      tp = XMALLOC (token_ty);
      x_perl_prelex (mlp, tp);
      tp->last_type = last_token_type;
      last_token_type = tp->type;

#if DEBUG_PERL
      fprintf (stderr, "%s:%d: x_perl_prelex returned %s\n",
               real_file_name, line_number, token2string (tp));
#endif

      /* The interpretation of a slash or question mark after a function call
         depends on the prototype of that function.  If the function expects
         at least one argument, a regular expression is preferred, otherwise
         an operator.  With our limited means, we can only guess here.  If
         the function is a builtin that takes no arguments, we prefer an
         operator by silently turning the last symbol into a variable instead
         of a symbol.

         Method calls without parentheses are not ambiguous.  After them, an
         operator must follow.  Due to some ideosyncrasies in this parser
         they are treated in two different manners.  If the call is
         chained ($foo->bar->baz) the token left of the symbol is a
         dereference operator.  If it is not chained ($foo->bar) the
         dereference operator is consumed with the extracted variable.  The
         latter case is handled below.  */
      if (tp->type == token_type_symbol)
        {
          if (tp->last_type == token_type_dereference)
            {
              /* Class method call or chained method call (with at least
                 two arrow operators).  */
              last_token_type = token_type_variable;
            }
          else if (tp->last_type == token_type_object)
            {
              /* Instance method, not chained.  */
              last_token_type = token_type_variable;
            }
          else if (strcmp (tp->string, "wantarray") == 0
                   || strcmp (tp->string, "fork") == 0
                   || strcmp (tp->string, "getlogin") == 0
                   || strcmp (tp->string, "getppid") == 0
                   || strcmp (tp->string, "getpwent") == 0
                   || strcmp (tp->string, "getgrent") == 0
                   || strcmp (tp->string, "gethostent") == 0
                   || strcmp (tp->string, "getnetent") == 0
                   || strcmp (tp->string, "getprotoent") == 0
                   || strcmp (tp->string, "getservent") == 0
                   || strcmp (tp->string, "setpwent") == 0
                   || strcmp (tp->string, "setgrent") == 0
                   || strcmp (tp->string, "endpwent") == 0
                   || strcmp (tp->string, "endgrent") == 0
                   || strcmp (tp->string, "endhostent") == 0
                   || strcmp (tp->string, "endnetent") == 0
                   || strcmp (tp->string, "endprotoent") == 0
                   || strcmp (tp->string, "endservent") == 0
                   || strcmp (tp->string, "time") == 0
                   || strcmp (tp->string, "times") == 0
                   || strcmp (tp->string, "wait") == 0
                   || strcmp (tp->string, "wantarray") == 0)
            {
              /* A Perl built-in function that does not accept arguments.  */
              last_token_type = token_type_variable;
            }
        }
    }
#if DEBUG_PERL
  else
    {
      fprintf (stderr, "%s:%d: %s recycled from stack\n",
               real_file_name, line_number, token2string (tp));
    }
#endif

  /* A symbol followed by a fat comma is really a single-quoted string.
     Function definitions or forward declarations also need a special
     handling because the dollars and at signs inside the parentheses
     must not be interpreted as the beginning of a variable ')'.  */
  if (tp->type == token_type_symbol || tp->type == token_type_named_op)
    {
      token_ty *next = token_stack_peek (&token_stack);

      if (!next)
        {
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: pre-fetching next token\n",
                   real_file_name, line_number);
#endif
          next = x_perl_lex (mlp);
          x_perl_unlex (next);
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: unshifted next token\n",
                   real_file_name, line_number);
#endif
        }

#if DEBUG_PERL
      fprintf (stderr, "%s:%d: next token is %s\n",
               real_file_name, line_number, token2string (next));
#endif

      if (next->type == token_type_fat_comma)
        {
          tp->type = token_type_string;
          tp->sub_type = string_type_q;
          tp->comment = add_reference (savable_comment);
#if DEBUG_PERL
          fprintf (stderr,
                   "%s:%d: token %s mutated to token_type_string\n",
                   real_file_name, line_number, token2string (tp));
#endif
        }
      else if (tp->type == token_type_symbol && tp->sub_type == symbol_type_sub
               && next->type == token_type_symbol)
        {
          /* Start of a function declaration or definition.  Mark this
             symbol as a function name, so that we can later eat up
             possible prototype information.  */
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: subroutine declaration/definition '%s'\n",
                   real_file_name, line_number, next->string);
#endif
          next->sub_type = symbol_type_function;
        }
      else if (tp->type == token_type_symbol
               && (tp->sub_type == symbol_type_sub
                   || tp->sub_type == symbol_type_function)
               && next->type == token_type_lparen)
        {
          /* For simplicity we simply consume everything up to the
             closing parenthesis.  Actually only a limited set of
             characters is allowed inside parentheses but we leave
             complaints to the interpreter and are prepared for
             future extensions to the Perl syntax.  */
          int c;

#if DEBUG_PERL
          fprintf (stderr, "%s:%d: consuming prototype information\n",
                   real_file_name, line_number);
#endif

          do
            {
              c = phase1_getc ();
#if DEBUG_PERL
              fprintf (stderr, "  consuming character '%c'\n", c);
#endif
            }
          while (c != EOF && c != ')');
          phase1_ungetc (c);
        }
    }

  return tp;
}

static void
x_perl_unlex (token_ty *tp)
{
  token_stack_push (&token_stack, tp);
}


/* ========================= Extracting strings.  ========================== */

/* Assuming TP is a string token, this function accumulates all subsequent
   . string2 . string3 ... to the string.  (String concatenation.)  */

static char *
collect_message (message_list_ty *mlp, token_ty *tp, int error_level)
{
  char *string;
  size_t len;

  extract_quotelike_pass3 (tp, error_level);
  string = xstrdup (tp->string);
  len = strlen (tp->string) + 1;

  for (;;)
    {
      int c;

      do
        c = phase2_getc ();
      while (is_whitespace (c));

      if (c != '.')
        {
          phase2_ungetc (c);
          return string;
        }

      do
        c = phase2_getc ();
      while (is_whitespace (c));

      phase2_ungetc (c);

      if (c == '"' || c == '\'' || c == '`'
          || ((c == '/' || c == '?')
              && prefer_regexp_over_division (tp->last_type))
          || c == 'q')
        {
          token_ty *qstring = x_perl_lex (mlp);
          if (qstring->type != token_type_string)
            {
              /* assert (qstring->type == token_type_symbol) */
              x_perl_unlex (qstring);
              return string;
            }

          extract_quotelike_pass3 (qstring, error_level);
          len += strlen (qstring->string);
          string = xrealloc (string, len);
          strcat (string, qstring->string);
          free_token (qstring);
        }
    }
}

/* The file is broken into tokens.  Scan the token stream, looking for
   a keyword, followed by a left paren, followed by a string.  When we
   see this sequence, we have something to remember.  We assume we are
   looking at a valid Perl program, and leave the complaints about
   the grammar to the compiler.

     Normal handling: Look for
       keyword ( ... msgid ... )
     Plural handling: Look for
       keyword ( ... msgid ... msgid_plural ... )

   We use recursion because the arguments before msgid or between msgid
   and msgid_plural can contain subexpressions of the same form.

   In Perl, parentheses around function arguments can be omitted.

   The general rules are:
     1) Functions declared with a prototype take exactly the specified number
        of arguments.
          sub one_arg ($) { ... }
          sub two_args ($$) { ... }
     2) When a function name is immediately followed by an opening parenthesis,
        the argument list ends at the corresponding closing parenthesis.

   If rule 1 and rule 2 are contradictory, i.e. when the program calls a
   function with an explicit argument list and the wrong number of arguments,
   the program is invalid:
     sub two_args ($$) { ... }
     foo two_args (x), y             - invalid due to rules 1 and 2

   Ambiguities are resolved as follows:
     3) Some built-ins, such as 'abs', 'sqrt', 'sin', 'cos', ..., and functions
        declared with a prototype of exactly one argument take exactly one
        argument:
          foo sin x, y  ==>  foo (sin (x), y)
          sub one_arg ($) { ... }
          foo one_arg x, y, z  ==>  foo (one_arg (x), y, z)
     4) Other identifiers, if not immediately followed by an opening
        parenthesis, consume the entire remaining argument list:
          foo bar x, y  ==>  foo (bar (x, y))
          sub two_args ($$) { ... }
          foo two_args x, y  ==>  foo (two_args (x, y))

   Other series of comma separated expressions without a function name at
   the beginning are comma expressions:
          sub two_args ($$) { ... }
          foo two_args x, (y, z)  ==>  foo (two_args (x, (y, z)))
   Note that the evaluation of comma expressions returns a list of values
   when in list context (e.g. inside the argument list of a function without
   prototype) but only one value when inside the argument list of a function
   with a prototype:
          sub print3 ($$$) { print @_ }
          print3 5, (6, 7), 8  ==>  578
          print 5, (6, 7), 8  ==>  5678

   Where rule 3 or 4 contradict rule 1 or 2, the program is invalid:
     sin (x, y)                      - invalid due to rules 2 and 3
     sub one_arg ($) { ... }
     one_arg (x, y)                  - invalid due to rules 2 and 3
     sub two_args ($$) { ... }
     foo two_args x, y, z            - invalid due to rules 1 and 4
 */

/* Extract messages until the next balanced closing parenthesis.
   Extracted messages are added to MLP.

   DELIM can be either token_type_rbrace, token_type_rbracket,
   token_type_rparen.  Additionally, if COMMA_DELIM is true, parsing
   stops at the next comma outside parentheses.

   ARG is the current argument list position, starts with 1.
   ARGPARSER is the corresponding argument list parser.

   Returns true for EOF, false otherwise.  */

static bool
extract_balanced (message_list_ty *mlp,
                  token_type_ty delim, bool eat_delim, bool comma_delim,
                  flag_context_ty outer_context,
                  flag_context_list_iterator_ty context_iter,
                  int arg, struct arglist_parser *argparser)
{
  /* Whether to implicitly assume the next tokens are arguments even without
     a '('.  */
  bool next_is_argument = false;
  /* Parameters of the keyword just seen.  Defined only when next_is_argument
     is true.  */
  const struct callshapes *next_shapes = NULL;
  struct arglist_parser *next_argparser = NULL;

  /* Whether to not consider strings until the next comma.  */
  bool skip_until_comma = false;

  /* Context iterator that will be used if the next token is a '('.  */
  flag_context_list_iterator_ty next_context_iter =
    passthrough_context_list_iterator;
  /* Current context.  */
  flag_context_ty inner_context =
    inherited_context (outer_context,
                       flag_context_list_iterator_advance (&context_iter));

#if DEBUG_PERL
  static int nesting_level = 0;

  ++nesting_level;
#endif

  for (;;)
    {
      /* The current token.  */
      token_ty *tp;

      tp = x_perl_lex (mlp);

      if (delim == tp->type)
        {
          xgettext_current_source_encoding = po_charset_utf8;
          arglist_parser_done (argparser, arg);
          xgettext_current_source_encoding = xgettext_global_source_encoding;
          if (next_argparser != NULL)
            free (next_argparser);
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: extract_balanced finished (%d)\n",
                   logical_file_name, tp->line_number, --nesting_level);
#endif
          if (eat_delim)
            free_token (tp);
          else
            /* Preserve the delimiter for the caller.  */
            x_perl_unlex (tp);
          return false;
        }

      if (comma_delim && tp->type == token_type_comma)
        {
          xgettext_current_source_encoding = po_charset_utf8;
          arglist_parser_done (argparser, arg);
          xgettext_current_source_encoding = xgettext_global_source_encoding;
          if (next_argparser != NULL)
            free (next_argparser);
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: extract_balanced finished at comma (%d)\n",
                   logical_file_name, tp->line_number, --nesting_level);
#endif
          x_perl_unlex (tp);
          return false;
        }

      if (next_is_argument && tp->type != token_type_lparen)
        {
          /* An argument list starts, even though there is no '('.  */
          bool next_comma_delim;

          x_perl_unlex (tp);

          if (next_shapes != NULL)
            /* We know something about the function being called.  Assume
               that it consumes only one argument if no argument number or
               total > 1 is specified.  */
            {
              size_t i;

              next_comma_delim = true;
              for (i = 0; i < next_shapes->nshapes; i++)
                {
                  const struct callshape *shape = &next_shapes->shapes[i];

                  if (shape->argnum1 > 1
                      || shape->argnum2 > 1
                      || shape->argnumc > 1
                      || shape->argtotal > 1)
                    next_comma_delim = false;
                }
            }
          else
            /* We know nothing about the function being called.  It could be
               a function prototyped to take only one argument, or on the other
               hand it could be prototyped to take more than one argument or an
               arbitrary argument list or it could be unprototyped.  Due to
               the way the parser works, assuming the first case gives the
               best results.  */
            next_comma_delim = true;

          if (extract_balanced (mlp, delim, false, next_comma_delim,
                                inner_context, next_context_iter,
                                1, next_argparser))
            {
              xgettext_current_source_encoding = po_charset_utf8;
              arglist_parser_done (argparser, arg);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              return true;
            }

          next_is_argument = false;
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          continue;
        }

      switch (tp->type)
        {
        case token_type_symbol:
        case token_type_keyword_symbol:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type symbol (%d) \"%s\"\n",
                   logical_file_name, tp->line_number, nesting_level,
                   tp->string);
#endif

          {
            void *keyword_value;

            if (hash_find_entry (&keywords, tp->string, strlen (tp->string),
                                 &keyword_value) == 0)
              {
                const struct callshapes *shapes =
                  (const struct callshapes *) keyword_value;

                next_shapes = shapes;
                next_argparser = arglist_parser_alloc (mlp, shapes);
              }
            else
              {
                next_shapes = NULL;
                next_argparser = arglist_parser_alloc (mlp, NULL);
              }
          }
          next_is_argument = true;
          next_context_iter =
            flag_context_list_iterator (
              flag_context_list_table_lookup (
                flag_context_list_table,
                tp->string, strlen (tp->string)));
          break;

        case token_type_variable:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type variable (%d) \"%s\"\n",
                   logical_file_name, tp->line_number, nesting_level,
                   tp->string);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_object:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type object (%d) \"%s->\"\n",
                   logical_file_name, tp->line_number, nesting_level,
                   tp->string);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_lparen:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type left parenthesis (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          if (next_is_argument)
            {
              /* Parse the argument list of a function call.  */
              if (extract_balanced (mlp, token_type_rparen, true, false,
                                    inner_context, next_context_iter,
                                    1, next_argparser))
                {
                  xgettext_current_source_encoding = po_charset_utf8;
                  arglist_parser_done (argparser, arg);
                  xgettext_current_source_encoding = xgettext_global_source_encoding;
                  return true;
                }
              next_is_argument = false;
              next_argparser = NULL;
            }
          else
            {
              /* Parse a parenthesized expression or comma expression.  */
              if (extract_balanced (mlp, token_type_rparen, true, false,
                                    inner_context, next_context_iter,
                                    arg, arglist_parser_clone (argparser)))
                {
                  xgettext_current_source_encoding = po_charset_utf8;
                  arglist_parser_done (argparser, arg);
                  xgettext_current_source_encoding = xgettext_global_source_encoding;
                  if (next_argparser != NULL)
                    free (next_argparser);
                  free_token (tp);
                  return true;
                }
              next_is_argument = false;
              if (next_argparser != NULL)
                free (next_argparser);
              next_argparser = NULL;
            }
          skip_until_comma = true;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_rparen:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type right parenthesis (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          skip_until_comma = true;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_comma:
        case token_type_fat_comma:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type comma (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          if (arglist_parser_decidedp (argparser, arg))
            {
              /* We have missed the argument.  */
              xgettext_current_source_encoding = po_charset_utf8;
              arglist_parser_done (argparser, arg);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              argparser = arglist_parser_alloc (mlp, NULL);
              arg = 0;
            }
          arg++;
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: arg: %d\n",
                   real_file_name, tp->line_number, arg);
#endif
          inner_context =
            inherited_context (outer_context,
                               flag_context_list_iterator_advance (
                                 &context_iter));
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          skip_until_comma = false;
          next_context_iter = passthrough_context_list_iterator;
          break;

        case token_type_string:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type string (%d): \"%s\"\n",
                   logical_file_name, tp->line_number, nesting_level,
                   tp->string);
#endif

          if (extract_all)
            {
              char *string = collect_message (mlp, tp, EXIT_SUCCESS);
              lex_pos_ty pos;

              pos.file_name = logical_file_name;
              pos.line_number = tp->line_number;
              xgettext_current_source_encoding = po_charset_utf8;
              remember_a_message (mlp, NULL, string, inner_context, &pos,
                                  NULL, tp->comment);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
            }
          else if (!skip_until_comma)
            {
              /* Need to collect the complete string, with error checking,
                 only if the argument ARG is used in ARGPARSER.  */
              bool must_collect = false;
              {
                size_t nalternatives = argparser->nalternatives;
                size_t i;

                for (i = 0; i < nalternatives; i++)
                  {
                    struct partial_call *cp = &argparser->alternative[i];

                    if (arg == cp->argnumc
                        || arg == cp->argnum1 || arg == cp->argnum2)
                      must_collect = true;
                  }
              }

              if (must_collect)
                {
                  char *string = collect_message (mlp, tp, EXIT_FAILURE);

                  xgettext_current_source_encoding = po_charset_utf8;
                  arglist_parser_remember (argparser, arg,
                                           string, inner_context,
                                           logical_file_name, tp->line_number,
                                           tp->comment);
                  xgettext_current_source_encoding = xgettext_global_source_encoding;
                }
            }

          if (arglist_parser_decidedp (argparser, arg))
            {
              xgettext_current_source_encoding = po_charset_utf8;
              arglist_parser_done (argparser, arg);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              argparser = arglist_parser_alloc (mlp, NULL);
            }

          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_number:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type number (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_eof:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type EOF (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          xgettext_current_source_encoding = po_charset_utf8;
          arglist_parser_done (argparser, arg);
          xgettext_current_source_encoding = xgettext_global_source_encoding;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          free_token (tp);
          return true;

        case token_type_lbrace:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type lbrace (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          if (extract_balanced (mlp, token_type_rbrace, true, false,
                                null_context, null_context_list_iterator,
                                1, arglist_parser_alloc (mlp, NULL)))
            {
              xgettext_current_source_encoding = po_charset_utf8;
              arglist_parser_done (argparser, arg);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              if (next_argparser != NULL)
                free (next_argparser);
              free_token (tp);
              return true;
            }
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_rbrace:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type rbrace (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_lbracket:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type lbracket (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          if (extract_balanced (mlp, token_type_rbracket, true, false,
                                null_context, null_context_list_iterator,
                                1, arglist_parser_alloc (mlp, NULL)))
            {
              xgettext_current_source_encoding = po_charset_utf8;
              arglist_parser_done (argparser, arg);
              xgettext_current_source_encoding = xgettext_global_source_encoding;
              if (next_argparser != NULL)
                free (next_argparser);
              free_token (tp);
              return true;
            }
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_rbracket:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type rbracket (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_semicolon:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type semicolon (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif

          /* The ultimate sign.  */
          xgettext_current_source_encoding = po_charset_utf8;
          arglist_parser_done (argparser, arg);
          xgettext_current_source_encoding = xgettext_global_source_encoding;
          argparser = arglist_parser_alloc (mlp, NULL);

          /* FIXME: Instead of resetting outer_context here, it may be better
             to recurse in the next_is_argument handling above, waiting for
             the next semicolon or other statement terminator.  */
          outer_context = null_context;
          context_iter = null_context_list_iterator;
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = passthrough_context_list_iterator;
          inner_context =
            inherited_context (outer_context,
                               flag_context_list_iterator_advance (
                                 &context_iter));
          break;

        case token_type_dereference:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type dereference (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_dot:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type dot (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_named_op:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type named operator (%d): %s\n",
                   logical_file_name, tp->line_number, nesting_level,
                   tp->string);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_regex_op:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type regex operator (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        case token_type_other:
#if DEBUG_PERL
          fprintf (stderr, "%s:%d: type other (%d)\n",
                   logical_file_name, tp->line_number, nesting_level);
#endif
          next_is_argument = false;
          if (next_argparser != NULL)
            free (next_argparser);
          next_argparser = NULL;
          next_context_iter = null_context_list_iterator;
          break;

        default:
          fprintf (stderr, "%s:%d: unknown token type %d\n",
                   real_file_name, tp->line_number, tp->type);
          abort ();
        }

      free_token (tp);
    }
}

void
extract_perl (FILE *f, const char *real_filename, const char *logical_filename,
              flag_context_list_table_ty *flag_table,
              msgdomain_list_ty *mdlp)
{
  message_list_ty *mlp = mdlp->item[0]->messages;

  fp = f;
  real_file_name = real_filename;
  logical_file_name = xstrdup (logical_filename);
  line_number = 0;

  last_comment_line = -1;
  last_non_comment_line = -1;

  flag_context_list_table = flag_table;

  init_keywords ();

  token_stack.items = NULL;
  token_stack.nitems = 0;
  token_stack.nitems_max = 0;
  linesize = 0;
  linepos = 0;
  eaten_here = 0;
  end_of_file = false;

  /* Safe assumption.  */
  last_token_type = token_type_semicolon;

  /* Eat tokens until eof is seen.  When extract_balanced returns
     due to an unbalanced closing brace, just restart it.  */
  while (!extract_balanced (mlp, token_type_rbrace, true, false,
                            null_context, null_context_list_iterator,
                            1, arglist_parser_alloc (mlp, NULL)))
    ;

  fp = NULL;
  real_file_name = NULL;
  free (logical_file_name);
  logical_file_name = NULL;
  line_number = 0;
  last_token_type = token_type_semicolon;
  token_stack_free (&token_stack);
  eaten_here = 0;
  end_of_file = true;
}