summaryrefslogtreecommitdiff
path: root/colm/pdarun.h
blob: 240d97a49bb23952ad7a4f7e927b84c76f581205 (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 *  Copyright 2007 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 _PDARUN_H
#define _PDARUN_H

#include <iostream>
#include "dlistval.h"
#include "bytecode.h"
#include "vector.h"
#include "dlist.h"

using std::ostream;

struct Tree;
struct ParseData;
struct FsmRun;
struct KlangEl;
struct PdaTables;
struct FsmTables;
struct InputStream;

struct Kid
{
	/* The tree needs to be first since pointers to kids are used to reference
	 * trees on the stack. A pointer to the word that is a Tree* is cast to
	 * a Kid*. */
	Tree *tree;
	Kid *next;
};

struct Ref
{
	Kid *kid;
	Ref *next;
};

struct Tree
{
	/* First four will be overlaid in other structures. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	Head *tokdata;
};

struct ParseTree
{
	/* Entire structure must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	Head *tokdata;

	/* Parsing algorithm. */
	long state;
	long region;
	char causeReduce;
	char retry_lower;
	char retry_upper;
};


struct Int
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	long value;
};

struct Pointer
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	Kid *value;
};

struct Str
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	Head *value;
};

struct ListEl
{
	/* Must overlay kid. */
	Tree *value;
	ListEl *next;
	ListEl *prev;

	ListEl() { }
	ListEl( Tree *value )
		: value(value) { }
};

struct List
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	ListEl *head;

	ListEl *tail;
	long listLen;
	GenericInfo *genericInfo;

	void prepend(ListEl *new_el) { addBefore(head, new_el); }
	void append(ListEl *new_el)  { addAfter(tail, new_el); }

	void addAfter( ListEl *prev_el, ListEl *new_el );
	void addBefore( ListEl *next_el, ListEl *new_el );

	ListEl *detachFirst()        { return detach(head); }
	ListEl *detachLast()         { return detach(tail); }
	ListEl *detach(ListEl *el);

	long length() const 
		{ return listLen; }
};

struct MapEl
{
	/* Must overlay Kid. */
	Tree *tree;
	MapEl *next;
	MapEl *prev;

	MapEl *left, *right, *parent;
	long height;
	Tree *key;

	Tree *getKey() const 
		{ return key; }
};

struct Map
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	MapEl *head;

	MapEl *tail;
	MapEl *root;
	long treeSize;
	GenericInfo *genericInfo;

	/* List functions. */
	MapEl *listDetach( MapEl *el );
	void listAddBefore( MapEl *next_el, MapEl *new_el );
	void listAddAfter( MapEl *prev_el, MapEl *new_el );
	void listAbandon();

	int compare( const Tree *w1, const Tree *w2 ) const
		{ return cmp_tree( w1, w2 ); }

	/* Insert a element into the tree. */
	MapEl *insert( MapEl *element, MapEl **lastFound = 0 );

	MapEl *insert( Program *p, Tree *key, MapEl **lastFound = 0 );

	/* Find a element in the tree. Returns the element if 
	 * key exists, false otherwise. */
	MapEl *find( Tree *key ) const;

	/* Detach a element from the tree. */
	MapEl *detach( Tree *key );

	/* Detach and delete a element from the tree. */
	bool remove( Tree *key );

	/* Detach a element from the tree. */
	MapEl *detach( MapEl *element );

	/* Detach and delete a element from the tree. */
	void remove( MapEl *element );

	/* Free all memory used by tree. */
	void empty();

	/** \brief Return the number of elements in the tree. */
	long length() const { return treeSize; }

	/* Recursive worker for the copy constructor. */
	MapEl *copyBranch( Program *p, MapEl *element, Kid *oldNextDown, Kid *&newNextDown );

	/* Recursively delete element in the tree. */
	void deleteChildrenOf(MapEl *n);

	/* rebalance the tree beginning at the leaf whose 
	 * grandparent is unbalanced. */
	MapEl *rebalance(MapEl *start);

	/* Move up the tree from a given element, recalculating the heights. */
	void recalcHeights(MapEl *start);

	/* Move up the tree and find the first element whose 
	 * grand-parent is unbalanced. */
	MapEl *findFirstUnbalGP(MapEl *start);

	/* Move up the tree and find the first element which is unbalanced. */
	MapEl *findFirstUnbalEl(MapEl *start);

	/* Replace a element in the tree with another element not in the tree. */
	void replaceEl(MapEl *element, MapEl *replacement);

	/* Remove a element from the tree and put another (normally a child of element)
	 * in its place. */
	void removeEl(MapEl *element, MapEl *filler);

	/* Once an insertion point is found at a leaf then do the insert. */
	void attachRebal( MapEl *element, MapEl *parentEl, MapEl *lastLess );
};

struct Stream
{
	/* Must overlay Tree. */
	short id;
	unsigned short flags;
	long refs;
	Kid *child;

	FILE *file;
	InputStream *in;
	FsmRun *fsmRun;
};

/*
 * Iterators.
 */

struct TreeIter
{
	TreeIter( const Ref &rootRef, int searchId, Tree **stackRoot ) : 
		rootRef(rootRef), searchId(searchId), 
		stackRoot(stackRoot), stackSize(0)
	{
		ref.kid = 0;
		ref.next = 0;
	}
	
	Ref rootRef;
	Ref ref;
	long searchId;
	Tree **stackRoot;
	long stackSize;
};

struct FunctionInfo
{
	const char *name;
	long frameId;
	long argSize;
	long ntrees;
	long frameSize;
};

struct UserIter
{
	UserIter( Tree **stackRoot, long argSize, long searchId ) : 
		stackRoot(stackRoot), 
		argSize(argSize), stackSize(0),
		resume(0), frame(0), searchId(searchId)
	{
		ref.kid = 0;
		ref.next = 0;
	}
		
	/* The current item. */
	Ref ref;
	Tree **stackRoot;
	long argSize;
	long stackSize;
	Code *resume;
	Tree **frame;
	long searchId;
};

/*
 * Program Data.
 */

struct PatReplInfo
{
	long offset;
	long numBindings;
};

struct PatReplNode
{
	long id;
	long next;
	long child;
	long bindId;
	const char *data;
	long length;
	long ignore;

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

struct LangElInfo
{
	const char *name;
	bool repeat;
	bool list;
	bool literal;
	bool ignore;

	long frameId;

	long objectTypeId;
	long ofiOffset;
	long objectLength;

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

struct ObjFieldInfo
{
	int typeId;
};

struct ProdInfo
{
	long length;
	unsigned long lhsId;
	const char *name;
	long frameId;
	bool lhsUpref;
};

struct FrameInfo
{
	Code *codeWV;
	long codeLenWV;
	Code *codeWC;
	long codeLenWC;
	char *trees;
	long treesLen;
};

struct RegionInfo
{
	const char *name;
	long defaultToken;
	long eofFrameId;
};

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

struct RuntimeData
{
	LangElInfo *lelInfo;
	long numLangEls;

	ProdInfo *prodInfo;
	long numProds;

	RegionInfo *regionInfo;
	long numRegions;

	Code *rootCode;
	long rootCodeLen;

	FrameInfo *frameInfo;
	long numFrames;

	FunctionInfo *functionInfo;
	long numFunctions;

	PatReplInfo *patReplInfo;
	long numPatterns;

	PatReplNode *patReplNodes;
	long numPatternNodes;

	GenericInfo *genericInfo;
	long numGenerics;

	const char **litdata;
	long *litlen;
	Head **literals;
	long numLiterals;

	CaptureAttr *captureAttr;
	long numCapturedAttr;

	FsmTables *fsmTables;
	PdaTables *pdaTables;
	int *startStates;
	int *eofLelIds;
	long numParsers;

	long globalSize;

	long firstNonTermId;

	long integerId;
	long stringId;
	long anyId;
	long eofId;
	long noTokenId;
};

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

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

	RuntimeData *rtd;
};

bool make_reverse_code( CodeVect *all, CodeVect &reverseCode );

typedef Vector<Tree*> Bindings;

struct PdaRun
{
	PdaRun( Tree **root, Program *prg, PdaTables *tables, int parserId,
			FsmRun *scanner, long stopTarget, bool revertOn )
	:
		root(root),
		prg(prg),
		tables(tables), 
		parserId(parserId), 
		fsmRun(scanner), 
		stopParsing(false),
		stopTarget(stopTarget),
		queue(0),
		queueLast(0),
		revertOn(revertOn)
	{
	}

	Tree **root;
	int numRetry;
	Kid *stackTop;
	int errCount;
	int cs;
	int nextRegionInd;

	/* Offset can be used to look at the next nextRegionInd. */
	int getNextRegion( int offset = 0 )
		{ return tables->tokenRegions[nextRegionInd+offset]; }

	Tree *getParsedRoot( bool stop );
	void clean();

	Program *prg;
	PdaTables *tables;
	int parserId;

	FsmRun *fsmRun;

	long stackTopTarget();
	void init();
	void commitKid( Tree **root, Kid *lel );
	void commit();
	void parseToken( Kid *input );
	bool isParserStopFinished();
	void match( Kid *tree, Kid *pattern );
	long undoParse( Tree *tree, CodeVect *rev );

	void send( Kid *kid );
	void ignore( Tree *tree );
	void sendBackIgnore();
	Kid *extractIgnore();

	/* Report an error encountered by the parser. */
	ostream &parse_error( int tokId, Tree *tree );

	/* Reused. */
	CodeVect reverseCode;
	CodeVect *allReverseCode;

	bool stopParsing;
	long stopTarget;

	Kid *accumIgnore;
	Kid *queue, *queueLast;

	Bindings bindings;

	bool revertOn;
};

void xml_print_list( RuntimeData *runtimeData, Kid *lel, int depth );

#endif /* _PDARUN_H */