public static void ParseProgramOrClass(TextSourceInfo textSourceInfo, ISearchableReadOnlyList<CodeElementsLine> codeElementsLines, TypeCobolOptions compilerOptions, SymbolTable customSymbols, out Program newProgram, out Class newClass, out IList<ParserDiagnostic> diagnostics)
        {
            // Create an Antlr compatible token source on top a the token iterator
            CodeElementsLinesTokenSource tokenSource = new CodeElementsLinesTokenSource(
                textSourceInfo.Name,
                codeElementsLines);

            // Init parser
            ITokenStream tokenStream = new TokensLinesTokenStream(tokenSource, Token.CHANNEL_SourceTokens);
            ProgramClassParser cobolParser = new ProgramClassParser(tokenStream);
            // -> activate full ambiguities detection
            //parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;

            // Register all parse errors in a list in memory
            DiagnosticSyntaxErrorListener errorListener = new DiagnosticSyntaxErrorListener();
            cobolParser.RemoveErrorListeners();
            cobolParser.AddErrorListener(errorListener);

            // Try to parse a Cobol program or class
            ProgramClassParser.CobolCompilationUnitContext codeElementParseTree = cobolParser.cobolCompilationUnit();

            // Visit the parse tree to build a first class object representing a Cobol program or class
            ParseTreeWalker walker = new ParseTreeWalker();
            CobolNodeBuilder programClassBuilder = new CobolNodeBuilder();
            programClassBuilder.CustomSymbols = customSymbols;
            programClassBuilder.Dispatcher = new NodeDispatcher();
            programClassBuilder.Dispatcher.CreateListeners();
            walker.Walk(programClassBuilder, codeElementParseTree);

            // Register compiler results
            newProgram = programClassBuilder.Program;
            newClass = programClassBuilder.Class;
            diagnostics = errorListener.Diagnostics;
        }
Beispiel #2
0
        public static void ParseProgramOrClass(TextSourceInfo textSourceInfo, ISearchableReadOnlyList <CodeElementsLine> codeElementsLines, TypeCobolOptions compilerOptions, SymbolTable customSymbols, out Program newProgram, out Class newClass, out IList <ParserDiagnostic> diagnostics)
        {
            // Create an Antlr compatible token source on top a the token iterator
            CodeElementsLinesTokenSource tokenSource = new CodeElementsLinesTokenSource(
                textSourceInfo.Name,
                codeElementsLines);

            // Init parser
            ITokenStream       tokenStream = new TokensLinesTokenStream(tokenSource, Token.CHANNEL_SourceTokens);
            ProgramClassParser cobolParser = new ProgramClassParser(tokenStream);
            // -> activate full ambiguities detection
            //parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;

            // Register all parse errors in a list in memory
            ParserDiagnosticErrorListener errorListener = new ParserDiagnosticErrorListener();

            cobolParser.RemoveErrorListeners();
            cobolParser.AddErrorListener(errorListener);

            // Try to parse a Cobol program or class
            ProgramClassParser.CobolCompilationUnitContext programClassParseTree = cobolParser.cobolCompilationUnit();

            // Visit the parse tree to build a first class object representing a Cobol program or class
            ParseTreeWalker  walker = new ParseTreeWalker();
            CobolNodeBuilder programClassBuilder = new CobolNodeBuilder();

            programClassBuilder.CustomSymbols = customSymbols;
            programClassBuilder.Dispatcher    = new NodeDispatcher();
            programClassBuilder.Dispatcher.CreateListeners();

            ParserDiagnostic programClassBuilderError = null;

            try { walker.Walk(programClassBuilder, programClassParseTree); }
            catch (Exception ex)
            {
                var code = Diagnostics.MessageCode.ImplementationError;
                programClassBuilderError = new ParserDiagnostic(ex.ToString(), null, null, code);
            }

            //Complete some information on Node and run checker that need a full AST
            if (programClassBuilder.Program != null)
            {
                programClassBuilder.Program.SyntaxTree.Root.AcceptASTVisitor(new Cobol85CompleteASTChecker());
            }

            // Register compiler results
            newProgram  = programClassBuilder.Program;
            newClass    = programClassBuilder.Class;
            diagnostics = programClassBuilder.GetDiagnostics(programClassParseTree);
            if (programClassBuilderError != null)
            {
                if (diagnostics == null)
                {
                    diagnostics = new List <ParserDiagnostic>();
                }
                diagnostics.Add(programClassBuilderError);
            }
        }
 /// <summary>parent: DATA DIVISION</summary>
 /// <param name="context">WORKING-STORAGE SECTION</param>
 public override void EnterWorkingStorageSection(ProgramClassParser.WorkingStorageSectionContext context)
 {
     var terminal = context.WorkingStorageSectionHeader();
     var header = terminal != null? (WorkingStorageSectionHeader)terminal.Symbol : null;
     Enter(new WorkingStorageSection(header), context);
 }
 public override void ExitWorkingStorageSection(ProgramClassParser.WorkingStorageSectionContext context)
 {
     ExitLastLevel1Definition();
     Exit(); // Exit WorkingStorageSection
 }
 public override void EnterCallStatementConditional(ProgramClassParser.CallStatementConditionalContext context)
 {
     var terminal = context.CallStatement();
     var statement = terminal != null? (CallStatement)terminal.Symbol : null;
     Enter(new Call(statement), context);
 }
 public override void ExitSubtractStatementConditional(ProgramClassParser.SubtractStatementConditionalContext context)
 {
     AttachEndIfExists(context.SubtractStatementEnd());
 }
 public override void ExitWhenOtherClause(ProgramClassParser.WhenOtherClauseContext context)
 {
     Exit();// exit WHEN OTHER
 }
 public override void ExitProcedureDivision(ProgramClassParser.ProcedureDivisionContext context)
 {
     Exit();
 }
 public override void ExitSentence(ProgramClassParser.SentenceContext context)
 {
     AttachEndIfExists(context.SentenceEnd());
     if (CurrentNode is Sentence) Exit();//TODO remove this and check what happens when exiting last CALL in FIN-STANDARD in BigBatch file (ie. CheckPerformance test)
 }
 public override void ExitEvaluateStatementWithBody(ProgramClassParser.EvaluateStatementWithBodyContext context)
 {
     AttachEndIfExists(context.EvaluateStatementEnd());// exit EVALUATE
 }
 public override void ExitFileSection(ProgramClassParser.FileSectionContext context)
 {
     ExitLastLevel1Definition();
     Exit();
 }
 public override void ExitDataDivision(ProgramClassParser.DataDivisionContext context)
 {
     Exit();
 }
 public override void ExitEnvironmentDivision(ProgramClassParser.EnvironmentDivisionContext context)
 {
     Exit();
 }
 public override void ExitConfigurationSection(ProgramClassParser.ConfigurationSectionContext context)
 {
     Exit(); // exit ConfigurationSection node
 }
 public override void ExitCobolProgram(ProgramClassParser.CobolProgramContext context)
 {
     AttachEndIfExists(context.ProgramEnd());
     Exit();
     programsStack.Pop();
 }
 public override void EnterXmlGenerateStatementConditional(ProgramClassParser.XmlGenerateStatementConditionalContext context)
 {
     var terminal = context.XmlGenerateStatement();
     var statement = terminal != null? (XmlGenerateStatement)terminal.Symbol : null;
     Enter(new XmlGenerate(statement), context);
 }
 public override void ExitOnSizeError(ProgramClassParser.OnSizeErrorContext context)
 {
     Exit();
 }
 public override void ExitFunctionDeclaration(ProgramClassParser.FunctionDeclarationContext context)
 {
     var terminal = context.FunctionDeclarationEnd();
     var end = terminal != null? terminal.Symbol is FunctionDeclarationEnd? (FunctionDeclarationEnd)terminal.Symbol : null : null;
     Enter(new FunctionEnd(end), context);
     Exit();
     Exit();// exit DECLARE FUNCTION
 }
 public override void ExitParagraph(ProgramClassParser.ParagraphContext context)
 {
     if (Program.SyntaxTree.CurrentNode is Paragraph) Exit();
 }
 public override void ExitIfStatementWithBody(ProgramClassParser.IfStatementWithBodyContext context)
 {
     Exit(); // Exit ELSE (if any) or THEN
     AttachEndIfExists(context.IfStatementEnd());
     // DO NOT Exit() IF node because this will be done in ExitStatement
 }
 public override void ExitSearchStatementWithBody(ProgramClassParser.SearchStatementWithBodyContext context)
 {
     AttachEndIfExists(context.SearchStatementEnd());
 }
 public override void ExitMultiplyStatementConditional(ProgramClassParser.MultiplyStatementConditionalContext context)
 {
     AttachEndIfExists(context.MultiplyStatementEnd());
 }
 public override void ExitStatement(ProgramClassParser.StatementContext context)
 {
     if (skipEmptyStatement) skipEmptyStatement = false;
     else Exit();
 }
 public override void ExitOnAtEnd(ProgramClassParser.OnAtEndContext context)
 {
     Exit();
 }
 public override void ExitUnstringStatementConditional(ProgramClassParser.UnstringStatementConditionalContext context)
 {
     AttachEndIfExists(context.UnstringStatementEnd());
 }
 public override void ExitOnException(ProgramClassParser.OnExceptionContext context)
 {
     Exit();
 }
 public override void ExitWhenSearchConditionClause(ProgramClassParser.WhenSearchConditionClauseContext context)
 {
     Exit(); // WHEN
 }
Beispiel #28
0
        public static void ParseProgramOrClass(TextSourceInfo textSourceInfo, ISearchableReadOnlyList <CodeElementsLine> codeElementsLines, TypeCobolOptions compilerOptions, SymbolTable customSymbols, PerfStatsForParserInvocation perfStatsForParserInvocation, out SourceFile root, out IList <ParserDiagnostic> diagnostics, out Dictionary <CodeElement, Node> nodeCodeElementLinkers)
        {
            // Create an Antlr compatible token source on top a the token iterator
            CodeElementsLinesTokenSource tokenSource = new CodeElementsLinesTokenSource(
                textSourceInfo.Name,
                codeElementsLines);

            // Init parser
            ITokenStream       tokenStream = new TokensLinesTokenStream(tokenSource, Token.CHANNEL_SourceTokens);
            ProgramClassParser cobolParser = new ProgramClassParser(tokenStream);

            // -> activate full ambiguities detection
            //parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;

            // Optionnaly activate Antlr Parser performance profiling
            // WARNING : use this in a single-treaded context only (uses static field)
            if (AntlrPerformanceProfiler == null && perfStatsForParserInvocation.ActivateDetailedAntlrPofiling)
            {
                AntlrPerformanceProfiler = new AntlrPerformanceProfiler(cobolParser);
            }
            if (AntlrPerformanceProfiler != null)
            {
                // Replace the generated parser by a subclass which traces all rules invocations
                cobolParser = new ProgramClassTracingParser(tokenStream);
                AntlrPerformanceProfiler.BeginParsingFile(textSourceInfo, null);
            }

            // Register all parse errors in a list in memory
            ParserDiagnosticErrorListener errorListener = new ParserDiagnosticErrorListener();

            cobolParser.RemoveErrorListeners();
            cobolParser.AddErrorListener(errorListener);

            // Try to parse a Cobol program or class
            perfStatsForParserInvocation.OnStartAntlrParsing();
            if (AntlrPerformanceProfiler != null)
            {
                AntlrPerformanceProfiler.BeginParsingSection();
            }
            ProgramClassParser.CobolCompilationUnitContext programClassParseTree = cobolParser.cobolCompilationUnit();
            if (AntlrPerformanceProfiler != null)
            {
                AntlrPerformanceProfiler.EndParsingSection(programClassParseTree.ChildCount);
            }
            perfStatsForParserInvocation.OnStopAntlrParsing(
                AntlrPerformanceProfiler != null ? (int)AntlrPerformanceProfiler.CurrentFileInfo.DecisionTimeMs : 0,
                AntlrPerformanceProfiler != null ? AntlrPerformanceProfiler.CurrentFileInfo.RuleInvocations.Sum() : 0);

            if (AntlrPerformanceProfiler != null)
            {
                AntlrPerformanceProfiler.EndParsingFile(cobolParser.ParseInfo.DecisionInfo, (int)(cobolParser.ParseInfo.GetTotalTimeInPrediction() / 1000000));
            }


            // Visit the parse tree to build a first class object representing a Cobol program or class
            ParseTreeWalker  walker = new ParseTreeWalker();
            CobolNodeBuilder programClassBuilder = new CobolNodeBuilder();

            programClassBuilder.SyntaxTree    = new SyntaxTree(); //Initializie SyntaxTree for the current source file
            programClassBuilder.CustomSymbols = customSymbols;
            programClassBuilder.Dispatcher    = new NodeDispatcher();
            programClassBuilder.Dispatcher.CreateListeners();

            perfStatsForParserInvocation.OnStartTreeBuilding();

            ParserDiagnostic programClassBuilderError = null;

            try { walker.Walk(programClassBuilder, programClassParseTree); }
            catch (Exception ex)
            {
                var code = Diagnostics.MessageCode.ImplementationError;
                programClassBuilderError = new ParserDiagnostic(ex.ToString(), null, null, code, ex);
            }

            //Create link between datas
            programClassBuilder.SyntaxTree.Root.AcceptASTVisitor(new TypeCobolLinker());

            perfStatsForParserInvocation.OnStopTreeBuilding();

            //Complete some information on Node and run checker that need a full AST
            programClassBuilder.SyntaxTree.Root.AcceptASTVisitor(new Cobol85CompleteASTChecker());


            // Register compiler results
            root                   = programClassBuilder.SyntaxTree.Root; //Set output root node
            diagnostics            = programClassBuilder.GetDiagnostics(programClassParseTree);
            nodeCodeElementLinkers = programClassBuilder.NodeCodeElementLinkers;

            if (programClassBuilderError != null)
            {
                if (diagnostics == null)
                {
                    diagnostics = new List <ParserDiagnostic>();
                }
                diagnostics.Add(programClassBuilderError);
            }
        }
 public override void ExitXmlParseStatementConditional(ProgramClassParser.XmlParseStatementConditionalContext context)
 {
     AttachEndIfExists(context.XmlStatementEnd());
 }
 public override void ExitOnOverflow(ProgramClassParser.OnOverflowContext context)
 {
     Exit();
 }
 public override void ExitOnInvalidKey(ProgramClassParser.OnInvalidKeyContext context)
 {
     Exit();
 }
 public override void EnterWhenSearchConditionClause(ProgramClassParser.WhenSearchConditionClauseContext context)
 {
     var terminal = context.WhenSearchCondition();
     var condition = terminal != null? (WhenSearchCondition)terminal.Symbol : null;
     Enter(new WhenSearch(condition), context);
     if (context.NextSentenceStatement() != null) {
         Enter(new NextSentence((NextSentenceStatement)context.NextSentenceStatement().Symbol));
         Exit();
     }
 }