Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 39... Line 69...
39
    INPUT FILE
69
    INPUT FILE
40
 
70
 
41
    This is the file from which the lexical routine read their input.
71
    This is the file from which the lexical routine read their input.
42
*/
72
*/
43
 
73
 
44
static FILE *lex_input ;
74
static FILE *lex_input;
45
 
75
 
46
 
76
 
47
/*
77
/*
48
    PENDING BUFFER
78
    PENDING BUFFER
49
 
79
 
Line 52... Line 82...
52
    characters pending, otherwise the pending characters are stored in
82
    characters pending, otherwise the pending characters are stored in
53
    the buffer.  The buffer may need increasing in size if the look-ahead
83
    the buffer.  The buffer may need increasing in size if the look-ahead
54
    required by the lexical analyser increases.
84
    required by the lexical analyser increases.
55
*/
85
*/
56
 
86
 
57
static int pending_buff [12] = { '?' } ;
87
static int pending_buff [12] = { '?' };
58
static int *pending = pending_buff ;
88
static int *pending = pending_buff;
59
 
89
 
60
 
90
 
61
/*
91
/*
62
    MAPPINGS AND DECLARATIONS FOR AUTOMATICALLY GENERATED SECTION
92
    MAPPINGS AND DECLARATIONS FOR AUTOMATICALLY GENERATED SECTION
63
 
93
 
64
    These macros give the mappings between the actions used in the
94
    These macros give the mappings between the actions used in the
65
    automatically generated lexical analyser and the routines defined
95
    automatically generated lexical analyser and the routines defined
66
    in this file.
96
    in this file.
67
*/
97
*/
68
 
98
 
69
static int read_char PROTO_S ( ( void ) ) ;
99
static int read_char(void);
70
static int read_comment PROTO_S ( ( void ) ) ;
100
static int read_comment(void);
71
static int read_identifier PROTO_S ( ( int, int ) ) ;
101
static int read_identifier(int, int);
72
static int read_string PROTO_S ( ( void ) ) ;
102
static int read_string(void);
73
 
103
 
74
#define get_comment( A, B )	read_comment ()
104
#define get_comment(A, B)	read_comment()
75
#define get_identifier( A )	read_identifier ( ( A ), 0 )
105
#define get_identifier(A)	read_identifier((A), 0)
76
#define get_sid_ident( A, B )	read_identifier ( ( B ), 1 )
106
#define get_sid_ident(A, B)	read_identifier((B), 1)
77
#define get_string( A )		read_string ()
107
#define get_string(A)		read_string()
78
#define unknown_token( A )	lex_unknown
108
#define unknown_token(A)	lex_unknown
79
#define unread_char( A )	*( ++pending ) = ( A )
109
#define unread_char(A)	*(++pending) = (A)
80
 
110
 
81
 
111
 
82
/*
112
/*
83
    AUTOMATICALLY GENERATED SECTION
113
    AUTOMATICALLY GENERATED SECTION
84
 
114
 
Line 93... Line 123...
93
 
123
 
94
    This routine reads the next character, either from the pending buffer
124
    This routine reads the next character, either from the pending buffer
95
    or from the input file.
125
    or from the input file.
96
*/
126
*/
97
 
127
 
98
static int read_char
128
static int
99
    PROTO_Z ()
129
read_char(void)
100
{
130
{
101
    int c ;
131
    int c;
102
    if ( pending != pending_buff ) {
132
    if (pending != pending_buff) {
103
	c = *( pending-- ) ;
133
	c = *(pending--);
104
    } else {
134
    } else {
105
	c = fgetc ( lex_input ) ;
135
	c = fgetc(lex_input);
106
	if ( c == '\n' ) crt_line_no++ ;
136
	if (c == '\n')crt_line_no++;
107
	if ( c == EOF ) return ( LEX_EOF ) ;
137
	if (c == EOF) return(LEX_EOF);
108
	c &= 0xff ;
138
	c &= 0xff;
109
    }
139
    }
110
    return ( c ) ;
140
    return(c);
111
}
141
}
112
 
142
 
113
 
143
 
114
/*
144
/*
115
    TOKEN BUFFER
145
    TOKEN BUFFER
116
 
146
 
117
    This buffer is used by read_token to hold the values of identifiers
147
    This buffer is used by read_token to hold the values of identifiers
118
    and strings.
148
    and strings.
119
*/
149
*/
120
 
150
 
121
char token_buff [2000] ;
151
char token_buff [2000];
122
static char *token_end = token_buff + sizeof ( token_buff ) ;
152
static char *token_end = token_buff + sizeof(token_buff);
123
char *first_comment = NULL ;
153
char *first_comment = NULL;
124
 
154
 
125
 
155
 
126
/*
156
/*
127
    READ AN IDENTIFIER
157
    READ AN IDENTIFIER
128
 
158
 
129
    This routine reads an identifier beginning with a, returning the
159
    This routine reads an identifier beginning with a, returning the
130
    corresponding lexical token.  Keywords are dealt with locally.
160
    corresponding lexical token.  Keywords are dealt with locally.
131
    The sid flag indicates whether a sid-style identifier is expected.
161
    The sid flag indicates whether a sid-style identifier is expected.
132
*/
162
*/
133
 
163
 
134
static int read_identifier
164
static int
135
    PROTO_N ( ( a, sid ) )
-
 
136
    PROTO_T ( int a X int sid )
165
read_identifier(int a, int sid)
137
{
166
{
138
    int c = a, cl ;
167
    int c = a, cl;
139
    int e = ( sid ? '-' : 'x' ) ;
168
    int e = (sid ? '-' : 'x');
140
    char *t = token_buff ;
169
    char *t = token_buff;
141
    do {
170
    do {
142
	*( t++ ) = ( char ) c ;
171
	*(t++) = (char)c;
143
	if ( t == token_end ) error ( ERROR_FATAL, "Buffer overflow" ) ;
172
	if (t == token_end)error(ERROR_FATAL, "Buffer overflow");
144
	c = read_char () ;
173
	c = read_char();
145
	cl = lookup_char ( c ) ;
174
	cl = lookup_char(c);
146
    } while ( is_alphanum ( cl ) || c == e ) ;
175
    } while (is_alphanum(cl) || c == e);
147
    *t = 0 ;
176
    *t = 0;
148
    unread_char ( c ) ;
177
    unread_char(c);
149
 
178
 
150
    /* Deal with keywords */
179
    /* Deal with keywords */
151
    if ( sid ) return ( lex_sid_Hidentifier ) ;
180
    if (sid) return(lex_sid_Hidentifier);
152
    t = token_buff ;
181
    t = token_buff;
153
#define MAKE_KEYWORD( A, B )\
182
#define MAKE_KEYWORD(A, B)\
154
    if ( streq ( t, ( A ) ) ) return ( B ) ;
183
    if (streq(t,(A))) return(B);
155
#include "keyword.h"
184
#include "keyword.h"
156
    return ( lex_identifier ) ;
185
    return(lex_identifier);
157
}
186
}
158
 
187
 
159
 
188
 
160
/*
189
/*
161
    READ A STRING
190
    READ A STRING
162
 
191
 
163
    This routine reads a string.  It is entered after the initial
192
    This routine reads a string.  It is entered after the initial
164
    quote has been read.
193
    quote has been read.
165
*/
194
*/
166
 
195
 
167
static int read_string
196
static int
168
    PROTO_Z ()
197
read_string(void)
169
{
198
{
170
    int c ;
199
    int c;
171
    int escaped = 0 ;
200
    int escaped = 0;
172
    char *t = token_buff ;
201
    char *t = token_buff;
173
    while ( c = read_char (), ( c != '"' || escaped ) ) {
202
    while (c = read_char(), (c != '"' || escaped)) {
174
	if ( c == '\n' || c == LEX_EOF ) {
203
	if (c == '\n' || c == LEX_EOF) {
175
	    error ( ERROR_SERIOUS, "Unexpected end of string" ) ;
204
	    error(ERROR_SERIOUS, "Unexpected end of string");
176
	    break ;
205
	    break;
177
	}
206
	}
178
	*( t++ ) = ( char ) c ;
207
	*(t++) = (char)c;
179
	if ( t == token_end ) error ( ERROR_FATAL, "Buffer overflow" ) ;
208
	if (t == token_end)error(ERROR_FATAL, "Buffer overflow");
180
	if ( escaped ) {
209
	if (escaped) {
181
	    escaped = 0 ;
210
	    escaped = 0;
182
	} else {
211
	} else {
183
	    if ( c == '\\' ) escaped = 1 ;
212
	    if (c == '\\')escaped = 1;
184
	}
213
	}
185
    }
214
    }
186
    *t = 0 ;
215
    *t = 0;
187
    return ( lex_string ) ;
216
    return(lex_string);
188
}
217
}
189
 
218
 
190
 
219
 
191
/*
220
/*
192
    READ A COMMENT
221
    READ A COMMENT
Line 194... Line 223...
194
    This routine reads a C style comment, returning the lexical token
223
    This routine reads a C style comment, returning the lexical token
195
    immediately following.  It is entered after the first two characters
224
    immediately following.  It is entered after the first two characters
196
    have been read.
225
    have been read.
197
*/
226
*/
198
 
227
 
199
static int read_comment
228
static int
200
    PROTO_Z ()
229
read_comment(void)
201
{
230
{
202
    int state = 0 ;
231
    int state = 0;
203
    char *t = token_buff ;
232
    char *t = token_buff;
204
    *( t++ ) = '/' ;
233
    *(t++) = '/';
205
    *( t++ ) = '*' ;
234
    *(t++) = '*';
206
    while ( state != 2 ) {
235
    while (state != 2) {
207
	int c = read_char () ;
236
	int c = read_char();
208
	if ( c == LEX_EOF ) {
237
	if (c == LEX_EOF) {
209
	    error ( ERROR_SERIOUS, "End of file in comment" ) ;
238
	    error(ERROR_SERIOUS, "End of file in comment");
210
	    return ( lex_eof ) ;
239
	    return(lex_eof);
211
	}
240
	}
212
	if ( c == '*' ) {
241
	if (c == '*') {
213
	    state = 1 ;
242
	    state = 1;
214
	} else if ( state == 1 && c == '/' ) {
243
	} else if (state == 1 && c == '/') {
215
	    state = 2 ;
244
	    state = 2;
216
	} else {
245
	} else {
217
	    state = 0 ;
246
	    state = 0;
218
	}
247
	}
219
	*( t++ ) = ( char ) c ;
248
	*(t++) = (char)c;
220
	if ( t == token_end ) t = token_buff + 2 ;
249
	if (t == token_end)t = token_buff + 2;
221
    }
250
    }
222
    *t = 0 ;
251
    *t = 0;
223
    if ( first_comment == NULL ) first_comment = xstrcpy ( token_buff ) ;
252
    if (first_comment == NULL)first_comment = xstrcpy(token_buff);
224
    return ( read_token () ) ;
253
    return(read_token());
225
}
254
}
226
 
255
 
227
 
256
 
228
/*
257
/*
229
    CURRENT TOKEN
258
    CURRENT TOKEN
230
 
259
 
231
    These variables are used by the parser to hold the current and former
260
    These variables are used by the parser to hold the current and former
232
    lexical tokens.
261
    lexical tokens.
233
*/
262
*/
234
 
263
 
235
int crt_lex_token ;
264
int crt_lex_token;
236
int saved_lex_token ;
265
int saved_lex_token;
237
 
266
 
238
 
267
 
239
/*
268
/*
240
    PROCESS FILE
269
    PROCESS FILE
241
 
270
 
242
    This routine processes the input file nm.
271
    This routine processes the input file nm.
243
*/
272
*/
244
 
273
 
245
void process_file
274
void
246
    PROTO_N ( ( nm ) )
-
 
247
    PROTO_T ( char *nm )
275
process_file(char *nm)
248
{
276
{
249
    crt_line_no = 1 ;
277
    crt_line_no = 1;
250
    if ( nm == NULL || streq ( nm, "-" ) ) {
278
    if (nm == NULL || streq(nm, "-")) {
251
	crt_file_name = "<stdin>" ;
279
	crt_file_name = "<stdin>";
252
	lex_input = stdin ;
280
	lex_input = stdin;
253
	nm = NULL ;
281
	nm = NULL;
254
    } else {
282
    } else {
255
	crt_file_name = nm ;
283
	crt_file_name = nm;
256
	lex_input = fopen ( nm, "r" ) ;
284
	lex_input = fopen(nm, "r");
257
	if ( lex_input == NULL ) {
285
	if (lex_input == NULL) {
258
	    error ( ERROR_SERIOUS, "Can't open input file, '%s'", nm ) ;
286
	    error(ERROR_SERIOUS, "Can't open input file, '%s'", nm);
259
	    return ;
287
	    return;
260
	}
288
	}
261
    }
289
    }
262
    ADVANCE_LEXER ;
290
    ADVANCE_LEXER;
263
    read_lex () ;
291
    read_lex();
264
    if ( nm ) fclose_v ( lex_input ) ;
292
    if (nm)fclose_v(lex_input);
265
    return ;
293
    return;
266
}
294
}