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$ *)
|
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
(* Output the DFA tables and its entry points *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
open Printf
|
1995-05-04 03:15:53 -07:00
|
|
|
open Syntax
|
1996-02-25 06:45:47 -08:00
|
|
|
open Lexgen
|
|
|
|
open Compact
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
(* To copy the ML code fragments *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-12-10 02:42:07 -08:00
|
|
|
let copy_buffer = String.create 1024
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let copy_chars_unix ic oc start stop =
|
1996-12-10 02:42:07 -08:00
|
|
|
let n = ref (stop - start) in
|
|
|
|
while !n > 0 do
|
|
|
|
let m = input ic copy_buffer 0 (min !n 1024) in
|
|
|
|
output oc copy_buffer 0 m;
|
|
|
|
n := !n - m
|
|
|
|
done
|
1996-02-25 06:45:47 -08:00
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let copy_chars_win32 ic oc start stop =
|
1996-12-10 06:45:58 -08:00
|
|
|
for i = start to stop - 1 do
|
|
|
|
let c = input_char ic in
|
|
|
|
if c <> '\r' then output_char oc c
|
|
|
|
done
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let copy_chars =
|
1996-12-10 06:45:58 -08:00
|
|
|
match Sys.os_type with
|
1998-04-23 01:24:50 -07:00
|
|
|
"Win32" -> copy_chars_win32
|
|
|
|
| _ -> copy_chars_unix
|
|
|
|
|
|
|
|
let copy_chunk sourcefile ic oc loc =
|
|
|
|
if loc.start_pos < loc.end_pos then begin
|
|
|
|
fprintf oc "# %d \"%s\"\n" loc.start_line sourcefile;
|
|
|
|
for i = 1 to loc.start_col do output_char oc ' ' done;
|
|
|
|
seek_in ic loc.start_pos;
|
|
|
|
copy_chars ic oc loc.start_pos loc.end_pos
|
|
|
|
end
|
1996-12-10 06:45:58 -08:00
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
(* To output an array of short ints, encoded as a string *)
|
|
|
|
|
|
|
|
let output_byte oc b =
|
|
|
|
output_char oc '\\';
|
|
|
|
output_char oc (Char.chr(48 + b / 100));
|
|
|
|
output_char oc (Char.chr(48 + (b / 10) mod 10));
|
|
|
|
output_char oc (Char.chr(48 + b mod 10))
|
|
|
|
|
|
|
|
let output_array oc v =
|
|
|
|
output_string oc " \"";
|
|
|
|
for i = 0 to Array.length v - 1 do
|
|
|
|
output_byte oc (v.(i) land 0xFF);
|
|
|
|
output_byte oc ((v.(i) asr 8) land 0xFF);
|
|
|
|
if i land 7 = 7 then output_string oc "\\\n "
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
1996-02-25 06:45:47 -08:00
|
|
|
output_string oc "\""
|
|
|
|
|
|
|
|
(* Output the tables *)
|
|
|
|
|
|
|
|
let output_tables oc tbl =
|
|
|
|
output_string oc "let lex_tables = {\n";
|
|
|
|
fprintf oc " Lexing.lex_base = \n%a;\n" output_array tbl.tbl_base;
|
|
|
|
fprintf oc " Lexing.lex_backtrk = \n%a;\n" output_array tbl.tbl_backtrk;
|
|
|
|
fprintf oc " Lexing.lex_default = \n%a;\n" output_array tbl.tbl_default;
|
|
|
|
fprintf oc " Lexing.lex_trans = \n%a;\n" output_array tbl.tbl_trans;
|
|
|
|
fprintf oc " Lexing.lex_check = \n%a\n" output_array tbl.tbl_check;
|
|
|
|
output_string oc "}\n\n"
|
|
|
|
|
|
|
|
(* Output the entries *)
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let output_entry sourcefile ic oc e =
|
1999-10-15 11:37:55 -07:00
|
|
|
fprintf oc "%s lexbuf = __ocaml_lex_%s_rec lexbuf %d\n"
|
1997-03-10 05:57:06 -08:00
|
|
|
e.auto_name e.auto_name e.auto_initial_state;
|
1999-10-15 11:37:55 -07:00
|
|
|
fprintf oc "and __ocaml_lex_%s_rec lexbuf state =\n" e.auto_name;
|
1997-03-10 05:57:06 -08:00
|
|
|
fprintf oc " match Lexing.engine lex_tables state lexbuf with\n ";
|
1996-02-25 06:45:47 -08:00
|
|
|
let first = ref true in
|
|
|
|
List.iter
|
|
|
|
(fun (num, loc) ->
|
|
|
|
if !first then first := false else fprintf oc " | ";
|
1998-04-23 01:24:50 -07:00
|
|
|
fprintf oc "%d -> (\n" num;
|
|
|
|
copy_chunk sourcefile ic oc loc;
|
1996-02-25 06:45:47 -08:00
|
|
|
fprintf oc ")\n")
|
|
|
|
e.auto_actions;
|
1999-10-15 11:37:55 -07:00
|
|
|
fprintf oc " | n -> lexbuf.Lexing.refill_buff lexbuf; \
|
|
|
|
__ocaml_lex_%s_rec lexbuf n\n\n"
|
1996-05-28 05:42:15 -07:00
|
|
|
e.auto_name
|
1996-02-25 06:45:47 -08:00
|
|
|
|
|
|
|
(* Main output function *)
|
|
|
|
|
1998-05-26 02:56:41 -07:00
|
|
|
exception Table_overflow
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let output_lexdef sourcefile ic oc header tables entry_points trailer =
|
1996-02-25 06:45:47 -08:00
|
|
|
Printf.printf "%d states, %d transitions, table size %d bytes\n"
|
|
|
|
(Array.length tables.tbl_base)
|
|
|
|
(Array.length tables.tbl_trans)
|
|
|
|
(2 * (Array.length tables.tbl_base + Array.length tables.tbl_backtrk +
|
|
|
|
Array.length tables.tbl_default + Array.length tables.tbl_trans +
|
|
|
|
Array.length tables.tbl_check));
|
|
|
|
flush stdout;
|
1998-05-26 02:56:41 -07:00
|
|
|
if Array.length tables.tbl_trans > 0x8000 then raise Table_overflow;
|
1998-04-23 01:24:50 -07:00
|
|
|
copy_chunk sourcefile ic oc header;
|
1996-02-25 06:45:47 -08:00
|
|
|
output_tables oc tables;
|
|
|
|
begin match entry_points with
|
|
|
|
[] -> ()
|
|
|
|
| entry1 :: entries ->
|
1998-04-23 01:24:50 -07:00
|
|
|
output_string oc "let rec "; output_entry sourcefile ic oc entry1;
|
1996-02-25 06:45:47 -08:00
|
|
|
List.iter
|
1998-04-23 01:24:50 -07:00
|
|
|
(fun e -> output_string oc "and "; output_entry sourcefile ic oc e)
|
1999-10-15 11:37:55 -07:00
|
|
|
entries;
|
|
|
|
output_string oc ";;\n\n";
|
1996-02-25 06:45:47 -08:00
|
|
|
end;
|
1998-04-23 01:24:50 -07:00
|
|
|
copy_chunk sourcefile ic oc trailer
|