Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "char.h"
63
#include "error.h"
6 7u83 64
#include "tendra.h"
2 7u83 65
#include "xalloc.h"
66
 
67
 
68
/*
69
    WHITE SPACE CHARACTERS
70
 
71
    This variable holds all the white space characters.
72
*/
73
 
6 7u83 74
letter *white_space = NULL;
2 7u83 75
 
76
 
77
/*
78
    LEXICAL PASS REPRESENTATIONS
79
 
80
    These variables describe the various lexical passes.
81
*/
82
 
83
static character passes [2] = {
6 7u83 84
    { LAST_LETTER, NULL, NULL, NULL, NULL, NULL },
85
    { LAST_LETTER, NULL, NULL, NULL, NULL, NULL }
86
};
2 7u83 87
 
6 7u83 88
character *pre_pass = passes;
89
character *main_pass = passes + 1;
2 7u83 90
 
91
 
92
/*
93
    ALLOCATE A NEW CHARACTER
94
 
95
    This routine allocates a new character with value c.
96
*/
97
 
6 7u83 98
static character *
99
new_char(letter c)
2 7u83 100
{
6 7u83 101
    character *p;
102
    static int chars_left = 0;
103
    static character *chars_free = NULL;
104
    if (chars_left == 0) {
105
	chars_left = 100;
106
	chars_free = xmalloc_nof(character, chars_left);
2 7u83 107
    }
6 7u83 108
    p = chars_free + (--chars_left);
109
    p->ch = c;
110
    p->defn = NULL;
111
    p->cond = NULL;
112
    p->opt = NULL;
113
    p->next = NULL;
114
    return(p);
2 7u83 115
}
116
 
117
 
118
/*
119
    ADD A CHARACTER
120
 
121
    This routine adds the string s (defined using data) to the lexical
122
    pass p.
123
*/
124
 
6 7u83 125
void
126
add_char(character *p, letter *s, char **data)
2 7u83 127
{
6 7u83 128
    character *q;
129
    letter c = *s;
130
    if (p->next == NULL) {
131
	q = new_char(c);
132
	p->next = q;
2 7u83 133
    } else {
6 7u83 134
	character *r = NULL;
135
	for (q = p->next; q && (q->ch < c); q = q->opt)r = q;
136
	if (q && q->ch == c) {
2 7u83 137
	    /* already exists */
138
	} else {
6 7u83 139
	    q = new_char(c);
140
	    if (r == NULL) {
141
		q->opt = p->next;
142
		p->next = q;
2 7u83 143
	    } else {
6 7u83 144
		q->opt = r->opt;
145
		r->opt = q;
2 7u83 146
	    }
147
	}
148
    }
6 7u83 149
    if (c == LAST_LETTER) {
150
	if (q->defn) {
151
	    error(ERROR_SERIOUS,
2 7u83 152
		    "String for '%s' has already been defined to give '%s'",
6 7u83 153
		    data [0], q->defn);
2 7u83 154
	}
6 7u83 155
	q->defn = data [0];
156
	q->args = data [1];
157
	q->cond = data [2];
2 7u83 158
    } else {
6 7u83 159
	add_char(q, s + 1, data);
2 7u83 160
    }
6 7u83 161
    return;
2 7u83 162
}
163
 
164
 
165
/*
166
    ARRAY OF ALL GROUPS
167
 
168
    This array gives all the character groups.
169
*/
170
 
6 7u83 171
char_group groups [ MAX_GROUPS ];
172
int no_groups = 0;
2 7u83 173
 
174
 
175
/*
176
    CREATE A NEW GROUP
177
 
178
    This routine creates a new character group with name nm and
179
    definition s.
180
*/
181
 
6 7u83 182
void
183
make_group(char *nm, letter *s)
2 7u83 184
{
6 7u83 185
    int i, n = no_groups;
186
    for (i = 0; i < n; i++) {
187
	if (streq(nm, groups [i].name)) {
188
	    error(ERROR_SERIOUS, "Group '%s' already defined", nm);
189
	    return;
2 7u83 190
	}
191
    }
6 7u83 192
    if (n >= MAX_GROUPS) {
193
	error(ERROR_SERIOUS, "Too many groups defined (%d)", n);
194
	return;
2 7u83 195
    }
6 7u83 196
    groups [n].name = nm;
197
    groups [n].defn = s;
198
    no_groups = n + 1;
199
    return;
2 7u83 200
}
201
 
202
 
203
/*
204
    IS A LETTER IN A GROUP?
205
 
206
    This routine checks whether the letter c is in the list p.
207
*/
208
 
6 7u83 209
int
210
in_group(letter *p, letter c)
2 7u83 211
{
6 7u83 212
    letter a;
213
    if (p == NULL) return(0);
214
    while (a = *(p++), a != LAST_LETTER) {
215
	if (a == c) {
216
	    return(1);
217
	} else if (a == WHITE_LETTER) {
218
	    if (in_group(white_space, c)) return(1);
219
	} else if (a >= GROUP_LETTER) {
220
	    int n = (int)(a - GROUP_LETTER);
221
	    if (in_group(groups [n].defn, c)) return(1);
2 7u83 222
	}
223
    }
6 7u83 224
    return(0);
2 7u83 225
}
226
 
227
 
228
/*
229
    FIND AN ESCAPE SEQUENCE
230
 
231
    This routine finds the character corresponding to the escape sequence c.
232
*/
233
 
6 7u83 234
letter
235
find_escape(int c)
2 7u83 236
{
6 7u83 237
    letter a;
238
    switch (c) {
239
	case 'n': a = '\n'; break;
240
	case 't': a = '\t'; break;
241
	case 'v': a = '\v'; break;
242
	case 'f': a = '\f'; break;
243
	case 'r': a = '\r'; break;
244
	case '?': a = '?'; break;
245
	case '"': a = '"'; break;
246
	case '[': a = '['; break;
247
	case '\\': a = '\\'; break;
248
	case '\'': a = '\''; break;
249
	case 'e': a = EOF_LETTER; break;
2 7u83 250
	default : {
6 7u83 251
	    error(ERROR_SERIOUS, "Unknown escape sequence, '\\%c'",
252
		   (unsigned char)c);
253
	    a = (letter)(c & 0xff);
254
	    break;
2 7u83 255
	}
256
    }
6 7u83 257
    return(a);
2 7u83 258
}
259
 
260
 
261
/*
262
    TRANSLATE A STRING INTO A CHARACTER STRING
263
 
264
    This routine translates the string s into an array of letters.
265
*/
266
 
6 7u83 267
letter *
268
make_string(char *s)
2 7u83 269
{
6 7u83 270
    int i = 0, n = (int)strlen(s);
271
    letter *p = xmalloc_nof(letter, n + 1);
272
    while (*s) {
273
	letter a;
274
	char c = *(s++);
275
	if (c == '\\') {
276
	    c = *(s++);
277
	    a = find_escape(c);
278
	} else if (c == '[') {
279
	    int j;
280
	    size_t glen;
281
	    char *gnm = s;
282
	    while (*s && *s != ']')s++;
283
	    glen = (size_t)(s - gnm);
284
	    if (*s) {
285
		s++;
2 7u83 286
	    } else {
6 7u83 287
		error(ERROR_SERIOUS,
288
			"Unterminated character group name, '%s'", gnm);
2 7u83 289
	    }
6 7u83 290
	    for (j = 0; j < no_groups; j++) {
291
		if (strncmp(gnm, groups [j].name, glen) == 0) {
292
		    a = GROUP_LETTER + j;
293
		    break;
2 7u83 294
		}
295
	    }
6 7u83 296
	    if (j == no_groups) {
297
		if (strncmp(gnm, "white", glen) == 0) {
298
		    a = WHITE_LETTER;
2 7u83 299
		} else {
6 7u83 300
		    error(ERROR_SERIOUS, "Unknown character group, '%.*s'",
301
			   (int)glen, gnm);
302
		    a = '?';
2 7u83 303
		}
304
	    }
6 7u83 305
	    SET(a);
2 7u83 306
	} else {
6 7u83 307
	    a = (letter)(c & 0xff);
2 7u83 308
	}
6 7u83 309
	p [i] = a;
310
	i++;
2 7u83 311
    }
6 7u83 312
    p [i] = LAST_LETTER;
313
    return(p);
2 7u83 314
}
315
 
316
 
317
/*
318
    LIST OF ALL KEYWORDS
319
 
320
    This variable gives a list of all the keywords.
321
*/
322
 
6 7u83 323
keyword *keywords = NULL;
2 7u83 324
 
325
 
326
/*
327
    ADD A KEYWORD
328
 
329
    This routine adds the keyword nm with its associated data to the list
330
    of all keywords.
331
*/
332
 
6 7u83 333
void
334
add_keyword(char *nm, char **data)
2 7u83 335
{
6 7u83 336
    static int keywords_left = 0;
337
    static keyword *keywords_free = NULL;
338
    keyword *p = keywords, *q = NULL;
339
    while (p) {
340
	int c = strcmp(nm, p->name);
341
	if (c == 0) {
342
	    error(ERROR_SERIOUS, "Keyword '%s' already defined", nm);
343
	    return;
2 7u83 344
	}
6 7u83 345
	if (c < 0)break;
346
	q = p;
347
	p = p->next;
2 7u83 348
    }
6 7u83 349
    if (keywords_left == 0) {
350
	keywords_left = 100;
351
	keywords_free = xmalloc_nof(keyword, keywords_left);
2 7u83 352
    }
6 7u83 353
    p = keywords_free + (--keywords_left);
354
    p->name = nm;
355
    p->defn = data [0];
356
    p->args = data [1];
357
    p->cond = data [2];
358
    p->done = 0;
359
    if (q == NULL) {
360
	p->next = keywords;
361
	keywords = p;
2 7u83 362
    } else {
6 7u83 363
	p->next = q->next;
364
	q->next = p;
2 7u83 365
    }
6 7u83 366
    return;
2 7u83 367
}