@creationix/rex 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rex-repl.ts CHANGED
@@ -24,7 +24,7 @@ const C = {
24
24
  // ── Syntax highlighting ───────────────────────────────────────
25
25
 
26
26
  const TOKEN_RE =
27
- /(?<blockComment>\/\*[\s\S]*?(?:\*\/|$))|(?<lineComment>\/\/[^\n]*)|(?<dstring>"(?:[^"\\]|\\.)*"?)|(?<sstring>'(?:[^'\\]|\\.)*'?)|(?<keyword>\b(?:when|unless|while|for|do|end|in|of|and|or|else|break|continue|delete|self)(?![a-zA-Z0-9_-]))|(?<literal>\b(?:true|false|null|undefined)(?![a-zA-Z0-9_-]))|(?<typePred>\b(?:string|number|object|array|boolean)(?![a-zA-Z0-9_-]))|(?<num>\b(?:0x[0-9a-fA-F]+|0b[01]+|(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)\b)/g;
27
+ /(?<blockComment>\/\*[\s\S]*?(?:\*\/|$))|(?<lineComment>\/\/[^\n]*)|(?<dstring>"(?:[^"\\]|\\.)*"?)|(?<sstring>'(?:[^'\\]|\\.)*'?)|(?<keyword>\b(?:when|unless|while|for|do|end|in|of|and|or|nor|else|break|continue|delete|self)(?![a-zA-Z0-9_-]))|(?<literal>\b(?:true|false|null|undefined|nan)(?![a-zA-Z0-9_-])|-?\binf\b)|(?<typePred>\b(?:string|number|object|array|boolean)(?![a-zA-Z0-9_-]))|(?<num>\b(?:0x[0-9a-fA-F]+|0b[01]+|(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)\b)/g;
28
28
 
29
29
  export function highlightLine(line: string): string {
30
30
  let result = "";
@@ -143,6 +143,7 @@ export function highlightRexc(text: string): string {
143
143
  break;
144
144
  }
145
145
  case "=": // assignment
146
+ case "/": // swap-assign
146
147
  case "~": // delete
147
148
  out += C.red + prefix + tag + C.reset;
148
149
  i++;
@@ -180,6 +181,39 @@ export function highlightRexc(text: string): string {
180
181
  return out;
181
182
  }
182
183
 
184
+ // ── JSON IR highlighting ─────────────────────────────────────
185
+
186
+ const JSON_TOKEN_RE =
187
+ /(?<key>"(?:[^"\\]|\\.)*")\s*:|(?<string>"(?:[^"\\]|\\.)*")|(?<number>-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)\b|(?<bool>true|false)|(?<null>null)|(?<brace>[{}[\]])|(?<punct>[:,])/g;
188
+
189
+ export function highlightJSON(json: string): string {
190
+ let result = "";
191
+ let lastIndex = 0;
192
+ JSON_TOKEN_RE.lastIndex = 0;
193
+
194
+ for (const m of json.matchAll(JSON_TOKEN_RE)) {
195
+ result += json.slice(lastIndex, m.index);
196
+ const text = m[0];
197
+ const g = m.groups!;
198
+ if (g.key) {
199
+ result += C.cyan + g.key + C.reset + ":";
200
+ } else if (g.string) {
201
+ result += C.green + text + C.reset;
202
+ } else if (g.number) {
203
+ result += C.yellow + text + C.reset;
204
+ } else if (g.bool) {
205
+ result += C.yellow + text + C.reset;
206
+ } else if (g.null) {
207
+ result += C.dim + text + C.reset;
208
+ } else {
209
+ result += text;
210
+ }
211
+ lastIndex = m.index! + text.length;
212
+ }
213
+ result += json.slice(lastIndex);
214
+ return result;
215
+ }
216
+
183
217
  // ── Multi-line detection ──────────────────────────────────────
184
218
 
185
219
  /** Strip string literals and comments, replacing them with spaces. */
@@ -224,7 +258,7 @@ export function isIncomplete(buffer: string): boolean {
224
258
  // Trailing binary operator or keyword suggests continuation
225
259
  const trimmed = buffer.trimEnd();
226
260
  if (/[+\-*/%&|^=<>]$/.test(trimmed)) return true;
227
- if (/\b(?:and|or|do|in|of)\s*$/.test(trimmed)) return true;
261
+ if (/\b(?:and|or|nor|do|in|of)\s*$/.test(trimmed)) return true;
228
262
 
229
263
  return false;
230
264
  }
@@ -276,8 +310,8 @@ export function formatVarState(vars: Record<string, unknown>): string {
276
310
 
277
311
  const KEYWORDS = [
278
312
  "when", "unless", "while", "for", "do", "end", "in", "of",
279
- "and", "or", "else", "break", "continue", "delete",
280
- "self", "true", "false", "null", "undefined",
313
+ "and", "or", "nor", "else", "break", "continue", "delete",
314
+ "self", "true", "false", "null", "undefined", "nan", "inf",
281
315
  "string", "number", "object", "array", "boolean",
282
316
  ];
283
317
 
@@ -504,7 +538,7 @@ export async function startRepl(): Promise<void> {
504
538
  const lowered = state.optimize ? optimizeIR(ir) : ir;
505
539
 
506
540
  if (state.showIR) {
507
- console.log(`${C.dim} IR: ${JSON.stringify(lowered)}${C.reset}`);
541
+ console.log(`${C.dim} IR:${C.reset} ${highlightJSON(JSON.stringify(lowered))}`);
508
542
  }
509
543
 
510
544
  const rexc = compile(source, { optimize: state.optimize });