summaryrefslogtreecommitdiff
path: root/src/pdarun.h
blob: 39c809fe559844133522915bb49089f30a704caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 *  Copyright 2007-2012 Adrian Thurston <thurston@complang.org>
 */

/*  This file is part of Colm.
 *
 *  Colm is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  Colm is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with Colm; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#ifndef __COLM_PDARUN_H
#define __COLM_PDARUN_H

#include <colm/input.h>
#include <colm/defs.h>
#include <colm/tree.h>
#include <colm/struct.h>

#ifdef __cplusplus
extern "C" {
#endif

struct colm_program;

#define MARK_SLOTS 32

struct fsm_tables
{
	long *actions;
	long *keyOffsets;
	char *transKeys;
	long *singleLengths;
	long *rangeLengths;
	long *indexOffsets;
	long *transTargsWI;
	long *transActionsWI;
	long *toStateActions;
	long *fromStateActions;
	long *eofActions;
	long *eofTargs;
	long *entryByRegion;

	long numStates;
	long numActions;
	long numTransKeys;
	long numSingleLengths;
	long numRangeLengths;
	long numIndexOffsets;
	long numTransTargsWI;
	long numTransActionsWI;
	long numRegions;

	long startState;
	long firstFinal;
	long errorState;

	struct GenAction **actionSwitch;
	long numActionSwitch;
};

void undoStreamPull( StreamImpl *inputStream, const char *data, long length );

#if SIZEOF_LONG != 4 && SIZEOF_LONG != 8 
	#error "SIZEOF_LONG contained an unexpected value"
#endif

struct colm_execution;

struct rt_code_vect
{
	Code *data;
	long tabLen;
	long allocLen;

	/* FIXME: leak when freed. */
};

void listAddAfter( List *list, ListEl *prev_el, ListEl *new_el );
void listAddBefore( List *list, ListEl *next_el, ListEl *new_el );

void listPrepend( List *list, ListEl *new_el );
void listAppend( List *list, ListEl *new_el );

ListEl *listDetach( List *list, ListEl *el );
ListEl *listDetachFirst(List *list );
ListEl *listDetachLast(List *list );

long listLength(List *list);

struct function_info
{
	long frameId;
	long argSize;
	long frameSize;
};

/*
 * Program Data.
 */

struct pat_cons_info
{
	long offset;
	long numBindings;
};

struct pat_cons_node
{
	long id;
	long prodNum;
	long next;
	long child;
	long bindId;
	const char *data;
	long length;
	long leftIgnore;
	long rightIgnore;

	/* Just match nonterminal, don't go inside. */
	unsigned char stop;
};

/* FIXME: should have a descriptor for object types to give the length. */

struct lang_el_info
{
	const char *name;
	const char *xmlTag;
	unsigned char repeat;
	unsigned char list;
	unsigned char literal;
	unsigned char ignore;

	long frameId;

	long objectTypeId;
	long ofiOffset;
	long objectLength;

	long termDupId;
	long markId;
	long captureAttr;
	long numCaptureAttr;
};

struct struct_el_info
{
	long size;
	short *trees;
	long treesLen;
};

struct prod_info
{
	unsigned long lhsId;
	short prodNum;
	long length;
	const char *name;
	long frameId;
	unsigned char lhsUpref;
	unsigned char *copy;
	long copyLen;
};

/* Must match the LocalType enum. */
#define LI_Tree 1
#define LI_Iter 2
#define LI_RevIter 3
#define LI_UserIter 4

struct local_info
{
	char type;
	short offset;
};

struct frame_info
{
	const char *name;
	Code *codeWV;
	long codeLenWV;
	Code *codeWC;
	long codeLenWC;
	struct local_info *locals;
	long localsLen;
	long argSize;
	long frameSize;
	char retTree;
};

struct region_info
{
	long defaultToken;
	long eofFrameId;
	int ciLelId;
};

typedef struct _CaptureAttr
{
	long mark_enter;
	long mark_leave;
	long offset;
} CaptureAttr;

struct pda_tables
{
	/* Parser table data. */
	int *indicies;
	int *owners;
	int *keys;
	unsigned int *offsets;
	unsigned int *targs;
	unsigned int *actInds;
	unsigned int *actions;
	int *commitLen;
	int *tokenRegionInds;
	int *tokenRegions;
	int *tokenPreRegions;

	int numIndicies;
	int numKeys;
	int numStates;
	int numTargs;
	int numActInds;
	int numActions;
	int numCommitLen;
	int numRegionItems;
	int numPreRegionItems;
};

struct pool_block
{
	void *data;
	struct pool_block *next;
};

struct pool_item
{
	struct pool_item *next;
};

struct pool_alloc
{
	struct pool_block *head;
	long nextel;
	struct pool_item *pool;
	int sizeofT;
};

struct pda_run
{
	/*
	 * Scanning.
	 */
	struct fsm_tables *fsm_tables;

	RunBuf *consumeBuf;

	long region, preRegion;
	long fsm_cs, next_cs, act;
	char *start;
	char *tokstart;
	long tokend;
	long toklen;
	char *p, *pe;

	/* Bits. */
	char eof;
	char returnResult;
	char skipToklen;

	char *mark[MARK_SLOTS];
	long matchedToken;

	/*
	 * Parsing
	 */
	int numRetry;
	ParseTree *stackTop;
	Ref *tokenList;
	int pda_cs;
	int nextRegionInd;

	struct pda_tables *pda_tables;
	int parserId;

	/* Reused. */
	struct rt_code_vect rcodeCollect;
	struct rt_code_vect reverseCode;

	int stopParsing;
	long stopTarget;

	ParseTree *accumIgnore;

	Kid *btPoint;

	struct Bindings *bindings;

	int revertOn;

	struct colm_struct *context;

	int stop;
	int parseError;

	long steps;
	long targetSteps;
	long shiftCount;
	long commitShiftCount;

	int onDeck;

	/*
	 * Data we added when refactoring the parsing engine into a coroutine.
	 */

	ParseTree *parseInput;
	struct frame_info *fi;
	int reduction;
	ParseTree *redLel;
	int curState;
	ParseTree *lel;
	int triggerUndo;

	int tokenId;
	Head *tokdata;
	int frameId;
	int next;
	ParseTree *undoLel;

	int checkNext;
	int checkStop;

	/* The lhs is sometimes saved before reduction actions in case it is
	 * replaced and we need to restore it on backtracking */
	Tree *parsed;

	int reject;

	/* Instruction pointer to use when we stop parsing and execute code. */
	Code *code;

	int rcBlockCount;

	Tree *parseErrorText;
};

void colm_pda_init( struct colm_program *prg, struct pda_run *pdaRun,
		struct pda_tables *tables, int parserId, long stopTarget,
		int revertOn, struct colm_struct *context );

void colm_pda_clear( struct colm_program *prg, struct colm_tree **sp,
		struct pda_run *pdaRun );

void rtCodeVectReplace( struct rt_code_vect *vect, long pos, const Code *val, long len );
void rtCodeVectEmpty( struct rt_code_vect *vect );
void rtCodeVectRemove( struct rt_code_vect *vect, long pos, long len );

void initRtCodeVect( struct rt_code_vect *codeVect );

inline static void append_code_val( struct rt_code_vect *vect, const Code val );
inline static void append_code_vect( struct rt_code_vect *vect, const Code *val, long len );
inline static void append_half( struct rt_code_vect *vect, Half half );
inline static void append_word( struct rt_code_vect *vect, Word word );

inline static void append_code_vect( struct rt_code_vect *vect, const Code *val, long len )
{
	rtCodeVectReplace( vect, vect->tabLen, val, len );
}

inline static void append_code_val( struct rt_code_vect *vect, const Code val )
{
	rtCodeVectReplace( vect, vect->tabLen, &val, 1 );
}

inline static void append_half( struct rt_code_vect *vect, Half half )
{
	/* not optimal. */
	append_code_val( vect, half & 0xff );
	append_code_val( vect, (half>>8) & 0xff );
}

inline static void append_word( struct rt_code_vect *vect, Word word )
{
	/* not optimal. */
	append_code_val( vect, word & 0xff );
	append_code_val( vect, (word>>8) & 0xff );
	append_code_val( vect, (word>>16) & 0xff );
	append_code_val( vect, (word>>24) & 0xff );
	#if SIZEOF_LONG == 8
	append_code_val( vect, (word>>32) & 0xff );
	append_code_val( vect, (word>>40) & 0xff );
	append_code_val( vect, (word>>48) & 0xff );
	append_code_val( vect, (word>>56) & 0xff );
	#endif
}

void colm_increment_steps( struct pda_run *pdaRun );
void colm_decrement_steps( struct pda_run *pdaRun );

void clearStreamImpl( struct colm_program *prg, Tree **sp, StreamImpl *inputStream );
void initSourceStream( StreamImpl *in );
void clearSourceStream( struct colm_program *prg, Tree **sp, StreamImpl *sourceStream );


void clearContext( struct pda_run *pdaRun, Tree **sp );
void runCommit( struct pda_run *pdaRun );
void pdaRunMatch(  struct pda_run *pdaRun, Kid *tree, Kid *pattern );

/* Offset can be used to look at the next nextRegionInd. */
int pdaRunGetNextRegion( struct pda_run *pdaRun, int offset );
int pdaRunGetNextPreRegion( struct pda_run *pdaRun );

#define PCR_START         1
#define PCR_DONE          2
#define PCR_REDUCTION     3
#define PCR_GENERATION    4
#define PCR_PRE_EOF       5
#define PCR_REVERSE       6

Head *colm_stream_pull( struct colm_program *prg, struct colm_tree **sp,
		struct pda_run *pdaRun, StreamImpl *is, long length );
Head *stringAllocPointer( struct colm_program *prg, const char *data, long length );

void streamPushText( StreamImpl *inputStream, const char *data, long length );
void streamPushTree( StreamImpl *inputStream, Tree *tree, int ignore );
void streamPushStream( StreamImpl *inputStream, Tree *tree );
void undoStreamPush( struct colm_program *prg, Tree **sp,
		StreamImpl *inputStream, long length );
void undoStreamAppend( struct colm_program *prg, Tree **sp,
		StreamImpl *inputStream, struct colm_tree *tree, long length );

Kid *make_token_with_data( struct colm_program *prg, struct pda_run *pdaRun,
		StreamImpl *inputStream, int id, Head *tokdata );

void pushBinding( struct pda_run *pdaRun, ParseTree *parseTree );

long colm_parse_loop( struct colm_program *prg, Tree **sp, struct pda_run *pdaRun, 
		StreamImpl *inputStream, long entry );

long colm_parse_frag( struct colm_program *prg, Tree **sp, struct pda_run *pdaRun,
		Stream *input, long stopId, long entry );
long colm_parse_finish( Tree **result, struct colm_program *prg, Tree **sp,
		struct pda_run *pdaRun, Stream *input , int revertOn, long entry );
long colm_parse_undo_frag( struct colm_program *prg, Tree **sp, struct pda_run *pdaRun,
		Stream *input, long steps, long entry );

#ifdef __cplusplus
}
#endif

#endif