/// <summary> /// Create replaced tokens for all matched tokens /// (a ReplacedToken references both the original token and the replacement token) /// </summary> private Token CreateReplacedTokens(Token originalToken, ReplaceOperation replaceOperation, IList <Token> originalMatchingTokens) { switch (replaceOperation.Type) { // One comparison token => zero or one replacement token case ReplaceOperationType.SingleToken: SingleTokenReplaceOperation singleTokenReplaceOperation = (SingleTokenReplaceOperation)replaceOperation; if (singleTokenReplaceOperation.ReplacementToken != null) { ReplacedToken replacedToken = new ReplacedToken(singleTokenReplaceOperation.ReplacementToken, originalToken); return(replacedToken); } else { return(null); } // One pure partial word => one replacement token case ReplaceOperationType.PartialWord: PartialWordReplaceOperation partialWordReplaceOperation = (PartialWordReplaceOperation)replaceOperation; string originalTokenText = originalToken.Text; string partToReplace = partialWordReplaceOperation.ComparisonToken.Text; //#258 - PartialReplacementToken can be null. In this case, we consider that it's an empty replacement var replacementPart = partialWordReplaceOperation.PartialReplacementToken != null ? partialWordReplaceOperation.PartialReplacementToken.Text : ""; // The index below is always >= 0 because CompareForReplace() above was true int indexOfPartToReplace = originalTokenText.IndexOf(partToReplace, StringComparison.OrdinalIgnoreCase); string replacedTokenText = (indexOfPartToReplace > 0 ? originalTokenText.Substring(0, indexOfPartToReplace) : String.Empty) + replacementPart + ((indexOfPartToReplace + partToReplace.Length) < (originalTokenText.Length) ? originalTokenText.Substring(indexOfPartToReplace + partToReplace.Length) : String.Empty); // TO DO : find a way to transfer the scanner context the of original token to the call below Diagnostic error = null; Token generatedToken = Scanner.Scanner.ScanIsolatedTokenInDefaultContext(replacedTokenText, out error); // TO DO : find a way to report the error above ... if (originalToken.PreviousTokenType != null) //In case orignal token was previously an other type of token reset it back to it's orignal type. { generatedToken.TokenType = originalToken.PreviousTokenType.Value; } ReplacedPartialCobolWord replacedPartialCobolWord = new ReplacedPartialCobolWord(generatedToken, partialWordReplaceOperation.PartialReplacementToken, originalToken); return(replacedPartialCobolWord); // One comparison token => more than one replacement tokens case ReplaceOperationType.SingleToMultipleTokens: SingleToMultipleTokensReplaceOperation singleToMultipleTokensReplaceOperation = (SingleToMultipleTokensReplaceOperation)replaceOperation; currentPosition.ReplacementTokensBeingReturned = new Token[singleToMultipleTokensReplaceOperation.ReplacementTokens.Length]; int i = 0; foreach (Token replacementToken in singleToMultipleTokensReplaceOperation.ReplacementTokens) { currentPosition.ReplacementTokensBeingReturned[i] = new ReplacedToken(replacementToken, originalToken); i++; } currentPosition.ReplacementTokenIndexLastReturned = 0; return(currentPosition.ReplacementTokensBeingReturned[currentPosition.ReplacementTokenIndexLastReturned]); // One first + several following comparison tokens => zero to many replacement tokens //case ReplaceOperationType.MultipleTokens: default: MultipleTokensReplaceOperation multipleTokensReplaceOperation = (MultipleTokensReplaceOperation)replaceOperation; if (multipleTokensReplaceOperation.ReplacementTokens != null) { if (multipleTokensReplaceOperation.ReplacementTokens.Length == 1) { ReplacedTokenGroup replacedTokenGroup = new ReplacedTokenGroup(multipleTokensReplaceOperation.ReplacementTokens[0], originalMatchingTokens); return(replacedTokenGroup); } else { currentPosition.ReplacementTokensBeingReturned = new Token[multipleTokensReplaceOperation.ReplacementTokens.Length]; i = 0; foreach (Token replacementToken in multipleTokensReplaceOperation.ReplacementTokens) { currentPosition.ReplacementTokensBeingReturned[i] = new ReplacedTokenGroup(replacementToken, originalMatchingTokens); i++; } currentPosition.ReplacementTokenIndexLastReturned = 0; return(currentPosition.ReplacementTokensBeingReturned[currentPosition.ReplacementTokenIndexLastReturned]); } } else { return(null); } } }
/// <summary> /// Create replaced tokens for all matched tokens /// (a ReplacedToken references both the original token and the replacement token) /// </summary> private Token CreateReplacedTokens(Token originalToken, ReplaceOperation replaceOperation, IList<Token> originalMatchingTokens) { switch (replaceOperation.Type) { // One comparison token => zero or one replacement token case ReplaceOperationType.SingleToken: SingleTokenReplaceOperation singleTokenReplaceOperation = (SingleTokenReplaceOperation)replaceOperation; if (singleTokenReplaceOperation.ReplacementToken != null) { ReplacedToken replacedToken = new ReplacedToken(singleTokenReplaceOperation.ReplacementToken, originalToken); return replacedToken; } else { return null; } // One pure partial word => one replacement token case ReplaceOperationType.PartialWord: PartialWordReplaceOperation partialWordReplaceOperation = (PartialWordReplaceOperation)replaceOperation; string originalTokenText = originalToken.Text; string partToReplace = partialWordReplaceOperation.ComparisonToken.Text; //#258 - PartialReplacementToken can be null. In this case, we consider that it's an empty replacement var replacementPart = partialWordReplaceOperation.PartialReplacementToken != null ? partialWordReplaceOperation.PartialReplacementToken.Text : ""; // The index below is always >= 0 because CompareForReplace() above was true int indexOfPartToReplace = originalTokenText.IndexOf(partToReplace, StringComparison.OrdinalIgnoreCase); string replacedTokenText = (indexOfPartToReplace > 0 ? originalTokenText.Substring(0, indexOfPartToReplace) : String.Empty) + replacementPart + ((indexOfPartToReplace + partToReplace.Length) < (originalTokenText.Length - 1) ? originalTokenText.Substring(indexOfPartToReplace + partToReplace.Length) : String.Empty); // TO DO : find a way to transfer the scanner context the of original token to the call below Diagnostic error = null; Token generatedToken = Scanner.Scanner.ScanIsolatedTokenInDefaultContext(replacedTokenText, out error); // TO DO : find a way to report the error above ... ReplacedPartialCobolWord replacedPartialCobolWord = new ReplacedPartialCobolWord(generatedToken, partialWordReplaceOperation.PartialReplacementToken, originalToken); return replacedPartialCobolWord; // One comparison token => more than one replacement tokens case ReplaceOperationType.SingleToMultipleTokens: SingleToMultipleTokensReplaceOperation singleToMultipleTokensReplaceOperation = (SingleToMultipleTokensReplaceOperation)replaceOperation; currentPosition.ReplacementTokensBeingReturned = new Token[singleToMultipleTokensReplaceOperation.ReplacementTokens.Length]; int i = 0; foreach (Token replacementToken in singleToMultipleTokensReplaceOperation.ReplacementTokens) { currentPosition.ReplacementTokensBeingReturned[i] = new ReplacedToken(replacementToken, originalToken); i++; } currentPosition.ReplacementTokenIndexLastReturned = 0; return currentPosition.ReplacementTokensBeingReturned[currentPosition.ReplacementTokenIndexLastReturned]; // One first + several following comparison tokens => zero to many replacement tokens //case ReplaceOperationType.MultipleTokens: default: MultipleTokensReplaceOperation multipleTokensReplaceOperation = (MultipleTokensReplaceOperation)replaceOperation; if (multipleTokensReplaceOperation.ReplacementTokens != null) { if (multipleTokensReplaceOperation.ReplacementTokens.Length == 1) { ReplacedTokenGroup replacedTokenGroup = new ReplacedTokenGroup(multipleTokensReplaceOperation.ReplacementTokens[0], originalMatchingTokens); return replacedTokenGroup; } else { currentPosition.ReplacementTokensBeingReturned = new Token[multipleTokensReplaceOperation.ReplacementTokens.Length]; i = 0; foreach (Token replacementToken in multipleTokensReplaceOperation.ReplacementTokens) { currentPosition.ReplacementTokensBeingReturned[i] = new ReplacedTokenGroup(replacementToken, originalMatchingTokens); i++; } currentPosition.ReplacementTokenIndexLastReturned = 0; return currentPosition.ReplacementTokensBeingReturned[currentPosition.ReplacementTokenIndexLastReturned]; } } else { return null; } } }