The best way to control Claude Code, Codex and more from any computer or phone

main
patch 431 lines 12.1 KB
Raw
1 diff --git a/dev/lib/math-flow.js b/dev/lib/math-flow.js
2 --- a/dev/lib/math-flow.js
3 +++ b/dev/lib/math-flow.js
4 @@ -4,7 +4,7 @@
5
6 import {ok as assert} from 'devlop'
7 import {factorySpace} from 'micromark-factory-space'
8 -import {markdownLineEnding} from 'micromark-util-character'
9 +import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
10 import {codes, constants, types} from 'micromark-util-symbol'
11
12 /** @type {Construct} */
13 @@ -32,6 +32,9 @@
14 ? tail[2].sliceSerialize(tail[1], true).length
15 : 0
16 let sizeOpen = 0
17 + let closed = false
18 + let slashes = 0
19 + let trailingAfterClose = false
20
21 return start
22
23 @@ -79,7 +82,32 @@
24 }
25
26 effects.exit('mathFlowFenceSequence')
27 - return factorySpace(effects, metaBefore, types.whitespace)(code)
28 +
29 + return factorySpace(effects, afterOpeningWhitespace, types.whitespace)(code)
30 + }
31 +
32 + /**
33 + * After opening fence whitespace, check for same-line content.
34 + *
35 + * ```markdown
36 + * > | $$ 1 + 1 $$
37 + * ^
38 + * ```
39 + *
40 + * @type {State}
41 + */
42 + function afterOpeningWhitespace(code) {
43 + if (code === codes.eof || markdownLineEnding(code)) {
44 + return metaAfter(code)
45 + }
46 +
47 + effects.exit('mathFlowFence')
48 +
49 + if (self.interrupt) {
50 + return ok(code)
51 + }
52 +
53 + return beforeContentChunk(code)
54 }
55
56 /**
57 @@ -230,6 +258,14 @@
58 )(code)
59 }
60
61 + if (code === codes.dollarSign) {
62 + return effects.attempt(
63 + {tokenize: tokenizeInlineClosingFence, partial: true},
64 + after,
65 + resumeContentAfterFailedClose
66 + )(code)
67 + }
68 +
69 effects.enter('mathFlowValue')
70 return contentChunk(code)
71 }
72 @@ -247,13 +283,74 @@
73 * @type {State}
74 */
75 function contentChunk(code) {
76 + const escaped = slashes % 2 === 1
77 + slashes = code === codes.backslash ? slashes + 1 : 0
78 if (code === codes.eof || markdownLineEnding(code)) {
79 effects.exit('mathFlowValue')
80 return beforeContentChunk(code)
81 }
82
83 + if (code === codes.dollarSign && !escaped) {
84 + effects.exit('mathFlowValue')
85 + return effects.attempt(
86 + {tokenize: tokenizeInlineClosingFence, partial: true},
87 + after,
88 + resumeContentAfterFailedClose
89 + )(code)
90 + }
91 +
92 effects.consume(code)
93 return contentChunk
94 + }
95 +
96 + /**
97 + * Resume content after a failed inline closing fence attempt.
98 + *
99 + * @type {State}
100 + */
101 + function resumeContentAfterFailedClose(code) {
102 + if (trailingAfterClose) return nok(code)
103 + effects.enter('mathFlowValue')
104 + effects.consume(code)
105 + return contentChunk
106 + }
107 +
108 + /** @type {Tokenizer} */
109 + function tokenizeInlineClosingFence(effects, ok, nok) {
110 + let size = 0
111 + return sequenceStart
112 +
113 + function sequenceStart(code) {
114 + effects.enter('mathFlowFence')
115 + effects.enter('mathFlowFenceSequence')
116 + return sequenceClose(code)
117 + }
118 +
119 + function sequenceClose(code) {
120 + if (code === codes.dollarSign) {
121 + size++
122 + effects.consume(code)
123 + return sequenceClose
124 + }
125 +
126 + if (size < sizeOpen) {
127 + return nok(code)
128 + }
129 +
130 + effects.exit('mathFlowFenceSequence')
131 + return factorySpace(effects, afterSequenceClose, types.whitespace)(code)
132 + }
133 +
134 + function afterSequenceClose(code) {
135 + if (code === codes.eof || markdownLineEnding(code)) {
136 + effects.exit('mathFlowFence')
137 + closed = true
138 + return ok(code)
139 + }
140 +
141 + trailingAfterClose = true
142 + return nok(code)
143 + }
144 }
145
146 /**
147 @@ -269,6 +366,7 @@
148 * @type {State}
149 */
150 function after(code) {
151 + if (!closed) return nok(code)
152 effects.exit('mathFlow')
153 return ok(code)
154 }
155 @@ -357,6 +455,7 @@
156 function afterSequenceClose(code) {
157 if (code === codes.eof || markdownLineEnding(code)) {
158 effects.exit('mathFlowFence')
159 + closed = true
160 return ok(code)
161 }
162
163 diff --git a/dev/lib/math-tex-flow.js b/dev/lib/math-tex-flow.js
164 --- a/dev/lib/math-tex-flow.js
165 +++ b/dev/lib/math-tex-flow.js
166 @@ -7,6 +7,10 @@
167 import {markdownLineEnding} from 'micromark-util-character'
168 import {codes, constants, types} from 'micromark-util-symbol'
169
170 +// Only EOF proves that every later opener in this flow lacks a closing fence.
171 +/** @type {WeakMap<TokenizeContext, number>} */
172 +const failedUntil = new WeakMap()
173 +
174 /** @type {Construct} */
175 export const mathTexFlow = {
176 tokenize: tokenizeMathFenced,
177 @@ -26,6 +30,7 @@
178 */
179 function tokenizeMathFenced(effects, ok, nok) {
180 const self = this
181 + let closed = false
182 const tail = self.events[self.events.length - 1]
183 const initialSize =
184 tail && tail[1].type === types.linePrefix
185 @@ -47,6 +52,7 @@
186 * @type {State}
187 */
188 function start(code) {
189 + if ((failedUntil.get(self) || 0) > self.now().offset) return nok(code)
190 assert(code === codes.backslash, 'expected `\\`')
191 effects.enter('mathFlow')
192 effects.enter('mathFlowFence')
193 @@ -90,6 +96,7 @@
194 * @type {State}
195 */
196 function metaAfter(code) {
197 + if (code !== codes.eof && !markdownLineEnding(code)) return nok(code)
198 effects.exit('mathFlowFence')
199
200 if (self.interrupt) {
201 @@ -245,6 +252,10 @@
202 * @type {State}
203 */
204 function after(code) {
205 + if (!closed) {
206 + if (code === codes.eof) failedUntil.set(self, self.now().offset)
207 + return nok(code)
208 + }
209 effects.exit('mathFlow')
210 return ok(code)
211 }
212 @@ -327,6 +338,7 @@
213 function afterSequenceClose(code) {
214 if (code === codes.eof || markdownLineEnding(code)) {
215 effects.exit('mathFlowFence')
216 + closed = true
217 return ok(code)
218 }
219
220 diff --git a/dev/lib/math-tex-text.js b/dev/lib/math-tex-text.js
221 --- a/dev/lib/math-tex-text.js
222 +++ b/dev/lib/math-tex-text.js
223 @@ -1,5 +1,5 @@
224 /**
225 - * @import {Construct, Previous, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
226 + * @import {Construct, Event, Previous, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
227 */
228
229 import {ok as assert} from 'devlop'
230 @@ -7,6 +7,12 @@
231 import {codes, types} from 'micromark-util-symbol'
232 import {mathText} from './math-text.js'
233
234 +// Failed scans are local to a text tokenizer and delimiter, never shared across documents.
235 +/** @type {WeakMap<Array<Event>, Record<number, number>>} */
236 +const failedUntil = new WeakMap()
237 +/** @type {WeakMap<Array<Event>, Record<number, number>>} */
238 +const singleLineFailedUntil = new WeakMap()
239 +
240 /** @type {Construct} */
241 export const mathTexText = {
242 tokenize: tokenizeMathText,
243 @@ -21,10 +27,12 @@
244 */
245 function tokenizeMathText(effects, ok, nok) {
246 const self = this
247 + const failures = self.mathSingleLine ? singleLineFailedUntil : failedUntil
248 /** @type {Token} */
249 let token
250 /** @type {number} */
251 let openSequenceCode
252 + let substantive = false
253
254 return start
255
256 @@ -66,6 +74,8 @@
257 return nok(code)
258 }
259
260 + if ((failures.get(self.events)?.[code] || 0) > self.now().offset) return nok(code)
261 +
262 openSequenceCode = code
263 effects.consume(code)
264 effects.exit('mathTextSequence')
265 @@ -83,8 +93,8 @@
266 * @type {State}
267 */
268 function between(code) {
269 - if (code === codes.eof) {
270 - return nok(code)
271 + if (code === codes.eof || code === codes.graveAccent || (self.mathSingleLine && markdownLineEnding(code))) {
272 + return unmatched(code)
273 }
274
275 if (code === codes.backslash) {
276 @@ -124,6 +134,8 @@
277 * @type {State}
278 */
279 function data(code) {
280 + if (code === codes.graveAccent) return unmatched(code)
281 + if (code !== codes.backslash && code !== codes.eof && code !== codes.space && code !== codes.comma && !markdownLineEnding(code) && !(code >= codes.digit0 && code <= codes.digit9)) substantive = true
282 if (
283 code === codes.eof ||
284 code === codes.space ||
285 @@ -138,6 +150,13 @@
286 return data
287 }
288
289 + function unmatched(code) {
290 + const ends = failures.get(self.events) || {}
291 + ends[openSequenceCode] = self.now().offset
292 + failures.set(self.events, ends)
293 + return nok(code)
294 + }
295 +
296 /**
297 * In closing sequence.
298 *
299 @@ -155,6 +174,7 @@
300 ? codes.rightParenthesis
301 : codes.rightSquareBracket
302 if (code === closeSequenceCode) {
303 + if (openSequenceCode === codes.leftSquareBracket && !substantive) return nok(code)
304 effects.consume(code)
305 effects.exit('mathTextSequence')
306 effects.exit('mathText')
307 @@ -180,5 +200,8 @@
308 */
309 function previous(code) {
310 // If there is a previous code, there will always be a tail.
311 - return code !== codes.backslash
312 + return (
313 + code !== codes.backslash ||
314 + this.events[this.events.length - 1][1].type === types.characterEscape
315 + )
316 }
317 diff --git a/dev/lib/math-text.js b/dev/lib/math-text.js
318 --- a/dev/lib/math-text.js
319 +++ b/dev/lib/math-text.js
320 @@ -10 +10 @@
321 -import {markdownLineEnding} from 'micromark-util-character'
322 +import {markdownLineEnding, markdownLineEndingOrSpace} from 'micromark-util-character'
323 @@ -42,6 +42,13 @@
324 let size
325 /** @type {Token} */
326 let token
327 + /** @type {boolean} */
328 + let openWhitespace
329 + /** @type {number | undefined} */
330 + let charBeforeClose
331 + let slashes = 0
332 + let previousData
333 + let beforeData
334
335 return start
336
337 @@ -89,6 +96,16 @@
338 }
339
340 effects.exit('mathTextSequence')
341 + // Reject if opening is followed by line ending
342 + if (markdownLineEnding(code)) {
343 + return nok(code)
344 + }
345 + // Track if opening sequence is followed by whitespace
346 + openWhitespace =
347 + code === codes.space ||
348 + code === codes.horizontalTab ||
349 + code === codes.comma
350 + if (sizeOpen === 1 && openWhitespace) return nok(code)
351 return between(code)
352 }
353
354 @@ -103,13 +120,14 @@
355 * @type {State}
356 */
357 function between(code) {
358 - if (code === codes.eof) {
359 + if (code === codes.eof || code === codes.graveAccent || ((sizeOpen === 1 || self.mathSingleLine) && markdownLineEnding(code))) {
360 return nok(code)
361 }
362
363 if (code === codes.dollarSign) {
364 token = effects.enter('mathTextSequence')
365 size = 0
366 + charBeforeClose = self.previous
367 return sequenceClose(code)
368 }
369
370 @@ -144,6 +162,16 @@
371 * @type {State}
372 */
373 function data(code) {
374 + if (code === codes.graveAccent) return nok(code)
375 + if (code === codes.dollarSign && slashes % 2 === 1) {
376 + slashes = 0
377 + effects.consume(code)
378 + return data
379 + }
380 + if (code === codes.slash && previousData === codes.slash && beforeData === codes.colon) return nok(code)
381 + beforeData = previousData
382 + previousData = code
383 + slashes = code === codes.backslash ? slashes + 1 : 0
384 if (
385 code === codes.eof ||
386 code === codes.space ||
387 @@ -179,9 +207,29 @@
388
389 // Done!
390 if (size === sizeOpen) {
391 - effects.exit('mathTextSequence')
392 - effects.exit('mathText')
393 - return ok(code)
394 + // Reject if closing is preceded by line ending
395 + if (markdownLineEnding(charBeforeClose)) {
396 + return nok(code)
397 + }
398 + // Reject if closing is followed by a digit
399 + if (code !== null && code >= codes.digit0 && code <= codes.digit9) {
400 + return nok(code)
401 + }
402 + if (charBeforeClose === codes.comma && !openWhitespace && code !== codes.eof && !markdownLineEndingOrSpace(code) && !/[.,;:)\]}]/.test(String.fromCharCode(code))) {
403 + return nok(code)
404 + }
405 + // Check if closing sequence has matching whitespace pattern
406 + const closeWhitespace =
407 + charBeforeClose === codes.space ||
408 + charBeforeClose === codes.horizontalTab ||
409 + (charBeforeClose === codes.comma && openWhitespace)
410 + if (openWhitespace === closeWhitespace) {
411 + effects.exit('mathTextSequence')
412 + effects.exit('mathText')
413 + return ok(code)
414 + }
415 + // Whitespace mismatch: mark as data
416 + return nok(code)
417 }
418
419 // More or less accents: mark as data.
420 diff --git a/package.json b/package.json
421 --- a/package.json
422 +++ b/package.json
423 @@ -27,7 +27,7 @@
424 "type": "module",
425 "exports": {
426 "development": "./dev/index.js",
427 - "default": "./index.js"
428 + "default": "./dev/index.js"
429 },
430 "files": [
431 "dev/",