#!/usr/bin/perl
#
# Format output generated by flex 2.5.31
#
# Usage:
#  flex -o$output $input
#  perl fix-flex $output > $tmp
#  mv $tmp $output
#
# (C) Copyright 2004-2010 Dave Beckett http://www.dajobe.org/
# (C) Copyright 2004 University of Bristol
#

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

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

#ifdef WIN32
#include <win32_raptor_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/;

  # Fix signed / unsigned comparison gcc 4.x warning:
  #   int n : in the macro YY_INPUT definition
  #   (size_t)num_to_read : which is silly since num_to_read is an int!
  s/yyg->yy_n_chars, \(size_t\) num_to_read \)/yyg->yy_n_chars, num_to_read \)/;

  print;
}
