1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The lexer generator. Command-line parsing. *)
|
|
|
|
|
|
|
|
open Syntax
|
|
|
|
open Lexgen
|
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
let ml_automata = ref false
|
2003-08-11 04:56:16 -07:00
|
|
|
let source_name = ref None
|
|
|
|
let output_name = ref None
|
2002-10-28 08:46:50 -08:00
|
|
|
|
2002-11-02 14:36:46 -08:00
|
|
|
let usage = "usage: ocamlex [options] sourcefile"
|
2002-10-28 08:46:50 -08:00
|
|
|
|
2004-08-20 10:04:35 -07:00
|
|
|
let print_version_string () =
|
|
|
|
print_string "The Objective Caml lexer generator, version ";
|
|
|
|
print_string Sys.ocaml_version ; print_newline();
|
|
|
|
exit 0
|
|
|
|
|
2010-05-20 07:06:29 -07:00
|
|
|
let print_version_num () =
|
|
|
|
print_endline Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
2003-08-11 04:56:16 -07:00
|
|
|
let specs =
|
|
|
|
["-ml", Arg.Set ml_automata,
|
|
|
|
" Output code that does not use the Lexing module built-in automata interpreter";
|
2003-11-06 07:12:44 -08:00
|
|
|
"-o", Arg.String (fun x -> output_name := Some x),
|
2003-08-11 04:56:16 -07:00
|
|
|
" <file> Set output file name to <file>";
|
2003-08-29 10:33:45 -07:00
|
|
|
"-q", Arg.Set Common.quiet_mode, " Do not display informational messages";
|
2004-08-20 10:04:35 -07:00
|
|
|
"-v", Arg.Unit print_version_string, " Print version and exit";
|
|
|
|
"-version", Arg.Unit print_version_string, " Print version and exit";
|
2010-05-20 07:06:29 -07:00
|
|
|
"-vnum", Arg.Unit print_version_num, " Print version number and exit";
|
2010-01-22 04:48:24 -08:00
|
|
|
]
|
2003-08-11 04:56:16 -07:00
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
let _ =
|
|
|
|
Arg.parse
|
2003-08-11 04:56:16 -07:00
|
|
|
specs
|
|
|
|
(fun name -> source_name := Some name)
|
2002-10-28 08:46:50 -08:00
|
|
|
usage
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let main () =
|
2003-08-11 04:56:16 -07:00
|
|
|
|
|
|
|
let source_name = match !source_name with
|
2010-01-22 04:48:24 -08:00
|
|
|
| None -> Arg.usage specs usage ; exit 2
|
2003-08-11 04:56:16 -07:00
|
|
|
| Some name -> name in
|
|
|
|
let dest_name = match !output_name with
|
|
|
|
| Some name -> name
|
|
|
|
| None ->
|
|
|
|
if Filename.check_suffix source_name ".mll" then
|
|
|
|
Filename.chop_suffix source_name ".mll" ^ ".ml"
|
|
|
|
else
|
|
|
|
source_name ^ ".ml" in
|
|
|
|
|
1996-12-10 02:42:07 -08:00
|
|
|
let ic = open_in_bin source_name in
|
1996-02-25 06:45:47 -08:00
|
|
|
let oc = open_out dest_name in
|
2002-11-02 14:36:46 -08:00
|
|
|
let tr = Common.open_tracker dest_name oc in
|
1996-12-10 02:42:07 -08:00
|
|
|
let lexbuf = Lexing.from_channel ic in
|
2004-04-21 16:26:06 -07:00
|
|
|
lexbuf.Lexing.lex_curr_p <-
|
|
|
|
{Lexing.pos_fname = source_name; Lexing.pos_lnum = 1;
|
|
|
|
Lexing.pos_bol = 0; Lexing.pos_cnum = 0};
|
1998-05-26 02:56:41 -07:00
|
|
|
try
|
|
|
|
let def = Parser.lexer_definition Lexer.main lexbuf in
|
2003-08-11 04:56:16 -07:00
|
|
|
let (entries, transitions) = Lexgen.make_dfa def.entrypoints in
|
2002-10-28 08:46:50 -08:00
|
|
|
if !ml_automata then begin
|
|
|
|
Outputbis.output_lexdef
|
2002-11-02 14:36:46 -08:00
|
|
|
source_name ic oc tr
|
2002-10-28 08:46:50 -08:00
|
|
|
def.header entries transitions def.trailer
|
|
|
|
end else begin
|
|
|
|
let tables = Compact.compact_tables transitions in
|
2002-11-02 14:36:46 -08:00
|
|
|
Output.output_lexdef source_name ic oc tr
|
2002-10-28 08:46:50 -08:00
|
|
|
def.header tables entries def.trailer
|
2002-11-02 14:36:46 -08:00
|
|
|
end;
|
|
|
|
close_in ic;
|
|
|
|
close_out oc;
|
|
|
|
Common.close_tracker tr;
|
1998-05-26 02:56:41 -07:00
|
|
|
with exn ->
|
|
|
|
close_in ic;
|
|
|
|
close_out oc;
|
2002-11-02 14:36:46 -08:00
|
|
|
Common.close_tracker tr;
|
1998-05-26 02:56:41 -07:00
|
|
|
Sys.remove dest_name;
|
|
|
|
begin match exn with
|
2004-04-29 04:12:49 -07:00
|
|
|
| Cset.Bad ->
|
|
|
|
let p = Lexing.lexeme_start_p lexbuf in
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\", line %d, character %d: character set expected.\n"
|
|
|
|
p.Lexing.pos_fname p.Lexing.pos_lnum
|
|
|
|
(p.Lexing.pos_cnum - p.Lexing.pos_bol)
|
|
|
|
| Parsing.Parse_error ->
|
2004-04-21 16:26:06 -07:00
|
|
|
let p = Lexing.lexeme_start_p lexbuf in
|
1998-05-26 02:56:41 -07:00
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\", line %d, character %d: syntax error.\n"
|
2004-04-21 16:26:06 -07:00
|
|
|
p.Lexing.pos_fname p.Lexing.pos_lnum
|
|
|
|
(p.Lexing.pos_cnum - p.Lexing.pos_bol)
|
|
|
|
| Lexer.Lexical_error(msg, file, line, col) ->
|
1998-05-26 02:56:41 -07:00
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\", line %d, character %d: %s.\n"
|
2004-04-21 16:26:06 -07:00
|
|
|
file line col msg
|
2002-10-28 08:46:50 -08:00
|
|
|
| Lexgen.Memory_overflow ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\":\n Position memory overflow, too many bindings\n"
|
2010-01-22 04:48:24 -08:00
|
|
|
source_name
|
1998-05-26 02:56:41 -07:00
|
|
|
| Output.Table_overflow ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\":\ntransition table overflow, automaton is too big\n"
|
|
|
|
source_name
|
|
|
|
| _ ->
|
|
|
|
raise exn
|
|
|
|
end;
|
|
|
|
exit 3
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
let _ = (* Printexc.catch *) main (); exit 0
|