Exemple #1
0
        private MultiDictionary <string, IConceptParser> CreateGenericParsers()
        {
            var stopwatch = Stopwatch.StartNew();

            var conceptMetadata = _conceptInfoPlugins
                                  .Select(conceptInfo => conceptInfo.GetType())
                                  .Distinct()
                                  .Select(conceptInfoType => new
            {
                conceptType    = conceptInfoType,
                conceptKeyword = ConceptInfoHelper.GetKeyword(conceptInfoType)
            })
                                  .Where(cm => cm.conceptKeyword != null)
                                  .ToList();

            _keywordsLogger.Trace(() => string.Join(" ", conceptMetadata.Select(cm => cm.conceptKeyword).OrderBy(keyword => keyword).Distinct()));

            var result = conceptMetadata.ToMultiDictionary(x => x.conceptKeyword, x =>
            {
                var parser           = new GenericParser(x.conceptType, x.conceptKeyword);
                parser.OnMemberRead += _onMemberRead;
                return((IConceptParser)parser);
            }, StringComparer.OrdinalIgnoreCase);

            _performanceLogger.Write(stopwatch, "CreateGenericParsers.");
            return(result);
        }
Exemple #2
0
        private static (int Level, bool ConcreteParent) GetNestingDepth(ConceptType conceptType)
        {
            int  level = 0;
            bool parentIsConcreteConceptType = true;

            var processed = new HashSet <ConceptType>();

            while (true)
            {
                var parentProperty = GenericParser.GetParentProperty(conceptType.Members);
                if (parentProperty == null)
                {
                    break;
                }
                level += 1;
                if (parentProperty.ConceptType == null) // For example, IConceptInfo.
                {
                    parentIsConcreteConceptType = false;
                    break;
                }

                processed.Add(conceptType);
                if (processed.Contains(parentProperty.ConceptType))
                {
                    break; // Avoid infinite loop when analyzing recursive concepts.
                }
                conceptType = parentProperty.ConceptType;
            }

            return(level, parentIsConcreteConceptType);
        }
Exemple #3
0
        public MultiDictionary <string, IConceptParser> CreateGenericParsers(List <ConceptType> conceptTypes)
        {
            var stopwatch = Stopwatch.StartNew();

            var parsableConcepts = conceptTypes
                                   .Where(c => c.Keyword != null)
                                   .ToList();

            var parsers = parsableConcepts.ToMultiDictionary(
                concept => concept.Keyword,
                concept =>
            {
                var parser           = new GenericParser(concept);
                parser.OnMemberRead += OnMemberRead;
                return((IConceptParser)parser);
            },
                StringComparer.OrdinalIgnoreCase);

            _performanceLogger.Write(stopwatch, "CreateGenericParsers.");

            _keywordsLogger.Trace(() => string.Join(" ", parsers.Select(p => p.Key).OrderBy(keyword => keyword)));

            return(parsers);
        }
Exemple #4
0
        public ValueOrError<object> ReadMemberValue(ConceptMember member, ITokenReader tokenReader, IConceptInfo lastConcept,
            bool firstMember, ref bool lastPropertyWasInlineParent, ref bool lastConceptUsed, bool readingAReference)
        {
            try
            {
                if (lastPropertyWasInlineParent && member.IsKey && !member.IsConceptInfo) // TODO: Removing "IsConceptInfo" from this condition would produce a mismatch. Think of a better solution for parsing the concept key.
                {
                    if (!tokenReader.TryRead("."))
                        return ValueOrError<object>.CreateError(string.Format(
                            "Parent property and the following key value ({0}) must be separated with a dot. Expected \".\"",
                            member.Name));
                }
                lastPropertyWasInlineParent = false;

                if (member.IsStringType)
                    return tokenReader.ReadText();

                if (member.ValueType == typeof(IConceptInfo))
                    if (firstMember && lastConcept != null)
                    {
                        lastConceptUsed = true;
                        return (object)lastConcept;
                    }
                    else
                        return ValueOrError<object>.CreateError("Member of type IConceptInfo can only be used as a first member and enclosed within the referenced parent concept.");

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept)
                         && member.ValueType.IsAssignableFrom(ConceptInfoType)) // Recursive "parent" property
                {
                    lastConceptUsed = true;
                    return (object)lastConcept;
                }

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept)
                         && firstMember)
                {
                    lastConceptUsed = true;
                    return (object)lastConcept;
                }

                if (member.IsConceptInfo && firstMember)
                {
                    if (member.ValueType == ConceptInfoType)
                        return ValueOrError.CreateError(string.Format(
                            "Recursive concept {0} cannot be used as a root because its parent property ({1}) must reference another concept. Use a non-recursive concept for the root and a derivation of the root concept with additional parent property as a recursive concept.",
                            ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType), member.Name));

                    if (!readingAReference && Members.Where(m => m.IsParsable).Count() == 1)
                    {
                        // This validation is not necessary for consistent parsing. It is enforced simply to avoid ambiguity when parsing
                        // similar concepts such as "Logging { AllProperties; }", "History { AllProperties; }" and "Persisted { AllProperties; }".

                        var parentMembers = ConceptMembers.Get(member.ValueType).Where(m => m.IsParsable).ToArray();
                        if (parentMembers.Count() == 1 && parentMembers.Single().IsConceptInfo)
                            return ValueOrError.CreateError(string.Format(
                                "{0} must be enclosed within the referenced parent concept {1}. A single-reference concept that references another single-reference concept must always be used with embedded syntax to avoid ambiguity.",
                                ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType),
                                ConceptInfoHelper.GetKeywordOrTypeName(member.ValueType)));
                    }

                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    lastConceptUsed = true;
                    lastPropertyWasInlineParent = true;
                    return subParser.ParseMembers(tokenReader, lastConcept, true).ChangeType<object>();
                }

                if (member.IsConceptInfo)
                {
                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    return subParser.ParseMembers(tokenReader, null, true).ChangeType<object>();
                }

                return ValueOrError.CreateError(string.Format(
                    "GenericParser does not support members of type \"{0}\". Try using string or implementation of IConceptInfo.",
                    member.ValueType.Name));
            }
            catch (DslSyntaxException ex)
            {
                return ValueOrError<object>.CreateError(ex.Message);
            }
        }
        private ValueOrError <object> ReadMemberValue(ConceptMemberSyntax member, ITokenReader tokenReader, ConceptSyntaxNode useLastConcept,
                                                      bool firstMember, ref bool parsedFirstReferenceElement, bool readingAReference)
        {
            try
            {
                if (member.IsStringType)
                {
                    if (useLastConcept != null)
                    {
                        return(ValueOrError <object> .CreateError($"This concept cannot be nested within {useLastConcept.Concept.TypeName}. Trying to read {_conceptType.TypeName}."));
                    }

                    if (readingAReference && parsedFirstReferenceElement)
                    {
                        if (!tokenReader.TryRead("."))
                        {
                            return(ValueOrError <object> .CreateError(string.Format(
                                                                          "Parent property and the following key value ({0}) must be separated with a dot. Expected \".\"",
                                                                          member.Name)));
                        }
                    }

                    parsedFirstReferenceElement = true;

                    // Legacy syntax:
                    if (!readingAReference && member.IsKey && member.IsStringType && !firstMember)
                    {
                        if (tokenReader.TryRead("."))
                        {
                            AddWarning($"Obsolete syntax: Remove '.' from {_conceptType.Keyword} statement. {((TokenReader)tokenReader).ReportPosition()}.");
                        }
                    }

                    return(tokenReader.ReadText().ChangeType <object>());
                }

                if (member.IsConceptInfoInterface)
                {
                    if (useLastConcept != null)
                    {
                        return((object)useLastConcept);
                    }
                    else
                    {
                        return(ValueOrError <object> .CreateError($"Member of type IConceptInfo can only be nested within" +
                                                                  $" the referenced parent concept. It must be a first member or marked with {nameof(ConceptParentAttribute)}."));
                    }
                }

                // If the 'member' is not IsStringType (see above) and not IsConceptInfoInterface (see above), then it is a concrete IConceptInfo implementation type, and member.ConceptType should not be null.
                if (member.ConceptType == null)
                {
                    throw new InvalidOperationException($"If the member type is not string or interface, it should have a ConceptType value. Member '{member.Name}' while reading {_conceptType.TypeName}.");
                }

                if (useLastConcept != null && member.ConceptType.IsInstanceOfType(useLastConcept))
                {
                    return((object)useLastConcept);
                }

                if (member.IsConceptInfo && firstMember && !readingAReference && _conceptType.Members.Count(m => m.IsParsable) == 1)
                {
                    // This validation is not necessary for consistent parsing. It is enforced simply to avoid ambiguity for future concept overloads
                    // when parsing similar concepts such as "Logging { AllProperties; }", "History { AllProperties; }" and "Persisted { AllProperties; }".
                    var parentMembers = member.ConceptType.Members.Where(m => m.IsParsable).ToArray();
                    if (parentMembers.Length == 1 && parentMembers.Single().IsConceptInfo)
                    {
                        return(ValueOrError.CreateError($"{_conceptType.GetKeywordOrTypeName()} must be nested" +
                                                        $" within the referenced parent concept {member.ConceptType.GetKeywordOrTypeName()}." +
                                                        $" A single-reference concept that references another single-reference concept must always be used with nested syntax to avoid ambiguity."));
                    }
                }

                if (member.IsConceptInfo)
                {
                    if (firstMember && member.ConceptType == _conceptType)
                    {
                        return(ValueOrError.CreateError(string.Format(
                                                            "Recursive concept '{0}' cannot be used as a root because its parent property ({1}) must reference another concept." +
                                                            " Use a non-recursive concept for the root and a derivation of the root concept with additional parent property as a recursive concept.",
                                                            _conceptType.TypeName, member.Name)));
                    }

                    var subParser = new GenericParser(member.ConceptType);
                    return(subParser.ParseMembers(tokenReader, useLastConcept, true, ref parsedFirstReferenceElement).ChangeType <object>());
                }

                return(ValueOrError.CreateError(string.Format(
                                                    "GenericParser does not support members of type \"{0}\". Try using string or implementation of IConceptInfo.",
                                                    member.ConceptType.TypeName)));
            }
            catch (DslSyntaxException ex)
            {
                return(ValueOrError <object> .CreateError(ex.Message));
            }
        }
Exemple #6
0
        public ValueOrError <object> ReadMemberValue(ConceptMember member, ITokenReader tokenReader, IConceptInfo lastConcept,
                                                     bool firstMember, ref bool lastPropertyWasInlineParent, ref bool lastConceptUsed, bool readingAReference)
        {
            try
            {
                if (lastPropertyWasInlineParent && member.IsKey && !member.IsConceptInfo) // TODO: Removing "IsConceptInfo" from this condition would produce a mismatch. Think of a better solution for parsing the concept key.
                {
                    if (!tokenReader.TryRead("."))
                    {
                        return(ValueOrError <object> .CreateError(string.Format(
                                                                      "Parent property and the following key value ({0}) must be separated with a dot. Expected \".\"",
                                                                      member.Name)));
                    }
                }
                lastPropertyWasInlineParent = false;

                if (member.IsStringType)
                {
                    return(tokenReader.ReadText().ChangeType <object>());
                }

                if (member.ValueType == typeof(IConceptInfo))
                {
                    if (firstMember && lastConcept != null)
                    {
                        lastConceptUsed = true;
                        return((object)lastConcept);
                    }
                    else
                    {
                        return(ValueOrError <object> .CreateError("Member of type IConceptInfo can only be used as a first member and enclosed within the referenced parent concept."));
                    }
                }

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept) &&
                    member.ValueType.IsAssignableFrom(ConceptInfoType))         // Recursive "parent" property
                {
                    lastConceptUsed = true;
                    return((object)lastConcept);
                }

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept) &&
                    firstMember)
                {
                    lastConceptUsed = true;
                    return((object)lastConcept);
                }

                if (member.IsConceptInfo && firstMember)
                {
                    if (member.ValueType == ConceptInfoType)
                    {
                        return(ValueOrError.CreateError(string.Format(
                                                            "Recursive concept {0} cannot be used as a root because its parent property ({1}) must reference another concept. Use a non-recursive concept for the root and a derivation of the root concept with additional parent property as a recursive concept.",
                                                            ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType), member.Name)));
                    }

                    if (!readingAReference && Members.Where(m => m.IsParsable).Count() == 1)
                    {
                        // This validation is not necessary for consistent parsing. It is enforced simply to avoid ambiguity when parsing
                        // similar concepts such as "Logging { AllProperties; }", "History { AllProperties; }" and "Persisted { AllProperties; }".

                        var parentMembers = ConceptMembers.Get(member.ValueType).Where(m => m.IsParsable).ToArray();
                        if (parentMembers.Count() == 1 && parentMembers.Single().IsConceptInfo)
                        {
                            return(ValueOrError.CreateError(string.Format(
                                                                "{0} must be enclosed within the referenced parent concept {1}. A single-reference concept that references another single-reference concept must always be used with embedded syntax to avoid ambiguity.",
                                                                ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType),
                                                                ConceptInfoHelper.GetKeywordOrTypeName(member.ValueType))));
                        }
                    }

                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    lastConceptUsed             = true;
                    lastPropertyWasInlineParent = true;
                    return(subParser.ParseMembers(tokenReader, lastConcept, true).ChangeType <object>());
                }

                if (member.IsConceptInfo)
                {
                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    return(subParser.ParseMembers(tokenReader, null, true).ChangeType <object>());
                }

                return(ValueOrError.CreateError(string.Format(
                                                    "GenericParser does not support members of type \"{0}\". Try using string or implementation of IConceptInfo.",
                                                    member.ValueType.Name)));
            }
            catch (DslSyntaxException ex)
            {
                return(ValueOrError <object> .CreateError(ex.Message));
            }
        }