1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
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 parsing engine *)
|
|
|
|
|
|
|
|
open Lexing
|
|
|
|
|
|
|
|
(* Internal interface to the parsing engine *)
|
|
|
|
|
|
|
|
type parser_env =
|
|
|
|
{ mutable s_stack : int array; (* States *)
|
|
|
|
mutable v_stack : Obj.t array; (* Semantic attributes *)
|
2002-11-01 09:06:47 -08:00
|
|
|
mutable symb_start_stack : position array; (* Start positions *)
|
|
|
|
mutable symb_end_stack : position array; (* End positions *)
|
1995-05-04 03:15:53 -07:00
|
|
|
mutable stacksize : int; (* Size of the stacks *)
|
1995-08-09 02:39:43 -07:00
|
|
|
mutable stackbase : int; (* Base sp for current parse *)
|
1995-05-04 03:15:53 -07:00
|
|
|
mutable curr_char : int; (* Last token read *)
|
|
|
|
mutable lval : Obj.t; (* Its semantic attribute *)
|
2002-11-01 09:06:47 -08:00
|
|
|
mutable symb_start : position; (* Start pos. of the current symbol*)
|
|
|
|
mutable symb_end : position; (* End pos. of the current symbol *)
|
1995-05-04 03:15:53 -07:00
|
|
|
mutable asp : int; (* The stack pointer for attributes *)
|
|
|
|
mutable rule_len : int; (* Number of rhs items in the rule *)
|
|
|
|
mutable rule_number : int; (* Rule number to reduce by *)
|
|
|
|
mutable sp : int; (* Saved sp for parse_engine *)
|
1995-08-09 02:39:43 -07:00
|
|
|
mutable state : int; (* Saved state for parse_engine *)
|
|
|
|
mutable errflag : int } (* Saved error flag for parse_engine *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-07-12 07:28:51 -07:00
|
|
|
type parse_tables =
|
|
|
|
{ actions : (parser_env -> Obj.t) array;
|
|
|
|
transl_const : int array;
|
|
|
|
transl_block : int array;
|
|
|
|
lhs : string;
|
|
|
|
len : string;
|
|
|
|
defred : string;
|
|
|
|
dgoto : string;
|
|
|
|
sindex : string;
|
|
|
|
rindex : string;
|
|
|
|
gindex : string;
|
|
|
|
tablesize : int;
|
|
|
|
table : string;
|
1995-08-09 02:39:43 -07:00
|
|
|
check : string;
|
2001-11-05 05:34:42 -08:00
|
|
|
error_function : string -> unit;
|
|
|
|
names_const : string;
|
|
|
|
names_block : string }
|
1995-07-12 07:28:51 -07:00
|
|
|
|
|
|
|
exception YYexit of Obj.t
|
|
|
|
exception Parse_error
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
type parser_input =
|
|
|
|
Start
|
|
|
|
| Token_read
|
|
|
|
| Stacks_grown_1
|
|
|
|
| Stacks_grown_2
|
|
|
|
| Semantic_action_computed
|
1995-08-09 02:39:43 -07:00
|
|
|
| Error_detected
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type parser_output =
|
|
|
|
Read_token
|
|
|
|
| Raise_parse_error
|
|
|
|
| Grow_stacks_1
|
|
|
|
| Grow_stacks_2
|
|
|
|
| Compute_semantic_action
|
1995-08-09 02:39:43 -07:00
|
|
|
| Call_error_function
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
external parse_engine :
|
|
|
|
parse_tables -> parser_env -> parser_input -> Obj.t -> parser_output
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_parse_engine"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2008-08-06 02:38:25 -07:00
|
|
|
external set_trace: bool -> bool
|
|
|
|
= "caml_set_parser_trace"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let env =
|
1996-04-22 04:15:41 -07:00
|
|
|
{ s_stack = Array.create 100 0;
|
|
|
|
v_stack = Array.create 100 (Obj.repr ());
|
2002-11-01 09:06:47 -08:00
|
|
|
symb_start_stack = Array.create 100 dummy_pos;
|
|
|
|
symb_end_stack = Array.create 100 dummy_pos;
|
1995-05-04 03:15:53 -07:00
|
|
|
stacksize = 100;
|
1995-08-09 02:39:43 -07:00
|
|
|
stackbase = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
curr_char = 0;
|
|
|
|
lval = Obj.repr ();
|
2002-11-01 09:06:47 -08:00
|
|
|
symb_start = dummy_pos;
|
|
|
|
symb_end = dummy_pos;
|
1995-05-04 03:15:53 -07:00
|
|
|
asp = 0;
|
|
|
|
rule_len = 0;
|
|
|
|
rule_number = 0;
|
|
|
|
sp = 0;
|
1995-08-09 02:39:43 -07:00
|
|
|
state = 0;
|
|
|
|
errflag = 0 }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let grow_stacks() =
|
|
|
|
let oldsize = env.stacksize in
|
|
|
|
let newsize = oldsize * 2 in
|
1996-04-22 04:15:41 -07:00
|
|
|
let new_s = Array.create newsize 0
|
|
|
|
and new_v = Array.create newsize (Obj.repr ())
|
2002-11-01 09:06:47 -08:00
|
|
|
and new_start = Array.create newsize dummy_pos
|
|
|
|
and new_end = Array.create newsize dummy_pos in
|
1995-05-04 03:15:53 -07:00
|
|
|
Array.blit env.s_stack 0 new_s 0 oldsize;
|
|
|
|
env.s_stack <- new_s;
|
|
|
|
Array.blit env.v_stack 0 new_v 0 oldsize;
|
|
|
|
env.v_stack <- new_v;
|
|
|
|
Array.blit env.symb_start_stack 0 new_start 0 oldsize;
|
|
|
|
env.symb_start_stack <- new_start;
|
|
|
|
Array.blit env.symb_end_stack 0 new_end 0 oldsize;
|
|
|
|
env.symb_end_stack <- new_end;
|
|
|
|
env.stacksize <- newsize
|
|
|
|
|
|
|
|
let clear_parser() =
|
|
|
|
Array.fill env.v_stack 0 env.stacksize (Obj.repr ());
|
|
|
|
env.lval <- Obj.repr ()
|
|
|
|
|
1999-12-08 00:21:57 -08:00
|
|
|
let current_lookahead_fun = ref (fun (x : Obj.t) -> false)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let yyparse tables start lexer lexbuf =
|
|
|
|
let rec loop cmd arg =
|
|
|
|
match parse_engine tables env cmd arg with
|
|
|
|
Read_token ->
|
|
|
|
let t = Obj.repr(lexer lexbuf) in
|
2002-11-01 09:06:47 -08:00
|
|
|
env.symb_start <- lexbuf.lex_start_p;
|
|
|
|
env.symb_end <- lexbuf.lex_curr_p;
|
1995-05-04 03:15:53 -07:00
|
|
|
loop Token_read t
|
|
|
|
| Raise_parse_error ->
|
|
|
|
raise Parse_error
|
|
|
|
| Compute_semantic_action ->
|
1995-08-09 02:39:43 -07:00
|
|
|
let (action, value) =
|
|
|
|
try
|
|
|
|
(Semantic_action_computed, tables.actions.(env.rule_number) env)
|
|
|
|
with Parse_error ->
|
|
|
|
(Error_detected, Obj.repr ()) in
|
|
|
|
loop action value
|
1995-05-04 03:15:53 -07:00
|
|
|
| Grow_stacks_1 ->
|
|
|
|
grow_stacks(); loop Stacks_grown_1 (Obj.repr ())
|
|
|
|
| Grow_stacks_2 ->
|
1995-08-09 02:39:43 -07:00
|
|
|
grow_stacks(); loop Stacks_grown_2 (Obj.repr ())
|
|
|
|
| Call_error_function ->
|
|
|
|
tables.error_function "syntax error";
|
|
|
|
loop Error_detected (Obj.repr ()) in
|
1995-05-04 03:15:53 -07:00
|
|
|
let init_asp = env.asp
|
|
|
|
and init_sp = env.sp
|
1997-06-30 06:02:35 -07:00
|
|
|
and init_stackbase = env.stackbase
|
1995-05-04 03:15:53 -07:00
|
|
|
and init_state = env.state
|
1997-06-30 06:02:35 -07:00
|
|
|
and init_curr_char = env.curr_char
|
2011-07-20 02:17:07 -07:00
|
|
|
and init_lval = env.lval
|
1997-06-30 06:02:35 -07:00
|
|
|
and init_errflag = env.errflag in
|
1995-08-09 02:39:43 -07:00
|
|
|
env.stackbase <- env.sp + 1;
|
1995-05-04 03:15:53 -07:00
|
|
|
env.curr_char <- start;
|
2003-08-01 16:28:58 -07:00
|
|
|
env.symb_end <- lexbuf.lex_curr_p;
|
1995-05-04 03:15:53 -07:00
|
|
|
try
|
|
|
|
loop Start (Obj.repr ())
|
|
|
|
with exn ->
|
|
|
|
let curr_char = env.curr_char in
|
|
|
|
env.asp <- init_asp;
|
|
|
|
env.sp <- init_sp;
|
1997-07-01 06:36:41 -07:00
|
|
|
env.stackbase <- init_stackbase;
|
1995-05-04 03:15:53 -07:00
|
|
|
env.state <- init_state;
|
|
|
|
env.curr_char <- init_curr_char;
|
2011-07-20 02:17:07 -07:00
|
|
|
env.lval <- init_lval;
|
1997-06-30 06:02:35 -07:00
|
|
|
env.errflag <- init_errflag;
|
1995-05-04 03:15:53 -07:00
|
|
|
match exn with
|
|
|
|
YYexit v ->
|
|
|
|
Obj.magic v
|
|
|
|
| _ ->
|
|
|
|
current_lookahead_fun :=
|
1995-06-18 07:45:56 -07:00
|
|
|
(fun tok ->
|
|
|
|
if Obj.is_block tok
|
|
|
|
then tables.transl_block.(Obj.tag tok) = curr_char
|
|
|
|
else tables.transl_const.(Obj.magic tok) = curr_char);
|
1995-05-04 03:15:53 -07:00
|
|
|
raise exn
|
|
|
|
|
1995-07-12 07:28:51 -07:00
|
|
|
let peek_val env n =
|
1995-05-04 03:15:53 -07:00
|
|
|
Obj.magic env.v_stack.(env.asp - n)
|
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
let symbol_start_pos () =
|
2009-05-20 04:52:42 -07:00
|
|
|
let rec loop i =
|
|
|
|
if i <= 0 then env.symb_end_stack.(env.asp)
|
|
|
|
else begin
|
|
|
|
let st = env.symb_start_stack.(env.asp - i + 1) in
|
|
|
|
let en = env.symb_end_stack.(env.asp - i + 1) in
|
|
|
|
if st <> en then st else loop (i - 1)
|
|
|
|
end
|
|
|
|
in
|
|
|
|
loop env.rule_len
|
2002-11-01 09:06:47 -08:00
|
|
|
;;
|
|
|
|
let symbol_end_pos () = env.symb_end_stack.(env.asp);;
|
|
|
|
let rhs_start_pos n = env.symb_start_stack.(env.asp - (env.rule_len - n));;
|
|
|
|
let rhs_end_pos n = env.symb_end_stack.(env.asp - (env.rule_len - n));;
|
|
|
|
|
|
|
|
let symbol_start () = (symbol_start_pos ()).pos_cnum;;
|
|
|
|
let symbol_end () = (symbol_end_pos ()).pos_cnum;;
|
|
|
|
let rhs_start n = (rhs_start_pos n).pos_cnum;;
|
|
|
|
let rhs_end n = (rhs_end_pos n).pos_cnum;;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let is_current_lookahead tok =
|
|
|
|
(!current_lookahead_fun)(Obj.repr tok)
|
1995-08-09 02:39:43 -07:00
|
|
|
|
1999-12-08 00:21:57 -08:00
|
|
|
let parse_error (msg : string) = ()
|