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';
|
2023-07-26 23:11:00 +03:00
|
|
|
import { DataCallback, postCheckExpression } from '../utils/backendAPI';
|
2023-08-22 22:38:27 +03:00
|
|
|
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';
|
|
|
|
|
2023-08-22 22:38:27 +03:00
|
|
|
function checkTypeConsistency(type: CstType, typification: string, args: IFunctionArg[]): boolean {
|
2023-08-26 21:37:57 +03:00
|
|
|
console.log(typification)
|
2023-08-22 22:38:27 +03:00
|
|
|
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;
|
2023-08-22 22:38:27 +03:00
|
|
|
|
|
|
|
case CstType.AXIOM:
|
|
|
|
case CstType.THEOREM:
|
2023-08-26 21:37:57 +03:00
|
|
|
return typification === LOGIC_TYPIIFCATION && args.length === 0;
|
2023-08-22 22:38:27 +03:00
|
|
|
|
|
|
|
case CstType.FUNCTION:
|
2023-08-26 21:37:57 +03:00
|
|
|
return typification !== LOGIC_TYPIIFCATION && args.length !== 0;
|
2023-08-22 22:38:27 +03:00
|
|
|
|
|
|
|
case CstType.PREDICATE:
|
2023-08-26 21:37:57 +03:00
|
|
|
return typification === LOGIC_TYPIIFCATION && args.length !== 0;
|
2023-08-22 22:38:27 +03:00
|
|
|
}
|
|
|
|
}
|
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-07-25 20:27:29 +03:00
|
|
|
const resetParse = useCallback(() => { setParseData(undefined); }, []);
|
|
|
|
|
2023-08-22 22:38:27 +03:00
|
|
|
function checkExpression(expression: string, activeCst?: IConstituenta, onSuccess?: DataCallback<IExpressionParse>) {
|
2023-07-20 17:11:03 +03:00
|
|
|
setError(undefined);
|
2023-08-22 22:38:27 +03:00
|
|
|
postCheckExpression(String(schema!.id), {
|
2023-07-26 23:11:00 +03:00
|
|
|
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),
|
2023-08-22 22:38:27 +03:00
|
|
|
onSuccess: parse => {
|
2023-08-27 15:39:49 +03:00
|
|
|
if (activeCst) {
|
|
|
|
adjustResults(parse, expression === getCstExpressionPrefix(activeCst), activeCst.cstType);
|
2023-08-22 22:38:27 +03:00
|
|
|
}
|
|
|
|
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;
|