public static bool IsRegexString(String str) { for (int i = 0; i < str.Length; i++) { char c = str.ToCharArray()[i]; if (AS3_exParser.IsIdentifierPart(c)) { continue; } if (c == ':' || c == '_' || c == '-') { continue; } return(true); } return(false); }
public String print(int startIndent) { if (mSourceData.IndexOfOrdinal(mIgnoreFileProcessing)>=0) { mParseErrors=new List<Exception>(); mParseErrors.Add(new Exception("File ignored: Ignore tag exists in file==> "+mIgnoreFileProcessing)); return null; } //mOutputRange = null; //mReplaceRange = null; mAdditionalTextAddedAllPasses=""; mRemovedTextAllPasses=""; int maxPasses=2; //right now, there's only two passes if we are aligning equals signs or adding braces int currentPasses=0; while (currentPasses<maxPasses) { currentPasses++; mAlreadyHaveDeclPositions=(mDeclEqualPosMap!=null); //initialize the parser state variables mBindablePos=(-1); mBindableMode=false; mInSingleLineFunctionMode=false; mAdditionalTextAdded=""; mRemovedText=""; mReplaceMap=null; mNextTokenInElseIfState=false; mElseIfNeedToLoseIndent=false; mLazyIndentType=(-1); mLazyFormatType=(-1); mFormatTypeStack=new List<Int32>(); if (mDoFormat) pushFormatMode(FORMAT_ALL); else pushFormatMode(FORMAT_INDENT); mLastBindableTagName=""; mExpressionWrapData=new List<WrapInfo>(); mSourceData=InfoCollector.Utilities.convertCarriageReturnsToLineFeeds(mSourceData); bool addedEndCRs=false; if (getEndEOLs(mSourceData)<2) { mSourceData+="\n\n"; addedEndCRs=true; } mAddBraceStack=new List<StatementBraceInfo>(); mCompletedBraceInfos=new List<StatementBraceInfo>(); mAddedCRs=new List<Int32>(); mParseErrors=null; mIndentStack=new List<IndentType>(); mIndentStack.Add(new IndentType(INITIAL_INDENT, startIndent)); mExcludeStackCount=0; mLastCapturePosition=0; mOutputBuffer = new StringBuilder(); // mAlignDeclEquals=true; if (mAlignDeclEquals && !mIsKeepingExcessDeclWhitespace)// && currentPasses==0) { mCurrentDeclEqualContext=new EqualContext(null, 0); mRootDeclEqualContext=mCurrentDeclEqualContext; } // else // mCurrentDeclEqualContext=null; try { // if (mIsCompleteFile) mWorkingSource=mSourceData; // else // mWorkingSource="class DummyClassWrapper {"+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); AS3_exParser.fileContents_return retVal=p2.fileContents(); //handle any remaining hidden tokens IToken t = new CommonToken(AS3_exParser.EOF, ""); t.TokenIndex = mRawTokens.Count; emit(t); //process braces that may need to be added or deleted List<EditItem> editItems=new List<EditItem>(); foreach (StatementBraceInfo info in mCompletedBraceInfos) { if (info.isBracesCurrentlyExist()) { if (isRemoveBraces(info)) { int startBracePos = mOutputBuffer.ToString().IndexOf('{', info.getStartBracePos()); int endBracePos = mOutputBuffer.ToString().LastIndexOf('}', info.getEndBracePos()); int endBracePosInSource=mSourceData.LastIndexOf('}', info.getOriginalDocEndPosition()); editItems.Add(new DeleteItem(startBracePos, 1, info.getOriginalDocStartPosition())); editItems.Add(new DeleteItem(endBracePos, 1, endBracePosInSource)); //if brace is followed by nothing but a carriage return, return the next //cr as well. int nextCR = mOutputBuffer.ToString().IndexOf('\n', startBracePos + 1); if (nextCR>=0) { String nextChars = mOutputBuffer.ToString().Substring(startBracePos + 1, nextCR - (startBracePos + 1)).Trim(); if (nextChars.Length==0) { editItems.Add(new DeleteItem(nextCR, 1, -1)); } } nextCR = mOutputBuffer.ToString().IndexOf('\n', endBracePos + 1); if (nextCR>=0) { String nextChars = mOutputBuffer.ToString().Substring(endBracePos + 1, nextCR - (endBracePos + 1)).Trim(); if (nextChars.Length==0) { editItems.Add(new DeleteItem(nextCR, 1, -1)); } } } } else { if (isAddBraces(info)) { editItems.Add(new InsertItem(info.getStartBracePos(), "{", info.getOriginalDocStartPosition())); editItems.Add(new InsertItem(info.getEndBracePos(), "}", info.getOriginalDocEndPosition())); } } } editItems.Sort((o1, o2) => o1.getLocation() - o2.getLocation()); /* Collections.sort(editItems, new IComparer<EditItem>() { public int compare(EditItem o1, EditItem o2) { return o1.mLocation-o2.mLocation; } }); */ if (mReplaceMap==null) mReplaceMap=new Dictionary<Int32, ReplacementRange>(); int addedChars=0; for (int j=0;j<editItems.Count;j++) { EditItem item=editItems[j]; if (item is InsertItem) { InsertItem iItem=(InsertItem)item; mOutputBuffer.Insert(item.getLocation()+addedChars, iItem.getData()); ReplacementRange existingRange=mReplaceMap[iItem.getOriginalInsertLocation()]; if (existingRange!=null) { //this can happen if two close braces are added at the same point in the file. As such, //I'm handling this special case. existingRange.setChangedText(existingRange.getAddedText()+iItem.getData(), existingRange.getDeletedText()); existingRange.mRangeInFormattedDoc.Y += iItem.getData().Length; } else { ReplacementRange range=new ReplacementRange(new Point(item.getLocation()+addedChars, item.getLocation()+addedChars+iItem.getData().Length), new Point(iItem.getOriginalInsertLocation(),iItem.getOriginalInsertLocation())); mReplaceMap[iItem.getOriginalInsertLocation()] = range; range.setChangedText(iItem.getData(), ""); } addedChars+=iItem.getData().Length; mAdditionalTextAdded+=iItem.getData(); mAdditionalTextAddedAllPasses+=iItem.getData(); } else { //add replacemap item, but only if non-whitespace is being deleted //TODO: what if a brace is added and deleted at same location? Will that break the merging code? DeleteItem dItem=(DeleteItem)item; Int32 start = dItem.getLocation() + addedChars; Int32 end = dItem.getLocation() + dItem.getLength() + addedChars; String removedData = mOutputBuffer.ToString().Substring(start, end - start); if (dItem.getOriginalDeleteLocation()>=0) { ReplacementRange range=new ReplacementRange(new Point(item.getLocation()+addedChars, item.getLocation()+addedChars), new Point(dItem.getOriginalDeleteLocation(),dItem.getOriginalDeleteLocation()+dItem.getLength())); mReplaceMap[dItem.getOriginalDeleteLocation()] = range; range.setChangedText("", removedData); } mOutputBuffer.Remove(dItem.getLocation()+addedChars, dItem.getLocation()+dItem.getLength()+addedChars); addedChars-=removedData.Length; mRemovedText+=removedData; mRemovedTextAllPasses+=removedData; } } String resultText=mOutputBuffer.ToString(); mParseErrors=p2.getParseErrors(); if (mParseErrors!=null || resultText==null) return null; String oldString=mWorkingSource+mAdditionalTextAdded; if (mAdditionalTextAdded.Length==0 && mRemovedText.Length==0 && ASFormatter.validateNonWhitespaceIdentical(resultText, mWorkingSource) || ((mAdditionalTextAdded.Length>0 || (mRemovedText.Length>0)) && ASFormatter.validateNonWhitespaceCharCounts(resultText+mRemovedText, oldString))) { //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; } if (mRootDeclEqualContext!=null) { mDeclEqualPosMap=new Dictionary<String, Int32>(); mRootDeclEqualContext.fillMap(mDeclEqualPosMap); } //if multiple passes are allowed and we would benefit from one and this is not a partial format if (mAllowMultiplePasses && needAnotherPass() && mOutputRange==null) { mSourceData=resultText; continue; } return resultText; } else { mParseErrors=new List<Exception>(); mParseErrors.Add(new Exception("Internal error: Formatted text doesn't match source. "+resultText+"!="+mWorkingSource)); } } finally { } return null; } //return what I have in case the multiple //passes algorithm gets confused return mSourceData; }
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; }