Blank C# preprocessor directives and the ``#if`` branches that derail parsing, or None. tree-sitter-c-sharp parses the raw text, directives and all branches together. Its error recovery around a directive can close the enclosing class early, so every later member loses its type owner, i
(source: bytes)
| 4696 | |
| 4697 | |
| 4698 | def _blank_csharp_preprocessor(source: bytes) -> bytes | None: |
| 4699 | """Blank C# preprocessor directives and the ``#if`` branches that derail parsing, or None. |
| 4700 | |
| 4701 | tree-sitter-c-sharp parses the raw text, directives and all branches |
| 4702 | together. Its error recovery around a directive can close the enclosing |
| 4703 | class early, so every later member loses its type owner, its field typing |
| 4704 | and its calls. |
| 4705 | |
| 4706 | Every directive line (``#if``/``#elif``/``#else``/``#endif``, and also |
| 4707 | ``#region``, ``#pragma``, ``#nullable``, ``#define`` ...) is blanked. The |
| 4708 | first branch of each ``#if`` group is always kept. A later ``#elif`` / |
| 4709 | ``#else`` branch is kept too, so members that exist only there (an |
| 4710 | older-framework fallback) stay in the graph (#2631), unless keeping it |
| 4711 | would derail the parse: |
| 4712 | |
| 4713 | * its ``{}`` or ``()`` do not net to zero on their own (``class A : B {`` |
| 4714 | ``#else`` ``class A {``), or |
| 4715 | * it declares a member (name + arity) that a kept branch of the same group |
| 4716 | already declares (``void Write(Span<char> s)`` ``#else`` |
| 4717 | ``void Write(string s)`` sharing one body). |
| 4718 | |
| 4719 | Nested groups are decided first, so an outer branch is judged on what is |
| 4720 | left of it. No symbols are evaluated. Measured against the compiler on |
| 4721 | Newtonsoft.Json, Dapper and AutoMapper. |
| 4722 | |
| 4723 | The rewrite is **byte-length preserving**: removed bytes become spaces and |
| 4724 | every ``\\n``/``\\r`` is kept, so every remaining byte keeps its line and |
| 4725 | column and source locations stay accurate (the #2876 contract). |
| 4726 | |
| 4727 | A ``#`` line inside a comment or a multi-line string is not a directive; |
| 4728 | comments and string literals are skipped while looking for line starts. |
| 4729 | Returns None when the file has no directive. |
| 4730 | """ |
| 4731 | n = len(source) |
| 4732 | # (line_start, line_end, keyword) for every directive line. |
| 4733 | directives: list[tuple[int, int, bytes]] = [] |
| 4734 | i = 0 |
| 4735 | while i < n: |
| 4736 | # i is at a line start here. |
| 4737 | p = i |
| 4738 | while p < n and source[p:p + 1] in (b" ", b"\t", b"\v", b"\f"): |
| 4739 | p += 1 |
| 4740 | if source[p:p + 1] == b"#": |
| 4741 | nl = source.find(b"\n", p) |
| 4742 | end = n if nl == -1 else nl |
| 4743 | k = p + 1 |
| 4744 | while k < end and source[k:k + 1] in (b" ", b"\t"): |
| 4745 | k += 1 |
| 4746 | w = k |
| 4747 | while w < end and source[w:w + 1].isalpha(): |
| 4748 | w += 1 |
| 4749 | directives.append((i, end, source[k:w])) |
| 4750 | i = end + 1 |
| 4751 | continue |
| 4752 | # Walk the line as code; a comment or string may run past its end. |
| 4753 | j = p |
| 4754 | while j < n: |
| 4755 | ch = source[j:j + 1] |
no test coverage detected