예제 #1
0
        public void ParseNextConcept_SameKeyWordDifferentContext_Enclosed()
        {
            string dsl = "concept name data { concept ref; }";

            var syntax             = DslSyntaxHelper.CreateDslSyntax(typeof(SimpleConceptInfo), typeof(EnclosedRefConceptInfo));
            var conceptParsersList = new List <IConceptParser>()
            {
                new GenericParserHelper <SimpleConceptInfo>(syntax, "concept"),
                new GenericParserHelper <EnclosedRefConceptInfo>(syntax, "concept")
            };
            var conceptParsers = new MultiDictionary <string, IConceptParser>();

            conceptParsers.Add("concept", conceptParsersList);

            var          context     = new Stack <IConceptInfo>();
            TokenReader  tokenReader = TestTokenReader(dsl);
            IConceptInfo concept     = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, context, conceptParsers);

            Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType());
            Assert.AreEqual("name", (concept as SimpleConceptInfo).Name);
            Assert.AreEqual("data", (concept as SimpleConceptInfo).Data);

            Assert.IsTrue(tokenReader.TryRead("{"), "Reading '{' between concepts.");

            context.Push(concept);
            concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, context, conceptParsers);
            Assert.AreEqual(typeof(EnclosedRefConceptInfo), concept.GetType());
            Assert.AreEqual("ref", (concept as EnclosedRefConceptInfo).Name);
            Assert.AreEqual("name", (concept as EnclosedRefConceptInfo).Reference.Name);
            Assert.AreEqual("data", (concept as EnclosedRefConceptInfo).Reference.Data);
        }
예제 #2
0
        public void ParseNextConcept_SameKeyWordDifferentContext()
        {
            string dsl = "concept simple simpledata; concept ext extdata extdata2;";

            var syntax             = DslSyntaxHelper.CreateDslSyntax(typeof(SimpleConceptInfo), typeof(ExtendedConceptInfo));
            var conceptParsersList = new List <IConceptParser>()
            {
                new GenericParserHelper <SimpleConceptInfo>(syntax, "concept"),
                new GenericParserHelper <ExtendedConceptInfo>(syntax, "concept")
            };
            var conceptParsers = new MultiDictionary <string, IConceptParser>();

            conceptParsers.Add("concept", conceptParsersList);

            var          noContext   = new Stack <IConceptInfo>();
            TokenReader  tokenReader = TestTokenReader(dsl);
            IConceptInfo concept     = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, noContext, conceptParsers);

            Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType());
            Assert.AreEqual("simple", (concept as SimpleConceptInfo).Name);
            Assert.AreEqual("simpledata", (concept as SimpleConceptInfo).Data);

            Assert.IsTrue(tokenReader.TryRead(";"), "Reading ';' between concepts.");

            concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, noContext, conceptParsers);
            Assert.AreEqual(typeof(ExtendedConceptInfo), concept.GetType());
            Assert.AreEqual("ext", (concept as ExtendedConceptInfo).Name);
            Assert.AreEqual("extdata", (concept as ExtendedConceptInfo).Data);
            Assert.AreEqual("extdata2", (concept as ExtendedConceptInfo).Data2);

            Assert.IsTrue(tokenReader.TryRead(";"), "Reading ';' after the concept.");
        }
예제 #3
0
        private static IEnumerable <ConceptSyntaxNode> DslParserParse(string[] dslScripts, Type[] conceptTypes)
        {
            var dslParser = new DslParser(
                new TestTokenizer(dslScripts),
                new Lazy <DslSyntax>(() => DslSyntaxHelper.CreateDslSyntax(conceptTypes)),
                new ConsoleLogProvider());
            var parsedConcepts = dslParser.GetConcepts();

            Console.WriteLine("Parsed concepts: " + string.Join("\r\n", parsedConcepts.Select(c => c.Concept.TypeName)));
            return(parsedConcepts);
        }
예제 #4
0
        public void InterfaceReference_Enclosed()
        {
            var parent = new SimpleConceptInfo {
                Name = "parent"
            };
            string dsl           = "intref data";
            var    syntax        = DslSyntaxHelper.CreateDslSyntax(typeof(InterfaceReferenceConceptInfo), typeof(SimpleConceptInfo));
            var    parsedConcept = new GenericParserHelper <InterfaceReferenceConceptInfo>(syntax, "intref").QuickParse(dsl, parent);

            Assert.AreEqual(typeof(SimpleConceptInfo), parsedConcept.Referece.GetType());
            Assert.AreEqual("parent", ((SimpleConceptInfo)parsedConcept.Referece).Name);
            Assert.AreEqual("data", parsedConcept.Data);
        }
예제 #5
0
        static List <IConceptInfo> DslModelFromScript(string dsl, IConceptInfo[] conceptInfoPluginsForGenericParser)
        {
            var nullDslParser = new DslParser(
                new TestTokenizer(dsl),
                new Lazy <DslSyntax>(() => DslSyntaxHelper.CreateDslSyntax(conceptInfoPluginsForGenericParser)),
                new ConsoleLogProvider());

            Console.WriteLine("Parsed concepts:");
            Console.WriteLine(string.Join(Environment.NewLine, nullDslParser.GetConcepts().Select(ci => " - " + ci.GetUserDescription())));

            var dslModel = NewDslModel(nullDslParser, conceptInfoPluginsForGenericParser);

            return(dslModel.Concepts.ToList());
        }
예제 #6
0
        public void Recursive_EnclosedNode()
        {
            ExtendedRootMenuCI root = new ExtendedRootMenuCI {
                Name = "R", RootData = "SomeData"
            };

            string dsl = "menu M";

            var       syntax = DslSyntaxHelper.CreateDslSyntax(typeof(MenuCI), typeof(ExtendedRootMenuCI), typeof(SubMenuCI));
            SubMenuCI mi     = new GenericParserHelper <SubMenuCI>(syntax, "menu").QuickParse(dsl, root);

            Assert.AreEqual("M", mi.Name);
            Assert.AreEqual("R", mi.Parent.Name);
            Assert.AreEqual("SomeData", ((ExtendedRootMenuCI)mi.Parent).RootData);
        }
예제 #7
0
        public void ParseEnclosedInWrongConcept()
        {
            var syntax = DslSyntaxHelper.CreateDslSyntax(typeof(EnclosedConceptInfo), typeof(ComplexConceptInfo));
            var parser = new GenericParserHelper <EnclosedConceptInfo>(syntax, "enclosed");

            TestUtility.ShouldFail <InvalidOperationException>(
                () => parser.QuickParse(
                    "enclosed myparent.myname",
                    new ComplexConceptInfo {
                Name = "c", SimpleConceptInfo = new SimpleConceptInfo {
                    Name = "s"
                }
            }),
                "EnclosedConceptInfo",
                "ComplexConceptInfo");
        }
예제 #8
0
        public void ParseNextConcept_SameKeyWordDifferentContext_Ambiguous()
        {
            string dsl = "concept simple data; concept ref simple;";

            var syntax             = DslSyntaxHelper.CreateDslSyntax(typeof(SimpleConceptInfo), typeof(RefConceptInfo));
            var conceptParsersList = new List <IConceptParser>()
            {
                new GenericParserHelper <SimpleConceptInfo>(syntax, "concept"),
                new GenericParserHelper <RefConceptInfo>(syntax, "concept")
            };
            var conceptParsers = new MultiDictionary <string, IConceptParser>();

            conceptParsers.Add("concept", conceptParsersList);

            var         noContext   = new Stack <IConceptInfo>();
            TokenReader tokenReader = TestTokenReader(dsl);

            TestUtility.ShouldFail <DslSyntaxException>(
                () => new TestDslParser(dsl).ParseNextConcept(tokenReader, noContext, conceptParsers),
                // Possible interpretations:
                "SimpleConceptInfo", "RefConceptInfo");
        }
예제 #9
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));
        }
예제 #10
0
            public StubDslParser(IEnumerable <IConceptInfo> rawConcepts)
            {
                var syntax = DslSyntaxHelper.CreateDslSyntax(rawConcepts.ToArray());

                ParsedConcepts = rawConcepts.Select(ci => syntax.CreateConceptSyntaxNode(ci));
            }
 public TestDslParser(string dsl, IConceptInfo[] conceptInfoPlugins)
     : this(dsl, DslSyntaxHelper.CreateDslSyntax(conceptInfoPlugins))
 {
 }
예제 #12
0
 public GenericParserHelper(string overrideKeyword = null)
     : this(DslSyntaxHelper.CreateDslSyntax(typeof(TConceptInfo)), overrideKeyword)
 {
 }