MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / scan_string_content

Function scan_string_content

codegraph-kernel/grammars/kotlin/scanner.c:180–364  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178}
179
180static bool scan_string_content(TSLexer *lexer, Stack *stack,
181 const bool *valid_symbols) {
182 if (stack->size < 2) return false; // Stack is empty. We're not in a string.
183 uint8_t packed = (uint8_t)stack->contents[stack->size - 2];
184 uint16_t prefix_len =
185 (uint16_t)(((packed >> PREFIX_HI_SHIFT) << 8) |
186 (uint8_t)stack->contents[stack->size - 1]);
187 // A prefix we could not record is longer than PREFIX_LEN_MAX, so every run
188 // up to PREFIX_LEN_MAX is certainly shorter than it and is literal content.
189 // Treating the prefix as PREFIX_LEN_MAX + 1 says exactly that, and leaves
190 // longer runs — the only ones we cannot decide — interpolating as before.
191 uint32_t trigger_len = (prefix_len == PREFIX_LEN_UNREPRESENTABLE)
192 ? (uint32_t)PREFIX_LEN_MAX + 1
193 : (uint32_t)prefix_len;
194 Delimiter raw_delim = (Delimiter)(packed & DELIMITER_MASK);
195 bool is_triple = (raw_delim & 1) != 0;
196 char end_char = is_triple ? (char)(raw_delim - 1) : (char)raw_delim;
197 bool has_content = false;
198 while (true) {
199 if (lexer->lookahead == '\0') {
200 // Stop at real end-of-input, but treat a literal NUL byte (not EOF) as
201 // ordinary string content: consume it to guarantee forward progress.
202 // Returning here without advancing would leave the lexer stuck at the
203 // same offset (mirrors the NUL handling in scan_multiline_comment).
204 if (lexer->eof(lexer)) break;
205 advance(lexer);
206 has_content = true;
207 continue;
208 }
209 if (lexer->lookahead == '$') {
210 // If we already have content, stop here so the caller can emit it
211 // before we deal with the potential interpolation.
212 if (has_content) {
213 lexer->result_symbol = STRING_CONTENT;
214 return true;
215 }
216 // Kotlin 2.1 multi-dollar interpolation: in a string with prefix_len N,
217 // exactly N consecutive '$' followed by alpha/'{' triggers interpolation.
218 // Excess leading '$' signs are literal string content.
219 //
220 // Strategy: consume the first '$' and mark_end there, then count
221 // remaining '$' signs. If total > prefix_len, return STRING_CONTENT
222 // for just the first '$' (tree-sitter rewinds to mark_end). On the
223 // next scan call, the remaining dollars will be re-examined.
224 advance(lexer);
225 lexer->mark_end(lexer);
226 // Saturate one past PREFIX_LEN_MAX. Only the comparison against
227 // trigger_len matters and trigger_len never exceeds PREFIX_LEN_MAX + 1,
228 // so capping keeps every decision below correct while making it
229 // impossible for a long run of '$' to wrap and read as a short one.
230 uint32_t additional_dollars = 0;
231 while (lexer->lookahead == '$') {
232 advance(lexer);
233 if (additional_dollars <= PREFIX_LEN_MAX) additional_dollars++;
234 }
235 uint32_t total_dollars = 1 + additional_dollars;
236 // The counter saturates strictly above trigger_len, so a run longer than
237 // the cap always reads as "more than the prefix", never as a match.

Callers 1

scan_tokenFunction · 0.70

Calls 2

stack_popFunction · 0.85
advanceFunction · 0.70

Tested by

no test coverage detected