1 (***********************************************************************)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the Q Public License version 1.0. *)
11 (***********************************************************************)
15 (* The lexer generator. Command-line parsing. *)
20 let ml_automata = ref false
21 let source_name = ref None
22 let output_name = ref None
24 let usage = "usage: ocamlex [options] sourcefile"
26 let print_version_string () =
27 print_string
"The Objective Caml lexer generator, version ";
28 print_string
Sys.ocaml_version
; print_newline
();
32 ["-ml", Arg.Set
ml_automata,
33 " Output code that does not use the Lexing module built-in automata interpreter";
34 "-o", Arg.String
(fun x
-> output_name := Some x
),
35 " <file> Set output file name to <file>";
36 "-q", Arg.Set
Common.quiet_mode
, " Do not display informational messages";
37 "-v", Arg.Unit
print_version_string, " Print version and exit";
38 "-version", Arg.Unit
print_version_string, " Print version and exit";
44 (fun name
-> source_name := Some name
)
50 let source_name = match !source_name with
51 | None
-> Arg.usage specs usage ; exit
2
52 | Some name
-> name
in
53 let dest_name = match !output_name with
56 if Filename.check_suffix
source_name ".mll" then
57 Filename.chop_suffix
source_name ".mll" ^
".ml"
59 source_name ^
".ml" in
61 let ic = open_in_bin
source_name in
62 let oc = open_out
dest_name in
63 let tr = Common.open_tracker
dest_name oc in
64 let lexbuf = Lexing.from_channel
ic in
65 lexbuf.Lexing.lex_curr_p
<-
66 {Lexing.pos_fname
= source_name; Lexing.pos_lnum
= 1;
67 Lexing.pos_bol
= 0; Lexing.pos_cnum
= 0};
69 let def = Parser.lexer_definition
Lexer.main lexbuf in
70 let (entries
, transitions
) = Lexgen.make_dfa
def.entrypoints
in
71 if !ml_automata then begin
72 Outputbis.output_lexdef
74 def.header entries transitions
def.trailer
76 let tables = Compact.compact_tables transitions
in
77 Output.output_lexdef
source_name ic oc tr
78 def.header
tables entries
def.trailer
82 Common.close_tracker
tr;
86 Common.close_tracker
tr;
90 let p = Lexing.lexeme_start_p
lexbuf in
92 "File \"%s\", line %d, character %d: character set expected.\n"
93 p.Lexing.pos_fname
p.Lexing.pos_lnum
94 (p.Lexing.pos_cnum
- p.Lexing.pos_bol
)
95 | Parsing.Parse_error
->
96 let p = Lexing.lexeme_start_p
lexbuf in
98 "File \"%s\", line %d, character %d: syntax error.\n"
99 p.Lexing.pos_fname
p.Lexing.pos_lnum
100 (p.Lexing.pos_cnum
- p.Lexing.pos_bol
)
101 | Lexer.Lexical_error
(msg
, file
, line
, col
) ->
102 Printf.fprintf stderr
103 "File \"%s\", line %d, character %d: %s.\n"
105 | Lexgen.Memory_overflow
->
106 Printf.fprintf stderr
107 "File \"%s\":\n Position memory overflow, too many bindings\n"
109 | Output.Table_overflow
->
110 Printf.fprintf stderr
111 "File \"%s\":\ntransition table overflow, automaton is too big\n"
118 let _ = (* Printexc.catch *) main (); exit
0