Search in sources :

Example 1 with PureUnmatchedFunctionException

use of org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException in project legend-pure by finos.

the class ExceptionTranslation method getCandidatesOutput.

private static void getCandidatesOutput(PureSession session, PureRuntime runtime, final PureUnmatchedFunctionException exception, SourceInformation sourceToBeModified, MutableList<Candidate> candidatesOutput, ListIterable<CoreInstance> candidates) {
    MutableMap<String, SourceInformation> sourceInfoAndTypeByPath = UnifiedMap.newMap(candidates.size());
    for (CoreInstance candidate : candidates) {
        SourceInformation sourceInfo = candidate.getSourceInformation();
        if ((null != sourceInfo) && (null != runtime)) {
            StringBuilder functionNameStringBuilder = new StringBuilder();
            try {
                org.finos.legend.pure.m3.navigation.function.Function.print(functionNameStringBuilder, candidate, runtime.getProcessorSupport());
            } catch (Exception functionPrintException) {
                // Log error if possible, then ignore the function and continue
                if (null != session) {
                // session.log("Error printing: " + candidate, functionPrintException);
                }
                continue;
            }
            sourceInfoAndTypeByPath.put(functionNameStringBuilder.toString(), sourceInfo);
        }
    }
    final MutableSet<String> pathsInSameSource = Sets.mutable.empty();
    final MutableSet<String> pathsNotInSameSource = Sets.mutable.empty();
    sourceInfoAndTypeByPath.forEachKeyValue(new Procedure2<String, SourceInformation>() {

        @Override
        public void value(String s, SourceInformation sourceInfo) {
            if (sourceInfo.getSourceId().equals(exception.getImportGroup().getSourceInformation().getSourceId())) {
                pathsInSameSource.add(s);
            } else {
                pathsNotInSameSource.add(s);
            }
        }
    });
    for (String path : pathsInSameSource.toSortedList().withAll(pathsNotInSameSource.toSortedList())) {
        SourceInformation sourceInfo = sourceInfoAndTypeByPath.get(path);
        String functionName = exception.getFunctionName();
        int index = functionName.lastIndexOf("::");
        if (-1 != index) {
            // id contains "::"
            functionName = functionName.substring(index + 2);
        }
        index = path.lastIndexOf(functionName);
        if (!(-1 == index)) {
            String importToAdd = "import " + path.substring(0, index) + "*;";
            int lineToBeModified = sourceToBeModified.getStartLine();
            int columnToBeModified = sourceToBeModified.getStartColumn();
            if (0 == columnToBeModified) {
                // special handling for importGroup without any imports, need to add import to the next line
                lineToBeModified++;
            }
            Candidate candidate = new Candidate();
            candidate.sourceID = sourceInfo.getSourceId();
            candidate.line = sourceInfo.getLine();
            candidate.column = sourceInfo.getColumn();
            candidate.foundName = path;
            candidate.fileToBeModified = sourceToBeModified.getSourceId();
            candidate.lineToBeModified = lineToBeModified;
            candidate.columnToBeModified = columnToBeModified;
            candidate.add = true;
            candidate.messageToBeModified = importToAdd;
            candidatesOutput.add(candidate);
        }
    }
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) PureUnmatchedFunctionException(org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) PureUnresolvedIdentifierException(org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException) PureException(org.finos.legend.pure.m4.exception.PureException)

Example 2 with PureUnmatchedFunctionException

use of org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException in project legend-pure by finos.

the class ExceptionTranslation method pureExceptionToJson.

public static IDEResponse pureExceptionToJson(PureSession session, PureException e, ByteArrayOutputStream pureResponse) {
    PureException original = e.getOriginatingPureException();
    PureRuntime runtime = (null == session) ? null : session.getPureRuntime();
    IDEExceptionResponse response;
    if (e instanceof PureUnresolvedIdentifierException) {
        final PureUnresolvedIdentifierException exception = (PureUnresolvedIdentifierException) e;
        if (exception.getImportCandidates(Objects.requireNonNull(session).codeStorage.getAllRepositories()).isEmpty()) {
            response = new IDEExceptionResponse();
        } else {
            RichIterable<CoreInstance> candidates = exception.getImportCandidates(session.codeStorage.getAllRepositories());
            MutableMap<String, Pair<SourceInformation, String>> sourceInfoAndTypeByPath = UnifiedMap.newMap(candidates.size());
            for (CoreInstance candidate : candidates) {
                SourceInformation sourceInfo = candidate.getSourceInformation();
                // TODO think about what we should do with candidates with no source info
                if ((null != sourceInfo) && (null != runtime)) {
                    sourceInfoAndTypeByPath.put(PackageableElement.getUserPathForPackageableElement(candidate), Tuples.pair(sourceInfo, candidate.getClassifier().getName()));
                }
            }
            SourceInformation sourceToBeModified = exception.getImportGroup().getSourceInformation();
            if (null == sourceToBeModified) {
                // avoid null pointer exception
                sourceToBeModified = DUMMY_SOURCE_INFORMATION;
            }
            final MutableSet<String> pathsInSameSource = Sets.mutable.empty();
            final MutableSet<String> pathsNotInSameSource = Sets.mutable.empty();
            sourceInfoAndTypeByPath.forEachKeyValue(new Procedure2<String, Pair<SourceInformation, String>>() {

                @Override
                public void value(String s, Pair<SourceInformation, String> sourceInformationStringPair) {
                    if (sourceInformationStringPair.getOne().getSourceId().equals(exception.getImportGroup().getSourceInformation().getSourceId())) {
                        pathsInSameSource.add(s);
                    } else {
                        pathsNotInSameSource.add(s);
                    }
                }
            });
            IDEPureUnresolvedIdentifierExceptionResponse unresolvedResponse = new IDEPureUnresolvedIdentifierExceptionResponse();
            response = unresolvedResponse;
            unresolvedResponse.candidateName = exception.getIdOrPath();
            MutableList<Candidate> candidatesOutput = Lists.mutable.of();
            unresolvedResponse.candidates = candidatesOutput;
            for (String path : pathsInSameSource.toSortedList().withAll(pathsNotInSameSource.toSortedList())) {
                Pair<SourceInformation, String> pair = sourceInfoAndTypeByPath.get(path);
                SourceInformation sourceInfo = pair.getOne();
                String type = pair.getTwo();
                String id = exception.getIdOrPath();
                int index = id.lastIndexOf("::");
                if (-1 != index) {
                    // id contains "::"
                    id = id.substring(index + 2);
                }
                index = path.lastIndexOf(id);
                if (-1 == index) {
                    throw new RuntimeException("Unable to find identifier: " + exception.getIdOrPath());
                }
                String importToAdd = "import " + path.substring(0, index) + "*;";
                int lineToBeModified = sourceToBeModified.getStartLine();
                int columnToBeModified = sourceToBeModified.getStartColumn();
                if (0 == columnToBeModified) {
                    // special handling for importGroup without any imports, need to add import to the next line
                    lineToBeModified++;
                }
                Candidate candidate = new Candidate();
                candidate.sourceID = sourceInfo.getSourceId();
                candidate.line = sourceInfo.getLine();
                candidate.column = sourceInfo.getColumn();
                candidate.foundName = path;
                candidate.fileToBeModified = sourceToBeModified.getSourceId();
                candidate.lineToBeModified = lineToBeModified;
                candidate.columnToBeModified = columnToBeModified;
                candidate.add = true;
                candidate.messageToBeModified = importToAdd;
                candidate.type = type;
                candidatesOutput.add(candidate);
            }
        }
    } else if (e instanceof PureUnmatchedFunctionException) {
        final PureUnmatchedFunctionException exception = (PureUnmatchedFunctionException) e;
        if (0 == exception.getImportCandidatesWithPackageNotImported().size() + exception.getImportCandidatesWithPackageImported().size()) {
            response = new IDEExceptionResponse();
        } else {
            SourceInformation sourceToBeModified = exception.getImportGroup().getSourceInformation();
            if (null == sourceToBeModified) {
                // avoid null pointer exception
                sourceToBeModified = DUMMY_SOURCE_INFORMATION;
            }
            IDEPureUnmatchedFunctionExceptionResponse ideUnmatchedResponse = new IDEPureUnmatchedFunctionExceptionResponse();
            response = ideUnmatchedResponse;
            ideUnmatchedResponse.candidateName = exception.getFunctionName();
            MutableList<Candidate> candidatesOutputWithPackageNotImported = Lists.mutable.of();
            ideUnmatchedResponse.candidates = candidatesOutputWithPackageNotImported;
            MutableList<Candidate> candidatesOutputWithPackageImported = Lists.mutable.of();
            ideUnmatchedResponse.candidatesWithPackageImported = candidatesOutputWithPackageImported;
            getCandidatesOutput(session, runtime, exception, sourceToBeModified, candidatesOutputWithPackageNotImported, exception.getImportCandidatesWithPackageNotImported());
            getCandidatesOutput(session, runtime, exception, sourceToBeModified, candidatesOutputWithPackageImported, exception.getImportCandidatesWithPackageImported());
        }
    } else if ((e instanceof PureParserException || e instanceof PureCompilationException)) {
        IDEParserOrCompilerException parserOrCompilerException = new IDEParserOrCompilerException();
        parserOrCompilerException.exceptionType = e.getExceptionName();
        response = parserOrCompilerException;
    } else {
        response = new IDEExceptionResponse();
    }
    if (null != pureResponse) {
        String pureResponseStr = new String(pureResponse.toByteArray()) + '\n';
        response.appendText(pureResponseStr);
    }
    if (e.hasPureStackTrace()) {
        response.appendText(original.getMessage() + "\n" + e.getPureStackTrace("    "));
    } else {
        response.appendText(original.getMessage());
    }
    SourceInformation sourceInformation = original.getSourceInformation();
    if ((null != sourceInformation) && (null != runtime) && (null != runtime.getSourceById(sourceInformation.getSourceId()))) {
        response.RO = runtime.isSourceImmutable(sourceInformation.getSourceId());
        response.source = sourceInformation.getSourceId();
        response.line = sourceInformation.getLine();
        response.column = sourceInformation.getColumn();
    }
    if (null != session) {
    // response.compiler = session.getCompilerLogs();
    }
    return response;
}
Also used : PureUnresolvedIdentifierException(org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException) MutableList(org.eclipse.collections.api.list.MutableList) Pair(org.eclipse.collections.api.tuple.Pair) PureUnmatchedFunctionException(org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureException(org.finos.legend.pure.m4.exception.PureException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 3 with PureUnmatchedFunctionException

use of org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException in project legend-pure by finos.

the class FunctionExpressionProcessor method throwNoMatchException.

private static void throwNoMatchException(FunctionExpression functionExpression, ProcessorState processorState, ProcessorSupport processorSupport) throws PureCompilationException {
    StringBuilder message = new StringBuilder("The system can't find a match for the function: ");
    StringBuilder functionSignatureBuilder = new StringBuilder();
    org.finos.legend.pure.m3.navigation.functionexpression.FunctionExpression.printFunctionSignatureFromExpression(functionSignatureBuilder, functionExpression, processorSupport);
    message.append(functionSignatureBuilder);
    SourceInformation functionExpressionSourceInformation = functionExpression.getSourceInformation();
    String functionName = getFunctionName(functionExpression);
    SetIterable<CoreInstance> possibleFunctions = processorSupport.function_getFunctionsForName(functionName);
    if ((0 < possibleFunctions.size()) && (possibleFunctions.size() < 20)) {
        ImportGroup functionExpressionImportGroup = functionExpression._importGroup();
        SetIterable<String> alreadyImportedPackages = functionExpressionImportGroup._imports().collect(ImportAccessor::_path, Sets.mutable.empty());
        PartitionSet<CoreInstance> partition = possibleFunctions.partition(f -> {
            Package functionPackage = ((Function<?>) f)._package();
            return null == functionPackage._package() || alreadyImportedPackages.contains(PackageableElement.getUserPathForPackageableElement(functionPackage));
        });
        SetIterable<CoreInstance> possibleFunctionsWithPackageNotImported = partition.getRejected();
        SetIterable<CoreInstance> possibleFunctionsWithPackageImported = partition.getSelected();
        MutableList<CoreInstance> candidatesNotInCoreImportsWithPackageNotImported = Lists.mutable.empty();
        MutableList<CoreInstance> candidatesInCoreImportsWithPackageNotImported = Lists.mutable.empty();
        MutableList<CoreInstance> candidatesNotInCoreImportsWithPackageImported = Lists.mutable.empty();
        MutableList<CoreInstance> candidatesInCoreImportsWithPackageImported = Lists.mutable.empty();
        // Get core imports, do not add functions found in coreImports to candidates
        ImportGroup coreImport = (ImportGroup) processorSupport.package_getByUserPath(M3Paths.coreImport);
        MutableSet<String> coreImports = coreImport._imports().collect(ImportAccessor::_path).toSet();
        populateFunctionCandidates(processorState, processorSupport, functionExpressionSourceInformation, possibleFunctionsWithPackageNotImported, candidatesNotInCoreImportsWithPackageNotImported, candidatesInCoreImportsWithPackageNotImported, coreImports);
        populateFunctionCandidates(processorState, processorSupport, functionExpressionSourceInformation, possibleFunctionsWithPackageImported, candidatesNotInCoreImportsWithPackageImported, candidatesInCoreImportsWithPackageImported, coreImports);
        throw new PureUnmatchedFunctionException(functionExpression.getSourceInformation(), functionSignatureBuilder.toString(), functionName, candidatesInCoreImportsWithPackageNotImported, candidatesNotInCoreImportsWithPackageNotImported, candidatesInCoreImportsWithPackageImported, candidatesNotInCoreImportsWithPackageImported, functionExpressionImportGroup, processorSupport);
    }
    throw new PureCompilationException(functionExpression.getSourceInformation(), message.toString());
}
Also used : ImportAccessor(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportAccessor) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Package(org.finos.legend.pure.m3.coreinstance.Package) ImportGroup(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup) PureUnmatchedFunctionException(org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

PureUnmatchedFunctionException (org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)3 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)3 PureUnresolvedIdentifierException (org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException)2 PureException (org.finos.legend.pure.m4.exception.PureException)2 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)2 MutableList (org.eclipse.collections.api.list.MutableList)1 Pair (org.eclipse.collections.api.tuple.Pair)1 Package (org.finos.legend.pure.m3.coreinstance.Package)1 ImportAccessor (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportAccessor)1 ImportGroup (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup)1 Function (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function)1 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)1 PureRuntime (org.finos.legend.pure.m3.serialization.runtime.PureRuntime)1