From 983f617068e6d0840618166c5176fb0ed105d9f9 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 2 Oct 1993 13:38:07 +0000 Subject: [PATCH] Added dynamic buffer growing. Added yyless() for section 3. --- flex.skl | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/flex.skl b/flex.skl index c5616eb..26407f2 100644 --- a/flex.skl +++ b/flex.skl @@ -177,6 +177,7 @@ extern FILE *yyin, *yyout; extern "C" { #endif extern void *yy_flex_alloc YY_PROTO(( int )); + extern void *yy_flex_realloc YY_PROTO(( void *ptr, int size )); extern void yy_flex_free YY_PROTO(( void * )); extern int yywrap YY_PROTO(( void )); #ifdef __cplusplus @@ -531,13 +532,32 @@ int yyFlexLexer::yy_get_next_buffer() int num_to_read = yy_current_buffer->yy_buf_size - number_to_move - 1; - if ( num_to_read > YY_READ_BUF_SIZE ) - num_to_read = YY_READ_BUF_SIZE; + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = yy_current_buffer; + + int yy_c_buf_p_offset = yy_c_buf_p - b->yy_ch_buf; + + b->yy_buf_size *= 2; + b->yy_ch_buf = (YY_CHAR *) + yy_flex_realloc( (void *) b->yy_ch_buf, + b->yy_buf_size ); - else if ( num_to_read <= 0 ) - YY_FATAL_ERROR( + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "fatal error - scanner input buffer overflow" ); + yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = yy_current_buffer->yy_buf_size - + number_to_move - 1; + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + /* Read in more data. */ YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]), yy_n_chars, num_to_read ); @@ -838,8 +858,7 @@ YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( FILE* file, int size ) /* yy_ch_buf has to be 2 characters longer than the size given because * we need to put in 2 end-of-buffer characters. */ - b->yy_ch_buf = - (YY_CHAR *) yy_flex_alloc( (unsigned) (b->yy_buf_size + 2) ); + b->yy_ch_buf = (YY_CHAR *) yy_flex_alloc( b->yy_buf_size + 2 ); if ( ! b->yy_ch_buf ) YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); @@ -864,8 +883,8 @@ void yyFlexLexer::yy_delete_buffer( YY_BUFFER_STATE b ) if ( b == yy_current_buffer ) yy_current_buffer = (YY_BUFFER_STATE) 0; - yy_flex_free( b->yy_ch_buf ); - yy_flex_free( b ); + yy_flex_free( (void *) b->yy_ch_buf ); + yy_flex_free( (void *) b ); } @@ -901,3 +920,19 @@ void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, FILE* file ) b->yy_eof_status = EOF_NOT_SEEN; } + + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + yytext[yyleng] = yy_hold_char; \ + yy_c_buf_p = yytext + n - YY_MORE_ADJ; \ + yy_hold_char = *yy_c_buf_p; \ + *yy_c_buf_p = '\0'; \ + yyleng = n; \ + } \ + while ( 0 ) -- 2.40.0