forked from william8000/lout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prg2lout.c
5538 lines (4913 loc) · 257 KB
/
prg2lout.c
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
/*****************************************************************************/
/* */
/* PRG2LOUT: A PROGRAM TO CONVERT PROGRAM SOURCES INTO LOUT */
/* COPYRIGHT (C) 2000, 2008 Jeffrey H. Kingston */
/* */
/* Part of Lout Version 3.39 */
/* */
/* Jeffrey H. Kingston (jeff@cs.su.oz.au) */
/* Basser Department of Computer Science */
/* The University of Sydney 2006 */
/* AUSTRALIA */
/* */
/* C and C++, Eiffel, Blue, Java, and Nonpareil by Jeff Kingston */
/* Perl and Pod by Jeff Kingston and Mark Summerfield */
/* Python by Mark Summerfield (Python 2.5 update Nov 2006) */
/* Ruby by Michael Piotrowski */
/* Haskell by Thorsten Seitz (Nov 2002), mods by Gabor Greif */
/* RSL by Darren Bane (February 2003) */
/* JavaScript by Mark Summerfield (Nov 2010) */
/* Tcl by Mark Summerfield (Nov 2010) */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either Version 3, or (at your option) */
/* any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* GENERAL INTRODUCTION TO PRG2LOUT */
/* */
/* The best way to see what the aim of prg2lout as currently conceived is, */
/* is to look in file cprint at the setup file options. You will see that */
/* the aim is to provide three basic styles: fixed (essentially mono font), */
/* varying (essentially varying-width font with various faces for different */
/* elements at the user's choice), and symbol (similar to varying). */
/* */
/* The elements currently aimed for are strings, identifiers, comments, */
/* keywords, numbers, and operators, and the end user is able to choose, */
/* for each of these kinds of elements, which font to set them in. */
/* */
/* This is achieved by a simple division of labour: prg2lout does the */
/* classifying of the input into a sequence of these elements, and the Lout */
/* end (cprint and cprintf, or their clones for other languages) does the */
/* formatting. For example, the C text */
/* */
/* inc = inc / 2 */
/* */
/* would be classified by prg2lout into identifier, operator, identifier, */
/* operator, number; and consequently prg2lout would emit */
/* */
/* @PI{inc} @PO{=} @PI{inc} @PO{"/"} @PN{2} */
/* */
/* which is readable by Lout, thanks to having quotes around everything */
/* potentially dangerous, and clearly tells Lout, by means of the commands */
/* @PC, @PI, etc., how each part of the input has been classified. */
/* */
/* The actual classification is carried out by prg2lout as follows. Each */
/* programming language is described to prg2lout as a collection of tokens; */
/* you say what the token begins with, what's a legal character inside the */
/* token, and how it ends. You also say which command (@PC, @PI etc.) to */
/* emit when a token of that kind is found. Prg2lout does the rest. */
/* */
/* Prg2lout knows all about tricky problems such as multi-line tokens (it */
/* breaks them up into single-line pieces) and backslash in Lout strings */
/* (it replaces any \ within an output string by \\, " by \", etc.). It */
/* also handles tab characters and formfeed characters properly, and it */
/* produces intelligible error messages when unexpected things happen, */
/* such as input terminating in the middle of a string. This attention to */
/* detail is a strong reason for using prg2lout rather than something more */
/* ad-hoc, such as @Verbatim or a quick script. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* HOW TO ADD ANOTHER LANGUAGE TO PRG2LOUT */
/* */
/* Step 1. Construct clones of (say) eiffel and eiffelf (these are in */
/* $LOUTLIB/include) with every occurrence of eiffel or Eiffel in them */
/* changed to your language as appropriate. Install your files in the */
/* Lout system include directory alongside eiffel and eiffelf. */
/* */
/* It is good to clone the files exactly because that way all program */
/* formatting works the same way, and one chapter of the User's Guide */
/* covers the lot. However if your language has some unique element, not */
/* readily classifiable as a string, identifier, comment, keyword, */
/* number, or operator, it is possible to emit a different command of */
/* your choice for the new element; but then your clones of eiffel and */
/* eiffelf have to be extended to handle that command. */
/* */
/* Step 2. Have a browse through the token declarations below, and work */
/* out which of them you need for your language. If you need a token that */
/* isn't there already, you'll have to define it; there are many examples */
/* and documentation there to help you. The tokens for Perl are rather */
/* complicated and don't make a good model for most languages, so look */
/* more at the C and Eiffel ones. */
/* */
/* Step 3. Browse through the language declarations, and declare your */
/* language following those examples: first you give a set of one or more */
/* alternative names for your language, then some other things, including */
/* the list of tokens of the language, and its keywords. */
/* */
/* Step 4. Add your language variable to the list in the initializer of */
/* variable languages, as you can see the others have been done. Try to */
/* keep the list alphabetical to deflect any charges of language bias. */
/* */
/* Step 5. If any lists of initializers now contain more than MAX_STARTS, */
/* MAX_STARTS2, MAX_NAMES, MAX_TOKENS, or MAX_KEYWORDS elements, increase */
/* these constants until they don't. The gcc compiler will warn you if */
/* you forget to do this. */
/* */
/* Step 6. Recompile and reinstall prg2lout, test "prg2lout -u" then */
/* "prg2lout -l <mylanguage> <myfile> | lout -s > out.ps". */
/* */
/* Step 7. Send your tested and tidied files to me for incorporation */
/* in the next Lout release. If you do this, please try hard to ensure */
/* that your new code conforms to the formal definition of your language. */
/* Feel free to email me for advice as you go along. */
/* */
/* Jeff Kingston */
/* jeff@it.usyd.edu.au */
/* */
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define BOOLEAN unsigned
#define MAX_CHAR 256
#define is_whitespace(ch) ((ch)==' ' || (ch)=='\t' || (ch)=='\n' || (ch)=='\f')
#define U (unsigned char *)
/*****************************************************************************/
/* */
/* MAX_STARTS 1 + Maximum length of "starts" array in any token */
/* MAX_STARTS2 1 + Maximum length of "starts2" array in any token */
/* MAX_NAMES 1 + Maximum number of names for any language */
/* MAX_TOKENS 1 + Maximum number of tokens in any language */
/* MAX_KEYWORDS 1 + Maximum number of keywords in any language */
/* */
/*****************************************************************************/
#define MAX_STARTS 120
#define MAX_STARTS2 30
#define MAX_NAMES 10
#define MAX_TOKENS 150
#define MAX_KEYWORDS 350
/*****************************************************************************/
/* */
/* Bracketing pairs */
/* */
/* This declaration explains to prg2lout that { matches }, etc. */
/* */
/*****************************************************************************/
typedef struct {
unsigned char *first;
unsigned char *second;
} CHAR_PAIR;
CHAR_PAIR pairs[] = {
{ (unsigned char *) "(", (unsigned char *) ")" },
{ (unsigned char *) "{", (unsigned char *) "}" },
{ (unsigned char *) "[", (unsigned char *) "]" },
{ (unsigned char *) "<", (unsigned char *) ">" },
{ NULL, NULL }
};
/*****************************************************************************/
/* */
/* Character sets */
/* */
/* These are prg2lout's definitions of various commonly needed sets of */
/* characters. May need enhancement for Latin1 etc. */
/* */
/*****************************************************************************/
#define AllCharacters NULL /* code will recognize NULL and do this */
/* It is not possible to further categorize the characters in the G1
* area of ISO 8859 code sets (code points 0xA0 through 0xFF) because
* there are no fixed ranges (e.g., 0xA1 is a punctuation mark in
* Latin 1, but a letter in Latin 2). However, this is not really a
* problem since all characters in this area can be considered
* printable. */
#define G1_Characters "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
unsigned char AllPrintable[] =
" !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`\\{|}~\
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" G1_Characters ;
unsigned char AllPrintablePlusNL[] =
" !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`\\{|}~\
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n" G1_Characters ;
unsigned char AllPrintablePlusTab[] =
" !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`\\{|}~\
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\t" G1_Characters ;
unsigned char AllPrintableTabNL[] =
" !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`\\{|}~\
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n\t" G1_Characters ;
unsigned char AllPrintableTabNLFF[] =
" !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`\\{|}~\
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n\t\f" G1_Characters ;
unsigned char Letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
unsigned char lowercaseLetters[] = "abcdefghijklmnopqrstuvwxyz" ;
unsigned char uppercaseLetters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
unsigned char Letter_Digit[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789" ;
unsigned char Letter_Digit_Quotes[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789`'" ;
unsigned char HaskellOpCharacters[] = "!#$%&*+./<=>?^|:-~";
unsigned char NonpareilOperatorPunct[] = "@$%^&*=+|<>/?`";
unsigned char Ruby_Methodname[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789?!=" ;
#define UppercaseSepLetters \
U "A", U "B", U "C", U "D", U "E", U "F", U "G", U "H", U "I", U "J", \
U "K", U "L", U "M", U "N", U "O", U "P", U "Q", U "R", U "S", U "T", \
U "U", U "V", U "W", U "X", U "Y", U "Z"
#define LowercaseSepLetters \
U "a", U "b", U "c", U "d", U "e", U "f", U "g", U "h", U "i", U "j", \
U "k", U "l", U "m", U "n", U "o", U "p", U "q", U "r", U "s", U "t", \
U "u", U "v", U "w", U "x", U "y", U "z"
#define SepLetters UppercaseSepLetters, LowercaseSepLetters
#define SepDigits \
U "0", U "1", U "2", U "3", U "4", U "5", U "6", U "7", U "8", U "9"
#define HexDigits \
U "A", U "a", U "B", U "b", U "C", U "c", U "D", U "d", U "E", U "e", \
U "F", U "f"
#define SepPunct \
U "/", U "(", U "[", U "{", U "<", U "!", U "%", U "#", U "|", U ",", \
U ":", U ";", U "$", U "\"", U "^", U "&", U "*", U "-", U "=", U "+", \
U "~", U "'", U "@", U "?", U ".", U "`"
#define BktPunct \
U "", U "(", U "[", U "{", U "<", U "", U "", U "", U "", U "", \
U "", U "", U "", U "", U "", U "", U "", U "", U "", U "", \
U "", U "", U "", U "", U "", U ""
#define EndPunct \
U "/", U ")", U "]", U "}", U ">", U "!", U "%", U "#", U "|", U ",", \
U ":", U ";", U "$", U "\"", U "^", U "&", U "*", U "-", U "=", U "+", \
U "~", U "'", U "@", U "?", U ".", U "`"
#define SepNonpareilOperatorPunct \
U "@", U "$", U "%", U "^", U "&", U "*", U "=", U "+", U "|", \
U "<", U ">", U "/", U "?", U "`"
#define HaskellOpChars \
U "!", U "#", U "$", U "%", U "&", U "*", U "+", U ".", U "/", \
U "<", U "=", U ">", U "?", U "^", U "|", U ":", U "-", U "~"
#define HaskellParenOpChars \
U "(!", U "(#", U "($", U "(%", U "(&", U "(*", U "(+", U "(.", U "(/", \
U "(<", U "(=", U "(>", U "(?", U "(^", U "(|", U "(:", U "(-", U "(~"
#define PercentLetters \
U "%A", U "%B", U "%C", U "%D", U "%E", U "%F", U "%G", U "%H", U "%I", \
U "%J", U "%K", U "%L", U "%M", U "%N", U "%O", U "%P", U "%Q", U "%R", \
U "%S", U "%T", U "%U", U "%V", U "%W", U "%X", U "%Y", U "%Z", \
U "%a", U "%b", U "%c", U "%d", U "%e", U "%f", U "%g", U "%h", U "%i", \
U "%j", U "%k", U "%l", U "%m", U "%n", U "%o", U "%p", U "%q", U "%r", \
U "%s", U "%t", U "%u", U "%v", U "%w", U "%x", U "%y", U "%z", U "%_"
/*****************************************************************************/
/* */
/* TOKEN - put your token declarations in this section */
/* */
/* The fields of token_rec have the following meanings: */
/* */
/* name */
/* The name of this token, e.g. "string" or "identifier". This field */
/* is used only by error messages generated by prg2lout; for example, */
/* prg2lout might print the message "input ended within string". */
/* */
/* print_style */
/* */
/* print_style What gets printed */
/* ------------------------------------------------------- */
/* PRINT_WHOLE_QUOTED command{"token"} */
/* PRINT_NODELIMS_QUOTED command{"token-minus-delims"} */
/* PRINT_WHOLE_UNQUOTED command{token} */
/* PRINT_NODELIMS_UNQUOTED command{token-minus-delims} */
/* PRINT_NODELIMS_INNER command{inner} */
/* PRINT_COMMAND_ONLY command */
/* */
/* If command (see next) is empty then the braces {} are not printed. */
/* */
/* PRINT_WHOLE_QUOTED. This command is the most frequently used one; */
/* it prints the token, enclosed in braces and quotes, preceded by the */
/* command. The quotes ensure that the result is legal Lout; any " or */
/* \ in the token is printed with a preceding \ as required in Lout. */
/* The usual arrangement for handling white space is that none of the */
/* tokens contain it; when it is encountered prg2lout generates the */
/* appropriate Lout without being told: a space for a space, a newline */
/* for a newline (possibly triggering a line number on the next line), */
/* @NP for a formfeed, and something clever for tab which does the */
/* required thing. However, you can define a token that contains */
/* white space if you wish, and then the effect will be: */
/* */
/* space and tab The quotation marks will be temporarily */
/* closed off, the white space handled as just */
/* described, then the quotes opened again */
/* */
/* newline and ff Both the quotation marks and the command */
/* will be closed off, the white space handled */
/* as just described, and then a new command */
/* started. In effect, the token is broken into */
/* a sequence of tokens at these characters. */
/* */
/* PRINT_NODELIMS_QUOTED. This is like PRINT_WHOLE_QUOTED except that */
/* the opening and closing delimiters of the token are omitted from */
/* the print. This is useful occasionally when these delimiters are */
/* formatting markers, not intended to be printed. */
/* */
/* PRINT_WHOLE_UNQUOTED. This style prints the command and braces */
/* as usual, but omits the quotes and prints the token absolutely */
/* verbatim. In general this is not going to produce legal Lout, */
/* but it is useful in two cases: when the token is a Lout escape, */
/* so that it is the user's responsibility to ensure that its content */
/* is legal Lout; and when the command is another filter command, so */
/* that the token content will not go directly into Lout anyway, it */
/* will go through the other filter first. Since the result has to */
/* be verbatim, there is no special treatment of white space characters */
/* and no insertion of line numbers. However, if braces are printed */
/* they really ought to match, so prg2lout checks this and will */
/* complain and insert braces into the verbatim part if necessary. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Meaning of TOKEN fields (ctd.) */
/* */
/* PRINT_NODELIMS_UNQUOTED. This is like PRINT_WHOLE_UNQUOTED except */
/* that the opening and closing delimiters of the token are omitted. */
/* */
/* PRINT_NODELIMS_INNER. Like PRINT_NODELIMS_UNQUOTED except that the */
/* inner part (i.e. not delimiters) is formatted in the same language. */
/* */
/* PRINT_COMMAND_ONLY. This ignores the token and prints just the */
/* command, presumably because the command says it all for that token. */
/* When using PRINT_COMMAND_ONLY you will probably need to enclose the */
/* command with braces: since there are no following braces in this */
/* print style, your command will run into the next one otherwise. */
/* */
/* command */
/* The Lout command to print. This command could be any legal Lout; */
/* programming language setup files offer the following Lout symbols */
/* that make the most common commands: */
/* */
/* @PI for formatting identifiers */
/* @PK for formatting keywords */
/* @PO for formatting operators */
/* @PN for formatting numbers */
/* @PS for formatting strings */
/* @PC for formatting comments */
/* @PA for printing an asterisk (lower on the line than usual) */
/* @PM for printing a minus sign (longer than a hyphen) */
/* @PD for printing a dot (.), only larger than usual */
/* */
/* The last three require PRINT_COMMAND_ONLY (they take no parameter). */
/* If command is NULL or "", then no command will be printed and */
/* furthermore the token will not be enclosed in the usual braces. */
/* */
/* alternate_command */
/* Every language has a list of keywords. Just before printing each */
/* token, it is compared against the keywords. If it is one of them, */
/* then alternate_command is used instead of command. For example, */
/* identifiers usually have command @PI and alternate_command @PK. */
/* */
/* following_command */
/* Print this Lout command (or commands) after the token. If it is a */
/* "broken" multi-line token, print this command after each fragment */
/* */
/* start_line_only */
/* A Boolean field. If TRUE, this token is to be recognized only */
/* if it occurs at the very start of a line. */
/* */
/* starts[] */
/* This field holds an array of strings. If prg2lout discovers any */
/* one of these strings while it is not reading some other token, */
/* then it deems that this token has begun. The recognized string */
/* is the token's "starting delimiter". */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Meaning of TOKEN fields (ctd.) */
/* */
/* starts2[], brackets2[], ends2[] */
/* These fields each hold an array of strings, and the three arrays */
/* must have equal length. If starts2[] has length zero, these fields */
/* do not apply. Otherwise, they modify the meaning of starts[], */
/* bracket_delimiter, and end_delimiter below. Their effect is best */
/* seen by looking at some examples from Perl, their main user: */
/* */
/* q/hello/ qq/hello/ qq?hello? qq{hel{}lo} */
/* */
/* These strings may begin with q, qq, qx, and several other things; */
/* this is then followed by a single character which determines the */
/* string terminator; e.g. / means "terminate with /", { means */
/* "terminate with }", etc. In some cases the start and end delims */
/* come in matching pairs, and then there may be nested matching */
/* pairs. This is implemented as follows: */
/* */
/* starts: { "q", "qq" } */
/* starts2: { "/", "?", "{" } */
/* brackets2: { "", "", "{" } */
/* ends2: { "/", "?", "}" } */
/* */
/* Briefly, every token with non-null starts2 is expanded into a set */
/* of tokens, one for each element i of starts2, whose starting delims */
/* are starts with starts2[i] added, bracketing delim brackets2[i], */
/* and end_delim ends2[i]. PerlQTypeToken is a larger example of this. */
/* */
/* legal */
/* This string defines the set of legal characters inside this token. */
/* For example, numbers might have "0123456789." for this field, since */
/* these are the characters that are legal within numbers, usually. */
/* */
/* escape */
/* This string defines a single character which is the escape */
/* character for this token. That is, if we are reading this token */
/* and come upon this character, the character following it is */
/* treated differently. An empty string "" means no escape character. */
/* */
/* escape_legal */
/* This string defines the set of characters which are legal after */
/* the escape character just mentioned. If any one of these appears */
/* immediately after the escape character, it is deemed to be part */
/* of the token even if without the preceding escape it would not be. */
/* */
/* inner_escape */
/* end_inner_escape */
/* The inner_escape string should be either empty (in which case it */
/* does not apply), or else it should contain a single character, the */
/* "inner escape" character. An inner escape is a temporary suspension */
/* of a token, reverting to the original language. It is used to set */
/* program text within comments. For example, in Eiffel and Blue, */
/* inner_escape is "`" and end_inner_escape is "'" and so we can write */
/* */
/* -- increment `balance' by `amount' */
/* */
/* to treat balance and amount as identifiers within a comment token. */
/* The inner escape is not limited to one token, it may have any */
/* number of tokens, and they may have inner escapes too; prg2lout */
/* imposes no limit on the depth of nesting of inner escapes. */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Meaning of TOKEN fields (ctd.) */
/* */
/* bracket_delimiter */
/* If this string is encountered within a token (not escaped), it */
/* brackets with the next end_delimiter, meaning that the next end */
/* delimiter will not end the token. */
/* */
/* end_delimiter */
/* This string shows how the token ends; for example, a string would */
/* have end_delimiter ". If empty, it means that the token ends */
/* just before the first character encountered that is not legal (see */
/* "legal" above). For example, identifiers and numbers would have */
/* empty end_delimiter. If ends2[] is not empty then end_delimiter */
/* is ignored, since ends2[] explains how the token ends. */
/* */
/* end_start_line_only */
/* A BOOLEAN field. If true, the end delimiter is to be recognized */
/* only if it occurs at the very start of a line. */
/* */
/* want_two_ends */
/* A Boolean feature used only by Perl; TRUE means that end_delimiter */
/* (or ends2[]) has to be encountered twice before the token ends, */
/* rather than the usual once. Used by PerSTypeToken to recognise */
/* */
/* s/abc/ABC/ */
/* */
/* etc. as single tokens. If there is a bracket delimiter (see above), */
/* this will look for a new matching delimiter pair, as in s{}<>. */
/* */
/*****************************************************************************/
#define PRINT_WHOLE_QUOTED 1
#define PRINT_NODELIMS_QUOTED 2
#define PRINT_WHOLE_UNQUOTED 3
#define PRINT_NODELIMS_UNQUOTED 4
#define PRINT_NODELIMS_INNER 5
#define PRINT_COMMAND_ONLY 6
typedef struct token_rec {
unsigned char *name;
int print_style;
unsigned char *command, *alternate_command, *following_command;
BOOLEAN start_line_only;
unsigned char *starts[MAX_STARTS];
unsigned char *starts2[MAX_STARTS2];
unsigned char *brackets2[MAX_STARTS2];
unsigned char *ends2[MAX_STARTS2];
unsigned char *legal;
unsigned char *escape;
unsigned char *escape_legal;
unsigned char *inner_escape;
unsigned char *end_inner_escape;
unsigned char *bracket_delimiter;
unsigned char *end_delimiter;
BOOLEAN end_start_line_only;
BOOLEAN want_two_ends;
/* The following options are initialized by the program, so don't you */
unsigned char chtype[MAX_CHAR]; /* char types within token */
unsigned char escape_chtype[MAX_CHAR]; /* char types after escape */
} TOKEN;
/*****************************************************************************/
/* */
/* Tokens defining strings and literal characters in non-Perl languages. */
/* NB "U" is a cast to (unsigned char *) */
/* */
/*****************************************************************************/
TOKEN CStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "\"" }, /* strings begin with a " character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable is OK */
U "\\", /* within strings, \\ is the escape character */
AllPrintablePlusNL, /* after escape char, any printable char or nl OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "\"", /* strings end with a " character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN CCharacterToken = {
U "character", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting characters */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "'" }, /* characters begin with a ' character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable character is OK */
U "\\", /* within characters, \\ is the escape character */
AllPrintable, /* after escape char, any printable char is OK */
U "", /* characters do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "'", /* characters end with a ' character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN EiffelStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "\"" }, /* strings begin with a " character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable except " is OK */
U "%", /* within strings, % is the escape character */
AllPrintable, /* after escape char, any printable char is OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "\"", /* strings end with a " character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN EiffelCharacterToken = {
U "character", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting characters */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "'" }, /* characters begin with a ' character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable except ' is OK */
U "%", /* within characters, % is the escape character */
AllPrintable, /* after escape char, any printable char is OK */
U "", /* characters do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "'", /* characters end with a ' character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN PythonDblStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "\"" }, /* strings begin with a " character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable is OK */
U "\\", /* within strings, \\ is the escape character */
AllPrintablePlusNL, /* after escape char, any printable char or nl OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "\"", /* strings end with a " character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN PythonSnglStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "'" }, /* strings begin with a ' character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable is OK */
U "\\", /* within strings, \\ is the escape character */
AllPrintablePlusNL, /* after escape char, any printable char or nl OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "'", /* strings end with a ' character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN PythonTriSnglStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "'''" }, /* strings begin with ''' */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintableTabNL, /* inside, any printable is OK */
U "\\", /* within strings, \\ is the escape character */
AllPrintableTabNL, /* after escape char, any printable char or nl OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "'''", /* strings end with ''' */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN PythonTriDblStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "\"\"\"" }, /* strings begin with """ */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintableTabNL, /* inside, any printable is OK */
U "\\", /* within strings, \\ is the escape character */
AllPrintableTabNL, /* after escape char, any printable char or nl OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "\"\"\"", /* strings end with """ */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN HaskellStringToken = {
U "string", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting strings */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "\"" }, /* strings begin with a " character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable except " is OK */
U "\\", /* within strings, \ is the escape character */
AllPrintable, /* after escape char, any printable char is OK */
U "", /* strings do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "\"", /* strings end with a " character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN HaskellCharacterToken = {
U "character", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PS", /* Lout command for formatting characters */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "'" }, /* characters begin with a ' character */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintable, /* inside, any printable except ' is OK */
U "\\", /* within characters, \ is the escape character */
AllPrintable, /* after escape char, any printable char is OK */
U "", /* characters do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "'", /* characters end with a ' character */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
/*****************************************************************************/
/* */
/* Identifiers, in the form common to most programming languages. */
/* */
/*****************************************************************************/
TOKEN IdentifierToken = {
U "identifier", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PI", /* Lout command for formatting identifiers */
U "@PK", /* Alternate command (for keywords) */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ SepLetters, U "_" }, /* identifiers begin with any letter or _ */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
Letter_Digit, /* inside, letters, underscores, digits are OK */
U "", /* no escape character within identifiers */
U "", /* so nothing legal after escape char either */
U "", /* identifiers do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "", /* identifiers do not end with a delimiter */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN HaskellIdentifierToken = {
U "identifier", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PI", /* Lout command for formatting identifiers */
U "@PK", /* Alternate command (for keywords) */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ SepLetters, U "_", U "`" }, /* identifiers begin with any letter or _ */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
Letter_Digit_Quotes, /* inside, letters, underscores, digits are OK */
U "", /* no escape character within identifiers */
U "", /* so nothing legal after escape char either */
U "", /* identifiers do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "", /* identifiers do not end with a delimiter */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
/*****************************************************************************/
/* */
/* Numbers, in the form common to most programming languages. */
/* */
/*****************************************************************************/
TOKEN NumberToken = {
U "number", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PN", /* Lout command for formatting numbers */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ SepDigits }, /* numbers must begin with a digit */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
U "0123456789.eE", /* inside, digits, decimal point, exponent */
U "", /* no escape character within numbers */
U "", /* so nothing legal after escape char either */
U "", /* numbers do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "", /* numbers do not end with a delimiter */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
/*****************************************************************************/
/* */
/* Operators, when user-defined from a set of punctuation characters */
/* */
/*****************************************************************************/
#define OperatorToken(start, legal) /* define operator token */ \
{ \
U "operator", /* name used for debugging only */ \
PRINT_WHOLE_QUOTED, /* print this token as usual */ \
U "@PO", /* Lout command for formatting this */ \
U "", /* no alternate command */ \
U "", /* no following command */ \
FALSE, /* token not just start of line */ \
{ start }, /* token begins with any of these */ \
{ NULL }, /* no start2 needed */ \
{ NULL }, /* so no brackets2 either */ \
{ NULL }, /* so no end2 either */ \
U legal, /* inside, same as start */ \
U "", U "", /* no escape character */ \
U "", U "", /* no inner escape; no end inner esc */ \
U "", /* no bracketing delimiter */ \
U "", /* no ending delimiter */ \
FALSE, /* end not have to be at line start */ \
FALSE, /* don't end delimiter twice to stop */ \
}
TOKEN NonpareilOperatorToken =
OperatorToken(SepNonpareilOperatorPunct, NonpareilOperatorPunct);
TOKEN HaskellOperatorToken =
OperatorToken(HaskellOpChars, HaskellOpCharacters);
/*****************************************************************************/
/* */
/* Tokens defining comments in various languages. */
/* */
/*****************************************************************************/
TOKEN CCommentToken = {
U "comment", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PC", /* Lout command for formatting comments */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "/*" }, /* comments begin with this character pair */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintableTabNLFF, /* inside, any printable char, tab, nl, ff is OK */
U "", /* no escape character within comments */
U "", /* so nothing legal after escape char either */
U "", /* C comments do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "*/", /* comments end with this character pair */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN CPPCommentToken = {
U "comment", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PC", /* Lout command for formatting comments */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "//" }, /* comments begin with this character pair */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintablePlusTab, /* inside, any printable char is OK (not NL) */
U "", /* no escape character within comments */
U "", /* so nothing legal after escape char either */
U "", /* C comments do not permit "inner escapes" */
U "", /* and so there is no end innner escape either */
U "", /* no bracketing delimiter */
U "", /* no end delimiter (end of line will end it) */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN EiffelCommentToken = {
U "comment", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PC", /* Lout command for formatting comments */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "--" }, /* comments begin with this character pair */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintablePlusTab, /* inside, any printable char is OK */
U "", /* no escape character within comments */
U "", /* so nothing legal after escape char either */
U "`", /* start of "inner escape" in Eiffel comment */
U "'", /* end of "inner escape" in Eiffel comment */
U "", /* no bracketing delimiter */
U "", /* no ending delimiter; end of line will end it */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN BlueCommentToken = {
U "comment", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */
U "@PC", /* Lout command for formatting comments */
U "", /* no alternate command */
U "", /* no following command */
FALSE, /* token allowed anywhere, not just start of line */
{ U "==", U "--" }, /* comments begin with this character pair */
{ NULL }, /* no start2 needed */
{ NULL }, /* so no brackets2 either */
{ NULL }, /* so no end2 either */
AllPrintablePlusTab, /* inside, any printable char is OK */
U "", /* no escape character within comments */
U "", /* so nothing legal after escape char either */
U "`", /* start of "inner escape" in Blue comment */
U "'", /* end of "inner escape" in Blue comment */
U "", /* no bracketing delimiter */
U "", /* no ending delimiter; end of line will end it */
FALSE, /* end delimiter does not have to be at line start */
FALSE, /* don't need to see end delimiter twice to stop */
};
TOKEN NonpareilCommentToken = {
U "comment", /* used by error messages involving this token */
PRINT_WHOLE_QUOTED, /* print this token in quotes etc. as usual */