Example #1
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="IResultOfExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        public static bool ResolveCompilerReferences(IResultOfExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IExpression Source = (IExpression)node.Source;
            IResultType ResolvedSourceResult = Source.ResolvedResult.Item;
            int         ResultNameIndex      = ResolvedSourceResult.ResultNameIndex;

            if (ResultNameIndex < 0)
            {
                errorList.AddError(new ErrorInvalidExpression(node));
                return(false);
            }

            Debug.Assert(ResolvedSourceResult.Preferred != null);
            resolvedResult = new ResultType(ResolvedSourceResult.Preferred);

            constantSourceList.Add(Source);

            ResultException.Propagate(Source.ResolvedException, out resolvedException);

#if COVERAGE
            Debug.Assert(node.IsComplex);
#endif

            return(true);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IUnaryNotExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        public static bool ResolveCompilerReferences(IUnaryNotExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IExpression RightExpression = (IExpression)node.RightExpression;
            IClass      EmbeddingClass  = node.EmbeddingClass;

            bool IsRightClassType = Expression.GetClassTypeOfExpression(RightExpression, errorList, out IClassType RightExpressionClassType);

            if (!IsRightClassType)
            {
                return(false);
            }

            Expression.IsLanguageTypeAvailable(LanguageClasses.Boolean.Guid, node, out ITypeName BooleanTypeName, out ICompiledType BooleanType);
            Expression.IsLanguageTypeAvailable(LanguageClasses.Event.Guid, node, out ITypeName EventTypeName, out ICompiledType EventType);

            Debug.Assert(RightExpressionClassType == BooleanType || RightExpressionClassType == EventType);

            node.SetIsEventExpression(RightExpressionClassType != BooleanType);

            constantSourceList.Add(RightExpression);

            resolvedResult = new ResultType(BooleanTypeName, BooleanType, string.Empty);
            ResultException.Propagate(RightExpression.ResolvedException, out resolvedException);

#if COVERAGE
            Debug.Assert(node.IsComplex);
#endif

            return(true);
        }
Example #3
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="IManifestCharacterExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        public static bool ResolveCompilerReferences(IManifestCharacterExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IClass EmbeddingClass = node.EmbeddingClass;
            string ValidText      = node.ValidText.Item;

            Debug.Assert(ValidText.Length == 1);
            char ValidChar = ValidText[0];

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Character.Guid, node, out ITypeName CharacterTypeName, out ICompiledType CharacterType))
            {
                errorList.AddError(new ErrorCharacterTypeMissing(node));
                return(false);
            }

            resolvedResult = new ResultType(CharacterTypeName, CharacterType, string.Empty);

            resolvedException  = new ResultException();
            expressionConstant = new CharacterLanguageConstant(ValidChar);

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IManifestNumberExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant type upon return.</param>
        public static bool ResolveCompilerReferences(IManifestNumberExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IClass EmbeddingClass = node.EmbeddingClass;
            string NumberText     = node.ValidText.Item;
            ISealableDictionary <ITypeName, ICompiledType> TypeTable = EmbeddingClass.TypeTable;

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Number.Guid, node, out ITypeName NumberTypeName, out ICompiledType NumberType))
            {
                errorList.AddError(new ErrorNumberTypeMissing(node));
                return(false);
            }

            resolvedResult    = new ResultType(NumberTypeName, NumberType, string.Empty);
            resolvedException = new ResultException();

            FormattedNumber FormattedNumber = FormattedNumber.Parse(NumberText);

            Debug.Assert(string.IsNullOrEmpty(FormattedNumber.InvalidText));

            expressionConstant = new NumberLanguageConstant(FormattedNumber.Value);

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
Example #5
0
        public void NewWithInnerException()
        {
            var inner = new ArgumentException();
            var error = new ResultException("Message", inner);

            Assert.Equal("Message", error.Message);
            Assert.True(ReferenceEquals(inner, error.InnerException));
        }
Example #6
0
 public void ToCustomResultT()
 {
     var ex = new ResultException(5);
     var r = ex.ToCustomResult<Result<Uri>>();
     Assert.Equal(5, r.Status);
     Assert.Equal("错误 5", r.Message);
     Assert.Equal(ex, r.Exception);
 }
Example #7
0
        public void ToResult()
        {
            var ex = new ResultException(5);
            var r = ex.ToResult();

            Assert.Equal(5, r.Status);
            Assert.Equal("错误 5", r.Message);
            Assert.Equal(ex, r.Exception);
        }
Example #8
0
        public void LoggerExceptionTest()
        {
            var exception     = new ResultException("Exception");
            var failureResult = GetFailureResult(exception);

            Assert.IsAssignableFrom <IFailureResult>(failureResult);
            Assert.Equal(LogMessage, exception.Message);
            Assert.Equal(LogException, exception);
        }
Example #9
0
 public void Constructor_String_Int32()
 {
     var message = "test";
     var status = 5;
     var ex = new ResultException(message, status);
     Assert.Equal(message, ex.Message);
     Assert.Equal(status, ex.Status);
     Assert.Throws<ArgumentOutOfRangeException>(() => new ResultException("", 0));
 }
Example #10
0
        public void Serialization()
        {
            var formatter = new BinaryFormatter();
            var stream    = new MemoryStream();
            var source    = new ResultException("Message", new ArgumentException("Some error"));

            formatter.Serialize(stream, source);
            var result = Assert.IsType <ResultException>(formatter.Deserialize(new MemoryStream(stream.ToArray())));

            Assert.Equal("Message", result.Message);
            var target = Assert.IsType <ArgumentException>(result.InnerException);

            Assert.Equal("Some error", target.Message);
        }
Example #11
0
        public async Task Login_WhenUserIdentityServiceThrowsResultException_ReturnsResult()
        {
            var resultException = new ResultException("key");

            _userIdentityService.Login(UserName, Password).Throws(resultException);

            var result = await _authenticationService.Login(new LoginRequestDto
            {
                UserName = UserName,
                Password = Password
            });

            result.IsSuccess.ShouldBeFalse();
            result.Message.Key.ShouldBe(resultException.Result.Message.Key);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IKeywordExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        public static bool ResolveCompilerReferences(IKeywordExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IClass EmbeddingClass = node.EmbeddingClass;

            BaseNode.Keyword Value = node.Value;

            if (!IsKeywordAvailable(Value, node, errorList, out ITypeName KeywordTypeName, out ICompiledType KeywordType))
            {
                return(false);
            }

            resolvedResult = new ResultType(KeywordTypeName, KeywordType, Value.ToString());

            resolvedException = new ResultException();

            bool IsHandled = false;

            switch (Value)
            {
            case BaseNode.Keyword.True:
            case BaseNode.Keyword.False:
                expressionConstant = new BooleanLanguageConstant(Value == BaseNode.Keyword.True);
                IsHandled          = true;
                break;

            case BaseNode.Keyword.Current:
            case BaseNode.Keyword.Value:
            case BaseNode.Keyword.Result:
            case BaseNode.Keyword.Retry:
            case BaseNode.Keyword.Exception:
            case BaseNode.Keyword.Indexer:
                IsHandled = true;
                break;
            }

            Debug.Assert(IsHandled);

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
Example #13
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="IInitializedObjectExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        /// <param name="initializedObjectTypeName">The initialized object type name upon return.</param>
        /// <param name="initializedObjectType">The initialized object type upon return.</param>
        /// <param name="assignedFeatureTable">The table of assigned values upon return.</param>
        public static bool ResolveCompilerReferences(IInitializedObjectExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out ITypeName initializedObjectTypeName, out IClassType initializedObjectType, out ISealableDictionary <string, ICompiledFeature> assignedFeatureTable)
        {
            resolvedResult            = null;
            resolvedException         = null;
            constantSourceList        = new SealableList <IExpression>();
            expressionConstant        = NeutralLanguageConstant.NotConstant;
            initializedObjectTypeName = null;
            initializedObjectType     = null;
            assignedFeatureTable      = new SealableDictionary <string, ICompiledFeature>();

            IIdentifier ClassIdentifier = (IIdentifier)node.ClassIdentifier;
            IList <IAssignmentArgument> AssignmentList = node.AssignmentList;
            IClass EmbeddingClass = node.EmbeddingClass;
            string ValidText      = ClassIdentifier.ValidText.Item;

            if (!EmbeddingClass.ImportedClassTable.ContainsKey(ValidText))
            {
                errorList.AddError(new ErrorUnknownIdentifier(ClassIdentifier, ValidText));
                return(false);
            }

            IClass BaseClass = EmbeddingClass.ImportedClassTable[ValidText].Item;

            Debug.Assert(BaseClass.ResolvedClassTypeName.IsAssigned);
            Debug.Assert(BaseClass.ResolvedClassType.IsAssigned);
            initializedObjectTypeName = BaseClass.ResolvedClassTypeName.Item;
            initializedObjectType     = BaseClass.ResolvedClassType.Item;

            if (!CheckAssignemntList(node, errorList, BaseClass.FeatureTable, constantSourceList, assignedFeatureTable))
            {
                return(false);
            }

            resolvedResult = new ResultType(initializedObjectTypeName, initializedObjectType, string.Empty);

            if (!ResolveObjectInitialization(node, errorList, assignedFeatureTable))
            {
                return(false);
            }

            resolvedException = new ResultException();

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
Example #14
0
        public Task ErrorEvent(HttpContext context)
        {
            var             feature = context.Features.Get <IExceptionHandlerFeature>();
            var             error   = feature?.Error;
            ResultException ex;

            if (error.GetType() == typeof(ResultException))
            {
                ex = (ResultException)error;
            }
            else
            {
                ex = new ResultException();
            }
            context.Response.ContentType = "application/json; charset=utf-8";
            context.Response.StatusCode  = ex.Code;
            return(context.Response.WriteAsync(JsonConvert.SerializeObject(new Result(ex.Code, ex.Message)), Encoding.GetEncoding("UTF-8")));
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="INewExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        /// <param name="resolvedFinalFeature">The matching feature upon return.</param>
        public static bool ResolveCompilerReferences(INewExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out ICompiledFeature resolvedFinalFeature)
        {
            resolvedResult       = null;
            resolvedException    = null;
            constantSourceList   = new SealableList <IExpression>();
            expressionConstant   = NeutralLanguageConstant.NotConstant;
            resolvedFinalFeature = null;

            IClass              EmbeddingClass = node.EmbeddingClass;
            IQualifiedName      Object         = (IQualifiedName)node.Object;
            IList <IIdentifier> ValidPath      = Object.ValidPath.Item;
            IClassType          BaseType       = EmbeddingClass.ResolvedClassType.Item;

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Boolean.Guid, node, out ITypeName BooleanTypeName, out ICompiledType BooleanType))
            {
                errorList.AddError(new ErrorBooleanTypeMissing(node));
                return(false);
            }

            ISealableDictionary <string, IScopeAttributeFeature> LocalScope = Scope.CurrentScope(node);

            if (!ObjectType.GetQualifiedPathFinalType(EmbeddingClass, BaseType, LocalScope, ValidPath, 0, errorList, out ICompiledFeature FinalFeature, out IDiscrete FinalDiscrete, out ITypeName FinalTypeName, out ICompiledType FinalType, out bool InheritBySideAttribute))
            {
                return(false);
            }

            if (FinalFeature == null)
            {
                errorList.AddError(new ErrorConstantNewExpression(node));
                return(false);
            }

            ObjectType.FillResultPath(EmbeddingClass, BaseType, LocalScope, ValidPath, 0, Object.ValidResultTypePath.Item);

            resolvedResult       = new ResultType(BooleanTypeName, BooleanType, string.Empty);
            resolvedException    = new ResultException();
            resolvedFinalFeature = FinalFeature;

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IAssertionTagExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        public static bool ResolveCompilerReferences(IAssertionTagExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            IExpression ResolvedBooleanExpression = node.ResolvedBooleanExpression.Item;

            resolvedResult = ResolvedBooleanExpression.ResolvedResult.Item;
            ResultException.Propagate(ResolvedBooleanExpression.ResolvedException, out resolvedException);
            expressionConstant = NeutralLanguageConstant.NotConstant;

            constantSourceList = new SealableList <IExpression>()
            {
                ResolvedBooleanExpression
            };

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
Example #17
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="ICloneOfExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        public static bool ResolveCompilerReferences(ICloneOfExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IExpression Source = (IExpression)node.Source;
            IResultType ResolvedSourceResult = Source.ResolvedResult.Item;

            foreach (IExpressionType Item in ResolvedSourceResult)
            {
                if (Item.ValueType is IClassType AsClassType)
                {
                    IClass BaseClass = AsClassType.BaseClass;

                    if (BaseClass.Cloneable == BaseNode.CloneableStatus.Cloneable)
                    {
                        AsClassType.MarkAsUsedInCloneOf();
                    }
                    else
                    {
                        errorList.AddError(new ErrorCloningNotAllowed(node, BaseClass.ValidClassName));
                        return(false);
                    }
                }
                else
                {
                    errorList.AddError(new ErrorClassTypeRequired(node));
                    return(false);
                }
            }

            resolvedResult = ResolvedSourceResult;
            ResultException.Propagate(Source.ResolvedException, out resolvedException);

#if COVERAGE
            Debug.Assert(node.IsComplex);
#endif

            return(true);
        }
Example #18
0
        public async Task RegisterTechnicalAdvisor_WhenIdentityUserServiceThrowsResultException_ShouldReturnErrorResultWithAppropriateKey()
        {
            var exception = new ResultException(ResultKey.User.Registration.UserAlreadyExists);

            _identityUserRegistrationServiceMock.When(ius => ius.CreateIdentityUser(Arg.Any <string>(), Arg.Any <Role>()))
            .Do(x => throw exception);

            var userEmail = "*****@*****.**";
            var registerTechnicalAdvisorRequestDto = new CreateTechnicalAdvisorRequestDto
            {
                Name  = userEmail,
                Email = userEmail
            };

            var nationalSocietyId = 1;
            var result            = await _technicalAdvisorService.Create(nationalSocietyId, registerTechnicalAdvisorRequestDto);


            result.IsSuccess.ShouldBeFalse();
            result.Message.Key.ShouldBe(exception.Result.Message.Key);
        }
Example #19
0
        private void ProcessResultException(ResultException e, string messagePrefix)
        {
            DiscordManager.active = false;
            DiscordManager.SetDeactivationReasonFromException(e);
            if (lastCheckDeactivationReason == deactivationReason)
            {
                return;                                                    // Already messaged this.
            }
            lastCheckDeactivationReason = deactivationReason;
            if (e.Result != Result.NotRunning && e.Result != Result.InternalError)
            {
                Plugin.log.Error(messagePrefix + e.Message);
                Plugin.log.Debug(e);
            }
            else
            {
                Plugin.log.Info(messagePrefix + "Discord is not running.");
#if DEBUG
                Plugin.log.Debug(e);
#endif
            }
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IKeywordEntityExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        /// <param name="resolvedFinalFeature">The feature if the end of the path is a feature.</param>
        public static bool ResolveCompilerReferences(IKeywordEntityExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out ICompiledFeature resolvedFinalFeature)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }
            if (errorList == null)
            {
                throw new ArgumentNullException(nameof(errorList));
            }

            resolvedResult       = null;
            resolvedException    = null;
            constantSourceList   = new SealableList <IExpression>();
            expressionConstant   = NeutralLanguageConstant.NotConstant;
            resolvedFinalFeature = null;

            IClass EmbeddingClass = node.EmbeddingClass;

            BaseNode.Keyword Value = node.Value;

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Entity.Guid, node, out ITypeName ResultTypeName, out ICompiledType ResultType))
            {
                errorList.AddError(new ErrorEntityTypeMissing(node));
                return(false);
            }

            Guid EntityGuid = Guid.Empty;

            switch (node.Value)
            {
            case BaseNode.Keyword.Indexer:
                if (EmbeddingClass.ClassIndexer.IsAssigned)
                {
                    IIndexerFeature ClassIndexer = EmbeddingClass.ClassIndexer.Item;
                    resolvedFinalFeature = ClassIndexer;
                    EntityGuid           = ClassIndexer.EntityGuid;

                    expressionConstant = new EntityLanguageConstant(ClassIndexer);
                }
                else
                {
                    errorList.AddError(new ErrorMissingIndexer(node));
                    return(false);
                }
                break;

            default:
                errorList.AddError(new ErrorInvalidExpression(node));
                return(false);
            }

            Debug.Assert(EntityGuid != Guid.Empty);

            ITypeName     EntityTypeName = EmbeddingClass.ImportedLanguageTypeTable[EntityGuid].Item1;
            ICompiledType EntityType     = EmbeddingClass.ImportedLanguageTypeTable[EntityGuid].Item2;

            resolvedResult    = new ResultType(EntityTypeName, EntityType, string.Empty);
            resolvedException = new ResultException();

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IAgentExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        /// <param name="resolvedFeature">The feature found upon return.</param>
        public static bool ResolveCompilerReferences(IAgentExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out ICompiledFeature resolvedFeature)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;
            resolvedFeature    = null;

            IIdentifier Delegated = (IIdentifier)node.Delegated;

            Debug.Assert(Delegated.ValidText.IsAssigned);
            string ValidText = Delegated.ValidText.Item;

            IFeatureInstance FeatureInstance;

            if (node.BaseType.IsAssigned)
            {
                IObjectType   BaseType         = (IObjectType)node.BaseType.Item;
                ICompiledType ResolvedBaseType = BaseType.ResolvedType.Item;
                ISealableDictionary <IFeatureName, IFeatureInstance> FeatureTable = null;

                switch (ResolvedBaseType)
                {
                case IClassType AsClassType:
                    FeatureTable = AsClassType.FeatureTable;
                    break;

                case IFormalGenericType AsFormalGenericType:
                    foreach (IConstraint Item in AsFormalGenericType.FormalGeneric.ConstraintList)
                    {
                        if (Item.ResolvedTypeWithRename.Item is IClassType Parent)
                        {
                            FeatureTable = Parent.FeatureTable;

                            if (FeatureName.TableContain(FeatureTable, ValidText, out IFeatureName ParentKey, out IFeatureInstance ParentFeatureInstance))
                            {
                                break;
                            }
                        }
                    }
                    break;
                }

                if (FeatureTable == null)
                {
                    errorList.AddError(new ErrorClassTypeRequired(node));
                    return(false);
                }

                if (!FeatureName.TableContain(FeatureTable, ValidText, out IFeatureName Key, out FeatureInstance))
                {
                    errorList.AddError(new ErrorUnknownIdentifier(node, ValidText));
                    return(false);
                }
            }
            else
            {
                IClass EmbeddingClass = node.EmbeddingClass;
                if (!FeatureName.TableContain(EmbeddingClass.FeatureTable, ValidText, out IFeatureName Key, out FeatureInstance))
                {
                    errorList.AddError(new ErrorUnknownIdentifier(node, ValidText));
                    return(false);
                }
            }

            Debug.Assert(FeatureInstance.Feature != null);
            resolvedFeature = FeatureInstance.Feature;

            resolvedResult = new ResultType(resolvedFeature.ResolvedAgentTypeName.Item, resolvedFeature.ResolvedAgentType.Item, string.Empty);

            resolvedException  = new ResultException();
            expressionConstant = new AgentLanguageConstant(resolvedFeature);

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IBinaryOperatorExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        /// <param name="selectedFeature">The matching feature upon return.</param>
        /// <param name="selectedOverload">The matching overload in <paramref name="selectedFeature"/> upon return.</param>
        /// <param name="featureCall">Details of the feature call.</param>
        public static bool ResolveCompilerReferences(IBinaryOperatorExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out IFunctionFeature selectedFeature, out IQueryOverload selectedOverload, out IFeatureCall featureCall)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;
            selectedFeature    = null;
            selectedOverload   = null;
            featureCall        = null;

            IExpression LeftExpression  = (IExpression)node.LeftExpression;
            IIdentifier Operator        = (IIdentifier)node.Operator;
            IExpression RightExpression = (IExpression)node.RightExpression;
            IResultType LeftResult      = LeftExpression.ResolvedResult.Item;

            if (LeftResult.TryGetResult(out ICompiledType LeftExpressionType))
            {
                if (LeftExpressionType is IClassType AsClassType)
                {
                    string OperatorName = Operator.ValidText.Item;

                    ISealableDictionary <IFeatureName, IFeatureInstance> LeftFeatureTable = AsClassType.FeatureTable;

                    if (!FeatureName.TableContain(LeftFeatureTable, OperatorName, out IFeatureName Key, out IFeatureInstance Value))
                    {
                        errorList.AddError(new ErrorUnknownIdentifier(Operator, OperatorName));
                        return(false);
                    }

                    Debug.Assert(Value.Feature != null);

                    ICompiledFeature OperatorFeature = Value.Feature;
                    ICompiledType    OperatorType    = OperatorFeature.ResolvedAgentType.Item;

                    if (OperatorFeature is IFunctionFeature AsFunctionFeature && OperatorType is FunctionType AsFunctionType)
                    {
                        IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >();
                        foreach (IQueryOverloadType Overload in AsFunctionType.OverloadList)
                        {
                            ParameterTableList.Add(Overload.ParameterTable);
                        }

                        IResultType RightResult = RightExpression.ResolvedResult.Item;
                        if (!Argument.ArgumentsConformToParameters(ParameterTableList, RightResult.ToList(), TypeArgumentStyles.Positional, errorList, Operator, out int SelectedIndex))
                        {
                            return(false);
                        }

                        IQueryOverloadType SelectedOverloadType = AsFunctionType.OverloadList[SelectedIndex];
                        resolvedResult   = new ResultType(SelectedOverloadType.ResultTypeList);
                        selectedFeature  = AsFunctionFeature;
                        selectedOverload = AsFunctionFeature.OverloadList[SelectedIndex];

                        IArgument         FirstArgument = new PositionalArgument(RightExpression);
                        IList <IArgument> ArgumentList  = new List <IArgument>()
                        {
                            FirstArgument
                        };

                        List <IExpressionType> MergedArgumentList = new List <IExpressionType>();
                        bool IsArgumentValid = Argument.Validate(ArgumentList, MergedArgumentList, out TypeArgumentStyles TypeArgumentStyle, errorList);
                        Debug.Assert(IsArgumentValid);

                        featureCall       = new FeatureCall(SelectedOverloadType.ParameterTable, SelectedOverloadType.ResultTable, ArgumentList, MergedArgumentList, TypeArgumentStyle);
                        resolvedException = new ResultException(SelectedOverloadType.ExceptionIdentifierList);

                        constantSourceList.Add(LeftExpression);
                        constantSourceList.Add(RightExpression);
                    }
                    else
                    {
                        errorList.AddError(new ErrorInvalidOperator(Operator, OperatorName));
                        return(false);
                    }
                }
                else
                {
                    errorList.AddError(new ErrorInvalidExpression(LeftExpression));
                    return(false);
                }
            }
Example #23
0
 public void get_Message()
 {
     var ex = new ResultException("test");
     Assert.Equal(ResultStatus.Failed, ex.Status);
     Assert.Equal("test", ex.Message);
 }
Example #24
0
 public void get_Status()
 {
     var ex = new ResultException(5);
     Assert.Equal(5, ex.Status);
     Assert.Equal("错误 5", ex.Message);
 }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IUnaryOperatorExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        /// <param name="selectedFeature">The matching feature upon return.</param>
        /// <param name="selectedOverload">The matching overload in <paramref name="selectedFeature"/> upon return.</param>
        /// <param name="selectedOverloadType">The matching overload type upon return.</param>
        public static bool ResolveCompilerReferences(IUnaryOperatorExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out IFunctionFeature selectedFeature, out IQueryOverload selectedOverload, out IQueryOverloadType selectedOverloadType)
        {
            resolvedResult       = null;
            resolvedException    = null;
            constantSourceList   = new SealableList <IExpression>();
            expressionConstant   = NeutralLanguageConstant.NotConstant;
            selectedFeature      = null;
            selectedOverload     = null;
            selectedOverloadType = null;

            IIdentifier Operator        = (IIdentifier)node.Operator;
            string      ValidText       = Operator.ValidText.Item;
            IExpression RightExpression = (IExpression)node.RightExpression;
            IResultType RightResult     = RightExpression.ResolvedResult.Item;

            if (!RightResult.TryGetResult(out ICompiledType RightExpressionType))
            {
                errorList.AddError(new ErrorInvalidExpression(RightExpression));
                return(false);
            }

            if (RightExpressionType is IClassType AsClassType)
            {
                ISealableDictionary <IFeatureName, IFeatureInstance> RightFeatureTable = AsClassType.FeatureTable;

                if (!FeatureName.TableContain(RightFeatureTable, ValidText, out IFeatureName Key, out IFeatureInstance Value))
                {
                    errorList.AddError(new ErrorUnknownIdentifier(RightExpression, ValidText));
                    return(false);
                }

                ICompiledFeature OperatorFeature = Value.Feature;
                ICompiledType    OperatorType    = OperatorFeature.ResolvedAgentType.Item;

                if (OperatorFeature is IFunctionFeature AsFunctionFeature && OperatorType is IFunctionType AsFunctionType)
                {
                    IList <IQueryOverloadType> OperatorOverloadList = AsFunctionType.OverloadList;

                    int SelectedOperatorIndex = -1;
                    for (int i = 0; i < OperatorOverloadList.Count; i++)
                    {
                        IQueryOverloadType Overload = OperatorOverloadList[i];
                        if (Overload.ParameterList.Count == 0 && Overload.ResultList.Count == 1)
                        {
                            SelectedOperatorIndex = i;
                            break;
                        }
                    }

                    if (SelectedOperatorIndex < 0)
                    {
                        errorList.AddError(new ErrorInvalidOperator(Operator, ValidText));
                        return(false);
                    }

                    resolvedResult       = Feature.CommonResultType(AsFunctionType.OverloadList);
                    selectedFeature      = AsFunctionFeature;
                    selectedOverload     = AsFunctionFeature.OverloadList[SelectedOperatorIndex];
                    selectedOverloadType = OperatorOverloadList[SelectedOperatorIndex];
                    resolvedException    = new ResultException(selectedOverloadType.ExceptionIdentifierList);

                    constantSourceList.Add(RightExpression);
                }
                else
                {
                    errorList.AddError(new ErrorInvalidOperator(Operator, ValidText));
                    return(false);
                }
            }
        /// <summary>
        /// Finds the matching nodes of a <see cref="IBinaryConditionalExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        public static bool ResolveCompilerReferences(IBinaryConditionalExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IExpression LeftExpression = (IExpression)node.LeftExpression;

            BaseNode.ConditionalTypes Conditional = node.Conditional;
            IExpression RightExpression           = (IExpression)node.RightExpression;
            IClass      EmbeddingClass            = node.EmbeddingClass;

            bool IsLeftClassType  = Expression.GetClassTypeOfExpression(LeftExpression, errorList, out IClassType LeftExpressionClassType);
            bool IsRightClassType = Expression.GetClassTypeOfExpression(RightExpression, errorList, out IClassType RightExpressionClassType);

            if (!IsLeftClassType || !IsRightClassType)
            {
                return(false);
            }

            Expression.IsLanguageTypeAvailable(LanguageClasses.Boolean.Guid, node, out ITypeName BooleanTypeName, out ICompiledType BooleanType);
            Expression.IsLanguageTypeAvailable(LanguageClasses.Event.Guid, node, out ITypeName EventTypeName, out ICompiledType EventType);

            Debug.Assert(LeftExpressionClassType == BooleanType || LeftExpressionClassType == EventType);
            Debug.Assert(RightExpressionClassType == BooleanType || RightExpressionClassType == EventType);

            if (LeftExpressionClassType != RightExpressionClassType)
            {
                errorList.AddError(new ErrorInvalidExpression(LeftExpression));
                return(false);
            }

            bool IsEventExpression = LeftExpressionClassType != BooleanType;

            if (IsEventExpression)
            {
                if (!((node.ParentSource is IBinaryConditionalExpression) || (node.ParentSource is IConditional) || (node.ParentSource is ICheckInstruction)))
                {
                    errorList.AddError(new ErrorInvalidExpression(node));
                    return(false);
                }

                resolvedResult = new ResultType(EventTypeName, EventType, string.Empty);
            }
            else
            {
                resolvedResult = new ResultType(BooleanTypeName, BooleanType, string.Empty);
            }

            constantSourceList.Add(LeftExpression);
            constantSourceList.Add(RightExpression);

            resolvedException = new ResultException();
            ResultException.Merge(resolvedException, LeftExpression.ResolvedException);
            ResultException.Merge(resolvedException, RightExpression.ResolvedException);

#if COVERAGE
            Debug.Assert(node.IsComplex);
#endif

            return(true);
        }
Example #27
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="IEqualityExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The constant value upon return, if any.</param>
        public static bool ResolveCompilerReferences(IEqualityExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant)
        {
            resolvedResult     = null;
            resolvedException  = null;
            constantSourceList = new SealableList <IExpression>();
            expressionConstant = NeutralLanguageConstant.NotConstant;

            IExpression LeftExpression  = (IExpression)node.LeftExpression;
            IExpression RightExpression = (IExpression)node.RightExpression;
            IClass      EmbeddingClass  = node.EmbeddingClass;
            IResultType LeftResult      = LeftExpression.ResolvedResult.Item;
            IResultType RightResult     = RightExpression.ResolvedResult.Item;

            if (LeftResult.Count != RightResult.Count)
            {
                errorList.AddError(new ErrorExpressionResultMismatch(node));
                return(false);
            }

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Boolean.Guid, node, out ITypeName ResultTypeName, out ICompiledType ResultType))
            {
                errorList.AddError(new ErrorBooleanTypeMissing(node));
                return(false);
            }

            if (LeftResult.Count > 1)
            {
                int MismatchingResultCount = 0;
                foreach (IExpressionType LeftItem in LeftResult)
                {
                    ICompiledType LeftExpressionType = LeftItem.ValueType;

                    bool MatchingNameFound = false;
                    foreach (IExpressionType RightItem in RightResult)
                    {
                        if (LeftItem.Name == RightItem.Name)
                        {
                            MatchingNameFound = true;
                            ICompiledType RightExpressionType = RightItem.ValueType;

                            if (!ObjectType.TypeConformToBase(LeftExpressionType, RightExpressionType, isConversionAllowed: true) && !ObjectType.TypeConformToBase(RightExpressionType, LeftExpressionType, isConversionAllowed: true))
                            {
                                MismatchingResultCount++;
                            }

                            break;
                        }
                    }

                    if (!MatchingNameFound)
                    {
                        MismatchingResultCount++;
                    }
                }

                if (MismatchingResultCount > 0)
                {
                    errorList.AddError(new ErrorExpressionResultMismatch(node));
                    return(false);
                }
            }

            resolvedResult = new ResultType(ResultTypeName, ResultType, string.Empty);

            constantSourceList.Add(LeftExpression);
            constantSourceList.Add(RightExpression);

            resolvedException = new ResultException();
            ResultException.Merge(resolvedException, LeftExpression.ResolvedException);
            ResultException.Merge(resolvedException, RightExpression.ResolvedException);

#if COVERAGE
            Debug.Assert(node.IsComplex);
#endif

            return(true);
        }
Example #28
0
        /// <summary>
        /// Finds the matching nodes of a <see cref="IEntityExpression"/>.
        /// </summary>
        /// <param name="node">The agent expression to check.</param>
        /// <param name="errorList">The list of errors found.</param>
        /// <param name="resolvedResult">The expression result types upon return.</param>
        /// <param name="resolvedException">Exceptions the expression can throw upon return.</param>
        /// <param name="constantSourceList">Sources of the constant expression upon return, if any.</param>
        /// <param name="expressionConstant">The expression constant upon return.</param>
        /// <param name="resolvedFinalFeature">The feature if the end of the path is a feature.</param>
        /// <param name="resolvedFinalDiscrete">The discrete if the end of the path is a discrete.</param>
        public static bool ResolveCompilerReferences(IEntityExpression node, IErrorList errorList, out IResultType resolvedResult, out IResultException resolvedException, out ISealableList <IExpression> constantSourceList, out ILanguageConstant expressionConstant, out ICompiledFeature resolvedFinalFeature, out IDiscrete resolvedFinalDiscrete)
        {
            resolvedResult        = null;
            resolvedException     = null;
            constantSourceList    = new SealableList <IExpression>();
            expressionConstant    = NeutralLanguageConstant.NotConstant;
            resolvedFinalFeature  = null;
            resolvedFinalDiscrete = null;

            IQualifiedName Query = (IQualifiedName)node.Query;

            IClass     EmbeddingClass = (Class)node.EmbeddingClass;
            IClassType BaseType       = EmbeddingClass.ResolvedClassType.Item;

            if (!Expression.IsLanguageTypeAvailable(LanguageClasses.Entity.Guid, node, out ITypeName ResultTypeName, out ICompiledType ResultType))
            {
                errorList.AddError(new ErrorEntityTypeMissing(node));
                return(false);
            }

            IList <IIdentifier> ValidPath      = Query.ValidPath.Item;
            IIdentifier         LastIdentifier = ValidPath[ValidPath.Count - 1];
            string ValidText = LastIdentifier.ValidText.Item;

            ISealableDictionary <string, IScopeAttributeFeature> LocalScope = Scope.CurrentScope(node);

            if (!ObjectType.GetQualifiedPathFinalType(EmbeddingClass, BaseType, LocalScope, ValidPath, 0, errorList, out ICompiledFeature FinalFeature, out IDiscrete FinalDiscrete, out ITypeName FinalTypeName, out ICompiledType FinalType, out bool InheritBySideAttribute))
            {
                return(false);
            }

            Guid EntityGuid;

            if (FinalFeature is IFeatureWithEntity AsFeatureWithEntity)
            {
                ObjectType.FillResultPath(EmbeddingClass, BaseType, LocalScope, ValidPath, 0, Query.ValidResultTypePath.Item);
                resolvedFinalFeature = FinalFeature;
                EntityGuid           = AsFeatureWithEntity.EntityGuid;

                expressionConstant = new EntityLanguageConstant(AsFeatureWithEntity);
            }
            else
            {
                Debug.Assert(FinalDiscrete != null);

                resolvedFinalDiscrete = FinalDiscrete;
                EntityGuid            = LanguageClasses.NamedFeatureEntity.Guid;

                expressionConstant = new EntityLanguageConstant(resolvedFinalDiscrete);
            }

            ITypeName     EntityTypeName = EmbeddingClass.ImportedLanguageTypeTable[EntityGuid].Item1;
            ICompiledType EntityType     = EmbeddingClass.ImportedLanguageTypeTable[EntityGuid].Item2;

            resolvedResult    = new ResultType(EntityTypeName, EntityType, ValidText);
            resolvedException = new ResultException();

#if COVERAGE
            Debug.Assert(!node.IsComplex);
#endif

            return(true);
        }
Example #29
0
        public void NewWithMessage()
        {
            var error = new ResultException("Message context");

            Assert.Equal("Message context", error.Message);
        }
Example #30
0
 public void New()
 {
     _ = new ResultException();
 }