Code element for conditional preprocessor directives.
상속: CommentedElement
예제 #1
0
        /// <summary>
        /// Creates an instance for cloning.
        /// </summary>
        /// <returns>Clone of the code element.</returns>
        protected override CodeElement DoClone()
        {
            ConditionDirectiveElement clone = new ConditionDirectiveElement();

            if (_elseCondition != null)
            {
                ConditionDirectiveElement elseClone = _elseCondition.Clone() as ConditionDirectiveElement;
                clone._elseCondition = elseClone;
            }

            return(clone);
        }
예제 #2
0
        public void GetAttributeTypeTest()
        {
            FieldElement fieldElement = new FieldElement();
            fieldElement.Name = "TestField";
            fieldElement.Access = CodeAccess.Protected;
            fieldElement.Type = "int";

            string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, fieldElement);
            Assert.AreEqual("int", attribute, "Unexpected attribute.");

            TypeElement typeElement = new TypeElement();
            typeElement.Type = TypeElementType.Interface;

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, typeElement);
            Assert.AreEqual("Interface", attribute, "Unexpected attribute.");

            CommentElement commentElement = new CommentElement(CommentType.Block);

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, commentElement);
            Assert.AreEqual("Block", attribute, "Unexpected attribute.");

            UsingElement usingElement = new UsingElement();
            usingElement.Name = "MySystem";
            usingElement.Redefine = "System";

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, usingElement);
            Assert.AreEqual("Alias", attribute, "Unexpected attribute.");

            ConditionDirectiveElement conditionElement = new ConditionDirectiveElement();

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, conditionElement);
            Assert.AreEqual(string.Empty, attribute, "Unexpected attribute.");
        }
예제 #3
0
        /// <summary>
        /// Parses a condition directive.
        /// </summary>
        /// <param name="line">The line to process.</param>
        /// <param name="isIf">Whether or not the directive is an if condition.</param>
        /// <returns>Condition directive code element.</returns>
        private ConditionDirectiveElement ParseConditionDirective(string line, out bool isIf)
        {
            int separatorIndex = line.IndexOfAny(WhiteSpaceCharacters);

            string directive = null;
            if (separatorIndex > 0)
            {
                directive = line.Substring(0, separatorIndex);
            }
            else
            {
                directive = line;
            }

            directive = VBKeyword.Normalize(directive);

            string condition = null;
            if (separatorIndex > 0)
            {
                condition = line.Substring(separatorIndex + 1).Trim();
            }

            isIf = directive == VBKeyword.If;

            switch (directive)
            {
                case VBKeyword.If:
                case VBKeyword.ElseIf:
                    if (string.IsNullOrEmpty(condition))
                    {
                        this.OnParseError("Expected a condition expression");
                    }
                    break;

                case VBKeyword.Else:
                    break;

                default:
                    this.OnParseError(
                        string.Format(
                        CultureInfo.InvariantCulture,
                        "Unhandled preprocessor directive '{0}'",
                        directive));
                    break;
            }

            ConditionDirectiveElement conditionDirective = new ConditionDirectiveElement();
            conditionDirective.ConditionExpression = condition;

            return conditionDirective;
        }
예제 #4
0
 /// <summary>
 /// Processes a condition directive element.
 /// </summary>
 /// <param name="element">Condition directive code element.</param>
 public abstract void VisitConditionDirectiveElement(ConditionDirectiveElement element);
예제 #5
0
        /// <summary>
        /// Writes a condition directive element.
        /// </summary>
        /// <param name="element">Condition directive code element.</param>
        public override void VisitConditionDirectiveElement(ConditionDirectiveElement element)
        {
            const string ConditionFormat = "{0}{1} {2}";
            ConditionDirectiveElement conditionDirective = element;

            this.WriteIndentedLine(
                string.Format(
                CultureInfo.InvariantCulture,
                ConditionFormat,
                VBSymbol.Preprocessor,
                VBKeyword.If,
                element.ConditionExpression));

            if (element.Children.Count == 0)
            {
                Writer.WriteLine();
            }

            WriteChildren(element);

            if (element.Children.Count > 0)
            {
                Writer.WriteLine();
            }

            while (conditionDirective.ElseCondition != null)
            {
                conditionDirective = conditionDirective.ElseCondition;

                if (conditionDirective.ElseCondition == null)
                {
                    this.WriteIndentedLine(VBSymbol.Preprocessor + VBKeyword.Else);
                }
                else
                {
                    this.WriteIndentedLine(
                        string.Format(
                        CultureInfo.InvariantCulture,
                        ConditionFormat,
                        VBSymbol.Preprocessor,
                        VBKeyword.ElseIf,
                        conditionDirective.ConditionExpression));
                }

                if (conditionDirective.Children.Count == 0)
                {
                    Writer.WriteLine();
                }

                WriteChildren(conditionDirective);

                if (conditionDirective.Children.Count > 0)
                {
                    Writer.WriteLine();
                }
            }

            this.WriteIndented(VBSymbol.Preprocessor + VBKeyword.End + " " + VBKeyword.If);
        }
        /// <summary>
        /// Creates an instance for cloning.
        /// </summary>
        /// <returns>Clone of the code element.</returns>
        protected override CodeElement DoClone()
        {
            ConditionDirectiveElement clone = new ConditionDirectiveElement();

            if (_elseCondition != null)
            {
                ConditionDirectiveElement elseClone = _elseCondition.Clone() as ConditionDirectiveElement;
                clone._elseCondition = elseClone;
            }

            return clone;
        }
예제 #7
0
        public void DefaultArrangeConditionDirectiveTest()
        {
            List<ICodeElement> codeElements = new List<ICodeElement>();

            ConditionDirectiveElement ifCondition = new ConditionDirectiveElement();
            ifCondition.ConditionExpression = "DEBUG";

            FieldElement field1 = new FieldElement();
            field1.Name = "zField";
            field1.Type = "int";

            FieldElement field2 = new FieldElement();
            field2.Name = "aField";
            field2.Type = "int";

            ifCondition.AddChild(field1);
            ifCondition.AddChild(field2);

            ifCondition.ElseCondition = new ConditionDirectiveElement();

            FieldElement field3 = new FieldElement();
            field3.Name = "testField";
            field3.Type = "int";

            FieldElement field1Clone = field1.Clone() as FieldElement;
            FieldElement field2Clone = field2.Clone() as FieldElement;

            TypeElement classElement = new TypeElement();
            classElement.Name = "TestClass";
            classElement.AddChild(field1Clone);
            classElement.AddChild(field2Clone);

            ifCondition.ElseCondition.AddChild(field3);
            ifCondition.ElseCondition.AddChild(classElement);

            codeElements.Add(ifCondition);

            CodeArranger arranger = new CodeArranger(CodeConfiguration.Default);

            ReadOnlyCollection<ICodeElement> arranged =
                arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned.");
            ConditionDirectiveElement ifConditionTest = arranged[0] as ConditionDirectiveElement;
            Assert.IsNotNull(ifConditionTest, "Expected a condition directive element.");

            Assert.AreEqual(2, ifConditionTest.Children.Count,
                "After arranging, an unexpected number of nested elements were returned.");
            Assert.AreEqual(field2.Name, ifConditionTest.Children[0].Name);
            Assert.AreEqual(field1.Name, ifConditionTest.Children[1].Name);

            ConditionDirectiveElement elseConditionTest = ifConditionTest.ElseCondition;
            Assert.IsNotNull(elseConditionTest, "Expected a condition directive element.");
            Assert.AreEqual(2, ifConditionTest.Children.Count,
                "After arranging, an unexpected number of nested elements were returned.");
            Assert.AreEqual(field3.Name, elseConditionTest.Children[0].Name);
            Assert.AreEqual(classElement.Name, elseConditionTest.Children[1].Name);

            TypeElement classElementTest = elseConditionTest.Children[1] as TypeElement;
            Assert.IsNotNull(classElementTest, "Expected a type element.");
            Assert.AreEqual(1, classElementTest.Children.Count);
            Assert.AreEqual("Fields", classElementTest.Children[0].Name);
        }