@yantrix/codegen
Version:
Yantrix framework code generator API
252 lines (241 loc) • 9.55 kB
text/typescript
import { TDiagramAction, TStateDiagramMatrix, TDiagramState } from '@yantrix/mermaid-parser';
import { TNullable } from '@yantrix/utils';
import { TDefine, TInjectIdent, TNotes, TMappedKeys, TExpression } from '@yantrix/yantrix-parser';
import { BasicStateDictionary, BasicActionDictionary, BasicEventDictionary } from '@yantrix/automata';
declare class JavaCodegen implements ICodegen<typeof ModuleNames.Java> {
stateDictionary: BasicStateDictionary;
actionDictionary: BasicActionDictionary;
diagram: TStateDiagramMatrixIncludeNotes;
handlersDict: string[];
initialContext: string;
initialContextKeys: string[];
changeStateHandlers: string[];
dictionaries: string[];
protected imports: {
'@yantrix/automata': string[];
};
private package;
constructor({ diagram }: TModuleParams);
getCode(options: TGetCodeOptionsMap[typeof ModuleNames.Java]): string;
getImports(): string;
getDictionaries(): string;
getActionToStateFromState(): string;
getStateTransitionMatrix(): string;
getTransitions(transitions: Record<string, TDiagramAction>): string[];
getDefaultContext(): string;
getClassTemplate(className: string): string;
private getDefaultConstructor;
private setupDictionaries;
private getInitialContext;
private getInitialState;
private getRootReducer;
private getRootReducerStateValidation;
private getRootReducerActionValidation;
private setupClassMembers;
private setupClassMembersAccessors;
private dispatchMethod;
private toStringMethod;
private getTypes;
}
type TImports = {
[path: string]: string[];
};
declare class JavaScriptCodegen implements ICodegen<typeof ModuleNames.JavaScript> {
stateDictionary: BasicStateDictionary;
actionDictionary: BasicActionDictionary;
eventDictionary: BasicEventDictionary;
diagram: TStateDiagramMatrixIncludeNotes;
handlersDict: string[];
defines: TDefine[];
injects: TInjectIdent[];
initialContextKeys: string[];
changeStateHandlers: string[];
dependencyGraph: Map<string, Set<string>>;
constants: TConstants | null;
expressions: TExpressionRecord;
injectedPath: TNullable<string>;
dictionaries: string[];
protected importNamespaces: TNullable<TImports>;
protected imports: TImports;
constructor({ diagram, constants, injectedFunctions }: TModuleParams);
getCode(options: TGetCodeOptionsMap[typeof ModuleNames.JavaScript]): string;
}
declare class PythonCodegen implements ICodegen<typeof ModuleNames.Python> {
stateDictionary: BasicStateDictionary;
actionDictionary: BasicActionDictionary;
diagram: TStateDiagramMatrixIncludeNotes;
handlersDict: string[];
initialContextKeys: string[];
changeStateHandlers: string[];
dependencyGraph: Map<string, Set<string>>;
constants: TConstants | null;
expressions: TExpressionRecord;
dictionaries: string[];
protected imports: {
'yantrix.automata': string[];
'yantrix.codegen': string[];
};
constructor({ diagram, constants }: TModuleParams);
getImports(): string;
getDictionaries(): string;
setupDictionaries(): void;
private getFunctionBody;
getClassTemplate(className: string): string;
replaceFileContents(replacementMap: Record<string, string>): string;
protected getHasStateFunc(): string;
protected getGetStateFunc(): string;
getCode(options: TGetCodeOptionsMap[typeof ModuleNames.Python]): string;
getObjectKeysMap(dict: Record<any, any>): Record<string, string>;
getActionsMap(): Record<string, string>;
getStatesMap(): Record<string, string>;
protected getGetActionFunc(): string;
protected getCreateActionFunc(): string;
getActionToStateFromState(): string;
getStateToContext(): string[];
getActionToStateDict(transitions: Record<string, TDiagramAction>): string[];
protected getIsKeyOf(): string;
getDefaultContext(): string;
protected getRootReducer(): string;
protected getRootReducerStateValidation(): string;
protected getRootReducerStateValidationHead(): string;
protected getRootReducerStateValidationError(): string;
protected getRootReducerActionValidation(): string;
protected getStateValidator(): string;
private buildDependencyGraph;
private detectCycles;
private checkForCyclicDependencies;
private registerCustomFunctions;
protected getActionValidator(): string;
protected getActionToStateFromStateDict(): string[];
protected getContextTransition: (value: number) => string;
private getInitialState;
private getContextItem;
private getInitialContextShape;
private getExpressionValue;
private getExpressionValueDefine;
private setupExpressions;
}
declare class TypeScriptCodegen extends JavaScriptCodegen implements ICodegen<typeof ModuleNames.TypeScript> {
constructor(params: TModuleParams);
getCode(options: TGetCodeOptionsMap[typeof ModuleNames.TypeScript]): string;
}
declare const ModuleNames: {
readonly JavaScript: "javascript";
readonly TypeScript: "typescript";
readonly Python: "python";
readonly Java: "java";
};
/**
* Currently supported language modules for code generation.
*/
declare const Modules: {
readonly javascript: typeof JavaScriptCodegen;
readonly typescript: typeof TypeScriptCodegen;
readonly python: typeof PythonCodegen;
readonly java: typeof JavaCodegen;
};
type TStateDiagramMatrixIncludeNotes = {
states: TStateIncludingNotes[];
} & Omit<TStateDiagramMatrix, 'states'>;
type TDiagramStateOmitNotes = Omit<TDiagramState, 'notes'>;
type TStateIncludingNotes = {
notes: TNotes | null;
} & TDiagramStateOmitNotes;
type TConstants = Record<string, string | number>;
interface ICodegenOptions<T = TOutLang> {
language: T;
constants: TNullable<TConstants>;
functionFilePath: TNullable<string>;
}
/**
* Represents the options for generating code.
*/
interface IGenerateOptions {
/**
* The name of the class for the generated Automata.
*/
className: string;
/**
* The output language for the generated code.
* You can check the supported languages and features [here](/integrations/100_language_support.html).
*/
outLang: TOutLang;
/**
* Included constants to be used in the generated Automata.
*/
constants?: string;
functionFilePath?: string;
}
interface ITypedObjectProps {
typeName: string;
typeGuardName: string;
name: string;
body: string;
}
interface ITypedObject extends ITypedObjectProps {
codeBlock: string;
}
interface IGetCodeJSOptions {
className: string;
}
interface IGetCodeTSOptions extends IGetCodeJSOptions {
}
interface IGetCodePythonOptions {
className: string;
}
interface IGetCodeJavaOptions {
className: string;
}
type TGetCodeOptionsMap = {
[ModuleNames.JavaScript]: IGetCodeJSOptions;
[ModuleNames.Python]: IGetCodePythonOptions;
[ModuleNames.TypeScript]: IGetCodeTSOptions;
[ModuleNames.Java]: IGetCodeJavaOptions;
};
/**
* Interface that is implemented by all code generators.
* @template T - The output language.
* @property getCode - The function that returns the generated code, following all the conventions established by the output language `T`.
*/
interface ICodegen<T extends TOutLang> {
/**
* The function that returns the generated code, following all the conventions established by the output language `T`
* @param options - Options for codegen in the desired output language `T`.
* @returns The generated code.
*/
getCode: (options: TGetCodeOptionsMap[T]) => string;
}
type TOutLang = keyof typeof Modules;
type TExpressionRecord = {
[K in TMappedKeys]: (arg: TExpression<K>) => string;
};
type TUserFunctionsDict = {
path: TNullable<string>;
};
declare const TAssignTypeDict: {
readonly PAYLOAD: "payload";
readonly PREV_CONTEXT: "prevContext";
};
type TAssignTypes = (typeof TAssignTypeDict)[keyof typeof TAssignTypeDict];
type TModuleParams = {
diagram: TStateDiagramMatrixIncludeNotes;
constants: TNullable<TConstants>;
injectedFunctions: TUserFunctionsDict;
};
/**
* @packageDocumentation
*
* The `@yantrix/codegen` package is a code generation engine for the Yantrix framework,
* designed to transform extended Mermaid state diagrams into type-safe finite state machine implementations.
*
* You can check the supported languages and features [here](/integrations/100_language_support.html).
*
*/
/**
* Main function that's used to asynchronously generate Yantrix FSMs.
* @param diagram - The state diagram, processed by Mermaid & Yantrix Parsers (`mermaid-parser`, `yantrix-parser`)
* @param options - Options for configuring the generation process, such as the output language and automata class name
* @returns - A promise with the code of generated FSM as string
*/
declare function generateAutomataFromStateDiagram(diagram: TStateDiagramMatrix, options: IGenerateOptions): Promise<string>;
export { type ICodegen, type ICodegenOptions, type IGenerateOptions, type IGetCodeJSOptions, type IGetCodeJavaOptions, type IGetCodePythonOptions, type IGetCodeTSOptions, type ITypedObject, type ITypedObjectProps, ModuleNames, Modules, TAssignTypeDict, type TAssignTypes, type TConstants, type TExpressionRecord, type TGetCodeOptionsMap, type TModuleParams, type TOutLang, type TStateDiagramMatrixIncludeNotes, type TStateIncludingNotes, type TUserFunctionsDict, generateAutomataFromStateDiagram };