Debian Codegraph @

terps/tads/tads3/vmgram.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmgram.h - T3 grammar-production metaclass
12 Function
13
14 Notes
15
16 Modified
17 02/15/00 MJRoberts - Creation
18 */
19
20 #ifndef VMGRAM_H
21 #define VMGRAM_H
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "os.h"
27 #include "t3std.h"
28 #include "vmtype.h"
29 #include "vmglob.h"
30 #include "vmobj.h"
31
32 /* ------------------------------------------------------------------------ */
33 /*
34 * intrinsic function vector indices
35 */
36 enum vmobjgram_meta_fnset
37 {
38 /* undefined function */
39 VMOBJGRAM_UNDEF = 0,
40
41 /* parseTokens(tokenList, dict) */
42 VMOBJGRAM_PARSE = 1
43 };
44
45 /* ------------------------------------------------------------------------ */
46 /*
47 * Match types
48 */
49 enum vmgram_match_type
50 {
51 /* uninitialized */
52 VMGRAM_MATCH_UNDEF = 0,
53
54 /* production - matches a sub-production */
55 VMGRAM_MATCH_PROD = 1,
56
57 /*
58 * part of speech - matches a word that appears in the dictionary
59 * under a particular part of speech
60 */
61 VMGRAM_MATCH_SPEECH = 2,
62
63 /* literal - matches a literal string */
64 VMGRAM_MATCH_LITERAL = 3,
65
66 /* token type - matches any token of a given type */
67 VMGRAM_MATCH_TOKTYPE = 4,
68
69 /* star - matches all remaining input tokens */
70 VMGRAM_MATCH_STAR = 5,
71
72 /*
73 * N parts of speech - matches a word that appears in the dictionary
74 * under any of a set of N parts of speech
75 */
76 VMGRAM_MATCH_NSPEECH = 6
77 };
78
79 /* ------------------------------------------------------------------------ */
80 /*
81 * Grammar production object - image file format
82 *
83 * UINT2 alt_count
84 *. alternative 1
85 *. alternative 2
86 *. etc
87 *
88 * Each alternative has the following structure:
89 *
90 *. INT2 score
91 *. INT2 badness
92 *. UINT4 processor_object_id
93 *. UINT2 token_count
94 *. token 1
95 *. token 2
96 *. etc
97 *
98 * Each token has this structure:
99 *
100 * UINT2 property_association
101 *. BYTE token_match_type (see below)
102 *. extra data depending on token_match_type (see below)
103 *
104 * The extra data for the token varies by match type:
105 *
106 * VMGRAM_MATCH_PROD - a UINT4 giving the production object ID
107 *
108 * VMGRAM_MATCH_SPEECH - a UINT2 giving the vocabulary property
109 *
110 * VMGRAM_MATCH_NSPEECH - a UINT2 giving a count, then that many
111 * additional UINT2's giving a list of vocabulary properties
112 *
113 * VMGRAM_MATCH_LITERAL - a UINT2 byte-length prefix followed by the
114 * UTF8-encoded bytes of the literal string
115 *
116 * VMGRAM_MATCH_TOKTYPE - a UINT4 giving the token enum's ID
117 *
118 * VMGRAM_MATCH_STAR - no additional data
119 */
120
121 /* property/match result enumeration entry */
122 struct vmgram_match_info
123 {
124 vm_prop_id_t prop;
125 };
126
127 /*
128 * Grammar production object extension
129 */
130 struct vm_gram_ext
131 {
132 /* pointer to load image data, if any */
133 const char *image_data_;
134 size_t image_data_size_;
135
136 /*
137 * The last comparator object we used to calculate hash values for
138 * literals. Each time we need literal hash values, we'll check to see
139 * if we're using the same comparator we were last time; if so, we'll
140 * use the cached hash values, otherwise we'll recalculate them. We
141 * reference this object weakly.
142 */
143 vm_obj_id_t comparator_;
144
145 /* flag: our rule list has been modified since load time */
146 uint modified_ : 1;
147
148 /* flag: we've cached hash values for our literals */
149 uint hashes_cached_ : 1;
150
151 /*
152 * flag: there's at least one circular rule among my rules (i.e.,
153 * there's a rule whose first element is a self-reference
154 * subproduction)
155 */
156 uint has_circular_alt : 1;
157
158 /* private memory pool - we use this to make allocation cheaper */
159 class CVmGramProdMem *mem_;
160
161 /*
162 * Property list enumeration space. We use this to build a list of
163 * properties for which a dictionary word is defined. We'll expand
164 * this list as needed when we find we need more space.
165 */
166 vmgram_match_info *prop_enum_arr_;
167 size_t prop_enum_max_;
168
169 /* array of rule alternatives */
170 struct vmgram_alt_info **alts_;
171
172 /* number of alternatives currently in use */
173 size_t alt_cnt_;
174
175 /* number of alternatives allocated */
176 size_t alt_alo_;
177 };
178
179 /*
180 * Alternative object. Each of these objects represents one of our rule
181 * alternatives.
182 */
183 struct vmgram_alt_info
184 {
185 vmgram_alt_info(size_t ntoks);
186 ~vmgram_alt_info();
187
188 /* the alternative's score and badness values */
189 int score;
190 int badness;
191
192 /* flag: this alternative is marked for deletion */
193 int del;
194
195 /*
196 * the "processor object" for this alternative - this is the class we
197 * instantiate to represent a match to the alternative
198 */
199 vm_obj_id_t proc_obj;
200
201 /* array of token elements in the alternative */
202 struct vmgram_tok_info *toks;
203 size_t tok_cnt;
204 };
205
206 /*
207 * Grammar rule token entry. This represents a token in a grammar rule.
208 */
209 struct vmgram_tok_info
210 {
211 vmgram_tok_info() { typ = VMGRAM_MATCH_UNDEF; }
212 ~vmgram_tok_info()
213 {
214 switch (typ)
215 {
216 case VMGRAM_MATCH_LITERAL:
217 t3free(typinfo.lit.str);
218 break;
219
220 case VMGRAM_MATCH_NSPEECH:
221 t3free(typinfo.nspeech.props);
222 break;
223 }
224 }
225
226 void set_literal(const char *str, size_t len)
227 {
228 set_literal(len);
229 memcpy(typinfo.lit.str, str, len);
230 }
231
232 void set_literal(size_t len)
233 {
234 typ = VMGRAM_MATCH_LITERAL;
235 typinfo.lit.len = len;
236 typinfo.lit.hash = 0;
237 typinfo.lit.str = (char *)t3malloc(len);
238 }
239
240 void set_nspeech(size_t cnt)
241 {
242 typ = VMGRAM_MATCH_NSPEECH;
243 typinfo.nspeech.cnt = cnt;
244 typinfo.nspeech.props = (vm_prop_id_t *)t3malloc(
245 cnt * sizeof(vm_prop_id_t));
246 }
247
248 /*
249 * property association - this is the property of the processor object
250 * that we'll set to point to the match object or input token if we
251 * match this rule token
252 */
253 vm_prop_id_t prop;
254
255 /* token type - this is a VMGRAM_MATCH_xxx value */
256 uchar typ;
257
258 /* extra data, depending on 'typ' */
259 union
260 {
261 /* VMGRAM_MATCH_PROD - the sub-production object */
262 vm_obj_id_t prod_obj;
263
264 /* VMGRAM_MATCH_SPEECH - the part-of-speech property */
265 vm_prop_id_t speech_prop;
266
267 /* VMGRAM_MATCH_NSPEECH - an array of part-of-speech proeprties */
268 struct
269 {
270 size_t cnt;
271 vm_prop_id_t *props;
272 } nspeech;
273
274 /* VMGRAM_MATCH_LITERAL - the literal string to match */
275 struct
276 {
277 /* the literal text and its length */
278 char *str;
279 size_t len;
280
281 /* the cached hash value for the literal */
282 uint hash;
283 } lit;
284
285 /* VMGRAM_MATCH_TOKTYPE - token type enum */
286 uint32_t toktyp_enum;
287
288 } typinfo;
289 };
290
291 /* ------------------------------------------------------------------------ */
292 /*
293 * Grammar-Production object interface
294 */
295 class CVmObjGramProd: public CVmObject
296 {
297 friend class CVmMetaclassGramProd;
298 friend struct CVmGramCompResults;
299
300 public:
301 /* metaclass registration object */
302 static class CVmMetaclass *metaclass_reg_;
303 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
304
305 /* am I of the given metaclass? */
306 virtual int is_of_metaclass(class CVmMetaclass *meta) const
307 {
308 /* try my own metaclass and my base class */
309 return (meta == metaclass_reg_
310 || CVmObject::is_of_metaclass(meta));
311 }
312
313 /* create dynamically using stack arguments */
314 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
315 uint argc);
316
317 /*
318 * call a static property - we don't have any of our own, so simply
319 * "inherit" the base class handling
320 */
321 static int call_stat_prop(VMG_ vm_val_t *result,
322 const uchar **pc_ptr, uint *argc,
323 vm_prop_id_t prop)
324 { return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); }
325
326 /* determine if an object is a GrammarProduction object */
327 static int is_gramprod_obj(VMG_ vm_obj_id_t obj)
328 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
329
330 /* notify of deletion */
331 void notify_delete(VMG_ int in_root_set);
332
333 /* get a property */
334 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
335 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
336
337 /* set a property */
338 void set_prop(VMG_ class CVmUndo *undo,
339 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
340
341 /* receive notification of a new undo savepoint */
342 void notify_new_savept() { }
343
344 /* apply undo */
345 void apply_undo(VMG_ struct CVmUndoRecord *rec);
346
347 /* discard additional information associated with an undo record */
348 void discard_undo(VMG_ struct CVmUndoRecord *rec);
349
350 /* mark a reference in an undo record */
351 void mark_undo_ref(VMG_ struct CVmUndoRecord *rec);
352
353 /* remove stale weak references from an undo record */
354 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
355
356 /* mark object references */
357 void mark_refs(VMG_ uint state);
358
359 /* remove weak references */
360 void remove_stale_weak_refs(VMG0_);
361
362 /* load from an image file */
363 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
364
365 /* restart/save/restore */
366 void reset_to_image(VMG_ vm_obj_id_t self);
367 void save_to_file(VMG_ class CVmFile *fp);
368 void restore_from_file(VMG_ vm_obj_id_t self,
369 class CVmFile *fp, class CVmObjFixup *fixups);
370
371 /* determine if the object has been changed since it was loaded */
372 int is_changed_since_load() const { return FALSE; }
373
374 /*
375 * rebuild for image file - we can't change during execution, so our
376 * image file data never change
377 */
378 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
379
380 /* convert to constant data */
381 virtual void convert_to_const_data(VMG_ class CVmConstMapper *,
382 vm_obj_id_t) { }
383
384 protected:
385 /* private constructor */
386 CVmObjGramProd(VMG0_);
387
388 /* clear the alternative list */
389 void clear_alts();
390
391 /* ensure that there's space for the given number of alternatives */
392 void ensure_alts(size_t cnt, size_t margin);
393
394 /* insert/delete an alternative, without keeping undo information */
395 void insert_alt(vm_obj_id_t self, size_t idx, vmgram_alt_info *alt);
396 void delete_alt(size_t idx);
397
398 /* check the alternative list for circular references */
399 void check_circular_refs(vm_obj_id_t self);
400
401 /* build the grammarAltProps list for a match object */
402 void build_alt_props(VMG_ vm_obj_id_t match_obj);
403 size_t build_alt_props_list(VMG_ vm_obj_id_t match_obj,
404 class CVmObjList *lst);
405
406 /* add an alternative from a parsed rule, keeping undo */
407 void add_alt(VMG_ vm_obj_id_t self, class CTcGramProdAlt *alt,
408 vm_obj_id_t dict, vm_obj_id_t match);
409
410 /* delete an alternative, keeping undo */
411 void delete_alt_undo(VMG_ vm_obj_id_t self, int idx);
412
413 /* delete a batch of alternatives - deletes each item marked with 'del' */
414 void delete_marked_alts(VMG_ vm_obj_id_t self, vm_obj_id_t dict);
415
416 /* find a literal in the surviving alternatives */
417 int find_literal_in_alts(const char *str, size_t len);
418
419 /* save to a data stream */
420 void save_to_stream(VMG_ class CVmStream *dst);
421
422 /* mark references in an alt list (for garbage collection tracing) */
423 void mark_alt_refs(VMG_ const vmgram_alt_info *alt, uint state);
424
425 /* load alternatives from a stream, in image file format */
426 void load_alts(VMG_ vm_obj_id_t self,
427 class CVmStream *src, class CVmObjFixup *fixups);
428
429 /* property evaluation - undefined property */
430 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
431
432 /* property evaluation - parseTokens */
433 int getp_parse(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
434
435 /* property evaluation - getGrammarInfo */
436 int getp_get_gram_info(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
437
438 /* property evaluator - delete an alternative */
439 int getp_deleteAlt(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
440
441 /* property evaluator - delete all alternatives */
442 int getp_clearAlts(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
443
444 /* property evaluator - add an alternative */
445 int getp_addAlt(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
446
447 /* get my extension, properly cast */
448 vm_gram_ext *get_ext() const { return (vm_gram_ext *)ext_; }
449
450 /* callback for dictionary word property enumeration */
451 static void enum_props_cb(VMG_ void *ctx, vm_prop_id_t prop,
452 const vm_val_t *match_val);
453
454 /* search a token for a match to the given vocabulary property */
455 static int find_prop_in_tok(const struct vmgramprod_tok *tok,
456 vm_prop_id_t prop);
457
458 /* enqueue our alternatives */
459 void enqueue_alts(VMG_ class CVmGramProdMem *mem,
460 const struct vmgramprod_tok *tok,
461 size_t tok_cnt, size_t start_tok_pos,
462 struct CVmGramProdState *state,
463 struct CVmGramProdQueue *queues,
464 vm_obj_id_t self, int circ_only,
465 struct CVmGramProdMatch *circ_match,
466 class CVmObjDict *dict);
467
468 /* create and enqueue a new state */
469 static struct CVmGramProdState *
470 enqueue_new_state(class CVmGramProdMem *mem,
471 size_t start_tok_pos,
472 struct CVmGramProdState *enclosing_state,
473 const vmgram_alt_info *altp, vm_obj_id_t self,
474 int *need_to_clone,
475 struct CVmGramProdQueue *queues,
476 int circular_alt);
477
478 /* create a new state */
479 static struct CVmGramProdState *
480 create_new_state(class CVmGramProdMem *mem,
481 size_t start_tok_pos,
482 struct CVmGramProdState *enclosing_state,
483 const vmgram_alt_info *altp, vm_obj_id_t self,
484 int *need_to_clone, int circular_alt);
485
486 /* enqueue a state */
487 static void enqueue_state(struct CVmGramProdState *state,
488 struct CVmGramProdQueue *queues);
489
490 /* process the work queue */
491 static void process_work_queue(VMG_ CVmGramProdMem *mem,
492 const struct vmgramprod_tok *tok,
493 size_t tok_cnt,
494 struct CVmGramProdQueue *queues,
495 class CVmObjDict *dict);
496
497
498 /* process the first work queue entry */
499 static void process_work_queue_head(VMG_ CVmGramProdMem *mem,
500 const struct vmgramprod_tok *tok,
501 size_t tok_cnt,
502 struct CVmGramProdQueue *queues,
503 class CVmObjDict *dict);
504
505 /* build a match tree */
506 static void build_match_tree(VMG_ const struct CVmGramProdMatch *match,
507 const vm_val_t *tok_list,
508 const vm_val_t *tok_match_list,
509 vm_val_t *retval,
510 size_t *first_tok, size_t *last_tok);
511
512 /* cache the hash values for the literal tokens in our alternatives */
513 void cache_hashes(VMG_ CVmObjDict *dict);
514
515 /* calculate the hash value for a literal string */
516 static unsigned int calc_str_hash(VMG_ class CVmObjDict *dict,
517 const vm_val_t *strval,
518 const char *str, size_t len);
519
520 /* check to see if a token matches a literal */
521 static int tok_equals_lit(VMG_ const struct vmgramprod_tok *tok,
522 const char *lit, size_t lit_len,
523 class CVmObjDict *dict,
524 vm_val_t *match_result);
525
526 /* property evaluation function table */
527 static int (CVmObjGramProd::*func_table_[])(VMG_ vm_obj_id_t self,
528 vm_val_t *retval, uint *argc);
529 };
530
531
532 /* ------------------------------------------------------------------------ */
533 /*
534 * Registration table object
535 */
536 class CVmMetaclassGramProd: public CVmMetaclass
537 {
538 public:
539 /* get the global name */
540 const char *get_meta_name() const { return "grammar-production/030002"; }
541
542 /* create from image file */
543 void create_for_image_load(VMG_ vm_obj_id_t id)
544 {
545 new (vmg_ id) CVmObjGramProd(vmg0_);
546 G_obj_table->set_obj_gc_characteristics(id, TRUE, TRUE);
547 }
548
549 /* create from restoring from saved state */
550 void create_for_restore(VMG_ vm_obj_id_t id)
551 {
552 new (vmg_ id) CVmObjGramProd(vmg0_);
553 G_obj_table->set_obj_gc_characteristics(id, TRUE, TRUE);
554 }
555
556 /* create dynamically using stack arguments */
557 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
558 { return CVmObjGramProd::create_from_stack(vmg_ pc_ptr, argc); }
559
560 /* call a static property */
561 int call_stat_prop(VMG_ vm_val_t *result,
562 const uchar **pc_ptr, uint *argc,
563 vm_prop_id_t prop)
564 {
565 return CVmObjGramProd::
566 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
567 }
568 };
569
570
571 #endif /* VMGRAM_H */
572
573 /*
574 * Register the class
575 */
576 VM_REGISTER_METACLASS(CVmObjGramProd)
577