示例#1
0
        private void OnMemberRead(ITokenReader iTokenReader, ConceptSyntaxNode conceptInfo, ConceptMemberSyntax conceptMember, ValueOrError <object> valueOrError)
        {
            // have we reached a new keyword after target pos? if so, prevent further member parsing
            if (result.NextKeywordToken != null)
            {
                return;
            }

            var tokenReader = (TokenReader)iTokenReader;

            if (tokenReader.PositionInTokenList <= 0 || lastTokenBeforeTarget == null)
            {
                return;
            }

            var concept       = conceptInfo.Concept;
            var lastTokenRead = result.Tokens[tokenReader.PositionInTokenList - 1];

            // track last tokens/members parsed before or at target
            if (lastTokenRead.PositionInDslScript <= lastTokenBeforeTarget.PositionInDslScript && !valueOrError.IsError)
            {
                result.LastTokenParsed[concept]       = lastTokenRead;
                result.LastMemberReadAttempt[concept] = conceptMember;
            }

            // we are interested in those concepts whose member parsing stops at or after target position
            if (lastTokenRead.PositionInDslScript >= lastTokenBeforeTarget.PositionInDslScript && !result.ActiveConceptValidTypes.Contains(concept))
            {
                result.ActiveConceptValidTypes.Add(concept);
            }
        }
示例#2
0
        public TConceptInfo QuickParse(string dsl, Stack <IConceptInfo> context)
        {
            var contextNodes = new Stack <ConceptSyntaxNode>(context
                                                             .Select(ci => DslSyntax.CreateConceptSyntaxNode(ci)).Reverse());

            tokenReader = GenericParserTest.TestTokenReader(dsl);
            ConceptSyntaxNode node = GenericParser.Parse(tokenReader, contextNodes, out var warnings).Value;

            return((TConceptInfo)ConceptInfoHelper.ConvertNodeToConceptInfo(node));
        }
示例#3
0
 public ValueOrError <ConceptSyntaxNode> Parse(ITokenReader tokenReader, Stack <ConceptSyntaxNode> context, out List <string> warnings)
 {
     warnings = null;
     if (tokenReader.ReadText().Value == Keyword)
     {
         if (tokenReader.TryRead("-"))
         {
             var node = new ConceptSyntaxNode(new ConceptType());
             node.Parameters[0] = "";
             node.Parameters[1] = "";
         }
         else
         {
             return(ValueOrError.CreateError(ErrorMessage));
         }
     }
     return(ValueOrError <ConceptSyntaxNode> .CreateError(""));
 }
示例#4
0
        public static ConceptSyntaxNode CreateConceptSyntaxNode(this DslSyntax dslSyntax, IConceptInfo ci)
        {
            var conceptInfoType = ci.GetType();
            var node            = new ConceptSyntaxNode(dslSyntax.GetConceptType(conceptInfoType));
            var members         = ConceptMembers.Get(conceptInfoType);

            if (node.Parameters.Length != members.Length)
            {
                throw new InvalidOperationException(
                          $"{nameof(ConceptSyntaxNode)} parameters count ({node.Parameters.Length})" +
                          $" does not match {nameof(ConceptMembers)} count ({members.Length}).");
            }

            for (int m = 0; m < members.Length; m++)
            {
                object value = members[m].GetValue(ci);

                node.Parameters[m] = value;

                if (value == null)
                {
                    node.Parameters[m] = null;
                }
                else if (value is string)
                {
                    node.Parameters[m] = value;
                }
                else if (value is IConceptInfo referencedConceptInfo)
                {
                    var referencedNode = CreateConceptSyntaxNode(dslSyntax, referencedConceptInfo);
                    node.Parameters[m] = referencedNode;
                }
                else
                {
                    var valueType = value?.GetType().Name ?? "null";
                    throw new ArgumentException($"Value type {valueType} is not expected in '{ci.GetUserDescription()}', parameter {members[m].Name}.");
                }
            }

            return(node);
        }
示例#5
0
        public void ConceptTypeIsInstanceOfType()
        {
            var types  = new[] { typeof(SimpleConceptInfo), typeof(DerivedConceptInfo), typeof(Derived2ConceptInfo), typeof(RefConceptInfo) };
            var syntax = DslSyntaxHelper.CreateDslSyntax(types);

            var conceptTypes = types.Select(t => syntax.ConceptTypes.Single(ct => ct.TypeName == t.Name)).ToList();

            var report = new List <string>();

            foreach (var baseType in conceptTypes)
            {
                foreach (var derivedType in conceptTypes)
                {
                    var derivedNode = new ConceptSyntaxNode(derivedType);
                    report.Add($"{baseType.TypeName}.IsInstanceOfType({derivedType.TypeName} node) = {baseType.IsInstanceOfType(derivedNode)}");
                }
            }

            string expected = @"
SimpleConceptInfo.IsInstanceOfType(SimpleConceptInfo node) = True
SimpleConceptInfo.IsInstanceOfType(DerivedConceptInfo node) = True
SimpleConceptInfo.IsInstanceOfType(Derived2ConceptInfo node) = True
SimpleConceptInfo.IsInstanceOfType(RefConceptInfo node) = False
DerivedConceptInfo.IsInstanceOfType(SimpleConceptInfo node) = False
DerivedConceptInfo.IsInstanceOfType(DerivedConceptInfo node) = True
DerivedConceptInfo.IsInstanceOfType(Derived2ConceptInfo node) = True
DerivedConceptInfo.IsInstanceOfType(RefConceptInfo node) = False
Derived2ConceptInfo.IsInstanceOfType(SimpleConceptInfo node) = False
Derived2ConceptInfo.IsInstanceOfType(DerivedConceptInfo node) = False
Derived2ConceptInfo.IsInstanceOfType(Derived2ConceptInfo node) = True
Derived2ConceptInfo.IsInstanceOfType(RefConceptInfo node) = False
RefConceptInfo.IsInstanceOfType(SimpleConceptInfo node) = False
RefConceptInfo.IsInstanceOfType(DerivedConceptInfo node) = False
RefConceptInfo.IsInstanceOfType(Derived2ConceptInfo node) = False
RefConceptInfo.IsInstanceOfType(RefConceptInfo node) = True
";

            Assert.AreEqual(expected.Trim(), string.Join("\r\n", report));
        }