#!/usr/bin/perl
#
# $Id: fix-flex,v 1.1 2008/07/08 10:44:59 larsl Exp $
#
# Format output generated by flex 2.5.31
#
# Usage:
#  flex -o$output $input
#  perl fix-flex $output > $tmp
#  mv $tmp $output
#
# Copyright (C) 2004-2006, David Beckett http://purl.org/net/dajobe/
# Copyright (C) 2004, University of Bristol, UK http://www.bristol.ac.uk/
#

my $line_offset=1; # #line directives always refer to the NEXT line

print <<'EOT';
#ifdef HAVE_CONFIG_H
#include <rasqal_config.h>
#endif

#ifdef WIN32
#include <win32_rasqal_config.h>
#endif

EOT
$line_offset+=8; # added 8 lines to output

my $prefix=undef;

while(<>) {
  # Remove generated yy_fatal_error declaration and definition to avoid warnings about unused/non-defined static function
  # declaration
  if(/^static void yy_fatal_error\s*\(.*\)\s*\;\s*$/) {
    $line_offset--; # skipped 1 line
    next;
  }
  # definition
  if(/^static void yy_fatal_error\s*\(.*\)\s*[^\;]\s*$/) {
    do {
      $_=<>;
      $line_offset--; # skipped 1 line
    } while(!/^}/);
    $line_offset--; # skipped 1 line
    next;
  }

  # Replace calls to yy_fatal_error("msg", yyscanner) to YY_FATAL_ERROR("msg") macro
  s/(^\s*)yy_fatal_error\s*\(\s*(\".*\")\s*,\s*yyscanner\s*\)/$1YY_FATAL_ERROR($2)/;

  # Generated ensure_buffer_stack() does not check for alloc failures
  # Allocs are followed by a memset - add alloc failure check before the memset
  if(/(^\s*)(memset\(yyg->yy_buffer_stack.*)$/) {
    $_=<<"EOT";
$1if(!yyg->yy_buffer_stack)
$1  YY_FATAL_ERROR(\"ouf of memory - lexer yy_buffer_stack\");

$1$2
EOT
    $line_offset+=3; # added 3 lines to output
  }

  # flex has %option nounistd however it does not work in 2.5.31
  # It is safe to add yet another wrapper. 
  if(m%^(\#include \<unistd.h\>)$%) {
    $_=<<"EOT";
#ifndef YY_NO_UNISTD_H
$1
#endif
EOT
    $line_offset+=2; # added 2 lines to output
  }

  # Add $prefix_cleanup() call at the end of $prefix_lex_destroy()
  # find the start of lex_destroy function definition and capture prefix
  if(/^int\s+(\S+)_lex_destroy\s*\(.*\)\s*[^\;]\s*$/) { $prefix=$1; }
  # look for lexer_free(yyscanner, yyscanner) statement within the function and place the cleanup call before it
  if($prefix) {
    if(/(^\s*)(${prefix}_free\s*\(\s*yyscanner\s*,\s*yyscanner\s*\)\s*\;)\s*$/) {
      $_=<<"EOT";
$1/* clean up leaks if any before freeing yyscanner */
$1${prefix}_cleanup(yyscanner);
$1$2
EOT
      $line_offset+=2; # added 2 lines to output
      $prefix=undef; # lex_destroy() patched
    }
  }

  # Fix .[ch] line references because we have added lines to it
  my $line = $. +$line_offset;
  s/^#line \d+ (\".*\.[ch]\")/#line $line $1/;

  print;
}
