예제 #1
0
파일: SectionTest.cs 프로젝트: anthrax3/dcg
        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
파일: SectionTest.cs 프로젝트: anthrax3/dcg
        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 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);
        }
예제 #5
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;"));
        }
예제 #6
0
        public void SingleStaticText()
        {
            AtTemplateAst ast = new AtTemplateAst();

            ast.Head.Parameters.Add(new Parameter("myName", "string"));
            ast.Body.Directives.Add(new StaticText("Hello World!\r\n", 2));

            SourceGenerator generator = new SourceGenerator();

            generator.Ast       = ast;
            generator.Debugging = true;
            generator.Walk();

            Console.WriteLine(generator.SourceCode);
        }
예제 #7
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));
        }
예제 #8
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));
        }
예제 #9
0
 public void Parse()
 {
     try
     {
         InitParsing();
         BuildAst();
         WalkAst();
         this.references = this.ast.Head.References
                           .ConvertAll(reference => reference.Value)
                           .ToArray();
     }
     finally
     {
         this.ast = null;
     }
 }
예제 #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 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"));
        }
예제 #12
0
파일: SectionTest.cs 프로젝트: anthrax3/dcg
        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));
        }
예제 #13
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("    "));
        }
예제 #14
0
        private void InitParsing()
        {
            this.ast = new AtTemplateAst();

            this.ast.Head.References.Add(new Reference("System.dll"));
            this.ast.Head.References.Add(new Reference(Assembly.GetExecutingAssembly().Location));

            this.currentDirective = this.ast.Body;

            if (this.outputStack == null)
            {
                this.outputStack = new Stack <OutputContext>();
            }
            else
            {
                this.outputStack.Clear();
            }

            if (this.contextStack == null)
            {
                this.contextStack = new ContextStack();
            }
            else
            {
                this.contextStack.Clear();
            }

            if (this.sections == null)
            {
                this.sections = new Dictionary <string, object>();
            }
            else
            {
                this.sections.Clear();
            }

            this.contextStack.Push(new Context(TemplateMode.Static, string.Empty));

            this.lineCount       = 0;
            this.isInGlobalBlock = false;
        }
예제 #15
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"));
        }
예제 #16
0
파일: SectionTest.cs 프로젝트: anthrax3/dcg
        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"));
        }
예제 #17
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);
        }