コード例 #1
0
 /// <summary>
 /// Visit method which is invoked when visitor operates on SyntaxTree
 /// </summary>
 /// <param name="statement">The Syntax Tree</param>
 public void Visit(SyntaxTree statement)
 {
     foreach (var s in statement.Statements)
     {
         s.Accept(this);
     }
 }
コード例 #2
0
 /// <summary>
 /// Visit method which is invoked when visitor operates on SyntaxTree
 /// The indent is increased for each syntax tree.
 /// Writes start and end of the method to the console.
 /// </summary>
 /// <param name="statement">The Syntax Tree</param>
 public void Visit(SyntaxTree statement)
 {
     Write("Start visit SyntaxTree");
     _indent++;
     foreach (var s in statement.Statements)
     {
         s.Accept(this);
     }
     _indent--;
     Write("End visit SyntaxTree");
 }
コード例 #3
0
ファイル: VisitorTest.cs プロジェクト: tstune/designpattern
        public void Visitor_Should_FindAllValueStatements()
        {
            // Arrange
            var firstValue = "1";
            var fifthValue = "5";

            // Create first statement
            var valueStatement1a = new ValueStatement(firstValue);
            var valueStatement1b = new ValueStatement("2");
            var binaryStatement1 = new BinaryStatement(valueStatement1a, valueStatement1b);

            // Create second statement (nested)
            var valueStatement2a = new ValueStatement("3");
            var valueStatement22a = new ValueStatement("4");
            var valueStatement22b = new ValueStatement(fifthValue);
            var binaryStatement22 = new BinaryStatement(valueStatement22a, valueStatement22b);
            var binaryStatement2 = new BinaryStatement(valueStatement2a, binaryStatement22);

            var syntaxTree = new SyntaxTree(new[] { binaryStatement1, binaryStatement2 });

            var result = new List<string>();
            var syntaxTreeValueVisitor = new SyntaxTreeValueVisitor();
            syntaxTreeValueVisitor.VisitResultEvent += (sender, e) =>
            {
                Trace.WriteLine(string.Format("Level {0}, Value Expression {1}", e.Depth, e.Value));
                result.Add(e.Value);
            };

            // Act
            // Run through the syntax tree with the simple value visitor
            Trace.WriteLine("Start SyntaxTreeValueVisitor");
            syntaxTree.Accept(syntaxTreeValueVisitor);

            Trace.WriteLine("End SyntaxTreeValueVisitor");
            Trace.WriteLine(string.Empty);

            // Run through the syntax tree with the indent visitor
            Trace.WriteLine("Start SyntaxTreeIndendedVisitor");
            syntaxTree.Accept(new SyntaxTreeIndentVisitor());
            Trace.WriteLine("End SyntaxTreeIndendedVisitor");

            // Assert
            Assert.AreEqual(5, result.Count);
            Assert.AreEqual(firstValue, result[0]);
            Assert.AreEqual(fifthValue, result[4]);
        }