#!/usr/local/bin/perl -w  

# Conversion of commented LoPiX program (lpx)-files 
#   to .tex-files for documentation.
# (c) 5.3.2001 Wolfgang May, may@informatik.uni-freiburg.de

# Input: a LoPiX .lpx program file
# Output: TeX source, to be \input from another .tex file
#         which provides the document frame.

# program code is typeset in {verbatim}
# short comment (lead by %) are skipped (with %, comments can be
#       given in the .lpx file which are not documented.)
# long comments (enclosed by <!-- ... -->) are typeset with LaTeX
# "?- sys.echo" commands are skipped
# auxiliary queries (lead by "?-" not followed by "sys") are skipped
# (if a short comment, a sys.echo or auxiliary query should be 
#     printed, leave a blank at the begin of the line)  

$user_file = $ARGV[0] . '.lpx';
$output_file = $ARGV[0] . '.texx';

`rm $ARGV[0].texx`;

print "Creating $output_file from $user_file\n";

# Open input and output file
open user_file or die "can not open file" ;
open (OUT,">>$output_file") or die "can not open output file" ;

print OUT "% $output_file\n";
print OUT "% This file is automatically generated by lpx2tex.pl\n";
print OUT "% from $user_file. Do not change it; change $user_file\n";
print OUT "% instead and run lpx2tex.pl again.\n";

# Main difficulty: if between two long comments 
# /* ... */ and /* ... */, there is no effective program code,
# there must be no \begin{verbatim} ... \end{verbatim}. Thus,
# /* must not be translated *directly* into \begin{verbatim}
# but must be delayed until verbatim output is found - and 
# only then, the next /* must be translated into a new
# \end{verbatim}.

# Initialization: 

# beginverbatim = 1 iff the current line ends with a */, i.e.
# the current line is output and, if program code follows,
# it has to be prefixed with '\begin{verbatim}.
$beginverbatim = 0;
$endverbatim = '';

# default: next line is a program code line and has to be 
# prefixed with '\begin{verbatim}' (this is reset if a 
# begin-of-comment follows).
$pre = '\begin{verbatim}' . "\n";

while (<user_file>) {
    $_=~s|\A\?- sys.echo.*|%|g;
    $_=~s|\A\?- [^s].*|%|g;
    $_=~s|\A\?- s[^y].*|%|g;
    $_=~s|\A\?- sy[^s].*|%|g;
    # current line contains --> (at the end): kill --> and 
    # set $beginverbatim to 1
    if ($_=~s|-->||) {$beginverbatim = 1};
    # current line contains *only* <!-- (begin comment):
    # end verbatim mode (if verbatim mode is active) and
    # reset $pre (following line has not to be prefixed) 
    if (/<!--\s*\Z/)
       {$pre = '';
        if ($endverbatim eq '') {$_ = '%'}
        else {$_ = $endverbatim}};
    # current line contains <!-- and text:
    # end verbatim mode (if active) and output text and
    # reset $pre.
    if ($_=~s|<!--(.*[A-Za-z0-9])|$endverbatim$1|) {$pre = ''};
    # if current line starts with %, it is ignored.
    # Otherwise, output $pre (i.e. start verbatim mode if 
    # it is the first program line), output the current 
    # line, reset $pre and initialize $endverbatim (i.e. 
    # say that verbatim mode has to be closed when program 
    # code segment is finished).
    if (/\A%/) {} else 
       {print OUT $pre; print OUT $_;
        $pre = '';  
        $endverbatim = '\end{verbatim}' . "\n"};
    # $beginverbatim =1: program code follows. Verbatim 
    # mode has to be started before outputting next line  
    # (if no program code is output, $pre will be reset
    #  with the begin-of-comment)
    if ($beginverbatim == 1) 
       {$pre = '\begin{verbatim}' . "\n"; 
        $beginverbatim = 0;
        $endverbatim = ''};
}

# close verbatim mode (if open).
print OUT $endverbatim;
 
# Close open Files
close(user_file);
close(OUT);


