MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / tokenize

Function tokenize

packages/ast/parse/linen/tokenize/index.js:9–89  ·  view source on GitHub ↗
(input)

Source from the content-addressed store, hash-verified

7}
8
9function tokenize(input) {
10 const tokens = [];
11 const length = input.length;
12 let index = 0;
13 let type = 'text';
14 let value = '';
15
16 function flush() {
17 if (value) {
18 tokens.push({ type, value });
19 value = '';
20 }
21 }
22
23 while (index < length) {
24 const current = input[index];
25
26 function serialize(tag, node) {
27 if (type === 'code' || type === 'pre') {
28 value += current;
29 } else {
30 const previous = input[index - 1];
31 const next = input[index + 1];
32 if (next === tag || isWhitespace(next)) {
33 value += current;
34 } else if (next && (!previous || isWhitespace(previous))) {
35 flush();
36 value += current;
37 type = node;
38 } else {
39 value += current;
40 }
41 }
42 }
43
44 if (current === '@' && !['user', 'signal'].includes(type)) {
45 serialize('@', 'user');
46 } else if (current === '!' && !['user', 'signal'].includes(type)) {
47 serialize('!', 'signal');
48 } else if (isWhitespace(current) || isSpecialCharacter(current)) {
49 if (type === 'user' || type === 'signal') {
50 flush();
51 type = 'text';
52 }
53 value += current;
54 } else if (
55 current === '`' &&
56 input[index + 1] === '`' &&
57 input[index + 2] === '`'
58 ) {
59 if (type === 'pre') {
60 value += '```';
61 index += 2;
62 flush();
63 type = 'text';
64 } else {
65 flush();
66 type = 'pre';

Callers

nothing calls this directly

Calls 4

isSpecialCharacterFunction · 0.85
serializeFunction · 0.70
isWhitespaceFunction · 0.70
flushFunction · 0.70

Tested by

no test coverage detected