Debian Codegraph @

src/utils/cst_tokenstream.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 1999 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: July 1999 */
35 /*************************************************************************/
36 /* */
37 /* Tokenizer for strings and files */
38 /* */
39 /*************************************************************************/
40 #include "cst_tokenstream.h"
41
42 const cst_string * const cst_ts_default_whitespacesymbols = " \t\n\r";
43 const cst_string * const cst_ts_default_singlecharsymbols = "(){}[]";
44 const cst_string * const cst_ts_default_prepunctuationsymbols = "\"'`({[";
45 const cst_string * const cst_ts_default_postpunctuationsymbols = "\"'`.,:;!?(){}[]";
46
47 #define TS_BUFFER_SIZE 256
48
49 static cst_string ts_getc(cst_tokenstream *ts);
50 static cst_string internal_ts_getc(cst_tokenstream *ts);
51
52 static void set_charclass_table(cst_tokenstream *ts)
53 {
54 int i;
55 memset(ts->charclass,0,256); /* zero everything */
56
57 for (i=0; ts->p_whitespacesymbols[i]; i++)
58 ts->charclass[(unsigned char)ts->p_whitespacesymbols[i]] |= TS_CHARCLASS_WHITESPACE;
59 for (i=0; ts->p_singlecharsymbols[i]; i++)
60 ts->charclass[(unsigned char)ts->p_singlecharsymbols[i]] |= TS_CHARCLASS_SINGLECHAR;
61 for (i=0; ts->p_prepunctuationsymbols[i]; i++)
62 ts->charclass[(unsigned char)ts->p_prepunctuationsymbols[i]] |= TS_CHARCLASS_PREPUNCT;
63 for (i=0; ts->p_postpunctuationsymbols[i]; i++)
64 ts->charclass[(unsigned char)ts->p_postpunctuationsymbols[i]]|=TS_CHARCLASS_POSTPUNCT;
65 return;
66 }
67
68 void set_charclasses(cst_tokenstream *ts,
69 const cst_string *whitespace,
70 const cst_string *singlecharsymbols,
71 const cst_string *prepunctuation,
72 const cst_string *postpunctuation)
73 {
74 ts->p_whitespacesymbols =
75 (whitespace ? whitespace : cst_ts_default_whitespacesymbols);
76 ts->p_singlecharsymbols =
77 (singlecharsymbols ? singlecharsymbols : cst_ts_default_singlecharsymbols);
78 ts->p_prepunctuationsymbols =
79 (prepunctuation ? prepunctuation : cst_ts_default_prepunctuationsymbols);
80 ts->p_postpunctuationsymbols =
81 (postpunctuation ? postpunctuation : cst_ts_default_postpunctuationsymbols);
82
83 set_charclass_table(ts);
84 return;
85 }
86
87 static void extend_buffer(cst_string **buffer,int *buffer_max)
88 {
89 int new_max;
90 cst_string *new_buffer;
91
92 new_max = (*buffer_max)+(*buffer_max)/5;
93 new_buffer = cst_alloc(cst_string,new_max);
94 memmove(new_buffer,*buffer,*buffer_max);
95 cst_free(*buffer);
96 *buffer = new_buffer;
97 *buffer_max = new_max;
98 }
99
100 static cst_tokenstream *new_tokenstream(const cst_string *whitespace,
101 const cst_string *singlechars,
102 const cst_string *prepunct,
103 const cst_string *postpunct)
104 { /* Constructor function */
105 cst_tokenstream *ts = cst_alloc(cst_tokenstream,1);
106 ts->fd = NULL;
107 ts->file_pos = 0;
108 ts->line_number = 0;
109 ts->eof_flag = 0;
110 ts->string_buffer = NULL;
111 ts->token_pos = 0;
112 ts->whitespace = cst_alloc(cst_string,TS_BUFFER_SIZE);
113 ts->ws_max = TS_BUFFER_SIZE;
114 if (prepunct && prepunct[0])
115 {
116 ts->prepunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE);
117 ts->prep_max = TS_BUFFER_SIZE;
118 }
119 ts->token = cst_alloc(cst_string,TS_BUFFER_SIZE);
120 ts->token_max = TS_BUFFER_SIZE;
121 if (postpunct && postpunct[0])
122 {
123 ts->postpunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE);
124 ts->postp_max = TS_BUFFER_SIZE;
125 }
126
127 set_charclasses(ts,whitespace,singlechars,prepunct,postpunct);
128 ts->current_char = 0;
129
130 return ts;
131 }
132
133 void delete_tokenstream(cst_tokenstream *ts)
134 {
135 cst_free(ts->whitespace);
136 cst_free(ts->token);
137 if (ts->tags) delete_features(ts->tags);
138 if (ts->prepunctuation) cst_free(ts->prepunctuation);
139 if (ts->postpunctuation) cst_free(ts->postpunctuation);
140 cst_free(ts);
141 }
142
143 cst_tokenstream *ts_open(const char *filename,
144 const cst_string *whitespace,
145 const cst_string *singlechars,
146 const cst_string *prepunct,
147 const cst_string *postpunct)
148 {
149 cst_tokenstream *ts = new_tokenstream(whitespace,
150 singlechars,
151 prepunct,
152 postpunct);
153
154 #ifndef UNDER_CE
155 if (cst_streq("-",filename))
156 ts->fd = stdin;
157 else
158 #endif
159 ts->fd = cst_fopen(filename,CST_OPEN_READ|CST_OPEN_BINARY);
160 ts_getc(ts);
161
162 if (ts->fd == NULL)
163 {
164 delete_tokenstream(ts);
165 return NULL;
166 }
167 else
168 return ts;
169 }
170
171 cst_tokenstream *ts_open_string(const cst_string *string,
172 const cst_string *whitespace,
173 const cst_string *singlechars,
174 const cst_string *prepunct,
175 const cst_string *postpunct)
176 {
177 cst_tokenstream *ts = new_tokenstream(whitespace,
178 singlechars,
179 prepunct,
180 postpunct);
181
182 ts->string_buffer = cst_strdup(string);
183 ts_getc(ts);
184
185 return ts;
186 }
187
188 cst_tokenstream *ts_open_generic(const char *filename,
189 const cst_string *whitespacesymbols,
190 const cst_string *singlecharsymbols,
191 const cst_string *prepunctsymbols,
192 const cst_string *postpunctsymbols,
193 void *streamtype_data,
194 int (*open)(cst_tokenstream *ts,
195 const char *filename),
196 void (*close)(cst_tokenstream *ts),
197 int (*eof)(cst_tokenstream *ts),
198 int (*seek)(cst_tokenstream *ts, int pos),
199 int (*tell)(cst_tokenstream *ts),
200 int (*size)(cst_tokenstream *ts),
201 int (*getc)(cst_tokenstream *ts))
202 { /* Its a generic token stream where user has specified the low level */
203 /* file/stream access functions */
204 cst_tokenstream *ts = new_tokenstream(whitespacesymbols,
205 singlecharsymbols,
206 prepunctsymbols,
207 postpunctsymbols);
208
209 ts->streamtype_data = streamtype_data;
210 ts->open = open;
211 ts->close = close;
212 ts->eof = eof;
213 ts->seek = seek;
214 ts->tell = tell;
215 ts->size = size;
216 ts->getc = getc;
217
218 if ((ts->open)(ts,filename) != 0)
219 {
220 (ts->getc)(ts);
221 return ts;
222 }
223 else
224 {
225 delete_tokenstream(ts);
226 return NULL;
227 }
228 }
229
230 void ts_close(cst_tokenstream *ts)
231 {
232 if (ts->fd != NULL)
233 {
234 #ifndef UNDER_CE
235 if (ts->fd != stdin)
236 #endif
237 cst_fclose(ts->fd);
238 ts->fd = NULL; /* just in case close gets called twice */
239 }
240 if (ts->string_buffer != NULL)
241 {
242 cst_free(ts->string_buffer);
243 ts->string_buffer = NULL;
244 }
245 if (ts->open)
246 (ts->close)(ts);
247 delete_tokenstream(ts);
248 }
249
250 static void get_token_sub_part(cst_tokenstream *ts,
251 int charclass,
252 cst_string **buffer,
253 int *buffer_max)
254 {
255 int p;
256
257 for (p=0; ((!ts_eof(ts)) &&
258 (ts_charclass(ts->current_char,charclass,ts)) &&
259 (!ts_charclass(ts->current_char,
260 TS_CHARCLASS_SINGLECHAR,ts))); p++)
261 {
262 if (p+1 >= *buffer_max) extend_buffer(buffer,buffer_max);
263 (*buffer)[p] = ts->current_char;
264 ts_getc(ts);
265 }
266 (*buffer)[p] = '\0';
267 }
268
269 #ifdef _WIN32
270 __inline int ts_utf8_sequence_length(char c0)
271 #else
272 int ts_utf8_sequence_length(char c0)
273 #endif
274 {
275 /* Get the expected length of UTF8 sequence given its most */
276 /* significant byte */
277 return (( 0xE5000000 >> (( c0 >> 3 ) & 0x1E )) & 3 ) + 1;
278 }
279
280 /* Can't afford dynamically generate this char class so have separated func */
281 /* so do the core token part -- this goes while not givenlass (while the */
282 /* above function oes while is givenclass */
283 static void get_token_sub_part_2(cst_tokenstream *ts,
284 int endclass1,
285 cst_string **buffer,
286 int *buffer_max)
287 {
288 int p;
289
290 for (p=0; ((!ts_eof(ts)) &&
291 (!ts_charclass(ts->current_char,endclass1,ts)) &&
292 (!ts_charclass(ts->current_char,
293 TS_CHARCLASS_SINGLECHAR,ts)));
294 p++)
295 {
296 if (p+1 >= *buffer_max) extend_buffer(buffer,buffer_max);
297 (*buffer)[p] = ts->current_char;
298 /* If someone sets tags we end the token */
299 /* This can't happen in standard tokenstreams, but can in user */
300 /* defined ones */
301 if (ts->tags) break;
302
303 /* In the special utf8 char by char mode we end at end of a utf8 char */
304 if ((ts->utf8_explode_mode) &&
305 (p == ts_utf8_sequence_length((*buffer)[0])))
306 break;
307
308 ts_getc(ts);
309 }
310 (*buffer)[p] = '\0';
311 }
312
313 static void get_token_postpunctuation(cst_tokenstream *ts)
314 {
315 int p,t;
316
317 t = cst_strlen(ts->token);
318 for (p=t;
319 (p > 0) &&
320 ((ts->token[p] == '\0') ||
321 (ts_charclass(ts->token[p],TS_CHARCLASS_POSTPUNCT,ts)));
322 p--);
323
324 if (t != p)
325 {
326 if (t-p >= ts->postp_max)
327 extend_buffer(&ts->postpunctuation,&ts->postp_max);
328 /* Copy postpunctuation from token */
329 memmove(ts->postpunctuation,&ts->token[p+1],(t-p));
330 /* truncate token at postpunctuation */
331 ts->token[p+1] = '\0';
332 }
333 }
334
335 int ts_eof(cst_tokenstream *ts)
336 {
337 if (ts->eof_flag)
338 return TRUE;
339 else
340 return FALSE;
341 }
342
343 int ts_set_stream_pos(cst_tokenstream *ts, int pos)
344 {
345 /* Note this doesn't preserve line_pos */
346 int new_pos, l;
347
348 if (ts->fd)
349 {
350 new_pos = (int)cst_fseek(ts->fd,(long)pos,CST_SEEK_ABSOLUTE);
351 if (new_pos == pos)
352 ts->eof_flag = FALSE;
353 }
354 else if (ts->string_buffer)
355 {
356 l = cst_strlen(ts->string_buffer);
357 if (pos > l)
358 new_pos = l;
359 else if (pos < 0)
360 new_pos = 0;
361 else
362 new_pos = pos;
363 ts->eof_flag = FALSE;
364 }
365 else if (ts->open)
366 {
367 new_pos = (ts->seek)(ts,pos);
368 if (new_pos == pos)
369 ts->eof_flag = FALSE;
370 }
371 else
372 new_pos = pos; /* not sure it can get here */
373 ts->file_pos = new_pos;
374 ts->current_char = ' '; /* To be safe (but this is wrong) */
375
376 return ts->file_pos;
377 }
378
379 int ts_get_stream_pos(cst_tokenstream *ts)
380 {
381 if (ts->open)
382 return (ts->tell)(ts);
383 else
384 return ts->file_pos;
385 }
386
387 int ts_get_stream_size(cst_tokenstream *ts)
388 {
389 int current_pos, end_pos;
390 if (ts->fd)
391 {
392 current_pos = ts->file_pos;
393 end_pos = (int)cst_fseek(ts->fd,(long)0,CST_SEEK_ENDREL);
394 cst_fseek(ts->fd,(long)current_pos,CST_SEEK_ABSOLUTE);
395 return end_pos;
396 } else if (ts->string_buffer)
397 return cst_strlen(ts->string_buffer);
398 else if (ts->open)
399 return (ts->size)(ts);
400 else
401 return 0;
402 }
403
404 cst_string private_ts_getc(cst_tokenstream *ts)
405 {
406 return internal_ts_getc(ts);
407 }
408
409 static cst_string ts_getc(cst_tokenstream *ts)
410 {
411 if (ts->open)
412 ts->current_char = (ts->getc)(ts);
413 else
414 ts->current_char = internal_ts_getc(ts);
415 return ts->current_char;
416 }
417
418 static cst_string internal_ts_getc(cst_tokenstream *ts)
419 {
420 if (ts->fd)
421 {
422 ts->current_char = cst_fgetc(ts->fd);
423 if (ts->current_char == -1)
424 ts->eof_flag = TRUE;
425 }
426 else if (ts->string_buffer)
427 {
428 if (ts->string_buffer[ts->file_pos] == '\0')
429 {
430 ts->eof_flag = TRUE;
431 ts->current_char = '\0';
432 }
433 else
434 ts->current_char = ts->string_buffer[ts->file_pos];
435 }
436
437 if (!ts_eof(ts))
438 ts->file_pos++;
439 if (ts->current_char == '\n')
440 ts->line_number++;
441 return ts->current_char;
442 }
443
444 const cst_string *ts_get_quoted_token(cst_tokenstream *ts,
445 char quote,
446 char escape)
447 {
448 /* for reading the next quoted token that starts with quote and
449 ends with quote, quote may appear only if preceded by escape */
450 int p;
451
452 /* Hmm can't change quotes within a ts */
453 ts->charclass[(unsigned int)quote] |= TS_CHARCLASS_QUOTE;
454 ts->charclass[(unsigned int)escape] |= TS_CHARCLASS_QUOTE;
455
456 /* skipping whitespace */
457 get_token_sub_part(ts,TS_CHARCLASS_WHITESPACE,
458 &ts->whitespace,
459 &ts->ws_max);
460 ts->token_pos = ts->file_pos - 1;
461
462 if (ts->current_char == quote)
463 { /* go until quote */
464 ts_getc(ts);
465 for (p=0; ((!ts_eof(ts)) &&
466 (ts->current_char != quote));
467 p++)
468 {
469 if (p >= ts->token_max)
470 extend_buffer(&ts->token,&ts->token_max);
471 ts->token[p] = ts->current_char;
472 ts_getc(ts);
473 if (ts->current_char == escape)
474 {
475 ts_get(ts);
476 if (p >= ts->token_max)
477 extend_buffer(&ts->token,&ts->token_max);
478 ts->token[p] = ts->current_char;
479 ts_get(ts);
480 }
481 }
482 ts->token[p] = '\0';
483 ts_getc(ts);
484 }
485 else /* its not quoted, like to be careful dont you */
486 { /* treat is as standard token */
487 /* Get prepunctuation */
488 extend_buffer(&ts->prepunctuation,&ts->prep_max);
489 get_token_sub_part(ts,TS_CHARCLASS_PREPUNCT,
490 &ts->prepunctuation,
491 &ts->prep_max);
492 /* Get the symbol itself */
493 if (ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts))
494 {
495 if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max);
496 ts->token[0] = ts->current_char;
497 ts->token[1] = '\0';
498 ts_getc(ts);
499 }
500 else
501 get_token_sub_part_2(ts,
502 TS_CHARCLASS_WHITESPACE, /* end class1 */
503 &ts->token,
504 &ts->token_max);
505 /* This'll have token *plus* post punctuation in ts->token */
506 /* Get postpunctuation */
507 get_token_postpunctuation(ts);
508 }
509
510 return ts->token;
511 }
512
513 const cst_string *ts_get(cst_tokenstream *ts)
514 {
515 /* Get next token */
516
517 if (ts->tags)
518 { /* Someone didn't delete them before -- so we delete them now */
519 delete_features(ts->tags);
520 ts->tags = NULL;
521 }
522
523 /* Skip whitespace */
524 get_token_sub_part(ts,
525 TS_CHARCLASS_WHITESPACE,
526 &ts->whitespace,
527 &ts->ws_max);
528
529 /* quoted strings currently ignored */
530 ts->token_pos = ts->file_pos - 1;
531
532 /* Get prepunctuation */
533 if (!ts_eof(ts) &&
534 ts_charclass(ts->current_char,TS_CHARCLASS_PREPUNCT,ts))
535 get_token_sub_part(ts,
536 TS_CHARCLASS_PREPUNCT,
537 &ts->prepunctuation,
538 &ts->prep_max);
539 else if (ts->prepunctuation)
540 ts->prepunctuation[0] = '\0';
541 /* Get the symbol itself */
542 if (!ts_eof(ts) &&
543 ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts))
544 {
545 if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max);
546 ts->token[0] = ts->current_char;
547 ts->token[1] = '\0';
548 ts_getc(ts);
549 }
550 else
551 get_token_sub_part_2(ts,
552 TS_CHARCLASS_WHITESPACE, /* end class1 */
553 &ts->token,
554 &ts->token_max);
555 /* This'll have token *plus* post punctuation in ts->token */
556 /* Get postpunctuation */
557 if (ts->postpunctuation)
558 ts->postpunctuation[0] = '\0';
559 if (ts->p_postpunctuationsymbols[0])
560 get_token_postpunctuation(ts);
561
562 return ts->token;
563 }
564
565 int ts_read(void *buff, int size, int num, cst_tokenstream *ts)
566 {
567 /* people should complain about the speed here */
568 /* people will complain about EOF as end of file */
569 int i,j,p;
570 cst_string *cbuff;
571
572 cbuff = (cst_string *)buff;
573
574 for (p=i=0; i < num; i++)
575 for (j=0; j < size; j++,p++)
576 cbuff[p] = ts_getc(ts);
577
578 return i;
579 }