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

Method tokenize

scripts/rubi/wl-parser.ts:72–153  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

70 }
71
72 private tokenize(): void {
73 const s = this.src;
74 while (this.pos < s.length) {
75 let spaceBefore = false;
76 for (;;) {
77 if (this.pos < s.length && /\s/.test(s[this.pos])) {
78 spaceBefore = true;
79 this.pos++;
80 } else if (s.startsWith('(*', this.pos)) {
81 spaceBefore = true;
82 this.skipComment();
83 } else break;
84 }
85 if (this.pos >= s.length) break;
86 const c = s[this.pos];
87 // \[Star] is Times (Rubi uses it for display-deferred multiplication);
88 // any other \[…] escape is unsupported — fail loudly.
89 if (c === '\\' && s[this.pos + 1] === '[') {
90 const m = /^\\\[([A-Za-z]+)\]/.exec(s.slice(this.pos));
91 if (m?.[1] === 'Star') {
92 this.tokens.push({ kind: 'punct', value: '*', spaceBefore });
93 this.pos += m[0].length;
94 continue;
95 }
96 throw new Error(`unsupported escape ${m?.[0] ?? '\\['} at ${this.pos}`);
97 }
98 if (/[0-9]/.test(c) || (c === '.' && /[0-9]/.test(s[this.pos + 1]))) {
99 // includes trailing-dot reals (`x_^2.` in some Rubi patterns)
100 const m = /^(?:[0-9]+\.?[0-9]*|\.[0-9]+)(`[0-9.]*)?/.exec(
101 s.slice(this.pos)
102 )!;
103 this.tokens.push({ kind: 'number', value: m[0], spaceBefore });
104 this.pos += m[0].length;
105 continue;
106 }
107 if (c === '"') {
108 const m = /^"((?:[^"\\]|\\.)*)"/.exec(s.slice(this.pos));
109 if (!m) throw new Error(`unterminated string at ${this.pos}`);
110 this.tokens.push({ kind: 'string', value: m[1], spaceBefore });
111 this.pos += m[0].length;
112 continue;
113 }
114 if (/[A-Za-z$_]/.test(c)) {
115 const m =
116 /^([A-Za-z$][A-Za-z0-9$]*)?(_(?:[A-Za-z][A-Za-z0-9$]*)?)?/.exec(
117 s.slice(this.pos)
118 )!;
119 const [, name, blank] = m;
120 this.pos += m[0].length;
121 if (blank !== undefined) {
122 // `a_`, `a_Symbol`, `_`, `_Head` — plus optional trailing `.`
123 let optional = false;
124 if (s[this.pos] === '.' && !/[0-9]/.test(s[this.pos + 1])) {
125 optional = true;
126 this.pos++;
127 }
128 this.tokens.push({
129 kind: 'pattern',

Callers 1

constructorMethod · 0.95

Calls 3

skipCommentMethod · 0.95
findMethod · 0.80
sliceMethod · 0.65

Tested by

no test coverage detected