All files / transpiler/output/codegen/helpers MemberSeparatorResolver.ts

100% Statements 25/25
100% Branches 29/29
100% Functions 4/4
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                    69x       69x     69x   69x                                       72x 3x       69x 17x       52x 3x       49x   4x         4x         45x 15x 15x       30x 1x       29x                       16x   16x       16x                         74x 62x             12x                  
/**
 * Member Separator Resolver
 *
 * Determines the appropriate separator for member access chains in C-Next.
 * Different separators are used based on context:
 * - `_` for scope member access (Motor.speed -> Motor_speed)
 * - `_` for register field access (GPIO7.DR_SET -> GPIO7_DR_SET)
 * - `.` for struct member access (point.x -> point.x)
 * - `->` for struct parameter member access in C mode
 * - `::` for C++ namespace/class access
 *
 * Issue #387, #409, ADR-016
 */
 
import ISeparatorContext from "../types/ISeparatorContext";
import IMemberSeparatorDeps from "../types/IMemberSeparatorDeps";
 
/**
 * Static utility for resolving member access separators
 */
class MemberSeparatorResolver {
  /**
   * Build the separator context for a member access chain
   */
  static buildContext(
    firstId: string,
    hasGlobal: boolean,
    hasThis: boolean,
    currentScope: string | null,
    isStructParam: boolean,
    deps: IMemberSeparatorDeps,
    isCppAccess: boolean,
  ): ISeparatorContext {
    const isCrossScope =
      hasGlobal &&
      (deps.isKnownScope(firstId) || deps.isKnownRegister(firstId));
 
    const scopedRegName =
      hasThis && currentScope ? `${currentScope}_${firstId}` : null;
 
    const isScopedRegister =
      scopedRegName !== null && deps.isKnownRegister(scopedRegName);
 
    return {
      hasGlobal,
      isCrossScope,
      isStructParam,
      isCppAccess,
      scopedRegName,
      isScopedRegister,
    };
  }
 
  /**
   * Get the separator for the first member access operation
   */
  static getFirstSeparator(
    identifierChain: string[],
    memberName: string,
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    // C++ namespace/class access
    if (ctx.isCppAccess) {
      return "::";
    }
 
    // Struct parameter uses -> in C mode, . in C++ mode
    if (ctx.isStructParam) {
      return deps.getStructParamSeparator();
    }
 
    // Cross-scope access (global.Scope.member or global.Register.member)
    if (ctx.isCrossScope) {
      return "_";
    }
 
    // Register member access: GPIO7.DR_SET -> GPIO7_DR_SET
    if (deps.isKnownRegister(identifierChain[0])) {
      // Validate register access from inside scope requires global. prefix
      deps.validateRegisterAccess(
        identifierChain[0],
        memberName,
        ctx.hasGlobal,
      );
      return "_";
    }
 
    // Scope member access: Sensor.buffer -> Sensor_buffer
    // Works with or without global. prefix (both are valid syntax)
    if (deps.isKnownScope(identifierChain[0])) {
      deps.validateCrossScopeVisibility(identifierChain[0], memberName);
      return "_";
    }
 
    // Scoped register: this.MOTOR_REG.SPEED -> Scope_MOTOR_REG_SPEED
    if (ctx.isScopedRegister) {
      return "_";
    }
 
    // Default: struct field access
    return ".";
  }
 
  /**
   * Get the separator for subsequent member access operations (after the first)
   */
  static getSubsequentSeparator(
    identifierChain: string[],
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    // Check for register chains
    const chainSoFar = identifierChain.slice(0, -1).join("_");
    const isRegisterChain =
      deps.isKnownRegister(identifierChain[0]) ||
      deps.isKnownRegister(chainSoFar) ||
      (ctx.scopedRegName !== null && deps.isKnownRegister(ctx.scopedRegName));
 
    return isRegisterChain ? "_" : ".";
  }
 
  /**
   * Get separator, dispatching to first or subsequent based on position
   */
  static getSeparator(
    isFirstOp: boolean,
    identifierChain: string[],
    memberName: string,
    ctx: ISeparatorContext,
    deps: IMemberSeparatorDeps,
  ): string {
    if (isFirstOp) {
      return MemberSeparatorResolver.getFirstSeparator(
        identifierChain,
        memberName,
        ctx,
        deps,
      );
    }
    return MemberSeparatorResolver.getSubsequentSeparator(
      identifierChain,
      ctx,
      deps,
    );
  }
}
 
export default MemberSeparatorResolver;