Exemple #1
0
/*
** convert an hexadecimal numeric string to a number, following
** C99 specification for 'strtod'
*/
static lua_Number lua_strx2number (const char *s, char **endptr) {
  lua_Number r = 0.0;  /* result (accumulator) */
  int sigdig = 0;  /* number of significant digits */
  int nosigdig = 0;  /* number of non-significant digits */
  int e = 0;  /* exponent correction */
  int neg = 0;  /* 1 if number is negative */
  int dot = 0;  /* true after seen a dot */
  *endptr = cast(char *, s);  /* nothing is valid yet */
  while (lisspace(cast_uchar(*s))) s++;  /* skip initial spaces */
  neg = isneg(&s);  /* check signal */
  if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X')))  /* check '0x' */
    return 0.0;  /* invalid format (no '0x') */
  for (s += 2; ; s++) {  /* skip '0x' and read numeral */
    if (*s == '.') {
      if (dot) break;  /* second dot? stop loop */
      else dot = 1;
    }
    else if (lisxdigit(cast_uchar(*s))) {
      if (sigdig == 0 && *s == '0') {  /* non-significant zero? */
        nosigdig++;
        if (dot) e--;  /* zero after dot? correct exponent */
      }
      else {
        if (++sigdig <= MAXSIGDIG) {  /* can read it without overflow? */
          r = (r * cast_num(16.0)) + luaO_hexavalue(cast_uchar(*s));
          if (dot) e--;  /* decimal digit */
        }
        else  /* too many digits; ignore */ 
          if (!dot) e++;  /* still count it for exponent */
      }
    }
    else break;  /* neither a dot nor a digit */
  }
  if (nosigdig + sigdig == 0)  /* no digits? */
    return 0.0;  /* invalid format */
  *endptr = cast(char *, s);  /* valid up to here */
  e *= 4;  /* each digit multiplies/divides value by 2^4 */
  if (*s == 'p' || *s == 'P') {  /* exponent part? */
    int exp1 = 0;  /* exponent value */
    int neg1;  /* exponent signal */
    s++;  /* skip 'p' */
    neg1 = isneg(&s);  /* signal */
    if (!lisdigit(cast_uchar(*s)))
      return 0.0;  /* invalid; must have at least one digit */
    while (lisdigit(cast_uchar(*s)))  /* read exponent */
      exp1 = exp1 * 10 + *(s++) - '0';
    if (neg1) exp1 = -exp1;
    e += exp1;
    *endptr = cast(char *, s);  /* valid up to here */
  }
Exemple #2
0
/*
** this function is quite liberal in what it accepts, as 'luaO_str2num'
** will reject ill-formed numerals.
*/
static int read_numeral (LexState *ls, SemInfo *seminfo) {
  TValue obj;
  const char *expo = "Ee";
  int first = ls->current;
  lua_assert(lisdigit(ls->current));
  save_and_next(ls);
  if (first == '0' && check_next2(ls, "xX"))  /* hexadecimal? */
    expo = "Pp";
  for (;;) {
    if (check_next2(ls, expo))  /* exponent part? */
      check_next2(ls, "-+");  /* optional exponent sign */
    if (lisxdigit(ls->current))
      save_and_next(ls);
    else if (ls->current == '.')
      save_and_next(ls);
    else break;
  }
  save(ls, '\0');
  buffreplace(ls, '.', ls->decpoint);  /* follow locale for decimal point */
  if (!buff2num(ls->buff, &obj))  /* format error? */
    trydecpoint(ls, &obj); /* try to update decimal point separator */
  if (ttisinteger(&obj)) {
    seminfo->i = ivalue(&obj);
    return TK_INT;
  }
  else {
    lua_assert(ttisfloat(&obj));
    seminfo->r = fltvalue(&obj);
    return TK_FLT;
  }
}
Exemple #3
0
/*
** this function is quite liberal in what it accepts, as 'luaO_str2num'
** will reject ill-formed numerals.
*/
static int read_numeral (LexState *ls, SemInfo *seminfo) {
  TValue obj;
  const char *expo = "Ee";
  int first = ls->current;
  lua_assert(lisdigit(ls->current));
  save_and_next(ls);
  if (first == '0' && check_next2(ls, "xX"))  /* hexadecimal? */
    expo = "Pp";
  for (;;) {
    if (check_next2(ls, expo))  /* exponent part? */
      check_next2(ls, "-+");  /* optional exponent sign */
    if (lisxdigit(ls->current))
      save_and_next(ls);
    else if (ls->current == '.')
      save_and_next(ls);
    else break;
  }
  save(ls, '\0');
  if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0)  /* format error? */
    lexerror(ls, "malformed number", TK_FLT);
  if (ttisinteger(&obj)) {
    seminfo->i = ivalue(&obj);
    return TK_INT;
  }
  else {
    lua_assert(ttisfloat(&obj));
    seminfo->r = fltvalue(&obj);
    return TK_FLT;
  }
}
Exemple #4
0
static void read_numeric(lexer_state *ls, token_info *info)
{
  int seen_dot  = 0;

  for(;;) {
    if(ls->current == '.') {
      if(seen_dot) break;
      seen_dot = 1;
    }
    else if(!lisdigit(ls->current)) {

      // bad number
      if(lisalpha(ls->current)) {
        while(lisalnum(ls->current)) save_and_next(ls);
        lexer_error(ls, "badly formatted number");
      }

      // end of number
      break;
    }

    save_and_next(ls);
  }

  long double number = strtold(ls->buf->buf, NULL);

  info->string = strdup(ls->buf->buf);
  info->number = number;
}
Exemple #5
0
static int readdecesc (LexState *ls) {
  int i;
  int r = 0;  /* result accumulator */
  for (i = 0; i < 3 && lisdigit(ls->current); i++) {  /* read up to 3 digits */
    r = 10*r + ls->current - '0';
    save_and_next(ls);
  }
  esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
  luaZ_buffremove(ls->buff, i);  /* remove read digits from buffer */
  return r;
}
Exemple #6
0
static int getnum(charptr* linep)
{
	int n;
	charptr cp;

	n = 0;
	cp = *linep;
	while (lisdigit(*cp))
		n = n * 10 + ((*cp++) - '0');
	*linep = cp;
	return(n);
}
Exemple #7
0
/*
** convert an hexadecimal numeric string to a number, following
** C99 specification for 'strtod'
*/
static lua_Number lua_strx2number (const char *s, char **endptr) {
  lua_Number r = 0.0;
  int e = 0, i = 0;
  int neg = 0;  /* 1 if number is negative */
  *endptr = cast(char *, s);  /* nothing is valid yet */
  while (lisspace(cast_uchar(*s))) s++;  /* skip initial spaces */
  neg = isneg(&s);  /* check signal */
  if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X')))  /* check '0x' */
    return 0.0;  /* invalid format (no '0x') */
  s += 2;  /* skip '0x' */
  r = readhexa(&s, r, &i);  /* read integer part */
  if (*s == '.') {
    s++;  /* skip dot */
    r = readhexa(&s, r, &e);  /* read fractional part */
  }
  if (i == 0 && e == 0)
    return 0.0;  /* invalid format (no digit) */
  e *= -4;  /* each fractional digit divides value by 2^-4 */
  *endptr = cast(char *, s);  /* valid up to here */
  if (*s == 'p' || *s == 'P') {  /* exponent part? */
    int exp1 = 0;
    int neg1;
    s++;  /* skip 'p' */
    neg1 = isneg(&s);  /* signal */
    if (!lisdigit(cast_uchar(*s)))
      goto ret;  /* must have at least one digit */
    while (lisdigit(cast_uchar(*s)))  /* read exponent */
      exp1 = exp1 * 10 + *(s++) - '0';
    if (neg1) exp1 = -exp1;
    e += exp1;
  }
  *endptr = cast(char *, s);  /* valid up to here */
 ret:
  if (neg) r = -r;
  return l_mathop(ldexp)(r, e);
}
Exemple #8
0
static int read_numeral (LexState *ls, SemInfo *seminfo) {
  TValue obj;
  int first = ls->current;
  lua_assert(lisdigit(ls->current));
  save_and_next(ls);
  if (first == '0')
    check_next2(ls, "xX");  /* hexadecimal? */
  for (;;) {
    if (lisxdigit(ls->current))
      save_and_next(ls);
    else break;
  }
  save(ls, '\0');
  if (!buff2num(ls->buff, &obj))  /* format error? */
    lexerror(ls, "malformed number", TK_INT);
  lua_assert(ttisinteger(&obj));
  seminfo->i = ivalue(&obj);
  return TK_INT;
}
Exemple #9
0
int luaO_hexavalue (int c) {
  if (lisdigit(c)) return c - '0';
  else return ltolower(c) - 'a' + 10;
}
Exemple #10
0
static int llex (LexState *ls, SemInfo *seminfo) {
  luaZ_resetbuffer(ls->buff);
  for (;;) {
    switch (ls->current) {
      case '\n': case '\r': {  /* line breaks */
        inclinenumber(ls);
        break;
      }
      case ' ': case '\f': case '\t': case '\v': {  /* spaces */
        next(ls);
        break;
      }
      case '-': {  /* '-' or '--' (comment) */
        next(ls);
        if (ls->current != '-') return '-';
        /* else is a comment */
        next(ls);
        if (ls->current == '[') {  /* long comment? */
          int sep = skip_sep(ls);
          luaZ_resetbuffer(ls->buff);  /* 'skip_sep' may dirty the buffer */
          if (sep >= 0) {
            read_long_string(ls, NULL, sep);  /* skip long comment */
            luaZ_resetbuffer(ls->buff);  /* previous call may dirty the buff. */
            break;
          }
        }
        /* else short comment */
        while (!currIsNewline(ls) && ls->current != EOZ)
          next(ls);  /* skip until end of line (or end of file) */
        break;
      }
      case '[': {  /* long string or simply '[' */
        int sep = skip_sep(ls);
        if (sep >= 0) {
          read_long_string(ls, seminfo, sep);
          return TK_STRING;
        }
        else if (sep != -1)  /* '[=...' missing second bracket */
          lexerror(ls, "invalid long string delimiter", TK_STRING);
        return '[';
      }
      case '=': {
        next(ls);
        if (check_next1(ls, '=')) return TK_EQ;
        else return '=';
      }
      case '<': {
        next(ls);
        if (check_next1(ls, '=')) return TK_LE;
        else if (check_next1(ls, '<')) return TK_SHL;
        else return '<';
      }
      case '>': {
        next(ls);
        if (check_next1(ls, '=')) return TK_GE;
        else if (check_next1(ls, '>')) return TK_SHR;
        else return '>';
      }
      case '/': {
        next(ls);
        if (check_next1(ls, '/')) return TK_IDIV;
        else return '/';
      }
      case '~': {
        next(ls);
        if (check_next1(ls, '=')) return TK_NE;
        else return '~';
      }
      case ':': {
        next(ls);
        if (check_next1(ls, ':')) return TK_DBCOLON;
        else return ':';
      }
      case '"': case '\'': {  /* short literal strings */
        read_string(ls, ls->current, seminfo);
        return TK_STRING;
      }
      case '`': {  /* relative paths */
        read_string(ls, ls->current, seminfo);
        return TK_PATH;
      }
      case '.': {  /* '.', '..', '...', or number */
        save_and_next(ls);
        if (check_next1(ls, '.')) {
          if (check_next1(ls, '.'))
            return TK_DOTS;   /* '...' */
          else return TK_CONCAT;   /* '..' */
        }
        else if (!lisdigit(ls->current)) return '.';
        else return read_numeral(ls, seminfo);
      }
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9': {
        return read_numeral(ls, seminfo);
      }
      case EOZ: {
        return TK_EOS;
      }
      default: {
        if (lislalpha(ls->current)) {  /* identifier or reserved word? */
          TString *ts;
          do {
            save_and_next(ls);
          } while (lislalnum(ls->current));
          ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
                                  luaZ_bufflen(ls->buff));
          seminfo->ts = ts;
          if (isreserved(ts))  /* reserved word? */
            return ts->extra - 1 + FIRST_RESERVED;
          else {
            return TK_NAME;
          }
        }
        else {  /* single-char tokens (+ - / ...) */
          int c = ls->current;
          next(ls);
          return c;
        }
      }
    }
  }
}
Exemple #11
0
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
  save_and_next(ls);  /* keep delimiter (for error messages) */
  while (ls->current != del) {
    switch (ls->current) {
      case EOZ:
        lexerror(ls, "unfinished string", TK_EOS);
        break;  /* to avoid warnings */
      case '\n':
      case '\r':
        lexerror(ls, "unfinished string", TK_STRING);
        break;  /* to avoid warnings */
      case '\\': {  /* escape sequences */
        int c;  /* final character to be saved */
        save_and_next(ls);  /* keep '\\' for error messages */
        switch (ls->current) {
          case 'a': c = '\a'; goto read_save;
          case 'b': c = '\b'; goto read_save;
          case 'f': c = '\f'; goto read_save;
          case 'n': c = '\n'; goto read_save;
          case 'r': c = '\r'; goto read_save;
          case 't': c = '\t'; goto read_save;
          case 'v': c = '\v'; goto read_save;
          case 'x': c = readhexaesc(ls); goto read_save;
          case 'u': utf8esc(ls);  goto no_save;
          case '\n': case '\r':
            inclinenumber(ls); c = '\n'; goto only_save;
          case '\\': case '\"': case '\'':
            c = ls->current; goto read_save;
          case EOZ: goto no_save;  /* will raise an error next loop */
          case 'z': {  /* zap following span of spaces */
            luaZ_buffremove(ls->buff, 1);  /* remove '\\' */
            next(ls);  /* skip the 'z' */
            while (lisspace(ls->current)) {
              if (currIsNewline(ls)) inclinenumber(ls);
              else next(ls);
            }
            goto no_save;
          }
          default: {
            esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
            c = readdecesc(ls);  /* digital escape '\ddd' */
            goto only_save;
          }
        }
       read_save:
         next(ls);
         /* go through */
       only_save:
         luaZ_buffremove(ls->buff, 1);  /* remove '\\' */
         save(ls, c);
         /* go through */
       no_save: break;
      }
      default:
        save_and_next(ls);
    }
  }
  save_and_next(ls);  /* skip delimiter */
  seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
                                   luaZ_bufflen(ls->buff) - 2);
}
Exemple #12
0
void esp_vprintf(const func_ptr f_ptr,
	charptr ctrl, va_list argp)
{

	int long_flag;
	int dot_flag;
	char ch;


	out_char = f_ptr;

	for (; *ctrl; ctrl++) {

		/* move format string chars to buffer until a  */
		/* format control is found.                    */
		if (*ctrl != '%') {
			out_char(*ctrl);
			continue;
		}

		/* initialize all the flags for this format.   */
		dot_flag =
			long_flag =
			left_flag =
			do_padding = 0;
		pad_character = ' ';
		num2 = 32767;

	try_next:
		ch = *(++ctrl);

		if (lisdigit(ch)) {
			if (dot_flag)
				num2 = getnum(&ctrl);
			else {
				if (ch == '0')
					pad_character = '0';

				num1 = getnum(&ctrl);
				do_padding = 1;
			}
			ctrl--;
			goto try_next;
		}

		switch (ltolower(ch)) {
		case '%':
			out_char('%');
			continue;

		case '-':
			left_flag = 1;
			break;

		case '.':
			dot_flag = 1;
			break;

		case 'l':
			long_flag = 1;
			break;

		case 'i':
		case 'd':
			if (long_flag || ch == 'D') {
				outnum(va_arg(argp, long), 10L);
				continue;
			}
			else {
				outnum(va_arg(argp, int), 10L);
				continue;
			}
		case 'x':
			outnum((long)va_arg(argp, int), 16L);
			continue;

		case 's':
			outs(va_arg(argp, charptr));
			continue;

		case 'c':
			out_char(va_arg(argp, int));
			continue;

		case '\\':
			switch (*ctrl) {
			case 'a':
				out_char(0x07);
				break;
			case 'h':
				out_char(0x08);
				break;
			case 'r':
				out_char(0x0D);
				break;
			case 'n':
				out_char(0x0D);
				out_char(0x0A);
				break;
			default:
				out_char(*ctrl);
				break;
			}
			ctrl++;
			break;

		default:
			continue;
		}
Exemple #13
0
static int lex(lexer_state *ls, token_info *info)
{
  buffer_reset(ls->buf);

  if(setjmp(ls->error.buf))
    return TK_ERROR;

  for(;;) {
    switch(ls->current) {

    case '\n': case '\r': { // newline
      inc_line(ls);
      break;
    }

    case ' ': case '\t': { // whitespace
      next(ls);
      break;
    }

    case '-': { // comment or minus
      // minus
      if(next(ls) != '-') return '-';

      // comment, skip line
      while(next(ls) != EOS && !isnewline(ls));
      break;
    }

    case '=': { // EQ
      next(ls);
      return '=';
    }

    case '<': { // LT, LTE, ASSIGN
      next(ls);
      if(ls->current == '=') {
        next(ls);
        return TK_LTE;
      }
      else if(ls->current == '-'){
        next(ls);
        return TK_ASSIGN;
      }
      else return '<';
    }

    case '>': { // GT, GTE
      next(ls);
      if(ls->current == '=') {
        next(ls);
        return TK_GTE;
      }
      else return '>';
    }

    case '/': { // NEQ, DIV
      next(ls);
      if(ls->current == '=') {
        next(ls);
        return TK_NEQ;
      }

      else return '/';
    }

    case '"': { // STRING
      read_string(ls, info);
      return TK_STRING;
    }

    case EOS: { // EOS
      return TK_EOS;
    }

    default: {
      if(lisdigit(ls->current)) { // NUMERIC
        read_numeric(ls, info);
        return TK_REAL;
      }

      if(lisalpha(ls->current)) { // ID or RESERVED
        return read_id_or_reserved(ls, info);
      }

      int c = ls->current;

      // valid operators, single character tokens, etc.
      switch(ls->current) {
      case '+': case '-': case '*': case '/':
      case '!': case '>': case '<': case '=':
      case '(': case ')': case '[': case ']':
      case '{': case '}': case ':': case '.':
      case ',':
        next(ls);
        return c;

      default:
        lexer_error(ls, "unrecognized symbol %c", c);
        next(ls);
      }
    }
    }
  }
}