@@ -279,20 +279,80 @@ void cbm_channels_push(CBMChannelArray *arr, CBMArena *a, CBMChannel ch) {
279279typedef struct {
280280 const char * string ;
281281 uint32_t length ;
282+ /* #2078: serve one "\n" at offset `length` -- see cbm_parse_source. */
283+ bool virtual_newline ;
282284} CBMStringInput ;
283285
284286static const char * cbm_string_read (void * payload , uint32_t byte , TSPoint point ,
285287 uint32_t * bytes_read ) {
286288 (void )point ;
287289 CBMStringInput * self = (CBMStringInput * )payload ;
288290 if (byte >= self -> length ) {
291+ if (self -> virtual_newline && byte == self -> length ) {
292+ * bytes_read = 1 ;
293+ return "\n" ;
294+ }
289295 * bytes_read = 0 ;
290296 return "" ;
291297 }
292298 * bytes_read = self -> length - byte ;
293299 return self -> string + byte ;
294300}
295301
302+ /* Where the parser stands after reading all `len` real bytes. */
303+ static TSPoint cbm_source_end_point (const char * source , uint32_t len ) {
304+ TSPoint end = {0 , 0 };
305+ const char * p = source ;
306+ const char * stop = source + len ;
307+ const char * nl ;
308+ while (p < stop && (nl = memchr (p , '\n' , (size_t )(stop - p ))) != NULL ) {
309+ end .row ++ ;
310+ p = nl + 1 ;
311+ }
312+ end .column = (uint32_t )(stop - p );
313+ return end ;
314+ }
315+
316+ /* #2078: parse every file as if its last line were terminated.
317+ *
318+ * Many grammars treat the line terminator as part of the construct it ends --
319+ * a markdown fence, heading or list marker, a Makefile recipe, a Dockerfile
320+ * instruction. A file whose last byte is not "\n" leaves that construct
321+ * unterminated: at best a zero-width MISSING token (#1610/#1746 excuse those at
322+ * EOF), at worst a WIDTH-BEARING error that costs the whole last line or, for a
323+ * one-line file, the whole file, and sometimes a silent loss (an unterminated
324+ * markdown heading produced no Section and no flag). The same bytes plus one
325+ * "\n" parse clean, so the absent newline is supplied instead of excused.
326+ *
327+ * The parser reads one virtual "\n" past EOF; afterwards a single tree edit
328+ * deletes it again. tree-sitter's own edit arithmetic then clamps every node
329+ * that reached into the virtual byte back to the real end -- byte offsets AND
330+ * row/column points -- so every consumer (def line ranges, node text, call
331+ * byte spans, parse-coverage ranges, the retained tree the LSP passes reuse)
332+ * sees only real bytes and real lines; nothing downstream needs to know. The
333+ * caller's buffer is never copied or written.
334+ *
335+ * A file that is empty or already ends in "\n" is parsed exactly as before. */
336+ TSTree * cbm_parse_source (TSParser * parser , const char * source , uint32_t source_len ,
337+ TSParseOptions opts ) {
338+ CBMStringInput input = {source , source_len , source_len > 0 && source [source_len - 1 ] != '\n' };
339+ TSInput ts_input = {& input , cbm_string_read , TSInputEncodingUTF8 , NULL };
340+ TSTree * tree = ts_parser_parse_with_options (parser , NULL , ts_input , opts );
341+ if (tree && input .virtual_newline ) {
342+ TSPoint end = cbm_source_end_point (source , source_len );
343+ TSInputEdit drop_virtual_newline = {
344+ .start_byte = source_len ,
345+ .old_end_byte = source_len + 1 ,
346+ .new_end_byte = source_len ,
347+ .start_point = end ,
348+ .old_end_point = {end .row + 1 , 0 },
349+ .new_end_point = end ,
350+ };
351+ ts_tree_edit (tree , & drop_virtual_newline );
352+ }
353+ return tree ;
354+ }
355+
296356// --- Parse timeout callback ---
297357
298358/* Budget for the tree-sitter progress callback. The PRIMARY gate is per-thread
@@ -1262,7 +1322,11 @@ static void cbm_error_regions_push(cbm_error_regions_t *acc, TSNode n) {
12621322 * #1746: the Dockerfile grammar places that zero-width missing newline before
12631323 * trailing whitespace rather than at raw EOF. Preserve the broad exact-EOF
12641324 * rule above; only extend it past blanks when the missing token is specifically
1265- * a newline. */
1325+ * a newline.
1326+ *
1327+ * #2078: cbm_parse_source now supplies the absent final newline, so a MISSING
1328+ * newline at EOF no longer arises from it; the rule stays for the other
1329+ * zero-width terminators a grammar can leave at EOF. */
12661330static bool cbm_is_blank_not_newline (char c ) {
12671331 return c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\r' ;
12681332}
@@ -2206,8 +2270,6 @@ static void extract_cpp_branch_views(const CBMExtractCtx *raw, const TSLanguage
22062270 return ;
22072271 }
22082272 ts_parser_reset (parser );
2209- CBMStringInput input = {view , (uint32_t )raw -> source_len };
2210- TSInput ts_input = {& input , cbm_string_read , TSInputEncodingUTF8 , NULL };
22112273 TSParseOptions opts = {0 };
22122274 CBMParseBudget budget = {0 }; // cppcheck-suppress unreadVariable
22132275 if (timeout_micros > 0 ) {
@@ -2217,7 +2279,9 @@ static void extract_cpp_branch_views(const CBMExtractCtx *raw, const TSLanguage
22172279 opts .payload = & budget ;
22182280 opts .progress_callback = cbm_timeout_cb ;
22192281 }
2220- TSTree * tree = ts_parser_parse_with_options (parser , NULL , ts_input , opts );
2282+ /* A view keeps the raw source's length and line structure, so it is
2283+ * parsed under the same terminated-last-line rule (#2078). */
2284+ TSTree * tree = cbm_parse_source (parser , view , (uint32_t )raw -> source_len , opts );
22212285 if (!tree ) {
22222286 return ;
22232287 }
@@ -2325,15 +2389,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
23252389
23262390 uint64_t t0 = now_ns ();
23272391
2328- // Build string input + timeout options for parse_with_options
2329- CBMStringInput str_input = {source , (uint32_t )source_len };
2330- TSInput ts_input = {
2331- & str_input ,
2332- cbm_string_read ,
2333- TSInputEncodingUTF8 ,
2334- NULL ,
2335- };
2336-
2392+ // Timeout options for parse_with_options
23372393 TSParseOptions opts = {0 };
23382394 CBMParseBudget budget = {0 }; // cppcheck-suppress unreadVariable
23392395 uint64_t budget_ns = 0 ;
@@ -2361,12 +2417,14 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
23612417 /* #1735: a SQL data dump's literal-only INSERT rows carry no graph content
23622418 * but dominate its parse. Keep them out through included ranges; offsets
23632419 * and positions of everything kept are unchanged. The parser is
2364- * thread-local and reused, so the ranges are cleared right after. */
2420+ * thread-local and reused, so the ranges are cleared right after. The last
2421+ * range ends at the real EOF, so the virtual final newline cbm_parse_source
2422+ * supplies (#2078) stays outside a ranged parse. */
23652423 CBMSqlKeptRanges sql_kept = {NULL , 0 };
23662424 bool sql_ranged = language == CBM_LANG_SQL && cbm_sql_values_exclusion_on (rel_path ) &&
23672425 cbm_sql_values_kept_ranges (source , (uint32_t )source_len , & sql_kept ) &&
23682426 ts_parser_set_included_ranges (parser , sql_kept .items , sql_kept .count );
2369- TSTree * tree = ts_parser_parse_with_options (parser , NULL , ts_input , opts );
2427+ TSTree * tree = cbm_parse_source (parser , source , ( uint32_t ) source_len , opts );
23702428 if (sql_ranged ) {
23712429 (void )ts_parser_set_included_ranges (parser , NULL , 0 );
23722430 }
@@ -2587,7 +2645,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
25872645 TSParser * pp_parser = get_thread_parser (ts_lang , language );
25882646 if (pp_parser ) {
25892647 ts_parser_reset (pp_parser );
2590- CBMStringInput pp_input = {expanded , (uint32_t )expanded_len };
2648+ CBMStringInput pp_input = {expanded , (uint32_t )expanded_len , false };
25912649 TSInput pp_ts_input = {
25922650 & pp_input ,
25932651 cbm_string_read ,
0 commit comments