#ifndef TREE_SITTER_C_DIRECTIVE_UNITS_H_
#define TREE_SITTER_C_DIRECTIVE_UNITS_H_
enum { UNIT_WORD = 1, UNIT_SPACE = 2, UNIT_PUNCTUATION = 4, UNIT_UNDERSCORE = 8, UNIT_P = 16, UNIT_LINE = 32, UNIT_BLOCK = 64, UNIT_QUOTE = 128 };
#define UNIT_PRAGMA (UNIT_UNDERSCORE | UNIT_P)
#define UNIT_CLASS_COUNT 128
typedef struct {
uint32_t consumed, line_feeds, tail;
} DirectiveUnits;
typedef struct {
const char *units;
const uint8_t *classes;
uint32_t count, i, start, line_feeds, line, quote;
int32_t previous;
DirectiveScanMode mode;
bool lines, beginning, star, number, pragma, fresh;
uint8_t seen;
} DirectiveUnitPass;
#if defined(__GNUC__) || defined(__clang__)
#define DIRECTIVE_INLINE __attribute__((always_inline)) static inline
#else
#define DIRECTIVE_INLINE static inline
#endif
DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c);
DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, DirectiveScanMode mode, uint32_t length);
DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE void directive_units_note_pragma(DirectiveUnitPass *pass, unsigned size);
DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index);
static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte);
static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end);
static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end);
DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const char *units, uint32_t count, unsigned size, uint32_t byte, bool lines) {
DirectiveUnitPass pass = {
.units = units, .classes = scan->classes, .count = count, .quote = (uint32_t)scan->quote, .previous = scan->previous,
.mode = scan->mode, .lines = lines, .beginning = scan->beginning, .star = scan->star, .number = scan->number,
};
for (bool more = true; more;) {
switch (pass.mode) {
case SCAN_WORD:
more = directive_units_word(&pass, size);
break;
case SCAN_LINE_COMMENT:
more = directive_units_line_comment(&pass, size);
break;
case SCAN_BLOCK_COMMENT:
more = directive_units_block_comment(&pass, size);
break;
case SCAN_QUOTE:
more = directive_units_quoted(&pass, size);
break;
default:
more = directive_units_code(&pass, size);
}
}
if (!lines) return (DirectiveUnits){pass.mode == SCAN_CODE ? pass.i : pass.start, 0, 0};
if (!pass.i) return (DirectiveUnits){0};
if (pass.mode != SCAN_CODE) directive_units_note_pragma(&pass, size);
if (pass.pragma) *scan->pragma = true;
scan->mode = pass.mode;
scan->beginning = pass.beginning;
scan->star = pass.star;
scan->number = pass.number;
scan->quote = (int32_t)pass.quote;
scan->escape = false;
directive_scan_units_end(scan, units, size, pass.fresh ? pass.start : UINT32_MAX, pass.i, byte);
return (DirectiveUnits){pass.i, pass.line_feeds, pass.i - pass.line};
}
DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size) {
uint32_t c = 0;
uint8_t class = 0, plain = 0;
while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
((class = pass->classes[c]) & (UNIT_SPACE | UNIT_PUNCTUATION))) {
plain |= class;
pass->i++;
}
if (plain & UNIT_PUNCTUATION) pass->beginning = false;
if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
if (!(class & UNIT_WORD)) return directive_units_code_token(pass, size, c);
pass->beginning = false;
pass->number = c >= '0' && c <= '9';
directive_units_open(pass, SCAN_WORD, 1);
pass->seen = class;
return true;
}
DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
if (c == '\n') {
if (!directive_units_line_feed(pass, size)) return false;
pass->beginning = true;
return true;
}
if (c == '"' || c == '\'') {
pass->quote = c;
directive_units_open(pass, SCAN_QUOTE, 1);
} else if (c == '/') {
if (pass->i + 1 >= pass->count) return false;
uint32_t next = directive_unit(pass->units, size, pass->i + 1);
if (next == '\\') return false;
if (next == '/' || next == '*') {
pass->star = false;
directive_units_open(pass, next == '/' ? SCAN_LINE_COMMENT : SCAN_BLOCK_COMMENT, 2);
return true;
}
pass->i++;
} else if (c == '#' && !pass->beginning) {
pass->i++;
} else {
return false;
}
pass->beginning = false;
return true;
}
DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size) {
uint32_t c = 0;
uint8_t class;
for (;;) {
while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
((class = pass->classes[c]) & UNIT_WORD)) {
pass->seen |= class;
pass->i++;
}
if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT || c == '\\') return false;
if (!pass->number) break;
int32_t previous = pass->i > (pass->fresh ? pass->start : 0)
? (int32_t)directive_unit(pass->units, size, pass->i - 1)
: pass->previous;
bool exponent_sign = (c == '+' || c == '-') &&
(previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
if (!(c == '.' || c == '\'' || exponent_sign)) break;
pass->i++;
}
if (c == '"' && (!pass->fresh || pass->i - pass->start < sizeof ((GnuRawStringPrefix *)0)->value)) return false;
return directive_units_close(pass, size);
}
DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size) {
for (;;) {
if (pass->i >= pass->count) return false;
uint32_t c = directive_unit(pass->units, size, pass->i);
if (c >= UNIT_CLASS_COUNT) return false;
uint8_t class = pass->classes[c];
if (class & UNIT_LINE) {
pass->seen |= class;
pass->i++;
} else if (c == '\\' && directive_units_escape(pass, size)) {
pass->i++;
} else if (c == '\n' && !pass->lines) {
pass->mode = SCAN_CODE;
return false;
} else if (c == '\n' && directive_units_line_feed(pass, size)) {
pass->beginning = true;
return directive_units_close(pass, size);
} else {
return false;
}
}
}
DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size) {
for (;;) {
if (pass->i >= pass->count) return false;
uint32_t c = directive_unit(pass->units, size, pass->i);
if (c >= UNIT_CLASS_COUNT) return false;
uint8_t class = pass->classes[c];
bool star = pass->star;
pass->star = c == '*';
if (class & UNIT_BLOCK) {
pass->seen |= class;
pass->i++;
} else if (c == '*') {
pass->i++;
} else if (c == '/') {
pass->i++;
if (star) return directive_units_close(pass, size);
} else if (c == '\\' && directive_units_escape(pass, size)) {
pass->i++;
} else if (c != '\n' || !directive_units_line_feed(pass, size)) {
pass->star = star;
return false;
}
}
}
DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size) {
for (;;) {
if (pass->i >= pass->count) return false;
uint32_t c = directive_unit(pass->units, size, pass->i);
if (c >= UNIT_CLASS_COUNT) return false;
uint8_t class = pass->classes[c];
if (class & UNIT_QUOTE) {
pass->seen |= class;
pass->i++;
} else if (c == pass->quote) {
pass->i++;
return directive_units_close(pass, size);
} else if (c == '"' || c == '\'') {
pass->i++;
} else if (c == '\\' && directive_units_escape(pass, size)) {
pass->seen |= pass->classes[directive_unit(pass->units, size, pass->i + 1)];
pass->i += 2;
} else {
return false;
}
}
}
DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, DirectiveScanMode mode, uint32_t length) {
pass->mode = mode;
pass->start = pass->i;
pass->fresh = true;
pass->seen = 0;
pass->i += length;
}
DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size) {
directive_units_note_pragma(pass, size);
pass->mode = SCAN_CODE;
return true;
}
DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size) {
if (!pass->lines || pass->i + 1 >= pass->count || directive_unit(pass->units, size, pass->i + 1) == '\r') return false;
pass->line_feeds++;
pass->line = ++pass->i;
return true;
}
DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size) {
if (pass->i + 1 >= pass->count) return false;
uint32_t next = directive_unit(pass->units, size, pass->i + 1);
return next < UNIT_CLASS_COUNT && (pass->classes[next] & (UNIT_LINE | UNIT_SPACE)) == UNIT_LINE;
}
DIRECTIVE_INLINE void directive_units_note_pragma(DirectiveUnitPass *pass, unsigned size) {
if ((pass->seen & UNIT_PRAGMA) != UNIT_PRAGMA) return;
if (directive_units_pragma(pass->units, size, pass->fresh ? pass->start : 0, pass->i)) pass->pragma = true;
}
DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index) {
if (size == 1) return (uint8_t)units[index];
uint16_t unit;
memcpy(&unit, units + 2 * (size_t)index, sizeof unit);
return unit;
}
static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte) {
if (scan->mode == SCAN_CODE) {
scan->recent = 0;
} else {
uint64_t recent = from == UINT32_MAX ? scan->recent : 0;
uint32_t first = from == UINT32_MAX ? 0 : from;
for (uint32_t k = end - first > 8 ? end - 8 : first; k < end; k++) recent = recent << 8 | directive_unit(units, size, k);
scan->recent = recent;
if (from != UINT32_MAX && scan->mode == SCAN_WORD) scan->raw = (GnuRawStringPrefix){0};
if (scan->mode == SCAN_WORD && end > first) directive_scan_units_word(scan, units, size, first, end);
}
uint32_t run = end;
while (run > 0) {
uint32_t c = directive_unit(units, size, run - 1);
if (!directive_horizontal((int32_t)c) && c != '\n' && c != '\\') break;
run--;
}
bool continued = run == 0 && scan->in_run;
scan->in_run = run < end;
if (!scan->in_run) return;
if (!continued) {
scan->run_spans = 0;
scan->run_start = byte + run * size;
}
scan->run_end = byte + (end - 1) * size + 1;
}
static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end) {
scan->previous = (int32_t)directive_unit(units, size, end - 1);
if (scan->raw.length >= sizeof scan->raw.value) return;
if (end - first > sizeof scan->raw.value - 1 - scan->raw.length) {
gnu_raw_string_prefix_invalidate(&scan->raw);
return;
}
for (uint32_t k = first; k < end; k++) gnu_raw_string_prefix_character(&scan->raw, (int32_t)directive_unit(units, size, k));
}
static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end) {
uint32_t length = sizeof pragma_word - 1;
for (uint32_t i = start; i + length <= end; i++) {
uint32_t matched = 0;
while (matched < length && directive_unit(units, size, i + matched) == (uint8_t)pragma_word[matched]) matched++;
if (matched == length) return true;
}
return false;
}
static bool directive_pragma_pending(uint64_t recent) {
uint64_t word = 0;
for (unsigned length = 1; length < sizeof pragma_word - 1; length++) {
word = word << 8 | (uint8_t)pragma_word[length - 1];
if ((recent & ((1ull << 8 * length) - 1)) == word) return true;
}
return false;
}
static void directive_unit_classes(uint8_t *classes) {
for (int32_t c = 0; c < UNIT_CLASS_COUNT; c++) {
bool text = c != 0 && !directive_newline(c) && c != '\\';
uint8_t code = directive_scan_ascii_word(c) ? UNIT_WORD : directive_horizontal(c) ? UNIT_SPACE : directive_scan_ascii_plain(c) ? UNIT_PUNCTUATION : 0;
uint8_t letter = c == '_' ? UNIT_UNDERSCORE : c == 'P' ? UNIT_P : 0;
uint8_t block = text && c != '*' && c != '/' ? UNIT_BLOCK : 0, quote = text && c != '"' && c != '\'' ? UNIT_QUOTE : 0;
classes[c] = (uint8_t)(code | letter | (text ? UNIT_LINE : 0) | block | quote);
}
}
#endif