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 | 112x 112x 112x 112x 112x 291x 291x 28x 28x 28x 1x 27x 290x 290x 111x | /**
* EnumCollector - Extracts enum type declarations from parse trees.
* ADR-017: Enums provide named integer constants with auto-increment support.
*
* Produces TType-based IEnumSymbol with proper IScopeSymbol references.
*/
import * as Parser from "../../../parser/grammar/CNextParser";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import IEnumSymbol from "../../../../types/symbols/IEnumSymbol";
import IScopeSymbol from "../../../../types/symbols/IScopeSymbol";
import ExpressionEvaluator from "../utils/ExpressionEvaluator";
class EnumCollector {
/**
* Collect an enum declaration and return an IEnumSymbol.
*
* @param ctx The enum declaration context
* @param sourceFile Source file path
* @param scope The scope this enum belongs to (IScopeSymbol)
* @returns The enum symbol with proper scope reference
* @throws Error if any member has a negative value
*/
static collect(
ctx: Parser.EnumDeclarationContext,
sourceFile: string,
scope: IScopeSymbol,
): IEnumSymbol {
const name = ctx.IDENTIFIER().getText();
const line = ctx.start?.line ?? 0;
// Collect member values with auto-increment
const members = new Map<string, number>();
let currentValue = 0;
for (const member of ctx.enumMember()) {
const memberName = member.IDENTIFIER().getText();
if (member.expression()) {
// Explicit value with <-
const valueText = member.expression()!.getText();
const value = ExpressionEvaluator.evaluateConstant(valueText);
if (value < 0) {
throw new Error(
`Error: Negative values not allowed in enum (found ${value} in ${name}.${memberName})`,
);
}
currentValue = value;
}
members.set(memberName, currentValue);
currentValue++;
}
return {
kind: "enum",
name,
scope,
sourceFile,
sourceLine: line,
sourceLanguage: ESourceLanguage.CNext,
isExported: true,
members,
};
}
}
export default EnumCollector;
|