Exemple #1
0
        /// <summary>
        /// Generate
        /// </summary>
        /// <param name="compilationUnit"></param>
        /// <param name="columns"></param>
        public void Generate(CompilationUnit compilationUnit, ColumnsLayout columns = ColumnsLayout.FreeTextFormat)
        {
            this.Layout = columns;
            Compiler.Preprocessor.ProcessedTokensDocument processedTokensDocument = compilationUnit.ProcessedTokensDocumentSnapshot;

            // Create a token iterator on top of pre-processed tokens lines
            Compiler.Scanner.ITokensLinesIterator tokensIterator = Compiler.Preprocessor.ProcessedTokensDocument.GetProcessedTokensIterator(
                compilationUnit.TextSourceInfo, processedTokensDocument.Lines, compilationUnit.CompilerOptions);

            //var date1 = DateTime.Now;
            TypeCobol.Compiler.Scanner.Token curToken = null;
            while ((curToken = (curToken == null ? tokensIterator.NextToken() : curToken)) != null)
            {
                var token = curToken;
                curToken = null;
                if (token.TokenType == Compiler.Scanner.TokenType.EndOfFile)
                {
                    break;
                }
                else if (token is TypeCobol.Compiler.Preprocessor.ImportedToken)
                {
                    InsertImportedTokensAction(token, tokensIterator);
                    curToken = tokensIterator.CurrentToken;
                }
                else if (token is TypeCobol.Compiler.Preprocessor.ReplacedPartialCobolWord)
                {
                    ReplaceAction(token as TypeCobol.Compiler.Preprocessor.ReplacedPartialCobolWord);
                }
            }
            //Now Run Actions
            PerformActions();
            TargetDocument.Write(Destination);

            //var date2 = DateTime.Now;
            //var date_diff = date2 - date1;
            //System.Console.Out.WriteLine(date_diff);
        }
Exemple #2
0
        /// <summary>
        /// Create the action of Inserting all Imported Tokens from the current iterator
        /// </summary>
        /// <param name="firstToken">The First Imported Token</param>
        /// <param name="tokensIterator"></param>
        private void InsertImportedTokensAction(TypeCobol.Compiler.Scanner.Token firstToken, Compiler.Scanner.ITokensLinesIterator tokensIterator)
        {
            TypeCobol.Compiler.Preprocessor.ImportedToken importedToken = (TypeCobol.Compiler.Preprocessor.ImportedToken)firstToken;
            TypeCobol.Compiler.Directives.CopyDirective   copyDirective = importedToken.CopyDirective;
            Token        copyToken     = copyDirective.COPYToken;
            Token        copyNameToken = copyDirective.TextNameSymbol;
            int          startLine     = copyToken.Line;
            int          endLine       = copyNameToken.Line;
            int          startColumn   = copyToken.Column;
            int          endColumn     = copyNameToken.EndColumn;
            List <Token> tokens        = new List <Token>()
            {
                copyToken, copyNameToken
            };
            Position from = null;
            Position to   = null;

            ComputePositions(tokens, out from, out to);
            TargetDocument.Source.AddPosition(from); //from position
            TargetDocument.Source.AddPosition(to);   //To Pos
            //Delete the copy Instruction
            bool     bAddNewLine = false;
            Position insertPos   = ComputeErasureAfterCopyInstructionAction(copyToken, copyNameToken, out bAddNewLine);

            Compiler.Source.StringSourceText buffer = new Compiler.Source.StringSourceText();
            if (bAddNewLine)
            {
                buffer.Insert(Environment.NewLine, 0, 0);//First new line
            }
            int currentLine   = firstToken.Line;
            int currentColumn = 1;

            TypeCobol.Compiler.Scanner.Token currentToken = firstToken;
            do
            {
                TypeCobol.Compiler.Preprocessor.ImportedToken impToken = currentToken as TypeCobol.Compiler.Preprocessor.ImportedToken;
                if (impToken.CopyDirective.COPYToken.Line != copyToken.Line ||
                    impToken.CopyDirective.COPYToken.Column != copyToken.Column)
                {
                    break;//This is from another COPY
                }
                int nInserPos = buffer.Size;
                int newLine   = currentToken.Line;
                if (newLine != currentLine)
                {
                    currentColumn = 1;
                    currentLine   = newLine;
                    buffer.Insert(Environment.NewLine, nInserPos, nInserPos);
                    nInserPos += Environment.NewLine.Length;
                }
                int nPadLength = currentToken.Column - currentColumn;
                if (nPadLength > 0)
                {
                    buffer.Insert(new string(' ', nPadLength), nInserPos, nInserPos);
                    nInserPos += nPadLength;
                }
                buffer.Insert(currentToken.Text, nInserPos, nInserPos);
                currentColumn = currentToken.EndColumn + 1;
                currentToken  = tokensIterator.NextToken();
            } while (currentToken.TokenType != TokenType.EndOfFile && currentToken is TypeCobol.Compiler.Preprocessor.ImportedToken);
            buffer.Insert(Environment.NewLine, buffer.Size, buffer.Size);
            Action action = new Action()
            {
                Kind = ActionKind.Insert, From = to, To = to, Text = buffer
            };

            Actions.Add(action);
        }