All files / transpiler/logic/symbols/cnext/collectors ScopeCollector.ts

100% Statements 53/53
93.75% Branches 15/16
100% Functions 1/1
100% Lines 53/53

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 151 152 153 154 155 156 157 158 159 160                                                                    173x 173x   173x 173x 173x   173x   270x   270x 270x     270x 77x 77x 77x 77x   77x             77x       270x 140x 140x 140x 140x   140x           140x       270x 30x 30x 30x 30x   30x         30x       270x 2x 2x 2x 2x   2x         2x       270x 13x 13x 13x 13x   13x         13x       270x 5x 5x 5x 5x   5x           5x       173x                     173x          
/**
 * ScopeCollector - Extracts scope declarations and their nested members.
 * ADR-016: Scopes group related functions and control member visibility.
 */
 
import * as Parser from "../../../parser/grammar/CNextParser";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import ESymbolKind from "../../../../../utils/types/ESymbolKind";
import IScopeSymbol from "../../types/IScopeSymbol";
import TSymbol from "../../types/TSymbol";
import IScopeCollectorResult from "../types/IScopeCollectorResult";
import BitmapCollector from "./BitmapCollector";
import EnumCollector from "./EnumCollector";
import StructCollector from "./StructCollector";
import FunctionCollector from "./FunctionCollector";
import VariableCollector from "./VariableCollector";
import RegisterCollector from "./RegisterCollector";
 
class ScopeCollector {
  /**
   * Collect a scope declaration and all its nested members.
   *
   * @param ctx The scope declaration context
   * @param sourceFile Source file path
   * @param knownBitmaps Set of known bitmap type names for register resolution
   * @param constValues Map of constant names to their numeric values (for resolving array dimensions)
   * @returns The scope symbol and all member symbols
   */
  static collect(
    ctx: Parser.ScopeDeclarationContext,
    sourceFile: string,
    knownBitmaps: Set<string>,
    constValues?: Map<string, number>,
  ): IScopeCollectorResult {
    const scopeName = ctx.IDENTIFIER().getText();
    const line = ctx.start?.line ?? 0;
 
    const memberNames: string[] = [];
    const memberVisibility = new Map<string, "public" | "private">();
    const memberSymbols: TSymbol[] = [];
 
    for (const member of ctx.scopeMember()) {
      // ADR-016: Extract visibility (private by default)
      const visibilityMod = member.visibilityModifier();
      const visibility: "public" | "private" =
        (visibilityMod?.getText() as "public" | "private") ?? "private";
      const isPublic = visibility === "public";
 
      // Handle variable declarations
      if (member.variableDeclaration()) {
        const varDecl = member.variableDeclaration()!;
        const varName = varDecl.IDENTIFIER().getText();
        memberNames.push(varName);
        memberVisibility.set(varName, visibility);
 
        const varSymbol = VariableCollector.collect(
          varDecl,
          sourceFile,
          scopeName,
          isPublic,
          constValues,
        );
        memberSymbols.push(varSymbol);
      }
 
      // Handle function declarations
      if (member.functionDeclaration()) {
        const funcDecl = member.functionDeclaration()!;
        const funcName = funcDecl.IDENTIFIER().getText();
        memberNames.push(funcName);
        memberVisibility.set(funcName, visibility);
 
        const funcSymbol = FunctionCollector.collect(
          funcDecl,
          sourceFile,
          scopeName,
          visibility,
        );
        memberSymbols.push(funcSymbol);
      }
 
      // Handle enum declarations
      if (member.enumDeclaration()) {
        const enumDecl = member.enumDeclaration()!;
        const enumName = enumDecl.IDENTIFIER().getText();
        memberNames.push(enumName);
        memberVisibility.set(enumName, visibility);
 
        const enumSymbol = EnumCollector.collect(
          enumDecl,
          sourceFile,
          scopeName,
        );
        memberSymbols.push(enumSymbol);
      }
 
      // Handle bitmap declarations
      if (member.bitmapDeclaration()) {
        const bitmapDecl = member.bitmapDeclaration()!;
        const bitmapName = bitmapDecl.IDENTIFIER().getText();
        memberNames.push(bitmapName);
        memberVisibility.set(bitmapName, visibility);
 
        const bitmapSymbol = BitmapCollector.collect(
          bitmapDecl,
          sourceFile,
          scopeName,
        );
        memberSymbols.push(bitmapSymbol);
      }
 
      // Handle struct declarations
      if (member.structDeclaration()) {
        const structDecl = member.structDeclaration()!;
        const structName = structDecl.IDENTIFIER().getText();
        memberNames.push(structName);
        memberVisibility.set(structName, visibility);
 
        const structSymbol = StructCollector.collect(
          structDecl,
          sourceFile,
          scopeName,
        );
        memberSymbols.push(structSymbol);
      }
 
      // Handle register declarations
      if (member.registerDeclaration()) {
        const regDecl = member.registerDeclaration()!;
        const regName = regDecl.IDENTIFIER().getText();
        memberNames.push(regName);
        memberVisibility.set(regName, visibility);
 
        const regSymbol = RegisterCollector.collect(
          regDecl,
          sourceFile,
          knownBitmaps,
          scopeName,
        );
        memberSymbols.push(regSymbol);
      }
    }
 
    const scopeSymbol: IScopeSymbol = {
      name: scopeName,
      sourceFile,
      sourceLine: line,
      sourceLanguage: ESourceLanguage.CNext,
      isExported: true,
      kind: ESymbolKind.Namespace,
      members: memberNames,
      memberVisibility,
    };
 
    return { scopeSymbol, memberSymbols };
  }
}
 
export default ScopeCollector;