Skip to content

Commit

Permalink
#76, structure iterator support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Mar 2, 2024
1 parent 11c3457 commit 89033f9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
12 changes: 12 additions & 0 deletions stage_3_generation/build/decorators/createDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ async function buildDecorator(
typeToClass.addTypeMember(true, module.createCopyFieldsMethod());
typeToClass.addTypeMember(false, module.createToJSONMethod());

{
const properties = interfaceMembers.arrayOfKind(StructureKind.PropertySignature);
if (properties.some(prop => /^#.*Manager$/.test(prop.name))) {
typeToClass.addTypeMember(false, module.createStructureIteratorMethod());
typeToClass.isGeneratorCallback = {
isGenerator: function(isStatic: boolean, kind: StructureKind.Method, memberName: string): boolean {
return isStatic === false && memberName === "[STRUCTURE_AND_TYPES_CHILDREN]";
}
};
}
}

if (name.startsWith("StatementedNode")) {
const cloneStatementFilter = new CloneStatement_Statements(module);
router.filters.unshift(cloneStatementFilter);
Expand Down
2 changes: 2 additions & 0 deletions stage_3_generation/build/fieldStatements/StatementsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import GetterFilter from "./GetterFilter.js";

import ArrayBooleanAndString from "./ArrayBooleanAndString.js";
import CopyFieldsStatements from "./CopyFields.js";
import StructureIteratorStatements from "./StructureIterator.js";
import ToJSONStatements from "./ToJSON.js";

import TypeManagerStatements from "./TypeStructures/Manager.js";
Expand All @@ -32,6 +33,7 @@ class StatementsRouter extends StatementGetterBase
new ArrayBooleanAndString(module),
new CopyFieldsStatements(module),
new ToJSONStatements(module),
new StructureIteratorStatements(module),
new TypeManagerStatements(module),
new TypeGetterStatements(module),
new TypeStructureGetterStatements(module),
Expand Down
42 changes: 42 additions & 0 deletions stage_3_generation/build/fieldStatements/StructureIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
ClassFieldStatementsMap,
MemberedStatementsKey,
type stringWriterOrStatementImpl
} from "#stage_two/snapshot/source/exports.js";
import GetterFilter from "./GetterFilter.js";

export default class StructureIteratorStatements extends GetterFilter
{
accept(
key: MemberedStatementsKey
): boolean
{
return (key.statementGroupKey === "[STRUCTURE_AND_TYPES_CHILDREN]") && (
/^#.*Manager$/.test(key.fieldKey) || (key.fieldKey === ClassFieldStatementsMap.FIELD_HEAD_SUPER_CALL)
);
}

/*
public *[STRUCTURE_AND_TYPES_CHILDREN](): IterableIterator<
StructureImpls | TypeStructures
> {
yield* super[STRUCTURE_AND_TYPES_CHILDREN]();
if (typeof this.returnTypeStructure === "object")
yield this.returnTypeStructure;
}
*/

getStatements(
key: MemberedStatementsKey
): readonly stringWriterOrStatementImpl[]
{
if (key.fieldKey === ClassFieldStatementsMap.FIELD_HEAD_SUPER_CALL) {
return [`yield* super[STRUCTURE_AND_TYPES_CHILDREN]();`];
}

const propertyName = key.fieldKey.substring(1).replace("Manager", "Structure");
return [
`if (typeof this.${propertyName} === "object") yield this.${propertyName};`
]
}
}
23 changes: 22 additions & 1 deletion stage_3_generation/moduleClasses/DecoratorModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
StringTypeStructureImpl,
TypeAliasDeclarationImpl,
TypeArgumentedTypeStructureImpl,
UnionTypeStructureImpl,
VariableDeclarationImpl,
VariableStatementImpl,
} from "#stage_two/snapshot/source/exports.js";
Expand Down Expand Up @@ -57,7 +58,7 @@ class DecoratorModule extends BaseClassModule

readonly baseName: string;
readonly #fieldsType: LiteralTypeStructureImpl;
readonly decoratorName: string
readonly decoratorName: string;

constructor(
baseName: string,
Expand Down Expand Up @@ -127,6 +128,26 @@ class DecoratorModule extends BaseClassModule
return methodSignature;
}

createStructureIteratorMethod(): MethodSignatureImpl
{
this.addImports("public", [], ["StructureImpls", "TypeStructures"]);
this.addImports("internal", ["STRUCTURE_AND_TYPES_CHILDREN"], []);

const methodSignature = new MethodSignatureImpl("[STRUCTURE_AND_TYPES_CHILDREN]");
methodSignature.docs.push(InternalJSDocTag);

methodSignature.returnTypeStructure = new TypeArgumentedTypeStructureImpl(
LiteralTypeStructureImpl.get("IterableIterator"),
[
new UnionTypeStructureImpl([
LiteralTypeStructureImpl.get("StructureImpls"),
LiteralTypeStructureImpl.get("TypeStructures")
])
]
);
return methodSignature;
}

protected getSourceFileImpl(): SourceFileImpl
{
const sourceFile = new SourceFileImpl;
Expand Down

0 comments on commit 89033f9

Please sign in to comment.