xref: /plan9/sys/src/ape/cmd/pdksh/lex.h (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 /*
2  * Source input, lexer and parser
3  */
4 
5 /* $Id: lex.h,v 1.4 1994/05/31 13:34:34 michael Exp $ */
6 
7 #define	IDENT	64
8 
9 typedef struct source Source;
10 struct source {
11 	const char *str;	/* input pointer */
12 	int	type;		/* input type */
13 	const char *start;	/* start of current buffer */
14 	union {
15 		char **strv;	/* string [] */
16 		struct shf *shf; /* shell file */
17 		struct tbl *tblp; /* alias (SALIAS) */
18 		char *freeme;	/* also for SREREAD */
19 	} u;
20 	char	ugbuf[2];	/* buffer for ungetsc() (SREREAD) and
21 				 * alias (SALIAS) */
22 	int	line;		/* line number */
23 	int	errline;	/* line the error occured on (0 if not set) */
24 	const char *file;	/* input file name */
25 	int	flags;		/* SF_* */
26 	Area	*areap;
27 	XString	xs;		/* input buffer */
28 	Source *next;		/* stacked source */
29 };
30 
31 /* Source.type values */
32 #define	SEOF		0	/* input EOF */
33 #define	SFILE		1	/* file input */
34 #define SSTDIN		2	/* read stdin */
35 #define	SSTRING		3	/* string */
36 #define	SWSTR		4	/* string without \n */
37 #define	SWORDS		5	/* string[] */
38 #define	SWORDSEP	6	/* string[] seperator */
39 #define	SALIAS		7	/* alias expansion */
40 #define SREREAD		8	/* read ahead to be re-scanned */
41 
42 /* Source.flags values */
43 #define SF_ECHO		BIT(0)	/* echo input to shlout */
44 #define SF_ALIAS	BIT(1)	/* faking space at end of alias */
45 #define SF_ALIASEND	BIT(2)	/* faking space at end of alias */
46 #define SF_TTY		BIT(3)	/* type == SSTDIN & it is a tty */
47 
48 /*
49  * states while lexing word
50  */
51 #define	SBASE	0		/* outside any lexical constructs */
52 #define	SWORD	1		/* implicit quoting for substitute() */
53 #ifdef KSH
54 #define	SLETPAREN 2		/* inside (( )), implicit quoting */
55 #endif /* KSH */
56 #define	SSQUOTE	3		/* inside '' */
57 #define	SDQUOTE	4		/* inside "" */
58 #define	SBRACE	5		/* inside ${} */
59 #define	SCSPAREN 6		/* inside $() */
60 #define	SBQUOTE	7		/* inside `` */
61 #define	SASPAREN 8		/* inside $(( )) */
62 #define SHEREDELIM 9		/* parsing <<,<<- delimiter */
63 #define SHEREDQUOTE 10		/* parsing " in <<,<<- delimiter */
64 #define SPATTERN 11		/* parsing *(...|...) pattern (*+?@!) */
65 #define STBRACE 12		/* parsing ${..[#%]..} */
66 
67 typedef union {
68 	int	i;
69 	char   *cp;
70 	char  **wp;
71 	struct op *o;
72 	struct ioword *iop;
73 } YYSTYPE;
74 
75 /* If something is added here, add it to tokentab[] in syn.c as well */
76 #define	LWORD	256
77 #define	LOGAND	257		/* && */
78 #define	LOGOR	258		/* || */
79 #define	BREAK	259		/* ;; */
80 #define	IF	260
81 #define	THEN	261
82 #define	ELSE	262
83 #define	ELIF	263
84 #define	FI	264
85 #define	CASE	265
86 #define	ESAC	266
87 #define	FOR	267
88 #define SELECT	268
89 #define	WHILE	269
90 #define	UNTIL	270
91 #define	DO	271
92 #define	DONE	272
93 #define	IN	273
94 #define	FUNCTION 274
95 #define	TIME	275
96 #define	REDIR	276
97 #ifdef KSH
98 #define MDPAREN	277		/* (( )) */
99 #endif /* KSH */
100 #define BANG	278		/* ! */
101 #define DBRACKET 279		/* [[ .. ]] */
102 #define COPROC	280		/* |& */
103 #define	YYERRCODE 300
104 
105 /* flags to yylex */
106 #define	CONTIN	BIT(0)		/* skip new lines to complete command */
107 #define	ONEWORD	BIT(1)		/* single word for substitute() */
108 #define	ALIAS	BIT(2)		/* recognize alias */
109 #define	KEYWORD	BIT(3)		/* recognize keywords */
110 #define LETEXPR	BIT(4)		/* get expression inside (( )) */
111 #define VARASN	BIT(5)		/* check for var=word */
112 #define ARRAYVAR BIT(6)		/* parse x[1 & 2] as one word */
113 #define ESACONLY BIT(7)		/* only accept esac keyword */
114 #define CMDWORD BIT(8)		/* parsing simple command (alias related) */
115 #define HEREDELIM BIT(9)	/* parsing <<,<<- delimiter */
116 
117 #define	HERES	10		/* max << in line */
118 
119 EXTERN	Source *source;		/* yyparse/yylex source */
120 EXTERN	YYSTYPE	yylval;		/* result from yylex */
121 EXTERN	struct ioword *heres [HERES], **herep;
122 EXTERN	char	ident [IDENT+1];
123 
124 #ifdef HISTORY
125 # define HISTORYSIZE	128	/* size of saved history */
126 
127 EXTERN	char  **history;	/* saved commands */
128 EXTERN	char  **histptr;	/* last history item */
129 EXTERN	int	histsize;	/* history size */
130 #endif /* HISTORY */
131