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 *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The run-time library for lexers generated by camllex *)
|
|
|
|
|
|
|
|
type lexbuf =
|
|
|
|
{ refill_buff : lexbuf -> unit;
|
1995-08-08 06:37:34 -07:00
|
|
|
mutable lex_buffer : string;
|
2002-03-18 08:16:31 -08:00
|
|
|
mutable lex_buffer_len : int;
|
1995-05-04 03:15:53 -07:00
|
|
|
mutable lex_abs_pos : int;
|
|
|
|
mutable lex_start_pos : int;
|
|
|
|
mutable lex_curr_pos : int;
|
1996-05-28 05:43:22 -07:00
|
|
|
mutable lex_last_pos : int;
|
1997-04-15 12:19:16 -07:00
|
|
|
mutable lex_last_action : int;
|
|
|
|
mutable lex_eof_reached : bool }
|
1996-02-25 06:45:47 -08:00
|
|
|
|
|
|
|
type lex_tables =
|
|
|
|
{ lex_base: string;
|
|
|
|
lex_backtrk: string;
|
|
|
|
lex_default: string;
|
|
|
|
lex_trans: string;
|
|
|
|
lex_check: string }
|
|
|
|
|
|
|
|
external engine: lex_tables -> int -> lexbuf -> int = "lex_engine"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-03-05 06:38:24 -08:00
|
|
|
let lex_refill read_fun aux_buffer lexbuf =
|
1995-05-04 03:15:53 -07:00
|
|
|
let read =
|
1997-03-05 06:38:24 -08:00
|
|
|
read_fun aux_buffer (String.length aux_buffer) in
|
1995-05-04 03:15:53 -07:00
|
|
|
let n =
|
|
|
|
if read > 0
|
|
|
|
then read
|
1997-04-15 12:19:16 -07:00
|
|
|
else (lexbuf.lex_eof_reached <- true; 0) in
|
2002-03-11 00:45:10 -08:00
|
|
|
(* Current state of the buffer:
|
|
|
|
<-------|---------------------|----------->
|
|
|
|
| junk | valid data | junk |
|
|
|
|
^ ^ ^ ^
|
|
|
|
0 start_pos buffer_end String.length buffer
|
|
|
|
*)
|
2002-03-18 08:16:31 -08:00
|
|
|
if lexbuf.lex_buffer_len + n > String.length lexbuf.lex_buffer then begin
|
2002-03-11 00:45:10 -08:00
|
|
|
(* There is not enough space at the end of the buffer *)
|
2002-03-18 08:16:31 -08:00
|
|
|
if lexbuf.lex_buffer_len - lexbuf.lex_start_pos + n
|
2002-03-11 00:45:10 -08:00
|
|
|
<= String.length lexbuf.lex_buffer
|
|
|
|
then begin
|
|
|
|
(* But there is enough space if we reclaim the junk at the beginning
|
|
|
|
of the buffer *)
|
|
|
|
String.blit lexbuf.lex_buffer lexbuf.lex_start_pos
|
|
|
|
lexbuf.lex_buffer 0
|
2002-03-18 08:16:31 -08:00
|
|
|
(lexbuf.lex_buffer_len - lexbuf.lex_start_pos)
|
2002-03-11 00:45:10 -08:00
|
|
|
end else begin
|
|
|
|
(* We must grow the buffer. Doubling its size will provide enough
|
2002-03-29 06:24:22 -08:00
|
|
|
space since n <= String.length aux_buffer <= String.length buffer.
|
|
|
|
Watch out for string length overflow, though. *)
|
|
|
|
let newlen =
|
|
|
|
min (2 * String.length lexbuf.lex_buffer) Sys.max_string_length in
|
|
|
|
if lexbuf.lex_buffer_len - lexbuf.lex_start_pos + n > newlen
|
|
|
|
then failwith "Lexing.lex_refill: cannot grow buffer";
|
|
|
|
let newbuf = String.create newlen in
|
2002-03-11 00:45:10 -08:00
|
|
|
(* Copy the valid data to the beginning of the new buffer *)
|
|
|
|
String.blit lexbuf.lex_buffer lexbuf.lex_start_pos
|
|
|
|
newbuf 0
|
2002-03-18 08:16:31 -08:00
|
|
|
(lexbuf.lex_buffer_len - lexbuf.lex_start_pos);
|
2002-03-11 00:45:10 -08:00
|
|
|
lexbuf.lex_buffer <- newbuf
|
|
|
|
end;
|
|
|
|
(* Reallocation or not, we have shifted the data left by
|
|
|
|
start_pos characters; update the positions *)
|
|
|
|
let s = lexbuf.lex_start_pos in
|
|
|
|
lexbuf.lex_abs_pos <- lexbuf.lex_abs_pos + s;
|
|
|
|
lexbuf.lex_curr_pos <- lexbuf.lex_curr_pos - s;
|
|
|
|
lexbuf.lex_start_pos <- 0;
|
|
|
|
lexbuf.lex_last_pos <- lexbuf.lex_last_pos - s;
|
2002-03-18 08:16:31 -08:00
|
|
|
lexbuf.lex_buffer_len <- lexbuf.lex_buffer_len - s
|
1995-08-08 06:37:34 -07:00
|
|
|
end;
|
2002-03-11 00:45:10 -08:00
|
|
|
(* There is now enough space at the end of the buffer *)
|
|
|
|
String.blit aux_buffer 0
|
2002-03-18 08:16:31 -08:00
|
|
|
lexbuf.lex_buffer lexbuf.lex_buffer_len
|
2002-03-11 00:45:10 -08:00
|
|
|
n;
|
2002-03-18 08:16:31 -08:00
|
|
|
lexbuf.lex_buffer_len <- lexbuf.lex_buffer_len + n
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let from_function f =
|
1997-03-05 06:38:24 -08:00
|
|
|
{ refill_buff = lex_refill f (String.create 512);
|
|
|
|
lex_buffer = String.create 1024;
|
2002-03-18 08:16:31 -08:00
|
|
|
lex_buffer_len = 0;
|
2002-03-11 02:38:43 -08:00
|
|
|
lex_abs_pos = 0;
|
|
|
|
lex_start_pos = 0;
|
|
|
|
lex_curr_pos = 0;
|
|
|
|
lex_last_pos = 0;
|
1997-04-15 12:19:16 -07:00
|
|
|
lex_last_action = 0;
|
|
|
|
lex_eof_reached = false }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let from_channel ic =
|
|
|
|
from_function (fun buf n -> input ic buf 0 n)
|
|
|
|
|
|
|
|
let from_string s =
|
1997-04-15 12:19:16 -07:00
|
|
|
{ refill_buff = (fun lexbuf -> lexbuf.lex_eof_reached <- true);
|
|
|
|
lex_buffer = s ^ "";
|
2002-03-18 08:16:31 -08:00
|
|
|
lex_buffer_len = String.length s;
|
1995-05-04 03:15:53 -07:00
|
|
|
lex_abs_pos = 0;
|
|
|
|
lex_start_pos = 0;
|
|
|
|
lex_curr_pos = 0;
|
1996-05-28 05:43:22 -07:00
|
|
|
lex_last_pos = 0;
|
1997-04-15 12:19:16 -07:00
|
|
|
lex_last_action = 0;
|
|
|
|
lex_eof_reached = true }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lexeme lexbuf =
|
|
|
|
let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in
|
|
|
|
let s = String.create len in
|
1995-07-11 01:54:13 -07:00
|
|
|
String.unsafe_blit lexbuf.lex_buffer lexbuf.lex_start_pos s 0 len;
|
|
|
|
s
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lexeme_char lexbuf i =
|
|
|
|
String.get lexbuf.lex_buffer (lexbuf.lex_start_pos + i)
|
|
|
|
|
|
|
|
let lexeme_start lexbuf =
|
|
|
|
lexbuf.lex_abs_pos + lexbuf.lex_start_pos
|
|
|
|
and lexeme_end lexbuf =
|
|
|
|
lexbuf.lex_abs_pos + lexbuf.lex_curr_pos
|
|
|
|
|