/* The "get_next_char" routine for lexers generated by camllex. */

#include "mlvalues.h"
#include "stacks.h"
#include "str.h"

value callback(value closure, value argument);

struct lexer_buffer {
  value refill_buff;
  value lex_buffer;
  value lex_abs_pos;
  value lex_start_pos;
  value lex_curr_pos;
  value lex_last_pos;
  value lex_last_action;
};

value get_next_char(lexbuf)     /* ML */
     struct lexer_buffer * lexbuf;
{
  mlsize_t buffer_len, curr_pos;
  
  buffer_len = string_length(lexbuf->lex_buffer);
  curr_pos = Long_val(lexbuf->lex_curr_pos);
  if (curr_pos >= buffer_len) {
    Push_roots (r, 1);
    r[0] = (value) lexbuf;
    callback(lexbuf->refill_buff, (value) lexbuf);
    lexbuf = (struct lexer_buffer *) r[0];
    curr_pos = Long_val(lexbuf->lex_curr_pos);
    Pop_roots ();
  }
  lexbuf->lex_curr_pos += 2;
  return Val_int(Byte_u(lexbuf->lex_buffer, curr_pos));
}

