Blame tests/yyextra.l

Packit f00812
/*
Packit f00812
 * This file is part of flex.
Packit f00812
 * 
Packit f00812
 * Redistribution and use in source and binary forms, with or without
Packit f00812
 * modification, are permitted provided that the following conditions
Packit f00812
 * are met:
Packit f00812
 * 
Packit f00812
 * 1. Redistributions of source code must retain the above copyright
Packit f00812
 *    notice, this list of conditions and the following disclaimer.
Packit f00812
 * 2. Redistributions in binary form must reproduce the above copyright
Packit f00812
 *    notice, this list of conditions and the following disclaimer in the
Packit f00812
 *    documentation and/or other materials provided with the distribution.
Packit f00812
 * 
Packit f00812
 * Neither the name of the University nor the names of its contributors
Packit f00812
 * may be used to endorse or promote products derived from this software
Packit f00812
 * without specific prior written permission.
Packit f00812
 * 
Packit f00812
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit f00812
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit f00812
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit f00812
 * PURPOSE.
Packit f00812
 */
Packit f00812
Packit f00812
%{
Packit f00812
/* A file to build "scanner.c". */
Packit f00812
/* This tests that we can use "yyextra". 
Packit f00812
   We buffer all input into a growable array, then print it.
Packit f00812
   We run diff on the input and output.
Packit f00812
*/
Packit f00812
Packit f00812
#include <stdio.h>
Packit f00812
#include <stdlib.h>
Packit f00812
#include "config.h"
Packit f00812
Packit f00812
Packit f00812
/* We'll store the entire input in this buffer, growing as necessary. */
Packit f00812
struct Buffer {
Packit f00812
    int  curr_len;
Packit f00812
    int  max_len;
Packit f00812
    int  grow_len;
Packit f00812
    char * data;
Packit f00812
};
Packit f00812
#define YY_EXTRA_TYPE struct Buffer *
Packit f00812
Packit f00812
/* Save char into junk array at next position. */
Packit f00812
static void append_char (char c,  yyscan_t  scanner );
Packit f00812
Packit f00812
%}
Packit f00812
Packit f00812
%option 8bit prefix="test"
Packit f00812
%option nounput nomain noyywrap nodefault noinput
Packit f00812
%option warn
Packit f00812
%option reentrant
Packit f00812
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
.|\r|\n  { append_char (yytext[0],yyscanner); }
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
int main(void);
Packit f00812
Packit f00812
int
Packit f00812
main ()
Packit f00812
{   
Packit f00812
    yyscan_t  scanner;
Packit f00812
    struct Buffer * buf;
Packit f00812
    int i;
Packit f00812
Packit f00812
    buf = malloc(sizeof(struct Buffer));
Packit f00812
    buf->curr_len =0;
Packit f00812
    buf->max_len = 4; 
Packit f00812
    buf->grow_len = 100;
Packit f00812
    buf->data = malloc(buf->max_len);
Packit f00812
 
Packit f00812
    testlex_init(&scanner);
Packit f00812
    testset_in( stdin, scanner);
Packit f00812
    testset_out( stdout, scanner);
Packit f00812
    testset_extra( buf, scanner );
Packit f00812
    testlex(scanner);
Packit f00812
Packit f00812
        buf = testget_extra(scanner);
Packit f00812
        for(i=0; i < buf->curr_len; i++)
Packit f00812
            fputc( buf->data[i], stdout );
Packit f00812
        free( buf->data);
Packit f00812
        free( buf);
Packit f00812
Packit f00812
    testlex_destroy(scanner);
Packit f00812
    return 0;
Packit f00812
}
Packit f00812
Packit f00812
/* Save char into junk array at next position. */
Packit f00812
static void append_char (char c,  yyscan_t  scanner )
Packit f00812
{
Packit f00812
    struct Buffer *buf, *new_buf;
Packit f00812
    buf = testget_extra(scanner);
Packit f00812
Packit f00812
    /* Grow buffer if necessary. */
Packit f00812
Packit f00812
    if( buf->curr_len >= buf->max_len )
Packit f00812
    {
Packit f00812
        new_buf = malloc(sizeof(struct Buffer));
Packit f00812
        new_buf->max_len = buf->max_len + buf->grow_len;
Packit f00812
        new_buf->grow_len = buf->grow_len;
Packit f00812
        new_buf->data = malloc(new_buf->max_len);
Packit f00812
        for( new_buf->curr_len = 0;
Packit f00812
             new_buf->curr_len < buf->curr_len;
Packit f00812
             new_buf->curr_len++ )
Packit f00812
        {
Packit f00812
            new_buf->data[ new_buf->curr_len] = buf->data [ new_buf->curr_len];
Packit f00812
        }
Packit f00812
        free( buf->data );
Packit f00812
        free( buf );
Packit f00812
        buf = new_buf;
Packit f00812
        testset_extra( buf, scanner );
Packit f00812
    }
Packit f00812
Packit f00812
    
Packit f00812
    buf->data[ buf->curr_len++ ] = c;
Packit f00812
}