Source

fparsec / Doc / html / tutorial.html

Full commit
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Tutorial</title>
 <link rel="stylesheet" type="text/css" media="all" href="css/style.css" />
 <link rel="stylesheet" type="text/css" media="screen" href="css/screen-sidebar.css" />
 <!--[if lt IE 9]>
 <link rel="stylesheet" type="text/css" media="all" href="css/style-ie.css" />
 <![endif]-->
 <!--[if IE 6]>
 <link rel="stylesheet" type="text/css" media="all" href="css/style-ie6.css" />
 <![endif]-->
 <link rel="stylesheet" type="text/css" media="print" href="css/print.css" />
</head>
<body>
 <div id="fixed-layer">
 <div id="fixed-wrapper">
 <div id="sidebar">
  <div id="top-links"><span><a href="http://bitbucket.org/fparsec/main">FParsec @ BitBucket</a> | <a href="https://bitbucket.org/fparsec/main/issues">Report a bug</a> | <a href="mailto:fparsec [at] quanttec.com?subject=FParsec&amp;body=Hello Stephan,%0A%0A[your feedback]">Feedback</a></span></div>
  <div id="nav-tree">
   <table class="nav n1">
    <tbody class="nav-open n1">
     <tr class="nav-entry n1 _1">
      <td class="nav-number n1"></td>
      <td class="nav-title n1"><a href="index.html">FParsec Documentation</a></td>
     </tr>
     <tr class="nav-subentries n1 _1">
      <td class="nav-subentries-number n1"></td>
      <td class="nav-subentries n1">
       <table class="nav n2">
        <tbody class="nav-before-open n2">
         <tr class="nav-entry n2 _1">
          <td class="nav-number n2"><a href="about/index.html"><span class="section-number">1</span><span class="nav-space"></span></a></td>
          <td class="nav-title n2"><a href="about/index.html">About FParsec</a></td>
         </tr>
         <tr class="nav-entry n2 _2">
          <td class="nav-number n2"><a href="license.html"><span class="section-number">2</span><span class="nav-space"></span></a></td>
          <td class="nav-title n2"><a href="license.html">License</a></td>
         </tr>
         <tr class="nav-entry n2 _3">
          <td class="nav-number n2">
           <a href="download-and-installation.html"><span class="section-number">3</span><span class="nav-space"></span></a>
          </td>
          <td class="nav-title n2"><a href="download-and-installation.html">Download and installation</a></td>
         </tr>
        </tbody>
        <tbody class="nav-open selected n2">
         <tr class="nav-entry selected n2 _4">
          <td class="nav-number selected n2"><a href="#"><span class="section-number">4</span><span class="nav-space"></span></a></td>
          <td class="nav-title selected n2"><a href="#">Tutorial</a></td>
         </tr>
         <tr class="nav-subentries selected n2 _4">
          <td class="nav-subentries-number selected n2"></td>
          <td class="nav-subentries selected n2">
           <table class="nav n3">
            <tbody class="nav-before-open n3">
             <tr class="nav-entry n3 _1">
              <td class="nav-number n3"><a href="#preliminaries"><span class="section-number">1</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#preliminaries">Preliminaries</a></td>
             </tr>
             <tr class="nav-entry n3 _2">
              <td class="nav-number n3">
               <a href="#parsing-a-single-float"><span class="section-number">2</span><span class="nav-space"></span></a>
              </td>
              <td class="nav-title n3"><a href="#parsing-a-single-float">Parsing a single float</a></td>
             </tr>
             <tr class="nav-entry n3 _3">
              <td class="nav-number n3">
               <a href="#parsing-a-float-between-brackets"><span class="section-number">3</span><span class="nav-space"></span></a>
              </td>
              <td class="nav-title n3"><a href="#parsing-a-float-between-brackets">Parsing a float between brackets</a></td>
             </tr>
             <tr class="nav-entry n3 _4">
              <td class="nav-number n3"><a href="#abstracting-parsers"><span class="section-number">4</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#abstracting-parsers">Abstracting parsers</a></td>
             </tr>
             <tr class="nav-entry n3 _5">
              <td class="nav-number n3">
               <a href="#parsing-a-list-of-floats"><span class="section-number">5</span><span class="nav-space"></span></a>
              </td>
              <td class="nav-title n3"><a href="#parsing-a-list-of-floats">Parsing a list of floats</a></td>
             </tr>
             <tr class="nav-entry n3 _6">
              <td class="nav-number n3"><a href="#handling-whitespace"><span class="section-number">6</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#handling-whitespace">Handling whitespace</a></td>
             </tr>
             <tr class="nav-entry n3 _7">
              <td class="nav-number n3"><a href="#parsing-string-data"><span class="section-number">7</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#parsing-string-data">Parsing string data</a></td>
             </tr>
             <tr class="nav-entry n3 _8">
              <td class="nav-number n3">
               <a href="#sequentially-applying-parsers"><span class="section-number">8</span><span class="nav-space"></span></a>
              </td>
              <td class="nav-title n3"><a href="#sequentially-applying-parsers">Sequentially applying parsers</a></td>
             </tr>
             <tr class="nav-entry n3 _9">
              <td class="nav-number n3"><a href="#parsing-alternatives"><span class="section-number">9</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#parsing-alternatives">Parsing alternatives</a></td>
             </tr>
             <tr class="nav-entry n3 _0">
              <td class="nav-number n3"><a href="#fs-value-restriction"><span class="section-number">10</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#fs-value-restriction">F#’s value restriction</a></td>
             </tr>
             <tr class="nav-entry n3 _1">
              <td class="nav-number n3"><a href="#parsing-json"><span class="section-number">11</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#parsing-json">Parsing JSON</a></td>
             </tr>
             <tr class="nav-entry n3 _2">
              <td class="nav-number n3"><a href="#what-now"><span class="section-number">12</span><span class="nav-space"></span></a></td>
              <td class="nav-title n3"><a href="#what-now">What now?</a></td>
             </tr>
            </tbody>
           </table>
          </td>
         </tr>
        </tbody>
        <tbody class="nav-after-open n2">
         <tr class="nav-entry n2 _5">
          <td class="nav-number n2"><a href="users-guide/index.html"><span class="section-number">5</span><span class="nav-space"></span></a></td>
          <td class="nav-title n2"><a href="users-guide/index.html">User’s Guide</a></td>
         </tr>
         <tr class="nav-entry n2 _6">
          <td class="nav-number n2"><a href="reference/index.html"><span class="section-number">6</span><span class="nav-space"></span></a></td>
          <td class="nav-title n2"><a href="reference/index.html">Reference</a></td>
         </tr>
        </tbody>
       </table>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <div id="copyright">
    <span>Copyright © 2012 <a href="about/contact.html">Stephan Tolksdorf</a></span>
  </div>
 </div>
 </div>
 </div>
 <div id="wrapper">
 <div id="main">
 <div id="main-content">
 <div id="breadcrumbs">
  <span class="breadcrumbs">
   <span id="breadcrumbs-parents"><a href="index.html">FParsec Documentation</a></span><span class="breadcrumbs-sep"> > </span>Tutorial
  </span>
 </div>
 <div class="section s1">
  <h1 class="title h1"><span><span class="section-number">4</span> Tutorial</span></h1>
  <div class="intro i1">
   <div class="para _1">
    <p>
     This tutorial introduces you to the basic concepts of FParsec. Our goal is to give you an intuition for how you can build parser applications
     using the FParsec library. We will only cover the basic ideas and only cursorily explore FParsec’s API, but hopefully we will cover enough ground
     to enable you to further explore FParsec with the help of the <a href="users-guide/index.html">user’s guide</a>, the <a
     href="reference/index.html">API reference</a> and the sample parsers in the <span class="tt">Samples</span> folder.
    </p>
   </div>
   <div class="para _2">
    <p>
     <em>A Japanese translation of this tutorial by Gab_km is available <a href="http://blog.livedoor.jp/gab_km/archives/1437534.html">here</a>.</em>
    </p>
   </div>
   <div class="para _3 lcinp">
    <div class="local-toc">
     <div class="toc-toc-title">
      Contents
     </div>
     <table class="toc t1">
      <tr class="toc-entry toc t1 _0">
       <td class="toc-number toc t1"><a href="#preliminaries"><span class="toc-number">1</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#preliminaries">Preliminaries</a></td>
      </tr>
      <tr class="toc-entry toc t1 _1">
       <td class="toc-number toc t1"><a href="#parsing-a-single-float"><span class="toc-number">2</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#parsing-a-single-float">Parsing a single float</a></td>
      </tr>
      <tr class="toc-entry toc t1 _2">
       <td class="toc-number toc t1">
        <a href="#parsing-a-float-between-brackets"><span class="toc-number">3</span><span class="toc-space"></span></a>
       </td>
       <td class="toc-title toc t1"><a href="#parsing-a-float-between-brackets">Parsing a float between brackets</a></td>
      </tr>
      <tr class="toc-entry toc t1 _3">
       <td class="toc-number toc t1"><a href="#abstracting-parsers"><span class="toc-number">4</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#abstracting-parsers">Abstracting parsers</a></td>
      </tr>
      <tr class="toc-entry toc t1 _4">
       <td class="toc-number toc t1"><a href="#parsing-a-list-of-floats"><span class="toc-number">5</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#parsing-a-list-of-floats">Parsing a list of floats</a></td>
      </tr>
      <tr class="toc-entry toc t1 _5">
       <td class="toc-number toc t1"><a href="#handling-whitespace"><span class="toc-number">6</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#handling-whitespace">Handling whitespace</a></td>
      </tr>
      <tr class="toc-entry toc t1 _6">
       <td class="toc-number toc t1"><a href="#parsing-string-data"><span class="toc-number">7</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#parsing-string-data">Parsing string data</a></td>
      </tr>
      <tr class="toc-entry toc t1 _7">
       <td class="toc-number toc t1">
        <a href="#sequentially-applying-parsers"><span class="toc-number">8</span><span class="toc-space"></span></a>
       </td>
       <td class="toc-title toc t1"><a href="#sequentially-applying-parsers">Sequentially applying parsers</a></td>
      </tr>
      <tr class="toc-entry toc t1 _8">
       <td class="toc-number toc t1"><a href="#parsing-alternatives"><span class="toc-number">9</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#parsing-alternatives">Parsing alternatives</a></td>
      </tr>
      <tr class="toc-entry toc t1 _9">
       <td class="toc-number toc t1"><a href="#fs-value-restriction"><span class="toc-number">10</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#fs-value-restriction">F#’s value restriction</a></td>
      </tr>
      <tr class="toc-entry toc t1 _0">
       <td class="toc-number toc t1"><a href="#parsing-json"><span class="toc-number">11</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#parsing-json">Parsing JSON</a></td>
      </tr>
      <tr class="toc-entry toc t1 _1">
       <td class="toc-number toc t1"><a href="#what-now"><span class="toc-number">12</span><span class="toc-space"></span></a></td>
       <td class="toc-title toc t1"><a href="#what-now">What now?</a></td>
      </tr>
     </table>
    </div>
   </div>
  </div>
  <div id="preliminaries" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.1</span> Preliminaries</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      FParsec is built as two DLLs: <span class="tt">FParsec.dll</span> and <span class="tt">FParsecCS.dll</span>. The easiest way to build these DLLs
      is to use the Visual Studio solution files in the folders <span class="tt">Build/VS9</span> (for Visual Studio 2008) and <span
      class="tt">Build/VS10</span> (for Visual Studio 2010). Any project that uses FParsec has to reference both DLLs. See <a
      href="download-and-installation.html">Download and Installation</a> for more details.
     </p>
    </div>
    <div class="para _2 lcinp">
     <p>
      All FParsec types and modules are declared in the <code class="fsharp"><span class="ci">FParsec</span></code> namespace. This namespace contains
      some basic classes (such as <code class="fsharp"><a href="reference/charstream.html#CharStream"><span class="ci">CharStream</span></a></code>
      and <code class="fsharp"><a href="reference/reply.html"><span class="ci">Reply</span></a></code>) and four F# modules, namely
     </p>
     <ul class="l1">
      <li class="_1">
       <code class="fsharp"><a href="reference/primitives.html"><span class="ci">Primitives</span></a></code>, containing basic type definitions and
       parser combinators,
      </li>
      <li class="_2">
       <code class="fsharp"><a href="reference/charparsers.html"><span class="ci">CharParsers</span></a></code>, containing parsers for chars, strings
       and numbers, and functions for applying parsers to input streams,
      </li>
      <li class="_3">
       <code class="fsharp"><a href="reference/primitives.html#members.Error"><span class="ci">Error</span></a></code>, containing types and helper
       functions for creating, processing and formatting parser error messages,
      </li>
      <li class="_4">
       <code class="fsharp"><a href="reference/staticmapping.html"><span class="ci">StaticMapping</span></a></code>, containing functions for
       compiling static key‐value mappings into optimized functions.
      </li>
     </ul>
    </div>
    <div class="para _3">
     <p>All code snippets in this tutorial assume that you’ve opened the <code class="fsharp"><span class="ci">FParsec</span></code> namespace:</p>
<pre class="code fsharp"><span class="ck">open</span> <span class="ci">FParsec</span>
</pre>
     <p>
      Opening the <code class="fsharp"><span class="ci">FParsec</span></code> namespace also automatically opens the <code class="fsharp"><a
      href="reference/primitives.html"><span class="ci">Primitives</span></a></code>, <code class="fsharp"><a href="reference/charparsers.html"><span
      class="ci">CharParsers</span></a></code> and <code class="fsharp"><a href="reference/error.html"><span class="ci">Error</span></a></code>
      modules.
     </p>
    </div>
    <div class="para _4 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         All code snippets in this tutorial are contained in the <span class="tt">Samples/Tutorial</span> project. Having this project open while
         reading the tutorial can be quite helpful. For example, you can hover the mouse over an identifier to get an Intellisense popup with the
         inferred type. And if you’re curious how a library function is implemented, you can click the <span class="i">Go to definition</span> context
         menu option to view its source code.
        </p>
       </div>
      </div>
     </div>
    </div>
   </div>
  </div>
  <div id="parsing-a-single-float" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.2</span> Parsing a single float</span></h2>
   <div class="intro i2">
    <div class="para _1 lcinp">
     <p>Parsing input with FParsec involves two steps:</p>
     <ol class="l1">
      <li class="_1">building a parser and</li>
      <li class="_2">applying the parser to the input.</li>
     </ol>
    </div>
    <div class="para _2">
     <p>Let’s start with a simple example: parsing a single floating‐point number in a string.</p>
    </div>
    <div class="para _3 lcinp">
     <p>
      In this case the first step, building the parser, is trivial, because the <code class="fsharp"><a href="reference/charparsers.html"><span
      class="ci">CharParsers</span></a></code> module already comes with a built‐in float parser:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ci">float</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span></pre>
    </div>
    <div class="para _4">
     <p>
      The generic type <code class="fsharp"><a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span
      class="cp">&lt;</span><span class="ctv">'Result</span><span class="cp">,</span><span class="ctv">'UserState</span><span
      class="cp">&gt;</span></code> is the type of all parsers in FParsec. If you follow the hyperlink into the reference, you’ll see that <code
      class="fsharp"><a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a></code> is a type abbreviation for a
      function type. However, at this point we don’t need to go into the details of the <code class="fsharp"><a
      href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a></code> type. It’s enough to note that the first type argument
      represents the type of the parser result. Thus, in the case of <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span
      class="ci">pfloat</span></a></code> the type tells us that if the parser succeeds it returns a floating‐point number of type <code
      class="fsharp"><span class="ci">float</span></code>. We won’t use a &#x201C;user state&#x201D; in this tutorial, so you can just ignore the
      second type argument for the time being.
     </p>
    </div>
    <div class="para _5 lcinp">
     <p>
      To apply the <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> parser to a
      string, we can use the <code class="fsharp"><a href="reference/charparsers.html#members.run"><span class="ci">run</span></a></code> function
      from the <code class="fsharp"><a href="reference/charparsers.html"><span class="ci">CharParsers</span></a></code> module:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/charparsers.html#members.run"><span class="ci">run</span></a><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'Result</span><span class="cp">,</span> <span class="ci">unit</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <span class="ci">string</span> <span class="cr">-&gt;</span> <a href="reference/charparsers.html#members.ParserResult"><span class="ci">ParserResult</span></a><span class="cp">&lt;</span><span class="ctv">'Result</span><span class="cp">,</span><span class="ci">unit</span><span class="cp">&gt;</span></pre>
    </div>
    <div class="para _6">
     <p>
      <code class="fsharp"><a href="reference/charparsers.html#members.run"><span class="ci">run</span></a></code> is the simplest function out of <a
      href="reference/charparsers.html#interface.runparser-functions">several</a> provided by the <code class="fsharp"><a
      href="reference/charparsers.html"><span class="ci">CharParsers</span></a></code> module for running parsers on input. Other functions allow you,
      for example, to run parsers directly on the contents of a file or a <code class="fsharp"><a
      href="http://msdn.microsoft.com/en-us/library/system.io.stream.aspx"><span class="ci">System</span><span class="cm">.</span><span
      class="ci">IO</span><span class="cm">.</span><span class="ci">Stream</span></a></code>.
     </p>
    </div>
    <div class="para _7">
     <p>
      <code class="fsharp"><a href="reference/charparsers.html#members.run"><span class="ci">run</span></a></code> applies the parser passed as the
      first argument to the string passed as the second argument and returns the return value of the parser in form of a <code class="fsharp"><a
      href="reference/charparsers.html#members.ParserResult"><span class="ci">ParserResult</span></a></code> value. The <code class="fsharp"><a
      href="reference/charparsers.html#members.ParserResult"><span class="ci">ParserResult</span></a></code> type is a discriminated union type with
      the two cases: <code class="fsharp"><a href="reference/charparsers.html#members.Success"><span class="ci">Success</span></a></code> and <code
      class="fsharp"><a href="reference/charparsers.html#members.Failure"><span class="ci">Failure</span></a></code>. In case the parser succeeds, the
      <code class="fsharp"><a href="reference/charparsers.html#members.ParserResult"><span class="ci">ParserResult</span></a></code> value contains
      the result value, otherwise it contains an error message.
     </p>
    </div>
    <div class="para _8 lcinp">
     <p>To simplify testing we write a little helper function that prints the result value or error message:</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">test</span> <span class="ci">p</span> <span class="ci">str</span> <span class="cp">=</span>
    <span class="ck">match</span> <a href="reference/charparsers.html#members.run"><span class="ci">run</span></a> <span class="ci">p</span> <span class="ci">str</span> <span class="ck">with</span>
    <span class="cp">|</span> <a href="reference/charparsers.html#members.Success"><span class="ci">Success</span></a><span class="cp">(</span><span class="ci">result</span><span class="cp">,</span> <span class="ci">_</span><span class="cp">,</span> <span class="ci">_</span><span class="cp">)</span>   <span class="cr">-&gt;</span> <span class="ci">printfn</span> <span class="cs"><span class="cld">"</span>Success: %A<span class="crd">"</span></span> <span class="ci">result</span>
    <span class="cp">|</span> <a href="reference/charparsers.html#members.Failure"><span class="ci">Failure</span></a><span class="cp">(</span><span class="ci">errorMsg</span><span class="cp">,</span> <span class="ci">_</span><span class="cp">,</span> <span class="ci">_</span><span class="cp">)</span> <span class="cr">-&gt;</span> <span class="ci">printfn</span> <span class="cs"><span class="cld">"</span>Failure: %s<span class="crd">"</span></span> <span class="ci">errorMsg</span>
</pre>
    </div>
    <div class="para _9 lcinp">
     <p>
      With this helper function in place, we can test <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span
      class="ci">pfloat</span></a></code> by executing
     </p>
<pre class="code fsharp"><span class="ci">test</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <span class="cs"><span class="cld">"</span>1.25<span class="crd">"</span></span></pre>
     <p>which produces the output</p>
<pre class="code fsharp"><span class="cout">Success: 1.25</span></pre>
    </div>
    <div class="para _0 lcinp">
     <p>
      Testing <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> with a number literal
      that has an invalid exponent
     </p>
<pre class="code fsharp"><span class="ci">test</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <span class="cs"><span class="cld">"</span>1.25E 3<span class="crd">"</span></span></pre>
     <p>yields the error message</p>
<pre class="code fsharp"><span class="cout">Failure: Error in Ln: 1 Col: 6
1.25E 3
     ^
Expecting: decimal digit
</span></pre>
    </div>
   </div>
  </div>
  <div id="parsing-a-float-between-brackets" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.3</span> Parsing a float between brackets</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      Implementing parsers with FParsec typically means combining higher‐level parsers from lower‐level ones. You start with the parser primitives
      provided by the library and then successively combine these into higher‐level parsers until you finally have a single parser for the complete
      input.
     </p>
    </div>
    <div class="para _2 lcinp">
     <p>
      In the following sections we will illustrate this approach by discussing various sample parsers that build on each other. In this section we
      will begin with a very simple parser for a floating‐point number between brackets:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">str</span> <span class="ci">s</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="ci">s</span>
<span class="ck">let</span> <span class="ci">floatBetweenBrackets</span> <span class="cp">=</span> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _3 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         If you’re trying to compile this or another code snippet and you get a compiler error mentioning F#’s &#x201C;value restriction&#x201D;,
         please see <a href="#fs-value-restriction">section 4.10</a>.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _4">
     <p>
      The definition of <code class="fsharp"><span class="ci">str</span></code> and <code class="fsharp"><span
      class="ci">floatBetweenBrackets</span></code> involves three library functions that we haven’t yet introduced: <code class="fsharp"><a
      href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code>, <code class="fsharp"><a
      href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a></code> and <code class="fsharp"><a
      href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a></code>.
     </p>
    </div>
    <div class="para _5">
     <p>The function</p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a><span class="cp">:</span> <span class="ci">string</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ci">string</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span></pre>
     <p>
      takes a string as the argument and returns a parser for that string. When this parser is applied to an input stream it checks whether the
      following chars in the input stream match the given string. If the chars match the complete string, the parser consumes them, i.e. skips over
      them. Otherwise it fails without consuming any input. When the parser succeeds, it also returns the given string as the parser result, but since
      the string is a constant, you’ll rarely make use of the result.
     </p>
    </div>
    <div class="para _6">
     <p>
      The <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code> function isn’t named
      <code class="fsharp"><span class="ci">string</span></code> because otherwise it would hide the built‐in F# function <code class="fsharp"><span
      class="ci">string</span></code>. In general, parser names in FParsec that would otherwise conflict with built‐in F# function names are prefixed
      by a single p char. <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> is
      another example for this naming convention.
     </p>
    </div>
    <div class="para _7">
     <p>
      To save a few keystrokes we abbreviate <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span
      class="ci">pstring</span></a></code> as <code class="fsharp"><span class="ci">str</span></code>. So, for instance, <code class="fsharp"><span
      class="ci">str</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span></code> is a parser that skips over the char
      <code class="fsharp"><span class="cc"><span class="cld">'</span>[<span class="crd">'</span></span></code>.
     </p>
    </div>
    <div class="para _8 lcinp">
     <p>
      The binary operators <code class="fsharp"><a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a></code>
      and <code class="fsharp"><span class="co">&lt;&lt;.</span></code> have the following types:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <span class="cp">(</span><a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a><span class="cp">)</span><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'b</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'b</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span>
<span class="ck">val</span> <span class="cp">(</span><a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a><span class="cp">)</span><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'b</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span>
</pre>
    </div>
    <div class="para _9">
     <p>
      As you can see from these signatures, both operators are parser combinators that construct a new parser from the two argument parsers. The
      parser <code class="fsharp"><span class="ci">p1</span> <a href="reference/primitives.html#members.:62::62:.."><span
      class="co">&gt;&gt;.</span></a> <span class="ci">p2</span></code> parses <code class="fsharp"><span class="ci">p1</span></code> and <code
      class="fsharp"><span class="ci">p2</span></code> in sequence and returns the result of <code class="fsharp"><span class="ci">p2</span></code>.
      The parser <code class="fsharp"><span class="ci">p1</span> <a href="reference/primitives.html#members...:62::62:"><span
      class="co">.&gt;&gt;</span></a> <span class="ci">p2</span></code> also parses <code class="fsharp"><span class="ci">p1</span></code> and <code
      class="fsharp"><span class="ci">p2</span></code> in sequence, but it returns the result of <code class="fsharp"><span
      class="ci">p1</span></code> instead of <code class="fsharp"><span class="ci">p2</span></code>. In each case the point points to the side of the
      parser whose result is returned. By combining both operators in <code class="fsharp"><span class="ci">p1</span> <a
      href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">p2</span> <a
      href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">p3</span></code> we obtain a parser
      that parses <code class="fsharp"><span class="ci">p1</span></code>, <code class="fsharp"><span class="ci">p2</span></code> and <code
      class="fsharp"><span class="ci">p3</span></code> in sequence and returns the result from <code class="fsharp"><span class="ci">p2</span></code>.
     </p>
    </div>
    <div class="para _0 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         With the somewhat imprecise wording &#x201C;parses <code class="fsharp"><span class="ci">p1</span></code> and <code class="fsharp"><span
         class="ci">p2</span></code> in sequence&#x201D; we actually mean: The parser <code class="fsharp"><span class="ci">p1</span></code> is
         applied to the input and if <code class="fsharp"><span class="ci">p1</span></code> succeeds then <code class="fsharp"><span
         class="ci">p2</span></code> is applied to the remaining input; in case any of the two element parsers fails, the aggregate parser immediately
         propagates the error message.
        </p>
       </div>
       <div class="para _2">
        <p>
         In the documentation for FParsec we often use expressions such as &#x201C;parses <code class="fsharp"><span
         class="ci">p</span></code>&#x201D; or &#x201C;parses an occurrence of <code class="fsharp"><span class="ci">p</span></code>&#x201D; instead
         of the technically more accurate &#x201C;applies the parser <code class="fsharp"><span class="ci">p</span></code> to the remaining input and
         if <code class="fsharp"><span class="ci">p</span></code> succeeds …&#x201D;, hoping that the exact meaning is obvious from the context.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _1">
     <p>
      The following tests show that <code class="fsharp"><span class="ci">floatBetweenBrackets</span></code> parses valid input as expected and
      produces informative error messages when it encounters invalid input:
     </p>
    </div>
    <div class="para _2 lcinp">
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatBetweenBrackets</span> <span class="cs"><span class="cld">"</span>[1.0]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: 1.0

</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatBetweenBrackets</span> <span class="cs"><span class="cld">"</span>[]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 2
[]
 ^
Expecting: floating-point number

</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatBetweenBrackets</span> <span class="cs"><span class="cld">"</span>[1.0<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 5
[1.0
    ^
Note: The error occurred at the end of the input stream.
Expecting: ']'
</span></pre>
    </div>
   </div>
  </div>
  <div id="abstracting-parsers" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.4</span> Abstracting parsers</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>One of FParsec’s greatest strenghts is the ease with which you can define your own parser abstractions.</p>
    </div>
    <div class="para _2 lcinp">
     <p>
      Take for instance the <code class="fsharp"><span class="ci">floatBetweenBrackets</span></code> from the previous section. If you intend to also
      parse other elements between strings, you could define your own specialized combinator for this purpose:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">betweenStrings</span> <span class="ci">s1</span> <span class="ci">s2</span> <span class="ci">p</span> <span class="cp">=</span> <span class="ci">str</span> <span class="ci">s1</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">p</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">str</span> <span class="ci">s2</span>
</pre>
    </div>
    <div class="para _3 lcinp">
     <p>
      You could then define <code class="fsharp"><span class="ci">floatInBrackets</span></code> and other parsers with the help of this combinator:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">floatBetweenBrackets</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <span class="co">|&gt;</span> <span class="ci">betweenStrings</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span>
<span class="ck">let</span> <span class="ci">floatBetweenDoubleBrackets</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <span class="co">|&gt;</span> <span class="ci">betweenStrings</span> <span class="cs"><span class="cld">"</span>[[<span class="crd">"</span></span> <span class="cs"><span class="cld">"</span>]]<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _4 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         In case you’re new to F#:<br /> <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span
         class="ci">pfloat</span></a> <span class="co">|&gt;</span> <span class="ci">betweenStrings</span> <span class="cs"><span
         class="cld">"</span>[<span class="crd">"</span></span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span></code>
         is just another way to write <code class="fsharp"><span class="ci">betweenStrings</span> <span class="cs"><span class="cld">"</span>[<span
         class="crd">"</span></span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span> <a
         href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code>.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _5 lcinp">
     <p>
      Once you notice that you frequently need to apply a parser between two others, you could go a step further and factor <code class="fsharp"><span
      class="ci">betweenStrings</span></code> as follows:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">between</span> <span class="ci">pBegin</span> <span class="ci">pEnd</span> <span class="ci">p</span>  <span class="cp">=</span> <span class="ci">pBegin</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">p</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">pEnd</span>
<span class="ck">let</span> <span class="ci">betweenStrings</span> <span class="ci">s1</span> <span class="ci">s2</span> <span class="ci">p</span> <span class="cp">=</span> <span class="ci">p</span> <span class="co">|&gt;</span> <span class="ci">between</span> <span class="cp">(</span><span class="ci">str</span> <span class="ci">s1</span><span class="cp">)</span> <span class="cp">(</span><span class="ci">str</span> <span class="ci">s2</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _6">
     <p>
      Actually, you don’t need to define <code class="fsharp"><a href="reference/primitives.html#members.between"><span
      class="ci">between</span></a></code>, because this is already a built‐in FParsec combinator.
     </p>
    </div>
    <div class="para _7">
     <p>
      These are all trivial examples, of course. But since FParsec is merely an F# library and not some external parser generator tool, there are no
      limits to the abstractions you can define. You can write functions that take whatever input you need, do some arbitrarily complex computations
      on the input and then return a special purpose parser or parser combinator.
     </p>
    </div>
    <div class="para _8">
     <p>
      For example, you could write a function that takes a regular‐expression pattern as the input and returns a <code class="fsharp"><a
      href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a></code> for parsing input conforming to that pattern. This
      function could use another parser to parse the pattern into an AST and then compile this AST into a special‐purpose parser function.
      Alternatively, it could construct a .NET regular expression from the pattern and then return a parser function that uses FParsec’s <code
      class="fsharp"><a href="reference/charstream.html#CharStream"><span class="ci">CharStream</span></a></code> API to directly apply the regex to
      the input stream (which is what the built‐in <code class="fsharp"><a href="reference/charparsers.html#members.regex"><span
      class="ci">regex</span></a></code> parser actually does).
     </p>
    </div>
    <div class="para _9">
     <p>
      Another example are extensible parser applications. By storing parser functions in dictionaries or other data structures and defining an
      appropriate extension protocol, you could allow plugins to dynamically register new parsers or modify existing ones.
     </p>
    </div>
    <div class="para _0">
     <p>
      The possibilities are really endless. But before you can fully exploit these possibilities, you first need to be familiar with the fundamentals
      of FParsec.
     </p>
    </div>
   </div>
  </div>
  <div id="parsing-a-list-of-floats" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.5</span> Parsing a list of floats</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      We’ve already spent three sections on discussing how to parse a single floating‐point number, so it’s about time we try something more
      ambitious: parsing a list of floating‐point numbers.
     </p>
    </div>
    <div class="para _2">
     <p>
      Let us first assume that we need to parse a sequence of floating‐point numbers in brackets, i.e. text in the following EBNF format: <code
      class="other">("[" float "]")*</code>. Valid input strings in this format are for example: <code class="fsharp"><span class="cs"><span
      class="cld">"</span><span class="crd">"</span></span></code>, <code class="fsharp"><span class="cs"><span class="cld">"</span>[1.0]<span
      class="crd">"</span></span></code>, <code class="fsharp"><span class="cs"><span class="cld">"</span>[2][3][4]<span
      class="crd">"</span></span></code>.
     </p>
    </div>
    <div class="para _3 lcinp">
     <p>
      Since we already have a parser for a float between brackets, we only need a way to repeatedly apply this parser to parse a sequence. This is
      what the <code class="fsharp"><a href="reference/primitives.html#members.many"><span class="ci">many</span></a></code> combinator is for:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/primitives.html#members.many"><span class="ci">many</span></a><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span> <span class="ci">list</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span></pre>
    </div>
    <div class="para _4">
     <p>
      The parser <code class="fsharp"><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span
      class="ci">p</span></code> repeatedly applies the parser <code class="fsharp"><span class="ci">p</span></code> until <code class="fsharp"><span
      class="ci">p</span></code> fails, i.e. it &#x201C;greedily&#x201D; parses as many occurrences of <code class="fsharp"><span
      class="ci">p</span></code> as possible. The results of <code class="fsharp"><span class="ci">p</span></code> are returned as a list in the order
      of occurrence.
     </p>
    </div>
    <div class="para _5 lcinp">
     <p>
      Some simple tests show that <code class="fsharp"><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span
      class="ci">floatInBrackets</span></code> works as expected:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span class="ci">floatBetweenBrackets</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span><span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: []
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span class="ci">floatBetweenBrackets</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>[1.0]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: [1.0]
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span class="ci">floatBetweenBrackets</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>[2][3][4]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: [2.0; 3.0; 4.0]
</span></pre>
    </div>
    <div class="para _6 lcinp">
     <p>
      If <code class="fsharp"><span class="ci">floatBetweenBrackets</span></code> fails <em>after consuming input</em>, then the combined parser fails
      too:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span class="ci">floatBetweenBrackets</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>[1][2.0E]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 9
[1][2.0E]
        ^
Expecting: decimal digit
</span></pre>
    </div>
    <div class="para _7 lcinp">
     <p>
      Note that <code class="fsharp"><a href="reference/primitives.html#members.many"><span class="ci">many</span></a></code> also succeeds for an
      empty sequence. If you want to require at least one element, you can use <code class="fsharp"><a
      href="reference/primitives.html#members.many1"><span class="ci">many1</span></a></code> instead:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many1"><span class="ci">many1</span></a> <span class="ci">floatBetweenBrackets</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>(1)<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 1
(1)
^
Expecting: '['
</span></pre>
    </div>
    <div class="para _8 lcinp">
     <div class="admonition">
      <div class="admonition-title">Tip</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         If you’d prefer the last error message to be worded in terms of the higher level <code class="fsharp"><span
         class="ci">floatBetweenBrackets</span></code> parser instead of the lower level <code class="fsharp"><span class="ci">str</span> <span
         class="cs"><span class="cld">"</span>[<span class="crd">"</span></span></code> parser, you could use the <code class="fsharp"><a
         href="reference/primitives.html#members.:60::63::62:"><span class="co">&lt;?&gt;</span></a></code> operator as in the following example:
        </p>
       </div>
       <div class="para _2 lcinp">
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many1"><span class="ci">many1</span></a> <span class="cp">(</span><span class="ci">floatBetweenBrackets</span> <a href="reference/primitives.html#members.:60::63::62:"><span class="co">&lt;?&gt;</span></a> <span class="cs"><span class="cld">"</span>float between brackets<span class="crd">"</span></span><span class="cp">)</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>(1)<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 1
(1)
^
Expecting: float between brackets
</span></pre>
       </div>
       <div class="para _3">
        <p>
         Please see <a href="users-guide/customizing-error-messages.html">section 5.8</a> of the user’s guide to learn more about customizing error
         messages.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _9">
     <p>
      If you just want to skip over a sequence and don’t need the list of parser results, you could use the optimized combinators <code
      class="fsharp"><a href="reference/primitives.html#members.skipMany"><span class="ci">skipMany</span></a></code> or <code class="fsharp"><a
      href="reference/primitives.html#members.skipMany1"><span class="ci">skipMany1</span></a></code> instead of <code class="fsharp"><a
      href="reference/primitives.html#members.many"><span class="ci">many</span></a></code> and <code class="fsharp"><a
      href="reference/primitives.html#members.many1"><span class="ci">many1</span></a></code>.
     </p>
    </div>
    <div class="para _0 lcinp">
     <p>
      Another frequently used combinator for parsing sequences is <code class="fsharp"><a href="reference/primitives.html#members.sepBy"><span
      class="ci">sepBy</span></a></code>:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'b</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span> <span class="ci">list</span><span class="cp">,</span> <span class="ctv">'u</span><span class="cp">&gt;</span></pre>
    </div>
    <div class="para _1">
     <p>
      <code class="fsharp"><a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a></code> takes an &#x201C;element&#x201D;
      parser and a &#x201C;separator&#x201D; parser as the arguments and returns a parser for a list of elements separated by separators. In EBNF
      notation <code class="fsharp"><a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a> <span
      class="ci">p</span> <span class="ci">pSep</span></code> could be written as <code class="other">(p (pSep p)*)?</code>. Similar to <code
      class="fsharp"><a href="reference/primitives.html#members.many"><span class="ci">many</span></a></code>, there are <a
      href="reference/primitives.html#interface.sepBy-parsers">several variants</a> of <code class="fsharp"><a
      href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a></code>.
     </p>
    </div>
    <div class="para _2 lcinp">
     <p>
      With the help of <code class="fsharp"><a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a></code> we can parse a
      more readable list format, where floating‐point numbers are separated by a comma:
     </p>
<pre class="code other">floatList: "[" (float ("," float)*)? "]"</pre>
    </div>
    <div class="para _3">
     <p>
      Valid input strings in this format are for example: <code class="fsharp"><span class="cs"><span class="cld">"</span>[]<span
      class="crd">"</span></span></code>, <code class="fsharp"><span class="cs"><span class="cld">"</span>[1.0]<span
      class="crd">"</span></span></code>, <code class="fsharp"><span class="cs"><span class="cld">"</span>[2,3,4]<span
      class="crd">"</span></span></code>.
     </p>
    </div>
    <div class="para _4 lcinp">
     <p>The straightforward implementation of this format is</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">floatList</span> <span class="cp">=</span> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span>,<span class="crd">"</span></span><span class="cp">)</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _5 lcinp">
     <p>Testing <code class="fsharp"><span class="ci">floatList</span></code> with valid test strings yields the expected result:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatList</span> <span class="cs"><span class="cld">"</span>[]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: []
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatList</span> <span class="cs"><span class="cld">"</span>[1.0]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: [1.0]
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatList</span> <span class="cs"><span class="cld">"</span>[4,5,6]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: [4.0; 5.0; 6.0]
</span></pre>
    </div>
    <div class="para _6 lcinp">
     <p>Testing with invalid input shows that <code class="fsharp"><span class="ci">floatList</span></code> produces helpful error messages:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatList</span> <span class="cs"><span class="cld">"</span>[1.0,]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 6
[1.0,]
     ^
Expecting: floating-point number

</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatList</span> <span class="cs"><span class="cld">"</span>[1.0,2.0<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 9
[1.0,2.0
        ^
Note: The error occurred at the end of the input stream.
Expecting: ',' or ']'
</span></pre>
    </div>
   </div>
  </div>
  <div id="handling-whitespace" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.6</span> Handling whitespace</span></h2>
   <div class="intro i2">
    <div class="para _1 lcinp">
     <p>
      FParsec treats whitespace (spaces, tabs, newlines, etc) just as any other input, so our <code class="fsharp"><span
      class="ci">floatList</span></code> parser can’t yet deal with whitespace:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">floatBetweenBrackets</span> <span class="cs"><span class="cld">"</span>[1.0, 2.0]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 5
[1.0, 2.0]
    ^
Expecting: ']'
</span></pre>
    </div>
    <div class="para _2">
     <p>If we want the parser to ignore whitespace, we need to make this explicit in the parser definition.</p>
    </div>
    <div class="para _3 lcinp">
     <p>
      First, we need to define what we want to accept as whitespace. For simplicity we will just use the built‐in <code class="fsharp"><a
      href="reference/charparsers.html#members.spaces"><span class="ci">spaces</span></a></code> parser, which skips over any (possibly empty)
      sequence of <code class="fsharp"><span class="cc"><span class="cld">'</span> <span class="crd">'</span></span></code>, <code
      class="fsharp"><span class="cc"><span class="cld">'</span><span class="ce">\t</span><span class="crd">'</span></span></code>, <code
      class="fsharp"><span class="cc"><span class="cld">'</span><span class="ce">\r</span><span class="crd">'</span></span></code> or <code
      class="fsharp"><span class="cc"><span class="cld">'</span><span class="ce">\n</span><span class="crd">'</span></span></code> chars.
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">ws</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.spaces"><span class="ci">spaces</span></a>
</pre>
    </div>
    <div class="para _4">
     <p>
      Next, we need to insert the <code class="fsharp"><span class="ci">ws</span></code> parser at every point where we want to ignore whitespace. In
      general it’s best to skip whitespace <em>after</em> one parses elements, i.e. skip trailing instead of leading whitespace, because that reduces
      the need for backtracking (which will be explained below). Hence, we insert <code class="fsharp"><span class="ci">ws</span></code> at two places
      to skip over any whitespace after brackets or numbers:
     </p>
    </div>
    <div class="para _5 lcinp">
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">str_ws</span> <span class="ci">s</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="ci">s</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">ws</span>
<span class="ck">let</span> <span class="ci">float_ws</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">ws</span>
<span class="ck">let</span> <span class="ci">numberList</span> <span class="cp">=</span> <span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a> <span class="ci">float_ws</span> <span class="cp">(</span><span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>,<span class="crd">"</span></span><span class="cp">)</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _6 lcinp">
     <p>A simple test shows that <code class="fsharp"><span class="ci">numberList</span></code> ignores whitespace:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">numberList</span> <span class="cs"><span class="cld">@"</span>[ 1 ,
                          2 ] <span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: [1.0; 2.0]
</span></pre>
    </div>
    <div class="para _7 lcinp">
     <p>
      If we introduce an error on the second line, we see that <code class="fsharp"><span class="ci">FParsec</span></code> automatically keeps track
      of the line count:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">numberList</span> <span class="cs"><span class="cld">@"</span>[ 1,
                         2; 3]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">
Failure: Error in Ln: 2 Col: 27
                         2; 3]
                          ^
Expecting: ',' or ']'
</span></pre>
    </div>
    <div class="para _8 lcinp">
     <p>
      Our <code class="fsharp"><span class="ci">numberList</span></code> parser still doesn’t skip leading whitespace, because that’s not necessary
      when we put it together with other parsers that skip all trailing whitespace. If we wanted to parse a whole input stream with only a list of
      floating‐point numbers, we could use the following parser:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">numberListFile</span> <span class="cp">=</span> <span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">numberList</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <a href="reference/charparsers.html#members.eof"><span class="ci">eof</span></a>
</pre>
    </div>
    <div class="para _9 lcinp">
     <p>
      The end‐of‐file parser <code class="fsharp"><a href="reference/charparsers.html#members.eof"><span class="ci">eof</span></a></code> will
      generate an error if the end of the stream hasn’t been reached. This is useful for making sure that the complete input gets consumed. Without
      the <code class="fsharp"><a href="reference/charparsers.html#members.eof"><span class="ci">eof</span></a></code> parser the following test
      wouldn’t produce an error:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">numberListFile</span> <span class="cs"><span class="cld">"</span> [1, 2, 3] [4]<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 12
 [1, 2, 3] [4]
           ^
Expecting: end of input
</span></pre>
    </div>
   </div>
  </div>
  <div id="parsing-string-data" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.7</span> Parsing string data</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      FParsec contains various built‐in parsers for chars, strings, numbers and whitespace. In this section we will introduce a few of the char and
      string parsers. For an overview of all available parsers please refer to the <a href="reference/parser-overview.html">parser overview</a> in the
      reference.
     </p>
    </div>
    <div class="para _2">
     <p>
      You’ve already seen several applications of the <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span
      class="ci">pstring</span></a></code> parser (abbreviated as <code class="fsharp"><span class="ci">str</span></code>), which simply skips over a
      constant string in the input. When the <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span
      class="ci">pstring</span></a></code> parser succeeds, it also returns the skipped string as the parser result. The following example
      demonstrates this:
     </p>
    </div>
    <div class="para _3 lcinp">
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/primitives.html#members.many"><span class="ci">many</span></a> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span>a<span class="crd">"</span></span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>b<span class="crd">"</span></span><span class="cp">)</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>abba<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: ["a"; "b"; "b"; "a"]
</span></pre>
    </div>
    <div class="para _4">
     <p>
      In this example we also used the <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span
      class="co">&lt;|&gt;</span></a></code> combinator to combine two alternative parsers. We’ll discuss this combinator in more detail below.
     </p>
    </div>
    <div class="para _5 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         We refer to both <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code> and
         <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span
         class="cld">"</span>a<span class="crd">"</span></span></code> as &#x201C;parsers&#x201D;. Strictly speaking, <code class="fsharp"><a
         href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code> is function taking a string argument and
         returning a <code class="fsharp"><a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a></code>, but it’s more
         convenient to just refer to it as a (parametric) parser.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _6">
     <p>
      When you don’t need the result of the <code class="fsharp"><a href="reference/charparsers.html#members.pstring"><span
      class="ci">pstring</span></a></code> parser, you can alternatively use the <code class="fsharp"><span class="ci">skipString</span></code>
      parser, which returns the <code class="fsharp"><span class="ci">unit</span></code> value <code class="fsharp"><span class="cp">()</span></code>
      instead of the argument string. In this case it doesn’t make any difference to performance whether you use <code class="fsharp"><a
      href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code> or <code class="fsharp"><span
      class="ci">skipString</span></code>, since the returned string is a constant. However, for most other built‐in parsers and combinators you
      should prefer the variants with the &#x201C;skip&#x201D; name prefix when you don’t need the parser result values, because these will generally
      be faster. If you look at the <a href="reference/parser-overview.html">parser overview</a>, you’ll see &#x201C;skip&#x201D; variants for many of
      the built‐in parsers and combinators.
     </p>
    </div>
    <div class="para _7 lcinp">
     <p>
      If you want to parse a <span class="b">c</span>ase <span class="b">i</span>nsensitive string constant you can use <code class="fsharp"><a
      href="reference/charparsers.html#members.pstringCI"><span class="ci">pstringCI</span></a></code> and <code class="fsharp"><a
      href="reference/charparsers.html#members.skipStringCI"><span class="ci">skipStringCI</span></a></code>. For example:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><a href="reference/charparsers.html#members.skipStringCI"><span class="ci">skipStringCI</span></a> <span class="cs"><span class="cld">"</span>&lt;float&gt;<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a><span class="cp">)</span> <span class="cs"><span class="cld">"</span>&lt;FLOAT&gt;1.0<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: 1.0
</span></pre>
    </div>
    <div class="para _8">
     <p>
      Frequently one needs to parse string variables whose chars have to satisfy certain criteria. For instance, identifiers in programming languages
      often need to start with a letter or underscore and then need to continue with letters, digits or underscores. To parse such an identifier you
      could use the following parser:
     </p>
    </div>
    <div class="para _9 lcinp">
<pre class="code fsharp"><span class="ck">let</span> <a href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a> <span class="cp">=</span>
    <span class="ck">let</span> <span class="ci">isIdentifierFirstChar</span> <span class="ci">c</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.isLetter"><span class="ci">isLetter</span></a> <span class="ci">c</span> <span class="co">||</span> <span class="ci">c</span> <span class="co">=</span> <span class="cc"><span class="cld">'</span>_<span class="crd">'</span></span>
    <span class="ck">let</span> <span class="ci">isIdentifierChar</span> <span class="ci">c</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.isLetter"><span class="ci">isLetter</span></a> <span class="ci">c</span> <span class="co">||</span> <a href="reference/charparsers.html#members.isDigit"><span class="ci">isDigit</span></a> <span class="ci">c</span> <span class="co">||</span> <span class="ci">c</span> <span class="co">=</span> <span class="cc"><span class="cld">'</span>_<span class="crd">'</span></span>

    <a href="reference/charparsers.html#members.many1Satisfy2L"><span class="ci">many1Satisfy2L</span></a> <span class="ci">isIdentifierFirstChar</span> <span class="ci">isIdentifierChar</span> <span class="cs"><span class="cld">"</span>identifier<span class="crd">"</span></span>
    <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">ws</span> <span class="clc"><span class="cld">//</span> skips trailing whitepace</span>
</pre>
    </div>
    <div class="para _0">
     <p>
      Here we have used the <code class="fsharp"><a href="reference/charparsers.html#members.many1Satisfy2L"><span
      class="ci">many1Satisfy2L</span></a></code> string parser, which is one of several primitives for parsing strings based on char predicates (i.e.
      functions that take a char as input and return a boolean value). It parses any sequence of one or more chars (hence the &#x201C;many1&#x201D; in
      the name) whose first char satisfies the first predicate function and whose remaining chars satisfy the second predicate (hence the
      &#x201C;Satisfy2&#x201D;). The string label given as the third argument (hence the &#x201C;L&#x201D;) is used in error message to describe the
      expected input.
     </p>
    </div>
    <div class="para _1 lcinp">
     <p>The following tests show how this parser works:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <a href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a> <span class="cs"><span class="cld">"</span>_<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: "_"
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <a href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a> <span class="cs"><span class="cld">"</span>_test1=<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: "_test1"
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <a href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a> <span class="cs"><span class="cld">"</span>1<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 1
1
^
Expecting: identifier
</span></pre>
    </div>
    <div class="para _2 lcinp">
     <div class="admonition">
      <div class="admonition-title">Tip</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         If you want to parse identifiers based on the Unicode XID syntax, consider using the built‐in <code class="fsharp"><a
         href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a></code> parser.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _3 lcinp">
     <p>
      Many string formats are complicated enough that you need to combine several char and string parser primitives. For example, consider the
      following string literal format:
     </p>
<pre class="code other">  stringLiteral: '"' (normalChar|escapedChar)* '"'
  normalChar:    any char except '\' and '"'
  escapedChar:   '\\' ('\\'|'"'|'n'|'r'|'t')
</pre>
    </div>
    <div class="para _4 lcinp">
     <p>A straightforward translation of this grammar to FParsec looks like:</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">stringLiteral</span> <span class="cp">=</span>
    <span class="ck">let</span> <span class="ci">normalChar</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.satisfy"><span class="ci">satisfy</span></a> <span class="cp">(</span><span class="ck">fun</span> <span class="ci">c</span> <span class="cr">-&gt;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\\</span><span class="crd">'</span></span> <span class="co">&amp;&amp;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span>"<span class="crd">'</span></span><span class="cp">)</span>
    <span class="ck">let</span> <span class="ci">unescape</span> <span class="ci">c</span> <span class="cp">=</span> <span class="ck">match</span> <span class="ci">c</span> <span class="ck">with</span>
                     <span class="cp">|</span> <span class="cc"><span class="cld">'</span>n<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\n</span><span class="crd">'</span></span>
                     <span class="cp">|</span> <span class="cc"><span class="cld">'</span>r<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\r</span><span class="crd">'</span></span>
                     <span class="cp">|</span> <span class="cc"><span class="cld">'</span>t<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\t</span><span class="crd">'</span></span>
                     <span class="cp">|</span> <span class="ci">c</span>   <span class="cr">-&gt;</span> <span class="ci">c</span>
    <span class="ck">let</span> <span class="ci">escapedChar</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\\</span><span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.anyOf"><span class="ci">anyOf</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\\</span>nrt<span class="ce">\"</span><span class="crd">"</span></span> <a href="reference/primitives.html#members.:124::62::62:"><span class="co">|&gt;&gt;</span></a> <span class="ci">unescape</span><span class="cp">)</span>
    <a href="reference/primitives.html#members.between"><span class="ci">between</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span>
            <span class="cp">(</span><a href="reference/charparsers.html#members.manyChars"><span class="ci">manyChars</span></a> <span class="cp">(</span><span class="ci">normalChar</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">escapedChar</span><span class="cp">)</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _5 lcinp">
     <p>In this example we use several library functions that we haven’t yet introduced:</p>
     <ul class="l1">
      <li class="_1">
       <code class="fsharp"><a href="reference/charparsers.html#members.satisfy"><span class="ci">satisfy</span></a></code> parses any char that
       satisfies the given predicate function.
      </li>
      <li class="_2">
       <code class="fsharp"><a href="reference/charparsers.html#members.anyOf"><span class="ci">anyOf</span></a></code> parses any char contained in
       the argument string.
      </li>
      <li class="_3">
       The pipeline combinator <code class="fsharp"><a href="reference/primitives.html#members.:124::62::62:"><span
       class="co">|&gt;&gt;</span></a></code> applies the function on the right side (<code class="fsharp"><span class="ci">unescape</span></code>) to
       the result of the parser on the left side (<code class="fsharp"><a href="reference/charparsers.html#members.anyOf"><span
       class="ci">anyOf</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\\</span>nrt<span class="ce">\"</span><span
       class="crd">"</span></span></code>).
      </li>
      <li class="_4">
       The choice combinator <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span
       class="co">&lt;|&gt;</span></a></code> applies the parser on the right side if the parser on the left side fails, so that <code
       class="fsharp"><span class="ci">normalChar</span> <a href="reference/primitives.html#members.:60::124::62:"><span
       class="co">&lt;|&gt;</span></a> <span class="ci">escapedChar</span></code> can parse both normal and escaped chars. (We will discuss this
       operator in more detail two sections below.)
      </li>
      <li class="_5">
       <code class="fsharp"><a href="reference/charparsers.html#members.manyChars"><span class="ci">manyChars</span></a></code> parses a sequence of
       chars with the given char parser and returns it as a string.
      </li>
     </ul>
    </div>
    <div class="para _6 lcinp">
     <p>Let’s test the <code class="fsharp"><span class="ci">stringLiteral</span></code> parser with a few test inputs:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">stringLiteral</span> <span class="cs"><span class="cld">"</span><span class="ce">\"</span>abc<span class="ce">\"</span><span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: "abc"
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">stringLiteral</span> <span class="cs"><span class="cld">"</span><span class="ce">\"</span>abc<span class="ce">\\</span><span class="ce">\"</span>def<span class="ce">\\</span><span class="ce">\\</span>ghi<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: "abc"def\ghi"
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">stringLiteral</span> <span class="cs"><span class="cld">"</span><span class="ce">\"</span>abc<span class="ce">\\</span>def<span class="ce">\"</span><span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 6
"abc\def"
     ^
Expecting: any char in ‘\nrt"’
</span></pre>
    </div>
    <div class="para _7 lcinp">
     <p>Instead of parsing the string literal char‐by‐char we could also parse it &#x201C;snippet‐by‐snippet&#x201D;:</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">stringLiteral2</span> <span class="cp">=</span>
    <span class="ck">let</span> <span class="ci">normalCharSnippet</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.many1Satisfy"><span class="ci">many1Satisfy</span></a> <span class="cp">(</span><span class="ck">fun</span> <span class="ci">c</span> <span class="cr">-&gt;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\\</span><span class="crd">'</span></span> <span class="co">&amp;&amp;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span>"<span class="crd">'</span></span><span class="cp">)</span>
    <span class="ck">let</span> <span class="ci">escapedChar</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\\</span><span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.anyOf"><span class="ci">anyOf</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\\</span>nrt<span class="ce">\"</span><span class="crd">"</span></span> <a href="reference/primitives.html#members.:124::62::62:"><span class="co">|&gt;&gt;</span></a> <span class="ck">function</span>
                                                            <span class="cp">|</span> <span class="cc"><span class="cld">'</span>n<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\n</span><span class="crd">"</span></span>
                                                            <span class="cp">|</span> <span class="cc"><span class="cld">'</span>r<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\r</span><span class="crd">"</span></span>
                                                            <span class="cp">|</span> <span class="cc"><span class="cld">'</span>t<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\t</span><span class="crd">"</span></span>
                                                            <span class="cp">|</span> <span class="ci">c</span>   <span class="cr">-&gt;</span> <span class="ci">string</span> <span class="ci">c</span><span class="cp">)</span>
    <a href="reference/primitives.html#members.between"><span class="ci">between</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span>
            <span class="cp">(</span><a href="reference/charparsers.html#members.manyStrings"><span class="ci">manyStrings</span></a> <span class="cp">(</span><span class="ci">normalCharSnippet</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">escapedChar</span><span class="cp">)</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _8">
     <p>
      Here we have used the <code class="fsharp"><a href="reference/charparsers.html#members.manyStrings"><span
      class="ci">manyStrings</span></a></code> combinator, which parses a sequence of strings with the given string parser and returns the strings in
      concatenated form.
     </p>
    </div>
    <div class="para _9 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         We have to require <code class="fsharp"><span class="ci">normalCharSnippet</span></code> to consume at least one char, i.e. use <code
         class="fsharp"><a href="reference/charparsers.html#members.many1Satisfy"><span class="ci">many1Satisfy</span></a></code> instead of <code
         class="fsharp"><a href="reference/charparsers.html#members.manySatisfy"><span class="ci">manySatisfy</span></a></code>. Otherwise <code
         class="fsharp"><span class="ci">normalCharSnippet</span></code> would succeed even if doesn’t consume input, <code class="fsharp"><span
         class="ci">escapedChar</span></code> would never be called and <code class="fsharp"><a
         href="reference/charparsers.html#members.manyStrings"><span class="ci">manyStrings</span></a></code> would eventually throw an exception to
         prevent an infinite loop.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _0">
     <p>
      Parsing a string chunk‐wise using an optimized parser like <code class="fsharp"><a href="reference/charparsers.html#members.many1Satisfy"><span
      class="ci">many1Satisfy</span></a></code> is usually a bit faster than parsing it char‐wise using <code class="fsharp"><a
      href="reference/charparsers.html#members.manyChars"><span class="ci">manyChars</span></a></code> and <code class="fsharp"><a
      href="reference/charparsers.html#members.satisfy"><span class="ci">satisfy</span></a></code>. In this case we can optimize our parser even a bit
      further – once we realize that two normal char snippets must be separated by at least one escaped char:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">stringLiteral3</span> <span class="cp">=</span>
    <span class="ck">let</span> <span class="ci">normalCharSnippet</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.manySatisfy"><span class="ci">manySatisfy</span></a> <span class="cp">(</span><span class="ck">fun</span> <span class="ci">c</span> <span class="cr">-&gt;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\\</span><span class="crd">'</span></span> <span class="co">&amp;&amp;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span>"<span class="crd">'</span></span><span class="cp">)</span>
    <span class="ck">let</span> <span class="ci">escapedChar</span> <span class="cp">=</span> <span class="cbc"><span class="cld">(*</span> like in stringLiteral2 <span class="crd">*)</span></span>
    <a href="reference/primitives.html#members.between"><span class="ci">between</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span> <span class="cp">(</span><a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span>
            <span class="cp">(</span><a href="reference/charparsers.html#members.stringsSepBy"><span class="ci">stringsSepBy</span></a> <span class="ci">normalCharSnippet</span> <span class="ci">escapedChar</span><span class="cp">)</span>

</pre>
     <p>
      The <code class="fsharp"><a href="reference/charparsers.html#members.stringsSepBy"><span class="ci">stringsSepBy</span></a></code> combinator
      parses a sequence of strings (with the first argument parser) separated by other strings (parsed with the second argument parser). It returns
      all parsed strings, including the separator strings, as a single, concatenated string.
     </p>
    </div>
    <div class="para _1">
     <p>
      Note that <code class="fsharp"><span class="ci">stringLiteral3</span></code> uses <code class="fsharp"><a
      href="reference/charparsers.html#members.manySatisfy"><span class="ci">manySatisfy</span></a></code> instead of <code class="fsharp"><a
      href="reference/charparsers.html#members.many1Satisfy"><span class="ci">many1Satisfy</span></a></code> in its <code class="fsharp"><span
      class="ci">normalCharSnippet</span></code> definition, so that it can parse escaped chars that are not separated by normal chars. This can’t
      lead to an infinite loop because <code class="fsharp"><span class="ci">escapedChar</span></code> can’t succeed without consuming input.
     </p>
    </div>
   </div>
  </div>
  <div id="sequentially-applying-parsers" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.8</span> Sequentially applying parsers</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      Whenever you need to apply multiple parsers in sequence and only need the result of one of them, a suitable combination of <code
      class="fsharp"><a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a></code> and <code class="fsharp"><a
      href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a></code> operators will do the job. However, these
      combinators won’t suffice if you need the result of more than one of the involved parsers. In that case you can use the <code class="fsharp"><a
      href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a></code>, …, <code class="fsharp"><a
      href="reference/primitives.html#members.pipe5"><span class="ci">pipe5</span></a></code> combinators, which apply multiple parsers in sequence
      and pass all the individual results to a function that computes the aggregate result.
     </p>
    </div>
    <div class="para _2">
     <p>
      For instance, with the <code class="fsharp"><a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a></code>
      combinator
     </p>
<pre class="code fsharp"><span class="ck">val</span> <a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'b</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <span class="cp">(</span><span class="ctv">'a</span> <span class="cr">-&gt;</span> <span class="ci">b</span> <span class="cr">-&gt;</span> <span class="ctv">'c</span><span class="cp">)</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'c</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span></pre>
     <p>
      you can construct a parser <code class="fsharp"><a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a> <span
      class="ci">p1</span> <span class="ci">p2</span> <span class="ci">f</span></code> that sequentially applies the two parsers <code
      class="fsharp"><span class="ci">p1</span></code> and <code class="fsharp"><span class="ci">p2</span></code> and then returns the result of the
      function application <code class="fsharp"><span class="ci">f</span> <span class="ci">x1</span> <span class="ci">x2</span></code>, where <code
      class="fsharp"><span class="ci">x1</span></code> and <code class="fsharp"><span class="ci">x2</span></code> are the results returned by <code
      class="fsharp"><span class="ci">p1</span></code> and <code class="fsharp"><span class="ci">p2</span></code>.
     </p>
    </div>
    <div class="para _3 lcinp">
     <p>
      In the following example we use <code class="fsharp"><a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a></code>
      to parse a product of two numbers:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">product</span> <span class="cp">=</span> <a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a> <span class="ci">float_ws</span> <span class="cp">(</span><span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>*<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">float_ws</span><span class="cp">)</span>
                    <span class="cp">(</span><span class="ck">fun</span> <span class="ci">x</span> <span class="ci">y</span> <span class="cr">-&gt;</span> <span class="ci">x</span> <span class="co">*</span> <span class="ci">y</span><span class="cp">)</span>
</pre>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">product</span> <span class="cs"><span class="cld">"</span>3 * 5<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: 15.0
</span></pre>
    </div>
    <div class="para _4 lcinp">
     <p>
      The <code class="fsharp"><a href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a><span class="co">-</span><span
      class="cn">5</span></code> combinators are particularly useful for constructing AST objects. In the following example we use <code
      class="fsharp"><a href="reference/primitives.html#members.pipe3"><span class="ci">pipe3</span></a></code> to parse a string constant definition
      into a <code class="fsharp"><span class="ci">StringConstant</span></code> object:
     </p>
<pre class="code fsharp"><span class="ck">type</span> <span class="ci">StringConstant</span> <span class="cp">=</span> <span class="ci">StringConstant</span> <span class="ck">of</span> <span class="ci">string</span> <span class="cp">*</span> <span class="ci">string</span>

<span class="ck">let</span> <span class="ci">stringConstant</span> <span class="cp">=</span> <a href="reference/primitives.html#members.pipe3"><span class="ci">pipe3</span></a> <a href="reference/charparsers.html#members.identifier"><span class="ci">identifier</span></a> <span class="cp">(</span><span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>=<span class="crd">"</span></span><span class="cp">)</span> <span class="ci">stringLiteral</span>
                           <span class="cp">(</span><span class="ck">fun</span> <span class="ci">id</span> <span class="ci">_</span> <span class="ci">str</span> <span class="cr">-&gt;</span> <span class="ci">StringConstant</span><span class="cp">(</span><span class="ci">id</span><span class="cp">,</span> <span class="ci">str</span><span class="cp">)</span><span class="cp">)</span>
</pre>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">stringConstant</span> <span class="cs"><span class="cld">"</span>myString = <span class="ce">\"</span>stringValue<span class="ce">\"</span><span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: StringConstant ("myString","stringValue")
</span></pre>
    </div>
    <div class="para _5">
     <p>
      If you just want to return the parsed values as a tuple, you can use the predefined <code class="fsharp"><a
      href="reference/primitives.html#members.tuple2"><span class="ci">tuple2</span></a><span class="co">-</span><span class="cn">5</span></code>
      parsers. For instance, <code class="fsharp"><a href="reference/primitives.html#members.tuple2"><span class="ci">tuple2</span></a> <span
      class="ci">p1</span> <span class="ci">p2</span></code> is equivalent to <code class="fsharp"><a
      href="reference/primitives.html#members.pipe2"><span class="ci">pipe2</span></a> <span class="ci">p1</span> <span class="ci">p2</span> <span
      class="cp">(</span><span class="ck">fun</span> <span class="ci">x1</span> <span class="ci">x2</span> <span class="cr">-&gt;</span> <span
      class="cp">(</span><span class="ci">x1</span><span class="cp">,</span> <span class="ci">x2</span><span class="cp">)</span><span
      class="cp">)</span></code>.
     </p>
    </div>
    <div class="para _6 lcinp">
     <p>
      The <code class="fsharp"><a href="reference/primitives.html#members.tuple2"><span class="ci">tuple2</span></a></code> parser is also available
      under the operator name <code class="fsharp"><a href="reference/primitives.html#members...:62::62:.."><span
      class="co">.&gt;&gt;.</span></a></code>, so that you can write <code class="fsharp"><span class="ci">p1</span> <a
      href="reference/primitives.html#members...:62::62:.."><span class="co">.&gt;&gt;.</span></a> <span class="ci">p2</span></code> instead of <code
      class="fsharp"><a href="reference/primitives.html#members.tuple2"><span class="ci">tuple2</span></a> <span class="ci">p1</span> <span
      class="ci">p2</span></code>. In the following example we parse a pair of comma separated numbers with this operator:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><span class="ci">float_ws</span> <a href="reference/primitives.html#members...:62::62:.."><span class="co">.&gt;&gt;.</span></a> <span class="cp">(</span><span class="ci">str_ws</span> <span class="cs"><span class="cld">"</span>,<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">float_ws</span><span class="cp">)</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span>123, 456<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: (123.0, 456.0)
</span></pre>
    </div>
    <div class="para _7">
     <p>
      Hopefully you find the <span class="i"><code class="fsharp"><span class="co">&gt;&gt;</span></code>‐with‐1‐or‐2‐dots‐notation</span> intuitive
      by now.
     </p>
    </div>
    <div class="para _8">
     <p>
      If you need a <code class="fsharp"><span class="ci">pipe</span></code> or <code class="fsharp"><span class="ci">tuple</span></code> parser with
      more than 5 arguments, you can easily construct one using the existing ones. For example, do you have an idea how to define a <code
      class="fsharp"><span class="ci">pipe7</span></code> parser? This footnote gives a possible solution: <sup class="fn-mark"><a
      id="sequentially-applying-parsers.:FN:1:B:" href="#sequentially-applying-parsers.:FN:1">[1]</a></sup>
     </p>
    </div>
   </div>
  </div>
  <div id="parsing-alternatives" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.9</span> Parsing alternatives</span></h2>
   <div class="intro i2">
    <div class="para _1 lcinp">
     <p>
      In the section on <code class="fsharp"><span class="ci">Parsing</span> <span class="ci">string</span> <span class="ci">data</span></code> we
      already shortly introduced the choice combinator <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span
      class="co">&lt;|&gt;</span></a></code>:
     </p>
<pre class="code fsharp"><span class="ck">val</span> <span class="cp">(</span><a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a><span class="cp">)</span><span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span> <span class="cr">-&gt;</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'a</span><span class="cp">,</span><span class="ci">u</span><span class="cp">&gt;</span>
</pre>
    </div>
    <div class="para _2">
     <p>
      This combinator allows you to support multiple alternative input formats at a given input position. For example, in the above section we used
      <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code> to combine a parser
      for unescaped chars and a parser for escaped chars into a parser that supports both: <code class="fsharp"><span class="ci">normalChar</span> <a
      href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">escapedChar</span></code>.
     </p>
    </div>
    <div class="para _3 lcinp">
     <p>
      Another example that shows how <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span
      class="co">&lt;|&gt;</span></a></code> works is the following parser for boolean variables:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">boolean</span> <span class="cp">=</span>     <span class="cp">(</span><a href="reference/charparsers.html#members.stringReturn"><span class="ci">stringReturn</span></a> <span class="cs"><span class="cld">"</span>true<span class="crd">"</span></span>  <span class="cb">true</span><span class="cp">)</span>
              <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.stringReturn"><span class="ci">stringReturn</span></a> <span class="cs"><span class="cld">"</span>false<span class="crd">"</span></span> <span class="cb">false</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _4">
     <p>
      Here we have also used the <code class="fsharp"><a href="reference/charparsers.html#members.stringReturn"><span
      class="ci">stringReturn</span></a></code> parser, which skips the string constant given as the first argument and, if successful, returns the
      value given as the second argument.
     </p>
    </div>
    <div class="para _5 lcinp">
     <p>Testing the <code class="fsharp"><span class="ci">boolean</span></code> parser with some inputs yields:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">boolean</span> <span class="cs"><span class="cld">"</span>false<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: false
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">boolean</span> <span class="cs"><span class="cld">"</span>true<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: true
</span><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="ci">boolean</span> <span class="cs"><span class="cld">"</span>tru<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 1
tru
^
Expecting: 'false' or 'true'
</span></pre>
    </div>
    <div class="para _6 lcinp">
     <p>
      The behaviour of the <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code>
      combinator has two important characteristics:
     </p>
     <ul class="l1">
      <li class="_1">
       <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code> only tries the
       parser on the right side if the parser on the left side fails. It does <em>not</em> implement a longest match rule.
      </li>
      <li class="_2">However, it only tries the right parser if the left parser fails <em>without consuming input</em>.</li>
     </ul>
    </div>
    <div class="para _7 lcinp">
     <p>
      A consequence of the second point is that the following test fails because the parser on the left side of <code class="fsharp"><a
      href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code> consumes whitespace before it fails:
     </p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><span class="cp">(</span><span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>a<span class="crd">"</span></span><span class="cp">)</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="cp">(</span><span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>b<span class="crd">"</span></span><span class="cp">)</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span> b<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Failure: Error in Ln: 1 Col: 2
 b
 ^
Expecting: 'a'
</span></pre>
    </div>
    <div class="para _8 lcinp">
     <p>Fortunately, we can easily fix this parser by factoring out <code class="fsharp"><span class="ci">ws</span></code>:</p>
<pre class="code fsharp"><span class="cmd"><span class="cmdp">&gt;</span> <span class="ci">test</span> <span class="cp">(</span><span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span>a<span class="crd">"</span></span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>b<span class="crd">"</span></span><span class="cp">)</span><span class="cp">)</span> <span class="cs"><span class="cld">"</span> b<span class="crd">"</span></span><span class="cp">;;</span></span>
<span class="cout">Success: "b"
</span></pre>
    </div>
    <div class="para _9">
     <p>
      If you’re curious why <code class="fsharp"><a href="reference/primitives.html#members.:60::124::62:"><span
      class="co">&lt;|&gt;</span></a></code> behaves this way and how you can handle situations where you need <code class="fsharp"><a
      href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code> to try the alternative parser even if the
      first parser fails after consuming input, please see <a href="users-guide/parsing-alternatives.html">section 5.6</a> and <a
      href="users-guide/looking-ahead-and-backtracking.html">section 5.7</a> in the user’s guide.
     </p>
    </div>
    <div class="para _0">
     <p>
      If you want to try more than two alternative parsers, you can chain multiple <code class="fsharp"><a
      href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a></code> operators, like in <code
      class="fsharp"><span class="ci">p1</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span
      class="ci">p2</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span
      class="ci">p3</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span
      class="co">...</span></code>, or you can use the <code class="fsharp"><a href="reference/primitives.html#members.choice"><span
      class="ci">choice</span></a></code> combinator, which accepts a sequence of parsers as the argument, like in <code class="fsharp"><a
      href="reference/primitives.html#members.choice"><span class="ci">choice</span></a> <span class="cp">[</span><span class="ci">p1</span><span
      class="cp">;</span> <span class="ci">p2</span><span class="cp">;</span> <span class="ci">p3</span><span class="cp">;</span> <span
      class="co">...</span><span class="cp">]</span></code>.
     </p>
    </div>
   </div>
  </div>
  <div id="fs-value-restriction" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.10</span> F#’s value restriction</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      When you start writing your own parsers with FParsec or try to compile some individual code snippets from above, you’ll come across a compiler
      issue that often causes some head‐scratching among new users of F# and FParsec: the <span class="i">value restriction</span>. In this section
      we’ll explain the value restriction and how you can handle it in your FParsec programs.
     </p>
    </div>
    <div class="para _2 lcinp">
     <div class="admonition">
      <div class="admonition-title">Note</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         If you find the discussion in this section too technical for the moment, just skip to the next section and come back later when you actually
         see a compiler message mentioning &#x201C;value restriction&#x201D; for the first time.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _3 lcinp">
     <p>F#’s value restriction is the reason that the following code snippet does not compile</p>
<pre class="code fsharp"><span class="ck">open</span> <span class="ci">FParsec</span>
<span class="ck">let</span> <span class="ci">p</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span>test<span class="crd">"</span></span>
</pre>
     <p>
      even though the following snippet compiles without a problem<sup class="fn-mark"><a id="fs-value-restriction.:FN:2:B:"
      href="#fs-value-restriction.:FN:2">[2]</a></sup>:
     </p>
<pre class="code fsharp"><span class="ck">open</span> <span class="ci">FParsec</span>
<span class="ck">let</span> <span class="ci">p</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span>test<span class="crd">"</span></span>
<a href="reference/charparsers.html#members.run"><span class="ci">run</span></a> <span class="ci">p</span> <span class="cs"><span class="cld">"</span>input<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _4 lcinp">
     <p>The compiler error generated for the first sample is the following:</p>
<pre class="code fsharp"><span class="cout">error FS0030: Value restriction.
The value 'p' has been inferred to have generic type
    val p : Parser&lt;string,'_a&gt;
Either make the arguments to 'p' explicit or,
if you do not intend for it to be generic, add a type annotation.
</span></pre>
    </div>
    <div class="para _5">
     <p>
      When you work with FParsec you’ll sooner or later see this or similar error messages, in particular if you work with the interactive console
      prompt. Fortunately, this kind of error is usually easy to workaround.
     </p>
    </div>
    <div class="para _6">
     <p>
      The problem with the first snippet above is that the F# compiler infers the <code class="fsharp"><span class="ci">p</span></code> value to have
      an unresolved generic type, although F# doesn’t permit a generic value in this situation. The return type of the <code class="fsharp"><a
      href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a></code> function is <code class="fsharp"><a
      href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span
      class="ci">string</span><span class="cp">,</span><span class="ctv">'u</span><span class="cp">&gt;</span></code>, where the type parameter <code
      class="fsharp"><span class="ctv">'u</span></code> represents the type of the <code class="fsharp"><a
      href="reference/charstream.html#CharStream"><span class="ci">CharStream</span></a></code> user state. Since there is nothing in the first
      snippet that constrains this type parameter, the compiler infers the type <code class="fsharp"><a
      href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span
      class="ci">string</span><span class="cp">,</span><span class="ctv">'_a</span><span class="cp">&gt;</span></code> for the parser value <code
      class="fsharp"><span class="ci">p</span></code>, with <code class="fsharp"><span class="ctv">'_a</span></code> representing an unresolved type
      parameter.
     </p>
    </div>
    <div class="para _7">
     <p>
      In the second snippet this problem doesn’t occur, because the use of <code class="fsharp"><span class="ci">p</span></code> as the first argument
      to the <code class="fsharp"><a href="reference/charparsers.html#members.run"><span class="ci">run</span></a></code> function constrains the user
      state type. Since <code class="fsharp"><a href="reference/charparsers.html#members.run"><span class="ci">run</span></a></code> only accepts
      parsers of type <code class="fsharp"><a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span
      class="cp">&lt;</span><span class="ctv">'t</span><span class="cp">,</span><span class="ci">unit</span><span class="cp">&gt;</span></code>, the
      compiler infers the non‐generic type <code class="fsharp"><a href="reference/primitives.html#members.Parser"><span
      class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ci">string</span><span class="cp">,</span><span class="ci">unit</span><span
      class="cp">&gt;</span></code> for <code class="fsharp"><span class="ci">p</span></code>.
     </p>
    </div>
    <div class="para _8 lcinp">
     <p>This example suggests two ways to handle the value restriction in FParsec programs:</p>
     <ul class="l1">
      <li class="_1">
       Either make sure that the type of a parser value is constrained to a non‐generic type by subsequent uses of this parser value <em>in the same
       compilation unit</em>,
      </li>
      <li class="_2">
       or provide an explicit type annotation to manually constrain the type of the parser value (usually, a few type annotations in key locations are
       enough for a whole parser module).
      </li>
     </ul>
    </div>
    <div class="para _9 lcinp">
     <p>Often it is convenient to define some type abbreviations like the following</p>
<pre class="code fsharp"><span class="ck">type</span> <span class="ci">UserState</span> <span class="cp">=</span> <span class="ci">unit</span> <span class="clc"><span class="cld">//</span> doesn't have to be unit, of course</span>
<span class="ck">type</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'t</span><span class="cp">&gt;</span> <span class="cp">=</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ctv">'t</span><span class="cp">,</span> <span class="ci">UserState</span><span class="cp">&gt;</span>
</pre>
    </div>
    <div class="para _0 lcinp">
     <p>With such an abbreviation in place, type annotations become as simple as</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">p</span> <span class="cp">:</span> <a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a><span class="cp">&lt;</span><span class="ci">_</span><span class="cp">&gt;</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pstring"><span class="ci">pstring</span></a> <span class="cs"><span class="cld">"</span>test<span class="crd">"</span></span>
</pre>
    </div>
    <div class="para _1">
     <p>
      Of course, constraining the type of a parser value to a non‐generic type is only a solution if you don’t actually need a generic type. If you do
      need a generic value, you’ll have to apply other techniques, as they are for example explained in the <a
      href="http://msdn.microsoft.com/en-us/library/dd233183.aspx">F# reference</a> or in a <a
      href="http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx">blog entry</a> by Dmitry Lomov. However, FParsec <code
      class="fsharp"><a href="reference/primitives.html#members.Parser"><span class="ci">Parser</span></a></code> values (not parametric parser
      functions) are usually only used in the context of a specific parser application with a fixed user state type. In that situation constraining
      the type is indeed the appropriate measure to avoid a value restriction error.
     </p>
    </div>
   </div>
  </div>
  <div id="parsing-json" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.11</span> Parsing JSON</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>Now that we have discussed the basics of FParsec we are well prepared to work through a real world parser example: a JSON parser.</p>
    </div>
    <div class="para _2">
     <p>
      JSON (JavaScript Object Notation) is a text‐based data interchange format with a simple and lightweight syntax. You can find descriptions of the
      syntax on <a href="json.org">json.org</a> and in <a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4626</a>.
     </p>
    </div>
    <div class="para _3">
     <p>
      In many applications one only has to deal with JSON files describing one particular kind of object. In such a context it sometimes can be
      appropriate to write a specialized parser just for that specific kind of JSON file. In this tutorial, however, we will follow a more general
      approach. We will implement a parser that can parse any general JSON file into an AST, i.e. an intermediate data structure describing the
      contents of the file. Applications can then conveniently query this data structure and extract the information they need. This is an approach
      comparable to that of XML parsers which build a data structure describing the document tree of an XML document. The great advantage of this
      approach is that the JSON parser itself becomes reusable and the document specific parsing logic can be expressed in the form of simple
      functions processing the AST of the JSON document.
     </p>
    </div>
    <div class="para _4">
     <p>
      The natural way to implement an AST in F# is with the help of a discriminated union type. If you look at the <a href="http://json.org">JSON
      specification</a>, you can see that a JSON value can be a string, a number, a boolean, null, a comma‐separated list of values in square
      brackets, or an object with a sequence of key‐value pairs in curly brackets.
     </p>
    </div>
    <div class="para _5 lcinp">
     <p>In our parser we will use the following union type to represent JSON values:</p>
<pre class="code fsharp"><span class="ck">type</span> <span class="ci">Json</span> <span class="cp">=</span> <span class="ci">JString</span> <span class="ck">of</span> <span class="ci">string</span>
          <span class="cp">|</span> <span class="ci">JNumber</span> <span class="ck">of</span> <span class="ci">float</span>
          <span class="cp">|</span> <span class="ci">JBool</span>   <span class="ck">of</span> <span class="ci">bool</span>
          <span class="cp">|</span> <span class="ci">JNull</span>
          <span class="cp">|</span> <span class="ci">JList</span>   <span class="ck">of</span> <span class="ci">Json</span> <span class="ci">list</span>
          <span class="cp">|</span> <span class="ci">JObject</span> <span class="ck">of</span> <span class="ci">Map</span><span class="cp">&lt;</span><span class="ci">string</span><span class="cp">,</span> <span class="ci">Json</span><span class="cp">&gt;</span>
</pre>
    </div>
    <div class="para _6">
     <p>
      Here we’ve chosen the F# <code class="fsharp"><span class="ci">list</span></code> type to represent a sequence of values and the <code
      class="fsharp"><span class="ci">Map</span></code> type to represent a sequence of key‐value pairs, because these types are particularly
      convenient to process in F#.<sup class="fn-mark"><a id="parsing-json.:FN:3:B:" href="#parsing-json.:FN:3">[3]</a></sup> Note that the <code
      class="fsharp"><span class="ci">Json</span></code> type is recursive, since both <code class="fsharp"><span class="ci">JList</span></code> and
      <code class="fsharp"><span class="ci">JObject</span></code> values can themselves contain <code class="fsharp"><span
      class="ci">Json</span></code> values. Our parser will have to reflect this recursive structure.
     </p>
    </div>
    <div class="para _7 lcinp">
     <div class="admonition">
      <div class="admonition-title">Tip</div>
      <div class="admonition-body">
       <div class="para _1">
        <p>
         If you’re new to FParsec and have a little time, it would be a good exercise to try to implement the JSON parser on your own (with the help
         of the reference documentation). This tutorial already covered almost everything you need and the JSON grammar is simple enough that this
         shouldn’t take too much time. Of course, you can always peek at the implementation below if you get stuck.
        </p>
       </div>
      </div>
     </div>
    </div>
    <div class="para _8 lcinp">
     <p>
      We start the actual parser implementation by covering the simple <code class="fsharp"><span class="cnu">null</span></code> and boolean cases:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">jnull</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.stringReturn"><span class="ci">stringReturn</span></a> <span class="cs"><span class="cld">"</span>null<span class="crd">"</span></span> <span class="ci">JNull</span>
<span class="ck">let</span> <span class="ci">jool</span> <span class="cp">=</span>      <span class="cp">(</span><a href="reference/charparsers.html#members.stringReturn"><span class="ci">stringReturn</span></a> <span class="cs"><span class="cld">"</span>true<span class="crd">"</span></span>  <span class="cp">(</span><span class="ci">JBool</span> <span class="cb">true</span><span class="cp">)</span><span class="cp">)</span>
            <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="cp">(</span><a href="reference/charparsers.html#members.stringReturn"><span class="ci">stringReturn</span></a> <span class="cs"><span class="cld">"</span>false<span class="crd">"</span></span> <span class="cp">(</span><span class="ci">JBool</span> <span class="cb">false</span><span class="cp">)</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _9">
     <p>
      Handling the number case is just as simple, because the JSON number format is based on the typical floating‐point number format used in many
      programming languages and hence can be parsed with FParsec’s built‐in <code class="fsharp"><a
      href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> parser:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">jnumber</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a> <a href="reference/primitives.html#members.:124::62::62:"><span class="co">|&gt;&gt;</span></a> <span class="ci">JNumber</span>
</pre>
     <p>
      (Note that F# allows us to pass the object constructor <code class="fsharp"><span class="ci">JNumber</span></code> as a function argument.)
     </p>
    </div>
    <div class="para _0">
     <p>
      If you compare the precise number format supported by <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span
      class="ci">pfloat</span></a></code> with that in the JSON spec, you’ll see that <code class="fsharp"><a
      href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> supports a superset of the JSON format. In contrast
      to the JSON format the <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code> parser
      also recognizes <code class="fsharp"><span class="ci">NaN</span></code> and <code class="fsharp"><span class="ci">Infinity</span></code> values,
      accepts a leading plus sign, accepts leading zeros and even supports the hexadecimal float format of Java and C99. Depending on the context this
      behaviour can be considered a feature or a limitation of the parser. For most applications it probably doesn’t matter, and the JSON RFC clearly
      states that a JSON parser may support a superset of the JSON syntax. However, if you’d rather only support the exact JSON number format, you can
      implement such a float parser rather easily based on the configurable <code class="fsharp"><a
      href="reference/charparsers.html#members.numberLiteral"><span class="ci">numberLiteral</span></a></code> parser (just have a look at how this is
      currently done in the <code class="fsharp"><a href="reference/charparsers.html#members.pfloat"><span class="ci">pfloat</span></a></code>
      source).
     </p>
    </div>
    <div class="para _1">
     <p>
      The JSON string format takes a little more effort to implement, but we’ve already parsed a similar format with the <code class="fsharp"><span
      class="ci">stringLiteral</span></code> parsers in <a href="#parsing-string-data">section 4.7</a>, so we can just adapt one of those parsers for
      our purpose:
     </p>
    </div>
    <div class="para _2 lcinp">
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">stringLiteral</span> <span class="cp">=</span>
    <span class="ck">let</span> <span class="ci">escape</span> <span class="cp">=</span>  <a href="reference/charparsers.html#members.anyOf"><span class="ci">anyOf</span></a> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="ce">\\</span>/bfnrt<span class="crd">"</span></span>
                  <a href="reference/primitives.html#members.:124::62::62:"><span class="co">|&gt;&gt;</span></a> <span class="ck">function</span>
                      <span class="cp">|</span> <span class="cc"><span class="cld">'</span>b<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\b</span><span class="crd">"</span></span>
                      <span class="cp">|</span> <span class="cc"><span class="cld">'</span>f<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\u000C</span><span class="crd">"</span></span>
                      <span class="cp">|</span> <span class="cc"><span class="cld">'</span>n<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\n</span><span class="crd">"</span></span>
                      <span class="cp">|</span> <span class="cc"><span class="cld">'</span>r<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\r</span><span class="crd">"</span></span>
                      <span class="cp">|</span> <span class="cc"><span class="cld">'</span>t<span class="crd">'</span></span> <span class="cr">-&gt;</span> <span class="cs"><span class="cld">"</span><span class="ce">\t</span><span class="crd">"</span></span>
                      <span class="cp">|</span> <span class="ci">c</span>   <span class="cr">-&gt;</span> <span class="ci">string</span> <span class="ci">c</span> <span class="clc"><span class="cld">//</span> every other char is mapped to itself</span>

    <span class="ck">let</span> <span class="ci">unicodeEscape</span> <span class="cp">=</span>
    	<span class="clc"><span class="cld">//</span>/ converts a hex char ([0-9a-fA-F]) to its integer number (0-15)</span>
        <span class="ck">let</span> <span class="ci">hex2int</span> <span class="ci">c</span> <span class="cp">=</span> <span class="cp">(</span><span class="ci">int</span> <span class="ci">c</span> <span class="co">&amp;&amp;&amp;</span> <span class="cn">15</span><span class="cp">)</span> <span class="co">+</span> <span class="cp">(</span><span class="ci">int</span> <span class="ci">c</span> <span class="co">&gt;&gt;&gt;</span> <span class="cn">6</span><span class="cp">)</span><span class="co">*</span><span class="cn">9</span>

        <span class="ci">str</span> <span class="cs"><span class="cld">"</span>u<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/primitives.html#members.pipe4"><span class="ci">pipe4</span></a> <a href="reference/charparsers.html#members.hex"><span class="ci">hex</span></a> <a href="reference/charparsers.html#members.hex"><span class="ci">hex</span></a> <a href="reference/charparsers.html#members.hex"><span class="ci">hex</span></a> <a href="reference/charparsers.html#members.hex"><span class="ci">hex</span></a> <span class="cp">(</span><span class="ck">fun</span> <span class="ci">h3</span> <span class="ci">h2</span> <span class="ci">h1</span> <span class="ci">h0</span> <span class="cr">-&gt;</span>
            <span class="cp">(</span><span class="ci">hex2int</span> <span class="ci">h3</span><span class="cp">)</span><span class="co">*</span><span class="cn">4096</span> <span class="co">+</span> <span class="cp">(</span><span class="ci">hex2int</span> <span class="ci">h2</span><span class="cp">)</span><span class="co">*</span><span class="cn">256</span> <span class="co">+</span> <span class="cp">(</span><span class="ci">hex2int</span> <span class="ci">h1</span><span class="cp">)</span><span class="co">*</span><span class="cn">16</span> <span class="co">+</span> <span class="ci">hex2int</span> <span class="ci">h0</span>
            <span class="co">|&gt;</span> <span class="ci">char</span> <span class="co">|&gt;</span> <span class="ci">string</span>
        <span class="cp">)</span>

    <span class="ck">let</span> <span class="ci">escapedCharSnippet</span> <span class="cp">=</span> <span class="ci">str</span> <span class="cs"><span class="cld">"</span><span class="ce">\\</span><span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="cp">(</span><span class="ci">escape</span> <a href="reference/primitives.html#members.:60::124::62:"><span class="co">&lt;|&gt;</span></a> <span class="ci">unicodeEscape</span><span class="cp">)</span>
    <span class="ck">let</span> <span class="ci">normalCharSnippet</span>  <span class="cp">=</span> <a href="reference/charparsers.html#members.manySatisfy"><span class="ci">manySatisfy</span></a> <span class="cp">(</span><span class="ck">fun</span> <span class="ci">c</span> <span class="cr">-&gt;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span>"<span class="crd">'</span></span> <span class="co">&amp;&amp;</span> <span class="ci">c</span> <span class="co">&lt;&gt;</span> <span class="cc"><span class="cld">'</span><span class="ce">\\</span><span class="crd">'</span></span><span class="cp">)</span>

    <a href="reference/primitives.html#members.between"><span class="ci">between</span></a> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span><span class="ce">\"</span><span class="crd">"</span></span><span class="cp">)</span>
            <span class="cp">(</span><a href="reference/charparsers.html#members.stringsSepBy"><span class="ci">stringsSepBy</span></a> <span class="ci">normalCharSnippet</span> <span class="ci">escapedCharSnippet</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _3">
     <p>
      <code class="fsharp"><span class="ci">stringLiteral</span></code> parses string literals as a sequence of normal char snippets separated by
      escaped char snippets. A normal char snippet is any sequence of chars that does not contain the chars <code class="fsharp"><span
      class="cc"><span class="cld">'</span>"<span class="crd">'</span></span></code> and <code class="fsharp"><span class="cc"><span
      class="cld">'</span><span class="ce">\\</span><span class="crd">'</span></span></code>. An escaped char snippet consists of a backslash followed
      by any of the chars <code class="fsharp"><span class="cc"><span class="cld">'</span><span class="ce">\\</span><span
      class="crd">'</span></span></code>, <code class="fsharp"><span class="cc"><span class="cld">'</span><span class="ce">\"</span><span
      class="crd">'</span></span></code>, <code class="fsharp"><span class="cc"><span class="cld">'</span>/<span class="crd">'</span></span></code>,
      <code class="fsharp"><span class="cc"><span class="cld">'</span>b<span class="crd">'</span></span></code>, <code class="fsharp"><span
      class="cc"><span class="cld">'</span>f<span class="crd">'</span></span></code>, <code class="fsharp"><span class="cc"><span
      class="cld">'</span>n<span class="crd">'</span></span></code>, <code class="fsharp"><span class="cc"><span class="cld">'</span>r<span
      class="crd">'</span></span></code>, <code class="fsharp"><span class="cc"><span class="cld">'</span>t<span class="crd">'</span></span></code>,
      or an Unicode escape. An Unicode escape consists of an <code class="fsharp"><span class="cc"><span class="cld">'</span>u<span
      class="crd">'</span></span></code> followed by four hex chars representing an UTF‐16 code point.
     </p>
    </div>
    <div class="para _4 lcinp">
     <p>
      <span class="a" id="parsing-json.createParserForwardedToRef-example"></span> The grammar rules for JSON lists and objects are recursive, because
      any list or object can contain itself any kind of JSON value. Hence, in order to write parsers for the list and object grammar rules, we need a
      way to refer to the parser for any kind of JSON value, even though we haven’t yet constructed this parser. Like it is so often in computing, we
      can solve this problem by introducing an extra indirection:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">jvalue</span><span class="cp">,</span> <span class="ci">jvalueRef</span> <span class="cp">=</span> <a href="reference/primitives.html#members.createParserForwardedToRef"><span class="ci">createParserForwardedToRef</span></a><span class="cp">&lt;</span><span class="ci">Json</span><span class="cp">,</span> <span class="ci">unit</span><span class="cp">&gt;</span><span class="cp">()</span>
</pre>
    </div>
    <div class="para _5">
     <p>
      As you might have guessed from the name, <code class="fsharp"><a href="reference/primitives.html#members.createParserForwardedToRef"><span
      class="ci">createParserForwardedToRef</span></a></code> creates a parser (<code class="fsharp"><span class="ci">jvalue</span></code>) that
      forwards all invocations to the parser in a reference cell (<code class="fsharp"><span class="ci">jvalueRef</span></code>). Initially, the
      reference cell holds a dummy parser, but since the reference cell is mutable, we can later replace the dummy parser with the actual value
      parser, once we have finished constructing it.
     </p>
    </div>
    <div class="para _6 lcinp">
     <p>
      The JSON RFC sensibly only permits spaces, (horizontal) tabs, line feeds and carriage returns as whitespace characters, which allows us to use
      the built‐in <code class="fsharp"><a href="reference/charparsers.html#members.spaces"><span class="ci">spaces</span></a></code> parser for
      parsing whitespace:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">ws</span> <span class="cp">=</span> <a href="reference/charparsers.html#members.spaces"><span class="ci">spaces</span></a>
</pre>
    </div>
    <div class="para _7 lcinp">
     <p>
      Both JSON lists and objects are syntactically represented as a comma‐separated lists of &#x201C;elements&#x201D; between brackets, where
      whitespace is allowed before and after any bracket, comma and list element. We can conveniently parse such lists with the following helper
      function:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">listBetweenStrings</span> <span class="ci">sOpen</span> <span class="ci">sClose</span> <span class="ci">pElement</span> <span class="ci">f</span> <span class="cp">=</span>
    <a href="reference/primitives.html#members.between"><span class="ci">between</span></a> <span class="cp">(</span><span class="ci">str</span> <span class="ci">sOpen</span><span class="cp">)</span> <span class="cp">(</span><span class="ci">str</span> <span class="ci">sClose</span><span class="cp">)</span>
            <span class="cp">(</span><span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <a href="reference/primitives.html#members.sepBy"><span class="ci">sepBy</span></a> <span class="cp">(</span><span class="ci">pElement</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">ws</span><span class="cp">)</span> <span class="cp">(</span><span class="ci">str</span> <span class="cs"><span class="cld">"</span>,<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">ws</span><span class="cp">)</span> <a href="reference/primitives.html#members.:124::62::62:"><span class="co">|&gt;&gt;</span></a> <span class="ci">f</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _8">
     <p>
      This function takes four arguments: an opening string, a closing string, an element parser and a function that is applied to the parsed list of
      elements.
     </p>
    </div>
    <div class="para _9 lcinp">
     <p>With the help of this function we can define the parser for a JSON list as follows:</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">jlist</span>   <span class="cp">=</span> <span class="ci">listBetweenStrings</span> <span class="cs"><span class="cld">"</span>[<span class="crd">"</span></span> <span class="cs"><span class="cld">"</span>]<span class="crd">"</span></span> <span class="ci">jvalue</span> <span class="ci">JList</span>
</pre>
    </div>
    <div class="para _0">
     <p>JSON objects are lists of key‐value pairs, so we need a parser for a key‐value pair:</p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">keyValue</span> <span class="cp">=</span> <span class="ci">stringLiteral</span> <a href="reference/primitives.html#members...:62::62:.."><span class="co">.&gt;&gt;.</span></a> <span class="cp">(</span><span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">str</span> <span class="cs"><span class="cld">"</span>:<span class="crd">"</span></span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">jvalue</span><span class="cp">)</span>
</pre>
     <p>
      (Remember, the points on both sides of <code class="fsharp"><a href="reference/primitives.html#members...:62::62:.."><span
      class="co">.&gt;&gt;.</span></a></code> indicate that the results of the two parsers on both sides are returned as a tuple.)
     </p>
    </div>
    <div class="para _1 lcinp">
     <p>
      By passing the <code class="fsharp"><span class="ci">keyValue</span></code> parser to <code class="fsharp"><span
      class="ci">listBetweenStrings</span></code> we obtain a parser for JSON objects:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">jobject</span> <span class="cp">=</span> <span class="ci">listBetweenStrings</span> <span class="cs"><span class="cld">"</span>{<span class="crd">"</span></span> <span class="cs"><span class="cld">"</span>}<span class="crd">"</span></span> <span class="ci">keyValue</span> <span class="cp">(</span><span class="ci">Map</span><span class="cm">.</span><span class="ci">ofList</span> <span class="co">&gt;&gt;</span> <span class="ci">JObject</span><span class="cp">)</span>
</pre>
    </div>
    <div class="para _2">
     <p>
      <span class="a" id="parsing-json.json-value-parser"></span> Having defined parsers for all the possible kind of JSON values, we can combine the
      different cases with a <code class="fsharp"><a href="reference/primitives.html#members.choice"><span class="ci">choice</span></a></code> parser
      to obtain the finished parser for JSON values:
     </p>
    </div>
    <div class="para _3 lcinp">
<pre class="code fsharp"><span class="ck">do</span> <span class="ci">jvalueRef</span> <span class="co">:=</span> <a href="reference/primitives.html#members.choice"><span class="ci">choice</span></a> <span class="cp">[</span><span class="ci">jobject</span>
                        <span class="ci">jlist</span>
                        <span class="ci">jstring</span>
                        <span class="ci">jnumber</span>
                        <span class="ci">jtrue</span>
                        <span class="ci">jfalse</span>
                        <span class="ci">jnull</span><span class="cp">]</span>
</pre>
    </div>
    <div class="para _4 lcinp">
     <p>
      The <code class="fsharp"><span class="ci">jvalue</span></code> parser doesn’t accept leading or trailing whitespace, so we need to define our
      parser for complete JSON documents as follows:
     </p>
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">json</span> <span class="cp">=</span> <span class="ci">ws</span> <a href="reference/primitives.html#members.:62::62:.."><span class="co">&gt;&gt;.</span></a> <span class="ci">jvalue</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <span class="ci">ws</span> <a href="reference/primitives.html#members...:62::62:"><span class="co">.&gt;&gt;</span></a> <a href="reference/charparsers.html#members.eof"><span class="ci">eof</span></a>
</pre>
    </div>
    <div class="para _5">
     <p>
      This parser will try to consume a complete JSON input stream and, if successful, will return a <code class="fsharp"><span
      class="ci">Json</span></code> AST of the input as the parser result
     </p>
    </div>
    <div class="para _6">
     <p>
      And that’s it, we’re finished with our JSON parser. If you want to try this parser out on some sample input, please take a look at the JSON
      project in the <span class="tt">Samples</span> folder.
     </p>
    </div>
   </div>
  </div>
  <div id="what-now" class="section s2">
   <h2 class="title h2"><span><span class="section-number">4.12</span> What now?</span></h2>
   <div class="intro i2">
    <div class="para _1">
     <p>
      If this tutorial has whet your appetite for a more in‐depth introduction to FParsec, just head over to the <a
      href="users-guide/index.html">user’s guide</a>. If you can’t wait to write your own parser, then bookmark the <a
      href="reference/parser-overview.html">parser overview</a> page, maybe take a short look at the example parsers in the <span
      class="tt">Samples</span> folder and just start hacking. You can always consult the user’s guide at a later point should you get stuck
      somewhere.
     </p>
    </div>
   </div>
  </div>
  <div class="fn-list">
   <div class="fn-title">Footnotes:</div>
   <table class="fn">
    <tr class="fn _1">
     <th class="fn _1">
      <a class="footnote-backlink" id="sequentially-applying-parsers.:FN:1" href="#sequentially-applying-parsers.:FN:1:B:">[1]</a>
     </th>
     <td class="fn _2">
<pre class="code fsharp"><span class="ck">let</span> <span class="ci">pipe7</span> <span class="ci">p1</span> <span class="ci">p2</span> <span class="ci">p3</span> <span class="ci">p4</span> <span class="ci">p5</span> <span class="ci">p6</span> <span class="ci">p7</span> <span class="ci">f</span> <span class="cp">=</span>
    <a href="reference/primitives.html#members.pipe4"><span class="ci">pipe4</span></a> <span class="ci">p1</span> <span class="ci">p2</span> <span class="ci">p3</span> <span class="cp">(</span><a href="reference/primitives.html#members.tuple4"><span class="ci">tuple4</span></a> <span class="ci">p4</span> <span class="ci">p5</span> <span class="ci">p6</span> <span class="ci">p7</span><span class="cp">)</span>
          <span class="cp">(</span><span class="ck">fun</span> <span class="ci">x1</span> <span class="ci">x2</span> <span class="ci">x3</span> <span class="cp">(</span><span class="ci">x4</span><span class="cp">,</span> <span class="ci">x5</span><span class="cp">,</span> <span class="ci">x6</span><span class="cp">,</span> <span class="ci">x7</span><span class="cp">)</span> <span class="cr">-&gt;</span> <span class="ci">f</span> <span class="ci">x1</span> <span class="ci">x2</span> <span class="ci">x3</span> <span class="ci">x4</span> <span class="ci">x5</span> <span class="ci">x6</span> <span class="ci">x7</span><span class="cp">)</span>
</pre>
     </td>
    </tr>
    <tr class="fn _2">
     <th class="fn _1"><a class="footnote-backlink" id="fs-value-restriction.:FN:2" href="#fs-value-restriction.:FN:2:B:">[2]</a></th>
     <td class="fn _2">Assuming you referenced the two FParsec DLLs.</td>
    </tr>
    <tr class="fn _3">
     <th class="fn _1"><a class="footnote-backlink" id="parsing-json.:FN:3" href="#parsing-json.:FN:3:B:">[3]</a></th>
     <td class="fn _2">
      If you need to parse huge sequences and objects, it might be more appropriate to use an array and dictionary for JList and JObject respectively.
     </td>
    </tr>
   </table>
  </div>
 </div>
 </div>
 </div>
 </div>
</body>
</html>