Skip to content

Instantly share code, notes, and snippets.

@enebo
Created April 6, 2020 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enebo/0bace27bf5cbd6122a6fb828d7a4b7bf to your computer and use it in GitHub Desktop.
Save enebo/0bace27bf5cbd6122a6fb828d7a4b7bf to your computer and use it in GitHub Desktop.
rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset)
{
const char *m = name;
const char *e = m + len;
int type = ID_JUNK;
if (!rb_enc_asciicompat(enc)) return -1;
if (!m || len <= 0) return -1;
switch (*m) {
case '\0':
return -1;
case '$':
type = ID_GLOBAL;
if (is_special_global_name(++m, e, enc)) return type;
goto id;
case '@':
type = ID_INSTANCE;
if (*++m == '@') {
++m;
type = ID_CLASS;
}
goto id;
case '<':
switch (*++m) {
case '<': ++m; break;
case '=': if (*++m == '>') ++m; break;
default: break;
}
break;
case '>':
switch (*++m) {
case '>': case '=': ++m; break;
}
break;
case '=':
switch (*++m) {
case '~': ++m; break;
case '=': if (*++m == '=') ++m; break;
default: return -1;
}
break;
case '*':
if (*++m == '*') ++m;
break;
case '+': case '-':
if (*++m == '@') ++m;
break;
case '|': case '^': case '&': case '/': case '%': case '~': case '`':
++m;
break;
case '[':
if (m[1] != ']') goto id;
++m;
if (*++m == '=') ++m;
break;
case '!':
if (len == 1) return ID_JUNK;
switch (*++m) {
case '=': case '~': ++m; break;
default:
if (allowed_attrset & (1U << ID_JUNK)) goto id;
return -1;
}
break;
default:
type = rb_sym_constant_char_p(m, e-m, enc) ? ID_CONST : ID_LOCAL;
id:
if (m >= e || (*m != '_' && !ISALPHA(*m) && ISASCII(*m))) {
if (len > 1 && *(e-1) == '=') {
type = rb_enc_symname_type(name, len-1, enc, allowed_attrset);
if (type != ID_ATTRSET) return ID_ATTRSET;
}
return -1;
}
while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
if (m >= e) break;
switch (*m) {
case '!': case '?':
if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE) return -1;
type = ID_JUNK;
++m;
if (m + 1 < e || *m != '=') break;
/* fall through */
case '=':
if (!(allowed_attrset & (1U << type))) return -1;
type = ID_ATTRSET;
++m;
break;
}
break;
}
return m == e ? type : -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment