Debian Codegraph @

terps/tads/tads3/vmpat.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmpat.h - regular-expression compiled pattern object
10 Function
11 Encapsulates a compiled regular expression as an object. This object
12 allows a compiled regular expression to be saved for re-use; since regex
13 compilation is time-consuming, it's much more efficient to re-use a
14 compiled pattern in repeated searches than to recompile the expression
15 each time it's needed.
16 Notes
17
18 Modified
19 08/27/02 MJRoberts - Creation
20 */
21
22 #ifndef VMPAT_H
23 #define VMPAT_H
24
25 #include <stdlib.h>
26 #include "os.h"
27 #include "vmtype.h"
28 #include "vmobj.h"
29 #include "vmglob.h"
30 #include "vmregex.h"
31
32 /* ------------------------------------------------------------------------ */
33 /*
34 * Our serialized data stream, in both the image file and a saved file,
35 * consists of:
36 *
37 * DATAHOLDER src_val
38 *
39 * 'src_val' is the source value - this is the string that was compiled to
40 * create the pattern.
41 */
42
43 /* ------------------------------------------------------------------------ */
44 /*
45 * Our in-memory extension consists of a simple structure with a pointer
46 * to the compiled pattern data (the re_compiled_pattern structure) and
47 * the original string value that was used to create the pattern (we hold
48 * onto the original string mostly for debugging purposes).
49 */
50 struct vmobj_pat_ext
51 {
52 /* the compiled pattern data */
53 re_compiled_pattern *pat;
54
55 /* the original pattern source string */
56 vm_val_t str;
57 };
58
59 /* ------------------------------------------------------------------------ */
60 /*
61 * Pattern intrinsic class
62 */
63 class CVmObjPattern: public CVmObject
64 {
65 friend class CVmMetaclassPattern;
66
67 public:
68 /* metaclass registration object */
69 static class CVmMetaclass *metaclass_reg_;
70 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
71
72 /* am I of the given metaclass? */
73 virtual int is_of_metaclass(class CVmMetaclass *meta) const
74 {
75 /* try my own metaclass and my base class */
76 return (meta == metaclass_reg_
77 || CVmObject::is_of_metaclass(meta));
78 }
79
80 /* is this a pattern object? */
81 static int is_pattern_obj(VMG_ vm_obj_id_t obj)
82 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
83
84 /* create dynamically using stack arguments */
85 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
86 uint argc);
87
88 /*
89 * call a static property - we don't have any of our own, so simply
90 * "inherit" the base class handling
91 */
92 static int call_stat_prop(VMG_ vm_val_t *result,
93 const uchar **pc_ptr, uint *argc,
94 vm_prop_id_t prop)
95 {
96 /* defer to our base class */
97 return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
98 }
99
100 /* notify of deletion */
101 void notify_delete(VMG_ int in_root_set);
102
103 /* set a property */
104 void set_prop(VMG_ class CVmUndo *undo,
105 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
106
107 /* get a property */
108 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
109 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
110
111 /* cast to string - return the original pattern string */
112 const char *cast_to_string(VMG_ vm_obj_id_t self,
113 vm_val_t *new_str) const
114 {
115 /* return the original string value */
116 *new_str = *get_orig_str();
117 return new_str->get_as_string(vmg0_);
118 }
119
120 /* undo operations - we are immutable and hence keep no undo */
121 void notify_new_savept() { }
122 void apply_undo(VMG_ struct CVmUndoRecord *) { }
123 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
124 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
125
126 /* mark references */
127 void mark_refs(VMG_ uint);
128
129 /* remove stale weak references - we have no weak references */
130 void remove_stale_weak_refs(VMG0_) { }
131
132 /* load from an image file */
133 void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t);
134
135 /* perform post-load initialization */
136 void post_load_init(VMG_ vm_obj_id_t self);
137
138 /* rebuild for image file */
139 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
140
141 /* convert to constant data */
142 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
143 vm_obj_id_t self);
144
145 /* save to a file */
146 void save_to_file(VMG_ class CVmFile *fp);
147
148 /* restore from a file */
149 void restore_from_file(VMG_ vm_obj_id_t self,
150 class CVmFile *fp, class CVmObjFixup *fixup);
151
152 /* get my compiled pattern */
153 re_compiled_pattern *get_pattern(VMG0_) { return get_ext()->pat; }
154
155 /* get my string object */
156 const vm_val_t *get_orig_str() const { return &get_ext()->str; }
157
158 protected:
159 /* create with no extension */
160 CVmObjPattern() { ext_ = 0; }
161
162 /* create with a given pattern object and source string value */
163 CVmObjPattern(VMG_ re_compiled_pattern *pat, const vm_val_t *src_str);
164
165 /* set my compiled pattern data structure */
166 void set_pattern(re_compiled_pattern *pat) { get_ext()->pat = pat; }
167
168 /* set my original source string value */
169 void set_orig_str(const vm_val_t *val) { get_ext()->str = *val; }
170
171 /* get my extension data */
172 vmobj_pat_ext *get_ext() const { return (vmobj_pat_ext *)ext_; }
173
174 /* property evaluator - undefined property */
175 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
176
177 /* property evaluator - get my original string */
178 int getp_get_str(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc);
179
180 /* property evaluation function table */
181 static int (CVmObjPattern::*func_table_[])(VMG_ vm_obj_id_t self,
182 vm_val_t *retval, uint *argc);
183 };
184
185 /* ------------------------------------------------------------------------ */
186 /*
187 * Registration table object
188 */
189 class CVmMetaclassPattern: public CVmMetaclass
190 {
191 public:
192 /* get the global name */
193 const char *get_meta_name() const { return "regex-pattern/030000"; }
194
195 /* create from image file */
196 void create_for_image_load(VMG_ vm_obj_id_t id)
197 {
198 new (vmg_ id) CVmObjPattern();
199 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
200 }
201
202 /* create from restoring from saved state */
203 void create_for_restore(VMG_ vm_obj_id_t id)
204 {
205 new (vmg_ id) CVmObjPattern();
206 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
207 }
208
209 /* create dynamically using stack arguments */
210 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
211 { return CVmObjPattern::create_from_stack(vmg_ pc_ptr, argc); }
212
213 /* call a static property */
214 int call_stat_prop(VMG_ vm_val_t *result,
215 const uchar **pc_ptr, uint *argc,
216 vm_prop_id_t prop)
217 {
218 return CVmObjPattern::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
219 }
220 };
221
222 #endif /* VMPAT_H */
223
224 /*
225 * Register the class
226 */
227 VM_REGISTER_METACLASS(CVmObjPattern)
228