#! /usr/bin/perl -w
# Converts broken oberon compler errors to the format,
# Manx's Aztec C compiler on the Amiga used.
# This allows the usage von vim's quickfix mode.
#
# Daniel Hottinger <hodaniel@student.ethz.ch>
# Published under the terms of the GNU General Public License v2 or later
#
# $Id: omake.pl,v 1.2 2002/08/20 14:45:45 hotti Exp $
# Created: Mon Mar 26 18:59:31 CEST 2001
# Last modified: Tue Aug 20 16:45:10 CEST 2002
#
# Changes:
#   2001-04-27: Now remembers the Text of the error message
#     since it is printed only once.
#     Column now calculated correct.
#     Thinkcrime support. :-)
#
# Format of input:
#   compiling  
#   pos    478  err   0 undeclared identifier
#   pos    615  err 100 incompatible operands of dyadic operator
#
# Format of output:
#   filename>line:col:E:errornr:description

$filename = shift || die "You must give a filename to parse!\n";
open(FILE, "<$filename") || die "Cannot open $filename!\n";

$line = 0;    # when tell > $pos we're already on the next line
%errmsg = (); # $msg is given only once. bug? feature?

while (<STDIN>) {
  next if ! m/\serr\s/;

  # extract data                       v-- $msg optional
  m/\s*pos\s*(\d+)\s*err\s*(\d+)\s*(.+)?/;
  my ($pos, $err, $msg) = ($1, $2, $3 || $errmsg{$2} ||
    ("Invariant violation [sponsored by Microsoft(tm)]",
     "Can you hear Niklaus Wirth scream?", 
     "Illegal error - You are not allowed to get this error. " .
     "Next time you will get a penalty for that.",
     "This error message is a sentinel and will never be printed."
    )[int rand 4]
  );
  $errmsg{$err} = $msg; 

  while ((($newpos = tell FILE) < $pos) && ! eof(FILE)) {
    $oldpos = tell FILE;
    $_ = <FILE>;
    m/(\w+)\s+su(cks|x)\s+/g && 
      print "$filename>".($line+1).":".pos().":E:23:$1 doesn't suck!\n";
    $line++;
    $oldpos = $newpos;
  }
  print "$filename>$line:".($pos-$oldpos).":E:$err:$msg\n";
}
close(FILE);

