program bnf2pas  (input, output);  {-*-Mode: fundamental-mode } 
uses base, parse;

const
  family_name = 'xxx_';

var
  ph                  : parse_handle_t;
  a, dsn_bnf, dsn_unit, dsn_main : path_name_t;
  ac, i, offs         : integer;
  check_set           : check_set_t; 
  translate, err      : boolean;

procedure write_help (s: err_str_t);
begin
 writeln (StdErr, 'Usage: ' + ParamStr (0) + ' [ -ckasrknh123 ]  [bnf_file [dst_unit [dest_main]]]]');
 writeln (StdErr, '       c = write_calls,      k = write_keywords,        a = write_actions');
 writeln (StdErr, '       s = write_structure,  r = write_reference_error, v = write_convergence');
 writeln (StdErr, '       n = no translate,     h = help');
 writeln (StdErr, ' 0,1,2,3 = Pascal indentation layout');
 if s<>'' then writeln (StdErr, s);
 halt (1)
end;

begin 
  check_set   := [];         {default no diagnostics output}
  translate   := true;       {default is pascal translation}
  err         := false;      {no errors up to now}
  offs        := -1;         {indentation values >= 0 overwrite default value given from bnf_file}
  dsn_bnf     := '';         {BNF source file}
  dsn_unit    := '';         {destination file for pascal unit/module}
  dsn_main    := '';         {optional main program} 
  for ac:=1 to ParamCount do
    begin
      a:= ParamStr (ac);
      if (length (a) > 1) and (a [1] = '-')
        then for i:=2 to length (a) do case a [i] of
                'c' : check_set:= check_set + [write_calls];
                'k' : check_set:= check_set + [write_keywords];
                'a' : check_set:= check_set + [write_actions_s, write_actions_c];
                's' : check_set:= check_set + [write_structure];
                'r' : check_set:= check_set + [write_reference_error];
                'v' : check_set:= check_set + [write_convergence];
                'h' : write_help ('');
                'n' : translate:= false;                   {no translate, default is translate}
           '0'..'3' : offs:= ord (a [i])-ord ('0');        {indentation}
           otherwise write_help ('Illegal switch: ' + a [i]) end
        else if         dsn_bnf  = '' then dsn_bnf  :=a
                else if dsn_unit = '' then dsn_unit :=a
                else if dsn_main = '' then dsn_main :=a
                else write_help ('Too many parameters')
    end;
{ if dsn_bnf=''                       then write_help ('Missing bnf_file'); } {take Standard Input}
  if (check_set=[]) and not translate then write_help ('Do nothing?');
  read_bnf          (ph, dsn_bnf, check_set, err);
  if err then abort ('Error detected in read_bnf');
  if translate then translate_bnf  (ph, family_name, dsn_unit, dsn_main, offs);
  close_parser   (ph)
end.
