Example #1
0
 public override void Walk(AstVisitor visitor)
 {
     if (visitor.Walk(this))
     {
         m_condition.Walk(visitor);
         if (TrueBlock != null)
         {
             TrueBlock.Walk(visitor);
         }
         if (FalseBlock != null)
         {
             FalseBlock.Walk(visitor);
         }
     }
     visitor.PostWalk(this);
 }
Example #2
0
File: if.cs Project: formist/LinkMe
 internal override bool EncloseBlock(EncloseBlockType type)
 {
     // if there's an else block, recurse down that branch
     if (FalseBlock != null)
     {
         return(FalseBlock.EncloseBlock(type));
     }
     else if (type == EncloseBlockType.IfWithoutElse)
     {
         // there is no else branch -- we might have to enclose the outer block
         return(true);
     }
     else if (TrueBlock != null)
     {
         return(TrueBlock.EncloseBlock(type));
     }
     return(false);
 }
Example #3
0
 internal override bool EncloseBlock(EncloseBlockType type)
 {
     // if there's an else block, recurse down that branch.
     // if we aren't forcing braces and the block contains nothing, then we don't
     // really have a false block.
     if (FalseBlock != null && (FalseBlock.ForceBraces || FalseBlock.Count > 0))
     {
         return(FalseBlock.EncloseBlock(type));
     }
     else if (type == EncloseBlockType.IfWithoutElse)
     {
         // there is no else branch -- we might have to enclose the outer block
         return(true);
     }
     else if (TrueBlock != null)
     {
         return(TrueBlock.EncloseBlock(type));
     }
     return(false);
 }
Example #4
0
File: if.cs Project: formist/LinkMe
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("if(");
            sb.Append(Condition.ToCode());
            sb.Append(')');

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            ToCodeFormat elseFormat = ToCodeFormat.Normal;

            if (FalseBlock != null && FalseBlock.Count == 1)
            {
                if (Parser.Settings.MacSafariQuirks &&
                    FalseBlock[0] is FunctionObject)
                {
                    elseFormat = ToCodeFormat.AlwaysBraces;
                }
                else if (FalseBlock[0] is IfNode)
                {
                    elseFormat = ToCodeFormat.ElseIf;
                }
            }

            // get the else block -- we need to know if there is anything in order
            // to fully determine if the true-branch needs curly-braces
            string elseBlock = (
                FalseBlock == null
                ? string.Empty
                : FalseBlock.ToCode(elseFormat));

            // we'll need to force the true block to be enclosed in curly braces if
            // there is an else block and the true block contains a single statement
            // that ends in an if that doesn't have an else block
            ToCodeFormat trueFormat = (FalseBlock != null &&
                                       TrueBlock != null &&
                                       TrueBlock.EncloseBlock(EncloseBlockType.IfWithoutElse)
                ? ToCodeFormat.AlwaysBraces
                : ToCodeFormat.Normal);

            if (elseBlock.Length > 0 &&
                TrueBlock != null &&
                TrueBlock.EncloseBlock(EncloseBlockType.SingleDoWhile))
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            if (Parser.Settings.MacSafariQuirks &&
                TrueBlock != null &&
                TrueBlock.Count == 1 &&
                TrueBlock[0] is FunctionObject)
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // add the true block
            string trueBlock = (
                TrueBlock == null
                ? string.Empty
                : TrueBlock.ToCode(trueFormat));

            sb.Append(trueBlock);

            if (elseBlock.Length > 0)
            {
                if (trueFormat != ToCodeFormat.AlwaysBraces &&
                    !trueBlock.EndsWith(";", StringComparison.Ordinal) &&
                    (TrueBlock == null || TrueBlock.RequiresSeparator))
                {
                    sb.Append(';');
                }

                // if we are in pretty-print mode, drop the else onto a new line
                Parser.Settings.NewLine(sb);
                sb.Append("else");
                // if the first character could be interpreted as a continuation
                // of the "else" statement, then we need to add a space
                if (JSScanner.StartsWithIdentifierPart(elseBlock))
                {
                    sb.Append(' ');
                }

                sb.Append(elseBlock);
            }
            return(sb.ToString());
        }
Example #5
0
        /// <summary>
        /// Parse all conditions in template
        /// </summary>
        private void ParseConditions()
        {
            int idxPrevious = 0;
            int idxCurrent  = 0;

            this._ParsedBlock = "";
            while ((idxCurrent = this._strTemplateBlock.IndexOf(this.ConditionTagIfBegin, idxCurrent)) != -1)
            {
                string VarName;
                string TrueBlock, FalseBlock;
                string ElseStatment, EndIfStatment;
                int    idxIfBegin, idxIfEnd, idxElseBegin, idxEndIfBegin;
                bool   boolValue;

                idxIfBegin  = idxCurrent;
                idxCurrent += this.ConditionTagIfBegin.Length;

                // Searching for EndIf Index

                idxIfEnd = this._strTemplateBlock.IndexOf(this.ConditionTagIfEnd, idxCurrent);
                if (idxIfEnd == -1)
                {
                    throw new Exception("Could not find ConditionTagIfEnd");
                }

                // Getting Value Name

                VarName = this._strTemplateBlock.Substring(idxCurrent, (idxIfEnd - idxCurrent));

                idxCurrent = idxIfEnd + this.ConditionTagIfEnd.Length;

                // Compare ElseIf and EndIf Indexes

                ElseStatment  = this.ConditionTagElseBegin + VarName + this.ConditionTagElseEnd;
                EndIfStatment = this.ConditionTagEndIfBegin + VarName + this.ConditionTagEndIfEnd;
                idxElseBegin  = this._strTemplateBlock.IndexOf(ElseStatment, idxCurrent);
                idxEndIfBegin = this._strTemplateBlock.IndexOf(EndIfStatment, idxCurrent);
                if (idxElseBegin > idxEndIfBegin)
                {
                    throw new Exception("Condition Else Tag placed after Condition Tag EndIf for '" + VarName + "'");
                }

                // Getting True and False Condition Blocks

                if (idxElseBegin != -1)
                {
                    TrueBlock  = this._strTemplateBlock.Substring(idxCurrent, (idxElseBegin - idxCurrent));
                    FalseBlock = this._strTemplateBlock.Substring((idxElseBegin + ElseStatment.Length), (idxEndIfBegin - idxElseBegin - ElseStatment.Length));
                }
                else
                {
                    TrueBlock  = this._strTemplateBlock.Substring(idxCurrent, (idxEndIfBegin - idxCurrent));
                    FalseBlock = "";
                }

                // Parse Condition

                try
                {
                    boolValue = Convert.ToBoolean(this._hstValues[VarName]);
                }
                catch
                {
                    boolValue = false;
                }

                string BeforeBlock = this._strTemplateBlock.Substring(idxPrevious, (idxIfBegin - idxPrevious));

                if (this._hstValues.ContainsKey(VarName) && boolValue)
                {
                    this._ParsedBlock += BeforeBlock + TrueBlock.Trim();
                }
                else
                {
                    this._ParsedBlock += BeforeBlock + FalseBlock.Trim();
                }

                idxCurrent  = idxEndIfBegin + EndIfStatment.Length;
                idxPrevious = idxCurrent;
            }
            this._ParsedBlock += this._strTemplateBlock.Substring(idxPrevious);
        }