#include "tree_sitter/alloc.h"
#include "tree_sitter/array.h"
#include "tree_sitter/parser.h"
#include <string.h>
#include <wctype.h>
typedef enum {
LINE_BREAK,
NO_LINE_BREAK,
SIMPLE_SYMBOL,
STRING_START,
SYMBOL_START,
SUBSHELL_START,
REGEX_START,
STRING_ARRAY_START,
SYMBOL_ARRAY_START,
HEREDOC_BODY_START,
STRING_CONTENT,
HEREDOC_CONTENT,
STRING_END,
HEREDOC_BODY_END,
HEREDOC_START,
FORWARD_SLASH,
BLOCK_AMPERSAND,
SPLAT_STAR,
UNARY_MINUS,
UNARY_MINUS_NUM,
BINARY_MINUS,
BINARY_STAR,
SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE,
HASH_KEY_SYMBOL,
IDENTIFIER_SUFFIX,
CONSTANT_SUFFIX,
HASH_SPLAT_STAR_STAR,
BINARY_STAR_STAR,
ELEMENT_REFERENCE_BRACKET,
SHORT_INTERPOLATION,
UNARY_PLUS,
BINARY_PLUS,
LINE_COMMENT,
UNINTERPRETED,
WORD_SEPARATOR,
SCOPE_OPEN,
BLOCK_SCOPE_OPEN,
SCOPE_CLOSE,
LOCAL_VARIABLE,
PARAMETER_BINDING,
LAMBDA_BRACE,
IMAGINARY_SUFFIX,
RATIONAL_SUFFIX,
RATIONAL_IMAGINARY_SUFFIX,
NONE
} TokenType;
typedef Array(char) String;
typedef struct {
TokenType type;
int32_t open_delimiter;
int32_t close_delimiter;
int32_t nesting_depth;
bool allows_interpolation;
} Literal;
typedef struct {
String word;
bool end_word_indentation_allowed;
bool allows_interpolation;
bool started;
bool mid_line;
} Heredoc;
enum { MAX_LOCALS = 32, MAX_SCOPES = 16, ISOLATED_SCOPE = 0x80 };
typedef struct {
bool has_leading_whitespace;
Array(Literal) literal_stack;
Array(Heredoc) open_heredocs;
uint8_t local_count;
uint32_t locals[MAX_LOCALS];
uint8_t scope_count;
uint8_t scopes[MAX_SCOPES];
uint16_t scope_overflow;
} Scanner;
const char NON_IDENTIFIER_CHARS[] = {
'\0', '\n', '\r', '\t', '\f', '\v', ' ', ':', ';', '`', '"', '\'', '@', '$', '#', '.', ',', '|', '^', '&',
'<', '=', '>', '+', '-', '*', '/', '\\', '%', '?', '!', '~', '(', ')', '[', ']', '{', '}',
};
static inline void skip(Scanner *scanner, TSLexer *lexer) {
scanner->has_leading_whitespace = true;
lexer->advance(lexer, true);
}
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static inline void reset(Scanner *scanner) {
array_delete(&scanner->literal_stack);
for (uint32_t i = 0; i < scanner->open_heredocs.size; i++) {
array_delete(&array_get(&scanner->open_heredocs, i)->word);
}
array_delete(&scanner->open_heredocs);
scanner->local_count = 0;
scanner->scope_count = 0;
scanner->scope_overflow = 0;
}
static inline unsigned serialize(Scanner *scanner, char *buffer) {
unsigned size = 0;
if (scanner->literal_stack.size * 5 + 2 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}
buffer[size++] = (char)scanner->literal_stack.size;
for (uint32_t i = 0; i < scanner->literal_stack.size; i++) {
Literal *literal = array_get(&scanner->literal_stack, i);
buffer[size++] = literal->type;
buffer[size++] = (char)literal->open_delimiter;
buffer[size++] = (char)literal->close_delimiter;
buffer[size++] = (char)literal->nesting_depth;
buffer[size++] = (char)literal->allows_interpolation;
}
buffer[size++] = (char)scanner->open_heredocs.size;
for (uint32_t i = 0; i < scanner->open_heredocs.size; i++) {
Heredoc *heredoc = array_get(&scanner->open_heredocs, i);
if (heredoc->word.size > UINT8_MAX || size + 4 + heredoc->word.size > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}
buffer[size++] = (char)heredoc->end_word_indentation_allowed;
buffer[size++] = (char)heredoc->allows_interpolation;
buffer[size++] = (char)(heredoc->started | heredoc->mid_line << 1);
buffer[size++] = (char)heredoc->word.size;
if (heredoc->word.size > 0) {
memcpy(&buffer[size], heredoc->word.contents, heredoc->word.size);
}
size += heredoc->word.size;
}
if (scanner->local_count > 0 || scanner->scope_count > 0 || scanner->scope_overflow > 0) {
if (size + 4 + sizeof(uint32_t) * scanner->local_count + scanner->scope_count > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return size;
}
buffer[size++] = (char)scanner->local_count;
memcpy(&buffer[size], scanner->locals, sizeof(uint32_t) * scanner->local_count);
size += sizeof(uint32_t) * scanner->local_count;
buffer[size++] = (char)scanner->scope_count;
memcpy(&buffer[size], scanner->scopes, scanner->scope_count);
size += scanner->scope_count;
memcpy(&buffer[size], &scanner->scope_overflow, sizeof(uint16_t));
size += sizeof(uint16_t);
}
return size;
}
static inline void deserialize(Scanner *scanner, const char *buffer, unsigned length) {
unsigned size = 0;
scanner->has_leading_whitespace = false;
reset(scanner);
if (length == 0) {
return;
}
uint8_t literal_depth = buffer[size++];
for (unsigned j = 0; j < literal_depth; j++) {
Literal literal = {0};
literal.type = (TokenType)(buffer[size++]);
literal.open_delimiter = (unsigned char)buffer[size++];
literal.close_delimiter = (unsigned char)buffer[size++];
literal.nesting_depth = (unsigned char)buffer[size++];
literal.allows_interpolation = buffer[size++];
array_push(&scanner->literal_stack, literal);
}
uint8_t open_heredoc_count = buffer[size++];
for (unsigned j = 0; j < open_heredoc_count; j++) {
Heredoc heredoc = {0};
heredoc.end_word_indentation_allowed = buffer[size++];
heredoc.allows_interpolation = buffer[size++];
heredoc.started = buffer[size] & 1;
heredoc.mid_line = (buffer[size++] >> 1) & 1;
heredoc.word = (String)array_new();
uint8_t word_length = buffer[size++];
if (word_length > 0) {
array_reserve(&heredoc.word, word_length);
memcpy(heredoc.word.contents, &buffer[size], word_length);
}
heredoc.word.size = word_length;
size += word_length;
array_push(&scanner->open_heredocs, heredoc);
}
if (size < length) {
scanner->local_count = (uint8_t)buffer[size++];
memcpy(scanner->locals, &buffer[size], sizeof(uint32_t) * scanner->local_count);
size += sizeof(uint32_t) * scanner->local_count;
scanner->scope_count = (uint8_t)buffer[size++];
memcpy(scanner->scopes, &buffer[size], scanner->scope_count);
size += scanner->scope_count;
memcpy(&scanner->scope_overflow, &buffer[size], sizeof(uint16_t));
size += sizeof(uint16_t);
}
assert(size == length);
}
static inline bool is_iden_char(int32_t c);
static inline bool is_at_line_start(const TSLexer *lexer) {
TSLexerPosition position;
lexer->get_position(lexer, &position);
return position.column == 0;
}
static inline bool is_ascii_space(int32_t c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
static inline bool scan_line_comment(TSLexer *lexer);
static inline bool scan_suffixed_word_rest(TSLexer *lexer, const bool *valid_symbols) {
while (is_iden_char(lexer->lookahead)) {
advance(lexer);
}
if (valid_symbols[HASH_KEY_SYMBOL] && lexer->lookahead == ':') {
lexer->mark_end(lexer);
advance(lexer);
lexer->result_symbol = HASH_KEY_SYMBOL;
return lexer->lookahead != ':';
}
if (valid_symbols[IDENTIFIER_SUFFIX] && lexer->lookahead == '!') {
advance(lexer);
lexer->mark_end(lexer);
lexer->result_symbol = IDENTIFIER_SUFFIX;
return lexer->lookahead != '=';
}
return false;
}
static inline bool line_starts_with_and_or(TSLexer *lexer) {
char word[4] = {0};
size_t length = 0;
while (length < sizeof(word) - 1 && lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
word[length++] = (char)lexer->lookahead;
advance(lexer);
}
return !is_iden_char(lexer->lookahead) && lexer->lookahead != '?' && lexer->lookahead != '!' &&
(strcmp(word, "and") == 0 || strcmp(word, "or") == 0);
}
static inline bool is_local(const Scanner *scanner, uint32_t hash) {
uint8_t first_visible = 0;
for (uint8_t i = scanner->scope_count; i > 0; i--) {
if (scanner->scopes[i - 1] & ISOLATED_SCOPE) {
first_visible = scanner->scopes[i - 1] & ~ISOLATED_SCOPE;
break;
}
}
for (uint8_t i = scanner->local_count; i > first_visible; i--) {
if (scanner->locals[i - 1] == hash) {
return true;
}
}
return false;
}
static inline void bind_local(Scanner *scanner, uint32_t hash) {
if (scanner->local_count < MAX_LOCALS && !is_local(scanner, hash)) {
scanner->locals[scanner->local_count++] = hash;
}
}
static inline bool scan_scope_token(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[SCOPE_CLOSE]) {
if (scanner->scope_overflow > 0) {
scanner->scope_overflow--;
} else if (scanner->scope_count > 0) {
scanner->local_count = scanner->scopes[--scanner->scope_count] & ~ISOLATED_SCOPE;
}
lexer->result_symbol = SCOPE_CLOSE;
return true;
}
bool isolated = valid_symbols[SCOPE_OPEN];
if (scanner->scope_count == MAX_SCOPES) {
scanner->scope_overflow++;
} else {
scanner->scopes[scanner->scope_count++] = scanner->local_count | (isolated ? ISOLATED_SCOPE : 0);
}
lexer->result_symbol = isolated ? SCOPE_OPEN : BLOCK_SCOPE_OPEN;
return true;
}
enum { MAX_KEYWORD_LENGTH = 8 };
static inline bool is_keyword(const char *word) {
static const char *const KEYWORDS[] = {
"alias", "and", "begin", "break", "case", "class", "def", "do", "else", "elsif",
"end", "ensure", "false", "for", "if", "in", "module", "next", "nil", "not", "or",
"redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless", "until",
"when", "while", "yield",
};
for (size_t i = 0; i < sizeof(KEYWORDS) / sizeof(KEYWORDS[0]); i++) {
if (strcmp(word, KEYWORDS[i]) == 0) {
return true;
}
}
return false;
}
static inline uint32_t scan_local_name(TSLexer *lexer, char word[MAX_KEYWORD_LENGTH + 1]) {
uint32_t hash = 2166136261u;
uint32_t length = 0;
while (is_iden_char(lexer->lookahead)) {
if (length < MAX_KEYWORD_LENGTH) {
word[length] = (char)lexer->lookahead;
}
hash = (hash ^ (uint32_t)lexer->lookahead) * 16777619u;
length++;
advance(lexer);
}
word[length <= MAX_KEYWORD_LENGTH ? length : 0] = '\0';
return hash;
}
static inline bool scan_assignment(TSLexer *lexer) {
char operator[4] = {0};
size_t operator_length = 0;
while (operator_length < 3 && lexer->lookahead != 0 && strchr("+-*/%|&^<>", lexer->lookahead) != NULL) {
operator[operator_length++] = (char)lexer->lookahead;
advance(lexer);
}
if (lexer->lookahead != '=') {
return false;
}
advance(lexer);
static const char *const ASSIGNMENT_OPERATORS[] = {"", "+", "-", "*", "/", "%", "**", "|", "&", "^", "<<", ">>", "||", "&&"};
for (size_t i = 0; i < sizeof(ASSIGNMENT_OPERATORS) / sizeof(ASSIGNMENT_OPERATORS[0]); i++) {
if (strcmp(operator, ASSIGNMENT_OPERATORS[i]) == 0) {
return operator_length > 0 || (lexer->lookahead != '=' && lexer->lookahead != '~' && lexer->lookahead != '>');
}
}
return false;
}
static inline bool scan_local_variable(Scanner *scanner, TSLexer *lexer, uint32_t hash) {
bool gap = false;
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') {
gap = true;
advance(lexer);
}
bool operand = gap && lexer->lookahead != 0 && strchr("<+-*&%/[", lexer->lookahead) != NULL && is_local(scanner, hash);
if (!operand && !scan_assignment(lexer)) {
return false;
}
bind_local(scanner, hash);
lexer->result_symbol = LOCAL_VARIABLE;
return true;
}
static inline bool scan_clause_keyword(TSLexer *lexer) {
static const char *const KEYWORDS[] = {"rescue", "ensure", "else", "elsif", "end", "when", "in"};
char word[7] = {0};
size_t length = 0;
while (length < sizeof(word) - 1 && lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
word[length++] = (char)lexer->lookahead;
advance(lexer);
}
if (is_iden_char(lexer->lookahead) || lexer->lookahead == '?' || lexer->lookahead == '!') {
return false;
}
for (size_t i = 0; i < sizeof(KEYWORDS) / sizeof(KEYWORDS[0]); i++) {
if (strcmp(word, KEYWORDS[i]) == 0) {
return true;
}
}
return false;
}
static inline bool scan_whitespace(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
bool heredoc_body_start_is_valid = scanner->open_heredocs.size > 0 && !scanner->open_heredocs.contents[0].started &&
valid_symbols[HEREDOC_BODY_START];
bool crossed_newline = false;
bool after_shorthand_line = false;
for (;;) {
if (!valid_symbols[NO_LINE_BREAK] && valid_symbols[LINE_BREAK] && lexer->is_at_included_range_start(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = LINE_BREAK;
return true;
}
if (heredoc_body_start_is_valid && lexer->eof(lexer)) {
lexer->result_symbol = HEREDOC_BODY_START;
scanner->open_heredocs.contents[0].started = true;
return true;
}
switch (lexer->lookahead) {
case ' ':
case '\t':
case '\f':
case '\v':
skip(scanner, lexer);
break;
case '\r':
if (heredoc_body_start_is_valid) {
lexer->result_symbol = HEREDOC_BODY_START;
scanner->open_heredocs.contents[0].started = true;
return true;
} else {
skip(scanner, lexer);
break;
}
case '\n':
if (heredoc_body_start_is_valid) {
lexer->result_symbol = HEREDOC_BODY_START;
scanner->open_heredocs.contents[0].started = true;
return true;
} else if (!valid_symbols[NO_LINE_BREAK] && valid_symbols[LINE_BREAK] && !crossed_newline) {
lexer->mark_end(lexer);
lexer->advance(lexer, true);
crossed_newline = true;
} else if (valid_symbols[NO_LINE_BREAK] && valid_symbols[LINE_BREAK] && !crossed_newline) {
skip(scanner, lexer);
after_shorthand_line = true;
} else {
skip(scanner, lexer);
}
break;
case '\\':
advance(lexer);
if (lexer->lookahead == '\r') {
skip(scanner, lexer);
}
if (is_ascii_space(lexer->lookahead)) {
skip(scanner, lexer);
} else {
return false;
}
break;
default:
if (after_shorthand_line && lexer->lookahead >= 'a' && lexer->lookahead <= 'z' &&
strchr("eirw", lexer->lookahead) != NULL) {
lexer->mark_end(lexer);
if (scan_clause_keyword(lexer)) {
lexer->result_symbol = LINE_BREAK;
return true;
}
return scan_suffixed_word_rest(lexer, valid_symbols);
}
if (crossed_newline) {
if (lexer->lookahead == '|') {
advance(lexer);
if (lexer->lookahead == '|') {
return false;
}
lexer->result_symbol = LINE_BREAK;
} else if (lexer->lookahead != '.' && lexer->lookahead != '&' && lexer->lookahead != '#') {
lexer->result_symbol = LINE_BREAK;
if (line_starts_with_and_or(lexer)) {
return false;
}
} else if (lexer->lookahead == '.') {
advance(lexer);
if (!lexer->eof(lexer) && lexer->lookahead == '.') {
lexer->result_symbol = LINE_BREAK;
} else {
return false;
}
}
}
return true;
}
}
}
static inline bool scan_operator(TSLexer *lexer) {
switch (lexer->lookahead) {
case '<':
advance(lexer);
if (lexer->lookahead == '<') {
advance(lexer);
} else if (lexer->lookahead == '=') {
advance(lexer);
if (lexer->lookahead == '>') {
advance(lexer);
}
}
return true;
case '>':
advance(lexer);
if (lexer->lookahead == '>' || lexer->lookahead == '=') {
advance(lexer);
}
return true;
case '=':
advance(lexer);
if (lexer->lookahead == '~') {
advance(lexer);
return true;
}
if (lexer->lookahead == '=') {
advance(lexer);
if (lexer->lookahead == '=') {
advance(lexer);
}
return true;
}
return false;
case '+':
case '-':
case '~':
advance(lexer);
if (lexer->lookahead == '@') {
advance(lexer);
}
return true;
case '&':
case '^':
case '|':
case '/':
case '%':
case '`':
advance(lexer);
return true;
case '!':
advance(lexer);
if (lexer->lookahead == '=' || lexer->lookahead == '~' || lexer->lookahead == '@') {
advance(lexer);
}
return true;
case '*':
advance(lexer);
if (lexer->lookahead == '*') {
advance(lexer);
}
return true;
case '[':
advance(lexer);
if (lexer->lookahead == ']') {
advance(lexer);
} else {
return false;
}
if (lexer->lookahead == '=') {
advance(lexer);
}
return true;
default:
return false;
}
}
static inline bool is_iden_char(int32_t c) {
return c > 0x7f || memchr(&NON_IDENTIFIER_CHARS, c, sizeof(NON_IDENTIFIER_CHARS)) == NULL;
}
typedef enum { NOT_SPECIAL, SPECIAL_GLOBAL, INVALID_GLOBAL } SpecialGlobal;
static inline SpecialGlobal scan_special_global_name(TSLexer *lexer);
static inline void scan_symbol_name_suffix(TSLexer *lexer);
static inline bool scan_symbol_identifier(TSLexer *lexer) {
if (lexer->lookahead == '@') {
advance(lexer);
if (lexer->lookahead == '@') {
advance(lexer);
}
} else if (lexer->lookahead == '$') {
advance(lexer);
SpecialGlobal special = scan_special_global_name(lexer);
if (special != NOT_SPECIAL) {
return special == SPECIAL_GLOBAL;
}
}
if (iswdigit(lexer->lookahead)) {
return false;
}
if (!is_iden_char(lexer->lookahead)) {
return scan_operator(lexer);
}
while (is_iden_char(lexer->lookahead)) {
advance(lexer);
}
lexer->mark_end(lexer);
scan_symbol_name_suffix(lexer);
return true;
}
static inline SpecialGlobal scan_special_global_name(TSLexer *lexer) {
if (lexer->lookahead == '-') {
advance(lexer);
if (!is_iden_char(lexer->lookahead)) {
return INVALID_GLOBAL;
}
advance(lexer);
return SPECIAL_GLOBAL;
}
if (lexer->lookahead > 0 && lexer->lookahead < 0x80 &&
strchr("!@&`'+~=/\\,;.<>*$?:\"", lexer->lookahead) != NULL) {
advance(lexer);
return SPECIAL_GLOBAL;
}
if (!iswdigit(lexer->lookahead)) {
return NOT_SPECIAL;
}
while (iswdigit(lexer->lookahead)) {
advance(lexer);
}
return SPECIAL_GLOBAL;
}
static inline void scan_symbol_name_suffix(TSLexer *lexer) {
if (lexer->lookahead == '?' || lexer->lookahead == '!') {
advance(lexer);
if (lexer->lookahead != '=' && lexer->lookahead != '~') {
lexer->mark_end(lexer);
}
} else if (lexer->lookahead == '=') {
advance(lexer);
if (lexer->lookahead == '=') {
TSLexerCheckpoint setter_end;
lexer->get_checkpoint(lexer, &setter_end);
advance(lexer);
if (lexer->lookahead == '>') {
lexer->mark_end_at(lexer, &setter_end);
}
} else if (lexer->lookahead != '>' && lexer->lookahead != '~') {
lexer->mark_end(lexer);
}
}
}
static inline bool scan_open_delimiter(Scanner *scanner, TSLexer *lexer, Literal *literal, const bool *valid_symbols) {
switch (lexer->lookahead) {
case '"':
literal->type = STRING_START;
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
literal->allows_interpolation = true;
advance(lexer);
return true;
case '\'':
literal->type = STRING_START;
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
literal->allows_interpolation = false;
advance(lexer);
return true;
case '`':
if (!valid_symbols[SUBSHELL_START]) {
return false;
}
literal->type = SUBSHELL_START;
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
literal->allows_interpolation = true;
advance(lexer);
return true;
case '/':
if (!valid_symbols[REGEX_START]) {
return false;
}
literal->type = REGEX_START;
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
literal->allows_interpolation = true;
advance(lexer);
if (valid_symbols[FORWARD_SLASH] && lexer->lookahead != '/') {
if (!scanner->has_leading_whitespace) {
return false;
}
if (lexer->lookahead == ' ' || lexer->lookahead == '\t' || lexer->lookahead == '\n' ||
lexer->lookahead == '\r') {
return false;
}
if (lexer->lookahead == '=') {
return false;
}
}
return true;
case '%':
if (valid_symbols[FORWARD_SLASH] && (!valid_symbols[SPLAT_STAR] || !scanner->has_leading_whitespace)) {
return false;
}
advance(lexer);
switch (lexer->lookahead) {
case 's':
if (!valid_symbols[SIMPLE_SYMBOL]) {
return false;
}
literal->type = SYMBOL_START;
literal->allows_interpolation = false;
advance(lexer);
break;
case 'r':
if (!valid_symbols[REGEX_START]) {
return false;
}
literal->type = REGEX_START;
literal->allows_interpolation = true;
advance(lexer);
break;
case 'x':
if (!valid_symbols[SUBSHELL_START]) {
return false;
}
literal->type = SUBSHELL_START;
literal->allows_interpolation = true;
advance(lexer);
break;
case 'q':
if (!valid_symbols[STRING_START]) {
return false;
}
literal->type = STRING_START;
literal->allows_interpolation = false;
advance(lexer);
break;
case 'Q':
if (!valid_symbols[STRING_START]) {
return false;
}
literal->type = STRING_START;
literal->allows_interpolation = true;
advance(lexer);
break;
case 'w':
if (!valid_symbols[STRING_ARRAY_START]) {
return false;
}
literal->type = STRING_ARRAY_START;
literal->allows_interpolation = false;
advance(lexer);
break;
case 'i':
if (!valid_symbols[SYMBOL_ARRAY_START]) {
return false;
}
literal->type = SYMBOL_ARRAY_START;
literal->allows_interpolation = false;
advance(lexer);
break;
case 'W':
if (!valid_symbols[STRING_ARRAY_START]) {
return false;
}
literal->type = STRING_ARRAY_START;
literal->allows_interpolation = true;
advance(lexer);
break;
case 'I':
if (!valid_symbols[SYMBOL_ARRAY_START]) {
return false;
}
literal->type = SYMBOL_ARRAY_START;
literal->allows_interpolation = true;
advance(lexer);
break;
default:
if (!valid_symbols[STRING_START]) {
return false;
}
literal->type = STRING_START;
literal->allows_interpolation = true;
if (lexer->lookahead == '=' && valid_symbols[FORWARD_SLASH]) {
return false;
}
break;
}
switch (lexer->lookahead) {
case '(':
literal->open_delimiter = '(';
literal->close_delimiter = ')';
break;
case '[':
literal->open_delimiter = '[';
literal->close_delimiter = ']';
break;
case '{':
literal->open_delimiter = '{';
literal->close_delimiter = '}';
break;
case '<':
literal->open_delimiter = '<';
literal->close_delimiter = '>';
break;
case '\r':
case '\n':
case ' ':
case '\t':
if (valid_symbols[FORWARD_SLASH]) {
return false;
}
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
break;
case '|':
case '!':
case '#':
case '/':
case '\\':
case '@':
case '$':
case '%':
case '^':
case '&':
case '*':
case ')':
case ']':
case '}':
case '>':
case '=':
case '+':
case '-':
case '~':
case '`':
case ',':
case '.':
case '?':
case ':':
case ';':
case '_':
case '"':
case '\'':
literal->open_delimiter = lexer->lookahead;
literal->close_delimiter = lexer->lookahead;
break;
default:
return false;
}
advance(lexer);
return true;
default:
return false;
}
}
static inline bool is_heredoc_word_char(int32_t c) { return c > 0x7f || iswalnum(c) || c == '_'; }
static inline uint8_t encode_utf8(int32_t c, char bytes[4]) {
if (c < 0x80) {
bytes[0] = (char)c;
return 1;
}
if (c < 0x800) {
bytes[0] = (char)(0xc0 | (c >> 6));
bytes[1] = (char)(0x80 | (c & 0x3f));
return 2;
}
if (c < 0x10000) {
bytes[0] = (char)(0xe0 | (c >> 12));
bytes[1] = (char)(0x80 | ((c >> 6) & 0x3f));
bytes[2] = (char)(0x80 | (c & 0x3f));
return 3;
}
bytes[0] = (char)(0xf0 | (c >> 18));
bytes[1] = (char)(0x80 | ((c >> 12) & 0x3f));
bytes[2] = (char)(0x80 | ((c >> 6) & 0x3f));
bytes[3] = (char)(0x80 | (c & 0x3f));
return 4;
}
static inline void push_heredoc_word_char(String *word, int32_t c) {
char bytes[4];
uint8_t length = encode_utf8(c, bytes);
for (uint8_t i = 0; i < length; i++) {
array_push(word, bytes[i]);
}
}
static inline bool scan_heredoc_word(TSLexer *lexer, Heredoc *heredoc) {
String word = array_new();
int32_t quote = 0;
switch (lexer->lookahead) {
case '\'':
case '"':
case '`':
quote = lexer->lookahead;
advance(lexer);
while (lexer->lookahead != quote && !lexer->eof(lexer)) {
push_heredoc_word_char(&word, lexer->lookahead);
advance(lexer);
}
advance(lexer);
break;
default:
if (is_heredoc_word_char(lexer->lookahead)) {
push_heredoc_word_char(&word, lexer->lookahead);
advance(lexer);
while (is_heredoc_word_char(lexer->lookahead)) {
push_heredoc_word_char(&word, lexer->lookahead);
advance(lexer);
}
}
break;
}
heredoc->word = word;
heredoc->allows_interpolation = quote != '\'';
return quote != 0;
}
static inline bool scan_short_interpolation(TSLexer *lexer, const bool has_content, const TSSymbol content_symbol) {
int32_t start = lexer->lookahead;
if (start == '@' || start == '$') {
if (has_content) {
lexer->result_symbol = content_symbol;
return true;
}
lexer->mark_end(lexer);
advance(lexer);
bool is_short_interpolation = false;
if (start == '$') {
if (lexer->lookahead > 0 && lexer->lookahead < 0x80 &&
strchr("!@&`'+~=/\\,;.<>*$?:\"", lexer->lookahead) != NULL) {
is_short_interpolation = true;
} else {
if (lexer->lookahead == '-') {
advance(lexer);
is_short_interpolation = is_iden_char(lexer->lookahead) && !iswdigit(lexer->lookahead);
} else {
is_short_interpolation = is_iden_char(lexer->lookahead);
}
}
}
if (start == '@') {
if (lexer->lookahead == '@') {
advance(lexer);
}
is_short_interpolation = is_iden_char(lexer->lookahead) && !iswdigit(lexer->lookahead);
}
if (is_short_interpolation) {
lexer->result_symbol = SHORT_INTERPOLATION;
return true;
}
}
return false;
}
static inline bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer) {
uint32_t index = 0;
while (index + 1 < scanner->open_heredocs.size && !array_get(&scanner->open_heredocs, index)->started) {
index++;
}
Heredoc *heredoc = array_get(&scanner->open_heredocs, index);
size_t position_in_word = 0;
bool look_for_heredoc_end = !heredoc->mid_line;
bool has_content = false;
heredoc->mid_line = false;
for (;;) {
if (look_for_heredoc_end && position_in_word == heredoc->word.size &&
(position_in_word > 0 || is_at_line_start(lexer))) {
if (!has_content) {
lexer->mark_end(lexer);
}
if (lexer->lookahead == '\n' || lexer->lookahead == '\r' || lexer->eof(lexer)) {
if (has_content) {
lexer->result_symbol = HEREDOC_CONTENT;
} else {
array_delete(&heredoc->word);
array_erase(&scanner->open_heredocs, index);
lexer->result_symbol = HEREDOC_BODY_END;
}
return true;
}
has_content = true;
lexer->mark_end(lexer);
position_in_word = 0;
look_for_heredoc_end = false;
}
if (lexer->eof(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = HEREDOC_CONTENT;
return has_content;
}
char bytes[4];
uint8_t length = encode_utf8(lexer->lookahead, bytes);
if (look_for_heredoc_end && position_in_word + length <= heredoc->word.size &&
memcmp(bytes, array_get(&heredoc->word, position_in_word), length) == 0) {
advance(lexer);
position_in_word += length;
} else {
if (position_in_word > 0) {
has_content = true;
lexer->mark_end(lexer);
}
position_in_word = 0;
look_for_heredoc_end = false;
if (heredoc->allows_interpolation && lexer->lookahead == '\\') {
heredoc->mid_line = true;
if (has_content) {
lexer->result_symbol = HEREDOC_CONTENT;
return true;
}
return false;
}
if (heredoc->allows_interpolation && lexer->lookahead == '#') {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == '{') {
heredoc->mid_line = true;
if (has_content) {
lexer->result_symbol = HEREDOC_CONTENT;
return true;
}
return false;
}
if (scan_short_interpolation(lexer, has_content, HEREDOC_CONTENT)) {
heredoc->mid_line = true;
return true;
}
has_content = true;
lexer->mark_end(lexer);
} else if (lexer->lookahead == '\r' || lexer->lookahead == '\n') {
if (index > 0) {
if (has_content) {
lexer->result_symbol = HEREDOC_CONTENT;
return true;
}
array_get(&scanner->open_heredocs, 0)->started = true;
lexer->result_symbol = HEREDOC_BODY_START;
return true;
}
if (lexer->lookahead == '\r') {
advance(lexer);
if (lexer->lookahead == '\n') {
advance(lexer);
}
} else {
advance(lexer);
}
has_content = true;
look_for_heredoc_end = true;
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') {
advance(lexer);
if (!heredoc->end_word_indentation_allowed) {
look_for_heredoc_end = false;
}
}
lexer->mark_end(lexer);
} else {
has_content = true;
advance(lexer);
lexer->mark_end(lexer);
}
}
}
}
static inline bool scan_literal_content(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
Literal *literal = array_back(&scanner->literal_stack);
bool heredoc_body_start_is_valid = scanner->open_heredocs.size > 0 && !scanner->open_heredocs.contents[0].started &&
valid_symbols[HEREDOC_BODY_START];
bool has_content = false;
bool stop_on_space = literal->type == SYMBOL_ARRAY_START || literal->type == STRING_ARRAY_START;
for (;;) {
if (heredoc_body_start_is_valid && (lexer->lookahead == '\n' || lexer->lookahead == '\r')) {
lexer->mark_end(lexer);
if (has_content) {
lexer->result_symbol = STRING_CONTENT;
} else {
lexer->result_symbol = HEREDOC_BODY_START;
scanner->open_heredocs.contents[0].started = true;
}
return true;
}
if (stop_on_space && is_ascii_space(lexer->lookahead)) {
if (has_content) {
lexer->mark_end(lexer);
lexer->result_symbol = STRING_CONTENT;
return true;
}
lexer->mark_end(lexer);
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') {
advance(lexer);
}
if (heredoc_body_start_is_valid && (lexer->lookahead == '\n' || lexer->lookahead == '\r')) {
lexer->result_symbol = HEREDOC_BODY_START;
scanner->open_heredocs.contents[0].started = true;
return true;
}
while (is_ascii_space(lexer->lookahead)) {
advance(lexer);
}
lexer->mark_end(lexer);
lexer->result_symbol = WORD_SEPARATOR;
return true;
}
if (lexer->lookahead == literal->close_delimiter) {
lexer->mark_end(lexer);
if (literal->nesting_depth == 1) {
if (has_content) {
lexer->result_symbol = STRING_CONTENT;
} else {
advance(lexer);
if (literal->type == REGEX_START) {
while (iswlower(lexer->lookahead)) {
advance(lexer);
}
}
array_pop(&scanner->literal_stack);
lexer->result_symbol = STRING_END;
lexer->mark_end(lexer);
}
return true;
}
literal->nesting_depth--;
advance(lexer);
} else if (lexer->lookahead == literal->open_delimiter) {
literal->nesting_depth++;
advance(lexer);
} else if (literal->allows_interpolation && lexer->lookahead == '#') {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == '{') {
if (has_content) {
lexer->result_symbol = STRING_CONTENT;
return true;
}
return false;
}
if (scan_short_interpolation(lexer, has_content, STRING_CONTENT)) {
return true;
}
} else if (lexer->lookahead == '\\') {
if (literal->allows_interpolation) {
if (has_content) {
lexer->mark_end(lexer);
lexer->result_symbol = STRING_CONTENT;
return true;
}
return false;
}
advance(lexer);
bool escaped_cr = lexer->lookahead == '\r';
advance(lexer);
if (escaped_cr && lexer->lookahead == '\n') {
advance(lexer);
}
} else if (lexer->eof(lexer)) {
advance(lexer);
lexer->mark_end(lexer);
return false;
} else {
advance(lexer);
}
has_content = true;
}
}
static inline bool scan_numeric_suffix(TSLexer *lexer, const bool *valid_symbols) {
TokenType suffix = lexer->lookahead == 'i' ? IMAGINARY_SUFFIX : RATIONAL_SUFFIX;
advance(lexer);
if (suffix == RATIONAL_SUFFIX && !lexer->is_at_included_range_start(lexer) && lexer->lookahead == 'i') {
suffix = RATIONAL_IMAGINARY_SUFFIX;
advance(lexer);
}
if (!valid_symbols[suffix] || (!lexer->is_at_included_range_start(lexer) && is_iden_char(lexer->lookahead))) {
return false;
}
lexer->result_symbol = suffix;
return true;
}
static inline bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
bool raw_data = valid_symbols[UNINTERPRETED] && !valid_symbols[STRING_START];
if (raw_data && scanner->open_heredocs.size == 0 && scanner->literal_stack.size == 0) {
return false;
}
scanner->has_leading_whitespace = false;
bool error_recovery = valid_symbols[SCOPE_OPEN] && valid_symbols[SCOPE_CLOSE];
if (!error_recovery && (valid_symbols[SCOPE_OPEN] || valid_symbols[BLOCK_SCOPE_OPEN] || valid_symbols[SCOPE_CLOSE])) {
return scan_scope_token(scanner, lexer, valid_symbols);
}
if (!valid_symbols[STRING_START]) {
if ((valid_symbols[STRING_CONTENT] || valid_symbols[STRING_END]) && scanner->literal_stack.size > 0) {
return scan_literal_content(scanner, lexer, valid_symbols);
}
if ((valid_symbols[HEREDOC_CONTENT] || valid_symbols[HEREDOC_BODY_END]) && scanner->open_heredocs.size > 0) {
return scan_heredoc_content(scanner, lexer);
}
}
if (!error_recovery && !lexer->is_at_included_range_start(lexer) &&
((lexer->lookahead == 'i' && valid_symbols[IMAGINARY_SUFFIX]) ||
(lexer->lookahead == 'r' && (valid_symbols[RATIONAL_SUFFIX] || valid_symbols[RATIONAL_IMAGINARY_SUFFIX])))) {
return scan_numeric_suffix(lexer, valid_symbols);
}
lexer->result_symbol = NONE;
if (!scan_whitespace(scanner, lexer, valid_symbols)) {
return false;
}
if (lexer->result_symbol != NONE) {
return true;
}
switch (lexer->lookahead) {
case '#':
if (!raw_data && valid_symbols[LINE_COMMENT] && scan_line_comment(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = LINE_COMMENT;
return true;
}
return false;
case '&':
if (valid_symbols[BLOCK_AMPERSAND] && (scanner->has_leading_whitespace || !valid_symbols[BINARY_STAR])) {
advance(lexer);
if (lexer->lookahead != '&' && lexer->lookahead != '.' && lexer->lookahead != '=' &&
(!is_ascii_space(lexer->lookahead) || !valid_symbols[BINARY_STAR])) {
lexer->result_symbol = BLOCK_AMPERSAND;
return true;
}
return false;
}
break;
case '<':
if (valid_symbols[SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE]) {
advance(lexer);
if (lexer->lookahead == '<') {
advance(lexer);
lexer->result_symbol = SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE;
return true;
}
return false;
}
break;
case '*':
if (valid_symbols[SPLAT_STAR] || valid_symbols[BINARY_STAR] || valid_symbols[HASH_SPLAT_STAR_STAR] ||
valid_symbols[BINARY_STAR_STAR]) {
advance(lexer);
if (lexer->lookahead == '=') {
return false;
}
if (lexer->lookahead == '*') {
if (valid_symbols[HASH_SPLAT_STAR_STAR] || valid_symbols[BINARY_STAR_STAR]) {
advance(lexer);
if (lexer->lookahead == '=') {
return false;
}
if (valid_symbols[BINARY_STAR_STAR] && !scanner->has_leading_whitespace) {
lexer->result_symbol = BINARY_STAR_STAR;
return true;
}
if (valid_symbols[HASH_SPLAT_STAR_STAR] && !is_ascii_space(lexer->lookahead)) {
lexer->result_symbol = HASH_SPLAT_STAR_STAR;
return true;
}
if (valid_symbols[BINARY_STAR_STAR]) {
lexer->result_symbol = BINARY_STAR_STAR;
return true;
}
if (valid_symbols[HASH_SPLAT_STAR_STAR]) {
lexer->result_symbol = HASH_SPLAT_STAR_STAR;
return true;
}
return false;
}
return false;
}
if (valid_symbols[BINARY_STAR] && !scanner->has_leading_whitespace) {
lexer->result_symbol = BINARY_STAR;
return true;
}
if (valid_symbols[SPLAT_STAR] && !is_ascii_space(lexer->lookahead)) {
lexer->result_symbol = SPLAT_STAR;
return true;
}
if (valid_symbols[BINARY_STAR]) {
lexer->result_symbol = BINARY_STAR;
return true;
}
if (valid_symbols[SPLAT_STAR]) {
lexer->result_symbol = SPLAT_STAR;
return true;
}
return false;
}
break;
case '-':
if (valid_symbols[UNARY_MINUS] || valid_symbols[UNARY_MINUS_NUM] || valid_symbols[BINARY_MINUS]) {
advance(lexer);
if (lexer->lookahead != '=' && lexer->lookahead != '>') {
if (valid_symbols[UNARY_MINUS_NUM] &&
(!valid_symbols[BINARY_STAR] || scanner->has_leading_whitespace) &&
iswdigit(lexer->lookahead)) {
lexer->result_symbol = UNARY_MINUS_NUM;
return true;
}
if (valid_symbols[UNARY_MINUS] && scanner->has_leading_whitespace && !is_ascii_space(lexer->lookahead)) {
lexer->result_symbol = UNARY_MINUS;
} else if (valid_symbols[BINARY_MINUS]) {
lexer->result_symbol = BINARY_MINUS;
} else {
lexer->result_symbol = UNARY_MINUS;
}
return true;
}
return false;
}
break;
case '+':
if (valid_symbols[UNARY_PLUS] || valid_symbols[BINARY_PLUS]) {
advance(lexer);
if (lexer->lookahead == '=') {
return false;
}
if (valid_symbols[UNARY_PLUS] && !is_ascii_space(lexer->lookahead) &&
(!valid_symbols[BINARY_STAR] || scanner->has_leading_whitespace)) {
lexer->result_symbol = UNARY_PLUS;
} else if (valid_symbols[BINARY_PLUS]) {
lexer->result_symbol = BINARY_PLUS;
} else {
lexer->result_symbol = UNARY_PLUS;
}
return true;
}
break;
case ':':
if (valid_symbols[SYMBOL_START]) {
Literal literal = {0};
literal.type = SYMBOL_START;
literal.nesting_depth = 1;
advance(lexer);
switch (lexer->lookahead) {
case '"':
advance(lexer);
literal.open_delimiter = '"';
literal.close_delimiter = '"';
literal.allows_interpolation = true;
array_push(&scanner->literal_stack, literal);
lexer->result_symbol = SYMBOL_START;
return true;
case '\'':
advance(lexer);
literal.open_delimiter = '\'';
literal.close_delimiter = '\'';
literal.allows_interpolation = false;
array_push(&scanner->literal_stack, literal);
lexer->result_symbol = SYMBOL_START;
return true;
default:
if (scan_symbol_identifier(lexer)) {
lexer->result_symbol = SIMPLE_SYMBOL;
return true;
}
}
return false;
}
break;
case '[':
if (valid_symbols[ELEMENT_REFERENCE_BRACKET] &&
(!scanner->has_leading_whitespace || !valid_symbols[STRING_START])) {
advance(lexer);
lexer->result_symbol = ELEMENT_REFERENCE_BRACKET;
return true;
}
break;
default:
break;
}
if (valid_symbols[LAMBDA_BRACE] && !error_recovery && lexer->lookahead == '{') {
advance(lexer);
lexer->result_symbol = LAMBDA_BRACE;
return true;
}
if (!error_recovery && (valid_symbols[LOCAL_VARIABLE] || valid_symbols[PARAMETER_BINDING]) &&
(lexer->lookahead == '_' || (lexer->lookahead >= 'a' && lexer->lookahead <= 'z') || lexer->lookahead > 0x7f)) {
char word[MAX_KEYWORD_LENGTH + 1] = {0};
uint32_t hash = scan_local_name(lexer, word);
lexer->mark_end(lexer);
if (valid_symbols[PARAMETER_BINDING]) {
if (lexer->lookahead != ':' && (strcmp(word, "do") == 0 || strcmp(word, "nil") == 0)) {
return false;
}
bind_local(scanner, hash);
lexer->result_symbol = PARAMETER_BINDING;
return true;
}
if (lexer->lookahead == ':' || lexer->lookahead == '!') {
return scan_suffixed_word_rest(lexer, valid_symbols);
}
return lexer->lookahead != '?' && !is_keyword(word) && scan_local_variable(scanner, lexer, hash);
}
if (((valid_symbols[HASH_KEY_SYMBOL] || valid_symbols[IDENTIFIER_SUFFIX]) &&
(iswalpha(lexer->lookahead) || lexer->lookahead == '_' || lexer->lookahead > 0x7f)) ||
(valid_symbols[CONSTANT_SUFFIX] && iswupper(lexer->lookahead))) {
TokenType validIdentifierSymbol = iswupper(lexer->lookahead) ? CONSTANT_SUFFIX : IDENTIFIER_SUFFIX;
while (is_iden_char(lexer->lookahead)) {
advance(lexer);
}
if (valid_symbols[HASH_KEY_SYMBOL] && lexer->lookahead == ':') {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead != ':') {
lexer->result_symbol = HASH_KEY_SYMBOL;
return true;
}
} else if (valid_symbols[validIdentifierSymbol] && lexer->lookahead == '!') {
advance(lexer);
if (lexer->lookahead != '=') {
lexer->result_symbol = validIdentifierSymbol;
return true;
}
}
return false;
}
if (valid_symbols[STRING_START]) {
Literal literal = {0};
literal.nesting_depth = 1;
if (lexer->lookahead == '<') {
if (valid_symbols[BINARY_STAR] && (!scanner->has_leading_whitespace || !valid_symbols[SPLAT_STAR])) {
return false;
}
advance(lexer);
if (lexer->lookahead != '<') {
return false;
}
advance(lexer);
Heredoc heredoc = {0};
if (lexer->lookahead == '-' || lexer->lookahead == '~') {
advance(lexer);
heredoc.end_word_indentation_allowed = true;
}
bool quoted = scan_heredoc_word(lexer, &heredoc);
if (heredoc.word.size == 0 && !quoted) {
array_delete(&heredoc.word);
return false;
}
uint32_t pending = 0;
while (pending < scanner->open_heredocs.size && !array_get(&scanner->open_heredocs, pending)->started) {
pending++;
}
if (pending < scanner->open_heredocs.size) {
array_insert(&scanner->open_heredocs, pending, heredoc);
} else {
array_push(&scanner->open_heredocs, heredoc);
}
lexer->result_symbol = HEREDOC_START;
return true;
}
if (scan_open_delimiter(scanner, lexer, &literal, valid_symbols)) {
array_push(&scanner->literal_stack, literal);
lexer->result_symbol = literal.type;
return true;
}
return false;
}
return false;
}
static inline bool scan_line_comment(TSLexer *lexer) {
advance(lexer);
while (!lexer->eof(lexer) && lexer->lookahead != '\n') {
if (lexer->is_at_included_range_start(lexer)) {
return true;
}
advance(lexer);
}
return false;
}
void *tree_sitter_ruby_external_scanner_create() {
Scanner *scanner = (Scanner *)ts_calloc(1, sizeof(Scanner));
return scanner;
}
bool tree_sitter_ruby_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
Scanner *scanner = (Scanner *)payload;
return scan(scanner, lexer, valid_symbols);
}
unsigned tree_sitter_ruby_external_scanner_serialize(void *payload, char *buffer) {
Scanner *scanner = (Scanner *)payload;
return serialize(scanner, buffer);
}
void tree_sitter_ruby_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
Scanner *scanner = (Scanner *)payload;
deserialize(scanner, buffer, length);
}
void tree_sitter_ruby_external_scanner_destroy(void *payload) {
Scanner *scanner = (Scanner *)payload;
for (uint32_t i = 0; i < scanner->open_heredocs.size; i++) {
array_delete(&array_get(&scanner->open_heredocs, i)->word);
}
array_delete(&scanner->open_heredocs);
array_delete(&scanner->literal_stack);
ts_free(scanner);
}