#!/usr/bin/perl
#
# $Id$
#
# Format output generated by flex 2.5.31
#
# Usage:
#  flex -o$output $input
#  perl fix-flex $output > $tmp
#  mv $tmp $output
#
# Copyright (C) 2004-2009, David Beckett http://www.dajobe.org/
# 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)/;

  # 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;
}
