@rsconcept/domain 1.7.6 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{analyzer-DqjdciC5.d.ts → analyzer-DpE0qknt.d.ts} +1 -2
- package/dist/index.d.ts +1 -1
- package/dist/library/index.d.ts +87 -1
- package/dist/library/index.js +255 -1
- package/dist/library/index.js.map +1 -0
- package/dist/library/oss-api.js +1 -1
- package/dist/library/rsform-api.d.ts +12 -2
- package/dist/library/rsform-api.js +24 -2
- package/dist/library/rsform-api.js.map +1 -1
- package/dist/library/rsform.d.ts +1 -1
- package/dist/library/structure-planner.js +1 -1
- package/dist/rslang/api.d.ts +12 -1
- package/dist/rslang/api.js +30 -2
- package/dist/rslang/api.js.map +1 -1
- package/dist/rslang/index.d.ts +1 -1
- package/dist/rslang/semantic/analyzer.d.ts +1 -1
- package/dist/rslang/semantic/analyzer.js +5 -13
- package/dist/rslang/semantic/analyzer.js.map +1 -1
- package/package.json +14 -13
- package/src/library/index.ts +7 -0
- package/src/library/rsform-api.test.ts +11 -1
- package/src/library/rsform-api.ts +40 -1
- package/src/library/template-instantiation.test.ts +308 -0
- package/src/library/template-instantiation.ts +426 -0
- package/src/rslang/api.test.ts +28 -0
- package/src/rslang/api.ts +31 -2
- package/src/rslang/semantic/analyzer.ts +6 -14
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rsform-api.js","names":[],"sources":["../../src/library/rsform-api.ts"],"sourcesContent":["/**\n * Module: API for formal representation for systems of concepts.\n */\n\nimport { applyEntityReferenceMapping } from '../cctext/language-api';\nimport {\n type AnalysisFull,\n RSErrorCode,\n TypeClass,\n TypeID,\n type TypePath,\n type Typification,\n ValueClass\n} from '../rslang';\nimport { type AliasMapping, applyAliasMapping, applyTypificationMapping } from '../rslang/api';\nimport { basic, bool, constant, type EchelonFunctional, isTypification } from '../rslang/semantic/typification';\nimport { applyPath } from '../rslang/semantic/typification-api';\n\nimport { type LibraryItem } from './library';\nimport {\n type ArgumentValue,\n type Constituenta,\n CstClass,\n CstStatus,\n CstType,\n type RSForm,\n type RSFormStats\n} from './rsform';\n\n/** Record of {@link CstType} prefixes. */\nconst CST_TYPE_PREFIX: Record<CstType, string> = {\n [CstType.NOMINAL]: 'N',\n [CstType.BASE]: 'X',\n [CstType.CONSTANT]: 'C',\n [CstType.STRUCTURED]: 'S',\n [CstType.AXIOM]: 'A',\n [CstType.TERM]: 'D',\n [CstType.FUNCTION]: 'F',\n [CstType.PREDICATE]: 'P',\n [CstType.STATEMENT]: 'T'\n};\n\n/** Record of {@link CstType} to {@link CstClass} mapping. */\nconst CST_TYPE_TO_CLASS: Record<CstType, CstClass> = {\n [CstType.NOMINAL]: CstClass.NOMINAL,\n [CstType.BASE]: CstClass.BASIC,\n [CstType.CONSTANT]: CstClass.BASIC,\n [CstType.STRUCTURED]: CstClass.BASIC,\n [CstType.TERM]: CstClass.DERIVED,\n [CstType.FUNCTION]: CstClass.DERIVED,\n [CstType.AXIOM]: CstClass.STATEMENT,\n [CstType.PREDICATE]: CstClass.DERIVED,\n [CstType.STATEMENT]: CstClass.STATEMENT\n};\n\n/** Checks if {@link Constituenta} is a schema issue. */\nexport function isSchemaIssue(cst: Constituenta): boolean {\n if (\n cst.homonyms.length > 0 ||\n cst.formalDuplicates.length > 0 ||\n cst.status === CstStatus.INCORRECT ||\n cst.status === CstStatus.INCALCULABLE ||\n cst.is_type_mismatch\n ) {\n return true;\n }\n if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {\n if (!cst.convention || !cst.term_resolved) {\n return true;\n }\n }\n return false;\n}\n\n/** Infers the status of an expression based on parsing and value information. */\nexport function inferStatus(parse: boolean, value?: ValueClass | null): CstStatus {\n if (!parse) {\n return CstStatus.INCORRECT;\n }\n if (value === null) {\n return CstStatus.INCALCULABLE;\n }\n if (value === ValueClass.PROPERTY) {\n return CstStatus.PROPERTY;\n }\n return CstStatus.VERIFIED;\n}\n\n/** Infers type of constituent for a given template and arguments. */\nexport function inferTemplatedType(templateType: CstType, args: ArgumentValue[]): CstType {\n if (args.length === 0 || args.some(arg => !arg.value)) {\n return templateType;\n } else if (templateType === CstType.PREDICATE) {\n return CstType.AXIOM;\n } else {\n return CstType.TERM;\n }\n}\n\n/** Checks if given expression is a template. */\nexport function inferTemplate(expression: string): boolean {\n const match = expression.match(/R\\d+/g);\n return (match && match?.length > 0) ?? false;\n}\n\n/** Infers the {@link CstClass} based on the provided {@link CstType} and template status. */\nexport function inferClass(type: CstType, isTemplate: boolean = false): CstClass {\n if (isTemplate) {\n return CstClass.TEMPLATE;\n }\n return CST_TYPE_TO_CLASS[type];\n}\n\n/** Check if {@link Constituenta} is a template or a category. */\nexport function isTemplateCst(cst: Constituenta): boolean {\n return cst.cst_type === CstType.FUNCTION || cst.cst_type === CstType.PREDICATE || cst.cst_type === CstType.STATEMENT;\n}\n\n/** Apply filter based on start {@link Constituenta} type. */\nexport function applyFilterCategory(start: Constituenta, items: Constituenta[]): Constituenta[] {\n const startIndex = items.indexOf(start);\n if (startIndex === -1) {\n return [];\n }\n const nextCategoryIndex = items.findIndex((cst, index) => index > startIndex && cst.cst_type === CstType.STATEMENT);\n\n return items.filter((_, index) => index >= startIndex && (nextCategoryIndex === -1 || index < nextCategoryIndex));\n}\n\n/** Prefix for alias indicating {@link CstType}. */\nexport function getCstTypePrefix(type: CstType): string {\n return CST_TYPE_PREFIX[type];\n}\n\n/** Guess {@link CstType} from user input hint. */\nexport function guessCstType(hint: string): CstType | null {\n if (hint.length !== 1) {\n return null;\n }\n for (const [type, prefix] of Object.entries(CST_TYPE_PREFIX)) {\n if (hint === prefix) {\n return type as CstType;\n }\n }\n return null;\n}\n\n/** Evaluate if {@link CstType} is basic concept. */\nexport function isBasicConcept(type: CstType): boolean {\n switch (type) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n return true;\n\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is base set or constant set. */\nexport function isBaseSet(type: CstType): boolean {\n switch (type) {\n case CstType.BASE:\n case CstType.CONSTANT:\n return true;\n\n case CstType.NOMINAL:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is a function. */\nexport function isFunctional(type: CstType): boolean {\n switch (type) {\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n case CstType.TERM:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is logical. */\nexport function isLogical(type: CstType): boolean {\n switch (type) {\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return false;\n }\n}\n\n/** Evaluate if {@link Constituenta} can be used produce structure. */\nexport function canProduceStructure(cst: Constituenta): boolean {\n switch (cst.cst_type) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.AXIOM:\n case CstType.STATEMENT:\n case CstType.PREDICATE:\n return false;\n }\n if (!cst.effectiveType) {\n return false;\n }\n if (cst.cst_type === CstType.FUNCTION) {\n const result = (cst.effectiveType as EchelonFunctional).result;\n return typeCanProduceStructure(result);\n }\n if (!isTypification(cst.effectiveType)) {\n return false;\n }\n return typeCanProduceStructure(cst.effectiveType as Typification);\n}\n\n/** Evaluate if {@link CstType} can have manual typification. */\nexport function canHaveManualTypification(type: CstType): boolean {\n switch (type) {\n case CstType.STRUCTURED:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Validate alias format for a given {@link CstType} (prefix + digits, min length 2). */\nexport function validateAliasFormat(alias: string, type: CstType): boolean {\n if (alias.length < 2) {\n return false;\n }\n const prefix = getCstTypePrefix(type);\n if (!alias.startsWith(prefix)) {\n return false;\n }\n if (!/^\\d+$/.exec(alias.substring(prefix.length))) {\n return false;\n }\n return true;\n}\n\n/** Validate new alias against {@link CstType} and {@link RSForm}. */\nexport function validateNewAlias(alias: string, type: CstType, schema: RSForm): boolean {\n if (!validateAliasFormat(alias, type)) {\n return false;\n }\n if (schema.cstByAlias.has(alias)) {\n return false;\n }\n return true;\n}\n\n/** Generate alias for new {@link Constituenta} of a given {@link CstType} for current {@link RSForm}. */\nexport function generateAlias(type: CstType, schema: RSForm, takenAliases: string[] = []): string {\n const prefix = getCstTypePrefix(type);\n if (schema.items.length <= 0) {\n return `${prefix}1`;\n }\n let index = schema.items.reduce((prev, cst, index) => {\n if (cst.cst_type !== type) {\n return prev;\n }\n index = Number(cst.alias.slice(1 - cst.alias.length)) + 1;\n return Math.max(prev, index);\n }, 1);\n let alias = `${prefix}${index}`;\n while (takenAliases.includes(alias)) {\n index = index + 1;\n alias = `${prefix}${index}`;\n }\n return alias;\n}\n\n/**\n * Id to pass as `insert_after` when creating a new {@link Constituenta} spawned by `targetId`:\n * after the last existing child of that spawner, or after the spawner itself if none.\n */\nexport function inferNewSpawnPosition(schema: RSForm, targetId: number): number {\n let last: number | null = null;\n for (const cst of schema.items) {\n if (cst.spawner === targetId) {\n last = cst.id;\n }\n }\n return last ?? targetId;\n}\n\n/** Sorts library items relevant for InlineSynthesis with specified {@link RSForm}. */\nexport function sortItemsForInlineSynthesis(receiver: RSForm, items: readonly LibraryItem[]): LibraryItem[] {\n const result = items.filter(item => item.location === receiver.location);\n for (const item of items) {\n if (item.visible && item.owner === item.owner && !result.includes(item)) {\n result.push(item);\n }\n }\n for (const item of items) {\n if (item.visible && !result.includes(item)) {\n result.push(item);\n }\n }\n for (const item of items) {\n if (!result.includes(item)) {\n result.push(item);\n }\n }\n return result;\n}\n\n/** Fields of {@link Constituenta} updated in place by alias mapping. */\nexport interface ConstituentaMappableFields {\n alias: string;\n definition_formal: string;\n typification_manual: string;\n term_raw: string;\n definition_raw: string;\n}\n\n/**\n * Remap formal expressions and terminological entity references in constituent fields.\n * Mutates each element of `items` in place; pass owned or cloned data only.\n */\nexport function applyMappingToConstituents<T extends ConstituentaMappableFields>(\n items: T[],\n mapping: AliasMapping,\n changeAliases: boolean\n): void {\n for (const cst of items) {\n if (changeAliases && cst.alias in mapping) {\n cst.alias = mapping[cst.alias];\n }\n cst.definition_formal = applyAliasMapping(cst.definition_formal, mapping);\n cst.typification_manual = applyTypificationMapping(cst.typification_manual, mapping);\n cst.term_raw = applyEntityReferenceMapping(cst.term_raw, mapping);\n cst.definition_raw = applyEntityReferenceMapping(cst.definition_raw, mapping);\n }\n}\n\n/** Remove alias from expression. */\nexport function removeAliasReference(expression: string, alias: string): string {\n const result = expression.replaceAll(new RegExp(`\\\\b${alias}\\\\b`, 'g'), 'DEL');\n return result === 'DEL' ? '' : result;\n}\n\n/** Add alias to expression. */\nexport function addAliasReference(expression: string, alias: string): string {\n return expression + ' ' + alias;\n}\n\n/** Returns expected {@link TypeClass} of formal definition for {@link CstType}. */\nexport function typeClassForCstType(cstType: CstType): TypeClass {\n switch (cstType) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.TERM:\n return TypeClass.typification;\n case CstType.FUNCTION:\n return TypeClass.function;\n case CstType.PREDICATE:\n return TypeClass.predicate;\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return TypeClass.logic;\n }\n}\n\n/** Checks whether a constituenta type supports formal definitions. */\nexport function canHaveFormalDefinition(cstType: CstType): boolean {\n return cstType !== CstType.BASE && cstType !== CstType.CONSTANT;\n}\n\n/** Analyze expression for {@link RSForm}. */\nexport function getAnalysisFor(expression: string, cstType: CstType, schema: RSForm, alias?: string): AnalysisFull {\n if (!canHaveFormalDefinition(cstType)) {\n if (expression.trim().length === 0) {\n const fallbackAlias = alias && alias.length > 0 ? alias : 'X0';\n const type = cstType === CstType.BASE ? bool(basic(fallbackAlias)) : bool(constant(fallbackAlias));\n return {\n success: true,\n type,\n valueClass: ValueClass.VALUE,\n errors: [],\n ast: null\n };\n }\n return {\n success: false,\n type: null,\n valueClass: null,\n errors: [\n {\n code: RSErrorCode.definitionNotAllowed,\n from: 0,\n to: Math.max(expression.length, 1),\n params: [cstType, alias ?? '']\n }\n ],\n ast: null\n };\n }\n return schema.analyzer.checkFull(expression, {\n expected: typeClassForCstType(cstType),\n isDomain: cstType === CstType.STRUCTURED\n });\n}\n\n/** Calculate statistics for {@link RSForm}. */\nexport function calculateSchemaStats(target: RSForm): RSFormStats {\n const items = target.items;\n return {\n count_all: items.length,\n count_crucial: items.reduce((sum, cst) => sum + (cst.crucial ? 1 : 0), 0),\n\n step_complexity: items.reduce((sum, cst) => sum + calculateStepComplexity(cst), 0),\n\n count_problematic: items.reduce((sum, cst) => sum + (isSchemaIssue(cst) ? 1 : 0), 0),\n count_homonyms: items.reduce((sum, cst) => sum + (cst.homonyms.length > 0 ? 1 : 0), 0),\n count_formal_duplicates: items.reduce((sum, cst) => sum + (cst.formalDuplicates.length > 0 ? 1 : 0), 0),\n count_missing_convention: items.reduce((sum, cst) => sum + (isMissingConvention(cst) ? 1 : 0), 0),\n count_type_mismatch: items.reduce((sum, cst) => sum + (cst.is_type_mismatch ? 1 : 0), 0),\n\n count_incorrect: items.reduce((sum, cst) => sum + (cst.status === CstStatus.INCORRECT ? 1 : 0), 0),\n count_property: items.reduce((sum, cst) => sum + (cst.analysis?.valueClass === ValueClass.PROPERTY ? 1 : 0), 0),\n count_incalculable: items.reduce(\n (sum, cst) => sum + (cst.analysis?.success && cst.analysis.valueClass === null ? 1 : 0),\n 0\n ),\n count_inherited: items.reduce((sum, cst) => sum + (cst.is_inherited ? 1 : 0), 0),\n\n count_text_term: items.reduce((sum, cst) => sum + (cst.term_raw ? 1 : 0), 0),\n count_definition: items.reduce((sum, cst) => sum + (cst.definition_raw ? 1 : 0), 0),\n count_convention: items.reduce((sum, cst) => sum + (isBasicConcept(cst.cst_type) && cst.convention ? 1 : 0), 0),\n count_comment: items.reduce((sum, cst) => sum + (!isBasicConcept(cst.cst_type) && !cst.convention ? 1 : 0), 0),\n\n count_base: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.BASE ? 1 : 0), 0),\n count_constant: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.CONSTANT ? 1 : 0), 0),\n count_structured: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STRUCTURED ? 1 : 0), 0),\n count_axiom: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.AXIOM ? 1 : 0), 0),\n count_term: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.TERM ? 1 : 0), 0),\n count_function: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.FUNCTION ? 1 : 0), 0),\n count_predicate: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.PREDICATE ? 1 : 0), 0),\n count_statement: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STATEMENT ? 1 : 0), 0),\n count_nominal: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.NOMINAL ? 1 : 0), 0)\n };\n}\n\n/** Finds {@link Constituenta} by structure path. */\nexport function findCstByStructure(schema: RSForm, target: Constituenta, path: TypePath): Constituenta | null {\n for (const cst of schema.items) {\n if (cst.spawner === target.id && cst.spawner_path) {\n if (cst.spawner_path.length === path.length && cst.spawner_path.every((v, i) => v === path[i])) {\n return cst;\n }\n }\n }\n return null;\n}\n\n/** Retrieves name for piece of target {@link Constituenta} structure. */\nexport function getStructureName(schema: RSForm, target: Constituenta, path: TypePath): string {\n const representation = findCstByStructure(schema, target, path);\n if (representation) {\n return `${representation.alias}: ${representation.term_resolved}`;\n }\n if (!isTypification(target.effectiveType)) {\n return '';\n }\n const type = applyPath(target.effectiveType as Typification, path);\n if (type?.typeID === TypeID.basic) {\n const cst = schema.cstByAlias.get(type.baseID);\n if (cst?.term_resolved) {\n return `${cst.alias}: ${cst.term_resolved}`;\n }\n }\n return '';\n}\n\n// ========= Internals =====\nfunction calculateStepComplexity(cst: Constituenta): number {\n if (cst.cst_type === CstType.AXIOM || cst.cst_type === CstType.NOMINAL || !isBasicConcept(cst.cst_type)) {\n return 0;\n }\n if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT || !cst.effectiveType) {\n return 1;\n }\n\n const type = cst.effectiveType as Typification;\n return calculateTypificationComplexity(type) + 1;\n}\n\nfunction calculateTypificationComplexity(type: Typification): number {\n switch (type.typeID) {\n case TypeID.basic:\n case TypeID.integer:\n case TypeID.anyTypification:\n return 0;\n case TypeID.tuple:\n return (\n type.factors.length + type.factors.reduce((sum, factor) => sum + calculateTypificationComplexity(factor), 0)\n );\n case TypeID.collection:\n if (type.base.typeID === TypeID.tuple) {\n let sum = 0;\n type.base.factors.forEach(factor => {\n sum += calculateTypificationComplexity(factor);\n });\n return sum + type.base.factors.length;\n } else if (type.base.typeID === TypeID.collection) {\n return calculateTypificationComplexity(type.base) + 1;\n }\n return 0;\n }\n}\n\nfunction isMissingConvention(cst: Constituenta): boolean {\n if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {\n if (!cst.convention || !cst.term_resolved) {\n return true;\n }\n }\n return false;\n}\n\n/** Evaluate if {@link Typification} can be used to produce structure. */\nfunction typeCanProduceStructure(type: Typification): boolean {\n if (type.typeID === TypeID.basic || type.typeID === TypeID.integer || type.typeID === TypeID.anyTypification) {\n return false;\n } else if (type.typeID === TypeID.tuple) {\n return true;\n } else {\n return (\n type.base.typeID !== TypeID.basic &&\n type.base.typeID !== TypeID.integer &&\n type.base.typeID !== T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gC,MAA4B;CACnE,QAAQ,KAAK,QAAb;EACE,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,OAAO,iBACV,OAAO;EACT,KAAK,OAAO,OACV,OACE,KAAK,QAAQ,SAAS,KAAK,QAAQ,QAAQ,KAAK,WAAW,MAAM,gCAAgC,MAAM,GAAG,CAAC;EAE/G,KAAK,OAAO;GACV,IAAI,KAAK,KAAK,WAAW,OAAO,OAAO;IACrC,IAAI,MAAM;IACV,KAAK,KAAK,QAAQ,SAAQ,WAAU;KAClC,OAAO,gCAAgC,MAAM;IAC/C,CAAC;IACD,OAAO,MAAM,KAAK,KAAK,QAAQ;GACjC,OAAO,IAAI,KAAK,KAAK,WAAW,OAAO,YACrC,OAAO,gCAAgC,KAAK,IAAI,IAAI;GAEtD,OAAO;CACX;AACF;AAEA,SAAS,oBAAoB,KAA4B;CACvD,IAAI,eAAe,IAAI,QAAQ,KAAK,CAAC,UAAU,IAAI,QAAQ,GACzD;MAAI,CAAC,IAAI,cAAc,CAAC,IAAI,eAC1B,OAAO;CACT;CAEF,OAAO;AACT;;AAGA,SAAS,wBAAwB,MAA6B;CAC5D,IAAI,KAAK,WAAW,OAAO,SAAS,KAAK,WAAW,OAAO,WAAW,KAAK,WAAW,OAAO,iBAC3F,OAAO;MACF,IAAI,KAAK,WAAW,OAAO,OAChC,OAAO;MAEP,OACE,KAAK,KAAK,WAAW,OAAO,SAC5B,KAAK,KAAK,WAAW,OAAO,WAC5B,KAAK,KAAK,WAAW,OAAO;AAGlC"}
|
|
1
|
+
{"version":3,"file":"rsform-api.js","names":[],"sources":["../../src/library/rsform-api.ts"],"sourcesContent":["/**\n * Module: API for formal representation for systems of concepts.\n */\n\nimport { applyEntityReferenceMapping } from '../cctext/language-api';\nimport { type AstNode } from '../parsing';\nimport {\n type AnalysisFull,\n RSErrorCode,\n TypeClass,\n TypeID,\n type TypePath,\n type Typification,\n ValueClass\n} from '../rslang';\nimport {\n type AliasMapping,\n applyAliasMapping,\n applyTypificationMapping,\n generateExpressionFromAst,\n parseRSLangExpression\n} from '../rslang/api';\nimport { basic, bool, constant, type EchelonFunctional, isTypification } from '../rslang/semantic/typification';\nimport { applyPath } from '../rslang/semantic/typification-api';\n\nimport { type LibraryItem } from './library';\nimport {\n type ArgumentValue,\n type Constituenta,\n CstClass,\n CstStatus,\n CstType,\n type RSForm,\n type RSFormStats\n} from './rsform';\n\n/** Record of {@link CstType} prefixes. */\nconst CST_TYPE_PREFIX: Record<CstType, string> = {\n [CstType.NOMINAL]: 'N',\n [CstType.BASE]: 'X',\n [CstType.CONSTANT]: 'C',\n [CstType.STRUCTURED]: 'S',\n [CstType.AXIOM]: 'A',\n [CstType.TERM]: 'D',\n [CstType.FUNCTION]: 'F',\n [CstType.PREDICATE]: 'P',\n [CstType.STATEMENT]: 'T'\n};\n\n/** Record of {@link CstType} to {@link CstClass} mapping. */\nconst CST_TYPE_TO_CLASS: Record<CstType, CstClass> = {\n [CstType.NOMINAL]: CstClass.NOMINAL,\n [CstType.BASE]: CstClass.BASIC,\n [CstType.CONSTANT]: CstClass.BASIC,\n [CstType.STRUCTURED]: CstClass.BASIC,\n [CstType.TERM]: CstClass.DERIVED,\n [CstType.FUNCTION]: CstClass.DERIVED,\n [CstType.AXIOM]: CstClass.STATEMENT,\n [CstType.PREDICATE]: CstClass.DERIVED,\n [CstType.STATEMENT]: CstClass.STATEMENT\n};\n\n/** Checks if {@link Constituenta} is a schema issue. */\nexport function isSchemaIssue(cst: Constituenta): boolean {\n if (\n cst.homonyms.length > 0 ||\n cst.formalDuplicates.length > 0 ||\n cst.status === CstStatus.INCORRECT ||\n cst.status === CstStatus.INCALCULABLE ||\n cst.is_type_mismatch\n ) {\n return true;\n }\n if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {\n if (!cst.convention || !cst.term_resolved) {\n return true;\n }\n }\n return false;\n}\n\n/** Infers the status of an expression based on parsing and value information. */\nexport function inferStatus(parse: boolean, value?: ValueClass | null): CstStatus {\n if (!parse) {\n return CstStatus.INCORRECT;\n }\n if (value === null) {\n return CstStatus.INCALCULABLE;\n }\n if (value === ValueClass.PROPERTY) {\n return CstStatus.PROPERTY;\n }\n return CstStatus.VERIFIED;\n}\n\n/** Infers type of constituent for a given template and arguments. */\nexport function inferTemplatedType(templateType: CstType, args: ArgumentValue[]): CstType {\n if (args.length === 0 || args.some(arg => !arg.value)) {\n return templateType;\n } else if (templateType === CstType.PREDICATE) {\n return CstType.AXIOM;\n } else {\n return CstType.TERM;\n }\n}\n\n/** Build alias mapping from template argument bindings. */\nexport function argumentValuesToMapping(args: ArgumentValue[]): AliasMapping {\n const mapping: AliasMapping = {};\n for (const arg of args) {\n if (arg.value) {\n mapping[arg.alias] = arg.value;\n }\n }\n return mapping;\n}\n\n/**\n * Normalize a RSLang expression for duplicate comparison.\n *\n * Returns `null` for basic concepts without formal definitions. Pass a pre-parsed `ast`\n * when the expression was already analyzed to avoid re-parsing.\n */\nexport function normalizeExpression(expression: string, cst_type: CstType, ast?: AstNode | null): string | null {\n if (isBasicConcept(cst_type) && cst_type !== CstType.AXIOM) {\n return null;\n }\n const parsed = ast ?? parseRSLangExpression(expression);\n let normalized = '';\n if (parsed && !parsed.hasError) {\n normalized = generateExpressionFromAst(parsed, { normalize: true });\n } else {\n normalized = expression;\n }\n normalized = normalized.replace(/\\s+/g, '');\n return normalized === '' ? null : normalized;\n}\n\n/** Checks if given expression is a template. */\nexport function inferTemplate(expression: string): boolean {\n const match = expression.match(/R\\d+/g);\n return (match && match?.length > 0) ?? false;\n}\n\n/** Infers the {@link CstClass} based on the provided {@link CstType} and template status. */\nexport function inferClass(type: CstType, isTemplate: boolean = false): CstClass {\n if (isTemplate) {\n return CstClass.TEMPLATE;\n }\n return CST_TYPE_TO_CLASS[type];\n}\n\n/** Check if {@link Constituenta} is a template or a category. */\nexport function isTemplateCst(cst: Constituenta): boolean {\n return cst.cst_type === CstType.FUNCTION || cst.cst_type === CstType.PREDICATE || cst.cst_type === CstType.STATEMENT;\n}\n\n/** Apply filter based on start {@link Constituenta} type. */\nexport function applyFilterCategory(start: Constituenta, items: Constituenta[]): Constituenta[] {\n const startIndex = items.indexOf(start);\n if (startIndex === -1) {\n return [];\n }\n const nextCategoryIndex = items.findIndex((cst, index) => index > startIndex && cst.cst_type === CstType.STATEMENT);\n\n return items.filter((_, index) => index >= startIndex && (nextCategoryIndex === -1 || index < nextCategoryIndex));\n}\n\n/** Prefix for alias indicating {@link CstType}. */\nexport function getCstTypePrefix(type: CstType): string {\n return CST_TYPE_PREFIX[type];\n}\n\n/** Guess {@link CstType} from user input hint. */\nexport function guessCstType(hint: string): CstType | null {\n if (hint.length !== 1) {\n return null;\n }\n for (const [type, prefix] of Object.entries(CST_TYPE_PREFIX)) {\n if (hint === prefix) {\n return type as CstType;\n }\n }\n return null;\n}\n\n/** Evaluate if {@link CstType} is basic concept. */\nexport function isBasicConcept(type: CstType): boolean {\n switch (type) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n return true;\n\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is base set or constant set. */\nexport function isBaseSet(type: CstType): boolean {\n switch (type) {\n case CstType.BASE:\n case CstType.CONSTANT:\n return true;\n\n case CstType.NOMINAL:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is a function. */\nexport function isFunctional(type: CstType): boolean {\n switch (type) {\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.AXIOM:\n case CstType.TERM:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Evaluate if {@link CstType} is logical. */\nexport function isLogical(type: CstType): boolean {\n switch (type) {\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return false;\n }\n}\n\n/** Evaluate if {@link Constituenta} can be used produce structure. */\nexport function canProduceStructure(cst: Constituenta): boolean {\n switch (cst.cst_type) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.AXIOM:\n case CstType.STATEMENT:\n case CstType.PREDICATE:\n return false;\n }\n if (!cst.effectiveType) {\n return false;\n }\n if (cst.cst_type === CstType.FUNCTION) {\n const result = (cst.effectiveType as EchelonFunctional).result;\n return typeCanProduceStructure(result);\n }\n if (!isTypification(cst.effectiveType)) {\n return false;\n }\n return typeCanProduceStructure(cst.effectiveType as Typification);\n}\n\n/** Evaluate if {@link CstType} can have manual typification. */\nexport function canHaveManualTypification(type: CstType): boolean {\n switch (type) {\n case CstType.STRUCTURED:\n case CstType.TERM:\n case CstType.FUNCTION:\n case CstType.PREDICATE:\n return true;\n\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return false;\n }\n}\n\n/** Validate alias format for a given {@link CstType} (prefix + digits, min length 2). */\nexport function validateAliasFormat(alias: string, type: CstType): boolean {\n if (alias.length < 2) {\n return false;\n }\n const prefix = getCstTypePrefix(type);\n if (!alias.startsWith(prefix)) {\n return false;\n }\n if (!/^\\d+$/.exec(alias.substring(prefix.length))) {\n return false;\n }\n return true;\n}\n\n/** Validate new alias against {@link CstType} and {@link RSForm}. */\nexport function validateNewAlias(alias: string, type: CstType, schema: RSForm): boolean {\n if (!validateAliasFormat(alias, type)) {\n return false;\n }\n if (schema.cstByAlias.has(alias)) {\n return false;\n }\n return true;\n}\n\n/** Generate alias for new {@link Constituenta} of a given {@link CstType} for current {@link RSForm}. */\nexport function generateAlias(type: CstType, schema: RSForm, takenAliases: string[] = []): string {\n const prefix = getCstTypePrefix(type);\n if (schema.items.length <= 0) {\n return `${prefix}1`;\n }\n let index = schema.items.reduce((prev, cst, index) => {\n if (cst.cst_type !== type) {\n return prev;\n }\n index = Number(cst.alias.slice(1 - cst.alias.length)) + 1;\n return Math.max(prev, index);\n }, 1);\n let alias = `${prefix}${index}`;\n while (takenAliases.includes(alias)) {\n index = index + 1;\n alias = `${prefix}${index}`;\n }\n return alias;\n}\n\n/**\n * Id to pass as `insert_after` when creating a new {@link Constituenta} spawned by `targetId`:\n * after the last existing child of that spawner, or after the spawner itself if none.\n */\nexport function inferNewSpawnPosition(schema: RSForm, targetId: number): number {\n let last: number | null = null;\n for (const cst of schema.items) {\n if (cst.spawner === targetId) {\n last = cst.id;\n }\n }\n return last ?? targetId;\n}\n\n/** Sorts library items relevant for InlineSynthesis with specified {@link RSForm}. */\nexport function sortItemsForInlineSynthesis(receiver: RSForm, items: readonly LibraryItem[]): LibraryItem[] {\n const result = items.filter(item => item.location === receiver.location);\n for (const item of items) {\n if (item.visible && item.owner === item.owner && !result.includes(item)) {\n result.push(item);\n }\n }\n for (const item of items) {\n if (item.visible && !result.includes(item)) {\n result.push(item);\n }\n }\n for (const item of items) {\n if (!result.includes(item)) {\n result.push(item);\n }\n }\n return result;\n}\n\n/** Fields of {@link Constituenta} updated in place by alias mapping. */\nexport interface ConstituentaMappableFields {\n alias: string;\n definition_formal: string;\n typification_manual: string;\n term_raw: string;\n definition_raw: string;\n}\n\n/**\n * Remap formal expressions and terminological entity references in constituent fields.\n * Mutates each element of `items` in place; pass owned or cloned data only.\n */\nexport function applyMappingToConstituents<T extends ConstituentaMappableFields>(\n items: T[],\n mapping: AliasMapping,\n changeAliases: boolean\n): void {\n for (const cst of items) {\n if (changeAliases && cst.alias in mapping) {\n cst.alias = mapping[cst.alias];\n }\n cst.definition_formal = applyAliasMapping(cst.definition_formal, mapping);\n cst.typification_manual = applyTypificationMapping(cst.typification_manual, mapping);\n cst.term_raw = applyEntityReferenceMapping(cst.term_raw, mapping);\n cst.definition_raw = applyEntityReferenceMapping(cst.definition_raw, mapping);\n }\n}\n\n/** Remove alias from expression. */\nexport function removeAliasReference(expression: string, alias: string): string {\n const result = expression.replaceAll(new RegExp(`\\\\b${alias}\\\\b`, 'g'), 'DEL');\n return result === 'DEL' ? '' : result;\n}\n\n/** Add alias to expression. */\nexport function addAliasReference(expression: string, alias: string): string {\n return expression + ' ' + alias;\n}\n\n/** Returns expected {@link TypeClass} of formal definition for {@link CstType}. */\nexport function typeClassForCstType(cstType: CstType): TypeClass {\n switch (cstType) {\n case CstType.NOMINAL:\n case CstType.BASE:\n case CstType.CONSTANT:\n case CstType.STRUCTURED:\n case CstType.TERM:\n return TypeClass.typification;\n case CstType.FUNCTION:\n return TypeClass.function;\n case CstType.PREDICATE:\n return TypeClass.predicate;\n case CstType.AXIOM:\n case CstType.STATEMENT:\n return TypeClass.logic;\n }\n}\n\n/** Checks whether a constituenta type supports formal definitions. */\nexport function canHaveFormalDefinition(cstType: CstType): boolean {\n return cstType !== CstType.BASE && cstType !== CstType.CONSTANT;\n}\n\n/** Analyze expression for {@link RSForm}. */\nexport function getAnalysisFor(expression: string, cstType: CstType, schema: RSForm, alias?: string): AnalysisFull {\n if (!canHaveFormalDefinition(cstType)) {\n if (expression.trim().length === 0) {\n const fallbackAlias = alias && alias.length > 0 ? alias : 'X0';\n const type = cstType === CstType.BASE ? bool(basic(fallbackAlias)) : bool(constant(fallbackAlias));\n return {\n success: true,\n type,\n valueClass: ValueClass.VALUE,\n errors: [],\n ast: null\n };\n }\n return {\n success: false,\n type: null,\n valueClass: null,\n errors: [\n {\n code: RSErrorCode.definitionNotAllowed,\n from: 0,\n to: Math.max(expression.length, 1),\n params: [cstType, alias ?? '']\n }\n ],\n ast: null\n };\n }\n return schema.analyzer.checkFull(expression, {\n expected: typeClassForCstType(cstType),\n isDomain: cstType === CstType.STRUCTURED\n });\n}\n\n/** Calculate statistics for {@link RSForm}. */\nexport function calculateSchemaStats(target: RSForm): RSFormStats {\n const items = target.items;\n return {\n count_all: items.length,\n count_crucial: items.reduce((sum, cst) => sum + (cst.crucial ? 1 : 0), 0),\n\n step_complexity: items.reduce((sum, cst) => sum + calculateStepComplexity(cst), 0),\n\n count_problematic: items.reduce((sum, cst) => sum + (isSchemaIssue(cst) ? 1 : 0), 0),\n count_homonyms: items.reduce((sum, cst) => sum + (cst.homonyms.length > 0 ? 1 : 0), 0),\n count_formal_duplicates: items.reduce((sum, cst) => sum + (cst.formalDuplicates.length > 0 ? 1 : 0), 0),\n count_missing_convention: items.reduce((sum, cst) => sum + (isMissingConvention(cst) ? 1 : 0), 0),\n count_type_mismatch: items.reduce((sum, cst) => sum + (cst.is_type_mismatch ? 1 : 0), 0),\n\n count_incorrect: items.reduce((sum, cst) => sum + (cst.status === CstStatus.INCORRECT ? 1 : 0), 0),\n count_property: items.reduce((sum, cst) => sum + (cst.analysis?.valueClass === ValueClass.PROPERTY ? 1 : 0), 0),\n count_incalculable: items.reduce(\n (sum, cst) => sum + (cst.analysis?.success && cst.analysis.valueClass === null ? 1 : 0),\n 0\n ),\n count_inherited: items.reduce((sum, cst) => sum + (cst.is_inherited ? 1 : 0), 0),\n\n count_text_term: items.reduce((sum, cst) => sum + (cst.term_raw ? 1 : 0), 0),\n count_definition: items.reduce((sum, cst) => sum + (cst.definition_raw ? 1 : 0), 0),\n count_convention: items.reduce((sum, cst) => sum + (isBasicConcept(cst.cst_type) && cst.convention ? 1 : 0), 0),\n count_comment: items.reduce((sum, cst) => sum + (!isBasicConcept(cst.cst_type) && !cst.convention ? 1 : 0), 0),\n\n count_base: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.BASE ? 1 : 0), 0),\n count_constant: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.CONSTANT ? 1 : 0), 0),\n count_structured: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STRUCTURED ? 1 : 0), 0),\n count_axiom: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.AXIOM ? 1 : 0), 0),\n count_term: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.TERM ? 1 : 0), 0),\n count_function: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.FUNCTION ? 1 : 0), 0),\n count_predicate: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.PREDICATE ? 1 : 0), 0),\n count_statement: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.STATEMENT ? 1 : 0), 0),\n count_nominal: items.reduce((sum, cst) => sum + (cst.cst_type === CstType.NOMINAL ? 1 : 0), 0)\n };\n}\n\n/** Finds {@link Constituenta} by structure path. */\nexport function findCstByStructure(schema: RSForm, target: Constituenta, path: TypePath): Constituenta | null {\n for (const cst of schema.items) {\n if (cst.spawner === target.id && cst.spawner_path) {\n if (cst.spawner_path.length === path.length && cst.spawner_path.every((v, i) => v === path[i])) {\n return cst;\n }\n }\n }\n return null;\n}\n\n/** Retrieves name for piece of target {@link Constituenta} structure. */\nexport function getStructureName(schema: RSForm, target: Constituenta, path: TypePath): string {\n const representation = findCstByStructure(schema, target, path);\n if (representation) {\n return `${representation.alias}: ${representation.term_resolved}`;\n }\n if (!isTypification(target.effectiveType)) {\n return '';\n }\n const type = applyPath(target.effectiveType as Typification, path);\n if (type?.typeID === TypeID.basic) {\n const cst = schema.cstByAlias.get(type.baseID);\n if (cst?.term_resolved) {\n return `${cst.alias}: ${cst.term_resolved}`;\n }\n }\n return '';\n}\n\n// ========= Internals =====\nfunction calculateStepComplexity(cst: Constituenta): number {\n if (cst.cst_type === CstType.AXIOM || cst.cst_type === CstType.NOMINAL || !isBasicConcept(cst.cst_type)) {\n return 0;\n }\n if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT || !cst.effectiveType) {\n return 1;\n }\n\n const type = cst.effectiveType as Typification;\n return calculateTypificationComplexity(type) + 1;\n}\n\nfunction calculateTypificationComplexity(type: Typification): number {\n switch (type.typeID) {\n case TypeID.basic:\n case TypeID.integer:\n case TypeID.anyTypification:\n return 0;\n case TypeID.tuple:\n return (\n type.factors.length + type.factors.reduce((sum, factor) => sum + calculateTypificationComplexity(factor), 0)\n );\n case TypeID.collection:\n if (type.base.typeID === TypeID.tuple) {\n let sum = 0;\n type.base.factors.forEach(factor => {\n sum += calculateTypificationComplexity(factor);\n });\n return sum + type.base.factors.length;\n } else if (type.base.typeID === TypeID.collection) {\n return calculateTypificationComplexity(type.base) + 1;\n }\n return 0;\n }\n}\n\nfunction isMissingConvention(cst: Constituenta): boolean {\n if (isBasicConcept(cst.cst_type) && !isLogical(cst.cst_type)) {\n if (!cst.convention || !cst.term_resolved) {\n return true;\n }\n }\n return false;\n}\n\n/** Evaluate if {@link Typification} can be used to produce structure. */\nfunction typeCanProduceStructure(type: Typification): boolean {\n if (type.typeID === TypeID.basic || type.typeID === TypeID.integer || type.typeID === TypeID.anyTypification) {\n return false;\n } else if (type.typeID === TypeID.tuple) {\n return true;\n } else {\n return (\n type.base.typeID !== TypeID.basic &&\n type.base.typeID !== TypeID.integer &&\n type.base.typeID !== TypeID.anyTypification\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAqCA,MAAM,kBAA2C;EAC9C,QAAQ,UAAU;EAClB,QAAQ,OAAO;EACf,QAAQ,WAAW;EACnB,QAAQ,aAAa;EACrB,QAAQ,QAAQ;EAChB,QAAQ,OAAO;EACf,QAAQ,WAAW;EACnB,QAAQ,YAAY;EACpB,QAAQ,YAAY;AACvB;;AAGA,MAAM,oBAA+C;EAClD,QAAQ,UAAU,SAAS;EAC3B,QAAQ,OAAO,SAAS;EACxB,QAAQ,WAAW,SAAS;EAC5B,QAAQ,aAAa,SAAS;EAC9B,QAAQ,OAAO,SAAS;EACxB,QAAQ,WAAW,SAAS;EAC5B,QAAQ,QAAQ,SAAS;EACzB,QAAQ,YAAY,SAAS;EAC7B,QAAQ,YAAY,SAAS;AAChC;;AAGA,SAAgB,cAAc,KAA4B;CACxD,IAC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AAO;EACZ,KAAK,OAAO,iBACV,OAAO;EACT,KAAK,OAAO,OACV,OACE,KAAK,QAAQ,SAAS,KAAK,QAAQ,QAAQ,KAAK,WAAW,MAAM,gCAAgC,MAAM,GAAG,CAAC;EAE/G,KAAK,OAAO;GACV,IAAI,KAAK,KAAK,WAAW,OAAO,OAAO;IACrC,IAAI,MAAM;IACV,KAAK,KAAK,QAAQ,SAAQ,WAAU;KAClC,OAAO,gCAAgC,MAAM;IAC/C,CAAC;IACD,OAAO,MAAM,KAAK,KAAK,QAAQ;GACjC,OAAO,IAAI,KAAK,KAAK,WAAW,OAAO,YACrC,OAAO,gCAAgC,KAAK,IAAI,IAAI;GAEtD,OAAO;CACX;AACF;AAEA,SAAS,oBAAoB,KAA4B;CACvD,IAAI,eAAe,IAAI,QAAQ,KAAK,CAAC,UAAU,IAAI,QAAQ,GACzD;MAAI,CAAC,IAAI,cAAc,CAAC,IAAI,eAC1B,OAAO;CACT;CAEF,OAAO;AACT;;AAGA,SAAS,wBAAwB,MAA6B;CAC5D,IAAI,KAAK,WAAW,OAAO,SAAS,KAAK,WAAW,OAAO,WAAW,KAAK,WAAW,OAAO,iBAC3F,OAAO;MACF,IAAI,KAAK,WAAW,OAAO,OAChC,OAAO;MAEP,OACE,KAAK,KAAK,WAAW,OAAO,SAC5B,KAAK,KAAK,WAAW,OAAO,WAC5B,KAAK,KAAK,WAAW,OAAO;AAGlC"}
|
package/dist/library/rsform.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as Graph } from "../graph-DR8rL2o3.js";
|
|
2
2
|
import { i as LibraryItemReference, n as CurrentVersion, r as LibraryItem, s as VersionInfo } from "../library-CYun28Xz.js";
|
|
3
3
|
import { f as ExpressionType, y as TypePath } from "../typification-DDuv88ir.js";
|
|
4
|
-
import { a as RSLangAnalyzer, t as AnalysisBase } from "../analyzer-
|
|
4
|
+
import { a as RSLangAnalyzer, t as AnalysisBase } from "../analyzer-DpE0qknt.js";
|
|
5
5
|
//#region src/library/rsform.d.ts
|
|
6
6
|
/** Represents {@link Constituenta} type. */
|
|
7
7
|
declare const CstType: {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { TypeID, bool, isTypification, makeTypePath } from "../rslang/semantic/typification.js";
|
|
2
2
|
import { labelType } from "../rslang/labels.js";
|
|
3
|
+
import { splitTemplateDefinition } from "../rslang/api.js";
|
|
3
4
|
import "../rslang/index.js";
|
|
4
5
|
import { CstType } from "./rsform.js";
|
|
5
|
-
import { splitTemplateDefinition } from "../rslang/api.js";
|
|
6
6
|
import { findCstByStructure } from "./rsform-api.js";
|
|
7
7
|
//#region src/library/structure-planner.ts
|
|
8
8
|
/** Structure planner for {@link Constituenta}. */
|
package/dist/rslang/api.d.ts
CHANGED
|
@@ -5,6 +5,15 @@ import { t as generateExpressionFromAst } from "../expression-generator-CoucLIkE
|
|
|
5
5
|
//#region src/rslang/api.d.ts
|
|
6
6
|
/** Represents alias mapping. */
|
|
7
7
|
type AliasMapping = Record<string, string>;
|
|
8
|
+
/** Whether {@link value} is a single schema global alias (e.g. `X1`, `F42`). */
|
|
9
|
+
declare function isGlobalAlias(value: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Parse a RSLang expression into a normalized AST.
|
|
12
|
+
*
|
|
13
|
+
* Returns `null` for blank input. Parses the original `expression` string so node
|
|
14
|
+
* offsets match the source text (required for slice-based AST walks).
|
|
15
|
+
*/
|
|
16
|
+
declare function parseRSLangExpression(expression: string): AstNode | null;
|
|
8
17
|
/** Extract arguments from AST. */
|
|
9
18
|
declare function extractArguments(ast: AstNode): ArgumentsType[];
|
|
10
19
|
/** Extracts global variable names from a given expression. */
|
|
@@ -39,10 +48,12 @@ declare function splitTemplateDefinition(target: string): {
|
|
|
39
48
|
* from the provided arguments.
|
|
40
49
|
*/
|
|
41
50
|
declare function substituteTemplateArgs(expression: string, mapping: AliasMapping): string;
|
|
51
|
+
/** Substitute local parameter aliases in an expression fragment. */
|
|
52
|
+
declare function substituteLocalAliases(expression: string, mapping: AliasMapping): string;
|
|
42
53
|
/** Apply alias mapping. */
|
|
43
54
|
declare function applyAliasMapping(target: string, mapping: AliasMapping): string;
|
|
44
55
|
/** Apply alias typification mapping. */
|
|
45
56
|
declare function applyTypificationMapping(target: string, mapping: AliasMapping): string;
|
|
46
57
|
//#endregion
|
|
47
|
-
export { AliasMapping, applyAliasMapping, applyTypificationMapping, extractArguments, extractGlobals, generateExpressionFromAst, isSetTypification, isSimpleExpression, splitTemplateDefinition, substituteTemplateArgs };
|
|
58
|
+
export { AliasMapping, applyAliasMapping, applyTypificationMapping, extractArguments, extractGlobals, generateExpressionFromAst, isGlobalAlias, isSetTypification, isSimpleExpression, parseRSLangExpression, splitTemplateDefinition, substituteLocalAliases, substituteTemplateArgs };
|
|
48
59
|
//# sourceMappingURL=api.d.ts.map
|
package/dist/rslang/api.js
CHANGED
|
@@ -1,10 +1,34 @@
|
|
|
1
|
+
import { buildTree } from "../parsing/ast.js";
|
|
2
|
+
import "../parsing/index.js";
|
|
3
|
+
import { parser } from "./parser/parser.js";
|
|
4
|
+
import { normalizeAST } from "./parser/normalize.js";
|
|
1
5
|
import { ArgumentsExtractor } from "./semantic/arguments-extractor.js";
|
|
2
6
|
import { generateExpressionFromAst } from "./parser/expression-generator.js";
|
|
3
7
|
//#region src/rslang/api.ts
|
|
8
|
+
/**
|
|
9
|
+
* Module: API for RSLanguage.
|
|
10
|
+
*/
|
|
4
11
|
const LOCALS_REGEXP = /[_a-zα-ω][a-zα-ω]*\d*/g;
|
|
5
|
-
const
|
|
12
|
+
const GLOBAL_ALIAS_SOURCE = "[XCSADFPTN]\\d+";
|
|
13
|
+
const GLOBALS_REGEXP = new RegExp(GLOBAL_ALIAS_SOURCE, "g");
|
|
6
14
|
const COMPLEX_SYMBOLS_REGEXP = /[∀∃×ℬ;|:]/g;
|
|
7
15
|
const TYPIFICATION_SET = /^ℬ+\([ℬ\(((X|C)\d+|Z)\)×]*\)$/g;
|
|
16
|
+
/** Whether {@link value} is a single schema global alias (e.g. `X1`, `F42`). */
|
|
17
|
+
function isGlobalAlias(value) {
|
|
18
|
+
return new RegExp(`^${GLOBAL_ALIAS_SOURCE}$`).test(value);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parse a RSLang expression into a normalized AST.
|
|
22
|
+
*
|
|
23
|
+
* Returns `null` for blank input. Parses the original `expression` string so node
|
|
24
|
+
* offsets match the source text (required for slice-based AST walks).
|
|
25
|
+
*/
|
|
26
|
+
function parseRSLangExpression(expression) {
|
|
27
|
+
if (!expression.trim()) return null;
|
|
28
|
+
const ast = buildTree(parser.parse(expression).cursor());
|
|
29
|
+
normalizeAST(ast, expression);
|
|
30
|
+
return ast;
|
|
31
|
+
}
|
|
8
32
|
/** Extract arguments from AST. */
|
|
9
33
|
function extractArguments(ast) {
|
|
10
34
|
return new ArgumentsExtractor().run(ast);
|
|
@@ -66,6 +90,10 @@ function substituteTemplateArgs(expression, mapping) {
|
|
|
66
90
|
if (!head) return body;
|
|
67
91
|
else return `[${head}] ${body}`;
|
|
68
92
|
}
|
|
93
|
+
/** Substitute local parameter aliases in an expression fragment. */
|
|
94
|
+
function substituteLocalAliases(expression, mapping) {
|
|
95
|
+
return applyPattern(expression, mapping, LOCALS_REGEXP);
|
|
96
|
+
}
|
|
69
97
|
/** Apply alias mapping. */
|
|
70
98
|
function applyAliasMapping(target, mapping) {
|
|
71
99
|
return applyPattern(target, mapping, GLOBALS_REGEXP);
|
|
@@ -128,6 +156,6 @@ function applyPattern(text, mapping, pattern) {
|
|
|
128
156
|
return output;
|
|
129
157
|
}
|
|
130
158
|
//#endregion
|
|
131
|
-
export { applyAliasMapping, applyTypificationMapping, extractArguments, extractGlobals, generateExpressionFromAst, isSetTypification, isSimpleExpression, splitTemplateDefinition, substituteTemplateArgs };
|
|
159
|
+
export { applyAliasMapping, applyTypificationMapping, extractArguments, extractGlobals, generateExpressionFromAst, isGlobalAlias, isSetTypification, isSimpleExpression, parseRSLangExpression, splitTemplateDefinition, substituteLocalAliases, substituteTemplateArgs };
|
|
132
160
|
|
|
133
161
|
//# sourceMappingURL=api.js.map
|
package/dist/rslang/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","names":[],"sources":["../../src/rslang/api.ts"],"sourcesContent":["/**\n * Module: API for RSLanguage.\n */\nimport { type AstNode } from '../parsing';\n\nimport { ArgumentsExtractor, type ArgumentsType } from './semantic/arguments-extractor';\nexport { generateExpressionFromAst } from './parser/expression-generator';\n\n/** Represents alias mapping. */\nexport type AliasMapping = Record<string, string>;\n\n// cspell:disable\nconst LOCALS_REGEXP = /[_a-zα-ω][a-zα-ω]*\\d*/g;\nconst
|
|
1
|
+
{"version":3,"file":"api.js","names":["rslangParser"],"sources":["../../src/rslang/api.ts"],"sourcesContent":["/**\n * Module: API for RSLanguage.\n */\nimport { type AstNode, buildTree } from '../parsing';\n\nimport { normalizeAST } from './parser/normalize';\nimport { parser as rslangParser } from './parser/parser';\nimport { ArgumentsExtractor, type ArgumentsType } from './semantic/arguments-extractor';\nexport { generateExpressionFromAst } from './parser/expression-generator';\n\n/** Represents alias mapping. */\nexport type AliasMapping = Record<string, string>;\n\n// cspell:disable\nconst LOCALS_REGEXP = /[_a-zα-ω][a-zα-ω]*\\d*/g;\nconst GLOBAL_ALIAS_SOURCE = '[XCSADFPTN]\\\\d+';\nconst GLOBALS_REGEXP = new RegExp(GLOBAL_ALIAS_SOURCE, 'g');\nconst COMPLEX_SYMBOLS_REGEXP = /[∀∃×ℬ;|:]/g;\nconst TYPIFICATION_SET = /^ℬ+\\([ℬ\\(((X|C)\\d+|Z)\\)×]*\\)$/g;\n// cspell:enable\n\n/** Whether {@link value} is a single schema global alias (e.g. `X1`, `F42`). */\nexport function isGlobalAlias(value: string): boolean {\n return new RegExp(`^${GLOBAL_ALIAS_SOURCE}$`).test(value);\n}\n\n/**\n * Parse a RSLang expression into a normalized AST.\n *\n * Returns `null` for blank input. Parses the original `expression` string so node\n * offsets match the source text (required for slice-based AST walks).\n */\nexport function parseRSLangExpression(expression: string): AstNode | null {\n if (!expression.trim()) {\n return null;\n }\n const tree = rslangParser.parse(expression);\n const ast = buildTree(tree.cursor());\n normalizeAST(ast, expression);\n return ast;\n}\n\n/** Extract arguments from AST. */\nexport function extractArguments(ast: AstNode): ArgumentsType[] {\n return new ArgumentsExtractor().run(ast);\n}\n\n/** Extracts global variable names from a given expression. */\nexport function extractGlobals(expression: string): Set<string> {\n return new Set(expression.match(GLOBALS_REGEXP) ?? []);\n}\n\n/** Check if expression is simple derivation. */\nexport function isSimpleExpression(text: string): boolean {\n return !text.match(COMPLEX_SYMBOLS_REGEXP);\n}\n\n/** Check if expression is set typification. */\nexport function isSetTypification(text: string): boolean {\n return !!text.match(TYPIFICATION_SET);\n}\n\n/**\n * Splits a string containing a template definition into its head and body parts.\n *\n * A template definition is expected to have the following format: `[head] body`.\n * If the input string does not contain the opening square bracket '[', the entire\n * string is treated as the body, and an empty string is assigned to the head.\n * If the opening bracket is present, the function attempts to find the matching\n * closing bracket ']' to determine the head and body parts.\n *\n * @example\n * const template = '[header] body content';\n * const result = splitTemplateDefinition(template);\n * // result: `{ head: 'header', body: 'body content' }`\n */\nexport function splitTemplateDefinition(target: string) {\n let start = 0;\n for (; start < target.length && target[start] !== '['; ++start);\n if (start < target.length) {\n for (let counter = 0, end = start + 1; end < target.length; ++end) {\n if (target[end] === '[') {\n ++counter;\n } else if (target[end] === ']') {\n if (counter !== 0) {\n --counter;\n } else {\n return {\n head: target.substring(start + 1, end).trim(),\n body: target.substring(end + 1).trim()\n };\n }\n }\n }\n }\n return {\n head: '',\n body: target\n };\n}\n\n/**\n * Substitutes values for template arguments in a given expression.\n *\n * This function takes an input mathematical expression and a list of argument values.\n * It replaces template argument placeholders in the expression with their corresponding values\n * from the provided arguments.\n */\nexport function substituteTemplateArgs(expression: string, mapping: AliasMapping): string {\n if (Object.keys(mapping).length === 0) {\n return expression;\n }\n let { head, body } = splitTemplateDefinition(expression);\n body = applyPattern(body, mapping, LOCALS_REGEXP);\n const argTexts = head.split(',').map(text => text.trim());\n head = argTexts\n .filter(arg => [...arg.matchAll(LOCALS_REGEXP)].every(local => local.every(match => !(match in mapping))))\n .join(', ');\n\n if (!head) {\n return body;\n } else {\n return `[${head}] ${body}`;\n }\n}\n\n/** Substitute local parameter aliases in an expression fragment. */\nexport function substituteLocalAliases(expression: string, mapping: AliasMapping): string {\n return applyPattern(expression, mapping, LOCALS_REGEXP);\n}\n\n/** Apply alias mapping. */\nexport function applyAliasMapping(target: string, mapping: AliasMapping): string {\n return applyPattern(target, mapping, GLOBALS_REGEXP);\n}\n\n/** Apply alias typification mapping. */\nexport function applyTypificationMapping(target: string, mapping: AliasMapping): string {\n const modified = applyAliasMapping(target, mapping);\n if (modified === target) {\n return target;\n }\n\n const deleteBrackets: number[] = [];\n const positions: number[] = [];\n const booleans: number[] = [];\n let boolCount: number = 0;\n let stackSize: number = 0;\n\n for (let i = 0; i < modified.length; i++) {\n const char = modified[i];\n if (char === 'ℬ') {\n boolCount++;\n continue;\n }\n if (char === '(') {\n stackSize++;\n positions.push(i);\n booleans.push(boolCount);\n }\n boolCount = 0;\n if (char === ')') {\n if (\n i < modified.length - 1 &&\n modified[i + 1] === ')' &&\n stackSize > 1 &&\n positions[stackSize - 2] + booleans[stackSize - 1] + 1 === positions[stackSize - 1]\n ) {\n deleteBrackets.push(i);\n deleteBrackets.push(positions[stackSize - 2]);\n }\n if (i === modified.length - 1 && stackSize === 1 && positions[0] === 0) {\n deleteBrackets.push(i);\n deleteBrackets.push(positions[0]);\n }\n stackSize--;\n positions.pop();\n booleans.pop();\n }\n }\n\n let result = '';\n for (let i = 0; i < modified.length; i++) {\n if (!deleteBrackets.includes(i)) {\n result += modified[i];\n }\n }\n\n return result;\n}\n\n// ====== Internals =========\n/** Text substitution guided by mapping and regular expression. */\nfunction applyPattern(text: string, mapping: AliasMapping, pattern: RegExp): string {\n if (text === '' || pattern === null) {\n return text;\n }\n let posInput = 0;\n let output = '';\n const patternMatches = text.matchAll(pattern);\n for (const segment of patternMatches) {\n const entity = segment[0];\n const start = segment.index ?? 0;\n if (entity in mapping) {\n output += text.substring(posInput, start);\n output += mapping[entity];\n posInput = start + segment[0].length;\n }\n }\n output += text.substring(posInput);\n return output;\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAM,gBAAgB;AACtB,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB,IAAI,OAAO,qBAAqB,GAAG;AAC1D,MAAM,yBAAyB;AAC/B,MAAM,mBAAmB;;AAIzB,SAAgB,cAAc,OAAwB;CACpD,OAAO,IAAI,OAAO,IAAI,oBAAoB,EAAE,CAAC,CAAC,KAAK,KAAK;AAC1D;;;;;;;AAQA,SAAgB,sBAAsB,YAAoC;CACxE,IAAI,CAAC,WAAW,KAAK,GACnB,OAAO;CAGT,MAAM,MAAM,UADCA,OAAa,MAAM,UACV,CAAA,CAAK,OAAO,CAAC;CACnC,aAAa,KAAK,UAAU;CAC5B,OAAO;AACT;;AAGA,SAAgB,iBAAiB,KAA+B;CAC9D,OAAO,IAAI,mBAAmB,CAAC,CAAC,IAAI,GAAG;AACzC;;AAGA,SAAgB,eAAe,YAAiC;CAC9D,OAAO,IAAI,IAAI,WAAW,MAAM,cAAc,KAAK,CAAC,CAAC;AACvD;;AAGA,SAAgB,mBAAmB,MAAuB;CACxD,OAAO,CAAC,KAAK,MAAM,sBAAsB;AAC3C;;AAGA,SAAgB,kBAAkB,MAAuB;CACvD,OAAO,CAAC,CAAC,KAAK,MAAM,gBAAgB;AACtC;;;;;;;;;;;;;;;AAgBA,SAAgB,wBAAwB,QAAgB;CACtD,IAAI,QAAQ;CACZ,OAAO,QAAQ,OAAO,UAAU,OAAO,WAAW,KAAK,EAAE;CACzD,IAAI,QAAQ,OAAO,QACjB;OAAK,IAAI,UAAU,GAAG,MAAM,QAAQ,GAAG,MAAM,OAAO,QAAQ,EAAE,KAC5D,IAAI,OAAO,SAAS,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EACA,IAAI,SAAS,KAAK;GAChB;GACA,UAAU,KAAK,CAAC;GAChB,SAAS,KAAK,SAAS;EACzB;EACA,YAAY;EACZ,IAAI,SAAS,KAAK;GAChB,IACE,IAAI,SAAS,SAAS,KACtB,SAAS,IAAI,OAAO,OACpB,YAAY,KACZ,UAAU,YAAY,KAAK,SAAS,YAAY,KAAK,MAAM,UAAU,YAAY,IACjF;IACA,eAAe,KAAK,CAAC;IACrB,eAAe,KAAK,UAAU,YAAY,EAAE;GAC9C;GACA,IAAI,MAAM,SAAS,SAAS,KAAK,cAAc,KAAK,UAAU,OAAO,GAAG;IACtE,eAAe,KAAK,CAAC;IACrB,eAAe,KAAK,UAAU,EAAE;GAClC;GACA;GACA,UAAU,IAAI;GACd,SAAS,IAAI;EACf;CACF;CAEA,IAAI,SAAS;CACb,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KACnC,IAAI,CAAC,eAAe,SAAS,CAAC,GAC5B,UAAU,SAAS;CAIvB,OAAO;AACT;;AAIA,SAAS,aAAa,MAAc,SAAuB,SAAyB;CAClF,IAAI,SAAS,MAAM,YAAY,MAC7B,OAAO;CAET,IAAI,WAAW;CACf,IAAI,SAAS;CACb,MAAM,iBAAiB,KAAK,SAAS,OAAO;CAC5C,KAAK,MAAM,WAAW,gBAAgB;EACpC,MAAM,SAAS,QAAQ;EACvB,MAAM,QAAQ,QAAQ,SAAS;EAC/B,IAAI,UAAU,SAAS;GACrB,UAAU,KAAK,UAAU,UAAU,KAAK;GACxC,UAAU,QAAQ;GAClB,WAAW,QAAQ,QAAQ,EAAE,CAAC;EAChC;CACF;CACA,UAAU,KAAK,UAAU,QAAQ;CACjC,OAAO;AACT"}
|
package/dist/rslang/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { d as printValue } from "../value-api-C2X4Pom5.js";
|
|
|
7
7
|
import { t as parser } from "../parser-Bwd8LxJ1.js";
|
|
8
8
|
import { t as TokenID } from "../token-DeXAmzwr.js";
|
|
9
9
|
import { t as ValueClass } from "../value-class-CNI-lqXJ.js";
|
|
10
|
-
import { a as RSLangAnalyzer, n as AnalysisFast, r as AnalysisFull, t as AnalysisBase } from "../analyzer-
|
|
10
|
+
import { a as RSLangAnalyzer, n as AnalysisFast, r as AnalysisFull, t as AnalysisBase } from "../analyzer-DpE0qknt.js";
|
|
11
11
|
import { n as ArgumentsType } from "../arguments-extractor-Dd1lFom9.js";
|
|
12
12
|
import { n as applyAsciiTypeSubstitutions, r as parseTypeText } from "../typification-parser-DHO42ZRc.js";
|
|
13
13
|
import { n as TypificationNodeData, t as TypificationGraph } from "../typification-graph-8VdK6_Fh.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as RSLangAnalyzer, i as AnalysisOptions, n as AnalysisFast, r as AnalysisFull, t as AnalysisBase } from "../../analyzer-
|
|
1
|
+
import { a as RSLangAnalyzer, i as AnalysisOptions, n as AnalysisFast, r as AnalysisFull, t as AnalysisBase } from "../../analyzer-DpE0qknt.js";
|
|
2
2
|
export { AnalysisBase, AnalysisFast, AnalysisFull, AnalysisOptions, RSLangAnalyzer };
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { annotateError } from "../ast-annotations.js";
|
|
2
2
|
import { RSErrorCode } from "../error.js";
|
|
3
|
-
import { buildTree } from "../../parsing/ast.js";
|
|
4
|
-
import "../../parsing/index.js";
|
|
5
3
|
import { TokenID } from "../parser/token.js";
|
|
6
4
|
import { TypeID, basic, bool, constant, debool } from "./typification.js";
|
|
7
5
|
import { labelType } from "../labels.js";
|
|
8
|
-
import {
|
|
9
|
-
import { normalizeAST } from "../parser/normalize.js";
|
|
6
|
+
import { parseRSLangExpression } from "../api.js";
|
|
10
7
|
import { t as extractSyntaxErrors } from "../../syntax-errors-B6c2vsan.js";
|
|
11
8
|
import { getTypeClass } from "./typification-api.js";
|
|
12
9
|
import { TypeAuditor } from "./type-auditor.js";
|
|
@@ -35,13 +32,13 @@ var RSLangAnalyzer = class {
|
|
|
35
32
|
return this.typeContext.get(alias) ?? null;
|
|
36
33
|
}
|
|
37
34
|
checkFast(expression, options) {
|
|
38
|
-
|
|
35
|
+
const ast = parseRSLangExpression(expression);
|
|
36
|
+
if (!ast) return {
|
|
39
37
|
success: false,
|
|
40
38
|
type: null,
|
|
41
39
|
valueClass: null,
|
|
42
40
|
ast: null
|
|
43
41
|
};
|
|
44
|
-
const ast = this.parse(expression);
|
|
45
42
|
if (ast.hasError) return {
|
|
46
43
|
success: false,
|
|
47
44
|
type: null,
|
|
@@ -87,7 +84,8 @@ var RSLangAnalyzer = class {
|
|
|
87
84
|
const reporter = (error) => {
|
|
88
85
|
errors.push(error);
|
|
89
86
|
};
|
|
90
|
-
|
|
87
|
+
const ast = parseRSLangExpression(expression);
|
|
88
|
+
if (!ast) {
|
|
91
89
|
reporter({
|
|
92
90
|
code: RSErrorCode.cstEmptyDerived,
|
|
93
91
|
from: 0,
|
|
@@ -101,7 +99,6 @@ var RSLangAnalyzer = class {
|
|
|
101
99
|
ast: null
|
|
102
100
|
};
|
|
103
101
|
}
|
|
104
|
-
const ast = this.parse(expression);
|
|
105
102
|
if (ast.hasError) {
|
|
106
103
|
extractSyntaxErrors(ast, expression, reporter, options?.annotateErrors ?? false, { expected: options?.expected });
|
|
107
104
|
return {
|
|
@@ -169,11 +166,6 @@ var RSLangAnalyzer = class {
|
|
|
169
166
|
ast
|
|
170
167
|
};
|
|
171
168
|
}
|
|
172
|
-
parse(expression) {
|
|
173
|
-
const ast = buildTree(parser.parse(expression).cursor());
|
|
174
|
-
normalizeAST(ast, expression);
|
|
175
|
-
return ast;
|
|
176
|
-
}
|
|
177
169
|
};
|
|
178
170
|
function isStructureDomain(node) {
|
|
179
171
|
switch (node.typeID) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyzer.js","names":[
|
|
1
|
+
{"version":3,"file":"analyzer.js","names":[],"sources":["../../../src/rslang/semantic/analyzer.ts"],"sourcesContent":["import { type AstNode } from '../../parsing';\nimport { parseRSLangExpression } from '../api';\nimport { annotateError } from '../ast-annotations';\nimport { RSErrorCode, type RSErrorDescription } from '../error';\nimport { labelType } from '../labels';\nimport { extractSyntaxErrors } from '../parser/syntax-errors';\nimport { TokenID } from '../parser/token';\n\nimport { TypeAuditor } from './type-auditor';\nimport {\n basic,\n bool,\n constant,\n debool,\n type ExpressionType,\n type TypeClass,\n type TypeContext,\n TypeID\n} from './typification';\nimport { getTypeClass } from './typification-api';\nimport { ValueAuditor } from './value-auditor';\nimport { ValueClass, type ValueClassContext } from './value-class';\n\nexport interface AnalysisBase {\n success: boolean;\n type: ExpressionType | null;\n valueClass: ValueClass | null;\n}\n\nexport interface AnalysisFast extends AnalysisBase {\n ast: AstNode | null;\n}\n\nexport interface AnalysisFull extends AnalysisBase {\n ast: AstNode | null;\n errors: RSErrorDescription[];\n}\n\nexport interface AnalysisOptions {\n expected?: TypeClass;\n isDomain?: boolean;\n annotateTypes?: boolean;\n annotateErrors?: boolean;\n}\n\nexport class RSLangAnalyzer {\n private typeContext: TypeContext = new Map<string, ExpressionType>();\n private valueContext: ValueClassContext = new Map<string, ValueClass>();\n private typeAuditor: TypeAuditor = new TypeAuditor(this.typeContext);\n private valueAuditor: ValueAuditor = new ValueAuditor(this.valueContext);\n\n public addBase(alias: string, isNumeric: boolean = false, valueClass: ValueClass = ValueClass.VALUE): void {\n if (isNumeric) {\n this.typeContext.set(alias, bool(constant(alias)));\n this.valueContext.set(alias, valueClass);\n } else {\n this.typeContext.set(alias, bool(basic(alias)));\n this.valueContext.set(alias, valueClass);\n }\n }\n\n public setGlobal(alias: string, type: ExpressionType | null, value: ValueClass | null): void {\n if (type) {\n this.typeContext.set(alias, type);\n }\n if (value) {\n this.valueContext.set(alias, value);\n }\n }\n\n public getType(alias: string): ExpressionType | null {\n return this.typeContext.get(alias) ?? null;\n }\n\n public checkFast(expression: string, options?: AnalysisOptions): AnalysisFast {\n const ast = parseRSLangExpression(expression);\n if (!ast) {\n return { success: false, type: null, valueClass: null, ast: null };\n }\n if (ast.hasError) {\n return { success: false, type: null, valueClass: null, ast: ast };\n }\n const type = this.typeAuditor.run(ast, options?.annotateTypes ?? false);\n if (type === null) {\n return { success: false, type: null, valueClass: null, ast: ast };\n }\n\n if (options?.isDomain) {\n if (!isStructureDomain(ast) || type.typeID !== TypeID.collection) {\n return { success: false, type: null, valueClass: null, ast: ast };\n }\n return { success: true, type: debool(type), valueClass: ValueClass.VALUE, ast: ast };\n }\n if (options?.expected && getTypeClass(type.typeID) !== options.expected) {\n return { success: false, type: null, valueClass: null, ast: ast };\n }\n\n return {\n success: true,\n type: type,\n valueClass: options?.isDomain ? ValueClass.VALUE : this.valueAuditor.run(ast),\n ast: ast\n };\n }\n\n public checkFull(expression: string, options?: AnalysisOptions): AnalysisFull {\n const errors: RSErrorDescription[] = [];\n const reporter = (error: RSErrorDescription) => {\n errors.push(error);\n };\n const ast = parseRSLangExpression(expression);\n if (!ast) {\n reporter({ code: RSErrorCode.cstEmptyDerived, from: 0, to: 0 });\n return { success: false, type: null, valueClass: null, errors: errors, ast: null };\n }\n if (ast.hasError) {\n extractSyntaxErrors(ast, expression, reporter, options?.annotateErrors ?? false, {\n expected: options?.expected\n });\n return { success: false, type: null, valueClass: null, errors, ast };\n }\n\n const type = this.typeAuditor.run(ast, options?.annotateTypes ?? false, reporter, options?.annotateErrors ?? false);\n if (type === null) {\n return { success: false, type: null, valueClass: null, errors, ast };\n }\n\n if (options?.isDomain) {\n if (!isStructureDomain(ast) || type.typeID !== TypeID.collection) {\n reporter({ code: RSErrorCode.globalStructure, from: ast.from, to: ast.to });\n if (options?.annotateErrors) {\n annotateError(ast, RSErrorCode.globalStructure);\n }\n return { success: false, type: null, valueClass: null, errors, ast };\n }\n return { success: true, type: debool(type), valueClass: ValueClass.VALUE, errors, ast };\n }\n if (options?.expected && getTypeClass(type.typeID) !== options.expected) {\n const errorParams = [String(options.expected), labelType(type)];\n reporter({\n code: RSErrorCode.expectedType,\n from: ast.from,\n to: ast.to,\n params: errorParams\n });\n if (options?.annotateErrors) {\n annotateError(ast, RSErrorCode.expectedType, errorParams);\n }\n return { success: false, type: null, valueClass: null, errors, ast };\n }\n const valueClass = options?.isDomain\n ? ValueClass.VALUE\n : this.valueAuditor.run(ast, reporter, options?.annotateErrors ?? false);\n return { success: true, type, valueClass, errors, ast };\n }\n}\n\n// ======= Internals ========\nfunction isStructureDomain(node: AstNode): boolean {\n switch (node.typeID) {\n case TokenID.LIT_WHOLE_NUMBERS:\n case TokenID.ID_GLOBAL:\n case TokenID.BOOLEAN:\n case TokenID.DECART:\n case TokenID.NT_ENUMERATION:\n break;\n default:\n return false;\n }\n for (const child of node.children) {\n if (!isStructureDomain(child)) {\n return false;\n }\n }\n return true;\n}\n"],"mappings":";;;;;;;;;;;;AA6CA,IAAa,iBAAb,MAA4B;CAC1B,8BAAmC,IAAI,IAA4B;CACnE,+BAA0C,IAAI,IAAwB;CACtE,cAAmC,IAAI,YAAY,KAAK,WAAW;CACnE,eAAqC,IAAI,aAAa,KAAK,YAAY;CAEvE,QAAe,OAAe,YAAqB,OAAO,aAAyB,WAAW,OAAa;EACzG,IAAI,WAAW;GACb,KAAK,YAAY,IAAI,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC;GACjD,KAAK,aAAa,IAAI,OAAO,UAAU;EACzC,OAAO;GACL,KAAK,YAAY,IAAI,OAAO,KAAK,MAAM,KAAK,CAAC,CAAC;GAC9C,KAAK,aAAa,IAAI,OAAO,UAAU;EACzC;CACF;CAEA,UAAiB,OAAe,MAA6B,OAAgC;EAC3F,IAAI,MACF,KAAK,YAAY,IAAI,OAAO,IAAI;EAElC,IAAI,OACF,KAAK,aAAa,IAAI,OAAO,KAAK;CAEtC;CAEA,QAAe,OAAsC;EACnD,OAAO,KAAK,YAAY,IAAI,KAAK,KAAK;CACxC;CAEA,UAAiB,YAAoB,SAAyC;EAC5E,MAAM,MAAM,sBAAsB,UAAU;EAC5C,IAAI,CAAC,KACH,OAAO;GAAE,SA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,kBAAkB,KAAK,GAC1B,OAAO;CAGX,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsconcept/domain",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Shared TypeScript domain for RS language, RSForm, RSModel, RSEngine, OSS, and concept-text helpers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "IRBorisov",
|
|
@@ -150,17 +150,6 @@
|
|
|
150
150
|
"README.md",
|
|
151
151
|
"LICENSE"
|
|
152
152
|
],
|
|
153
|
-
"scripts": {
|
|
154
|
-
"generate": "lezer-generator src/rslang/parser/rslang.grammar -o src/rslang/parser/parser.ts",
|
|
155
|
-
"build": "tsdown",
|
|
156
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
157
|
-
"test": "vitest run",
|
|
158
|
-
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
|
159
|
-
"lintFix": "eslint . --report-unused-disable-directives --max-warnings 0 --fix",
|
|
160
|
-
"format": "prettier --write \"src/**/*.ts\" \"*.{ts,js}\"",
|
|
161
|
-
"format:check": "prettier --check \"src/**/*.ts\" \"*.{ts,js}\"",
|
|
162
|
-
"prepublishOnly": "npm run build"
|
|
163
|
-
},
|
|
164
153
|
"dependencies": {
|
|
165
154
|
"@lezer/common": "^1.5.2",
|
|
166
155
|
"@lezer/lr": "^1.4.10"
|
|
@@ -176,9 +165,21 @@
|
|
|
176
165
|
"tsdown": "^0.22.3",
|
|
177
166
|
"typescript": "^6.0.3",
|
|
178
167
|
"typescript-eslint": "^8.62.1",
|
|
168
|
+
"unrun": "^0.3.1",
|
|
179
169
|
"vitest": "^4.1.9"
|
|
180
170
|
},
|
|
181
171
|
"publishConfig": {
|
|
182
172
|
"access": "public"
|
|
173
|
+
},
|
|
174
|
+
"scripts": {
|
|
175
|
+
"generate": "lezer-generator src/rslang/parser/rslang.grammar -o src/rslang/parser/parser.ts",
|
|
176
|
+
"build": "tsdown",
|
|
177
|
+
"dev": "tsdown --watch",
|
|
178
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
179
|
+
"test": "vitest run",
|
|
180
|
+
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
|
181
|
+
"lintFix": "eslint . --report-unused-disable-directives --max-warnings 0 --fix",
|
|
182
|
+
"format": "prettier --write \"src/**/*.ts\" \"*.{ts,js}\"",
|
|
183
|
+
"format:check": "prettier --check \"src/**/*.ts\" \"*.{ts,js}\""
|
|
183
184
|
}
|
|
184
|
-
}
|
|
185
|
+
}
|
package/src/library/index.ts
CHANGED
|
@@ -44,3 +44,10 @@ export {
|
|
|
44
44
|
type RSModel,
|
|
45
45
|
type RSModelStats
|
|
46
46
|
} from './rsmodel';
|
|
47
|
+
export {
|
|
48
|
+
planTemplateInstantiation,
|
|
49
|
+
type TemplateInstantiationInput,
|
|
50
|
+
type TemplateInstantiationItem,
|
|
51
|
+
type TemplateInstantiationPlan,
|
|
52
|
+
TemplateInstantiationPlanner
|
|
53
|
+
} from './template-instantiation';
|
|
@@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest';
|
|
|
3
3
|
import { RSErrorCode } from '../rslang/error';
|
|
4
4
|
|
|
5
5
|
import { CstType, type RSForm } from './rsform';
|
|
6
|
-
import { applyMappingToConstituents, getAnalysisFor, validateAliasFormat } from './rsform-api';
|
|
6
|
+
import { applyMappingToConstituents, getAnalysisFor, normalizeExpression, validateAliasFormat } from './rsform-api';
|
|
7
7
|
|
|
8
8
|
const emptySchema = {} as RSForm;
|
|
9
9
|
|
|
@@ -88,3 +88,13 @@ describe('applyMappingToConstituents', () => {
|
|
|
88
88
|
expect(items[0].definition_formal).toBe('X3');
|
|
89
89
|
});
|
|
90
90
|
});
|
|
91
|
+
|
|
92
|
+
describe('normalizeExpression', () => {
|
|
93
|
+
it('normalizes spacing in derived definitions', () => {
|
|
94
|
+
expect(normalizeExpression('1 = 2', CstType.TERM)).toBe('1=2');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('returns null for basic sets', () => {
|
|
98
|
+
expect(normalizeExpression('ℬ(X1)', CstType.STRUCTURED)).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { applyEntityReferenceMapping } from '../cctext/language-api';
|
|
6
|
+
import { type AstNode } from '../parsing';
|
|
6
7
|
import {
|
|
7
8
|
type AnalysisFull,
|
|
8
9
|
RSErrorCode,
|
|
@@ -12,7 +13,13 @@ import {
|
|
|
12
13
|
type Typification,
|
|
13
14
|
ValueClass
|
|
14
15
|
} from '../rslang';
|
|
15
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
type AliasMapping,
|
|
18
|
+
applyAliasMapping,
|
|
19
|
+
applyTypificationMapping,
|
|
20
|
+
generateExpressionFromAst,
|
|
21
|
+
parseRSLangExpression
|
|
22
|
+
} from '../rslang/api';
|
|
16
23
|
import { basic, bool, constant, type EchelonFunctional, isTypification } from '../rslang/semantic/typification';
|
|
17
24
|
import { applyPath } from '../rslang/semantic/typification-api';
|
|
18
25
|
|
|
@@ -97,6 +104,38 @@ export function inferTemplatedType(templateType: CstType, args: ArgumentValue[])
|
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
106
|
|
|
107
|
+
/** Build alias mapping from template argument bindings. */
|
|
108
|
+
export function argumentValuesToMapping(args: ArgumentValue[]): AliasMapping {
|
|
109
|
+
const mapping: AliasMapping = {};
|
|
110
|
+
for (const arg of args) {
|
|
111
|
+
if (arg.value) {
|
|
112
|
+
mapping[arg.alias] = arg.value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return mapping;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Normalize a RSLang expression for duplicate comparison.
|
|
120
|
+
*
|
|
121
|
+
* Returns `null` for basic concepts without formal definitions. Pass a pre-parsed `ast`
|
|
122
|
+
* when the expression was already analyzed to avoid re-parsing.
|
|
123
|
+
*/
|
|
124
|
+
export function normalizeExpression(expression: string, cst_type: CstType, ast?: AstNode | null): string | null {
|
|
125
|
+
if (isBasicConcept(cst_type) && cst_type !== CstType.AXIOM) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const parsed = ast ?? parseRSLangExpression(expression);
|
|
129
|
+
let normalized = '';
|
|
130
|
+
if (parsed && !parsed.hasError) {
|
|
131
|
+
normalized = generateExpressionFromAst(parsed, { normalize: true });
|
|
132
|
+
} else {
|
|
133
|
+
normalized = expression;
|
|
134
|
+
}
|
|
135
|
+
normalized = normalized.replace(/\s+/g, '');
|
|
136
|
+
return normalized === '' ? null : normalized;
|
|
137
|
+
}
|
|
138
|
+
|
|
100
139
|
/** Checks if given expression is a template. */
|
|
101
140
|
export function inferTemplate(expression: string): boolean {
|
|
102
141
|
const match = expression.match(/R\d+/g);
|