1 /* zztoop - zzt oop parser */
2 /* $Id: zztoop.h,v 1.1 2003/11/01 23:45:57 bitman Exp $ */
3 /* Copyright (C) 2002 Ryan Phillips <bitman@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef LIBZZT2_ZZTOOP_H
21 #define LIBZZT2_ZZTOOP_H 1
22 
23 #include "zzt.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* A single component of an OOP string */
30 typedef struct ZZTOOPcomponent {
31 	struct ZZTOOPcomponent* next;
32 
33 	int type;    /* Type of component */
34 
35 	int value;   /* Value of the component (meaning varies with type) */
36 	char * text; /* Text value of the component */
37 
38 	int pos;     /* Position of the component on the line */
39 } ZZTOOPcomponent;
40 
41 typedef struct ZZTOOPparser {
42 	char * line;   /* A line of ZZT-OOP (hopefully) */
43 	int flags;     /* Control how parsing is done */
44 
45 	/* We get results: */
46 	ZZTOOPcomponent * first, * last;
47 
48 	/* private: */
49 	/* Internal tokenizing information */
50 	char * token;     /* Current token */
51 	int tokenPos;     /* Pos of token in line */
52 	int nextTokenPos; /* Pos of next token in line */
53 	int tokenType;
54 
55 } ZZTOOPparser;
56 
57 /* Parser flags */
58 #define ZOOPFLAG_STRICTZZT  0x1  /* Enforce strict ZZT rules */
59 #define ZOOPFLAG_HELP       0x2  /* Set when parsing help code */
60 #define ZOOPFLAG_FIRSTLINE  0x4  /* Set when parsing the first line in a script */
61 
62 /* Types of tokens */
63 #define ZOOPTOK_SYMBOL      1
64 #define ZOOPTOK_SEPARATOR   2  /* semicolon */
65 #define ZOOPTOK_WHITESPACE  3
66 #define ZOOPTOK_TEXT        4
67 #define ZOOPTOK_NONE        5  /* empty token */
68 
69 
70 /* Component types */
71 #define ZOOPTYPE_NONE       0  /* Whitespace or unknown */
72 #define ZOOPTYPE_TEXT       1
73 #define ZOOPTYPE_SYMBOL     2  /* Symbols like $, #, :, etc. */
74 #define ZOOPTYPE_COMMENT    3
75 #define ZOOPTYPE_COMMAND    4
76 #define ZOOPTYPE_KEYWORD    5  /* Misc keywords (i.e. "then") */
77 #define ZOOPTYPE_MUSIC      6  /* ZZM music string */
78 #define ZOOPTYPE_OBJNAME    7
79 #define ZOOPTYPE_NUMBER     8
80 
81 /* These types have both standard and non-standard values */
82 #define ZOOPTYPE_LABEL      9  /* Labels are sent to */
83 #define ZOOPTYPE_MESSAGE   10  /* Msgs are sent from */
84 #define ZOOPTYPE_FLAG      11
85 #define ZOOPTYPE_FLAGMOD   12  /* Most notably: not */
86 
87 /* These types have a specific set of valid values */
88 #define ZOOPTYPE_ITEM      13
89 #define ZOOPTYPE_KIND      14
90 #define ZOOPTYPE_COLOR     15
91 #define ZOOPTYPE_DIR       16
92 #define ZOOPTYPE_DIRMOD    17  /* Direction modifier */
93 
94 #define ZOOPTYPE_MAX       17
95 
96 
97 /* Number of built-in values for several types */
98 #define ZOOPCOMMANDCOUNT    27
99 #define ZOOPMESSAGECOUNT     5
100 #define ZOOPFLAGCOUNT        5
101 #define ZOOPITEMCOUNT        5
102 #define ZOOPCOLOURCOUNT      7
103 #define ZOOPDIRCOUNT        15
104 #define ZOOPDIRMODCOUNT      4
105 
106 /* Built-in ZZT-OOP commands */
107 #define ZOOPCMND_BECOME     0
108 #define ZOOPCMND_BIND       1
109 #define ZOOPCMND_CHANGE     2
110 #define ZOOPCMND_CHAR       3
111 #define ZOOPCMND_CLEAR      4
112 #define ZOOPCMND_CYCLE      5
113 #define ZOOPCMND_DIE        6
114 #define ZOOPCMND_END        7
115 #define ZOOPCMND_ENDGAME    8
116 #define ZOOPCMND_GIVE       9
117 #define ZOOPCMND_GO        10
118 #define ZOOPCMND_IDLE      11
119 #define ZOOPCMND_IF        12
120 #define ZOOPCMND_LOCK      13
121 #define ZOOPCMND_PLAY      14
122 #define ZOOPCMND_PUT       15
123 #define ZOOPCMND_RESTART   16
124 #define ZOOPCMND_RESTORE   17
125 #define ZOOPCMND_SEND      18
126 #define ZOOPCMND_SET       19
127 #define ZOOPCMND_SHOOT     20
128 #define ZOOPCMND_TAKE      21
129 #define ZOOPCMND_THROWSTAR 22
130 #define ZOOPCMND_TRY       23
131 #define ZOOPCMND_UNLOCK    24
132 #define ZOOPCMND_WALK      25
133 #define ZOOPCMND_ZAP       26
134 
135 /* Values for the TEXT type */
136 #define ZOOPTEXT_NORMAL    0
137 #define ZOOPTEXT_HEADING   1
138 #define ZOOPTEXT_LABEL     2
139 #define ZOOPTEXT_HYPERTEXT 3
140 #define ZOOPTEXT_MAX       3
141 
142 /* Values for the FLAG type */
143 #define ZOOPFLAG_ALLIGNED  0
144 #define ZOOPFLAG_CONTACT   1
145 #define ZOOPFLAG_BLOCKED   2
146 #define ZOOPFLAG_ENERGIZED 3
147 #define ZOOPFLAG_ANY       4
148 
149 /* Command argument syntax:
150  * Each type of argument to a command is given a letter and stored
151  * in a string in zztcommandargs[], which corresponds to that same
152  * numbered element in zztcommands */
153 
154 #define ZOOPARG_KIND        'k'
155 #define ZOOPARG_OBJECTNAME  'o'
156 #define ZOOPARG_NUMBER      'n'
157 #define ZOOPARG_FLAG        'f'
158 #define ZOOPARG_ITEM        'i'
159 #define ZOOPARG_DIRECTION   'd'
160 #define ZOOPARG_THENMESSAGE 't'
161 #define ZOOPARG_MUSIC       's'
162 #define ZOOPARG_MESSAGE     'm'
163 
164 /* Create a new parser
165  * Do not free or modify "line" until you are done with the parser. */
166 ZZTOOPparser * zztoopCreateParser(char * line);
167 
168 /* Delete a parser */
169 void zztoopDeleteParser(ZZTOOPparser * parser);
170 
171 /* Parse the line */
172 ZZTOOPcomponent * zztoopParseLine(ZZTOOPparser * parser);
173 
174 /* Remove the component chain from a parser. Call this function
175  * before deleting the parser if you want to keep using the
176  * generated components. */
177 ZZTOOPcomponent * zztoopRemoveComponents(ZZTOOPparser * parser);
178 
179 /* private: */
180 
181 /* Add a component to the parser */
182 void zztoopAddComponent(ZZTOOPparser * parser, ZZTOOPcomponent * component);
183 
184 /* Add the current token to the parser and move on */
185 void zztoopAddToken(ZZTOOPparser * parser, int type, int value);
186 
187 /* Add remainder of the line (including token) as a component with given type and value */
188 void zztoopAddRemainder(ZZTOOPparser * parser, int type, int value);
189 
190 /* Add the next token if it is whitespace and advance */
191 void zztoopAddWhitespace(ZZTOOPparser * parser);
192 
193 
194 /* Create a new component
195  * Note: text will be free()ed when component is deleted. */
196 ZZTOOPcomponent * zztoopCreateComponent(int type, int value, char * text, int pos);
197 
198 /* Free a chain of components */
199 void zztoopDeleteComponentChain(ZZTOOPcomponent * components);
200 
201 
202 /* Parsing subroutines */
203 void zztoopParseRoot(ZZTOOPparser * parser);  /* First level */
204 void zztoopParseSymbol(ZZTOOPparser * parser);
205 void zztoopParseCommand(ZZTOOPparser * parser);
206 void zztoopParseLabel(ZZTOOPparser * parser);
207 void zztoopParseDirection(ZZTOOPparser * parser);
208 void zztoopParseMessage(ZZTOOPparser * parser);
209 void zztoopParseHypermessage(ZZTOOPparser * parser);
210 void zztoopParseCommandArgs(ZZTOOPparser * parser, int command);
211 
212 /* Grab the next token
213  * Returns the length of the token */
214 int zztoopNextToken(ZZTOOPparser * parser);
215 
216 /* Give a textual description of a token type */
217 #define zztoopTypeDescription(type) (type <= ZOOPTYPE_MAX ? zztooptypes[(type)] : zztooptypes[0])
218 
219 /* Token identifying macros (return -1 on failure) */
220 #define zztoopFindCommand(token) lookupString(zztoopcommands, ZOOPCOMMANDCOUNT, (token), STREQU_UNCASE)
221 #define zztoopFindMessage(token) lookupString(zztoopmessages, ZOOPMESSAGECOUNT, (token), STREQU_UNCASE)
222 #define zztoopFindFlag(token)    lookupString(zztoopflags,    ZOOPFLAGCOUNT,    (token), STREQU_UNCASE)
223 #define zztoopFindItem(token)    lookupString(zztoopitems,    ZOOPITEMCOUNT,    (token), STREQU_UNCASE)
224 #define zztoopFindDir(token)     lookupString(zztoopdirs,     ZOOPDIRCOUNT,     (token), STREQU_UNCASE)
225 #define zztoopFindDirMod(token)  lookupString(zztoopdirmods,  ZOOPDIRMODCOUNT,  (token), STREQU_UNCASE)
226 #define zztoopFindColour(token)  lookupString(zztoopcolours,  ZOOPCOLOURCOUNT,  (token), STREQU_UNCASE)
227 #define zztoopFindKind(token)    lookupString(_zzt_type_kind_table, ZZT_MAX_TYPE, (token), STREQU_UNCASE)
228 
229 /* Lookup tables */
230 extern const char * zztooptypes[ZOOPTYPE_MAX + 1];
231 
232 extern const char * zztoopcommands[ZOOPCOMMANDCOUNT];
233 extern const char * zztoopcommandargs[ZOOPCOMMANDCOUNT];
234 extern const char * zztoopmessages[ZOOPMESSAGECOUNT];
235 extern const char * zztoopflags[ZOOPFLAGCOUNT];
236 extern const char * zztoopitems[ZOOPITEMCOUNT];
237 extern const char * zztoopdirs[ZOOPDIRCOUNT];
238 extern const char * zztoopdirmods[ZOOPDIRMODCOUNT];
239 extern const char * zztoopcolours[ZOOPCOLOURCOUNT];
240 
241 #ifdef __cplusplus
242 }
243 #endif
244 
245 #endif /* LIBZZT2_ZZTOOP_H */
246