PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Rhino.Etl.Dsl/Macros/AbstractChildMacro.cs

http://github.com/ayende/rhino-etl
C# | 89 lines | 52 code | 12 blank | 25 comment | 7 complexity | bf88712f9dd9d884a2476a49fa93bcd2 MD5 | raw file
 1namespace Rhino.Etl.Dsl.Macros
 2{
 3    using System;
 4    using System.Collections.Generic;
 5    using Boo.Lang.Compiler;
 6    using Boo.Lang.Compiler.Ast;
 7
 8    /// <summary>
 9    /// Base class for child macros, also handle validating the parents of a macro
10    /// </summary>
11    public abstract class AbstractChildMacro : AbstractAstMacro
12    {
13        private readonly string[] allowedParents;
14        /// <summary>
15        /// The parent macro statement
16        /// </summary>
17        protected MacroStatement parent;
18
19        /// <summary>
20        /// Initializes a new instance of the <see cref="AbstractChildMacro"/> class.
21        /// </summary>
22        /// <param name="allowedParents">The allowed parents.</param>
23        public AbstractChildMacro(params string[] allowedParents)
24        {
25            this.allowedParents = allowedParents;
26        }
27
28        /// <summary>
29        /// Expands the specified macro, validate that the parent is a valid parent, 
30        /// leave the actual processing to a base class
31        /// </summary>
32        /// <param name="macro">The macro.</param>
33        /// <returns></returns>
34        public override Statement Expand(MacroStatement macro)
35        {
36            if (ValidateParent(macro, out parent) == false)
37                return null;
38
39            return DoExpand(macro);
40        }
41
42        /// <summary>
43        /// Perform the actual expansion of the macro
44        /// </summary>
45        /// <param name="macro">The macro.</param>
46        /// <returns></returns>
47        protected abstract Statement DoExpand(MacroStatement macro);
48
49        private bool ValidateParent(MacroStatement macro, out MacroStatement parent)
50        {
51            parent = macro.ParentNode.ParentNode as MacroStatement;
52
53            if (parent != null)
54            {
55                foreach (string validParent in allowedParents)
56                {
57                    if (parent.Name.Equals(validParent, StringComparison.InvariantCultureIgnoreCase))
58                    {
59                        return true;
60                    }
61                }
62            }
63
64            string msg = string.Format("A {0} statement can appear only under a {1}",
65                                       macro.Name,
66                                       string.Join(" | ", allowedParents));
67            Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, msg));
68
69            return false;
70        }
71
72
73        /// <summary>
74        /// Gets the parent methods collection
75        /// </summary>
76        /// <value>The parent methods.</value>
77        protected IList<Method> ParentMethods
78        {
79            get
80            {
81                IList<Method> members = (IList<Method>)parent["members"];
82                if (members == null)
83                    parent["members"] = members = new List<Method>();
84                return members;
85
86            }
87        }
88    }
89}