Example #1
0
        public override void Validate(TrsReductionRule validationInput)
        {
            if (validationInput.Head is TrsVariable)
            {
                ValidationMessages.Add(new InterpreterResultMessage
                {
                    Message     = "A reduction rule head may not only be a variable.",
                    MessageType = InterpreterMessageType.Error,
                    InputEntity = validationInput
                });
            }

            // All variables in tail must appear on head
            HashSet <TrsVariable> headVariables = new HashSet <TrsVariable>(validationInput.Head.GetVariables());
            HashSet <TrsVariable> tailVariables = new HashSet <TrsVariable>(validationInput.Tail.GetVariables());

            tailVariables.UnionWith(headVariables);
            if (tailVariables.Count != headVariables.Count)
            {
                ValidationMessages.Add(new InterpreterResultMessage
                {
                    Message     = "A reduction rule head must contain all variables that is in the reduction rule tail.",
                    MessageType = InterpreterMessageType.Error,
                    InputEntity = validationInput
                });
            }

            // Head validation
            if (validationInput.Head is TrsTermProduct)
            {
                foreach (var term in ((TrsTermProduct)validationInput.Head).TermList)
                {
                    termValidator.Validate(term);
                }
            }
            else
            {
                termValidator.Validate(validationInput.Head);
            }

            // The tail validation must take the case into account where the "native" keywork have been used
            if (validationInput.Tail.GetType() != typeof(TrsNativeKeyword))
            {
                termValidator.Validate(validationInput.Tail);
            }
            ValidationMessages.AddRange(termValidator.ValidationMessages);
        }
Example #2
0
        /// <summary>
        /// Checks sub-validators
        /// </summary>
        private void ValidateSubValidators(TrsProgramBlock programBlockIn)
        {
            foreach (var statement in programBlockIn.Statements)
            {
                var rule           = statement as TrsReductionRule;
                var term           = statement as TrsTermBase;
                var typeDefinition = statement as TrsTypeDefinition;

                if (rule != null)
                {
                    reductionRuleValidator.Validate(rule);
                }
                else if (term != null)
                {
                    if (term is TrsVariable)
                    {
                        termValidator.ValidationMessages.Add(new InterpreterResultMessage
                        {
                            InputEntity = term,
                            Message     = "A term cannot only be a variable, this would match all rewrite rules excluding those resulting from the occurs check, taking type definitions into account.",
                            MessageType = InterpreterMessageType.Error
                        });
                    }
                    else if (typeDefinition != null)
                    {
                        if (Keywords.NativeTypes.Contains(typeDefinition.Name.TypeName))
                        {
                            termValidator.ValidationMessages.Add(new InterpreterResultMessage
                            {
                                InputEntity = term,
                                Message     = "A type definition name may not be the same as the native type names: "
                                              + Keywords.TrsConstant + " "
                                              + Keywords.TrsNumber + " "
                                              + Keywords.TrsString + " "
                                              + Keywords.TrsVariable,
                                MessageType = InterpreterMessageType.Error
                            });
                        }
                    }
                    else
                    {
                        termValidator.Validate(term);
                    }
                }
            }
            ValidationMessages.AddRange(reductionRuleValidator.ValidationMessages);
            ValidationMessages.AddRange(termValidator.ValidationMessages);
        }