]> granicus.if.org Git - flex/commitdiff
Added dynamic buffer growing.
authorVern Paxson <vern@ee.lbl.gov>
Sat, 2 Oct 1993 13:38:07 +0000 (13:38 +0000)
committerVern Paxson <vern@ee.lbl.gov>
Sat, 2 Oct 1993 13:38:07 +0000 (13:38 +0000)
Added yyless() for section 3.

flex.skl

index c5616eb723c607667e06dd9182c35864cc0e427a..26407f20e689e0294f8dd0b6005a64ff50c48580 100644 (file)
--- 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 )