Пример #1
0
        public void SimpleTest()
        {
            // Arrange
            var input       = new AntlrInputStream(new StringReader(@"
module X

entity A {
    a : A
    b : B
}

entity B {}"));
            var termFactory = new ATermFactory(new NullTermCache(), new Dictionary <Type, ATermFactory.TermConstructor>
            {
                [typeof(IModuleTerm)]   = (f, s, a) => new ModuleATerm(f, (IStringTerm)s[0], (IListTerm <IEntityTerm>)s[1], a),
                [typeof(IEntityTerm)]   = (f, s, a) => new EntityATerm(f, (IStringTerm)s[0], (IListTerm <IPropertyTerm>)s[1], a),
                [typeof(IPropertyTerm)] = (f, s, a) => new PropertyATerm(f, (IStringTerm)s[0], (ITypeTerm)s[1], a),
                [typeof(ITypeTerm)]     = (f, s, a) => new TypeATerm(f, (IStringTerm)s[0], a),
            });
            var        lexer  = new EntityLanguageLexer(input);
            var        tokens = new CommonTokenStream(lexer);
            var        parser = new EntityLanguageParser(tokens);
            IParseTree tree   = parser.start();

            // Act
            var ast = new AstBuilder(termFactory).Visit(tree);

            // Assert
            var expectedAst =
                termFactory.Create <IModuleTerm>(
                    termFactory.String("X"),
                    termFactory.List(
                        termFactory.Create <IEntityTerm>(
                            termFactory.String("A"),
                            termFactory.List(
                                termFactory.Create <IPropertyTerm>(
                                    termFactory.String("a"),
                                    termFactory.Create <ITypeTerm>(termFactory.String("A"))
                                    ),
                                termFactory.Create <IPropertyTerm>(
                                    termFactory.String("b"),
                                    termFactory.Create <ITypeTerm>(termFactory.String("B"))
                                    )
                                )
                            ),
                        termFactory.Create <IEntityTerm>(
                            termFactory.String("B"),
                            termFactory.List <IPropertyTerm>()
                            )
                        )
                    );

            Assert.Equal(expectedAst, ast);
        }
        public void SimpleTest()
        {
            // Arrange
            var input       = new AntlrInputStream(new StringReader(@"
module X

entity A {
    a : A
    b : B
}

entity B {}"));
            var termFactory = new ATermFactory(new NullTermCache(), new Dictionary <Type, ATermFactory.TermConstructor>
            {
                [typeof(IModuleTerm)]   = (f, s, a) => new ModuleATerm(f, (IStringTerm)s[0], (IListTerm <IEntityTerm>)s[1], a),
                [typeof(IEntityTerm)]   = (f, s, a) => new EntityATerm(f, (IStringTerm)s[0], (IListTerm <IPropertyTerm>)s[1], a),
                [typeof(IPropertyTerm)] = (f, s, a) => new PropertyATerm(f, (IStringTerm)s[0], (ITypeTerm)s[1], a),
                [typeof(ITypeTerm)]     = (f, s, a) => new TypeATerm(f, (IStringTerm)s[0], a),
            });
            var        lexer  = new EntityLanguageLexer(input);
            var        tokens = new CommonTokenStream(lexer);
            var        parser = new EntityLanguageParser(tokens);
            IParseTree tree   = parser.start();
            var        ast    = new AstBuilder(termFactory).Visit(tree);

            // Act
            var result = new CodeGenerator(termFactory).Generate(ast);

            // Assert
            var expectedString           = @"using System;

namespace Entities
{
	public class A
	{
		public A a;
		public B b;
	}
	public class B
	{
	}
}
";
            var normalizedExpectedString = Regex.Replace(expectedString, @"\r\n|\n", Environment.NewLine);

            Assert.Equal(normalizedExpectedString, result.Value);
        }
Пример #3
0
        public void Test1()
        {
            // Assert
            var input  = new AntlrInputStream(new StringReader(@"
module X

entity A {
    a : A
    b : B
}

entity B {}"));
            var lexer  = new EntityLanguageLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new EntityLanguageParser(tokens);

            // Act
            IParseTree tree = parser.start();

            // Assert
            string s = tree.ToStringTree(parser);

            Assert.Equal("(start module X (definition entity A { (property a : (type A)) (property b : (type B)) }) (definition entity B { }))", s);
        }