#
#  File: options.k3
#  Reads the command line and extracts the basename of
#  the root grammar and the various options set by the user.
#
#  Copyright 2005 Radboud University of Nijmegen
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Library General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  CVS ID: "$Id: options.k3,v 1.38 2005/10/19 15:37:10 marcs Exp $"
#
MODULE options.

DEFINES ACTION parse command line.

USES globals, io.

ARG_COUNT =  INT.

ACTION parse command line:
  parse command line arguments (1).

ACTION parse command line arguments (>INT):
  command arg (INT, TEXT),
    parse argument (TEXT),
    parse command line arguments (INT + 1);
  +.

ACTION parse argument (>TEXT):
  head (TEXT, TEXT1),
    ( [ TEXT1 -> "-" ],
	tail (TEXT, TEXT2),
	parse options (TEXT2);
      parse filename (TEXT)
    ).

ACTION parse options (>TEXT):
  [ TEXT -> "" ];
  head (TEXT, TEXT1),
    ( [ TEXT1 -> "I" ],
        tail (TEXT, TEXT2),
        set include path (TEXT2);
#     [ TEXT1 -> "b" ],
#       tail (TEXT, TEXT2),
#       set bits per word (TEXT2);
      parse multiple options (TEXT)).

ACTION parse multiple options (>TEXT):
  [ TEXT -> "" ];
  head (TEXT, TEXT1),
    parse option (TEXT1),
    tail (TEXT, TEXT2),
    parse multiple options (TEXT2).

ACTION parse option (>TEXT):
  [ TEXT -> "h" ], give help;
  [ TEXT -> "G" ], set generative grammar option;
  [ TEXT -> "l" ], set log option;
  [ TEXT -> "c" ], set counters option;
  [ TEXT -> "D" ], set directors option;
  [ TEXT -> "F" ], set factorize option;
  [ TEXT -> "N" ], set negative memo option;
  [ TEXT -> "P" ], set positive memo option;
  [ TEXT -> "T" ], set trace option;
  [ TEXT -> "p" ], set profile option;
  [ TEXT -> "d" ], set debug option;
  [ TEXT -> "w" ], reset warnings option;
  [ TEXT -> "W" ], set warnings option;
  [ TEXT -> "v" ], increase verbose option, set warnings option;
  [ TEXT -> "A" ], set no analysis option;
  [ TEXT -> "a" ], set full analysis option;
  [ TEXT -> "t" ], set time option;
  [ TEXT -> "_" ], set underscore option;
  [ TEXT -> "f" ], +;				# To be deleted
  error ("unknown option '-" + TEXT + "'"),
    give help.

FUNCTION give help:
  write ("Usage: agfl-coder [flags] grammar-name [more flags]\n"),
    write ("where flags and more flags may be any of the following:\n"),
    write ("   -h: provide this help\n"),
    write ("   -G: generate a generator instead of a parser\n"),
    write ("   -l: generate log file\n"),
    write ("   -c: enable runtime system counters\n"),
    write ("   -D: enable director set optimization\n"),
    write ("   -F: enable factorization optimization\n"),
    write ("   -N: enable negative memo optimization\n"),
    write ("   -N: enable positive memo optimization\n"),
    write ("   -T: enable runtime system tracing\n"),
    write ("   -p: enable profiling\n"),
    write ("   -d: enable debugging\n"),
    write ("   -w: ignore warnings\n"),
    write ("   -W: give warnings\n"),
    write ("   -v: increase verbosity of parser generator\n"),
    write ("   -A: do not analyse grammar, just do syntax check\n"),
    write ("   -a: do a full analysis of the grammar\n"),
    write ("   -t: provide a timed report of the parser generator internals\n"),
    write ("   -_: output labels in the generated code with underscores\n"),
    write ("   -Ipath: use path to locate machdep.h\n"),
    !.

ACTION parse filename (>TEXT):
  get basename (TEXT1),
    ( [ TEXT1 -> "" ],
  	convert to base (TEXT, TEXT2),
	set basename (TEXT2);
      error ("Multiple input files specified ('" + TEXT + "' and '" + TEXT1 + "[.gra]')"),
        !
    ).

#
#  Utilities
#  Note head and tail will be moved to the standard CDL3 library
#
FUNCTION head (>TEXT, TEXT1>):
  extract (TEXT, 0, 1, TEXT1).

FUNCTION tail (>TEXT, TEXT1>):
  length (TEXT, INT),
    extract (TEXT, 1, INT, TEXT1).

FUNCTION convert to base (>TEXT, TEXT1>):
  length (TEXT, INT),
    ( less (INT, 5), [ TEXT -> TEXT1 ];
      extract (TEXT, INT - 4, INT, TEXT2),
        ( [ TEXT2 -> ".gra" ], extract (TEXT, 0, INT - 4, TEXT1);
          [ TEXT -> TEXT1 ]
	)
    ).
