aboutsummaryrefslogtreecommitdiff
path: root/doc/reference.xml
blob: 0e4d5bed5e064801d8bc0c2b93357056c564e6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
<?xml version="1.0"?>

<!--
	LSPIPAT - LUA SPIPAT WRAPPER
	Copyright (C) 2010, Robin Haberkorn
	License: LGPL

	DOCUMENTATION AND MODULE REFERENCE
-->

<book xmlns="http://docbook.org/ns/docbook"
      xmlns:xlink="http://www.w3.org/1999/xlink">
	<info>
		<title>SNOBOL/SPITBOL Patterns for Lua</title>
		<subtitle>libspipat Lua wrapper</subtitle>
		<titleabbrev>lspipat</titleabbrev>

		<author>
			<personname><firstname>Robin</firstname> <surname>Haberkorn</surname></personname>
			<email>robin.haberkorn at googlemail.com</email>
		</author>
		<copyright>
			<year>2010</year><holder>Robin Haberkorn</holder>
		</copyright>

		<mediaobject>
			<imageobject><imagedata format="PNG" fileref="lspipat.png"/></imageobject>
		</mediaobject>

		<abstract><para>
			The following document is the <productname>lspipat</productname>
			Lua 5.1 module documentation and reference.
		</para></abstract>
	</info>

	<acknowledgements>
		<title>Thanks To...</title>

		<para>
			<productname>lspipat</productname> would not be possible without:
		</para>
		<itemizedlist>
			<listitem>
				<firstname>Phil</firstname> <surname>Budne</surname>, for <productname>spipat</productname>.
				<productname>lspipat</productname> is merely a <productname>spipat</productname> wrapper.
			</listitem><listitem>
				<firstname>Robert</firstname> <surname>Dewar</surname> who has created Macro SPITBOL and
				the GNAT.Spitbol package.
				<productname>spipat</productname> was derived from GNAT.Spitbol, which is based on Macro SPITBOL.
			</listitem>
		</itemizedlist>
	</acknowledgements>

	<preface>
		<title>Introduction</title>

		<para>
			<productname>lspipat</productname> is a wrapper to <productname>spipat</productname>
			that brings support for a first-class SNOBOL/SPITBOL-like pattern data type.
			Patterns can be constructed and subsequently combined with other patterns,
			strings, numbers and functions using binary and unary operators allowing
			the construction of grammars describing any <emphasis>Context Free Language</emphasis>.
			Patterns can be matched against any Lua string.
			A major difference to other pattern matching techniques like regular expressions, besides
			the supported language class, is the possibility to construct patterns/grammars in a
			readable and intuitive way, somewhat reminiscent of the <emphasis>BNF</emphasis>.
		</para><para>
			They can include pattern elements that have side-effects (i.e. Lua code executed during
			pattern matching) or produce and influence pattern elements dynamically.
			For instance, functions can be specified that are executed during matching to produce
			the parameters necessary for the interpretation of a pattern element.
			Code can be embedded that generates entire patterns on the fly.
			Matching previously matched substrings and implementing recursive patterns
			is only one application of the powerful dynamic pattern elements traditionally
			offered by SNOBOL pattern matching and thus by <productname>lspipat</productname>.
		</para><para>
			SNOBOL/SPITBOL pattern matching was traditionally used in compiler construction
			and prototyping, artificial intelligence research and the humanities.
		</para>
	</preface>

	<chapter>
		<title>Resources</title>

		<para>
			These internet resources are more or less directly related to <productname>lspipat</productname> and
			might be useful to you:

			<itemizedlist>
				<listitem>
					<link xlink:href="http://luaforge.net/projects/lspipat/">http://luaforge.net/projects/lspipat/</link>:
					<productname>lspipat</productname> project page at LuaForge, downloads, bug tracker, etc.
				</listitem><listitem>
					<link xlink:href="http://www.snobol4.org/spipat/">http://www.snobol4.org/spipat/</link>:
					<productname>libspipat</productname> downloads
				</listitem><listitem>
					<link xlink:href="http://pypi.python.org/pypi/spipat/">http://pypi.python.org/pypi/spipat/</link>:
					<productname>libspipat's</productname> Python wrapper (included in <productname>libspipat</productname>
					packages).
				</listitem><listitem>
					<link xlink:href="http://www.infeig.unige.ch/support/ada/gnatlb/g-spipat.html">http://www.infeig.unige.ch/support/ada/gnatlb/g-spipat.html</link>:
					<productname>GNAT.Spitbol</productname> description. Also installed as <filename>pattern.txt</filename> by <productname>lspipat</productname>.
				</listitem><listitem>
					<link xlink:href="ftp://ftp.cs.arizona.edu/snobol/gb.pdf">ftp://ftp.cs.arizona.edu/snobol/gb.pdf</link>:
					The SNOBOL4 Programming Language (The famous <emphasis>Green Book</emphasis>)
				</listitem><listitem>
					<link xlink:href="ftp://ftp.snobol4.com/spitman.pdf">ftp://ftp.snobol4.com/spitman.pdf</link>:
					Macro SPITBOL Reference Manual
				</listitem><listitem>
					<link xlink:href="http://www.snobol4.org/">other interesting resources compiled by Phil Budne...</link>
				</listitem>
			</itemizedlist>
		</para>
	</chapter>

	<chapter>
		<title>Comparison with SNOBOL</title>

		<para>
			Just as patterns in <productname>SNOBOL</productname> are combined and constructed dynamically with
			binary and unary operators, <productname>lspipat</productname> also uses operators available in
			Lua to construct patterns in a simple and intuitive way.
			The operators and pattern-construction functions were chosen, so the pattern construction syntax
			is as similar as possible to <productname>SNOBOL</productname>/<productname>SPITBOL</productname>.
			The following table shows a comparision of operators between
			<productname>SPITBOL</productname> and <productname>lspipat</productname>:
		</para>
		<table xml:id="operator_comparison">
			<title>Comparision of SPITBOL and lspipat operators</title>

			<tgroup cols="4" colsep="1" rowsep="1">
				<colspec colnum="2" align="center"/> <!-- operators -->
				<colspec colnum="3" align="center"/>

				<thead>
					<row>
						<entry>Operation</entry>
						<entry>SPITBOL</entry>
						<entry>lspipat</entry>
						<entry>Notes</entry>
					</row>
				</thead><tbody>
					<row>
						<entry>Alternation</entry>
						<entry><command>|</command></entry>
						<entry><command>+</command></entry>
						<entry morerows="1"><para>
							Refer to <xref linkend="composition"/>.
							Cannot be used to combine two <emphasis>strings</emphasis>.
						</para></entry>
					</row><row>
						<entry>Concatenation</entry>
						<entry><emphasis>(space)</emphasis></entry>
						<entry><command>*</command></entry>
					</row><row>
						<entry>Immediate Assignment/Call</entry>
						<entry><command>$</command></entry>
						<entry><command>%</command></entry>
						<entry morerows="1"><para>
							<link linkend="assignment"><command>%</command> and <command>/</command></link> have the
							<link xlink:href="http://www.lua.org/manual/5.1/manual.html#2.5.6">same precedence</link>
							as <link linkend="concat"><command>*</command></link> in Lua.
							Also only call versions are supported (see <xref linkend="deferring"/>).
						</para></entry>
					</row><row>
						<entry>Deferred Assignment/Call</entry>
						<entry><command>.</command></entry>
						<entry><command>/</command></entry>
					</row><row>
						<entry morerows="1">Cursor Assignment</entry>
						<entry morerows="1"><command>@</command> <emphasis>(unary)</emphasis></entry>
						<entry><command>#</command> <emphasis>(unary)</emphasis></entry>
						<entry morerows="1"><para>
							Refer to <xref linkend="cursor"/>.
							<productname>lspipat</productname> only supports a call version
							(see <xref linkend="deferring"/>).
						</para></entry>
					</row><row>
						<!-- <entry/> -->
						<!-- <entry/> -->
						<entry><command>Setcur</command></entry>
					</row><row>
						<entry>Defer Expression</entry>
						<entry><command>*</command> <emphasis>(unary)</emphasis></entry>
						<entry morerows="1"><command>-</command> <emphasis>(unary)</emphasis> or <command>Pred</command></entry>
						<entry morerows="1"><para>
							Refer to <xref linkend="predicate"/>.
							In general, expressions can be wrapped in (anonymous) functions to defer them.
						</para></entry>
					</row><row>
						<entry>Interrogation/Predicate</entry>
						<entry><command>?</command> <emphasis>(unary)</emphasis></entry>
						<!-- <entry/> -->
					</row><row>
						<entry morerows="1">Pattern Match</entry>
						<entry><command>?</command></entry>
						<entry morerows="1"><command>smatch</command></entry>
						<entry morerows="1"><para>
							Refer to <xref linkend="smatch"/>.
							<code>S ? P</code> is roughly equivalent to <code>S:smatch(P)</code> in Lua.
						</para></entry>
					</row><row>
						<!-- <entry/> -->
						<entry><emphasis>(space)</emphasis></entry>
						<!-- <entry/> -->
					</row><row>
						<entry>Substring Replacement</entry>
						<entry><command>=</command></entry>
						<entry><command>ssub</command></entry>
						<entry><para>
							Refer to <xref linkend="ssub"/>.
							<code>S P = R</code> is roughly equivalent to <code>S:ssub(P, R, 1)</code> in Lua.
						</para></entry>
					</row>
				</tbody>
			</tgroup>
		</table>
	</chapter>

	<chapter>
		<title>Installation</title>

		<para>
			<productname>lspipat</productname> uses an autotools buildsystem. The standard
			<filename>INSTALL</filename> file contains instructions on how to use it from
			a package builder's perspective.
			Nevertheless, there are some quirks that should be mentioned.
		</para>

		<section>
			<title>Dependencies</title>

			<itemizedlist>
				<listitem><para>
					<link xlink:href="http://www.snobol4.org/spipat/test/spipat-0.9.3%2b.tar.gz">spipat 0.9.3+</link>:
					You are advised to apply the patch <filename>spipat-patches/0.9.3+_image.patch</filename> first
					before building <productname>spipat</productname>, even though it is not mandatory.
					It fixes a header file (so <productname>lspipat</productname> can make use of customized
					<link linkend="tostring">render-to-string</link> functionality) and various bugs.
				</para></listitem>
				<listitem><para>
					<link xlink:href="http://www.lua.org/download.html">Lua 5.1</link>:
					You probably have this already. The <filename>configure</filename> script
					should be able to cope with <productname>Ubuntu</productname> and
					<link xlink:href="http://lua-users.org/wiki/LuaBinaries">Lua Binaries</link>
					distributions. The standalone Lua compiler is only required if
					<link linkend="precompile">compilation of Lua scripts</link> is enabled.
				</para></listitem>
			</itemizedlist>
		</section>

		<section>
			<title>Configuration Options</title>

			<para>
				The following special <filename>configure</filename> script options
				are supported:
			</para>
			<variablelist>
				<varlistentry>
					<term><arg choice="plain">--enable-lua-libdir=<replaceable>DIR</replaceable></arg></term>
					<listitem><para>
						Change the installation directory of <productname>lspipat</productname>.
						It defaults to <filename>LIBDIR/lua/5.1</filename>. You probably want this to
						point to some directory in Lua's
						<link xlink:href="http://www.lua.org/manual/5.1/manual.html#pdf-require">
						module search path</link>, so the default should be ok.
					</para></listitem>
				</varlistentry>
				<varlistentry xml:id="precompile">
					<term><arg choice="plain">--disable-lua-precompile</arg></term>
					<listitem><para>
						Disable precompilation of Lua source files.
						Naturally, a Lua compiler will not be required when this option
						is used.
					</para></listitem>
				</varlistentry>
				<varlistentry>
					<term><arg choice="plain">--disable-lua-strip</arg></term>
					<listitem><para>
						Do not strip (i.e. remove debugging symbols from) compiled
						Lua sources.
					</para></listitem>
				</varlistentry>
				<varlistentry>
					<term><arg choice="plain">--disable-html-doc</arg></term>
					<listitem><para>
						Do not generate HTML documentation. The documentation is usually
						derived from <productname>Docbook</productname> using
						<application>XSLTProc</application>.
						Disabling this may be useful if you have got some problem
						with the tool chain but are satisfied with the precompiled
						documentation in the distribution.
					</para></listitem>
				</varlistentry>
			</variablelist>
			<para>
				Furthermore, you should note that <link linkend="tostring">render-to-string</link> results are not
				reminiscent of <productname>lspipat</productname> syntax (used in this document) by default.
				For <productname>lspipat</productname> to be able to customize these renderings,
				<filename>configure</filename> has to find some <productname>spipat</productname> headers which
				are not normally installed.
				Therefore it is highly recommended to add spipat's source directory to the C include search path
				using the <envar>CPPFLAGS</envar> variable before running <filename>configure</filename>.
			</para>
		</section>

		<para>
			Thus, supposing that <productname>spipat</productname> sources are located in your home directory,
			the most common way to install <productname>lspipat</productname> would be:
		</para>
		<informalexample>
			<programlisting><![CDATA[./configure CPPFLAGS=-I~/spipat-0.9.3+
make install]]></programlisting>
		</informalexample>
	</chapter>

	<chapter>
		<title>Usage</title>

		<para>
			After <productname>lspipat</productname> has been installed properly, you will
			be able use it in your Lua program by simply requiring <literal>lspipat</literal>
			(i.e. <code>require "lspipat"</code>).
		</para><para>
			The module table will be called <literal>spipat</literal>, but many functions
			(especially pattern constructors) will be registered as globals as well.
			Also, some operators will be overloaded.
			For details on all that (operators, globals, etc.) refer to
			<xref linkend="reference"/>.
		</para>
	</chapter>

	<chapter>
		<title>Examples</title>

		<para>
			The <filename>samples</filename> directory in the <productname>lspipat</productname> source package
			contains some small examples that I hope give you some inspiration on how and where to use
			<productname>lspipat</productname>.
		</para>
		<variablelist>
			<varlistentry>
				<term><filename>samples/exp2bf.lua</filename></term>
				<listitem>
					<cmdsynopsis><command>exp2bf.lua</command> <arg choice="plain"><replaceable>expression</replaceable></arg></cmdsynopsis>
					<para>
						Compiles simple arithmetic expressions to Brainfuck programs that when
						executed evaluate the expression and print the result
						(8-bit unsigned integer arithmetics).
						Prints these programs to <emphasis>stdout</emphasis>.
					</para><para>
						Use that for whatever you can imagine ;-)
					</para>
				</listitem>
			</varlistentry><varlistentry>
				<term><filename>samples/wave.lua</filename></term>
				<listitem>
					<cmdsynopsis><command>wave.lua</command> <arg choice="plain"><replaceable>wavefile</replaceable></arg></cmdsynopsis>
					<para>
						Validates/parses <filename>WAV</filename> files
						and prints some information about it.
					</para><para>
						This is an example of how to use <productname>lspipat</productname>
						to do pattern matching on "binary" data (formats, protocols). Some
						primitives were implemented in Lua for that reason - in the future
						there might be a separate C-module to do the encoding/decoding of
						integers in different byte-orders more efficiently.
					</para>
				</listitem>
			</varlistentry><varlistentry>
				<term><filename>samples/regexp.lua</filename></term>
				<listitem><para>
					Small regular expression example/test - uses a comprehensive regular
					expression describing IPs.
				</para></listitem>
			</varlistentry>
		</variablelist>
	</chapter>

	<chapter xml:id="deferring">
		<title>Variable Deferring Techniques</title>

		<para>
			In SNOBOL, arbitrary expressions could be <emphasis>deferred</emphasis>
			(i.e. their evaluation could be deferred) by using the unary asterisk operator.
			With <productname>lspipat</productname> however, you will have to pass functions
			(which can be constructed anonymously) to the appropriate constructors to achieve
			the same goal.
		</para><para>
			Deferring expressions which should be combined with other patterns is one
			application of the <link linkend="predicate"><command>Pred</command> constructor</link>
			and <command>-</command> operator respectively.
		</para><para>
			Deferring variables is just a special case of deferring expressions.
			In this chapter, different ways of optimizing variable deferrings will be
			explained using a simple example.
		</para><para>
			For instance if you would like to <link linkend="assignment">assign</link> a
			matched quotation character to a local variable and use that to subsequently match
			a simple quote/string, you could use function closures to write something like that:
		</para>
		<example>
			<title>Function Closures for Deferring Purposes</title>

			<programlisting language="lua">local cquote
string = <link linkend="Any">Any</link>("\"'") / function(c) cquote = c end
       * <link linkend="Break">Break</link>(function() return cquote end)
       * -function() return cquote end</programlisting>
		</example>
		<para>
			You may find this solution a bit verbose, compared with
			SNOBOL's elegant syntax.
			To save some typing you could define your own constructors
			that take the name of a global variable (as a string)
			and construct patterns whose arguments are retrieved by
			a function closure accessing the globals table.
		</para>
		<example>
			<title>Custom Constructors for Deferring Purposes</title>

			<programlisting language="lua">function _Break(name)
       return <link linkend="Break">Break</link>(function() return _G[name] end)
end
function _Pred(name)
       return -function() return _G[name] end
end

string = <link linkend="Any">Any</link>("\"'") / function(c) cquote = c end
       * _Break "cquote"
       * _Pred  "cquote"</programlisting>
		</example>
		<para>
			Of course, if you do not want to pollute the global namespace
			your custom functions could just as well access a local table.
			Furthermore, you could optimize the code by defining one generic
			table access function which is suitable to be used for
			<productname>lspipat</productname>'s pattern constructors -
			being able to pass so called <emphasis>cookies</emphasis>
			to functions comes in handy.
		</para>
		<example>
			<title>Generic Retrievers for Deferring Purposes</title>

			<programlisting language="lua">function getGlobal(name) return _G[name] end
function _Break(name) return <link linkend="Break">Break</link>(getGlobal, name) end
function _Pred(name) return <link linkend="Pred">Pred</link>(getGlobal, name) end
-- ...</programlisting>
		</example>
		<para>
			Fortunately, <productname>lspipat</productname> already defines
			such constructors (deferring global variables) for you.
			Whereever possible, there will be versions of constructors
			with leading underscores that work similar to the ones in
			the example above.
			You can of course overwrite these constructors, e.g. with
			versions accessing a special local table.
		</para>

		<section>
			<title>Recursive Patterns</title>

			<para>
				Recursive patterns can be implemented just as described above.
				Supposing you want to match the repetition of the predefined pattern
				<literal>P</literal> (<emphasis>greedy</emphasis>) you could write
				something like that:
			</para>
			<example>
				<title>Recursive Patterns</title>

				<programlisting language="lua"><![CDATA[foo = P * -"foo" + ""]]></programlisting>
			</example>
			<para>
				Sometimes however when using global variables is inappropriate,
				you might want to do the following trick:
			</para>
			<example>
				<title>Recursive Pattern Trick</title>

				<programlisting language="lua"><![CDATA[local function foo() return foo end
foo = P * -foo + ""]]></programlisting>
			</example>
			<para>
				It works because <literal>foo</literal> is still a function in the scope
				of the assignment's right side, but a pattern afterwards so the
				function - to which no (direct) reference exists anymore - will return
				the pattern <literal>foo</literal> after the assignment.
			</para>
		</section>
	</chapter>

	<reference xml:id="reference">
		<title>Module Reference</title>

		<partintro><para>
			A compilation of all functions in the <productname>lspipat</productname>
			module, global functions registered by the module, methods
			and overloaded operators follows.
		</para></partintro>

		<refentry xml:id="smatch">
			<refmeta><refentrytitle>smatch</refentrytitle></refmeta>

			<refnamediv>
				<refname>smatch</refname>
				<refpurpose>Perform pattern match on a subject string</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.smatch</command>
					( <arg choice="plain"><replaceable>subject</replaceable></arg>
					  <arg choice="plain">, <replaceable>pattern</replaceable></arg>
					  <arg>, <replaceable>flags</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><replaceable>subject</replaceable>:smatch</command>
					( <arg choice="plain"><replaceable>pattern</replaceable></arg>
					  <arg>, <replaceable>flags</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Tries to match <emphasis>pattern</emphasis> against <emphasis>subject</emphasis>
					using the given <emphasis>flags</emphasis>.
				</para>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist>
					<listitem>subject (<emphasis>string</emphasis>): A string against which the pattern match will be performed</listitem>
					<listitem>pattern (<emphasis>userdata</emphasis>): The pattern used for matching</listitem>
					<listitem>
						flags (<emphasis>number</emphasis> or <emphasis>nil</emphasis>):
						Optional <productname>spipat</productname> flags.
					</listitem>
				</orderedlist>

				<refsect2 xml:id="spipat_flags">
					<title>Spipat Flags</title>

					<para>
						Flags are added (e.g. <code>spipat.match_anchored + spipat.match_debug</code>),
						due to the lack of a logical/binary <emphasis>or</emphasis> operator in Lua.
					</para>
					<itemizedlist>
						<listitem>
							<literal>spipat.match_anchored</literal>: Match in anchored mode
						</listitem><listitem>
							<literal>spipat.match_debug</literal>:
							Match with progress being printed to <emphasis>stdout</emphasis>.
							Useful for pattern debugging as the name suggests.
						</listitem>
					</itemizedlist>
				</refsect2>
			</refsect1><refsect1>
				<title>Return Values</title>

				<para>
					In case of an exception during matching, raises an error.
					In case no substring matches, returns a single <emphasis>nil</emphasis> value.
					Otherwise returns
				</para>
				<orderedlist>
					<listitem><emphasis>number</emphasis>: Start of matched substring</listitem>
					<listitem><emphasis>number</emphasis>: End of matched substring</listitem>
				</orderedlist>
			</refsect1>
		</refentry>

		<refentry xml:id="ssub">
			<refmeta><refentrytitle>ssub</refentrytitle></refmeta>

			<refnamediv>
				<refname>ssub</refname>
				<refpurpose>Substitute substrings matching a pattern in a subject</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.ssub</command>
					( <arg choice="plain"><replaceable>subject</replaceable></arg>
					  <arg choice="plain">, <replaceable>pattern</replaceable></arg>
					  <arg choice="plain">, <replaceable>replacement</replaceable></arg>
					  <arg><arg choice="plain">, <replaceable>n</replaceable></arg><arg>, <replaceable>flags</replaceable></arg></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><replaceable>subject</replaceable>:ssub</command>
					( <arg choice="plain"><replaceable>pattern</replaceable></arg>
					  <arg choice="plain">, <replaceable>replacement</replaceable></arg>
					  <arg><arg choice="plain">, <replaceable>n</replaceable></arg><arg>, <replaceable>flags</replaceable></arg></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Substitutes regions in <emphasis>subject</emphasis> matching <emphasis>pattern</emphasis> either with a string
					if <emphasis>replacement</emphasis> is a string or if <emphasis>replacement</emphasis> is a function, the result
					of calling that function. This may be useful for deferring the evaluation of replacement strings
					which depend on (are built from) results of the matching process (e.g. call-on-match or call-immediately function executions).
				</para>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist>
					<listitem>subject (<emphasis>string</emphasis>): The subject for the first pattern match</listitem>
					<listitem>pattern (<emphasis>userdata</emphasis>): The pattern used for matching</listitem>
					<listitem>
						replacement (<emphasis>string</emphasis> or <emphasis>function</emphasis>):
						Replacement string or a function that's executed after matching to produce the replacement string
					</listitem><listitem>
						n (<emphasis>number</emphasis> or <emphasis>nil</emphasis>):
						Optional maximal number of match/replacement operations. The first match
						is performed on <emphasis>subject</emphasis>, subsequent matches on the result of the preceding
						replacements. Naturally replacement stops when the pattern does not match anymore.
						If <emphasis>n</emphasis> is absent or nil, replacement only stops when <emphasis>pattern</emphasis>
						does not match anymore.
					</listitem><listitem>
						flags (<emphasis>number</emphasis> or <emphasis>nil</emphasis>):
						Optional <productname>spipat</productname> flags, as in <xref linkend="spipat_flags"/>.
					</listitem>
				</orderedlist>
			</refsect1><refsect1>
				<title>Return Values</title>

				<para>
					In case of an exception during matching, raises an error.
					Otherwise returns
				</para>
				<orderedlist>
					<listitem>
						<emphasis>string</emphasis>: The result of the last replacement performed or the original
						<emphasis>subject</emphasis> if no substring matched at all
					</listitem><listitem>
						<emphasis>number</emphasis>: The number of match/replacement operations actually performed
					</listitem>
				</orderedlist>
			</refsect1><refsect1>
				<title>Example</title>

				<example>
					<title>Replacements with spipat.ssub</title>

					<screen>&gt; print(spipat.ssub("abc ccC bab", <link linkend="Span">Span</link>("abc") / function(s) str = s end, function() return "["..str:upper().."]" end, 2))
[ABC] [CC]C BaB
&gt;</screen>
				</example>
			</refsect1>
		</refentry>

		<refentry xml:id="siter">
			<refmeta><refentrytitle>siter</refentrytitle></refmeta>

			<refnamediv>
				<refname>siter</refname>
				<refpurpose>Return iterator of substrings matching a pattern in a subject</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.siter</command>
					( <arg choice="plain"><replaceable>subject</replaceable></arg>
					  <arg choice="plain">, <replaceable>pattern</replaceable></arg>
					  <arg>, <replaceable>flags</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><replaceable>subject</replaceable>:siter</command>
					( <arg choice="plain"><replaceable>pattern</replaceable></arg>
					  <arg>, <replaceable>flags</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Returns an iterator function performing a <emphasis>pattern</emphasis> match on <emphasis>subject</emphasis>
					and returning the matched substring (start/end positions in <emphasis>subject</emphasis>).
					Each time it is called, it begins matching where the last substring ended, but using the same
					<emphasis>subject</emphasis>.
				</para>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist>
					<listitem>subject (<emphasis>string</emphasis>): The subject used for pattern matching</listitem>
					<listitem>
						pattern (<emphasis>userdata</emphasis>): The pattern used for matching.
						Naturally, anchoring the <emphasis>pattern</emphasis> using any of the possible methods is nonsense.
					</listitem><listitem>
						flags (<emphasis>number</emphasis> or <emphasis>nil</emphasis>):
						Optional <productname>spipat</productname> flags, as in <xref linkend="spipat_flags"/>.
					</listitem>
				</orderedlist>
			</refsect1><refsect1>
				<title>Return Values</title>

				<para>
					In case of an exception during matching, raises an error.
					Otherwise returns
				</para>
				<orderedlist><listitem>
					<emphasis>function</emphasis>: The iterator function. Calling it returns
					<orderedlist>
						<listitem><emphasis>number</emphasis>: Start of matched substring</listitem>
						<listitem><emphasis>number</emphasis>: End of matched substring</listitem>
					</orderedlist>
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Example</title>

				<example xml:id="example_siter">
					<title>Iterating through substrings with spipat.siter</title>

					<screen>&gt; str = "abc"
&gt; for s, e in str:siter(<link linkend="Len">Len</link>(1)) do print(str:sub(s, e)) end
a
b
c
&gt;</screen>
				</example>
			</refsect1>
		</refentry>

		<refentry xml:id="free">
			<refmeta><refentrytitle>free</refentrytitle></refmeta>

			<refnamediv>
				<refname>free</refname>
				<refpurpose>Finalize pattern</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.free</command>( <arg choice="plain"><replaceable>pattern</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><replaceable>pattern</replaceable>:free</command>()
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Finalizes <emphasis>pattern</emphasis>, i.e. frees memory associated with it and unreferences any
					other Lua values (other patterns, functions, etc.) so they can get garbage collected.
				</para><para>
					Finalizing an already finalized <emphasis>pattern</emphasis> does nothing.
					Using a finalized <emphasis>pattern</emphasis> in any function or operator working with a pattern
					will raise an error.
				</para>
				<tip><para>
					<command>free</command> does early what would otherwise be done when the pattern is garbage
					collected, so in most cases you will not need it at all.
					It may be useful when you would like to free a large pattern you do not need anymore but
					removing all references to that pattern and enforcing a full garbage collection cycle
					is not feasible.
				</para></tip>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist><listitem>
					pattern (<emphasis>userdata</emphasis>): The pattern to be finalized
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Return Values</title>

				<para>
					Returns nothing.
				</para>
			</refsect1><refsect1>
				<title>Example</title>

				<example>
					<title>Finalizing a pattern</title>

					<screen>&gt; p = <link linkend="Arb">Arb</link>()
&gt; p:free()
&gt; print(p * "foo")
stdin:1: Pattern already freed
&gt;</screen>
				</example>
			</refsect1>
		</refentry>

		<refentry xml:id="conversion">
			<refmeta><refentrytitle>Conversion</refentrytitle></refmeta>

			<refnamediv xml:id="topattern">
				<refname>topattern</refname>
				<refpurpose>Convert a value to a pattern</refpurpose>
			</refnamediv>
			<refnamediv xml:id="tostring">
				<refname>tostring</refname>
				<refpurpose>Render a pattern as a string</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.topattern</command>( <arg choice="plain"><replaceable>value</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>topattern</command>( <arg choice="plain"><replaceable>value</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><replaceable>value</replaceable>:topattern</command>()
				</cmdsynopsis>

				<cmdsynopsis>
					<command>tostring</command>( <arg choice="plain"><replaceable>pattern</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>topattern</command> creates a pattern for a string or number, matching that string or number.
					If <emphasis>value</emphasis> is already a pattern it returns that pattern without modification.
					In case of an unsupported <emphasis>value</emphasis> type or miscelleaneous error, topattern always
					returns nil.
				</para>
				<tip><para>
					<command>topattern</command> is useful to explicitly create pattern, e.g. when an operator requires
					at least one operand to be a pattern but both are strings, numbers or functions.
				</para></tip>
				<para>
					Lua's <link xlink:href="http://www.lua.org/manual/5.1/manual.html#pdf-tostring">built-in <command>tostring</command></link>
					function called on a <emphasis>pattern</emphasis> renders that pattern as a string reminiscent of
					lspipat's pattern construction syntax.
				</para>
				<!--
				<note><para>
					The C++ wrapper overloads different operators than <productname>lspipat</productname> does,
					so <command>tostring</command> return values might look confusing if you are only used to
					<productname>lspipat</productname>.
				</para></note>
				-->
				<!--
				<warning><para>
					spipat's rendering support is quite buggy and has to be considered <emphasis>experimental</emphasis>
					at this point. You are advised not to use <command>tostring</command>.
					For debugging purposes you might want to have a look at
					<link linkend="dump"><command>spipat.dump</command></link>.
				</para></warning>
				-->
			</refsect1><refsect1>
				<title>Example</title>

				<example>
					<title>Explicit pattern construction &amp; implicit conversion to strings</title>

					<screen><![CDATA[> print("2" + 3)
5
> print(topattern("2") + 3)
("2" + "3")
>]]></screen>
				</example>
			</refsect1>
		</refentry>

		<refentry xml:id="dump">
			<refmeta><refentrytitle>dump</refentrytitle></refmeta>

			<refnamediv>
				<refname>dump</refname>
				<refpurpose>Dump a pattern to stdout</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.dump</command>( <arg choice="plain"><replaceable>pattern</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>dump</command> prints information about a <emphasis>pattern</emphasis> to
					<emphasis>stdout</emphasis>.
					The kind of information displayed is similar to <link linkend="tostring">
					<command>tostring</command>'s rendering</link>.
				</para><para>
					It is useful for debugging purposes.
				</para>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist><listitem>
					pattern (<emphasis>userdata</emphasis>): The pattern to be dumped
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Return Values</title>

				<para>
					Returns nothing.
				</para>
			</refsect1>
		</refentry>

		<refentry xml:id="composition">
			<refmeta><refentrytitle>Concatenation and Alternation</refentrytitle></refmeta>

			<refnamediv xml:id="concat">
				<refname>*</refname>
				<refpurpose>Concatenate patterns</refpurpose>
			</refnamediv>
			<refnamediv xml:id="alternate">
				<refname>+</refname>
				<refpurpose>Alternate patterns</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>*</command>
					<arg choice="plain"><replaceable>value</replaceable></arg>
				</cmdsynopsis><cmdsynopsis>
					<arg choice="plain"><replaceable>value</replaceable></arg><command>*</command>
					<arg choice="plain"><replaceable>pattern</replaceable></arg>
				</cmdsynopsis><cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>*</command>
					<arg choice="plain"><replaceable>pattern</replaceable></arg>
				</cmdsynopsis>

				<cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>+</command>
					<arg choice="plain"><replaceable>value</replaceable></arg>
				</cmdsynopsis><cmdsynopsis>
					<arg choice="plain"><replaceable>value</replaceable></arg><command>+</command>
					<arg choice="plain"><replaceable>pattern</replaceable></arg>
				</cmdsynopsis><cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>+</command>
					<arg choice="plain"><replaceable>pattern</replaceable></arg>
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					The <command>*</command> operator constructs a concatenation of two <emphasis>values</emphasis>
					if at least one of them is a <emphasis>pattern</emphasis> and returns the result as a <emphasis>pattern</emphasis>.
					A concatenation matches the left operand immediately followed by the right operand.
				</para><para>
					The <command>+</command> operator constructs an alternation between two <emphasis>values</emphasis>
					if at least one of them is a <emphasis>pattern</emphasis> and returns the result as a <emphasis>pattern</emphasis>.
					An alternation matches the left operand and <emphasis>if unsuccessful</emphasis> the right operand.
				</para><para>
					The non-pattern <emphasis>values</emphasis> may be strings or numbers, which are matched
					just like a <emphasis>pattern</emphasis> built by
					<link linkend="topattern"><command>topattern</command></link>.
				</para>
				<note><para>
					Even though the <emphasis>patterns</emphasis> participating in the composition will be copied,
					references will be kept, so they will not be garbage collected until all <emphasis>patterns</emphasis>
					using them are garbage collected.
				</para></note>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Result of the pattern composition
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Example</title>

				<example>
					<title>Concatenations and Alternations</title>

					<screen>&gt; pat = (<link linkend="topattern">topattern</link>("ABC") + "AB") * (<link linkend="topattern">topattern</link>("DEF") + "CDE") * (<link linkend="topattern">topattern</link>("GH") + "IJ")
&gt; assert(<link linkend="smatch">spipat.smatch</link>("ABCCDEGH", pat))
&gt; assert(<link linkend="smatch">spipat.smatch</link>("ABCDEFIJ", pat))
&gt;</screen>
				</example>
			</refsect1>
		</refentry>

		<refentry xml:id="assignment">
			<refmeta><refentrytitle>Assignment Calls</refentrytitle></refmeta>

			<refnamediv>
				<refname>%</refname>
				<refpurpose>Call Immediately</refpurpose>
			</refnamediv><refnamediv>
				<refname>/</refname>
				<refpurpose>Deferred Call</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>%</command>
					<arg choice="plain"><replaceable>function</replaceable></arg>
				</cmdsynopsis>

				<cmdsynopsis>
					<arg choice="plain"><replaceable>pattern</replaceable></arg><command>/</command>
					<arg choice="plain"><replaceable>function</replaceable></arg>
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					The <command>%</command> operator constructs a pattern matching operand <emphasis>pattern</emphasis> and
					calling a Lua <emphasis>function</emphasis> whenever <emphasis>pattern</emphasis> matches during a pattern
					match (i.e. <emphasis>function</emphasis> may be called more than once while matching regardless of whether
					the match fails or succeeds).
				</para><para>
					On the other hand, the <command>/</command> operator constructs a pattern matching operand
					<emphasis>pattern</emphasis> and calling a Lua <emphasis>function</emphasis> at most once - only if
					the match succeeds.
				</para><para>
					In both cases, <emphasis>function</emphasis> receives the following arguments when called:
					<orderedlist><listitem>
						<emphasis>string</emphasis>: The substring matched by <emphasis>pattern</emphasis>
					</listitem></orderedlist>
					Its return value is ignored.
				</para>
				<note><para>
					Unlike assignment operators in <emphasis>SNOBOL</emphasis>, the <command>%</command> and <command>/</command>
					operators in Lua have the <link xlink:href="http://www.lua.org/manual/5.1/manual.html#2.5.6">same precedence</link>
					as the <link linkend="concat">concatenation operator <command>*</command></link>,
					so using parentheses is advised.
				</para></note>
				<tip><para>
					Deferred assignments (assign on match &amp; assign immediately) are not directly possible but can be
					easily implemented using function closures as described in <xref linkend="deferring"/>.
				</para></tip>
				<note>
					<para>
						Even though the <emphasis>pattern</emphasis> operands will be copied, references will be kept,
						so they will not be garbage collected until all <emphasis>patterns</emphasis>
						using them are garbage collected.
					</para><para>
						Furthermore, references to <emphasis>functions</emphasis> will be kept so they will not be
						garbage collected until the patterns constructed by the operators are garbage collected.
					</para>
				</note>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the operators
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Example</title>

				See <xref linkend="example_siter"/>.
			</refsect1>
		</refentry>

		<refentry xml:id="cursor">
			<refmeta><refentrytitle>Cursor Assignment Calls</refentrytitle></refmeta>

			<refnamediv>
				<refname>Setcur</refname>
				<refpurpose>Cursor Assignment</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.Setcur</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>Setcur</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>#</command><arg choice="plain"><replaceable>function</replaceable></arg>
				</cmdsynopsis>

				<cmdsynopsis>
					<command>spipat._Setcur</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>_Setcur</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>Setcur</command> is a pattern constructor returning a pattern matching the null string <literal>""</literal>
					(i.e. always succeeds when matched) and immediately calling a Lua <emphasis>function</emphasis> when matched.
					This <emphasis>function</emphasis> receives the following arguments when called:
					<orderedlist>
						<listitem>
							<emphasis>number</emphasis>: The cursor in the subject string.
							In other words, the number of characters matched so far from the beginning of the subject string.
						</listitem><listitem>
							<emphasis>cookie</emphasis>: Any Lua value specified as a cookie in the pattern constructor or
							<emphasis>nil</emphasis> if no cookie was specified.
						</listitem>
					</orderedlist>
					Its return value is ignored.
				</para>
				<tip><para>
					The unary <command>#</command> operator is equivalent to the <command>Setcur</command> constructor with no
					<emphasis>cookie</emphasis> specified.
				</para></tip>
				<para>
					<command>_Setcur</command> is similar to <command>Setcur</command> but actually assigns the cursor position to
					the global variable whose name is specified by a <emphasis>string</emphasis> value.
					This means that <code>_Setcur(str)</code> does not assign the cursor position to the global variable <literal>str</literal>
					but rather to the variable with the name <emphasis>str</emphasis> contains, e.g. <literal>foo</literal> if <code>str == "foo"</code>.
					So generally <command>_Setcur</command> is equivalent to:
					<informalexample>
						<programlisting language="lua"><![CDATA[function _Setcur(val)
	return #function(str) _G[val] = str end
end]]></programlisting>
					</informalexample>
					In a similar manner, other kinds of deferred assignments can be implemented
					using function closures as described in <xref linkend="deferring"/>.
				</para>
				<note><para>
					References to <emphasis>function</emphasis> and <emphasis>cookie</emphasis> will be kept so they will not be
					garbage collected until the pattern constructed by <command>Setcur</command> is garbage collected.
				</para></note>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the constructor
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry xml:id="predicate">
			<refmeta><refentrytitle>Predicates</refentrytitle></refmeta>

			<refnamediv xml:id="Pred">
				<refname>Pred</refname>
				<refpurpose>Predicate Constructor</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.Pred</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>Pred</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>-</command><arg choice="plain"><replaceable>function</replaceable></arg>
				</cmdsynopsis>

				<cmdsynopsis>
					<command>spipat._Pred</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>_Pred</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>-</command><arg choice="plain"><replaceable>string</replaceable></arg>
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>Pred</command> constructs a pattern which allows you to transparently define its matching behaviour
					using a <emphasis>function</emphasis> called when this pattern is attempted to be matched.
					It receives the following arguments when invoked:
					<orderedlist><listitem>
						<emphasis>cookie</emphasis>: Any Lua value specified as a cookie in the pattern constructor or
						<emphasis>nil</emphasis> if no cookie was specified.
					</listitem></orderedlist>
				</para><para>
					The <emphasis>function's</emphasis> return value defines the behaviour dynamically, as shown in the following table:

					<table xml:id="dynfnc">
						<title>Dynamic Function Return Values</title>

						<tgroup cols="3" colsep="1" rowsep="1">
							<colspec colname="value"/>
							<colspec colname="type"/>
							<colspec colname="behaviour"/>

							<thead>
								<row>
									<entry>Value</entry>
									<entry>Type</entry>
									<entry>Behaviour</entry>
								</row>
							</thead><tbody>
								<row>
									<entry>nil</entry>
									<entry>nil</entry>
									<entry morerows="1"><para>
										Match the <literal>""</literal> string, i.e. succeed.
									</para></entry>
								</row><row>
									<entry>true</entry>
									<entry morerows="1">boolean</entry>
								</row><row>
									<entry>false</entry>
									<!-- <entry>boolean</entry> -->
									<entry><para>
										Pattern match fails, like when using the
										<link linkend="Fail"><command>Fail</command> primitive</link>.
									</para></entry>
								</row><row>
									<entry namest="value" nameend="type" align="center">any <emphasis>number</emphasis></entry>
									<entry><para>
										Try to match that number as a string, as if
										<link linkend="topattern">converted to a pattern</link>.
									</para></entry>
								</row><row>
									<entry namest="value" nameend="type" align="center">any <emphasis>string</emphasis></entry>
									<entry><para>
										Try to match that string, as if
										<link linkend="topattern">converted to a pattern</link>.
									</para></entry>
								</row><row>
									<entry namest="value" nameend="type" align="center">any <emphasis>pattern</emphasis></entry>
									<entry><para>
										Try to match that pattern. Returning a pattern assigned to a variable is the way
										to implement recursive patterns.
									</para></entry>
								</row>
							</tbody>
						</tgroup>
					</table>
				</para>
				<tip><para>
					The unary <command>-</command> operator applied to a <emphasis>function</emphasis> is equivalent
					to the <command>Pred</command> constructor with no <emphasis>cookie</emphasis> specified.
				</para></tip>
				<para>
					<command>_Pred</command> is similar to <command>Pred</command> but actually gets the Lua value defining its behaviour from
					the global variable whose name is specified by a <emphasis>string</emphasis> value.
					This means that <code>_Pred(str)</code> does not get the value from the global variable <literal>str</literal>
					but rather from the variable with the name <emphasis>str</emphasis> contains, e.g. <literal>foo</literal> if <code>str == "foo"</code>.
					So generally <command>_Pred</command> is equivalent to:
					<informalexample>
						<programlisting language="lua"><![CDATA[function _Pred(val)
	return -function() return _G[val] end
end]]></programlisting>
					</informalexample>
					In a similar manner, other kinds of variable deferring as well as recursive patterns can be implemented
					using function closures as described in <xref linkend="deferring"/>.
				</para>
				<tip><para>
					The unary <command>-</command> operator applied to a <emphasis>string</emphasis> which is not convertable to
					a <emphasis>number</emphasis> is equivalent to the <command>_Pred</command> constructor - naturally this
					<emphasis>should</emphasis> be true for all global variable names.
					This constraint comes from the way Lua handles operations by default (it checks whether it is an arithmetic operation
					before evaluating any metamethod - see <link xlink:href="http://www.lua.org/manual/5.1/manual.html#2.8">metatables</link>).
				</para></tip>
				<note><para>
					References to <emphasis>function</emphasis> and <emphasis>cookie</emphasis> will be kept so they will not be
					garbage collected until the pattern constructed by <command>Pred</command> is garbage collected.
				</para></note>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the constructor
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry xml:id="string_primitives">
			<refmeta><refentrytitle>String Primitives</refentrytitle></refmeta>

			<refnamediv xml:id="Any">
				<refname>Any</refname>
				<refpurpose>Match any character in a set</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>NotAny</refname>
				<refpurpose>Match any character not in a set</refpurpose>
			</refnamediv>
			<refnamediv xml:id="Break">
				<refname>Break</refname>
				<refpurpose>Match characters up to a break character</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>BreakX</refname>
				<refpurpose>Match characters up to a break character (extending)</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>NSpan</refname>
				<refpurpose>Match nothing or characters from a set</refpurpose>
			</refnamediv>
			<refnamediv xml:id="Span">
				<refname>Span</refname>
				<refpurpose>Match characters from a set</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command><arg>spipat.</arg>Any</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Any</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Any</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>NotAny</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>NotAny</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_NotAny</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>Break</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Break</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Break</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>BreakX</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>BreakX</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_BreakX</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>NSpan</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>NSpan</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_NSpan</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>Span</command>( <arg choice="plain"><replaceable>set</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Span</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Span</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					String primitives are pattern constructors that in their first form all take a <emphasis>string</emphasis> or
					<emphasis>number</emphasis> (which is converted to a <emphasis>string</emphasis>) as their sole argument
					(<emphasis>set</emphasis>).
				</para><para>
					In their second form they take a Lua <emphasis>function</emphasis> and an optional <emphasis>cookie</emphasis>
					as arguments. When the constructed pattern is about to be matched, the <emphasis>function</emphasis> is called
					and is supposed to return a <emphasis>string</emphasis> or <emphasis>number</emphasis> (which is converted to
					a <emphasis>string</emphasis>) to supply the primitive's argument dynamically.
					It receives the following arguments when invoked:
					<orderedlist><listitem>
						<emphasis>cookie</emphasis>: Any Lua value specified as a cookie in the pattern constructor or
						<emphasis>nil</emphasis> if no cookie was specified.
					</listitem></orderedlist>
				</para><para>
					The primitives with a leading underscore (e.g. <command>_Any</command>) are similar but actually get their argument
					from a global variable with the name a <emphasis>string</emphasis> argument contains.
					This means that for instance <code>_Any(str)</code> does not get its character set from the global variable <literal>str</literal>
					but rather from the variable with the name <emphasis>str</emphasis> contains, e.g. <literal>foo</literal> if <code>str == "foo"</code>.
					So generally <command>_Any</command> is equivalent to:
					<informalexample>
						<programlisting language="lua"><![CDATA[function _Any(val)
	return Any(function() return _G[val] end)
end]]></programlisting>
					</informalexample>
					In a similar manner, other kinds of variable deferring can be implemented
					using function closures as described in <xref linkend="deferring"/>.
				</para>
				<note><para>
					References to <emphasis>function</emphasis> and <emphasis>cookie</emphasis> will be kept so they will not be
					garbage collected until the pattern constructed is garbage collected.
				</para></note>
				<para>
					The following table describes what these primitives do:

					<!-- Largely copied from the GNAT.SPITBOL docs -->
					<table>
						<title>String Primitives</title>

						<tgroup cols="2" colsep="1" rowsep="1">
							<colspec colwidth="10%"/>

							<thead>
								<row>
									<entry>Primitive</entry>
									<entry>Description</entry>
								</row>
							</thead><tbody>
								<row>
									<entry><command>Any</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, matches a single character that is
										any one of the characters in S. Fails if the current
										character is not one of the given set of characters.
									</para></entry>
								</row><row>
									<entry><command>NotAny</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, matches a single character that is
										not one of the characters of S. Fails if the current
										characer is one of the given set of characters.
									</para></entry>
								</row><row>
									<entry><command>Break</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, matches a string of zero or more
										characters up to but not including a break character
										that is one of the characters given in the string S.
										Can match the null string, but cannot match the last
										character in the string, since a break character is
										required to be present.
									</para></entry>
								</row><row>
									<entry><command>BreakX</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, behaves exactly like <command>Break</command>(S) when
										it first matches, but if a string is successfully matched,
										then a susequent failure causes an attempt to extend the
										matched string.
									</para></entry>
								</row><row>
									<entry><command>NSpan</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, matches a string of zero or more
										characters that is among the characters given in the
										string. Always matches the longest possible such string.
										Always succeeds, since it can match the null string.
									</para></entry>
								</row><row>
									<entry><command>Span</command>( <arg choice="plain"><replaceable>S</replaceable></arg> )</entry>
									<entry><para>
										Where S is a string, matches a string of one or more
										characters that is among the characters given in the
										string. Always matches the longest possible such string.
										Fails if the current character is not one of the given
										set of characters.
									</para></entry>
								</row>
							</tbody>
						</tgroup>
					</table>
				</para>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the constructor
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry>
			<refmeta><refentrytitle>Arbno</refentrytitle></refmeta>

			<refnamediv>
				<refname>Arbno</refname>
				<refpurpose>Matches a pattern any number of times</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.Arbno</command>( <arg choice="plain"><replaceable>P</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>Arbno</command>( <arg choice="plain"><replaceable>P</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Where P is any pattern, matches any number of instances
					of the pattern, starting with zero occurrences. It is
					thus equivalent to <code>("" + (P * ("" + (P * ("" ....))))</code>.
					The pattern P may contain any number of pattern elements
					including the use of alternation and concatenation.
				</para><para>
					<command>Arbno</command> is a pattern constructor taking exactly one argument which is
					either a <emphasis>pattern</emphasis> or <emphasis>string</emphasis> (which is treated
					like it is <link linkend="topattern">converted to a pattern</link> first).
				</para>
				<note><para>
					A reference to <emphasis>P</emphasis> will be kept if it is a <emphasis>pattern</emphasis>
					so it will not be garbage collected until the pattern constructed is garbage collected.
				</para></note>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by <command>Arbno</command>
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry>
			<refmeta><refentrytitle>Fence</refentrytitle></refmeta>

			<refnamediv>
				<refname>Fence</refname>
				<refpurpose>Abort match when alternations are sought</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.Fence</command>( <arg><replaceable>P</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>Fence</command>( <arg><replaceable>P</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>Fence</command> is a pattern constructor taking no or exactly one
					<emphasis>pattern</emphasis> as an argument.
				</para>
				<note><para>
					A reference to pattern <emphasis>P</emphasis> will be kept so it will not
					be garbage collected until the pattern constructed is garbage collected.
				</para></note>
				<para>
					The following table describes what the two versions do:

					<!-- Largely copied from the GNAT.SPITBOL docs -->
					<table>
						<title>Fence Primitive</title>

						<tgroup cols="2" colsep="1" rowsep="1">
							<colspec colwidth="10%"/>

							<thead>
								<row>
									<entry>Primitive</entry>
									<entry>Description</entry>
								</row>
							</thead><tbody>
								<row>
									<entry><command>Fence</command>()</entry>
									<entry><para>
										Matches the null string at first, and then if a failure
										causes alternatives to be sought, aborts the match (like
										a Cancel). Note that using <command>Fence</command> at the
										start of a pattern has the same effect as matching in anchored mode.
									</para></entry>
								</row><row>
									<entry><command>Fence</command>( <arg choice="plain"><replaceable>P</replaceable></arg> )</entry>
									<entry><para>
										Where P is a pattern, attempts to match the pattern P
										including trying all possible alternatives of P. If none
										of these alternatives succeeds, then the <command>Fence</command> pattern
										fails. If one alternative succeeds, then the pattern
										match proceeds, but on a subsequent failure, no attempt
										is made to search for alternative matches of P. The
										pattern P may contain any number of pattern elements
										including the use of alternatiion and concatenation.
									</para></entry>
								</row>
							</tbody>
						</tgroup>
					</table>
				</para>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by <command>Fence</command>
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry xml:id="uint_primitives">
			<refmeta><refentrytitle>Integer Primitives</refentrytitle></refmeta>

			<refnamediv xml:id="Len">
				<refname>Len</refname>
				<refpurpose>Match a number of characters</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Pos</refname>
				<refpurpose>Match null string if number of characters have been matched</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>RPos</refname>
				<refpurpose>Match null string if number of characters remain to be matched</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Tab</refname>
				<refpurpose>Match characters until number of characters have been matched</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>RTab</refname>
				<refpurpose>Match characters until number of characters remain to be matched</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command><arg>spipat.</arg>Len</command>( <arg><replaceable>n</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Len</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Len</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>Pos</command>( <arg><replaceable>n</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Pos</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Pos</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>RPos</command>( <arg><replaceable>n</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>RPos</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_RPos</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>Tab</command>( <arg><replaceable>n</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>Tab</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_Tab</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>

				<cmdsynopsis>
					<command><arg>spipat.</arg>RTab</command>( <arg><replaceable>n</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>RTab</command>
					( <arg choice="plain"><replaceable>function</replaceable></arg><arg>, <replaceable>cookie</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command><arg>spipat.</arg>_RTab</command>( <arg choice="plain"><replaceable>string</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					Integer primitives are pattern constructors that in their first form all take a <emphasis>number</emphasis> or
					<emphasis>string</emphasis> (which is converted to a <emphasis>number</emphasis>) as their sole argument
					(<emphasis>n</emphasis>).
					This <emphasis>number</emphasis> has to be an unsigned integer - sometimes a natural number depending on the
					primitive.
				</para>
				<tip><para>
					If the argument is ommitted, <emphasis>zero</emphasis> is assumed.
				</para></tip>
				<para>
					In their second form the primitives take a Lua <emphasis>function</emphasis> and an optional <emphasis>cookie</emphasis>
					as arguments. When the constructed pattern is about to be matched, the <emphasis>function</emphasis> is called
					and is supposed to return a <emphasis>number</emphasis> or <emphasis>string</emphasis> (which is converted to
					a <emphasis>number</emphasis>) to supply the primitive's argument dynamically.
					It receives the following arguments when invoked:
					<orderedlist><listitem>
						<emphasis>cookie</emphasis>: Any Lua value specified as a cookie in the pattern constructor or
						<emphasis>nil</emphasis> if no cookie was specified.
					</listitem></orderedlist>
				</para><para>
					The primitives with a leading underscore (e.g. <command>_Len</command>) are similar but actually get their argument
					from a global variable with the name a <emphasis>string</emphasis> argument contains.
					This means that for instance <code>_Len(str)</code> does not get its argument from the global variable <literal>str</literal>
					but rather from the variable with the name <emphasis>str</emphasis> contains, e.g. <literal>foo</literal> if <code>str == "foo"</code>.
					So generally <command>_Len</command> is equivalent to:
					<informalexample>
						<programlisting language="lua"><![CDATA[function _Len(val)
	return Len(function() return _G[val] end)
end]]></programlisting>
					</informalexample>
					In a similar manner, other kinds of variable deferring can be implemented
					using function closures as described in <xref linkend="deferring"/>.
				</para>
				<note><para>
					References to <emphasis>function</emphasis> and <emphasis>cookie</emphasis> will be kept so they will not be
					garbage collected until the pattern constructed is garbage collected.
				</para></note>
				<para>
					The following table describes what these primitives do:

					<!-- Largely copied from the GNAT.SPITBOL docs -->
					<table>
						<title>Integer Primitives</title>

						<tgroup cols="2" colsep="1" rowsep="1">
							<colspec colwidth="10%"/>

							<thead>
								<row>
									<entry>Primitive</entry>
									<entry>Description</entry>
								</row>
							</thead><tbody>
								<row>
									<entry><command>Len</command>( <arg choice="plain"><replaceable>N</replaceable></arg> )</entry>
									<entry><para>
										Where N is a natural number, matches the given number of
										characters. For example, <code>Len(10)</code> matches any string that
										is exactly ten characters long.
									</para></entry>
								</row><row>
									<entry><command>Pos</command>( <arg choice="plain"><replaceable>N</replaceable></arg> )</entry>
									<entry><para>
										Where N is a natural number, matches the null string
										if exactly N characters have been matched so far, and
										otherwise fails.
									</para></entry>
								</row><row>
									<entry><command>RPos</command>( <arg choice="plain"><replaceable>N</replaceable></arg> )</entry>
									<entry><para>
										Where N is a natural number, matches the null string
										if exactly N characters remain to be matched, and
										otherwise fails.
									</para></entry>
								</row><row>
									<entry><command>Tab</command>( <arg choice="plain"><replaceable>N</replaceable></arg> )</entry>
									<entry><para>
										Where N is a natural number, matches characters from
										the current position until exactly N characters have
										been matched in all. Fails if more than N characters
										have already been matched.
									</para></entry>
								</row><row>
									<entry><command>RTab</command>( <arg choice="plain"><replaceable>N</replaceable></arg> )</entry>
									<entry><para>
										Where N is a natural number, matches characters from
										the current position until exactly N characters remain
										to be matched in the string. Fails if fewer than N
										unmatched characters remain in the string.
									</para></entry>
								</row>
							</tbody>
						</tgroup>
					</table>
				</para>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the constructor
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry xml:id="misc_primitives">
			<refmeta><refentrytitle>Miscelleanous Primitives</refentrytitle></refmeta>

			<refnamediv xml:id="Arb">
				<refname>Arb</refname>
				<refpurpose>Matches any string</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Bal</refname>
				<refpurpose>Matches parentheses balanced strings</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Abort</refname>
				<refpurpose>Immediately abort pattern match</refpurpose>
			</refnamediv>
			<refnamediv xml:id="Fail">
				<refname>Fail</refname>
				<refpurpose>Null alternation</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Rem</refname>
				<refpurpose>Match the entire remaining subject string</refpurpose>
			</refnamediv>
			<refnamediv>
				<refname>Succeed</refname>
				<refpurpose>Match the null string in every alternative</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.Arb</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Arb</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>spipat.Bal</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Bal</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>spipat.Abort</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Abort</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>spipat.Fail</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Fail</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>spipat.Rem</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Rem</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>spipat.Succeed</command>()
				</cmdsynopsis><cmdsynopsis>
					<command>Succeed</command>()
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					These are <emphasis>simple</emphasis> pattern constructor
					functions.
				</para><para>
					The following table describes what these primitives do:

					<!-- Largely copied from the GNAT.SPITBOL docs -->
					<table>
						<title>Miscelleanous Primitives</title>

						<tgroup cols="2" colsep="1" rowsep="1">
							<colspec colwidth="10%"/>

							<thead>
								<row>
									<entry>Primitive</entry>
									<entry>Description</entry>
								</row>
							</thead><tbody>
								<row>
									<entry><command>Arb</command>()</entry>
									<entry><para>
										Matches any string. First it matches the null string, and
										then on a subsequent failure, matches one character, and
										then two characters, and so on. It only fails if the
										entire remaining string is matched.
									</para></entry>
								</row><row>
									<entry><command>Bal</command>()</entry>
									<entry><para>
										Matches a non-empty string that is parentheses balanced
										with respect to ordinary <literal>()</literal> characters.
										Examples of balanced strings are <literal>"ABC"</literal>,
										<literal>"A((B)C)"</literal>, and <literal>"A(B)C(D)E"</literal>.
										<command>Bal</command> matches the shortest possible balanced
										string on the first attempt, and if there is a subsequent failure,
										attempts to extend the string.
									</para></entry>
								</row><row>
									<entry><command>Abort</command>()</entry>
									<entry><para>
										Immediately aborts the entire pattern match, signalling
										failure. This is a specialized pattern element, which is
										useful in conjunction with some of the special pattern
										elements that have side effects.
									</para></entry>
								</row><row>
									<entry><command>Fail</command>()</entry>
									<entry><para>
										The null alternation. Matches no possible strings, so it
										always signals failure. This is a specialized pattern
										element, which is useful in conjunction with some of the
										special pattern elements that have side effects.
									</para></entry>
								</row><row>
									<entry><command>Rem</command>()</entry>
									<entry><para>
										Matches from the current point to the last character in
										the string. This is a specialized pattern element, which
										is useful in conjunction with some of the special pattern
										elements that have side effects.
									</para></entry>
								</row><row>
									<entry><command>Succeed</command>()</entry>
									<entry><para>
										Repeatedly matches the null string (it is equivalent to
										the alternation <code>("" + "" + "" ....)</code>. This is a special
										pattern element, which is useful in conjunction with some
										of the special pattern elements that have side effects.
									</para></entry>
								</row>
							</tbody>
						</tgroup>
					</table>
				</para>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by the constructor
				</listitem></orderedlist>
			</refsect1>
			<!--<refsect1>
				<title>Example</title>

			</refsect1>--> <!-- TODO -->
		</refentry>

		<refentry xml:id="RegExp">
			<refmeta><refentrytitle>POSIX Extended Regular Expressions</refentrytitle></refmeta>

			<refnamediv>
				<refname>RegExp</refname>
				<refpurpose>Matches a pattern equivalent to a regular expression</refpurpose>
			</refnamediv>

			<refsynopsisdiv>
				<cmdsynopsis>
					<command>spipat.RegExp</command>
					( <arg choice="plain"><replaceable>expression</replaceable></arg><arg>, <replaceable>captures</replaceable></arg> )
				</cmdsynopsis><cmdsynopsis>
					<command>RegExp</command>
					( <arg choice="plain"><replaceable>expression</replaceable></arg><arg>, <replaceable>captures</replaceable></arg> )
				</cmdsynopsis>
			</refsynopsisdiv>

			<refsect1>
				<title>Description</title>

				<para>
					<command>RegExp</command> constructs from a
					<link xlink:href="http://www.opengroup.org/onlinepubs/7990989775/xbd/re.html#tag_007_004">
					POSIX Extended Regular Expression</link>, a pattern that is equivalent to that regular
					<emphasis>expression</emphasis> and can be combined with other patterns freely.
				</para><para>
					It can optionally construct the pattern to save the <emphasis>captures</emphasis>
					from a regular expression match in a Lua <emphasis>table</emphasis>.
				</para>
				<warning><para>
					Even though this implementation should support almost all elements of EREs,
					it is considered <emphasis>experimental</emphasis>.
					You are advised to use the usual pattern construction primitives.
				</para></warning>
			</refsect1><refsect1>
				<title>Parameters</title>

				<orderedlist>
					<listitem>
						expression (<emphasis>string</emphasis>): The POSIX ERE which is compiled
						to a pattern.
					</listitem><listitem>
						captures (<emphasis>table</emphasis>): Optional table, or more precisely
						array, to hold subexpression captures.
						Naturally, it has to exist when <command>RegExp</command> is called.
						When a subexpression is captured (i.e. the pattern equivalent to what is
						enclosed in parentheses), the matching string is added to the
						end of the table.
						Thus taken that <emphasis>captures</emphasis> is initially empty, if
						<code>RegExp("(a(b))", captures)</code> matches, <emphasis>captures</emphasis>
						will be <code>{"b", "ab"}</code>.
					</listitem>
				</orderedlist>
			</refsect1><refsect1>
				<title>Return Values</title>

				<orderedlist><listitem>
					<emphasis>pattern</emphasis> (userdata): Pattern built by <command>RegExp</command>
				</listitem></orderedlist>
			</refsect1><refsect1>
				<title>Example</title>

				<example>
					<title>Regular Expressions</title>

					<screen><![CDATA[> print(RegExp "^[[:digit:]]*?(abc\\.|de?)")
Pos(0) * Arbno(Any(<CS>)) * ("abc." + "d" * ("" + "e"))
>]]></screen>
				</example>
			</refsect1>
		</refentry>
	</reference>
</book>