Exemplo n.º 1
0
        public virtual void InsertBefore(string programName, int index, object text)
        {
            TokenRewriteStream.RewriteOperation         rewriteOperation = (TokenRewriteStream.RewriteOperation) new TokenRewriteStream.InsertBeforeOp(this, index, text);
            IList <TokenRewriteStream.RewriteOperation> program          = this.GetProgram(programName);

            rewriteOperation.instructionIndex = program.Count;
            program.Add(rewriteOperation);
        }
Exemplo n.º 2
0
        public virtual void Replace(string programName, int from, int to, object text)
        {
            if (from > to || from < 0 || (to < 0 || to >= this._tokens.Count))
            {
                throw new ArgumentException("replace: range invalid: " + (object)from + ".." + (object)to + "(size=" + (object)this._tokens.Count + ")");
            }
            TokenRewriteStream.RewriteOperation         rewriteOperation = (TokenRewriteStream.RewriteOperation) new TokenRewriteStream.ReplaceOp(this, from, to, text);
            IList <TokenRewriteStream.RewriteOperation> program          = this.GetProgram(programName);

            rewriteOperation.instructionIndex = program.Count;
            program.Add(rewriteOperation);
        }
Exemplo n.º 3
0
        protected virtual IList <TokenRewriteStream.RewriteOperation> GetKindOfOps(
            IList <TokenRewriteStream.RewriteOperation> rewrites,
            Type kind,
            int before)
        {
            IList <TokenRewriteStream.RewriteOperation> rewriteOperationList = (IList <TokenRewriteStream.RewriteOperation>) new List <TokenRewriteStream.RewriteOperation>();

            for (int index = 0; index < before && index < rewrites.Count; ++index)
            {
                TokenRewriteStream.RewriteOperation rewrite = rewrites[index];
                if (rewrite != null && rewrite.GetType() == kind)
                {
                    rewriteOperationList.Add(rewrite);
                }
            }
            return(rewriteOperationList);
        }
Exemplo n.º 4
0
        protected virtual IDictionary <int, TokenRewriteStream.RewriteOperation> ReduceToSingleOperationPerIndex(
            IList <TokenRewriteStream.RewriteOperation> rewrites)
        {
            for (int before = 0; before < rewrites.Count; ++before)
            {
                switch (rewrites[before])
                {
                case TokenRewriteStream.ReplaceOp _:
                    TokenRewriteStream.ReplaceOp rewrite = (TokenRewriteStream.ReplaceOp)rewrites[before];
                    IList <TokenRewriteStream.RewriteOperation> kindOfOps1 = this.GetKindOfOps(rewrites, typeof(TokenRewriteStream.InsertBeforeOp), before);
                    for (int index = 0; index < kindOfOps1.Count; ++index)
                    {
                        TokenRewriteStream.InsertBeforeOp insertBeforeOp = (TokenRewriteStream.InsertBeforeOp)kindOfOps1[index];
                        if (insertBeforeOp.index == rewrite.index)
                        {
                            rewrites[insertBeforeOp.instructionIndex] = (TokenRewriteStream.RewriteOperation)null;
                            rewrite.text = (object)(insertBeforeOp.text.ToString() + (rewrite.text != null ? rewrite.text.ToString() : string.Empty));
                        }
                        else if (insertBeforeOp.index > rewrite.index && insertBeforeOp.index <= rewrite.lastIndex)
                        {
                            rewrites[insertBeforeOp.instructionIndex] = (TokenRewriteStream.RewriteOperation)null;
                        }
                    }
                    IList <TokenRewriteStream.RewriteOperation> kindOfOps2 = this.GetKindOfOps(rewrites, typeof(TokenRewriteStream.ReplaceOp), before);
                    for (int index = 0; index < kindOfOps2.Count; ++index)
                    {
                        TokenRewriteStream.ReplaceOp replaceOp = (TokenRewriteStream.ReplaceOp)kindOfOps2[index];
                        if (replaceOp.index >= rewrite.index && replaceOp.lastIndex <= rewrite.lastIndex)
                        {
                            rewrites[replaceOp.instructionIndex] = (TokenRewriteStream.RewriteOperation)null;
                        }
                        else
                        {
                            bool flag1 = replaceOp.lastIndex <rewrite.index || replaceOp.index> rewrite.lastIndex;
                            bool flag2 = replaceOp.index == rewrite.index && replaceOp.lastIndex == rewrite.lastIndex;
                            if (replaceOp.text == null && rewrite.text == null && !flag1)
                            {
                                rewrites[replaceOp.instructionIndex] = (TokenRewriteStream.RewriteOperation)null;
                                rewrite.index     = Math.Min(replaceOp.index, rewrite.index);
                                rewrite.lastIndex = Math.Max(replaceOp.lastIndex, rewrite.lastIndex);
                                Console.WriteLine("new rop " + (object)rewrite);
                            }
                            else if (!flag1 && !flag2)
                            {
                                throw new ArgumentException("replace op boundaries of " + (object)rewrite + " overlap with previous " + (object)replaceOp);
                            }
                        }
                    }
                    break;
                }
            }
            for (int before = 0; before < rewrites.Count; ++before)
            {
                switch (rewrites[before])
                {
                case TokenRewriteStream.InsertBeforeOp _:
                    TokenRewriteStream.InsertBeforeOp           rewrite    = (TokenRewriteStream.InsertBeforeOp)rewrites[before];
                    IList <TokenRewriteStream.RewriteOperation> kindOfOps1 = this.GetKindOfOps(rewrites, typeof(TokenRewriteStream.InsertBeforeOp), before);
                    for (int index = 0; index < kindOfOps1.Count; ++index)
                    {
                        TokenRewriteStream.InsertBeforeOp insertBeforeOp = (TokenRewriteStream.InsertBeforeOp)kindOfOps1[index];
                        if (insertBeforeOp.index == rewrite.index)
                        {
                            rewrite.text = (object)this.CatOpText(rewrite.text, insertBeforeOp.text);
                            rewrites[insertBeforeOp.instructionIndex] = (TokenRewriteStream.RewriteOperation)null;
                        }
                    }
                    IList <TokenRewriteStream.RewriteOperation> kindOfOps2 = this.GetKindOfOps(rewrites, typeof(TokenRewriteStream.ReplaceOp), before);
                    for (int index = 0; index < kindOfOps2.Count; ++index)
                    {
                        TokenRewriteStream.ReplaceOp replaceOp = (TokenRewriteStream.ReplaceOp)kindOfOps2[index];
                        if (rewrite.index == replaceOp.index)
                        {
                            replaceOp.text   = (object)this.CatOpText(rewrite.text, replaceOp.text);
                            rewrites[before] = (TokenRewriteStream.RewriteOperation)null;
                        }
                        else if (rewrite.index >= replaceOp.index && rewrite.index <= replaceOp.lastIndex)
                        {
                            throw new ArgumentException("insert op " + (object)rewrite + " within boundaries of previous " + (object)replaceOp);
                        }
                    }
                    break;
                }
            }
            IDictionary <int, TokenRewriteStream.RewriteOperation> dictionary = (IDictionary <int, TokenRewriteStream.RewriteOperation>) new Dictionary <int, TokenRewriteStream.RewriteOperation>();

            for (int index = 0; index < rewrites.Count; ++index)
            {
                TokenRewriteStream.RewriteOperation rewrite = rewrites[index];
                if (rewrite != null)
                {
                    TokenRewriteStream.RewriteOperation rewriteOperation;
                    if (dictionary.TryGetValue(rewrite.index, out rewriteOperation) && rewriteOperation != null)
                    {
                        throw new Exception("should only be one op per index");
                    }
                    dictionary[rewrite.index] = rewrite;
                }
            }
            return(dictionary);
        }