ConceptPortal-public/rsconcept/frontend/src/hooks/useCheckExpression.ts

85 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-20 17:11:03 +03:00
import { useCallback, useState } from 'react'
2023-07-25 20:27:29 +03:00
import { type ErrorInfo } from '../components/BackendError';
import { DataCallback, postCheckExpression } from '../utils/backendAPI';
import { RSErrorType } from '../utils/enums';
import { CstType, IConstituenta, IExpressionParse, IFunctionArg, type IRSForm } from '../utils/models';
import { getCstExpressionPrefix } from '../utils/staticUI';
2023-08-26 21:37:57 +03:00
const LOGIC_TYPIIFCATION = 'LOGIC';
function checkTypeConsistency(type: CstType, typification: string, args: IFunctionArg[]): boolean {
switch (type) {
case CstType.BASE:
case CstType.CONSTANT:
case CstType.STRUCTURED:
case CstType.TERM:
2023-08-26 21:37:57 +03:00
return typification !== LOGIC_TYPIIFCATION && args.length === 0;
case CstType.AXIOM:
case CstType.THEOREM:
2023-08-26 21:37:57 +03:00
return typification === LOGIC_TYPIIFCATION && args.length === 0;
case CstType.FUNCTION:
2023-08-26 21:37:57 +03:00
return typification !== LOGIC_TYPIIFCATION && args.length !== 0;
case CstType.PREDICATE:
2023-08-26 21:37:57 +03:00
return typification === LOGIC_TYPIIFCATION && args.length !== 0;
}
}
2023-07-20 17:11:03 +03:00
2023-08-27 15:39:49 +03:00
function adjustResults(parse: IExpressionParse, emptyExpression: boolean, cstType: CstType) {
if (!parse.parseResult && parse.errors.length > 0 && parse.errors[0].errorType !== RSErrorType.syntax) {
return;
}
if (cstType === CstType.BASE || cstType === CstType.CONSTANT) {
if (!emptyExpression) {
parse.parseResult = false;
parse.errors.push({
errorType: RSErrorType.globalNonemptyBase,
isCritical: true,
params: [],
position: 0
});
}
}
if (!checkTypeConsistency(cstType, parse.typification, parse.args)) {
parse.parseResult = false;
parse.errors.push({
errorType: RSErrorType.globalUnexpectedType,
isCritical: true,
params: [],
position: 0
});
}
}
2023-07-25 20:27:29 +03:00
function useCheckExpression({ schema }: { schema?: IRSForm }) {
2023-07-20 17:11:03 +03:00
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorInfo>(undefined);
2023-07-29 03:31:21 +03:00
const [parseData, setParseData] = useState<IExpressionParse | undefined>(undefined);
2023-07-20 17:11:03 +03:00
2023-08-27 16:35:17 +03:00
const resetParse = useCallback(() => setParseData(undefined), []);
2023-07-25 20:27:29 +03:00
function checkExpression(expression: string, activeCst?: IConstituenta, onSuccess?: DataCallback<IExpressionParse>) {
2023-07-20 17:11:03 +03:00
setError(undefined);
postCheckExpression(String(schema!.id), {
data: { expression: expression },
2023-07-20 17:11:03 +03:00
showError: true,
2023-07-25 20:27:29 +03:00
setLoading,
2023-08-27 15:39:49 +03:00
onError: error => setError(error),
onSuccess: parse => {
2023-08-27 15:39:49 +03:00
if (activeCst) {
adjustResults(parse, expression === getCstExpressionPrefix(activeCst), activeCst.cstType);
}
setParseData(parse);
if (onSuccess) onSuccess(parse);
2023-07-20 17:11:03 +03:00
}
});
}
return { parseData, checkExpression, resetParse, error, setError, loading };
}
2023-07-25 20:27:29 +03:00
export default useCheckExpression;