Classify the line at src[pos..end): a conditional directive is a '#' in column 0, optional blanks, then the directive word — the same lines the Haskell scanner treats as conditionals.
| 21 | // column 0, optional blanks, then the directive word — the same lines the |
| 22 | // Haskell scanner treats as conditionals. |
| 23 | static int cpp_line_kind(const char *src, int pos, int end) { |
| 24 | if (pos >= end || src[pos] != '#') { |
| 25 | return CPP_LINE_CODE; |
| 26 | } |
| 27 | int p = pos + 1; |
| 28 | while (p < end && (src[p] == ' ' || src[p] == '\t')) { |
| 29 | p++; |
| 30 | } |
| 31 | int w = p; |
| 32 | while (w < end && cpp_word_char(src[w])) { |
| 33 | w++; |
| 34 | } |
| 35 | static const struct { |
| 36 | const char *word; |
| 37 | int kind; |
| 38 | } words[] = { |
| 39 | {"if", CPP_LINE_START}, {"ifdef", CPP_LINE_START}, {"ifndef", CPP_LINE_START}, |
| 40 | {"else", CPP_LINE_ELSE}, {"elif", CPP_LINE_ELSE}, {"elifdef", CPP_LINE_ELSE}, |
| 41 | {"elifndef", CPP_LINE_ELSE}, {"endif", CPP_LINE_END}, |
| 42 | }; |
| 43 | size_t n = (size_t)(w - p); |
| 44 | for (size_t i = 0; i < sizeof(words) / sizeof(words[0]); i++) { |
| 45 | if (strlen(words[i].word) == n && memcmp(src + p, words[i].word, n) == 0) { |
| 46 | return words[i].kind; |
| 47 | } |
| 48 | } |
| 49 | return CPP_LINE_CODE; |
| 50 | } |
| 51 | |
| 52 | static int cpp_line_end(const char *src, int pos, int len) { |
| 53 | const char *nl = memchr(src + pos, '\n', (size_t)(len - pos)); |
no test coverage detected