Exemplo n.º 1
0
/* (Private.)  Called by #parse to get the next token (as required by
 * racc).  Returns a 2-element array: [TOKEN_SYM, VALUE].
 */
VALUE cast_Parser_next_token(VALUE self) {
  cast_Parser *self_p;
  VALUE token, pos;
  Get_Struct(self, Parser, self_p);

  /* clear the token val */
  rb_ary_store(self_p->token, 1, Qnil);

  /* call the lexer */
  yylex(self, self_p);

  /* return nil if EOF */
  if (rb_ary_entry(self_p->token, 0) == Qnil)
  {
    freetab(self_p->file);
    return Qnil;
  }

  /**/

  unsigned int     diff;
  diff = strlen(self_p->bot) - strlen(self_p->tok);
  int     line = 0;
  int     col = 0;
  int     count = 0;

  while (self_p->file[line] && count + strlen(self_p->file[line]) < diff)
  {
      count += strlen(self_p->file[line]) + 1;
      line += 1;
  }
  col = diff - count;

  //////////////////////////////////////////////////////
  /* set self.pos */
  pos = rb_iv_get(self, "@pos");

  rb_funcall(pos, rb_intern("col_num="), 1, LONG2NUM(col));
  rb_funcall(pos, rb_intern("line_num="), 1, LONG2NUM(line + 1));
  /* make token */
  token = rb_funcall(rb_const_get(cast_cParser, rb_intern("Token")),
                     rb_intern("new"), 2,
                     rb_funcall2(pos, rb_intern("dup"), 0, NULL),
                     rb_ary_entry(self_p->token, 1));
  /* put the token in the array */
  rb_ary_store(self_p->token, 1, token);
  return self_p->token;
}
Exemplo n.º 2
0
/* (Private.)  Called by #parse to prepare for lexing.
 */
VALUE cast_Parser_prepare_lexer(VALUE self, VALUE string) {
  cast_Parser *self_p;
  char *b, *e;

  Get_Struct(self, Parser, self_p);
  string = rb_convert_type(string, T_STRING, "String", "to_s");


  b = RSTRING_PTR(string);
  e = b + RSTRING_LEN(string) + 1;

  self_p->file = split(b);
  self_p->bot = b;
  self_p->tok = b;
  self_p->ptr = b;
  self_p->cur = b;
  self_p->pos = b;
  self_p->lim = e;
  self_p->top = e;
  self_p->eof = e;
  self_p->lineno = 1;

  return Qnil;
}