コード例 #1
0
        /// <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);
                }
            }
        }
コード例 #2
0
        private static void BuildReplaceOperation(IList <ReplaceOperation> replaceOperations, ref Token comparisonToken, ref Token[] followingComparisonTokens, ref Token replacementToken, ref Token[] replacementTokens, ref int pseudoTextIndex, IList <IToken> operandTokens)
        {
            // Comparison tokens
            if (pseudoTextIndex % 2 == 0)
            {
                if (operandTokens != null && operandTokens.Count > 0)
                {
                    comparisonToken = (Token)operandTokens[0];
                    if (operandTokens.Count > 1)
                    {
                        followingComparisonTokens = new Token[operandTokens.Count - 1];
                        for (int i = 1; i < operandTokens.Count; i++)
                        {
                            followingComparisonTokens[i - 1] = (Token)operandTokens[i];
                        }
                    }
                }

                pseudoTextIndex++;
            }
            // Replacement tokens
            else
            {
                if (operandTokens != null && operandTokens.Count > 0)
                {
                    if (followingComparisonTokens == null && operandTokens.Count == 1)
                    {
                        replacementToken = (Token)operandTokens[0];
                    }
                    else
                    {
                        replacementTokens = new Token[operandTokens.Count];
                        for (int i = 0; i < operandTokens.Count; i++)
                        {
                            replacementTokens[i] = (Token)operandTokens[i];
                        }
                    }
                }

                // Build replace operation
                ReplaceOperation replaceOperation = null;
                if (followingComparisonTokens == null)
                {
                    if (replacementTokens == null)
                    {
                        if (comparisonToken == null || comparisonToken.TokenType != TokenType.PartialCobolWord)
                        {
                            replaceOperation = new SingleTokenReplaceOperation(comparisonToken, replacementToken);
                        }
                        else
                        {
                            replaceOperation = new PartialWordReplaceOperation(comparisonToken, replacementToken);
                        }
                    }
                    else
                    {
                        replaceOperation = new SingleToMultipleTokensReplaceOperation(comparisonToken, replacementTokens);
                    }
                }
                else
                {
                    replaceOperation = new MultipleTokensReplaceOperation(comparisonToken, followingComparisonTokens, replacementTokens);
                }
                replaceOperations.Add(replaceOperation);

                // Reset everything for the next replace operation
                pseudoTextIndex           = 0;
                comparisonToken           = null;
                followingComparisonTokens = null;
                replacementToken          = null;
                replacementTokens         = null;
            }
        }