rip-lang 3.14.5 → 3.15.0

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/src/compiler.js CHANGED
@@ -11,8 +11,12 @@
11
11
  import { Lexer } from './lexer.js';
12
12
  import { parser } from './parser.js';
13
13
  import { installComponentSupport } from './components.js';
14
- import { emitTypes, emitEnum } from './types.js';
15
- import { installSchemaSupport } from './schema.js';
14
+ // Type emission is CLI/editor-only. types-emit.js registers itself via
15
+ // setTypesEmitter() at module load. The browser never imports types-emit,
16
+ // so _typesEmitter stays null and .d.ts output is silently skipped.
17
+ let _typesEmitter = null;
18
+ export function setTypesEmitter(fn) { _typesEmitter = fn; }
19
+ import { installSchemaSupport } from '@rip-lang/schema';
16
20
  import { SourceMapGenerator } from './sourcemaps.js';
17
21
  import { RipError, toRipError } from './error.js';
18
22
 
@@ -2045,7 +2049,22 @@ export class CodeEmitter {
2045
2049
  emitComprehension(head, rest, context) {
2046
2050
  let [expr, iterators, guards] = rest;
2047
2051
  if (context === 'statement') return this.emitComprehensionAsLoop(expr, iterators, guards);
2048
- if (this.comprehensionTarget) return this.emitComprehensionWithTarget(expr, iterators, guards, this.comprehensionTarget);
2052
+ if (this.comprehensionTarget) {
2053
+ // Consume-and-clear: the auto-return-loop logic sets comprehensionTarget
2054
+ // expecting ONE consumer (the direct value-context comprehension being
2055
+ // routed to the named target). Without clearing here, nested
2056
+ // comprehensions inside this comprehension's body (call args, RHS
2057
+ // expressions) would inherit the target and skip their own IIFE,
2058
+ // producing malformed JS or wrong semantics. The body's own emit calls
2059
+ // see comprehensionTarget = null and correctly produce IIFEs.
2060
+ let target = this.comprehensionTarget;
2061
+ this.comprehensionTarget = null;
2062
+ try {
2063
+ return this.emitComprehensionWithTarget(expr, iterators, guards, target);
2064
+ } finally {
2065
+ this.comprehensionTarget = target;
2066
+ }
2067
+ }
2049
2068
 
2050
2069
  // Enclosed: expr, iterators (iterable expressions), guards
2051
2070
  let hasAwait = this.containsAwait(expr) || iterators.some(i => this.containsAwait(i)) || guards.some(g => this.containsAwait(g));
@@ -2614,6 +2633,21 @@ export class CodeEmitter {
2614
2633
  code += this.indent() + this.addSemicolon(stmt, this.emit(stmt, 'statement')) + '\n';
2615
2634
  return;
2616
2635
  }
2636
+ // Auto-return-loop: only for-in/for-of/for-as auto-collect into
2637
+ // _result, because emitForIn/emitForOf/emitForAs at value-context
2638
+ // wrap themselves in a comprehension that consumes
2639
+ // comprehensionTarget. `loop` and `while` have no such wrapping —
2640
+ // emitLoop/emitWhile just emit `while(...) { body }` and any
2641
+ // comprehensionTarget set here would LEAK into nested
2642
+ // expression-context comprehensions inside the body (causing
2643
+ // them to be routed to the wrong target and skip their own
2644
+ // IIFE). Loops with explicit `return X` inside their body
2645
+ // already work correctly; emit them as plain statements.
2646
+ let isCollectibleLoop = h === 'for-in' || h === 'for-of' || h === 'for-as';
2647
+ if (!isCollectibleLoop) {
2648
+ code += this.indent() + this.addSemicolon(stmt, this.emit(stmt, 'statement')) + '\n';
2649
+ return;
2650
+ }
2617
2651
  code += this.indent() + 'const _result = [];\n';
2618
2652
  this.comprehensionTarget = '_result';
2619
2653
  let saved = this._skipCompTargetInit;
@@ -3689,7 +3723,7 @@ export class Compiler {
3689
3723
 
3690
3724
  // If only terminators remain (type-only source), emit types and return early
3691
3725
  if (tokens.every(t => t[0] === 'TERMINATOR')) {
3692
- if (typeTokens) dts = emitTypes(typeTokens, ['program'], source);
3726
+ if (typeTokens && _typesEmitter) dts = _typesEmitter(typeTokens, ['program'], source);
3693
3727
  return { tokens, sexpr: ['program'], code: '', dts, data: dataSection, reactiveVars: {} };
3694
3728
  }
3695
3729
 
@@ -3759,6 +3793,11 @@ export class Compiler {
3759
3793
  skipDataPart: this.options.skipDataPart,
3760
3794
  stubComponents: this.options.stubComponents,
3761
3795
  reactiveVars: this.options.reactiveVars,
3796
+ // Schema runtime mode: 'browser' / 'validate' / 'server' / 'migration'.
3797
+ // Default 'migration' covers the common case (CLI, server, tests) where
3798
+ // the user might call any schema feature including .toSQL(). The browser
3799
+ // bundle build script overrides to 'browser' for size reduction.
3800
+ schemaMode: this.options.schemaMode,
3762
3801
  sourceMap,
3763
3802
  });
3764
3803
  let code = generator.compile(sexpr);
@@ -3766,15 +3805,28 @@ export class Compiler {
3766
3805
  let map = sourceMap ? sourceMap.toJSON() : null;
3767
3806
  let reverseMap = sourceMap ? sourceMap.toReverseMap() : null;
3768
3807
  if (map && this.options.sourceMap === 'inline') {
3769
- let b64 = typeof Buffer !== 'undefined' ? Buffer.from(map).toString('base64') : btoa(map);
3808
+ // map is already a JSON string (sourceMaps.toJSON() stringifies). UTF-8
3809
+ // safe encode: btoa() only handles Latin-1, so pre-encode non-ASCII via
3810
+ // TextEncoder before base64 in browsers. Bun's Buffer handles utf-8
3811
+ // directly. Source files containing emoji, em-dashes, accented chars,
3812
+ // etc. would otherwise break with `Failed to execute 'btoa'`.
3813
+ let b64;
3814
+ if (typeof Buffer !== 'undefined') {
3815
+ b64 = Buffer.from(map, 'utf8').toString('base64');
3816
+ } else {
3817
+ const bytes = new TextEncoder().encode(map);
3818
+ let bin = '';
3819
+ for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
3820
+ b64 = btoa(bin);
3821
+ }
3770
3822
  code += `\n//# sourceMappingURL=data:application/json;base64,${b64}`;
3771
3823
  } else if (map && this.options.filename) {
3772
3824
  code += `\n//# sourceMappingURL=${this.options.filename}.js.map`;
3773
3825
  }
3774
3826
 
3775
3827
  // Step 5: Emit .d.ts from annotated tokens + parsed s-expression
3776
- if (typeTokens) {
3777
- dts = emitTypes(typeTokens, sexpr, source);
3828
+ if (typeTokens && _typesEmitter) {
3829
+ dts = _typesEmitter(typeTokens, sexpr, source);
3778
3830
  }
3779
3831
 
3780
3832
  return { tokens, sexpr, code, dts, map, reverseMap, data: dataSection, reactiveVars: generator.reactiveVars };
@@ -3791,10 +3843,36 @@ export class Compiler {
3791
3843
  installComponentSupport(CodeEmitter, Lexer);
3792
3844
 
3793
3845
  // =============================================================================
3794
- // Type Support (enum generator)
3846
+ // Enum Codegen (CodeEmitter method)
3795
3847
  // =============================================================================
3848
+ // `enum` blocks compile to a runtime JavaScript object that maps both
3849
+ // forward (key → value) and reverse (value → key). This is real codegen,
3850
+ // not type machinery, so it lives with the rest of the emitter dispatch.
3851
+
3852
+ CodeEmitter.prototype.emitEnum = function emitEnum(head, rest, context) {
3853
+ let [name, body] = rest;
3854
+ let enumName = name?.valueOf?.() ?? name;
3855
+
3856
+ let pairs = [];
3857
+ if (Array.isArray(body)) {
3858
+ let items = body[0] === 'block' ? body.slice(1) : [body];
3859
+ for (let item of items) {
3860
+ if (Array.isArray(item)) {
3861
+ if (item[0]?.valueOf?.() === '=') {
3862
+ let key = item[1]?.valueOf?.() ?? item[1];
3863
+ let val = item[2]?.valueOf?.() ?? item[2];
3864
+ pairs.push([key, val]);
3865
+ }
3866
+ }
3867
+ }
3868
+ }
3869
+
3870
+ if (pairs.length === 0) return `const ${enumName} = {}`;
3796
3871
 
3797
- CodeEmitter.prototype.emitEnum = emitEnum;
3872
+ let forward = pairs.map(([k, v]) => `${k}: ${v}`).join(', ');
3873
+ let reverse = pairs.map(([k, v]) => `${v}: "${k}"`).join(', ');
3874
+ return `const ${enumName} = {${forward}, ${reverse}}`;
3875
+ };
3798
3876
 
3799
3877
  // =============================================================================
3800
3878
  // Schema Support (prototype installation)
@@ -808,7 +808,7 @@ grammar =
808
808
  # Schema
809
809
  # ============================================================================
810
810
  # `schema [:kind] INDENT ... OUTDENT` is parsed entirely by the schema
811
- # sub-parser at lexer-rewrite time (src/schema.js). The rewriter emits
811
+ # sub-parser at lexer-rewrite time (packages/schema/src/schema.js). The rewriter emits
812
812
  # a synthetic SCHEMA_BODY token whose .data carries the full descriptor
813
813
  # (kind, entries, per-entry loc). The main grammar only sees these two
814
814
  # terminals, which keeps schema body syntax (`name! type`, `@directive`,
@@ -154,7 +154,6 @@ class Generator
154
154
  for handle in handles
155
155
  [symbols, action, precedence] = @_parseHandle handle
156
156
 
157
- # Add symbols to grammar
158
157
  addSymbol symbol for symbol in symbols
159
158
 
160
159
  # Process semantic actions
package/src/lexer.js CHANGED
@@ -40,7 +40,7 @@
40
40
  // ==========================================================================
41
41
 
42
42
  import { installTypeSupport } from './types.js';
43
- import { installSchemaSupport } from './schema.js';
43
+ import { installSchemaSupport } from '@rip-lang/schema';
44
44
 
45
45
  // ==========================================================================
46
46
  // Token Category Sets
@@ -174,6 +174,15 @@ let TAGGABLE = new Set(['IDENTIFIER', 'PROPERTY', ')', 'CALL_END', ']', 'INDEX_E
174
174
  // Control flow tokens that don't end implicit calls/objects
175
175
  let CONTROL_IN_IMPLICIT = new Set(['IF', 'TRY', 'FINALLY', 'CATCH', 'CLASS', 'SWITCH', 'COMPONENT', 'FOR']);
176
176
 
177
+ // Tokens that complete an expression value. Used to detect postfix-position
178
+ // for keywords that exist in both prefix and postfix forms (FOR has no
179
+ // dedicated POST_FOR token like POST_IF, so we infer it from context).
180
+ let VALUE_END_TAGS = new Set([
181
+ 'IDENTIFIER', 'PROPERTY', 'NUMBER', 'STRING', 'STRING_END', 'REGEX', 'REGEX_END',
182
+ ')', 'CALL_END', ']', 'INDEX_END', '}', 'MAP_END', 'PICK_END',
183
+ 'BOOL', 'NULL', 'UNDEFINED', 'INFINITY', 'NAN', 'SUPER', 'THIS', '@', 'SYMBOL',
184
+ ]);
185
+
177
186
  // Single-liner keywords that get implicit INDENT/OUTDENT
178
187
  let SINGLE_LINERS = new Set(['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN']);
179
188
 
@@ -1782,8 +1791,21 @@ export class Lexer {
1782
1791
  i += 1;
1783
1792
  };
1784
1793
 
1785
- // Don't end implicit on INDENT for control flow inside implicit
1786
- if ((inImplicitCall() || inImplicitObject()) && CONTROL_IN_IMPLICIT.has(tag)) {
1794
+ // Don't end implicit on INDENT for control flow inside implicit.
1795
+ //
1796
+ // Special case: FOR is the only entry in CONTROL_IN_IMPLICIT that can
1797
+ // appear in postfix position (no POST_FOR token exists, unlike
1798
+ // POST_IF / POST_UNLESS). When FOR follows a value-completing token
1799
+ // on the same line (e.g. `addSymbol s for s in xs`), it's a postfix
1800
+ // comprehension that should END the implicit call so the comprehension
1801
+ // wraps the call rather than becoming the call's argument. Detect
1802
+ // postfix FOR by: not at the start of a new line, AND the previous
1803
+ // token is a value-completing token (IDENTIFIER, ), ], literal, etc.).
1804
+ // Prefix FOR (after `:`, `=`, `->`, comma, etc.) keeps the existing
1805
+ // CONTROL behaviour. Falling through lets the IMPLICIT_END handler
1806
+ // below close the implicit call.
1807
+ let isPostfixFor = tag === 'FOR' && !token.newLine && VALUE_END_TAGS.has(prevTag);
1808
+ if ((inImplicitCall() || inImplicitObject()) && CONTROL_IN_IMPLICIT.has(tag) && !isPostfixFor) {
1787
1809
  stack.push(['CONTROL', i, {ours: true}]);
1788
1810
  return forward(1);
1789
1811
  }
package/src/parser.js CHANGED
@@ -256,16 +256,16 @@ const parserInstance = {
256
256
  }
257
257
  },
258
258
  parse(input) {
259
- let EOF, TERROR, action, errStr, expected, len, lex, lexer, loc, locs, newState, p, parseTable, preErrorSymbol, r, recovering, rv, sharedState, state, stk, symbol, tokenLen, tokenLine, tokenLoc, tokenText, vals;
259
+ let EOF, TERROR, action, errStr, expected, k, len, lex, lexer, loc, locs, newState, p, parseTable, preErrorSymbol, r, recovering, rv, sharedState, state, stk, symbol, tokenLen, tokenLine, tokenLoc, tokenText, v, vals;
260
260
  [stk, vals, locs] = [[0], [null], []];
261
261
  [parseTable, tokenText, tokenLine, tokenLen, recovering] = [this.parseTable, "", 0, 0, 0];
262
262
  [TERROR, EOF] = [2, 1];
263
263
  lexer = Object.create(this.lexer);
264
264
  sharedState = { ctx: {} };
265
- for (const k in this.ctx) {
265
+ for (let k in this.ctx) {
266
266
  if (!Object.hasOwn(this.ctx, k))
267
267
  continue;
268
- const v = this.ctx[k];
268
+ let v = this.ctx[k];
269
269
  sharedState.ctx[k] = v;
270
270
  }
271
271
  lexer.setInput(input, sharedState.ctx);
@@ -294,7 +294,7 @@ const parserInstance = {
294
294
  if (!recovering)
295
295
  expected = (() => {
296
296
  const result = [];
297
- for (const p in parseTable[state]) {
297
+ for (let p in parseTable[state]) {
298
298
  if (!Object.hasOwn(parseTable[state], p))
299
299
  continue;
300
300
  if (this.tokenNames[p] && p > TERROR)
package/src/typecheck.js CHANGED
@@ -11,8 +11,9 @@
11
11
  // type errors mapped back to Rip source positions.
12
12
 
13
13
  import { Compiler, getStdlibCode } from './compiler.js';
14
- import { INTRINSIC_TYPE_DECLS, INTRINSIC_FN_DECL, ARIA_TYPE_DECLS, SIGNAL_INTERFACE, SIGNAL_FN, COMPUTED_INTERFACE, COMPUTED_FN, EFFECT_FN } from './types.js';
15
- import { hasSchemas } from './schema.js';
14
+ import { INTRINSIC_TYPE_DECLS, INTRINSIC_FN_DECL, ARIA_TYPE_DECLS, SIGNAL_INTERFACE, SIGNAL_FN, COMPUTED_INTERFACE, COMPUTED_FN, EFFECT_FN } from './types-emit.js';
15
+ import { hasSchemas } from '@rip-lang/schema';
16
+ import '@rip-lang/schema/loader-server'; // registers full schema runtime provider
16
17
  import { createRequire } from 'module';
17
18
  import { readFileSync, existsSync, readdirSync } from 'fs';
18
19
  import { resolve, relative, dirname } from 'path';