summaryrefslogtreecommitdiff
path: root/colm/tree.h
blob: f6f5d4889fabf5f85bd1d41e668a2bf3c1028122 (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
/*
 *  Copyright 2010-2011 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 _TREE_H
#define _TREE_H

#if defined(__cplusplus)
extern "C" {
#endif

typedef struct _TreePair
{
	Tree *key;
	Tree *val;
} TreePair;


void treeUpref( Tree *tree );
void treeDownref( Program *prg, Tree **sp, Tree *tree );
long cmpTree( Program *prg, const Tree *tree1, const Tree *tree2 );
Tree *treeLeftIgnore( Program *prg, Tree *tree );
Tree *treeRightIgnore( Program *prg, Tree *tree );
Kid *treeLeftIgnoreKid( Program *prg, Tree *tree );
Kid *treeRightIgnoreKid( Program *prg, Tree *tree );
Kid *treeChild( Program *prg, const Tree *tree );
Kid *kidListConcat( Kid *list1, Kid *list2 );
Kid *treeExtractChild( Program *prg, Tree *tree );

Tree *constructInteger( Program *prg, long i );
Tree *constructPointer( Program *prg, Tree *tree );
Tree *constructTerm( Program *prg, Word id, Head *tokdata );
Tree *constructReplacementTree( Tree **bindings, Program *prg, long pat );
Tree *createGeneric( Program *prg, long genericId );

Tree *makeToken2( Tree **root, Program *prg, long nargs );
int testFalse( Program *prg, Tree *tree );
Tree *makeTree( Tree **root, Program *prg, long nargs );
Stream *openFile( Program *prg, Tree *name, Tree *mode );
Stream *openStreamFd( Program *prg, long fd );
Kid *copyIgnoreList( Program *prg, Kid *ignoreHeader );
void streamFree( Program *prg, Stream *s );
Tree *copyTree( Program *prg, Tree *tree, Kid *oldNextDown, Kid **newNextDown );

Tree *getPtrVal( Pointer *ptr );
Tree *getPtrValSplit( Program *prg, Pointer *ptr );
Tree *getField( Tree *tree, Word field );
Tree *getFieldSplit( Program *prg, Tree *tree, Word field );
Tree *getRhsEl( Program *prg, Tree *lhs, long position );
void setField( Program *prg, Tree *tree, long field, Tree *value );

void setTriterCur( TreeIter *iter, Tree *tree );
void refSetValue( Ref *ref, Tree *v );
Tree *treeSearch( Program *prg, Kid *kid, long id );
Tree *treeSearch2( Program *prg, Tree *tree, long id );

int matchPattern( Tree **bindings, Program *prg, long pat, Kid *kid, int checkNext );
Tree *treeIterDerefCur( TreeIter *iter );

/* For making references of attributes. */
Kid *getFieldKid( Tree *tree, Word field );

Tree *copyRealTree( Program *prg, Tree *tree, Kid *oldNextDown, Kid **newNextDown, int parsed );
void splitIterCur( Tree ***psp, Program *prg, TreeIter *iter );
Tree *setListMem( List *list, Half field, Tree *value );

void listAppend2( Program *prg, List *list, Tree *val );
Tree *listRemoveEnd( Program *prg, List *list );
Tree *getListMem( List *list, Word field );
Tree *getListMemSplit( Program *prg, List *list, Word field );

Tree *treeIterAdvance( Program *prg, Tree ***psp, TreeIter *iter );
Tree *treeIterNextChild( Program *prg, Tree ***psp, TreeIter *iter );
Tree *treeRevIterPrevChild( Program *prg, Tree ***psp, RevTreeIter *iter );
Tree *treeIterNextRepeat( Program *prg, Tree ***psp, TreeIter *iter );
Tree *treeIterPrevRepeat( Program *prg, Tree ***psp, TreeIter *iter );

void printXmlTree( Tree **sp, Program *prg, Tree *tree, int commAttr );


/* An automatically grown buffer for collecting tokens. Always reuses space;
 * never down resizes. */
typedef struct _StrCollect
{
	char *data;
	int allocated;
	int length;
} StrCollect;

typedef struct _PrintArgs
{
	void *arg;
	void (*out)( void *arg, const char *data, int length );
} PrintArgs;

void initStrCollect( StrCollect *collect );
void strCollectDestroy( StrCollect *collect );
void strCollectAppend( StrCollect *collect, const char *data, long len );
void strCollectClear( StrCollect *collect );

void printTree( StrCollect *collect, Tree **sp, Program *prg, Tree *tree );

void printTreeArgs( PrintArgs *printArgs, Tree **sp, Program *prg, Tree *tree );
void printTreeCollect( StrCollect *collect, Tree **sp, Program *prg, Tree *tree );
void printTreeFile( FILE *out, Tree **sp, Program *prg, Tree *tree );

#if defined(__cplusplus)
}
#endif

#endif