xref: /original-bsd/usr.bin/pascal/src/yyprint.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)yyprint.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "0.h"
14 #include "tree_ty.h"	/* must be included for yy.h */
15 #include "yy.h"
16 
17 char	*tokname();
18 
19 STATIC	short bounce;
20 
21 /*
22  * Printing representation of a
23  * "character" - a lexical token
24  * not in a yytok structure.
25  * 'which' indicates which char * you want
26  * should always be called as "charname(...,0),charname(...,1)"
27  */
28 char *
29 charname(ch , which )
30 	int ch;
31 	int which;
32 {
33 	struct yytok Ych;
34 
35 	Ych.Yychar = ch;
36 	Ych.Yylval = nullsem(ch);
37 	return (tokname(&Ych , which ));
38 }
39 
40 /*
41  * Printing representation of a token
42  * 'which' as above.
43  */
44 char *
45 tokname(tp , which )
46 	register struct yytok *tp;
47 	int	 	      which;
48 {
49 	register char *cp;
50 	register struct kwtab *kp;
51 	char	*cp2;
52 
53 	cp2 = "";
54 	switch (tp->Yychar) {
55 		case YCASELAB:
56 			cp = "case-label";
57 			break;
58 		case YEOF:
59 			cp = "end-of-file";
60 			break;
61 		case YILLCH:
62 			cp = "illegal character";
63 			break;
64 		case 256:
65 			/* error token */
66 			cp = "error";
67 			break;
68 		case YID:
69 			cp = "identifier";
70 			break;
71 		case YNUMB:
72 			cp = "real number";
73 			break;
74 		case YINT:
75 		case YBINT:
76 			cp = "number";
77 			break;
78 		case YSTRING:
79 			cp = (char *) tp->Yylval;
80 			cp = cp == NIL || cp[1] == 0 ? "character" : "string";
81 			break;
82 		case YDOTDOT:
83 			cp = "'..'";
84 			break;
85 		default:
86 			if (tp->Yychar < 256) {
87 				cp = "'x'\0'x'\0'x'\0'x'";
88 				/*
89 				 * for four times reentrant code!
90 				 * used to be:
91 				 * if (bounce = ((bounce + 1) & 1))
92 				 *	cp += 4;
93 				 */
94 				bounce = ( bounce + 1 ) % 4;
95 				cp += (4 * bounce);	/* 'x'\0 is 4 chars */
96 				cp[1] = tp->Yychar;
97 				break;
98 			}
99 			for (kp = yykey; kp->kw_str != NIL && kp->kw_val != tp->Yychar; kp++)
100 				continue;
101 			cp = "keyword ";
102 			cp2 = kp->kw_str;
103 	}
104 	return ( which ? cp2 : cp );
105 }
106