#ifndef TREE_SITTER_C_DIRECTIVE_SCAN_H_
#define TREE_SITTER_C_DIRECTIVE_SCAN_H_
#include "pragma.h"
#include <stdlib.h>
typedef struct {
uint32_t start, end;
} DirectiveSpan;
typedef struct {
DirectiveSpan *items;
uint32_t count, capacity;
bool failed;
} DirectiveSpans;
#define DIRECTIVE_CHARACTER_BYTES 4
typedef enum { SCAN_CODE, SCAN_SLASH, SCAN_LINE_COMMENT, SCAN_BLOCK_COMMENT, SCAN_QUOTE, SCAN_WORD, SCAN_RAW_DELIMITER, SCAN_RAW_BODY } DirectiveScanMode;
typedef struct {
DirectiveScanMode mode;
GnuRawStringPrefix raw;
DirectiveSpans *run;
const uint8_t *classes;
bool *pragma;
uint64_t recent;
int32_t quote, previous, pair;
uint32_t spaces, run_start, run_end, run_spans;
unsigned delimiter_length, closing_length, closing_end;
char delimiter[16], closing[18];
bool beginning, number, escape, star, splice, in_run;
} DirectiveScan;
static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer);
static bool directive_scan_feed(DirectiveScan *scan, int32_t c);
static bool directive_scan_view(DirectiveScan *scan, TSLexer *lexer);
static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer);
static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer);
static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count);
static inline bool directive_pragma_word(uint64_t recent);
static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c);
static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer);
static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer);
static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw);
static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member);
static inline bool directive_scan_ascii_word(int32_t c);
static inline bool directive_scan_ascii_plain(int32_t c);
static bool directive_scan_pending(DirectiveScan *scan, int32_t c);
static bool directive_scan_splice(DirectiveScan *scan, int32_t c);
static bool directive_scan_logical(DirectiveScan *scan, int32_t c);
static bool directive_scan_slash(DirectiveScan *scan, int32_t c);
static void directive_scan_quote(DirectiveScan *scan, int32_t c);
static bool directive_scan_code(DirectiveScan *scan, int32_t c);
static void directive_scan_word_start(DirectiveScan *scan, int32_t c);
static bool directive_scan_word(DirectiveScan *scan, int32_t c);
static bool directive_scan_raw_delimiter(DirectiveScan *scan, int32_t c);
static void directive_scan_raw_body(DirectiveScan *scan, int32_t c);
static bool directive_horizontal(int32_t c);
static bool directive_newline(int32_t c);
static void directive_spans_push(DirectiveSpans *spans, uint32_t start, uint32_t end);
static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool skipped);
static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
#include "directiveUnits.h"
static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer) {
for (bool slash = false;;) {
if (scan->mode == SCAN_LINE_COMMENT || scan->mode == SCAN_BLOCK_COMMENT) return true;
int32_t c = lexer->lookahead;
bool space = !slash && scan->mode == SCAN_CODE && !scan->splice && (pragma_space(c) || c == '/');
if ((!space && scan->mode != SCAN_SLASH) || pragma_eof(lexer)) return false;
slash = slash || c == '/';
directive_scan_feed(scan, c);
directive_scan_lexer_run(scan, lexer, pragma_space(c) || c == '\\');
lexer->advance(lexer, false);
}
}
static void directive_scan_plain_run(DirectiveScan *scan, TSLexer *lexer) {
if (scan->splice) return;
if (scan->pair && directive_newline(lexer->lookahead)) return;
scan->pair = 0;
while (directive_scan_view(scan, lexer)) {}
while (directive_scan_plain_mode(scan, lexer)) {}
}
static bool directive_scan_view(DirectiveScan *scan, TSLexer *lexer) {
DirectiveScanMode mode = scan->mode;
bool readable = mode == SCAN_CODE || mode == SCAN_WORD || mode == SCAN_LINE_COMMENT || mode == SCAN_BLOCK_COMMENT ||
(mode == SCAN_QUOTE && !scan->escape);
if (!readable || (mode != SCAN_CODE && directive_pragma_pending(scan->recent))) return false;
uint32_t count, size;
const char *units = lexer->peek_units(lexer, &count, &size);
if (count < 2 || lexer->is_at_included_range_start(lexer)) return false;
TSLexerPosition position;
lexer->get_position(lexer, &position);
DirectiveUnits run = size == 1
? directive_scan_units(scan, units, count, 1, position.bytes, true)
: directive_scan_units(scan, units, count, 2, position.bytes, true);
lexer->advance_units(lexer, run.consumed, run.line_feeds, run.tail);
return run.consumed == count;
}
static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer) {
switch (scan->mode) {
case SCAN_WORD:
return directive_scan_plain_word(scan, lexer);
case SCAN_CODE:
return directive_scan_plain_code(scan, lexer);
case SCAN_BLOCK_COMMENT:
return directive_scan_plain_block_comment(scan, lexer);
case SCAN_LINE_COMMENT:
directive_scan_plain_text(scan, lexer, 0, false);
return false;
case SCAN_QUOTE:
if (!scan->escape) directive_scan_plain_text(scan, lexer, scan->quote, false);
return false;
case SCAN_RAW_BODY:
directive_scan_plain_text(scan, lexer, '"', true);
return false;
default:
return false;
}
}
static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer) {
int32_t c = lexer->lookahead;
uint64_t recent = scan->recent;
uint32_t count = 0;
for (; directive_scan_ascii_word(c); c = lexer->lookahead) {
recent = recent << 8 | (uint64_t)c;
count++;
lexer->advance(lexer, false);
}
if (count) directive_scan_word_characters(scan, recent, count);
if (!directive_scan_ascii_plain(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
scan->mode = SCAN_CODE;
return true;
}
static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count) {
scan->in_run = false;
scan->previous = (int32_t)(recent & 0xff);
scan->recent = recent;
if (directive_pragma_word(recent)) *scan->pragma = true;
if (scan->raw.length >= sizeof scan->raw.value) return;
if (count > sizeof scan->raw.value - 1 - scan->raw.length) {
gnu_raw_string_prefix_invalidate(&scan->raw);
return;
}
while (count--) gnu_raw_string_prefix_character(&scan->raw, (int32_t)(recent >> (8 * count) & 0xff));
}
static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer) {
for (;;) {
int32_t c = lexer->lookahead;
if (!directive_scan_ascii_plain(c)) return false;
bool space = directive_horizontal(c);
directive_scan_lexer_run(scan, lexer, space);
lexer->advance(lexer, false);
if (space) continue;
scan->beginning = false;
if (!directive_scan_ascii_word(c)) continue;
directive_scan_word_start(scan, c);
if (scan->mode == SCAN_WORD) return true;
}
}
static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer) {
bool star = scan->star, closed = false;
uint64_t recent = scan->recent;
while (!closed) {
int32_t c = lexer->lookahead;
if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\') break;
directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
recent = directive_scan_note(scan, recent, c);
lexer->advance(lexer, false);
closed = star && c == '/';
star = c == '*';
}
scan->recent = recent;
scan->star = star;
if (closed) scan->mode = SCAN_CODE;
return closed;
}
static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw) {
uint64_t recent = scan->recent;
for (;;) {
int32_t c = lexer->lookahead;
if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\' || c == end) break;
directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
if (raw) directive_scan_raw_body(scan, c);
recent = directive_scan_note(scan, recent, c);
lexer->advance(lexer, false);
}
scan->recent = recent;
}
static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member) {
if (!member) {
scan->in_run = false;
return;
}
TSLexerPosition position;
lexer->get_position(lexer, &position);
directive_scan_extend(scan, position.bytes, scan->in_run && lexer->is_at_included_range_start(lexer));
scan->run_end = position.bytes + 1;
}
static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped) {
if (!scan->in_run) {
scan->run_spans = 0;
scan->run_start = start;
scan->in_run = true;
} else if (skipped) {
directive_scan_push(scan, scan->run_start, scan->run_end);
scan->run_start = start;
}
}
static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool skipped) {
directive_scan_push(scan, scan->run_start, skipped ? scan->run_end : limit);
}
static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end) {
scan->run->count = scan->run_spans;
directive_spans_push(scan->run, start, end);
scan->run_spans = scan->run->count;
}
static inline bool directive_scan_ascii_word(int32_t c) {
return (uint32_t)c < 0x80 && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
}
static inline bool directive_scan_ascii_plain(int32_t c) {
static const uint64_t special[] = {
1ull | (1ull << '\n') | (1ull << '\r') | (1ull << '"') | (1ull << '#') | (1ull << '\'') | (1ull << '/'),
1ull << ('\\' - 64),
};
return (uint32_t)c < 0x80 && !((special[c / 64] >> (c % 64)) & 1);
}
static void directive_scan_run(DirectiveScan *scan, int32_t c, uint32_t start, uint32_t end) {
if (!pragma_space(c) && c != '\\') {
scan->in_run = false;
return;
}
directive_scan_extend(scan, start, scan->in_run && start != scan->run_end);
scan->run_end = end - start > DIRECTIVE_CHARACTER_BYTES ? start + DIRECTIVE_CHARACTER_BYTES : end;
}
static bool directive_scan_feed(DirectiveScan *scan, int32_t c) {
if ((uint32_t)c > 0x10ffff) c = 0xfffd;
scan->recent = directive_scan_note(scan, scan->recent, c);
if (scan->mode == SCAN_RAW_BODY) {
directive_scan_raw_body(scan, c);
return false;
}
if (directive_scan_pending(scan, c)) return false;
if (c == '\\') {
scan->splice = true;
return false;
}
if (directive_newline(c)) {
scan->pair = c;
c = '\n';
}
return directive_scan_logical(scan, c);
}
static bool directive_scan_pending(DirectiveScan *scan, int32_t c) {
if (scan->pair) {
int32_t pair = scan->pair;
scan->pair = 0;
if (directive_newline(c) && c != pair) return true;
}
return scan->splice && directive_scan_splice(scan, c);
}
static bool directive_scan_splice(DirectiveScan *scan, int32_t c) {
if (directive_horizontal(c)) {
scan->spaces++;
return true;
}
scan->splice = false;
if (directive_newline(c)) {
scan->spaces = 0;
scan->pair = c;
return true;
}
directive_scan_logical(scan, '\\');
for (; scan->spaces; scan->spaces--) directive_scan_logical(scan, ' ');
return false;
}
static bool directive_scan_logical(DirectiveScan *scan, int32_t c) {
switch (scan->mode) {
case SCAN_CODE:
return directive_scan_code(scan, c);
case SCAN_SLASH:
return directive_scan_slash(scan, c);
case SCAN_LINE_COMMENT:
if (c == '\n') {
scan->mode = SCAN_CODE;
scan->beginning = true;
}
return false;
case SCAN_BLOCK_COMMENT:
if (scan->star && c == '/') scan->mode = SCAN_CODE;
scan->star = c == '*';
return false;
case SCAN_QUOTE:
directive_scan_quote(scan, c);
return false;
case SCAN_WORD:
return directive_scan_word(scan, c);
case SCAN_RAW_DELIMITER:
return directive_scan_raw_delimiter(scan, c);
case SCAN_RAW_BODY:
break;
}
return false;
}
static bool directive_scan_slash(DirectiveScan *scan, int32_t c) {
scan->mode = c == '/' ? SCAN_LINE_COMMENT : c == '*' ? SCAN_BLOCK_COMMENT : SCAN_CODE;
scan->star = false;
if (scan->mode != SCAN_CODE) return false;
scan->beginning = false;
return directive_scan_code(scan, c);
}
static void directive_scan_quote(DirectiveScan *scan, int32_t c) {
if (scan->escape) scan->escape = false;
else if (c == scan->quote) scan->mode = SCAN_CODE;
else if (c == '\\') scan->escape = true;
if (c == '\n' && scan->mode == SCAN_QUOTE) {
scan->mode = SCAN_CODE;
scan->beginning = true;
}
}
static bool directive_scan_code(DirectiveScan *scan, int32_t c) {
if (c == '\n') {
scan->beginning = true;
return false;
}
if (c == '/') {
scan->mode = SCAN_SLASH;
return false;
}
if (directive_horizontal(c) || (scan->beginning && pragma_space(c))) return false;
if (c == '#' && scan->beginning) return true;
scan->beginning = false;
if (c == '"' || c == '\'') {
scan->mode = SCAN_QUOTE;
scan->quote = c;
scan->escape = false;
} else {
directive_scan_word_start(scan, c);
}
return false;
}
static void directive_scan_word_start(DirectiveScan *scan, int32_t c) {
if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
scan->mode = SCAN_WORD;
scan->number = c >= '0' && c <= '9';
scan->previous = c;
scan->recent = c < 0x80 ? (uint64_t)c : 0;
scan->raw = (GnuRawStringPrefix){0};
gnu_raw_string_prefix_character(&scan->raw, c);
}
static bool directive_scan_word(DirectiveScan *scan, int32_t c) {
int32_t previous = scan->previous;
bool exponent_sign = scan->number && (c == '+' || c == '-') &&
(previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
if (preproc_word(c, true) || (scan->number && (c == '.' || c == '\'' || exponent_sign))) {
scan->previous = c;
gnu_raw_string_prefix_character(&scan->raw, c);
return false;
}
scan->mode = SCAN_CODE;
if (c == '"' && gnu_raw_string_prefix(&scan->raw)) {
scan->mode = SCAN_RAW_DELIMITER;
scan->delimiter_length = 0;
return false;
}
return directive_scan_code(scan, c);
}
static bool directive_scan_raw_delimiter(DirectiveScan *scan, int32_t c) {
if (c == '(') {
scan->mode = SCAN_RAW_BODY;
scan->closing_length = scan->closing_end = 0;
return false;
}
if (scan->delimiter_length < sizeof scan->delimiter && c >= 0x21 && c <= 0x7e && c != ')' && c != '\\') {
scan->delimiter[scan->delimiter_length++] = (char)c;
return false;
}
char replay[sizeof scan->delimiter];
unsigned length = scan->delimiter_length;
memcpy(replay, scan->delimiter, length);
scan->mode = SCAN_QUOTE;
scan->quote = '"';
scan->escape = false;
for (unsigned i = 0; i < length; i++) directive_scan_logical(scan, replay[i]);
return directive_scan_logical(scan, c);
}
static void directive_scan_raw_body(DirectiveScan *scan, int32_t c) {
unsigned size = scan->delimiter_length + 2;
unsigned oldest = scan->closing_end;
scan->closing[oldest] = c > 0 && c < 0x80 ? (char)c : 0;
scan->closing_end = oldest + 1 == size ? 0 : oldest + 1;
if (scan->closing_length < size) scan->closing_length++;
if (c != '"' || scan->closing_length < size || scan->closing[scan->closing_end] != ')') return;
for (unsigned i = 0; i < scan->delimiter_length; i++) {
if (scan->closing[(scan->closing_end + 1 + i) % size] != scan->delimiter[i]) return;
}
scan->mode = SCAN_CODE;
}
static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c) {
recent = recent << 8 | ((uint32_t)c < 0x80 ? (uint64_t)c : 0);
if (directive_pragma_word(recent)) *scan->pragma = true;
return recent;
}
static inline bool directive_pragma_word(uint64_t recent) {
uint64_t word = 0;
for (unsigned i = 0; i < sizeof pragma_word - 1; i++) word = word << 8 | (uint8_t)pragma_word[i];
return (recent & ((1ull << 8 * (sizeof pragma_word - 1)) - 1)) == word;
}
static bool directive_horizontal(int32_t c) {
return c == ' ' || c == '\t' || c == '\v' || c == '\f';
}
static bool directive_newline(int32_t c) {
return c == '\n' || c == '\r';
}
static inline bool directive_grow_items(void **items, uint32_t count, uint32_t *capacity, size_t item_size) {
if (count < *capacity) return true;
uint32_t next = *capacity ? *capacity * 2 : 16;
void *grown = next > *capacity && (size_t)next <= SIZE_MAX / item_size ? realloc(*items, (size_t)next * item_size) : NULL;
if (!grown) return false;
*items = grown;
*capacity = next;
return true;
}
static void directive_spans_push(DirectiveSpans *spans, uint32_t start, uint32_t end) {
if (!directive_grow_items((void **)&spans->items, spans->count, &spans->capacity, sizeof(*spans->items))) {
spans->failed = true;
return;
}
spans->items[spans->count++] = (DirectiveSpan){start, end};
}
#endif