MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / parseCode

Function parseCode

src/common/syntax-highlighter.ts:302–335  ·  view source on GitHub ↗
(
  text: string,
  grammar: SyntaxGrammar = defaultGrammar,
  pos = 0
)

Source from the content-addressed store, hash-verified

300};
301
302export function parseCode(
303 text: string,
304 grammar: SyntaxGrammar = defaultGrammar,
305 pos = 0
306): CodeSpan[] {
307 const spans: CodeSpan[] = [];
308
309 const buf = new Buffer(text, pos);
310
311 while (!buf.atEnd()) {
312 const result =
313 grammar.comment?.(buf) ??
314 grammar.number?.(buf) ??
315 grammar.regex?.(buf) ??
316 grammar.string?.(buf) ??
317 grammar.keyword?.(buf) ??
318 grammar.identifier?.(buf);
319
320 if (result) {
321 spans.push(result);
322 continue;
323 }
324
325 // If last span is default, coalesce with this one
326 const lastSpan = spans.length ? spans[spans.length - 1] : null;
327 if (lastSpan?.tag === 'default') {
328 lastSpan.content += buf.consume();
329 } else {
330 spans.push({ content: buf.consume(), tag: 'default' });
331 }
332 }
333
334 return spans;
335}
336
337function renderCode(spans: CodeSpan[], theme = DEFAULT_THEME): StyledSpan[] {
338 return spans.map((span) => {

Callers 3

highlightCodeSpanFunction · 0.70
highlightCodeBlockFunction · 0.70

Calls 4

atEndMethod · 0.95
consumeMethod · 0.95
numberMethod · 0.65
stringMethod · 0.65

Tested by

no test coverage detected