summaryrefslogtreecommitdiff
path: root/colm/tree.cpp
blob: 198dc36e363ab2fc37a4eee6a95540536a2e70b5 (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
/*
 *  Copyright 2008 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 
 */

#include "bytecode.h"
#include "pdarun.h"
#include "fsmrun.h"
#include "pdarun.h"
#include <iostream>
#include <string.h>
#include <stdlib.h>

using std::cout;
using std::cerr;
using std::endl;
using std::ostream;


void printStr( ostream &out, Head *str )
{
	out.write( (char*)(str->data), str->length );
}


/* Note that this function causes recursion, thought it is not a big
 * deal since the recursion it is only caused by nonterminals that are ignored. */
void printIgnoreList( ostream &out, Tree **sp, Program *prg, Tree *tree )
{
	Kid *ignore = treeIgnore( prg, tree );

	/* Record the root of the stack and push everything. */
	Tree **root = vm_ptop();
	while ( ignore != 0 ) {
		vm_push( (SW)ignore );
		ignore = ignore->next;
	}

	/* Pop them off and print. */
	while ( vm_ptop() != root ) {
		ignore = (Kid*) vm_pop();
		printTree( out, sp, prg, ignore->tree );
	}
}

void printKid( ostream &out, Tree **&sp, Program *prg, Kid *kid, bool printIgnore )
{
	Tree **root = vm_ptop();
	Kid *child;

rec_call:
	/* If not currently skipping ignore data, then print it. Ignore data can
	 * be associated with terminals and nonterminals. */
	if ( printIgnore && treeIgnore( prg, kid->tree ) != 0 ) {
		/* Ignorelists are reversed. */
		printIgnoreList( out, sp, prg, kid->tree );
		printIgnore = false;
	}

	if ( kid->tree->id < prg->rtd->firstNonTermId ) {
		/* Always turn on ignore printing when we get to a token. */
		printIgnore = true;

		if ( kid->tree->id == LEL_ID_INT )
			out << ((Int*)kid->tree)->value;
		else if ( kid->tree->id == LEL_ID_BOOL ) {
			if ( ((Int*)kid->tree)->value )
				out << "true";
			else
				out << "false";
		}
		else if ( kid->tree->id == LEL_ID_PTR )
			out << '#' << (void*) ((Pointer*)kid->tree)->value;
		else if ( kid->tree->id == LEL_ID_STR )
			printStr( out, ((Str*)kid->tree)->value );
		else if ( kid->tree->id == LEL_ID_STREAM )
			out << '#' << (void*) ((Stream*)kid->tree)->file;
		else if ( kid->tree->tokdata != 0 && 
				stringLength( kid->tree->tokdata ) > 0 )
		{
			out.write( stringData( kid->tree->tokdata ), 
					stringLength( kid->tree->tokdata ) );
		}
	}
	else {
		/* Non-terminal. */
		child = treeChild( prg, kid->tree );
		if ( child != 0 ) {
			vm_push( (SW)kid );
			kid = child;
			while ( kid != 0 ) {
				goto rec_call;
				rec_return:
				kid = kid->next;
			}
			kid = (Kid*)vm_pop();
		}
	}

	if ( vm_ptop() != root )
		goto rec_return;
}

void printTree( ostream &out, Tree **&sp, Program *prg, Tree *tree )
{
	if ( tree == 0 )
		out << "NIL";
	else {
		Kid kid;
		kid.tree = tree;
		kid.next = 0;
		printKid( out, sp, prg, &kid, false );
	}
}


void xmlEscapeData( const char *data, long len )
{
	for ( int i = 0; i < len; i++ ) {
		if ( data[i] == '<' )
			cout << "&lt;";
		else if ( data[i] == '>' )
			cout << "&gt;";
		else if ( data[i] == '&' )
			cout << "&amp;";
		else if ( 32 <= data[i] && data[i] <= 126 )
			cout << data[i];
		else
			cout << "&#" << ((unsigned)data[i]) << ';';
	}
}

/* Might be a good idea to include this in the printXmlKid function since
 * it is recursive and can eat up stac, however it's probably not a big deal
 * since the additional stack depth is only caused for nonterminals that are
 * ignored. */
void printXmlIgnoreList( Tree **sp, Program *prg, Tree *tree, long depth )
{
	Kid *ignore = treeIgnore( prg, tree );
	while ( ignore != 0 ) {
		printXmlKid( sp, prg, ignore, true, depth );
		ignore = ignore->next;
	}
}

void printXmlKid( Tree **&sp, Program *prg, Kid *kid, bool commAttr, int depth )
{
	Kid *child;
	Tree **root = vm_ptop();
	long i, objectLength;
	LangElInfo *lelInfo = prg->rtd->lelInfo;

	long kidNum = 0;;

rec_call:

	if ( kid->tree == 0 ) {
		for ( i = 0; i < depth; i++ )
			cout << "  ";

		cout << "NIL" << endl;
	}
	else {
		/* First print the ignore tokens. */
		if ( commAttr )
			printXmlIgnoreList( sp, prg, kid->tree, depth );

		for ( i = 0; i < depth; i++ )
			cout << "  ";

		/* Open the tag. Afterwards we will either print data underneath it or
		 * we will close it off immediately. */
		cout << '<' << lelInfo[kid->tree->id].name;

		/* If this is an attribute then give the node an attribute number. */
		if ( vm_ptop() != root ) {
			objectLength = lelInfo[((Kid*)vm_top())->tree->id].objectLength;
			if ( kidNum < objectLength )
				cout << " an=\"" << kidNum << '"';
		}

		objectLength = lelInfo[kid->tree->id].objectLength;
		child = treeChild( prg, kid->tree );
		if ( (commAttr && objectLength > 0) || child != 0 ) {
			cout << '>' << endl;
			vm_push( (SW) kidNum );
			vm_push( (SW) kid );

			kidNum = 0;
			kid = kid->tree->child;

			/* Skip over attributes if not printing comments and attributes. */
			if ( ! commAttr )
				kid = child;

			while ( kid != 0 ) {
				/* FIXME: I don't think we need this check for ignore any more. */
				if ( kid->tree == 0 || !lelInfo[kid->tree->id].ignore ) {
					depth++;
					goto rec_call;
					rec_return:
					depth--;
				}
				
				kid = kid->next;
				kidNum += 1;

				/* If the parent kid is a repeat then skip this node and go
				 * right to the first child (repeated item). */
				if ( lelInfo[((Kid*)vm_top())->tree->id].repeat )
					kid = kid->tree->child;

				/* If we have a kid and the parent is a list (recursive prod of
				 * list) then go right to the first child. */
				if ( kid != 0 && lelInfo[((Kid*)vm_top())->tree->id].list )
					kid = kid->tree->child;
			}

			kid = (Kid*) vm_pop();
			kidNum = (long) vm_pop();

			for ( i = 0; i < depth; i++ )
				cout << "  ";
			cout << "</" << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else if ( kid->tree->id == LEL_ID_PTR ) {
			cout << '>' << (void*)((Pointer*)kid->tree)->value << 
					"</" << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else if ( kid->tree->id == LEL_ID_BOOL ) {
			if ( ((Int*)kid->tree)->value )
				cout << ">true</";
			else
				cout << ">false</";
			cout << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else if ( kid->tree->id == LEL_ID_INT ) {
			cout << '>' << ((Int*)kid->tree)->value << 
					"</" << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else if ( kid->tree->id == LEL_ID_STR ) {
			Head *head = (Head*) ((Str*)kid->tree)->value;

			cout << '>';
			xmlEscapeData( (char*)(head->data), head->length );
			cout << "</" << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else if ( 0 < kid->tree->id && kid->tree->id < prg->rtd->firstNonTermId &&
				kid->tree->tokdata != 0 && 
				stringLength( kid->tree->tokdata ) > 0 && 
				!lelInfo[kid->tree->id].literal )
		{
			cout << '>';
			xmlEscapeData( stringData( kid->tree->tokdata ), 
					stringLength( kid->tree->tokdata ) );
			cout << "</" << lelInfo[kid->tree->id].name << '>' << endl;
		}
		else
			cout << "/>" << endl;
	}

	if ( vm_ptop() != root )
		goto rec_return;
}

void printXmlTree( Tree **&sp, Program *prg, Tree *tree, bool commAttr )
{
	Kid kid;
	kid.tree = tree;
	kid.next = 0;
	printXmlKid( sp, prg, &kid, commAttr, 0 );
}