コード例 #1
0
 public String Print(int startIndent)
 {
     mMultipleBindableItems = false;
     mBindablePos = (-1);
     mBindableMode = false;
     mFirstTokenBeforeFormatIndent = false;
     mNextTokenInElseIfState = false;
     mElseIfNeedToLoseIndent = false;
     mLazyIndentType = (-1);
     mLazyFormatType = (-1);
     mFormatTypeStack = new List<int>();
     if (mDoFormat) PushFormatMode(FORMAT_ALL);
     else PushFormatMode(FORMAT_INDENT);
     mInE4XText = false;
     mSourceData = CodeFormatter.InfoCollector.Utilities.ConvertCarriageReturnsToLineFeeds(mSourceData);
     bool addedEndCRs = false;
     if (GetEndEOLs(mSourceData) < 2)
     {
         mSourceData += "\n\n";
         addedEndCRs = true;
     }
     mAddedCRs = new List<int>();
     mParseErrors = null;
     mIndentStack = new List<IndentType>();
     mIndentStack.Add(new IndentType(INITIAL_INDENT, startIndent));
     mExcludeStackCount = 0;
     mOutputBuffer = new StringBuilder();
     try
     {
         mWorkingSource = mSourceData;
         ANTLRStringStream stream = new ANTLRStringStream(mWorkingSource);
         AS3_exLexer l2 = new AS3_exLexer(stream);
         mRawTokens = new CommonTokenStream(l2);
         AS3_exParser p2 = new AS3_exParser(this, mRawTokens);
         bool isPackageFound=false;
         while (true)
         {
             CommonToken t = (CommonToken)l2.NextToken();
             if (t == null) break;
             if (t.Channel == Token.DEFAULT_CHANNEL)
             {
                 if (t.Type == AS3_exParser.PACKAGE) isPackageFound=true;
                 break;
             }
         }
         l2.Reset();
         p2.TokenStream = mRawTokens;
         if (isPackageFound)
         {
             AS3_exParser.fileContents_return retVal = p2.fileContents();
         }
         else p2.mxmlEmbedded();
         // Handle any remaining hidden tokens
         CommonToken tk = new CommonToken(AS3_exParser.EOF, "");
         tk.TokenIndex = mRawTokens.Count;
         Emit(tk);
         String resultText = mOutputBuffer.ToString();
         if (resultText == null) return null;
         if (ASFormatter.ValidateNonWhitespaceIdentical(resultText, mWorkingSource))
         {
             // Trim extra newlines at the end of result text
             if (mDoFormat)
             {
                 int oldEndLines = GetEndEOLs(mWorkingSource) - (addedEndCRs ? 2 : 0); // I may have added two above
                 int newEndLines = GetEndEOLs(resultText);
                 if (newEndLines > oldEndLines)
                 {
                     resultText = resultText.Substring(0, resultText.Length - (newEndLines - oldEndLines));
                     if (mOutputRange != null && mOutputRange.Y > resultText.Length)
                     {
                         mOutputRange.Y = resultText.Length;
                     }
                 }
             }
             // If we are trying to capture the output range but haven't captured it yet
             if (mOutputRange != null && mOutputRange.Y < 0)
             {
                 mOutputRange.Y = resultText.Length;
                 mReplaceRange.Y = mWorkingSource.Length;
                 if (mDoFormat && addedEndCRs) mReplaceRange.Y -= "\n\n".Length;
             }
             return resultText;
         }
         else
         {
             mParseErrors = new List<Exception>();
             mParseErrors.Add(new Exception("Internal error: Formatted text doesn't match source. " + resultText + "!=" + mWorkingSource));
         }
     }
     finally {}
     return null;
 }