示例#1
0
        public void TwoSectionDefinition()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"line1
@section sec
line2
@end_section
@section sec2
line3
line4
@end_section
");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst     ast      = parserIntruder.ReadField <AtTemplateAst>("ast");
            SectionDefinition section1 = (SectionDefinition)ast.Body.Directives[1];
            SectionDefinition section2 = (SectionDefinition)ast.Body.Directives[2];

            Assert.That(section1.Name, Is.EqualTo("sec"));
            Assert.That(section1.Directives.Count, Is.EqualTo(1));

            Assert.That(section2.Name, Is.EqualTo("sec2"));
            Assert.That(section2.Directives.Count, Is.EqualTo(2));
        }
示例#2
0
        public void SectionRefWithParameters()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"class A {
    @+ Fields(fields)
}
@section Fields(f: string[])
@code
    foreach (var field in f)
    {
        @text
        private string @(field);
        @end_text
    }
@end_code

@end_section");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst    ast        = parserIntruder.ReadField <AtTemplateAst>("ast");
            SectionReference sectionRef = (SectionReference)ast.Body.Directives[1];

            Assert.That(sectionRef.Name, Is.EqualTo("Fields"));
            Assert.That(sectionRef.Rest, Is.EqualTo("(fields)"));
        }
示例#3
0
        public void Head()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"@# comment
@reference System.Windows.Forms.dll
@import System.Windows.Forms
@param name: string
@
@global
string firstName;
string lastName;
@end_global");
            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(ast.Head.References.Count, Is.EqualTo(3));
            Assert.That(ast.Head.Imports.Count, Is.EqualTo(1));
            Assert.That(ast.Head.Parameters.Count, Is.EqualTo(1));
            Assert.That(ast.Head.Global.Length, Is.GreaterThan(0));
        }
示例#4
0
        public void Indentation()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"{
    @code
        int i = 0;
        @text
        123
        @end_text
    @end_code
}");
            parser.Debug = true;

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            SourceGenerator generator = new SourceGenerator();

            generator.Ast       = parserIntruder.ReadField <AtTemplateAst>("ast");
            generator.Debugging = true;
            generator.Walk();

            Console.WriteLine(generator.SourceCode);
        }
示例#5
0
        public void BadSectionDefinition(string code)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(code);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");
        }
示例#6
0
        public void Execution()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader("@! int a = 1;");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(((Execution)ast.Body.Directives[0]).Statement, Is.EqualTo(" int a = 1;"));
        }
示例#7
0
        public void StaticTexts(string text, string expected)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(text);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(((StaticText)ast.Body.Directives[0]).Value, Is.EqualTo(expected));
        }
示例#8
0
        public void Evaluations(string text, string expected)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(text);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(((Evaluation)ast.Body.Directives[1]).Expression, Is.EqualTo(expected));
        }
示例#9
0
        public void Between(string text)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(text);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(ast.Body.Directives[1] is Between);
        }
示例#10
0
        public void Text(string text)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(text);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(
                ((Code)ast.Body.Directives[0]).Directives[0] is Cavingdeep.Dcg.At.Parsing.Text);
        }
示例#11
0
        public EvaluationDirectiveParser(
            int index,
            int length,
            string sourceCode,
            int line,
            AtParser parser)
        {
            Debug.Assert(!string.IsNullOrEmpty(sourceCode));
            Debug.Assert(parser != null);

            this.index = index;
            this.length = length;
            this.sourceCode = sourceCode;
            this.line = line;
            this.parser = parser;
        }
示例#12
0
        public EvaluationDirectiveParser(
            int index,
            int length,
            string sourceCode,
            int line,
            AtParser parser)
        {
            Debug.Assert(!string.IsNullOrEmpty(sourceCode));
            Debug.Assert(parser != null);

            this.index      = index;
            this.length     = length;
            this.sourceCode = sourceCode;
            this.line       = line;
            this.parser     = parser;
        }
示例#13
0
        public void WithParameters(string code, int paramCount)
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(code);

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst     ast     = parserIntruder.ReadField <AtTemplateAst>("ast");
            SectionDefinition section = (SectionDefinition)ast.Body.Directives[0];

            Assert.That(section.Name, Is.EqualTo("Foo"));
            Assert.That(section.Parameters.Count, Is.EqualTo(paramCount));
        }
示例#14
0
        public void BadSectionRef()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"class A {
    @+ 12Fields
}
@section Fields
line1
@end_section");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");
        }
示例#15
0
        public void Output()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(@"@output key
123
@end_output");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(((Output)ast.Body.Directives[0]).WriterKey, Is.EqualTo("key"));
        }
示例#16
0
        public void NonTopLevelSectionDefinition()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"@code
    @text
    @section sec
    line2
    @end_section
    @end_text
@end_code");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");
        }
示例#17
0
        public void MultiEvaluation()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(@"@global
    string Foo() { return ""1" + "\r\n" + @"2""; }
@end_global
    @= Foo()");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(((MultiLineEvaluation)ast.Body.Directives[0]).Expression, Is.EqualTo(" Foo()"));
            Assert.That(((MultiLineEvaluation)ast.Body.Directives[0]).Spaces, Is.EqualTo("    "));
        }
示例#18
0
        public void TwoSectionDefinition_WithSameName()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"line1
@section sec
line2
@end_section
@section sec
line3
@end_section
");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");
        }
示例#19
0
        public void First()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"@# comment
@param name: string
@param age: int
@
Hello @(name), @{
    if (age >= 18)
    {
        @text
        you are adult.
        @end_text
    }
@}");
            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(ast.Head.Parameters.Count, Is.EqualTo(2));
            Assert.That(ast.Head.Parameters[0].Name, Is.EqualTo("name"));
            Assert.That(ast.Head.Parameters[0].Type, Is.EqualTo("string"));
            Assert.That(ast.Head.Parameters[1].Name, Is.EqualTo("age"));
            Assert.That(ast.Head.Parameters[1].Type, Is.EqualTo("int"));

            Assert.That(((StaticText)ast.Body.Directives[0]).Value, Is.EqualTo("Hello "));
            Assert.That(((Evaluation)ast.Body.Directives[1]).Expression, Is.EqualTo("name"));
            Assert.That(((StaticText)ast.Body.Directives[2]).Value, Is.EqualTo(", "));
            Assert.That(((DynamicText)((Between)ast.Body.Directives[3]).Directives[0]).Value, Is.EqualTo("    if (age >= 18)"));
            Assert.That(((DynamicText)((Between)ast.Body.Directives[3]).Directives[1]).Value, Is.EqualTo("    {"));
            Assert.That(((StaticText)((Cavingdeep.Dcg.At.Parsing.Text)((Between)ast.Body.Directives[3]).Directives[2]).Directives[0]).Value, Is.EqualTo("you are adult.\r\n"));
            Assert.That(((DynamicText)((Between)ast.Body.Directives[3]).Directives[3]).Value, Is.EqualTo("    }"));
            Assert.That(((StaticText)ast.Body.Directives[4]).Value, Is.EqualTo("\r\n"));
        }
示例#20
0
        public void SectionRefWithNoParameters()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(
                @"class A {
    @+ Fields  
}
@section Fields
line1
@end_section");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst    ast        = parserIntruder.ReadField <AtTemplateAst>("ast");
            SectionReference sectionRef = (SectionReference)ast.Body.Directives[1];

            Assert.That(sectionRef.Name, Is.EqualTo("Fields"));
        }
示例#21
0
        public void InnerDirectives()
        {
            AtParser parser = new AtParser();

            parser.Reader = new StringReader(@"@# comment
@code
    @text
        @code
        @end_code
    @end_text
@end_code");

            Intruder parserIntruder = new Intruder(parser);

            parserIntruder.CallMethod <object>("InitParsing");
            parserIntruder.CallMethod <object>("BuildAst");

            AtTemplateAst ast = parserIntruder.ReadField <AtTemplateAst>("ast");

            Assert.That(
                ((Cavingdeep.Dcg.At.Parsing.Text)((Code)ast.Body.Directives[0]).Directives[0]).Directives[0] is Code);
        }