ScummVM API documentation
parser.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef PARALLACTION_PARSER_H
23 #define PARALLACTION_PARSER_H
24 
25 #include "common/stream.h"
26 #include "common/stack.h"
27 #include "parallaction/objects.h"
28 
29 namespace Parallaction {
30 
31 #define MAX_TOKEN_LEN 50
32 extern int _numTokens;
33 extern char _tokens[][MAX_TOKEN_LEN];
34 
35 class Script {
36 
37  Common::ReadStream *_input;
38  bool _disposeSource;
39  uint _line; // for debug messages
40 
41  void clearTokens();
42  char *parseNextToken(char *s, char *tok, uint16 count, const char *brk);
43  char *readLineIntern(char *buf, size_t bufSize);
44 
45 public:
46  Script(Common::ReadStream *, bool _disposeSource = false);
47  ~Script();
48 
49  char *readLine(char *buf, size_t bufSize);
50  uint16 readLineToken(bool errorOnEOF = false);
51 
52  void skip(const char* endToken);
53 
54  uint getLine() { return _line; }
55 };
56 
57 
60 
61 
62 
63 class Parser {
64 
65 public:
66  Parser() { reset(); }
67  ~Parser() { reset(); }
68 
69  uint _lookup;
70 
72  Common::Stack<Table *> _statements;
73 
74  OpcodeSet *_currentOpcodes;
75  Table *_currentStatements;
76 
77  void reset();
78  void pushTables(OpcodeSet *opcodes, Table* statements);
79  void popTables();
80  void parseStatement();
81 
82 };
83 
84 #define DECLARE_UNQUALIFIED_ZONE_PARSER(sig) void locZoneParse_##sig()
85 #define DECLARE_UNQUALIFIED_ANIM_PARSER(sig) void locAnimParse_##sig()
86 #define DECLARE_UNQUALIFIED_COMMAND_PARSER(sig) void cmdParse_##sig()
87 #define DECLARE_UNQUALIFIED_LOCATION_PARSER(sig) void locParse_##sig()
88 #define DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(sig) void instParse_##sig()
89 
90 #define MAX_FORWARDS 50
91 
92 class Parallaction_ns;
93 class Parallaction_br;
94 
95 
97 
98 protected:
99  Parallaction_ns* _vm;
100  Script *_script;
101  Parser *_parser;
102 
103  Table *_zoneTypeNames;
104  Table *_zoneFlagNames;
105  uint _zoneProg;
106 
107  // location parser
108  OpcodeSet _locationParsers;
109  OpcodeSet _locationZoneParsers;
110  OpcodeSet _locationAnimParsers;
111  OpcodeSet _commandParsers;
112  Table *_commandsNames;
113  Table *_locationStmt;
114  Table *_locationZoneStmt;
115  Table *_locationAnimStmt;
116 
117  struct ParserContext {
118  bool end;
119 
120  const char *filename;
121  ZonePtr z;
122  AnimationPtr a;
123  int nextToken;
124  CommandList *list;
125  bool endcommands;
126  CommandPtr cmd;
127  } ctxt;
128 
129  void warning_unexpected();
130 
131  DECLARE_UNQUALIFIED_LOCATION_PARSER(endlocation);
132  DECLARE_UNQUALIFIED_LOCATION_PARSER(location);
133  DECLARE_UNQUALIFIED_LOCATION_PARSER(disk);
134  DECLARE_UNQUALIFIED_LOCATION_PARSER(nodes);
135  DECLARE_UNQUALIFIED_LOCATION_PARSER(zone);
136  DECLARE_UNQUALIFIED_LOCATION_PARSER(animation);
137  DECLARE_UNQUALIFIED_LOCATION_PARSER(localflags);
138  DECLARE_UNQUALIFIED_LOCATION_PARSER(commands);
139  DECLARE_UNQUALIFIED_LOCATION_PARSER(acommands);
140  DECLARE_UNQUALIFIED_LOCATION_PARSER(flags);
141  DECLARE_UNQUALIFIED_LOCATION_PARSER(comment);
142  DECLARE_UNQUALIFIED_LOCATION_PARSER(endcomment);
143  DECLARE_UNQUALIFIED_LOCATION_PARSER(sound);
144  DECLARE_UNQUALIFIED_LOCATION_PARSER(music);
145  DECLARE_UNQUALIFIED_ZONE_PARSER(limits);
146  DECLARE_UNQUALIFIED_ZONE_PARSER(moveto);
147  DECLARE_UNQUALIFIED_ZONE_PARSER(type);
148  DECLARE_UNQUALIFIED_ZONE_PARSER(commands);
149  DECLARE_UNQUALIFIED_ZONE_PARSER(label);
150  DECLARE_UNQUALIFIED_ZONE_PARSER(flags);
151  DECLARE_UNQUALIFIED_ZONE_PARSER(endzone);
152  DECLARE_UNQUALIFIED_ZONE_PARSER(null);
153  DECLARE_UNQUALIFIED_ANIM_PARSER(script);
154  DECLARE_UNQUALIFIED_ANIM_PARSER(commands);
155  DECLARE_UNQUALIFIED_ANIM_PARSER(type);
156  DECLARE_UNQUALIFIED_ANIM_PARSER(label);
157  DECLARE_UNQUALIFIED_ANIM_PARSER(flags);
158  DECLARE_UNQUALIFIED_ANIM_PARSER(file);
159  DECLARE_UNQUALIFIED_ANIM_PARSER(position);
160  DECLARE_UNQUALIFIED_ANIM_PARSER(moveto);
161  DECLARE_UNQUALIFIED_ANIM_PARSER(endanimation);
162  DECLARE_UNQUALIFIED_COMMAND_PARSER(flags);
163  DECLARE_UNQUALIFIED_COMMAND_PARSER(animation);
164  DECLARE_UNQUALIFIED_COMMAND_PARSER(zone);
165  DECLARE_UNQUALIFIED_COMMAND_PARSER(location);
166  DECLARE_UNQUALIFIED_COMMAND_PARSER(invObject);
167  DECLARE_UNQUALIFIED_COMMAND_PARSER(call);
168  DECLARE_UNQUALIFIED_COMMAND_PARSER(simple);
169  DECLARE_UNQUALIFIED_COMMAND_PARSER(move);
170  DECLARE_UNQUALIFIED_COMMAND_PARSER(endcommands);
171 public:
172  virtual void parseGetData(ZonePtr z);
173  virtual void parseExamineData(ZonePtr z);
174  virtual void parseDoorData(ZonePtr z);
175  virtual void parseMergeData(ZonePtr z);
176  virtual void parseHearData(ZonePtr z);
177  virtual void parseSpeakData(ZonePtr z);
178  virtual void parseNoneData(ZonePtr z);
179 protected:
180  Common::String parseComment();
181  Common::String parseDialogueString();
182  Dialogue *parseDialogue();
183  virtual Answer *parseAnswer();
184  void parseAnswerFlags(Answer *answer);
185  void parseAnswerBody(Answer *answer);
186  void parseQuestion(Question *q);
187 
188  uint32 buildZoneType(const char *t0, const char* t1);
189 
190  void parseZone(ZoneList &list, char *name);
191  virtual void parseZoneTypeBlock(ZonePtr z);
192  void parsePointList(PointList &list);
193  void parseAnimation(AnimationList &list, char *name);
194  void parseCommands(CommandList&);
195  void parseCommandFlags();
196  void parseCommandFlag(CommandPtr cmd, const char *flag, Table *table);
197  void createCommand(uint id);
198  void addCommand();
199 
200  void clearSet(OpcodeSet &opcodes) {
201  for (Common::Array<const Opcode *>::iterator i = opcodes.begin(); i != opcodes.end(); ++i)
202  delete *i;
203  opcodes.clear();
204  }
205 
206 public:
207  LocationParser_ns(Parallaction_ns *vm) : _vm(vm), _commandsNames(0), _locationStmt(0),
208  _locationZoneStmt(0), _locationAnimStmt(0) {
209  _script = 0;
210  _parser = 0;
211  _zoneTypeNames = 0;
212  _zoneFlagNames = 0;
213  _zoneProg = 0;
214  }
215 
216  virtual void init();
217 
218  virtual ~LocationParser_ns() {
219  delete _parser;
220  delete _commandsNames;
221  delete _locationStmt;
222  delete _locationZoneStmt;
223  delete _locationAnimStmt;
224  delete _zoneTypeNames;
225  delete _zoneFlagNames;
226 
227  clearSet(_commandParsers);
228  clearSet(_locationAnimParsers);
229  clearSet(_locationZoneParsers);
230  clearSet(_locationParsers);
231  }
232 
233  void parse(Script *script);
234 
235 };
236 
237 
239  BackgroundInfo *_info;
240 
241  Common::String _characterName;
242  Common::String _backgroundName;
243  Common::String _maskName;
244  Common::String _pathName;
245 };
246 
248 
249 protected:
250  Parallaction_br* _vm;
251  Table *_audioCommandsNames;
252 
254 
255  DECLARE_UNQUALIFIED_LOCATION_PARSER(location);
256  DECLARE_UNQUALIFIED_LOCATION_PARSER(zone);
257  DECLARE_UNQUALIFIED_LOCATION_PARSER(animation);
258  DECLARE_UNQUALIFIED_LOCATION_PARSER(localflags);
259  DECLARE_UNQUALIFIED_LOCATION_PARSER(flags);
260  DECLARE_UNQUALIFIED_LOCATION_PARSER(comment);
261  DECLARE_UNQUALIFIED_LOCATION_PARSER(endcomment);
262  DECLARE_UNQUALIFIED_LOCATION_PARSER(sound);
263  DECLARE_UNQUALIFIED_LOCATION_PARSER(music);
264  DECLARE_UNQUALIFIED_LOCATION_PARSER(redundant);
265  DECLARE_UNQUALIFIED_LOCATION_PARSER(ifchar);
266  DECLARE_UNQUALIFIED_LOCATION_PARSER(character);
267  DECLARE_UNQUALIFIED_LOCATION_PARSER(mask);
268  DECLARE_UNQUALIFIED_LOCATION_PARSER(path);
269  DECLARE_UNQUALIFIED_LOCATION_PARSER(escape);
270  DECLARE_UNQUALIFIED_LOCATION_PARSER(zeta);
271  DECLARE_UNQUALIFIED_LOCATION_PARSER(null);
272  DECLARE_UNQUALIFIED_COMMAND_PARSER(ifchar);
273  DECLARE_UNQUALIFIED_COMMAND_PARSER(endif);
274  DECLARE_UNQUALIFIED_COMMAND_PARSER(location);
275  DECLARE_UNQUALIFIED_COMMAND_PARSER(toggle);
276  DECLARE_UNQUALIFIED_COMMAND_PARSER(string);
277  DECLARE_UNQUALIFIED_COMMAND_PARSER(math);
278  DECLARE_UNQUALIFIED_COMMAND_PARSER(test);
279  DECLARE_UNQUALIFIED_COMMAND_PARSER(music);
280  DECLARE_UNQUALIFIED_COMMAND_PARSER(zeta);
281  DECLARE_UNQUALIFIED_COMMAND_PARSER(swap);
282  DECLARE_UNQUALIFIED_COMMAND_PARSER(give);
283  DECLARE_UNQUALIFIED_COMMAND_PARSER(text);
284  DECLARE_UNQUALIFIED_COMMAND_PARSER(unary);
285  DECLARE_UNQUALIFIED_ZONE_PARSER(limits);
286  DECLARE_UNQUALIFIED_ZONE_PARSER(moveto);
287  DECLARE_UNQUALIFIED_ZONE_PARSER(type);
288  DECLARE_UNQUALIFIED_ANIM_PARSER(file);
289  DECLARE_UNQUALIFIED_ANIM_PARSER(position);
290  DECLARE_UNQUALIFIED_ANIM_PARSER(moveto);
291  DECLARE_UNQUALIFIED_ANIM_PARSER(endanimation);
292 
293  void parseZoneTypeBlock(ZonePtr z) override;
294 public:
295  virtual void parsePathData(ZonePtr z);
296  void parseGetData(ZonePtr z) override;
297  void parseDoorData(ZonePtr z) override;
298  void parseHearData(ZonePtr z) override;
299  void parseNoneData(ZonePtr z) override;
300 protected:
301  void parseAnswerCounter(Answer *answer);
302  Answer *parseAnswer() override;
303 
304 public:
306  _audioCommandsNames(0), _out(0) {
307  }
308 
309  void init() override;
310 
311  ~LocationParser_br() override {
312  delete _audioCommandsNames;
313  }
314 
315  void parse(Script *script, LocationParserOutput_br *out);
316 
317 };
318 
319 
320 
322 
323 protected:
324  Parallaction_ns *_vm;
325  Parser *_parser;
326 
327  Script *_script;
328  ProgramPtr _program;
329 
330  // program parser
331  OpcodeSet _instructionParsers;
332  Table *_instructionNames;
333 
334  uint32 _currentInstruction; // index of the instruction being parsed
335 
336  struct ParserContext {
337  bool end;
338  AnimationPtr a;
339  InstructionPtr inst;
340  LocalVariable *locals;
341  } ctxt;
342 
343  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(defLocal);
344  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(animation);
345  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(loop);
346  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(x);
347  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(y);
348  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(z);
349  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(f);
350  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(inc);
351  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(set);
352  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(move);
353  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(put);
354  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(call);
355  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(sound);
356  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(null);
357  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(endscript);
358 
359  void parseInstruction();
360  void parseLValue(ScriptVar &var, const char *str);
361  virtual void parseRValue(ScriptVar &var, const char *str);
362 
363  void clearSet(OpcodeSet &opcodes) {
364  for (Common::Array<const Opcode *>::iterator i = opcodes.begin(); i != opcodes.end(); ++i)
365  delete *i;
366  opcodes.clear();
367  }
368 
369 public:
370  ProgramParser_ns(Parallaction_ns *vm) : _vm(vm), _parser(0), _instructionNames(0), _script(0), _currentInstruction(0) {
371  }
372 
373  virtual void init();
374 
375  virtual ~ProgramParser_ns() {
376  delete _parser;
377  delete _instructionNames;
378 
379  clearSet(_instructionParsers);
380  }
381 
382  virtual void parse(Script *script, ProgramPtr program);
383 
384 };
385 
386 
388 
389 protected:
390  Parallaction_br *_vm;
391 
392  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(zone);
393  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(color);
394  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(mask);
395  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(print);
396  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(text);
397  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(if_op);
398  DECLARE_UNQUALIFIED_INSTRUCTION_PARSER(endif);
399 
400  int32 _openIfStatement;
401  void beginIfStatement();
402  void endIfStatement();
403 
404  void parseRValue(ScriptVar &var, const char *str) override;
405 
406 public:
407  ProgramParser_br(Parallaction_br *vm) : ProgramParser_ns((Parallaction_ns*)vm), _vm(vm), _openIfStatement(0) {
408  }
409 
410  void init() override;
411  void parse(Script *script, ProgramPtr program) override;
412 };
413 
414 } // End of namespace Parallaction
415 
416 #endif
Definition: str.h:59
Definition: objects.h:388
Definition: objects.h:145
void clear()
Definition: array.h:320
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
Definition: objects.h:318
T * iterator
Definition: array.h:54
Definition: parallaction.h:377
Definition: parser.h:35
Definition: debug.h:8
Definition: graphics.h:352
Definition: parser.h:96
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Definition: objects.h:165
Definition: parser.h:63
Definition: objects.h:178
Definition: stream.h:385
Definition: parser.h:387
Definition: parser.h:247
Definition: stack.h:102
Definition: parser.h:321
Definition: parallaction.h:489
Definition: objects.h:509