1996-02-26 01:42:52 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1996-02-26 01:42:52 -08: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 GNU Library General Public License. */
|
1996-02-26 01:42:52 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* The table-driven automaton for lexers generated by camllex. */
|
|
|
|
|
1996-05-28 05:41:37 -07:00
|
|
|
#include "fail.h"
|
1996-02-26 01:42:52 -08:00
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
|
|
|
|
struct lexer_buffer {
|
|
|
|
value refill_buff;
|
|
|
|
value lex_buffer;
|
|
|
|
value lex_buffer_len;
|
|
|
|
value lex_abs_pos;
|
|
|
|
value lex_start_pos;
|
|
|
|
value lex_curr_pos;
|
|
|
|
value lex_last_pos;
|
1996-05-28 05:41:37 -07:00
|
|
|
value lex_last_action;
|
1997-04-15 12:18:03 -07:00
|
|
|
value lex_eof_reached;
|
1996-02-26 01:42:52 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lexing_table {
|
|
|
|
value lex_base;
|
|
|
|
value lex_backtrk;
|
|
|
|
value lex_default;
|
|
|
|
value lex_trans;
|
|
|
|
value lex_check;
|
|
|
|
};
|
|
|
|
|
1998-06-23 06:39:54 -07:00
|
|
|
#if defined(ARCH_BIG_ENDIAN) || SIZEOF_SHORT != 2
|
1996-02-26 01:42:52 -08:00
|
|
|
#define Short(tbl,n) \
|
1998-04-21 09:09:26 -07:00
|
|
|
(*((unsigned char *)((tbl) + (n) * 2)) + \
|
|
|
|
(*((schar *)((tbl) + (n) * 2 + 1)) << 8))
|
1996-02-26 01:42:52 -08:00
|
|
|
#else
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Short(tbl,n) (((short *)(tbl))[(n)])
|
1996-02-26 01:42:52 -08:00
|
|
|
#endif
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value lex_engine(struct lexing_table *tbl, value start_state,
|
|
|
|
struct lexer_buffer *lexbuf)
|
1996-02-26 01:42:52 -08:00
|
|
|
{
|
1996-05-28 05:41:37 -07:00
|
|
|
int state, base, backtrk, c;
|
1997-03-10 05:56:45 -08:00
|
|
|
|
|
|
|
state = Int_val(start_state);
|
|
|
|
if (state >= 0) {
|
|
|
|
/* First entry */
|
1996-05-28 05:41:37 -07:00
|
|
|
lexbuf->lex_last_pos = lexbuf->lex_start_pos = lexbuf->lex_curr_pos;
|
|
|
|
lexbuf->lex_last_action = Val_int(-1);
|
1997-03-10 05:56:45 -08:00
|
|
|
} else {
|
|
|
|
/* Reentry after refill */
|
|
|
|
state = -state - 1;
|
1996-05-28 05:41:37 -07:00
|
|
|
}
|
1996-02-26 01:42:52 -08:00
|
|
|
while(1) {
|
|
|
|
/* Lookup base address or action number for current state */
|
|
|
|
base = Short(tbl->lex_base, state);
|
|
|
|
if (base < 0) return Val_int(-base-1);
|
|
|
|
/* See if it's a backtrack point */
|
|
|
|
backtrk = Short(tbl->lex_backtrk, state);
|
|
|
|
if (backtrk >= 0) {
|
|
|
|
lexbuf->lex_last_pos = lexbuf->lex_curr_pos;
|
1996-05-28 05:41:37 -07:00
|
|
|
lexbuf->lex_last_action = Val_int(backtrk);
|
1996-02-26 01:42:52 -08:00
|
|
|
}
|
1996-05-28 05:41:37 -07:00
|
|
|
/* See if we need a refill */
|
1997-04-15 12:18:03 -07:00
|
|
|
if (lexbuf->lex_curr_pos >= lexbuf->lex_buffer_len){
|
1997-04-17 10:03:08 -07:00
|
|
|
if (lexbuf->lex_eof_reached == Val_bool (0)){
|
1997-05-19 08:42:21 -07:00
|
|
|
return Val_int(-state - 1);
|
1997-04-15 12:18:03 -07:00
|
|
|
}else{
|
1997-05-19 08:42:21 -07:00
|
|
|
c = 256;
|
1997-04-15 12:18:03 -07:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* Read next input char */
|
|
|
|
c = Byte_u(lexbuf->lex_buffer, Long_val(lexbuf->lex_curr_pos));
|
|
|
|
lexbuf->lex_curr_pos += 2;
|
|
|
|
}
|
1996-02-26 01:42:52 -08:00
|
|
|
/* Determine next state */
|
|
|
|
if (Short(tbl->lex_check, base + c) == state)
|
|
|
|
state = Short(tbl->lex_trans, base + c);
|
|
|
|
else
|
|
|
|
state = Short(tbl->lex_default, state);
|
|
|
|
/* If no transition on this char, return to last backtrack point */
|
|
|
|
if (state < 0) {
|
|
|
|
lexbuf->lex_curr_pos = lexbuf->lex_last_pos;
|
1996-05-28 05:41:37 -07:00
|
|
|
if (lexbuf->lex_last_action == Val_int(-1)) {
|
|
|
|
failwith("lexing: empty token");
|
|
|
|
} else {
|
|
|
|
return lexbuf->lex_last_action;
|
|
|
|
}
|
1997-04-16 06:19:12 -07:00
|
|
|
}else{
|
|
|
|
/* Erase the EOF condition only if the EOF pseudo-character was
|
1997-05-19 08:42:21 -07:00
|
|
|
consumed by the automaton (i.e. there was no backtrack above)
|
1997-04-16 06:19:12 -07:00
|
|
|
*/
|
1997-04-17 10:03:08 -07:00
|
|
|
if (c == 256) lexbuf->lex_eof_reached = Val_bool (0);
|
1996-02-26 01:42:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|