Skip to content

Commit 190abae

Browse files
authored
Merge pull request #2389 from harshitaajoshi/fix/php-inline-html-2000
fix(extract): parse PHP past inline HTML so symbols after ?> reach the graph (#2000)
2 parents 5689ea5 + 08b84e4 commit 190abae

8 files changed

Lines changed: 675 additions & 0 deletions

File tree

‎Makefile.cbm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ EXTRACTION_SRCS = \
417417
$(CBM_DIR)/openapi.c \
418418
$(CBM_DIR)/extract_nextjs.c \
419419
$(CBM_DIR)/sql_values.c \
420+
$(CBM_DIR)/php_inline_html.c \
420421
$(CBM_DIR)/helpers.c \
421422
$(CBM_DIR)/cpp_branch_views.c \
422423
$(CBM_DIR)/result_compact.c \

‎internal/cbm/cbm.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,18 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage
33193319
return r;
33203320
}
33213321

3322+
/* The bytes the grammar parses. PHP that leaves PHP mode (`?> markup <?php`)
3323+
* is parsed with its markup blanked (#2000); the rewrite keeps the length and
3324+
* every line break, so offsets and line numbers still refer to the file on
3325+
* disk. Every other language parses the file as it is. */
3326+
static const char *extract_parse_bytes(CBMArena *a, CBMLanguage language, const char *source,
3327+
int source_len) {
3328+
if (language == CBM_LANG_PHP) {
3329+
return cbm_php_mask_inline_html(a, source, source_len);
3330+
}
3331+
return source;
3332+
}
3333+
33223334
/* Initial block for the per-file traversal scratch arena, chosen by measuring
33233335
* arena_grow on a 14k-file TypeScript tree: it fires on one file in 12,000 at
33243336
* both this size and at 1 MB, and on most files at 256 KB, where the two
@@ -3739,6 +3751,8 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
37393751
return result;
37403752
}
37413753

3754+
source = extract_parse_bytes(a, language, source, source_len);
3755+
37423756
// Skip pathologically nested Perl before tree-sitter's recursive GLR stack
37433757
// merge overflows a small stack during the parse (see
37443758
// cbm_source_nesting_exceeds). Scoped to Perl: its ambiguous call grammar is

‎internal/cbm/cbm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,10 @@ void cbm_extract_definitions_without_module(CBMExtractCtx *ctx);
13851385
// per ref()/source() call. No-op unless the file parses as SQL and actually
13861386
// contains a dbt builtin call. Defined in extract_dbt.c.
13871387
void cbm_extract_dbt(CBMExtractCtx *ctx);
1388+
// PHP source with its inline-HTML regions blanked so the php_only grammar can
1389+
// parse past `?>` (#2000). Same length and line breaks as the input; returns
1390+
// `source` itself when nothing needs rewriting. Defined in php_inline_html.c.
1391+
const char *cbm_php_mask_inline_html(CBMArena *arena, const char *source, int source_len);
13881392
void cbm_extract_imports(CBMExtractCtx *ctx);
13891393
void cbm_extract_usages(CBMExtractCtx *ctx);
13901394
void cbm_extract_semantic(CBMExtractCtx *ctx);

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "php_lsp.h"
2121
#include "lsp_node_iter.h"
2222
#include "lsp_work.h"
23+
#include "../cbm.h"
2324
#include "../helpers.h"
2425
#include "../../../src/foundation/hash_table.h"
2526
#include <ctype.h>
@@ -4795,6 +4796,9 @@ void cbm_run_php_lsp_cross_with_bindings(CBMArena *arena, const char *source, in
47954796
const CBMPHPImportBindings *bindings) {
47964797
if (!arena || !source || source_len <= 0 || !out)
47974798
return;
4799+
/* The retained source is the file on disk; read it the way the per-file
4800+
* extract did, so the cached tree and node text agree (#2000). */
4801+
source = cbm_php_mask_inline_html(arena, source, source_len);
47984802

47994803
TSParser *parser = NULL;
48004804
TSTree *tree = cached_tree;

0 commit comments

Comments
 (0)