#include "unicode_character_classes.h"
#include "local_scope.h"
#include "tree_sitter/alloc.h"
#include "tree_sitter/array.h"
#include "tree_sitter/parser.h"
#include <string.h>
#include <wctype.h>
_Static_assert(TREE_SITTER_SERIALIZATION_BUFFER_SIZE <= (UINT16_MAX >> 4),
"Heredoc length flags must cover the serialization buffer");
typedef enum {
LINE_BREAK,
CHAINED_SCOPE_GAP,
CONTINUED_ARGUMENT_LINE,
ANONYMOUS_ARGUMENT_END,
NO_LINE_BREAK,
JUMP_OPERAND,
SIMPLE_SYMBOL,
STRING_START,
CHAINED_STRING_START,
WORD_SEPARATOR,
LITERAL_ESCAPE,
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_PLUS,
BINARY_PLUS,
UNARY_MINUS,
UNARY_MINUS_NUM,
BINARY_MINUS,
BINARY_STAR,
BITWISE_AND,
SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE,
HASH_KEY_SYMBOL,
IDENTIFIER_SUFFIX,
CONSTANT_SUFFIX,
HASH_SPLAT_STAR_STAR,
BINARY_STAR_STAR,
MODULO_ASSIGNMENT,
ELEMENT_REFERENCE_BRACKET,
SHORT_INTERPOLATION,
LINE_COMMENT,
BLOCK_COMMENT,
UNINTERPRETED,
NOT_PARENTHESIZED,
LAMBDA_BRACE,
HEREDOC_RESUME,
BODY_COMMENT,
BODY_LINE_BREAK,
END_OF_SOURCE,
ENDLESS_EQUALS,
SPACED_ENDLESS_EQUALS,
LOCAL_IDENTIFIER,
WORD_IDENTIFIER,
PARAMETER_IDENTIFIER,
LOCAL_SCOPE,
METHOD_SCOPE,
CLASS_BODY_SCOPE,
BLOCK_SCOPE,
LEAVE_SCOPE,
BINDING_IDENTIFIER,
FIRST_TARGET,
NEXT_TARGET,
STATEMENT_SEMICOLON,
COMMIT_TARGETS,
CAPTURE_MATCH_OPERATOR,
BEFORE_MATCH_DO,
PUBLISH_LOOP_MATCH,
BEFORE_LAMBDA_BRACE,
LEAVE_ASSIGNED_MATCH,
STRONG_REGEXP_OPERAND,
HASH_CAPTURE,
UNBRACED_HASH_CAPTURE,
DEFINED,
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;
bool resume_after_heredoc;
} Literal;
typedef struct {
Literal literal;
uint32_t parent;
uint32_t depth;
} LiteralFrame;
typedef struct {
uint32_t words[7];
} LiteralKey;
typedef struct {
uint32_t children[2];
uint32_t frame;
uint8_t bit;
} LiteralIndexNode;
typedef struct {
String word;
bool end_word_indentation_allowed;
bool squiggly;
bool allows_interpolation;
bool mid_line;
bool started;
bool newline_consumed;
bool host_line_continued;
bool finished;
} Heredoc;
typedef struct {
LocalScopes locals;
bool has_leading_whitespace;
bool continuation_comments;
Array(LiteralFrame) literal_frames;
Array(LiteralIndexNode) literal_index;
uint32_t literal_root;
Literal literal;
uint32_t literal_parent;
uint32_t literal_depth;
Array(Heredoc) open_heredocs;
} Scanner;
typedef enum {
HEREDOC_SCAN_CONTINUE,
HEREDOC_SCAN_TOKEN,
HEREDOC_SCAN_DEFER,
} HeredocScanResult;
typedef struct {
size_t matched;
TSLexerCheckpoint interpolation_start;
TSLexerCheckpoint interior_token_start;
bool has_interior_token;
} HeredocPrefixMatch;
typedef struct {
bool before_lf;
bool range_boundary;
TSLexerCheckpoint position;
} HeredocCarriageReturn;
static const bool NON_IDENTIFIER_ASCII[0x80] = {
['\0'] = true, ['\n'] = true, ['\r'] = true, ['\t'] = true, [' '] = true, [':'] = true,
[';'] = true, ['`'] = true, ['"'] = true, ['\''] = true, ['@'] = true, ['$'] = true,
['#'] = true, ['.'] = true, [','] = true, ['|'] = true, ['^'] = true, ['&'] = true,
['<'] = true, ['='] = true, ['>'] = true, ['+'] = true, ['-'] = true, ['*'] = true,
['/'] = true, ['\\'] = true, ['%'] = true, ['?'] = true, ['!'] = true, ['~'] = true,
['('] = true, [')'] = true, ['['] = true, [']'] = true, ['{'] = true, ['}'] = true,
};
static const bool GLOBAL_SYMBOL_PUNCTUATION[0x80] = {
['!'] = true, ['@'] = true, ['&'] = true, ['`'] = true, ['\''] = true, ['+'] = true, ['~'] = true,
['='] = true, ['/'] = true, ['\\'] = true, [','] = true, [';'] = true, ['.'] = true, ['<'] = true,
['>'] = true, ['*'] = true, ['$'] = true, ['?'] = true, [':'] = true, ['"'] = true,
};
static const bool PERCENT_PUNCTUATION[0x80] = {
['|'] = true, ['!'] = true, ['#'] = true, ['/'] = true, ['\\'] = true, ['@'] = true, ['$'] = true,
['%'] = true, ['^'] = true, ['&'] = true, ['*'] = true, [')'] = true, [']'] = true, ['}'] = true,
['>'] = true, ['='] = true, ['+'] = true, ['-'] = true, ['~'] = true, ['`'] = true, [','] = true,
['.'] = true, ['?'] = true, [':'] = true, [';'] = true, ['_'] = true, ['"'] = true, ['\''] = true,
};
static const bool BODY_CLOSER_START[0x80] = {
[')'] = true, [']'] = true, ['}'] = true, ['e'] = true, ['i'] = true, ['r'] = true, ['t'] = true, ['w'] = true,
};
static const bool REGEX_OPTION_ASCII[0x80] = {
['e'] = true, ['i'] = true, ['m'] = true, ['n'] = true, ['o'] = true, ['s'] = true, ['u'] = true, ['x'] = true,
};
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 bool at_eof(const TSLexer *lexer) { return lexer->lookahead == 0 && lexer->eof(lexer); }
static inline void scan_symbol_suffix(TSLexer *lexer);
static inline bool scan_global_symbol(TSLexer *lexer);
static inline bool scan_symbol_word(TSLexer *lexer);
static inline bool is_symbol_word_character(int32_t character);
static inline bool is_symbol_word_start(int32_t character);
static uint32_t save_literal(Scanner *scanner);
static void restore_literal(Scanner *scanner, uint32_t frame_id);
static LiteralKey literal_key(const LiteralFrame *frame);
static uint8_t literal_difference(LiteralKey left, LiteralKey right);
static inline uint8_t trailing_zeros(uint32_t bits);
static unsigned literal_bit(LiteralKey key, uint8_t bit);
static inline void trim_open_heredocs(Scanner *scanner, uint32_t keep) {
for (uint32_t i = keep; i < scanner->open_heredocs.size; i++) {
array_delete(&array_get(&scanner->open_heredocs, i)->word);
}
if (keep < scanner->open_heredocs.size) scanner->open_heredocs.size = keep;
}
static inline void reset(Scanner *scanner) {
restore_local_state(&scanner->locals, 0);
scanner->continuation_comments = 0;
restore_literal(scanner, 0);
}
static inline unsigned encode_scanner(Scanner *scanner, char *buffer, unsigned capacity) {
unsigned size = 0;
bool has_literal = scanner->literal_depth > 0;
uint32_t locals = snapshot_local_state(&scanner->locals);
buffer[size++] = (char)(has_literal | (scanner->continuation_comments << 1) | ((locals != 0) << 2));
if (locals) {
memcpy(buffer + size, &locals, sizeof(locals));
size += sizeof(locals);
}
if (has_literal) {
uint32_t frame_id = save_literal(scanner);
memcpy(&buffer[size], &frame_id, sizeof(frame_id));
size += sizeof(frame_id);
}
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 > (UINT16_MAX >> 4) ||
size + 4 + heredoc->word.size > capacity) {
return 0;
}
buffer[size++] = (char)(heredoc->end_word_indentation_allowed | (heredoc->squiggly << 1));
buffer[size++] = (char)(heredoc->allows_interpolation | (heredoc->mid_line << 1));
buffer[size++] = (char)(((heredoc->word.size >> 8) << 4) | (heredoc->finished << 3) | (heredoc->host_line_continued << 2) | (heredoc->newline_consumed << 1) | heredoc->started);
buffer[size++] = (char)(heredoc->word.size & 0xFF);
if (heredoc->word.size > 0) memcpy(&buffer[size], heredoc->word.contents, heredoc->word.size);
size += heredoc->word.size;
}
return size;
}
static inline unsigned serialize(Scanner *scanner, char *buffer) {
unsigned size = encode_scanner(scanner, buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE);
if (size || (!scanner->locals.scope && !scanner->locals.bindings && !scanner->locals.targets &&
!scanner->locals.capture && !scanner->locals.before_binary_operator)) return size;
unsigned capacity = TREE_SITTER_SERIALIZATION_BUFFER_SIZE + sizeof(uint32_t);
char *state = ts_malloc(capacity);
size = encode_scanner(scanner, state, capacity);
if (!size) {
ts_free(state);
return 0;
}
uint32_t prefix = 0;
for (unsigned i = 0; i < size; i++) {
prefix = intern_local_node(&scanner->locals, SNAPSHOT_BYTE, prefix, (uint8_t)state[i]);
}
ts_free(state);
buffer[0] = (char)128;
memcpy(buffer + 1, &prefix, sizeof(prefix));
memcpy(buffer + 1 + sizeof(prefix), &size, sizeof(size));
return 1 + sizeof(prefix) + sizeof(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) {
trim_open_heredocs(scanner, 0);
return;
}
char state[TREE_SITTER_SERIALIZATION_BUFFER_SIZE + sizeof(uint32_t)];
if ((uint8_t)buffer[0] == 128) {
uint32_t prefix;
memcpy(&prefix, buffer + 1, sizeof(prefix));
memcpy(&length, buffer + 1 + sizeof(prefix), sizeof(length));
for (unsigned i = length; i > 0; i--) {
const LocalNode node = *array_get(&scanner->locals.nodes, prefix - 1);
state[i - 1] = (char)node.right;
prefix = node.left;
}
buffer = state;
}
uint8_t flags = buffer[size++];
scanner->continuation_comments = (flags & 2) != 0;
if (flags & 4) {
uint32_t locals;
memcpy(&locals, buffer + size, sizeof(locals));
size += sizeof(locals);
restore_local_state(&scanner->locals, locals);
}
if (flags & 1) {
uint32_t frame_id;
memcpy(&frame_id, &buffer[size], sizeof(frame_id));
size += sizeof(frame_id);
restore_literal(scanner, frame_id);
}
uint8_t open_heredoc_count = buffer[size++];
uint32_t previous_size = scanner->open_heredocs.size;
trim_open_heredocs(scanner, open_heredoc_count);
array_reserve(&scanner->open_heredocs, open_heredoc_count);
scanner->open_heredocs.size = open_heredoc_count;
for (unsigned j = 0; j < open_heredoc_count; j++) {
Heredoc heredoc = {0};
uint8_t indentation_flags = (uint8_t)buffer[size++];
heredoc.end_word_indentation_allowed = (indentation_flags & 1) != 0;
heredoc.squiggly = (indentation_flags & 2) != 0;
uint8_t interpolation_flags = (uint8_t)buffer[size++];
heredoc.allows_interpolation = (interpolation_flags & 1) != 0;
heredoc.mid_line = (interpolation_flags & 2) != 0;
uint8_t state_and_length = (uint8_t)buffer[size++];
heredoc.started = (state_and_length & 1) != 0;
heredoc.newline_consumed = (state_and_length & 2) != 0;
heredoc.host_line_continued = (state_and_length & 4) != 0;
heredoc.finished = (state_and_length & 8) != 0;
heredoc.word = j < previous_size ? scanner->open_heredocs.contents[j].word : (String)array_new();
uint16_t word_length = ((uint16_t)(state_and_length >> 4) << 8) | (uint8_t)buffer[size++];
array_reserve(&heredoc.word, word_length);
if (word_length > 0) memcpy(heredoc.word.contents, &buffer[size], word_length);
heredoc.word.size = word_length;
size += word_length;
scanner->open_heredocs.contents[j] = heredoc;
}
assert(size == length);
}
static void push_literal(Scanner *scanner, Literal literal) {
scanner->literal_parent = scanner->literal_depth ? save_literal(scanner) : 0;
scanner->literal = literal;
scanner->literal_depth++;
}
static void pop_literal(Scanner *scanner) {
restore_literal(scanner, scanner->literal_parent);
}
static uint32_t save_literal(Scanner *scanner) {
LiteralFrame frame = {scanner->literal, scanner->literal_parent, scanner->literal_depth};
LiteralKey key = literal_key(&frame);
uint32_t current = scanner->literal_root;
while (current && !scanner->literal_index.contents[current - 1].frame) {
const LiteralIndexNode *node = &scanner->literal_index.contents[current - 1];
current = node->children[literal_bit(key, node->bit)];
}
uint8_t difference = 0;
if (current) {
uint32_t existing = scanner->literal_index.contents[current - 1].frame;
difference = literal_difference(key, literal_key(&scanner->literal_frames.contents[existing - 1]));
if (difference == UINT8_MAX) return existing;
}
array_push(&scanner->literal_frames, frame);
uint32_t frame_id = scanner->literal_frames.size;
LiteralIndexNode leaf = {{0, 0}, frame_id, 0};
array_push(&scanner->literal_index, leaf);
uint32_t inserted = scanner->literal_index.size;
if (!current) {
scanner->literal_root = inserted;
return frame_id;
}
uint32_t parent = 0;
unsigned direction = 0;
current = scanner->literal_root;
while (!scanner->literal_index.contents[current - 1].frame &&
scanner->literal_index.contents[current - 1].bit < difference) {
parent = current;
const LiteralIndexNode *node = &scanner->literal_index.contents[current - 1];
direction = literal_bit(key, node->bit);
current = node->children[direction];
}
LiteralIndexNode branch = {{0, 0}, 0, difference};
direction = literal_bit(key, difference);
branch.children[direction] = inserted;
branch.children[!direction] = current;
array_push(&scanner->literal_index, branch);
inserted = scanner->literal_index.size;
if (parent) {
const LiteralIndexNode *node = &scanner->literal_index.contents[parent - 1];
direction = literal_bit(key, node->bit);
scanner->literal_index.contents[parent - 1].children[direction] = inserted;
} else {
scanner->literal_root = inserted;
}
return frame_id;
}
static void restore_literal(Scanner *scanner, uint32_t frame_id) {
assert(frame_id <= scanner->literal_frames.size);
if (frame_id == 0) {
scanner->literal = (Literal){0};
scanner->literal_parent = 0;
scanner->literal_depth = 0;
return;
}
const LiteralFrame *frame = array_get(&scanner->literal_frames, frame_id - 1);
scanner->literal = frame->literal;
scanner->literal_parent = frame->parent;
scanner->literal_depth = frame->depth;
}
static LiteralKey literal_key(const LiteralFrame *frame) {
const Literal *literal = &frame->literal;
return (LiteralKey){{frame->parent, frame->depth, (uint32_t)literal->type,
(uint32_t)literal->open_delimiter, (uint32_t)literal->close_delimiter,
(uint32_t)literal->nesting_depth, literal->allows_interpolation | (literal->resume_after_heredoc << 1)}};
}
static uint8_t literal_difference(LiteralKey left, LiteralKey right) {
for (uint8_t word = 0; word < 7; word++) {
uint32_t difference = left.words[word] ^ right.words[word];
if (difference) return (uint8_t)(word * 32 + trailing_zeros(difference));
}
return UINT8_MAX;
}
static inline uint8_t trailing_zeros(uint32_t bits) {
#if defined(__clang__) || defined(__GNUC__)
return (uint8_t)__builtin_ctz(bits);
#else
uint8_t count = 0;
while (!(bits & 1)) {
bits >>= 1;
count++;
}
return count;
#endif
}
static unsigned literal_bit(LiteralKey key, uint8_t bit) {
return (key.words[bit / 32] >> (bit % 32)) & 1;
}
static inline bool scan_literal_resume(Literal *literal, TSLexer *lexer, const bool *valid_symbols);
static inline bool scan_heredoc_host_resume(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols);
static inline void finish_heredoc(Scanner *scanner, TSLexer *lexer, bool can_resume, uint32_t index);
static inline uint32_t first_started_heredoc_index(Scanner *scanner);
static inline bool has_pending_heredoc_body(Scanner *scanner, const bool *valid_symbols);
static inline bool scan_heredoc_after_literal_escape(Scanner *scanner, TSLexer *lexer, bool consume_plain_escape, bool continues_word);
static bool scan_control_newline_operand(TSLexer *lexer);
static inline bool finish_literal_content(TSLexer *lexer);
static inline bool scan_heredoc_after_literal_space(Scanner *scanner, TSLexer *lexer, bool separator_valid);
static inline bool is_horizontal_whitespace(int32_t codepoint);
static inline bool start_literal_heredoc_body(Scanner *scanner, TSLexer *lexer, bool before_newline);
static inline bool start_heredoc_body(Scanner *scanner, TSLexer *lexer, bool before_newline);
static inline bool is_short_interpolation_name_start(int32_t prefix, int32_t codepoint);
static inline bool is_global_symbol_punctuation(int32_t c);
static inline bool heredoc_word_starts_interpolation(const Heredoc *heredoc);
static inline bool scan_heredoc_end_word(Heredoc *heredoc, TSLexer *lexer);
static HeredocPrefixMatch match_heredoc_line_prefix(const Heredoc *heredoc, TSLexer *lexer, size_t matched);
static bool scan_heredoc_prefix_token(Heredoc *heredoc, TSLexer *lexer, size_t matched, bool consumed_cr);
static HeredocScanResult scan_heredoc_line_prefix(Scanner *scanner, TSLexer *lexer, uint32_t current, bool *has_content);
static HeredocScanResult scan_heredoc_interpolation(Heredoc *heredoc, TSLexer *lexer, bool *has_content);
static inline bool finish_heredoc_at_line_end(Scanner *scanner, TSLexer *lexer, bool has_content, uint32_t index, bool can_resume);
static size_t scan_heredoc_indentation(const Heredoc *heredoc, TSLexer *lexer, bool *has_content, HeredocCarriageReturn *cr);
static inline void mark_current_heredoc_position(TSLexer *lexer);
static inline uint32_t match_heredoc_character(const String *word, size_t position, int32_t codepoint);
static inline uint32_t encode_utf8(int32_t codepoint, char *bytes);
static inline void append_heredoc_character(String *word, int32_t codepoint);
static inline bool scan_line_comment(TSLexer *lexer);
static bool scan_comment_line_break(Scanner *scanner, TSLexer *lexer, bool opens_body);
static bool may_close_body(TSLexer *lexer);
static bool scan_closing_keyword(TSLexer *lexer);
static inline bool ends_keyword(int32_t c);
static inline bool is_iden_char(int32_t c);
static inline bool is_space(int32_t c);
static inline bool is_alpha(int32_t c);
static inline bool is_alnum(int32_t c);
static inline bool is_digit(int32_t c);
static inline bool is_lower(int32_t c);
static inline bool is_upper(int32_t c);
static bool scan_after_whitespace(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols, bool crossed_newline, bool crossed_line, bool escaped_space);
static bool scan_continuation_comment(Scanner *scanner, TSLexer *lexer);
static bool scan_endless_equals(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols);
static bool scan_chained_scope_gap(TSLexer *lexer);
static bool scan_argument_line_end(TSLexer *lexer, const bool *valid_symbols);
static bool argument_follows_newline(TSLexer *lexer);
static bool skip_blank_argument_lines(TSLexer *lexer);
static bool scan_clause_keyword(TSLexer *lexer);
enum { KEYWORD_BUFFER_SIZE = 7 };
static bool scan_short_keyword(TSLexer *lexer, char word[KEYWORD_BUFFER_SIZE]);
static bool is_clause_keyword(const char *word);
static bool is_modifier_keyword(const char *word);
static bool scan_splat(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols);
static bool next_word_is_do(TSLexer *lexer);
static bool operand_line_ends_expression(TSLexer *lexer);
static bool scan_word_token(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols, bool rescue_may_end_line);
static bool scan_not_parentheses_gap(TSLexer *lexer);
static inline bool scan_escaped_newline(TSLexer *lexer);
static bool scan_embdoc(TSLexer *lexer);
static bool scan_word_text(TSLexer *lexer, const char *word);
static bool scan_leading_logical_operator(TSLexer *lexer);
static bool scan_line_break(TSLexer *lexer);
static bool scan_host_line_escape(Scanner *scanner, TSLexer *lexer, bool heredoc_body_start_is_valid, bool *escaped_space);
static bool scan_end_marker_line_end(TSLexer *lexer);
static inline bool scan_whitespace(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols,
bool *rescue_may_end_line) {
bool heredoc_body_start_is_valid = has_pending_heredoc_body(scanner, valid_symbols);
bool crossed_newline = false;
bool crossed_line = lexer->is_at_included_range_start(lexer);
bool escaped_space = false;
bool line_break_valid = valid_symbols[LINE_BREAK] || valid_symbols[BODY_LINE_BREAK];
for (;;) {
if (!valid_symbols[NO_LINE_BREAK] && line_break_valid && lexer->is_at_included_range_start(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = LINE_BREAK;
return true;
}
switch (lexer->lookahead) {
case ' ':
case '\t':
case '\f':
case '\v':
skip(scanner, lexer);
break;
case '\r':
if (heredoc_body_start_is_valid) {
return start_heredoc_body(scanner, lexer, true);
} else {
skip(scanner, lexer);
break;
}
case '\n':
crossed_line = true;
if (heredoc_body_start_is_valid) {
return start_heredoc_body(scanner, lexer, true);
} else if (valid_symbols[CONTINUED_ARGUMENT_LINE] && !valid_symbols[STRING_CONTENT]) {
return scan_argument_line_end(lexer, valid_symbols);
} else if (!valid_symbols[NO_LINE_BREAK] && line_break_valid && !crossed_newline) {
lexer->mark_end(lexer);
lexer->advance(lexer, true);
crossed_newline = true;
} else {
*rescue_may_end_line = valid_symbols[NO_LINE_BREAK] && line_break_valid;
skip(scanner, lexer);
}
break;
case '\\':
if (!scan_host_line_escape(scanner, lexer, heredoc_body_start_is_valid, &escaped_space)) return false;
if (lexer->result_symbol == HEREDOC_BODY_START) return true;
break;
default:
if (heredoc_body_start_is_valid && at_eof(lexer)) {
mark_current_heredoc_position(lexer);
return start_heredoc_body(scanner, lexer, true);
}
return scan_after_whitespace(scanner, lexer, valid_symbols, crossed_newline, crossed_line, escaped_space);
}
}
}
static bool scan_argument_line_end(TSLexer *lexer, const bool *valid_symbols) {
TSLexerCheckpoint newline;
lexer->get_checkpoint(lexer, &newline);
if (argument_follows_newline(lexer)) {
lexer->result_symbol = CONTINUED_ARGUMENT_LINE;
} else {
lexer->mark_end_at(lexer, &newline);
lexer->result_symbol = ANONYMOUS_ARGUMENT_END;
}
return valid_symbols[lexer->result_symbol];
}
static bool argument_follows_newline(TSLexer *lexer) {
advance(lexer);
lexer->mark_end(lexer);
if (!skip_blank_argument_lines(lexer)) return true;
if (at_eof(lexer) || lexer->lookahead == ')' || lexer->lookahead == ']' || lexer->lookahead == '}' ||
lexer->lookahead == ',' || lexer->lookahead == ';') return false;
return !scan_clause_keyword(lexer);
}
static bool skip_blank_argument_lines(TSLexer *lexer) {
for (;;) {
while (is_space(lexer->lookahead) && !at_eof(lexer)) advance(lexer);
if (lexer->lookahead == '#') {
scan_line_comment(lexer);
} else if (lexer->lookahead == '=' && lexer->get_column(lexer) == 0) {
if (!scan_embdoc(lexer)) return false;
} else if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead == '\r') advance(lexer);
if (lexer->lookahead != '\n') return false;
} else {
return true;
}
}
}
static bool scan_clause_keyword(TSLexer *lexer) {
char word[KEYWORD_BUFFER_SIZE];
return scan_short_keyword(lexer, word) && is_clause_keyword(word);
}
static bool scan_short_keyword(TSLexer *lexer, char word[KEYWORD_BUFFER_SIZE]) {
memset(word, 0, KEYWORD_BUFFER_SIZE);
for (unsigned i = 0; is_symbol_word_character(lexer->lookahead); i++) {
if (i == KEYWORD_BUFFER_SIZE - 1 || lexer->lookahead > 127) return false;
word[i] = (char)lexer->lookahead;
advance(lexer);
}
return ends_keyword(lexer->lookahead);
}
static bool is_clause_keyword(const char *word) {
return !strcmp(word, "end") || !strcmp(word, "else") || !strcmp(word, "elsif") || !strcmp(word, "ensure") ||
!strcmp(word, "rescue") || !strcmp(word, "when") || !strcmp(word, "in") || !strcmp(word, "do") ||
!strcmp(word, "then") || !strcmp(word, "and") || !strcmp(word, "or");
}
static bool is_modifier_keyword(const char *word) {
return !strcmp(word, "if") || !strcmp(word, "unless") || !strcmp(word, "while") || !strcmp(word, "until");
}
static bool scan_after_whitespace(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols,
bool crossed_newline, bool crossed_line, bool escaped_space) {
if (valid_symbols[CHAINED_SCOPE_GAP] && !valid_symbols[STRING_CONTENT] && scanner->has_leading_whitespace && !crossed_line && lexer->lookahead == ':') {
return scan_chained_scope_gap(lexer);
}
if (lexer->lookahead == '#') {
if (scanner->continuation_comments) return scan_continuation_comment(scanner, lexer);
if (crossed_newline) {
return scan_comment_line_break(scanner, lexer, valid_symbols[BODY_LINE_BREAK] || valid_symbols[BODY_COMMENT]);
}
if (crossed_line && valid_symbols[BODY_COMMENT]) {
scan_line_comment(lexer);
lexer->mark_end(lexer);
lexer->result_symbol = BODY_COMMENT;
return true;
}
} else if (crossed_line && valid_symbols[BODY_COMMENT] && lexer->lookahead == '=' &&
lexer->get_column(lexer) == 0 && scan_embdoc(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = BODY_COMMENT;
return true;
} else if (!crossed_line && !escaped_space && lexer->lookahead == '=' &&
scan_endless_equals(scanner, lexer, valid_symbols)) {
return true;
}
return !crossed_newline || scan_line_break(lexer);
}
static bool scan_chained_scope_gap(TSLexer *lexer) {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead != ':') return false;
advance(lexer);
for (;;) {
while (is_space(lexer->lookahead)) advance(lexer);
if (lexer->lookahead != '\\') break;
if (!scan_escaped_newline(lexer)) return false;
}
if (!is_upper(lexer->lookahead)) return false;
lexer->result_symbol = CHAINED_SCOPE_GAP;
return true;
}
static bool scan_endless_equals(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[STRING_CONTENT]) return false;
TokenType symbol = scanner->has_leading_whitespace && valid_symbols[SPACED_ENDLESS_EQUALS] ? SPACED_ENDLESS_EQUALS
: ENDLESS_EQUALS;
if (!valid_symbols[symbol]) return false;
advance(lexer);
lexer->mark_end(lexer);
leave_local_scope(&scanner->locals);
lexer->result_symbol = symbol;
return true;
}
static bool scan_continuation_comment(Scanner *scanner, TSLexer *lexer) {
bool range_boundary = scan_line_comment(lexer);
TSLexerCheckpoint comment_end;
lexer->get_checkpoint(lexer, &comment_end);
while (!range_boundary && is_space(lexer->lookahead) && !lexer->is_at_included_range_start(lexer)) advance(lexer);
scanner->continuation_comments = !range_boundary && !lexer->is_at_included_range_start(lexer) && lexer->lookahead == '#';
lexer->mark_end_at(lexer, &comment_end);
lexer->result_symbol = LINE_COMMENT;
return true;
}
static bool scan_line_break(TSLexer *lexer) {
switch (lexer->lookahead) {
case '|':
case 'a':
case 'o':
if (scan_leading_logical_operator(lexer)) return false;
break;
case '.':
advance(lexer);
if (at_eof(lexer) || lexer->lookahead != '.') return false;
break;
case '&':
case '#':
return true;
}
lexer->result_symbol = LINE_BREAK;
return true;
}
static bool scan_comment_line_break(Scanner *scanner, TSLexer *lexer, bool opens_body) {
TSLexerCheckpoint comment_start;
TSLexerCheckpoint comment_end;
lexer->get_checkpoint(lexer, &comment_start);
uint32_t comment_count = 0;
do {
bool range_boundary = scan_line_comment(lexer);
if (comment_count++ == 0) lexer->get_checkpoint(lexer, &comment_end);
if (range_boundary) break;
while (is_space(lexer->lookahead) && !lexer->is_at_included_range_start(lexer)) advance(lexer);
} while (lexer->lookahead == '#' && !lexer->is_at_included_range_start(lexer));
bool comment_first = !lexer->is_at_included_range_start(lexer) &&
(!opens_body && may_close_body(lexer)
? scan_closing_keyword(lexer)
: !scan_line_break(lexer) || lexer->result_symbol != LINE_BREAK);
if (comment_first) {
scanner->continuation_comments = comment_count > 1;
lexer->mark_start(lexer, &comment_start);
lexer->mark_end_at(lexer, &comment_end);
lexer->result_symbol = LINE_COMMENT;
} else {
lexer->result_symbol = LINE_BREAK;
}
return true;
}
static bool may_close_body(TSLexer *lexer) {
int32_t c = lexer->lookahead;
return at_eof(lexer) || ((uint32_t)c < 0x80 && BODY_CLOSER_START[c]);
}
static bool scan_closing_keyword(TSLexer *lexer) {
static const char *const KEYWORDS[] = {"end", "else", "elsif", "ensure", "rescue", "when", "in", "then"};
if (at_eof(lexer) || !is_alpha(lexer->lookahead)) return true;
char word[8];
unsigned length = 0;
while (length < sizeof(word) - 1 && lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
word[length++] = (char)lexer->lookahead;
advance(lexer);
}
word[length] = '\0';
if (!ends_keyword(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 bool scan_host_line_escape(Scanner *scanner, TSLexer *lexer, bool heredoc_body_start_is_valid, bool *escaped_space) {
advance(lexer);
if (heredoc_body_start_is_valid && lexer->is_at_included_range_start(lexer)) return false;
if (lexer->lookahead == '\r') {
skip(scanner, lexer);
if (heredoc_body_start_is_valid && lexer->is_at_included_range_start(lexer)) return false;
}
if (!is_space(lexer->lookahead)) return false;
bool starts_body = heredoc_body_start_is_valid && lexer->lookahead == '\n';
*escaped_space |= lexer->lookahead != '\n';
skip(scanner, lexer);
if (!starts_body) return true;
array_get(&scanner->open_heredocs, 0)->host_line_continued = true;
mark_current_heredoc_position(lexer);
return start_heredoc_body(scanner, lexer, false);
}
static bool scan_leading_logical_operator(TSLexer *lexer) {
if (lexer->lookahead == '|') {
advance(lexer);
return lexer->lookahead == '|';
}
if (!scan_word_text(lexer, lexer->lookahead == 'a' ? "and" : "or")) return false;
return lexer->lookahead < 0x80 && !is_alnum(lexer->lookahead) && lexer->lookahead != '_' &&
lexer->lookahead != '?' && lexer->lookahead != '!';
}
static inline bool scan_operator(TSLexer *lexer) {
static const struct {
const char *name;
bool allowed;
} operators[] = {
{"|", true}, {"^", true}, {"&", true}, {"<=>", true}, {"==", true},
{"===", true}, {"=~", true}, {">", true}, {">=", true}, {"<", true},
{"<=", true}, {"+", true}, {"!=", true}, {"-", true}, {"*", true},
{"/", true}, {"%", true}, {"!", true}, {"!@", true}, {"!~", true},
{"**", true}, {"<<", true}, {">>", true}, {"~", true}, {"+@", true},
{"-@", true}, {"~@", true}, {"[]", true}, {"`", true}, {"[]=", true},
{"|=", false}, {"^=", false}, {"&=", false}, {"*=", false}, {"/=", false},
{"%=", false}, {"**=", false}, {"<<=", false}, {">>=", false}, {"&&", false},
{"||", false},
};
char prefix[3];
bool complete = false;
for (unsigned length = 0; length < sizeof(prefix); length++) {
if (lexer->lookahead <= 0 || lexer->lookahead > 0x7f) return complete;
prefix[length] = (char)lexer->lookahead;
bool found = false;
bool next_complete = false;
for (unsigned index = 0; index < sizeof(operators) / sizeof(operators[0]); index++) {
if (strncmp(operators[index].name, prefix, length + 1) == 0) {
found = true;
next_complete |= operators[index].allowed && operators[index].name[length + 1] == '\0';
}
}
if (!found) return complete;
advance(lexer);
complete = next_complete;
if (complete) lexer->mark_end(lexer);
}
return complete;
}
static inline bool ends_keyword(int32_t c) { return !is_iden_char(c) && c != '?' && c != '!'; }
static inline bool scan_symbol_identifier(TSLexer *lexer) {
if (lexer->lookahead == '$') return scan_global_symbol(lexer);
if (lexer->lookahead == '@') {
advance(lexer);
if (lexer->lookahead == '@') advance(lexer);
return scan_symbol_word(lexer);
}
if (!is_symbol_word_start(lexer->lookahead)) return scan_operator(lexer);
scan_symbol_word(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead == '!' || lexer->lookahead == '?' || lexer->lookahead == '=') {
scan_symbol_suffix(lexer);
}
return true;
}
static inline void scan_symbol_suffix(TSLexer *lexer) {
int32_t suffix = lexer->lookahead;
advance(lexer);
if (suffix != '=') {
if (lexer->lookahead != '=') lexer->mark_end(lexer);
} else 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_global_symbol(TSLexer *lexer) {
advance(lexer);
if (lexer->lookahead == '-') {
advance(lexer);
if (!is_symbol_word_character(lexer->lookahead)) return false;
advance(lexer);
return true;
}
if (lexer->lookahead >= '0' && lexer->lookahead <= '9') {
bool zero = lexer->lookahead == '0';
advance(lexer);
if (!zero) {
while (lexer->lookahead >= '0' && lexer->lookahead <= '9') advance(lexer);
}
return true;
}
if (is_global_symbol_punctuation(lexer->lookahead)) {
advance(lexer);
return true;
}
return scan_symbol_word(lexer);
}
static inline bool scan_symbol_word(TSLexer *lexer) {
if (!is_symbol_word_start(lexer->lookahead)) return false;
do {
advance(lexer);
} while (is_symbol_word_character(lexer->lookahead));
return true;
}
static inline bool expects_literal_operand(const bool *valid_symbols) {
return (valid_symbols[NO_LINE_BREAK] || valid_symbols[JUMP_OPERAND]) && !valid_symbols[STRING_CONTENT];
}
typedef enum {
SPACED_LITERAL_CLOSED,
SPACED_LITERAL_UNREADABLE,
SPACED_LITERAL_UNTERMINATED,
} SpacedLiteralEnd;
typedef Array(int32_t) RegexpPattern;
static bool regexp_starts_interpolation(TSLexer *lexer, const Literal *literal, RegexpPattern *pattern);
static void record_regexp_match(Scanner *scanner, TSLexer *lexer, const RegexpPattern *pattern, bool extended);
static bool skip_operand_gap(TSLexer *lexer);
static uint32_t target_list_end(TSLexer *lexer, bool first_target);
static uint32_t lexer_byte_offset(TSLexer *lexer);
static bool skip_balanced_target_suffix(TSLexer *lexer, unsigned *budget);
static SpacedLiteralEnd skip_spaced_percent_literal(Scanner *scanner, TSLexer *lexer, const Literal *literal, RegexpPattern *pattern, bool *interpolated);
static bool starts_symbol_operand(TSLexer *lexer);
static bool scan_command_continuation_keyword(TSLexer *lexer);
static inline bool is_spaced_percent_delimiter(int32_t delimiter) {
return delimiter == ' ' || delimiter == '\t' || delimiter == '\r';
}
static inline int32_t percent_close_delimiter(int32_t delimiter, bool binary_operator_valid, bool spaced_literal_prefix) {
switch (delimiter) {
case '(':
return ')';
case '[':
return ']';
case '{':
return '}';
case '<':
return '>';
case '\n':
return binary_operator_valid ? 0 : delimiter;
case '\r':
case ' ':
case '\t':
return binary_operator_valid && !spaced_literal_prefix ? 0 : delimiter;
default:
return (uint32_t)delimiter < 0x80 && PERCENT_PUNCTUATION[delimiter] ? delimiter : 0;
}
}
static bool spaced_percent_literal_ends_argument(Scanner *scanner, TSLexer *lexer, const Literal *literal, RegexpPattern *pattern) {
bool interpolated = false;
bool extended = false;
lexer->mark_end(lexer);
switch (skip_spaced_percent_literal(scanner, lexer, literal, pattern, &interpolated)) {
case SPACED_LITERAL_UNREADABLE:
return true;
case SPACED_LITERAL_UNTERMINATED:
return false;
case SPACED_LITERAL_CLOSED:
break;
}
if (literal->type == REGEX_START) {
while (is_symbol_word_character(lexer->lookahead)) {
if ((uint32_t)lexer->lookahead >= 0x80 || !REGEX_OPTION_ASCII[lexer->lookahead]) return false;
extended |= lexer->lookahead == 'x';
advance(lexer);
}
}
if (pattern && !interpolated) {
if (!skip_operand_gap(lexer)) return true;
} else {
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') advance(lexer);
}
if (lexer->lookahead == '=' && pattern && !interpolated) {
record_regexp_match(scanner, lexer, pattern, extended);
return true;
}
if (is_symbol_word_character(lexer->lookahead)) return scan_command_continuation_keyword(lexer);
return !starts_symbol_operand(lexer);
}
static bool starts_symbol_operand(TSLexer *lexer) {
switch (lexer->lookahead) {
case '@':
case '$':
case '(':
case '~':
return true;
case '!':
advance(lexer);
return lexer->lookahead != '=' && lexer->lookahead != '~';
case ':':
advance(lexer);
return lexer->lookahead != ':' && !is_space(lexer->lookahead) && !at_eof(lexer);
default:
return false;
}
}
static SpacedLiteralEnd skip_spaced_percent_literal(Scanner *scanner, TSLexer *lexer, const Literal *literal, RegexpPattern *pattern, bool *interpolated) {
const SpacedLiteralEnd line_break = scanner->open_heredocs.size > 0 ? SPACED_LITERAL_UNREADABLE : SPACED_LITERAL_UNTERMINATED;
while (lexer->lookahead != literal->close_delimiter) {
if (at_eof(lexer)) return SPACED_LITERAL_UNTERMINATED;
if (lexer->lookahead == '\n' || lexer->lookahead == '\r') return line_break;
int32_t character = lexer->lookahead;
if (pattern) array_push(pattern, character);
advance(lexer);
if (character == '\\' && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
character = lexer->lookahead;
if (pattern) array_push(pattern, character);
advance(lexer);
if (character == 'u' && literal->allows_interpolation && lexer->lookahead == '{') return SPACED_LITERAL_UNREADABLE;
} else if (character == '#' && literal->allows_interpolation) {
if (lexer->lookahead == '{') return SPACED_LITERAL_UNREADABLE;
if (pattern && regexp_starts_interpolation(lexer, literal, pattern)) *interpolated = true;
}
}
advance(lexer);
return SPACED_LITERAL_CLOSED;
}
static bool scan_command_continuation_keyword(TSLexer *lexer) {
static const char *const keywords[] = {"and", "do", "else", "elsif", "end", "ensure", "if", "in",
"or", "rescue", "then", "unless", "until", "when", "while"};
char word[7] = {0};
for (unsigned length = 0; is_symbol_word_character(lexer->lookahead); length++) {
if (length == sizeof(word) - 1 || lexer->lookahead > 0x7f) return false;
word[length] = (char)lexer->lookahead;
advance(lexer);
}
if (lexer->lookahead == '!' || lexer->lookahead == '?') {
advance(lexer);
if (lexer->lookahead != '=') return false;
}
for (unsigned i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
if (strcmp(word, keywords[i]) == 0) return true;
}
return false;
}
static bool skip_operand_gap(TSLexer *lexer) {
for (;;) {
if (is_horizontal_whitespace(lexer->lookahead)) {
advance(lexer);
} else if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead == '\r') advance(lexer);
if (lexer->lookahead != '\n') return false;
advance(lexer);
} else {
return true;
}
}
}
#include "regexp_captures.h"
static bool known_binary_operator(const Scanner *scanner, TSLexer *lexer) {
if (!scanner->locals.before_binary_operator) return false;
TSLexerPosition position;
lexer->get_position(lexer, &position);
return position.bytes == scanner->locals.binary_operator_position;
}
static inline bool scan_literal_start_or_assignment(Scanner *scanner, TSLexer *lexer, Literal *literal, const bool *valid_symbols) {
const bool binary_operator_valid =
valid_symbols[FORWARD_SLASH] && !expects_literal_operand(valid_symbols);
switch (lexer->lookahead) {
case '"':
literal->type = valid_symbols[STRING_START] ? STRING_START : CHAINED_STRING_START;
literal->open_delimiter = literal->close_delimiter = lexer->lookahead;
literal->allows_interpolation = true;
advance(lexer);
return true;
case '\'':
literal->type = valid_symbols[STRING_START] ? STRING_START : CHAINED_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 (binary_operator_valid) {
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;
}
}
if (!valid_symbols[STRONG_REGEXP_OPERAND]) scan_regexp_captures(scanner, lexer, literal);
return true;
case '%': {
bool modulo = known_binary_operator(scanner, lexer);
if (!valid_symbols[STRING_START] && valid_symbols[CHAINED_STRING_START]) return false;
advance(lexer);
if (valid_symbols[MODULO_ASSIGNMENT] && lexer->lookahead == '=') {
advance(lexer);
literal->type = MODULO_ASSIGNMENT;
return true;
}
if (modulo) return false;
static const struct {
int32_t prefix;
TokenType type;
bool allows_interpolation;
} kinds[] = {
{'s', SYMBOL_START, false},
{'r', REGEX_START, true},
{'x', SUBSHELL_START, true},
{'q', STRING_START, false},
{'Q', STRING_START, true},
{'w', STRING_ARRAY_START, false},
{'W', STRING_ARRAY_START, true},
{'i', SYMBOL_ARRAY_START, false},
{'I', SYMBOL_ARRAY_START, true},
};
literal->type = STRING_START;
literal->allows_interpolation = true;
bool spaced_literal_prefix = false;
for (unsigned i = 0; i < sizeof(kinds) / sizeof(kinds[0]); i++) {
if (lexer->lookahead == kinds[i].prefix) {
literal->type = kinds[i].type;
literal->allows_interpolation = kinds[i].allows_interpolation;
spaced_literal_prefix = kinds[i].type != STRING_ARRAY_START && kinds[i].type != SYMBOL_ARRAY_START;
advance(lexer);
break;
}
}
TokenType required_token = literal->type == SYMBOL_START ? SIMPLE_SYMBOL : literal->type;
if (!valid_symbols[required_token]) {
return false;
}
literal->open_delimiter = lexer->lookahead;
literal->close_delimiter = percent_close_delimiter(
lexer->lookahead, binary_operator_valid, spaced_literal_prefix && scanner->has_leading_whitespace);
if (literal->close_delimiter == 0) {
return false;
}
advance(lexer);
if (literal->close_delimiter == '\r' && lexer->lookahead == '\n' &&
!lexer->is_at_included_range_start(lexer)) {
if (binary_operator_valid || (valid_symbols[JUMP_OPERAND] && literal->type == REGEX_START)) return false;
literal->open_delimiter = literal->close_delimiter = '\n';
advance(lexer);
}
if (binary_operator_valid && is_spaced_percent_delimiter(literal->close_delimiter)) {
RegexpPattern pattern = array_new();
bool collect = literal->type == REGEX_START && !valid_symbols[STRONG_REGEXP_OPERAND];
bool accepted = spaced_percent_literal_ends_argument(scanner, lexer, literal, collect ? &pattern : NULL);
array_delete(&pattern);
return accepted;
}
if (literal->type == REGEX_START && !valid_symbols[STRONG_REGEXP_OPERAND]) scan_regexp_captures(scanner, lexer, literal);
return true;
}
default:
return false;
}
}
static inline bool scan_heredoc_word(TSLexer *lexer, Heredoc *heredoc) {
String *word = &heredoc->word;
int32_t quote = 0;
switch (lexer->lookahead) {
case '\'':
case '"':
case '`':
quote = lexer->lookahead;
advance(lexer);
while (lexer->lookahead != quote && !at_eof(lexer)) {
append_heredoc_character(word, lexer->lookahead);
advance(lexer);
}
if (at_eof(lexer)) return false;
advance(lexer);
break;
default:
while (is_symbol_word_character(lexer->lookahead)) {
append_heredoc_character(word, lexer->lookahead);
advance(lexer);
}
break;
}
heredoc->allows_interpolation = quote != '\'';
return quote != 0 || word->size != 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);
if (start == '@' && lexer->lookahead == '@') advance(lexer);
if (start == '$' && lexer->lookahead == '-') {
start = '-';
advance(lexer);
}
bool is_short_interpolation = is_short_interpolation_name_start(start, 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, const bool *valid_symbols) {
uint32_t current = first_started_heredoc_index(scanner);
if (current == scanner->open_heredocs.size) current = 0;
Heredoc *heredoc = array_get(&scanner->open_heredocs, current);
bool look_for_heredoc_end = !heredoc->mid_line ||
(!heredoc->squiggly && heredoc_word_starts_interpolation(heredoc) && lexer->get_column(lexer) == 0);
bool has_content = false;
for (;;) {
if (look_for_heredoc_end) {
HeredocScanResult result = scan_heredoc_line_prefix(scanner, lexer, current, &has_content);
if (result != HEREDOC_SCAN_CONTINUE) return result == HEREDOC_SCAN_TOKEN;
look_for_heredoc_end = false;
}
if (at_eof(lexer)) {
heredoc->mid_line = true;
lexer->mark_end(lexer);
lexer->result_symbol = HEREDOC_CONTENT;
return has_content;
}
if (heredoc->allows_interpolation && (lexer->lookahead == '#' || lexer->lookahead == '\\')) {
HeredocScanResult result = scan_heredoc_interpolation(heredoc, lexer, &has_content);
if (result != HEREDOC_SCAN_CONTINUE) return result == HEREDOC_SCAN_TOKEN;
continue;
}
bool nested_body = current > 0 && valid_symbols[HEREDOC_BODY_START];
bool carriage_return = nested_body && lexer->lookahead == '\r';
if (carriage_return) {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead != '\n' || lexer->is_at_included_range_start(lexer)) {
has_content = true;
continue;
}
}
bool line_end = lexer->lookahead == '\n';
if (line_end && nested_body) {
if (!carriage_return) lexer->mark_end(lexer);
lexer->result_symbol = HEREDOC_CONTENT;
return has_content || start_heredoc_body(scanner, lexer, true);
}
advance(lexer);
if (line_end) {
look_for_heredoc_end = true;
lexer->mark_end(lexer);
}
has_content = true;
}
}
static inline void mark_before_line_ending(TSLexer *lexer, const HeredocCarriageReturn *cr) {
if (cr->before_lf) lexer->mark_end_at(lexer, &cr->position);
else lexer->mark_end(lexer);
}
static HeredocScanResult scan_heredoc_line_prefix(Scanner *scanner, TSLexer *lexer, uint32_t current, bool *has_content) {
Heredoc *heredoc = array_get(&scanner->open_heredocs, current);
HeredocCarriageReturn cr = {0};
size_t matched = scan_heredoc_indentation(heredoc, lexer, has_content, &cr);
HeredocPrefixMatch prefix = match_heredoc_line_prefix(heredoc, lexer, matched);
bool has_label_line = !at_eof(lexer) || lexer->get_column(lexer) > 0;
bool consumed_cr = false;
if (has_label_line && prefix.matched == heredoc->word.size) {
consumed_cr = lexer->lookahead == '\r';
if (!*has_content) mark_before_line_ending(lexer, &cr);
if (finish_heredoc_at_line_end(scanner, lexer, *has_content, current, !cr.range_boundary)) return HEREDOC_SCAN_TOKEN;
if (consumed_cr && !prefix.matched) {
*has_content = true;
lexer->mark_end(lexer);
}
}
if (scan_heredoc_prefix_token(heredoc, lexer, prefix.matched, consumed_cr)) {
heredoc->mid_line = true;
if (*has_content) {
lexer->result_symbol = HEREDOC_CONTENT;
return HEREDOC_SCAN_TOKEN;
}
if (lexer->result_symbol != SHORT_INTERPOLATION) return HEREDOC_SCAN_DEFER;
lexer->mark_end_at(lexer, &prefix.interpolation_start);
return HEREDOC_SCAN_TOKEN;
}
if (prefix.has_interior_token) {
heredoc->mid_line = true;
lexer->mark_end_at(lexer, &prefix.interior_token_start);
lexer->result_symbol = HEREDOC_CONTENT;
return HEREDOC_SCAN_TOKEN;
}
if (prefix.matched > 0) {
*has_content = true;
lexer->mark_end(lexer);
}
return HEREDOC_SCAN_CONTINUE;
}
static HeredocPrefixMatch match_heredoc_line_prefix(const Heredoc *heredoc, TSLexer *lexer, size_t matched) {
HeredocPrefixMatch match = {.matched = matched};
while (match.matched < heredoc->word.size && !at_eof(lexer)) {
if (!match.has_interior_token && match.matched > 0 && heredoc->allows_interpolation &&
(lexer->lookahead == '#' || lexer->lookahead == '\\')) {
lexer->get_checkpoint(lexer, &match.interior_token_start);
match.has_interior_token = true;
}
uint32_t length = match_heredoc_character(&heredoc->word, match.matched, lexer->lookahead);
if (length == 0) break;
advance(lexer);
match.matched += length;
if (match.matched == 1 && heredoc->word.contents[0] == '#') {
lexer->get_checkpoint(lexer, &match.interpolation_start);
}
}
return match;
}
static bool scan_heredoc_prefix_token(Heredoc *heredoc, TSLexer *lexer, size_t matched, bool consumed_cr) {
if (!heredoc->allows_interpolation || matched == 0) return false;
if (heredoc->word.contents[0] == '\\') {
lexer->result_symbol = NONE;
return true;
}
if (heredoc->word.contents[0] != '#') return false;
int32_t prefix = matched > 1 ? (unsigned char)heredoc->word.contents[1] : (consumed_cr ? '\r' : lexer->lookahead);
if (prefix == '{') {
lexer->result_symbol = NONE;
return true;
}
if (prefix != '@' && prefix != '$') return false;
if (matched == 1) advance(lexer);
int32_t character = matched > 2 ? (unsigned char)heredoc->word.contents[2] : (consumed_cr ? '\r' : lexer->lookahead);
bool extended = (prefix == '@' && character == '@') || (prefix == '$' && character == '-');
if (extended && matched <= 2) advance(lexer);
if (extended && prefix == '$') prefix = '-';
size_t name_position = extended ? 3 : 2;
character = matched > name_position ? (unsigned char)heredoc->word.contents[name_position] : (consumed_cr ? '\r' : lexer->lookahead);
if (!is_short_interpolation_name_start(prefix, character)) return false;
lexer->result_symbol = SHORT_INTERPOLATION;
return true;
}
static HeredocScanResult scan_heredoc_interpolation(Heredoc *heredoc, TSLexer *lexer, bool *has_content) {
if (lexer->lookahead == '\\') {
if (!*has_content) return HEREDOC_SCAN_DEFER;
lexer->mark_end(lexer);
heredoc->mid_line = true;
lexer->result_symbol = HEREDOC_CONTENT;
return HEREDOC_SCAN_TOKEN;
}
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == '{') {
if (!*has_content) return HEREDOC_SCAN_DEFER;
heredoc->mid_line = true;
lexer->result_symbol = HEREDOC_CONTENT;
return HEREDOC_SCAN_TOKEN;
}
heredoc->mid_line = true;
if (scan_short_interpolation(lexer, *has_content, HEREDOC_CONTENT)) return HEREDOC_SCAN_TOKEN;
*has_content = true;
lexer->mark_end(lexer);
return HEREDOC_SCAN_CONTINUE;
}
static inline bool finish_heredoc_at_line_end(Scanner *scanner, TSLexer *lexer, bool has_content, uint32_t index, bool can_resume) {
can_resume &= !lexer->is_at_included_range_start(lexer) && !at_eof(lexer);
if (lexer->lookahead == '\r') {
advance(lexer);
if (lexer->lookahead != '\n') return false;
can_resume &= !lexer->is_at_included_range_start(lexer);
} else if (lexer->lookahead != '\n' && !at_eof(lexer)) {
return false;
}
if (has_content) {
array_get(&scanner->open_heredocs, index)->mid_line = false;
lexer->result_symbol = HEREDOC_CONTENT;
} else {
finish_heredoc(scanner, lexer, can_resume, index);
}
return true;
}
static size_t heredoc_label_whitespace(const String *word, const String *line) {
size_t prefix = 0;
while (prefix < word->size && prefix < line->size && is_horizontal_whitespace((unsigned char)word->contents[prefix])) {
prefix++;
}
if (!prefix || memcmp(line->contents + line->size - prefix, word->contents, prefix) != 0) return 0;
return prefix;
}
static size_t scan_heredoc_indentation(const Heredoc *heredoc, TSLexer *lexer, bool *has_content, HeredocCarriageReturn *cr) {
if (!heredoc->end_word_indentation_allowed || !is_horizontal_whitespace(lexer->lookahead)) return 0;
bool label_starts_with_space = heredoc->word.size && is_horizontal_whitespace((unsigned char)heredoc->word.contents[0]);
String whitespace = array_new();
Array(TSLexerCheckpoint) positions = array_new();
uint32_t count = 0;
while (is_horizontal_whitespace(lexer->lookahead)) {
int32_t character = lexer->lookahead;
TSLexerCheckpoint position;
if (label_starts_with_space || character == '\r') lexer->get_checkpoint(lexer, &position);
bool boundary = character == '\r' && lexer->is_at_included_range_start(lexer);
advance(lexer);
if (character == '\r' && lexer->lookahead == '\n') {
cr->before_lf = true;
cr->position = position;
cr->range_boundary = boundary;
break;
}
count++;
if (label_starts_with_space) {
array_push(&positions, position);
array_push(&whitespace, (char)character);
}
}
size_t prefix = heredoc_label_whitespace(&heredoc->word, &whitespace);
size_t indentation = count - prefix;
if (indentation != 0) {
if (prefix) lexer->mark_end_at(lexer, array_get(&positions, indentation));
else mark_before_line_ending(lexer, cr);
*has_content = true;
}
array_delete(&positions);
array_delete(&whitespace);
return prefix;
}
static inline bool heredoc_word_starts_interpolation(const Heredoc *heredoc) {
if (heredoc->word.size < 2 || heredoc->word.contents[0] != '#') return false;
const unsigned char *word = (const unsigned char *)heredoc->word.contents;
if (word[1] == '{') return true;
int32_t prefix = word[1];
if (prefix != '@' && prefix != '$') return false;
size_t index = 2;
if (index < heredoc->word.size && prefix == '@' && word[index] == '@') index++;
if (index < heredoc->word.size && prefix == '$' && word[index] == '-') {
prefix = '-';
index++;
}
return index < heredoc->word.size && is_short_interpolation_name_start(prefix, word[index]);
}
static inline bool is_short_interpolation_name_start(int32_t prefix, int32_t codepoint) {
if (prefix == '@') return is_iden_char(codepoint) && !is_digit(codepoint);
if (prefix == '-') return is_symbol_word_start(codepoint);
return is_symbol_word_character(codepoint) || is_global_symbol_punctuation(codepoint);
}
static inline bool scan_literal_content(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
Literal *literal = &scanner->literal;
bool has_content = false;
bool stop_on_space = literal->type == SYMBOL_ARRAY_START || literal->type == STRING_ARRAY_START;
bool pending_heredoc = has_pending_heredoc_body(scanner, valid_symbols);
if (literal->resume_after_heredoc &&
(scanner->open_heredocs.size == 0 || array_get(&scanner->open_heredocs, 0)->started)) {
return scan_literal_resume(literal, lexer, valid_symbols);
}
for (;;) {
if (pending_heredoc && stop_on_space && !has_content && lexer->lookahead != literal->close_delimiter &&
is_horizontal_whitespace(lexer->lookahead)) {
return scan_heredoc_after_literal_space(scanner, lexer, valid_symbols[WORD_SEPARATOR]);
}
if (pending_heredoc &&
(literal->resume_after_heredoc ||
(literal->close_delimiter != '\n' && lexer->lookahead != literal->close_delimiter)) &&
(lexer->lookahead == '\n' || lexer->lookahead == '\r')) {
lexer->mark_end(lexer);
if (has_content) {
lexer->result_symbol = STRING_CONTENT;
return true;
}
TSLexerCheckpoint body_start;
lexer->get_checkpoint(lexer, &body_start);
if (lexer->lookahead == '\r') {
advance(lexer);
if (lexer->lookahead != '\n' || lexer->is_at_included_range_start(lexer)) {
return finish_literal_content(lexer);
}
}
lexer->mark_start(lexer, &body_start);
lexer->mark_end_at(lexer, &body_start);
return start_literal_heredoc_body(scanner, lexer, true);
}
if (stop_on_space && is_space(lexer->lookahead)) {
if (has_content) {
return finish_literal_content(lexer);
}
if (valid_symbols[WORD_SEPARATOR]) {
do { advance(lexer); } while (is_space(lexer->lookahead));
lexer->mark_end(lexer);
lexer->result_symbol = WORD_SEPARATOR;
return true;
}
return false;
}
if (literal->close_delimiter == '\n' && lexer->lookahead == '\r') {
TSLexerCheckpoint closing_start;
lexer->get_checkpoint(lexer, &closing_start);
advance(lexer);
if (lexer->lookahead != '\n' || lexer->is_at_included_range_start(lexer)) {
has_content = true;
continue;
}
if (has_content) {
lexer->mark_end_at(lexer, &closing_start);
lexer->result_symbol = STRING_CONTENT;
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 {
bool newline_consumed = pending_heredoc && literal->close_delimiter == '\n';
advance(lexer);
if (literal->type == REGEX_START && !newline_consumed) {
while (is_lower(lexer->lookahead)) {
advance(lexer);
}
}
pop_literal(scanner);
if (newline_consumed) array_get(&scanner->open_heredocs, 0)->newline_consumed = true;
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 (has_content && (literal->allows_interpolation || pending_heredoc)) {
return finish_literal_content(lexer);
}
if (pending_heredoc) {
return scan_heredoc_after_literal_escape(scanner, lexer, !literal->allows_interpolation, stop_on_space);
}
if (literal->allows_interpolation) return false;
advance(lexer);
if (!lexer->is_at_included_range_start(lexer)) {
bool carriage_return = lexer->lookahead == '\r';
advance(lexer);
if (carriage_return && lexer->lookahead == '\n' && !lexer->is_at_included_range_start(lexer)) advance(lexer);
}
} else if (at_eof(lexer)) {
return false;
} else {
advance(lexer);
}
has_content = true;
}
}
static bool accepts_local_identifier(const bool *valid_symbols) {
return !valid_symbols[STRING_CONTENT] &&
(valid_symbols[HASH_CAPTURE] || valid_symbols[UNBRACED_HASH_CAPTURE] || valid_symbols[PARAMETER_IDENTIFIER] || valid_symbols[BINDING_IDENTIFIER] ||
valid_symbols[FIRST_TARGET] || valid_symbols[NEXT_TARGET] || valid_symbols[LOCAL_IDENTIFIER]);
}
static bool emit_scope_end(Scanner *scanner, TSLexer *lexer, TokenType token) {
leave_local_scope(&scanner->locals);
lexer->result_symbol = token;
return true;
}
static bool method_name_ends_here(TSLexer *lexer) {
if (lexer->lookahead == '=') return false;
lexer->mark_end(lexer);
bool newline = false;
for (;;) {
if (!skip_operand_gap(lexer)) return true;
if (lexer->lookahead == '#') {
while (lexer->lookahead != '\n' && !at_eof(lexer)) advance(lexer);
}
if (lexer->lookahead != '\n') break;
newline = true;
advance(lexer);
}
if (lexer->lookahead != '.') return lexer->lookahead != ':' || newline;
advance(lexer);
return lexer->lookahead == '.';
}
static bool in_capture_match(const Scanner *scanner) {
return scanner->locals.scope && is_match_scope(array_get(&scanner->locals.nodes, scanner->locals.scope - 1)->kind);
}
static bool in_defined_capture_match(const Scanner *scanner) {
enum LocalNodeKind kind = array_get(&scanner->locals.nodes, scanner->locals.scope - 1)->kind;
return kind == DEFINED_MATCH_SCOPE || kind == BLOCK_DEFINED_MATCH_SCOPE;
}
static TokenType capture_match_end(const Scanner *scanner, const bool *valid_symbols) {
if (!in_capture_match(scanner)) return NONE;
if (valid_symbols[LEAVE_SCOPE]) return LEAVE_SCOPE;
return valid_symbols[LEAVE_ASSIGNED_MATCH] ? LEAVE_ASSIGNED_MATCH : NONE;
}
static bool newline_ends_expression(TSLexer *lexer, bool operand_may_follow) {
if (lexer->lookahead != '\n') return false;
lexer->mark_end(lexer);
do {
while (lexer->lookahead == '\n' || is_horizontal_whitespace(lexer->lookahead)) advance(lexer);
if (lexer->lookahead != '#') break;
scan_line_comment(lexer);
} while (!at_eof(lexer));
if (operand_may_follow) return operand_line_ends_expression(lexer);
int32_t first = lexer->lookahead;
if (first != '.' && first != '&' && first != '|') return true;
advance(lexer);
if (first == '.') return lexer->lookahead == '.';
return first != '&' || lexer->lookahead != '.';
}
static bool operand_line_ends_expression(TSLexer *lexer) {
if (!skip_blank_argument_lines(lexer)) return false;
if (at_eof(lexer) || lexer->lookahead == ')' || lexer->lookahead == ']' || lexer->lookahead == '}' ||
lexer->lookahead == ',' || lexer->lookahead == ';') return true;
return scan_clause_keyword(lexer);
}
static bool scan_scope_marker(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[STRING_CONTENT] && valid_symbols[STRING_START]) return false;
if (valid_symbols[COMMIT_TARGETS]) {
TSLexerPosition position;
lexer->get_position(lexer, &position);
commit_local_targets(&scanner->locals, position.bytes);
lexer->result_symbol = COMMIT_TARGETS;
return true;
}
if (valid_symbols[METHOD_SCOPE] && !valid_symbols[STRING_CONTENT] && method_name_ends_here(lexer)) {
enter_local_scope(&scanner->locals, false);
lexer->result_symbol = METHOD_SCOPE;
return true;
}
if (valid_symbols[CLASS_BODY_SCOPE]) {
lexer->mark_end(lexer);
if (skip_operand_gap(lexer) && (lexer->lookahead == '\n' || lexer->lookahead == ';')) {
enter_local_scope(&scanner->locals, false);
lexer->result_symbol = CLASS_BODY_SCOPE;
return true;
}
}
if (valid_symbols[PUBLISH_LOOP_MATCH] && in_capture_match(scanner)) {
return emit_scope_end(scanner, lexer, PUBLISH_LOOP_MATCH);
}
if (valid_symbols[LOCAL_SCOPE] || valid_symbols[BLOCK_SCOPE]) {
enter_local_scope(&scanner->locals, valid_symbols[BLOCK_SCOPE]);
lexer->result_symbol = valid_symbols[BLOCK_SCOPE] ? BLOCK_SCOPE : LOCAL_SCOPE;
return true;
}
if (valid_symbols[LEAVE_SCOPE] && !in_capture_match(scanner)) return emit_scope_end(scanner, lexer, LEAVE_SCOPE);
return false;
}
static bool scan_ampersand_tail(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (lexer->lookahead == '&' || lexer->lookahead == '.' || lexer->lookahead == '=') return false;
if (valid_symbols[BLOCK_AMPERSAND] && (!valid_symbols[BITWISE_AND] ||
(scanner->has_leading_whitespace && !is_space(lexer->lookahead)))) {
lexer->result_symbol = BLOCK_AMPERSAND;
return true;
}
if (!valid_symbols[BITWISE_AND]) return false;
lexer->result_symbol = BITWISE_AND;
return true;
}
static inline bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
scanner->has_leading_whitespace = false;
bool host_line_resumes = scanner->open_heredocs.size > 0 && array_get(&scanner->open_heredocs, 0)->finished;
bool line_end_ahead = !host_line_resumes && (lexer->lookahead == '\r' || lexer->lookahead == '\n' ||
is_horizontal_whitespace(lexer->lookahead));
TokenType match_end = capture_match_end(scanner, valid_symbols);
if (line_end_ahead && !valid_symbols[STRING_CONTENT] && !has_pending_heredoc_body(scanner, valid_symbols) &&
match_end != NONE) {
while (lexer->lookahead == '\r' || is_horizontal_whitespace(lexer->lookahead)) skip(scanner, lexer);
if (lexer->lookahead == '\n') {
return newline_ends_expression(lexer, valid_symbols[NO_LINE_BREAK]) && emit_scope_end(scanner, lexer, match_end);
}
}
if (scan_scope_marker(scanner, lexer, valid_symbols)) return true;
bool raw_data = valid_symbols[UNINTERPRETED] && !valid_symbols[STRING_START];
if (raw_data && scanner->open_heredocs.size == 0 && scanner->literal_depth == 0) {
return false;
}
if (scanner->open_heredocs.size > 0 && array_get(&scanner->open_heredocs, 0)->finished) {
return scan_heredoc_host_resume(scanner, lexer, valid_symbols);
}
if (has_pending_heredoc_body(scanner, valid_symbols) &&
(array_get(&scanner->open_heredocs, 0)->newline_consumed ||
(!lexer->is_at_included_range_start(lexer) && lexer->get_column(lexer) == 0))) {
mark_current_heredoc_position(lexer);
return start_heredoc_body(scanner, lexer, false);
}
if (!valid_symbols[STRING_START]) {
if ((valid_symbols[STRING_CONTENT] || valid_symbols[STRING_END]) && scanner->literal_depth > 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, valid_symbols);
}
}
lexer->result_symbol = NONE;
bool rescue_may_end_line = false;
if (!scan_whitespace(scanner, lexer, valid_symbols, &rescue_may_end_line)) {
return false;
}
if (lexer->result_symbol != NONE) {
return true;
}
if (valid_symbols[BEFORE_LAMBDA_BRACE] && !valid_symbols[STRING_CONTENT] && lexer->lookahead == '{' &&
in_capture_match(scanner)) {
lexer->mark_end(lexer);
return emit_scope_end(scanner, lexer, BEFORE_LAMBDA_BRACE);
}
if (match_end != NONE) {
int32_t c = lexer->lookahead;
bool assigned = match_end == LEAVE_ASSIGNED_MATCH;
if (at_eof(lexer) || c == ';' || c == ',' || c == ')' || c == ']' || c == '}' ||
c == '?' || c == ':' || c == '&' || c == '|' || c == '=' || c == '!' || c == '.' ||
is_symbol_word_start(c)) {
lexer->mark_end(lexer);
if (assigned && (c == '?' || c == '!' || c == '|' || c == '.')) return false;
if (is_symbol_word_start(c)) {
char word[KEYWORD_BUFFER_SIZE];
bool keyword = scan_short_keyword(lexer, word);
if (keyword && valid_symbols[BEFORE_MATCH_DO] && !strcmp(word, "do")) {
lexer->result_symbol = BEFORE_MATCH_DO;
return true;
}
if (assigned && (!keyword || (!strcmp(word, "rescue") && !in_defined_capture_match(scanner)) ||
!(is_clause_keyword(word) || is_modifier_keyword(word)))) {
return false;
}
}
if (c == ':') {
advance(lexer);
if (lexer->lookahead == ':') return false;
} else if (c == '=') {
if (assigned && valid_symbols[CAPTURE_MATCH_OPERATOR] && at_recorded_capture_match(scanner, lexer)) {
return scan_capture_match_operator(scanner, lexer);
}
advance(lexer);
if (lexer->lookahead != '>' && (assigned || (lexer->lookahead != '=' && lexer->lookahead != '~'))) {
return false;
}
} else if (c == '&' || c == '|' || c == '.') {
advance(lexer);
if (lexer->lookahead != c) {
if (c != '&') return false;
lexer->mark_end(lexer);
return scan_ampersand_tail(scanner, lexer, valid_symbols);
}
if (assigned) return false;
if (c != '.') {
advance(lexer);
if (lexer->lookahead == '=') return false;
}
}
return emit_scope_end(scanner, lexer, match_end);
}
}
if (valid_symbols[CLASS_BODY_SCOPE] && !valid_symbols[STRING_CONTENT] && lexer->lookahead != '<' &&
lexer->lookahead != ':' && lexer->lookahead != ';' && lexer->lookahead != '.') {
enter_local_scope(&scanner->locals, false);
lexer->result_symbol = CLASS_BODY_SCOPE;
return true;
}
switch (lexer->lookahead) {
case ';':
if (!valid_symbols[STATEMENT_SEMICOLON] || !scanner->locals.targets || raw_data) return false;
advance(lexer);
scanner->locals.targets = 0;
lexer->result_symbol = STATEMENT_SEMICOLON;
return true;
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[LAMBDA_BRACE] && !valid_symbols[STRING_CONTENT]) {
advance(lexer);
lexer->result_symbol = LAMBDA_BRACE;
return true;
}
return false;
case '=':
if (valid_symbols[CAPTURE_MATCH_OPERATOR] && scan_capture_match_operator(scanner, lexer)) return true;
if (!raw_data && valid_symbols[BLOCK_COMMENT] && lexer->get_column(lexer) == 0 && scan_embdoc(lexer)) {
lexer->mark_end(lexer);
lexer->result_symbol = BLOCK_COMMENT;
return true;
}
return false;
case '&':
if (!valid_symbols[BLOCK_AMPERSAND] && !valid_symbols[BITWISE_AND]) return false;
advance(lexer);
lexer->mark_end(lexer);
return scan_ampersand_tail(scanner, lexer, valid_symbols);
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]) {
return scan_splat(scanner, lexer, valid_symbols);
}
break;
case '+':
if (valid_symbols[UNARY_PLUS]) {
advance(lexer);
if (lexer->lookahead == '=') {
return false;
}
lexer->result_symbol = (!valid_symbols[BINARY_PLUS] ||
(scanner->has_leading_whitespace && !is_space(lexer->lookahead)))
? UNARY_PLUS : BINARY_PLUS;
return true;
}
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) &&
is_digit(lexer->lookahead)) {
lexer->result_symbol = UNARY_MINUS_NUM;
return true;
}
if (valid_symbols[UNARY_MINUS] && scanner->has_leading_whitespace && !is_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[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;
push_literal(scanner, literal);
lexer->result_symbol = SYMBOL_START;
return true;
case '\'':
advance(lexer);
literal.open_delimiter = '\'';
literal.close_delimiter = '\'';
literal.allows_interpolation = false;
push_literal(scanner, 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[HASH_KEY_SYMBOL] || valid_symbols[IDENTIFIER_SUFFIX] || accepts_local_identifier(valid_symbols) ||
valid_symbols[DEFINED]) &&
is_symbol_word_start(lexer->lookahead)) ||
(valid_symbols[CONSTANT_SUFFIX] && is_upper(lexer->lookahead)) ||
(valid_symbols[NOT_PARENTHESIZED] && lexer->lookahead == 'n') ||
(valid_symbols[END_OF_SOURCE] && lexer->lookahead == '_' && lexer->get_column(lexer) == 0)) {
return scan_word_token(scanner, lexer, valid_symbols, rescue_may_end_line);
}
if (valid_symbols[STRING_START] || valid_symbols[CHAINED_STRING_START] || (lexer->lookahead == '%' && valid_symbols[MODULO_ASSIGNMENT])) {
Literal literal = {0};
literal.nesting_depth = 1;
if (lexer->lookahead == '<') {
bool after_operand = valid_symbols[BINARY_STAR] && !expects_literal_operand(valid_symbols);
if (after_operand && (!valid_symbols[SPLAT_STAR] || !scanner->has_leading_whitespace)) {
return false;
}
if (known_binary_operator(scanner, lexer)) return false;
advance(lexer);
if (lexer->lookahead != '<') {
return false;
}
advance(lexer);
Heredoc heredoc = {0};
if (lexer->lookahead == '-' || lexer->lookahead == '~') {
heredoc.squiggly = lexer->lookahead == '~';
advance(lexer);
heredoc.end_word_indentation_allowed = true;
}
if (!scan_heredoc_word(lexer, &heredoc)) {
array_delete(&heredoc.word);
return false;
}
uint32_t index = first_started_heredoc_index(scanner);
array_insert(&scanner->open_heredocs, index, heredoc);
lexer->result_symbol = HEREDOC_START;
return true;
}
if (scan_literal_start_or_assignment(scanner, lexer, &literal, valid_symbols)) {
if (literal.type != MODULO_ASSIGNMENT) {
if (literal.close_delimiter == '\n' && has_pending_heredoc_body(scanner, valid_symbols)) {
array_get(&scanner->open_heredocs, 0)->newline_consumed = true;
literal.resume_after_heredoc = true;
}
push_literal(scanner, literal);
}
lexer->result_symbol = literal.type;
return true;
}
return false;
}
return false;
}
static bool scan_splat(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
TokenType binary = BINARY_STAR;
TokenType splat = SPLAT_STAR;
advance(lexer);
if (lexer->lookahead == '*') {
binary = BINARY_STAR_STAR;
splat = HASH_SPLAT_STAR_STAR;
if (!valid_symbols[binary] && !valid_symbols[splat]) return false;
advance(lexer);
}
if (lexer->lookahead == '=') return false;
bool prefer_binary = !scanner->has_leading_whitespace || is_space(lexer->lookahead) || !valid_symbols[splat];
TokenType result = valid_symbols[binary] && prefer_binary ? binary : splat;
if (!valid_symbols[result]) return false;
if (result == SPLAT_STAR) {
lexer->mark_end(lexer);
if (next_word_is_do(lexer)) return false;
}
lexer->result_symbol = result;
return true;
}
static bool next_word_is_do(TSLexer *lexer) {
for (;;) {
while (is_horizontal_whitespace(lexer->lookahead)) advance(lexer);
if (lexer->lookahead != '\\' || !scan_escaped_newline(lexer)) break;
}
return scan_word_text(lexer, "do") && ends_keyword(lexer->lookahead);
}
static bool ruby_local_keyword(const char *word, unsigned length) {
static const char *const words[] = {
"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",
"__FILE__", "__LINE__", "__ENCODING__",
};
if (length >= 16) return false;
for (unsigned i = 0; i < sizeof(words) / sizeof(words[0]); i++) if (!strcmp(word, words[i])) return true;
return false;
}
static bool scan_local_assignment_operator(TSLexer *lexer) {
int32_t op = lexer->lookahead;
if (op > 0 && op < 128 && strchr("+-*/%&|^<>", (int)op)) {
advance(lexer);
if (strchr("*&|<>", (int)op) && lexer->lookahead == op) advance(lexer);
else if (op == '<' || op == '>') return false;
}
if (lexer->lookahead != '=') return false;
advance(lexer);
return lexer->lookahead != '=' && lexer->lookahead != '~' && lexer->lookahead != '>';
}
static bool scan_assignment_target(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols, uint32_t name) {
bool first_target = valid_symbols[FIRST_TARGET];
bool next_target = valid_symbols[NEXT_TARGET];
if ((first_target || next_target) && (lexer->lookahead == ',' ||
(next_target && (lexer->lookahead == ')' || lexer->lookahead == 'i')))) {
uint32_t list_end = target_list_end(lexer, !next_target);
if (list_end) stage_local_target(&scanner->locals, name, list_end);
lexer->result_symbol = WORD_IDENTIFIER;
return true;
}
if (valid_symbols[BINDING_IDENTIFIER] && lexer->lookahead != '.' && lexer->lookahead != '[' &&
lexer->lookahead != ':' && lexer->lookahead != '&' && !valid_symbols[STRING_CONTENT]) {
record_local_binding(&scanner->locals, name);
lexer->result_symbol = lexer->lookahead == ',' && valid_symbols[LOCAL_IDENTIFIER] ? WORD_IDENTIFIER : BINDING_IDENTIFIER;
return true;
}
return false;
}
static uint32_t target_list_end(TSLexer *lexer, bool first_target) {
enum { AFTER_TARGET, EXPECT_TARGET, AFTER_SPLAT, EXPECT_NAME } state = AFTER_TARGET;
int depth = 0;
int min_depth = 0;
unsigned budget = 1024;
while (budget > 0 && !at_eof(lexer)) {
budget--;
int32_t c = lexer->lookahead;
if (is_horizontal_whitespace(c) || c == '\n' || c == '\\') {
advance(lexer);
continue;
}
if (c == '#') {
while (!at_eof(lexer) && lexer->lookahead != '\n') advance(lexer);
continue;
}
bool optional_name = state == EXPECT_TARGET || state == AFTER_SPLAT;
if (state == EXPECT_NAME || (optional_name && (is_symbol_word_start(c) || c == '@' || c == '$'))) {
while (lexer->lookahead == '@' || lexer->lookahead == '$') advance(lexer);
if (!is_symbol_word_start(lexer->lookahead)) return 0;
bool is_in = optional_name && lexer->lookahead == 'i';
unsigned length = 0;
while (is_symbol_word_character(lexer->lookahead)) {
is_in = is_in && (length > 0 || lexer->lookahead == 'i') && (length != 1 || lexer->lookahead == 'n');
length++;
advance(lexer);
}
if (is_in && length == 2) return depth == min_depth ? lexer_byte_offset(lexer) : 0;
state = AFTER_TARGET;
continue;
}
if (state == AFTER_SPLAT) {
state = AFTER_TARGET;
continue;
}
if (state == EXPECT_TARGET) {
if (c == '(') {
depth++;
} else if (c == '*') {
advance(lexer);
state = AFTER_SPLAT;
continue;
} else if (c == ':') {
advance(lexer);
if (lexer->lookahead != ':') return 0;
state = EXPECT_NAME;
} else if (c == '=' || c == ')') {
state = AFTER_TARGET;
continue;
} else {
return 0;
}
advance(lexer);
continue;
}
switch (c) {
case ',':
state = EXPECT_TARGET;
break;
case ')':
if (--depth < min_depth) {
if (first_target) return 0;
min_depth = depth;
}
break;
case '.':
state = EXPECT_NAME;
break;
case '&':
advance(lexer);
if (lexer->lookahead != '.') return 0;
state = EXPECT_NAME;
break;
case ':':
advance(lexer);
if (lexer->lookahead != ':') return 0;
state = EXPECT_NAME;
break;
case '(':
case '[':
if (!skip_balanced_target_suffix(lexer, &budget)) return 0;
continue;
case '=':
if (depth != min_depth) return 0;
advance(lexer);
if (lexer->lookahead == '=' || lexer->lookahead == '~' || lexer->lookahead == '>') return 0;
return lexer_byte_offset(lexer);
case 'i':
if (depth != min_depth) return 0;
advance(lexer);
if (lexer->lookahead != 'n') return 0;
advance(lexer);
return is_symbol_word_character(lexer->lookahead) ? 0 : lexer_byte_offset(lexer);
default:
return 0;
}
advance(lexer);
}
return 0;
}
static uint32_t lexer_byte_offset(TSLexer *lexer) {
TSLexerPosition position;
lexer->get_position(lexer, &position);
return position.bytes;
}
static bool skip_balanced_target_suffix(TSLexer *lexer, unsigned *budget) {
int nesting = 0;
do {
int32_t c = lexer->lookahead;
if (c == '(' || c == '[') nesting++;
if (c == ')' || c == ']') nesting--;
advance(lexer);
if (c == '"' || c == '\'') {
while (!at_eof(lexer) && lexer->lookahead != c && *budget > 0) {
if (lexer->lookahead == '\\') advance(lexer);
advance(lexer);
(*budget)--;
}
if (at_eof(lexer)) return false;
advance(lexer);
}
if (*budget == 0 || at_eof(lexer)) return false;
(*budget)--;
} while (nesting > 0);
return true;
}
static bool scan_local_identifier(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols, uint32_t name, bool numbered_parameter) {
lexer->mark_end(lexer);
if (valid_symbols[PARAMETER_IDENTIFIER]) {
record_local_binding(&scanner->locals, name);
lexer->result_symbol = PARAMETER_IDENTIFIER;
return true;
}
if (!skip_operand_gap(lexer)) return false;
if (valid_symbols[NEXT_TARGET] && (lexer->lookahead == '\n' || lexer->lookahead == '#')) {
do {
if (lexer->lookahead == '#') scan_line_comment(lexer);
if (lexer->lookahead != '\n') return false;
advance(lexer);
if (!skip_operand_gap(lexer)) return false;
} while (lexer->lookahead == '\n' || lexer->lookahead == '#');
if (lexer->lookahead != '=' && lexer->lookahead != ')') return false;
}
TSLexerPosition position;
lexer->get_position(lexer, &position);
if (scan_assignment_target(scanner, lexer, valid_symbols, name)) return true;
if ((lexer->lookahead == '<' || lexer->lookahead == '%') && (local_is_bound(&scanner->locals, name) || numbered_parameter)) {
scanner->locals.before_binary_operator = true;
scanner->locals.binary_operator_position = position.bytes;
lexer->result_symbol = WORD_IDENTIFIER;
return true;
}
if (!scan_local_assignment_operator(lexer)) return false;
if (valid_symbols[NEXT_TARGET]) {
lexer->get_position(lexer, &position);
commit_local_targets(&scanner->locals, position.bytes);
} else {
scanner->locals.targets = 0;
}
record_local_binding(&scanner->locals, name);
lexer->result_symbol = WORD_IDENTIFIER;
return true;
}
typedef struct {
uint32_t name;
unsigned length;
char keyword[16];
bool end_marker;
} ScannedWord;
static ScannedWord scan_word_spelling(Scanner *scanner, TSLexer *lexer, bool intern_name, bool check_end_marker) {
ScannedWord word = {.end_marker = check_end_marker && lexer->get_column(lexer) == 0};
lexer->mark_end(lexer);
while (ruby_identifier_character(lexer->lookahead)) {
if (intern_name) word.name = intern_local_node(&scanner->locals, NAME_PREFIX, word.name, (uint32_t)lexer->lookahead);
if (word.length < sizeof(word.keyword) - 1) {
word.keyword[word.length] = (char)(lexer->lookahead < 128 ? lexer->lookahead : 127);
}
if (word.length && word.end_marker && lexer->is_at_included_range_start(lexer)) word.end_marker = false;
word.length++;
advance(lexer);
}
return word;
}
static bool scan_hash_capture(Scanner *scanner, TSLexer *lexer, uint32_t name, bool unbraced) {
lexer->mark_end(lexer);
advance(lexer);
bool line_end = false;
while (!at_eof(lexer)) {
if (unbraced && lexer->lookahead == '\n') {
line_end = true;
break;
}
if (lexer->lookahead == '#') {
while (lexer->lookahead != '\n' && !at_eof(lexer)) advance(lexer);
} else if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead == '\r') advance(lexer);
if (lexer->lookahead != '\n') return false;
advance(lexer);
} else if (lexer->lookahead == '\n' || is_horizontal_whitespace(lexer->lookahead)) {
advance(lexer);
} else break;
}
bool clause = false;
if (is_symbol_word_start(lexer->lookahead)) {
TSLexerCheckpoint key_end;
lexer->get_mark_end_checkpoint(lexer, &key_end);
ScannedWord next = scan_word_spelling(scanner, lexer, false, false);
lexer->mark_end_at(lexer, &key_end);
clause = ends_keyword(lexer->lookahead) && (!strcmp(next.keyword, "then") || !strcmp(next.keyword, "if") ||
!strcmp(next.keyword, "unless") || !strcmp(next.keyword, "end"));
if (!clause) return false;
}
if (line_end || clause || at_eof(lexer) || lexer->lookahead == ';' || lexer->lookahead == ',' ||
lexer->lookahead == '}' || lexer->lookahead == ']' || lexer->lookahead == ')') {
record_local_binding(&scanner->locals, name);
lexer->result_symbol = unbraced ? UNBRACED_HASH_CAPTURE : HASH_CAPTURE;
return true;
}
return false;
}
static bool scan_rescue_line_break(TSLexer *lexer) {
if (lexer->lookahead == ':') {
advance(lexer);
if (lexer->lookahead != ':') return false;
}
lexer->result_symbol = LINE_BREAK;
return true;
}
static bool scan_word_token(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols, bool rescue_may_end_line) {
TokenType suffix = is_upper(lexer->lookahead) ? CONSTANT_SUFFIX : IDENTIFIER_SUFFIX;
bool track = suffix == IDENTIFIER_SUFFIX && accepts_local_identifier(valid_symbols);
ScannedWord word = scan_word_spelling(scanner, lexer, track, valid_symbols[END_OF_SOURCE]);
if (track && valid_symbols[PARAMETER_IDENTIFIER] && lexer->lookahead == ':' && ruby_local_keyword(word.keyword, word.length)) {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == ':') return false;
lexer->result_symbol = PARAMETER_IDENTIFIER;
return true;
}
if (word.end_marker && !strcmp(word.keyword, "__END__") &&
(lexer->lookahead == '\n' || lexer->lookahead == '\r' || at_eof(lexer))) {
return scan_end_marker_line_end(lexer);
}
if (rescue_may_end_line && !strcmp(word.keyword, "rescue") && ends_keyword(lexer->lookahead)) {
return scan_rescue_line_break(lexer);
}
if (valid_symbols[DEFINED] && !strcmp(word.keyword, "defined") && lexer->lookahead == '?') {
advance(lexer);
if (lexer->lookahead != '<') return false;
lexer->mark_end(lexer);
TSLexerPosition position;
lexer->get_position(lexer, &position);
scanner->locals.before_binary_operator = true;
scanner->locals.binary_operator_position = position.bytes;
lexer->result_symbol = DEFINED;
return true;
}
if ((valid_symbols[HASH_CAPTURE] || valid_symbols[UNBRACED_HASH_CAPTURE]) && word.name && lexer->lookahead == ':' && !valid_symbols[STRING_CONTENT]) {
return scan_hash_capture(scanner, lexer, word.name, valid_symbols[UNBRACED_HASH_CAPTURE]);
}
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[suffix] && lexer->lookahead == '!') {
advance(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead != '=') {
lexer->result_symbol = suffix;
return true;
}
return false;
}
if (track && !ruby_local_keyword(word.keyword, word.length) && lexer->lookahead != '?' && lexer->lookahead != '!') {
bool numbered_parameter = word.length == 2 && word.keyword[0] == '_' && word.keyword[1] >= '1' && word.keyword[1] <= '9' &&
scope_has_numbered_parameters(&scanner->locals);
return scan_local_identifier(scanner, lexer, valid_symbols, word.name, numbered_parameter);
}
if (!valid_symbols[NOT_PARENTHESIZED] || strcmp(word.keyword, "not")) return false;
lexer->mark_end(lexer);
if (!scan_not_parentheses_gap(lexer)) return false;
lexer->result_symbol = NOT_PARENTHESIZED;
return true;
}
static bool scan_end_marker_line_end(TSLexer *lexer) {
lexer->mark_end(lexer);
if (lexer->lookahead == '\r') {
advance(lexer);
if (lexer->lookahead != '\n') return false;
}
lexer->result_symbol = END_OF_SOURCE;
return true;
}
static bool scan_not_parentheses_gap(TSLexer *lexer) {
bool separated = false;
bool line_break = false;
for (;;) {
if (is_space(lexer->lookahead)) {
separated = true;
line_break |= lexer->lookahead == '\n';
advance(lexer);
continue;
}
switch (lexer->lookahead) {
case '\\':
if (!scan_escaped_newline(lexer)) return false;
separated = true;
break;
case '#':
separated = true;
scan_line_comment(lexer);
break;
case '=':
if (!line_break || lexer->get_column(lexer) != 0 || !scan_embdoc(lexer)) return false;
separated = true;
break;
default:
return lexer->lookahead == '(' && (!separated || line_break);
}
}
}
static inline bool scan_escaped_newline(TSLexer *lexer) {
advance(lexer);
if (lexer->lookahead == '\r') advance(lexer);
if (lexer->lookahead != '\n') return false;
advance(lexer);
return true;
}
static bool scan_embdoc(TSLexer *lexer) {
if (!scan_word_text(lexer, "=begin") || !is_space(lexer->lookahead)) return false;
while (!at_eof(lexer)) {
bool closed = lexer->get_column(lexer) == 0 && lexer->lookahead == '=' &&
scan_word_text(lexer, "=end") && (is_space(lexer->lookahead) || at_eof(lexer));
while (!at_eof(lexer) && lexer->lookahead != '\n') advance(lexer);
if (closed) return true;
if (!at_eof(lexer)) advance(lexer);
}
return false;
}
static inline bool scan_literal_resume(Literal *literal, TSLexer *lexer, const bool *valid_symbols) {
if (!valid_symbols[HEREDOC_RESUME] || (lexer->lookahead != '\r' && lexer->lookahead != '\n')) return false;
bool carriage_return = lexer->lookahead == '\r';
advance(lexer);
if (carriage_return && lexer->lookahead == '\n' && !lexer->is_at_included_range_start(lexer)) advance(lexer);
literal->resume_after_heredoc = false;
lexer->result_symbol = HEREDOC_RESUME;
lexer->mark_end(lexer);
return true;
}
static inline bool scan_heredoc_host_resume(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (!valid_symbols[HEREDOC_RESUME]) return false;
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') advance(lexer);
if (lexer->lookahead == '\r') {
advance(lexer);
if (lexer->is_at_included_range_start(lexer)) return false;
}
if (lexer->lookahead != '\n') return false;
advance(lexer);
Heredoc *heredoc = array_get(&scanner->open_heredocs, 0);
array_delete(&heredoc->word);
array_erase(&scanner->open_heredocs, 0);
lexer->result_symbol = HEREDOC_RESUME;
lexer->mark_end(lexer);
return true;
}
static inline void finish_heredoc(Scanner *scanner, TSLexer *lexer, bool can_resume, uint32_t index) {
Heredoc *heredoc = array_get(&scanner->open_heredocs, index);
bool has_pending_sibling = index + 1 < scanner->open_heredocs.size &&
!array_get(&scanner->open_heredocs, index + 1)->started;
bool resume_host = heredoc->host_line_continued && index == 0 && !has_pending_sibling && can_resume;
if (resume_host && lexer->lookahead == '\r') {
advance(lexer);
resume_host = lexer->lookahead == '\n' && !lexer->is_at_included_range_start(lexer);
}
if (resume_host) {
heredoc->finished = true;
} else {
if (has_pending_sibling) {
Heredoc *next = array_get(&scanner->open_heredocs, index + 1);
next->host_line_continued |= heredoc->host_line_continued;
next->mid_line = true;
}
array_delete(&heredoc->word);
array_erase(&scanner->open_heredocs, index);
}
lexer->result_symbol = HEREDOC_BODY_END;
}
static inline uint32_t first_started_heredoc_index(Scanner *scanner) {
uint32_t index = 0;
while (index < scanner->open_heredocs.size && !array_get(&scanner->open_heredocs, index)->started) index++;
return index;
}
static inline bool has_pending_heredoc_body(Scanner *scanner, const bool *valid_symbols) {
return valid_symbols[HEREDOC_BODY_START] && scanner->open_heredocs.size > 0 &&
!array_get(&scanner->open_heredocs, 0)->started;
}
static inline bool scan_heredoc_after_literal_escape(Scanner *scanner, TSLexer *lexer, bool consume_plain_escape, bool continues_word) {
advance(lexer);
bool separated = lexer->is_at_included_range_start(lexer);
if (separated && (lexer->lookahead == '\r' || lexer->lookahead == '\n')) {
return finish_literal_content(lexer);
}
if (separated && !consume_plain_escape) return false;
if (!consume_plain_escape && (lexer->lookahead == 'c' || lexer->lookahead == 'C' || lexer->lookahead == 'M')) {
if (!scan_control_newline_operand(lexer)) return false;
if (continues_word) scanner->literal.resume_after_heredoc = true;
array_get(&scanner->open_heredocs, 0)->newline_consumed = true;
lexer->mark_end(lexer);
lexer->result_symbol = LITERAL_ESCAPE;
return true;
}
bool carriage_return = !separated && lexer->lookahead == '\r';
if (carriage_return) {
advance(lexer);
if (lexer->is_at_included_range_start(lexer)) return finish_literal_content(lexer);
}
if (!separated && lexer->lookahead == '\n') {
advance(lexer);
if (continues_word) {
scanner->literal.resume_after_heredoc = true;
array_get(&scanner->open_heredocs, 0)->newline_consumed = true;
lexer->mark_end(lexer);
lexer->result_symbol = consume_plain_escape ? STRING_CONTENT : LITERAL_ESCAPE;
return true;
}
mark_current_heredoc_position(lexer);
return start_literal_heredoc_body(scanner, lexer, false);
}
if (!consume_plain_escape) return false;
if (!carriage_return) advance(lexer);
return finish_literal_content(lexer);
}
static bool advance_within_range(TSLexer *lexer) {
advance(lexer);
return !lexer->is_at_included_range_start(lexer);
}
static bool scan_control_newline_operand(TSLexer *lexer) {
unsigned seen = 0;
for (;;) {
int32_t prefix = lexer->lookahead;
unsigned flag = prefix == 'M' ? 2 : 1;
if (seen & flag) return false;
seen |= flag;
if (!advance_within_range(lexer)) return false;
if (prefix != 'c') {
if (lexer->lookahead != '-') return false;
if (!advance_within_range(lexer)) return false;
}
if (lexer->lookahead != '\\') break;
if (!advance_within_range(lexer)) return false;
if (lexer->lookahead != 'c' && lexer->lookahead != 'C' && lexer->lookahead != 'M') break;
}
if (lexer->lookahead == '\r' && !advance_within_range(lexer)) return false;
if (lexer->lookahead != '\n') return false;
advance(lexer);
return true;
}
static inline bool finish_literal_content(TSLexer *lexer) {
lexer->mark_end(lexer);
lexer->result_symbol = STRING_CONTENT;
return true;
}
static inline bool scan_heredoc_after_literal_space(Scanner *scanner, TSLexer *lexer, bool separator_valid) {
TSLexerCheckpoint boundary_start, boundary_end, body_start;
lexer->get_checkpoint(lexer, &boundary_start);
bool after_carriage_return = false;
do {
after_carriage_return = lexer->lookahead == '\r';
if (after_carriage_return) lexer->get_checkpoint(lexer, &body_start);
lexer->advance(lexer, true);
if (lexer->is_at_included_range_start(lexer)) after_carriage_return = false;
} while (is_horizontal_whitespace(lexer->lookahead));
lexer->get_checkpoint(lexer, &boundary_end);
if (lexer->lookahead != '\n') {
if (!separator_valid) return false;
lexer->mark_start(lexer, &boundary_start);
lexer->mark_end_at(lexer, &boundary_end);
lexer->result_symbol = WORD_SEPARATOR;
return true;
}
if (!after_carriage_return) lexer->get_checkpoint(lexer, &body_start);
lexer->mark_start(lexer, &body_start);
lexer->mark_end_at(lexer, &body_start);
return start_literal_heredoc_body(scanner, lexer, true);
}
static inline bool is_horizontal_whitespace(int32_t codepoint) {
return codepoint == ' ' || codepoint == '\t' || codepoint == '\f' || codepoint == '\v' || codepoint == '\r';
}
static inline bool start_literal_heredoc_body(Scanner *scanner, TSLexer *lexer, bool before_newline) {
Literal *literal = &scanner->literal;
if (literal->close_delimiter == '\n') literal->resume_after_heredoc = true;
return start_heredoc_body(scanner, lexer, before_newline);
}
static inline bool start_heredoc_body(Scanner *scanner, TSLexer *lexer, bool before_newline) {
Heredoc *heredoc = array_get(&scanner->open_heredocs, 0);
heredoc->started = true;
heredoc->mid_line |= before_newline;
heredoc->newline_consumed = false;
if (heredoc->allows_interpolation && (lexer->lookahead == '#' || lexer->lookahead == '\\')) {
lexer->mark_end(lexer);
heredoc->mid_line = !scan_heredoc_end_word(heredoc, lexer);
}
lexer->result_symbol = HEREDOC_BODY_START;
return true;
}
static inline bool scan_heredoc_end_word(Heredoc *heredoc, TSLexer *lexer) {
for (size_t position = 0; position < heredoc->word.size;) {
uint32_t length = match_heredoc_character(&heredoc->word, position, lexer->lookahead);
if (length == 0) return false;
position += length;
advance(lexer);
}
return lexer->lookahead == '\r' || lexer->lookahead == '\n' || at_eof(lexer);
}
static inline void mark_current_heredoc_position(TSLexer *lexer) {
TSLexerCheckpoint body_start;
lexer->get_checkpoint(lexer, &body_start);
lexer->mark_start(lexer, &body_start);
lexer->mark_end_at(lexer, &body_start);
}
static inline bool is_symbol_word_character(int32_t character) {
return is_symbol_word_start(character) || is_digit(character);
}
static inline bool is_symbol_word_start(int32_t character) {
return character > 0x7f || character == '_' || ((uint32_t)character | 32) - 'a' < 26;
}
static inline bool is_iden_char(int32_t c) {
return (uint32_t)c >= 0x80 || !NON_IDENTIFIER_ASCII[c];
}
static inline bool is_global_symbol_punctuation(int32_t c) {
return (uint32_t)c < 0x80 && GLOBAL_SYMBOL_PUNCTUATION[c];
}
static inline bool is_space(int32_t c) {
return c == ' ' || (uint32_t)c - '\t' < 5;
}
static inline bool is_alpha(int32_t c) {
return (uint32_t)c < 0x80 ? ((uint32_t)c | 32) - 'a' < 26 : iswalpha((wint_t)c) != 0;
}
static inline bool is_alnum(int32_t c) {
return (uint32_t)c < 0x80 ? (uint32_t)c - '0' < 10 || ((uint32_t)c | 32) - 'a' < 26 : iswalnum((wint_t)c) != 0;
}
static inline bool is_digit(int32_t c) {
return (uint32_t)c - '0' < 10;
}
static inline bool is_lower(int32_t c) {
return (uint32_t)c < 0x80 ? (uint32_t)c - 'a' < 26 : iswlower((wint_t)c) != 0;
}
static inline bool is_upper(int32_t c) {
return (uint32_t)c < 0x80 ? (uint32_t)c - 'A' < 26 : ruby_unicode_uppercase(c);
}
static inline void append_heredoc_character(String *word, int32_t codepoint) {
char utf8[4];
uint32_t length = encode_utf8(codepoint, utf8);
array_extend(word, length, utf8);
}
static inline uint32_t match_heredoc_character(const String *word, size_t position, int32_t codepoint) {
if ((uint32_t)codepoint < 0x80) {
return (unsigned char)word->contents[position] == (uint32_t)codepoint ? 1 : 0;
}
char utf8[4];
uint32_t length = encode_utf8(codepoint, utf8);
if (position + length > word->size) return 0;
for (uint32_t i = 0; i < length; i++) {
if (word->contents[position + i] != utf8[i]) return 0;
}
return length;
}
static inline uint32_t encode_utf8(int32_t codepoint, char *bytes) {
if (codepoint < 0x80) {
bytes[0] = (char)codepoint;
return 1;
}
uint32_t length = codepoint < 0x800 ? 2 : codepoint < 0x10000 ? 3 : 4;
for (uint32_t index = length - 1; index > 0; index--) {
bytes[index] = (char)(0x80 | (codepoint & 0x3F));
codepoint >>= 6;
}
bytes[0] = (char)((0xF00 >> length) | codepoint);
return length;
}
static bool scan_word_text(TSLexer *lexer, const char *word) {
for (; *word; word++) {
if (lexer->lookahead != *word) return false;
advance(lexer);
}
return true;
}
static inline bool scan_line_comment(TSLexer *lexer) {
advance(lexer);
while (!at_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;
if (scanner->locals.before_binary_operator) {
TSLexerPosition position;
lexer->get_position(lexer, &position);
if (position.bytes > scanner->locals.binary_operator_position) scanner->locals.before_binary_operator = false;
}
bool scanned = scan(scanner, lexer, valid_symbols);
if (scanned && lexer->result_symbol == LINE_BREAK) {
TokenType match_end = capture_match_end(scanner, valid_symbols);
if (match_end != NONE) return emit_scope_end(scanner, lexer, match_end);
scanner->locals.targets = 0;
if (valid_symbols[BODY_LINE_BREAK]) lexer->result_symbol = BODY_LINE_BREAK;
}
return scanned;
}
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;
trim_open_heredocs(scanner, 0);
array_delete(&scanner->open_heredocs);
array_delete(&scanner->literal_frames);
array_delete(&scanner->literal_index);
array_delete(&scanner->locals.nodes);
array_delete(&scanner->locals.slots);
ts_free(scanner);
}