示例#1
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);
            }
        }
        public static void CupParseProgramOrClass(TextSourceInfo textSourceInfo, ISearchableReadOnlyList <CodeElementsLine> codeElementsLines, TypeCobolOptions compilerOptions, SymbolTable customSymbols, PerfStatsForParserInvocation perfStatsForParserInvocation, out SourceFile root, out List <Diagnostic> diagnostics, out Dictionary <CodeElement, Node> nodeCodeElementLinkers)
        {
            PrepareCupParser();
#if DEBUG_ANTRL_CUP_TIME
            var t1 = DateTime.UtcNow;
#endif
            CodeElementTokenizer             scanner = new CodeElementTokenizer(codeElementsLines);
            CupParser.TypeCobolProgramParser parser  = new CupParser.TypeCobolProgramParser(scanner);
            CupParserTypeCobolProgramDiagnosticErrorReporter diagReporter = new CupParserTypeCobolProgramDiagnosticErrorReporter();
            parser.ErrorReporter = diagReporter;
            ProgramClassBuilder builder = new ProgramClassBuilder();
            parser.Builder = builder;
            ParserDiagnostic programClassBuilderError = null;

            builder.SyntaxTree    = new SyntaxTree <CodeElement>(); //Initializie SyntaxTree for the current source file
            builder.CustomSymbols = customSymbols;
            builder.Dispatcher    = new NodeDispatcher <CodeElement>();
            builder.Dispatcher.CreateListeners();

            // Try to parse a Cobol program or class, with cup w are also building the The Syntax Tree Node
            perfStatsForParserInvocation.OnStartParsing();
            try
            {
                TUVienna.CS_CUP.Runtime.Symbol symbol = parser.parse();
            }
            catch (Exception ex)
            {
                var code = Diagnostics.MessageCode.ImplementationError;
                programClassBuilderError = new ParserDiagnostic(ex.ToString(), null, null, code, ex);
            }
            perfStatsForParserInvocation.OnStopParsing(0, 0);

#if DEBUG_ANTRL_CUP_TIME
            var t2 = DateTime.UtcNow;
            var t  = t2 - t1;
            System.Diagnostics.Debug.WriteLine("Time[" + textSourceInfo.Name + "];" + t.Milliseconds);
#endif
            root = builder.SyntaxTree.Root; //Set output root node

            perfStatsForParserInvocation.OnStartTreeBuilding();

            //Create link between data definition an Types, will be stored in SymbolTable
            root.AcceptASTVisitor(new TypeCobolLinker());

            //Stop measuring tree building performance
            perfStatsForParserInvocation.OnStopTreeBuilding();

            // Register compiler results
            diagnostics            = diagReporter.Diagnostics ?? new List <Diagnostic>();
            nodeCodeElementLinkers = builder.NodeCodeElementLinkers;

            if (programClassBuilderError != null)
            {
                diagnostics.Add(programClassBuilderError);
            }
        }
示例#3
0
        internal static void AddError(CodeElement e, string message, Scanner.Token token, string rulestack = null, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            if (e.Diagnostics == null)
            {
                e.Diagnostics = new List <Diagnostic>();
            }
            var parserDiag = new ParserDiagnostic(message, token, rulestack, code);

            e.Diagnostics.Add(parserDiag);
        }
示例#4
0
        internal static void AddError(CodeElement e, string message, DataDefinitionEntry data, string rulestack = null, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            if (e.Diagnostics == null)
            {
                e.Diagnostics = new List <Diagnostic>();
            }
            var parserDiag = new ParserDiagnostic(message, data.DataName.NameLiteral.Token, rulestack, code);

            e.Diagnostics.Add(parserDiag);
        }
示例#5
0
        internal static void AddError(CodeElement e, string message, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            if (e.Diagnostics == null)
            {
                e.Diagnostics = new List <Diagnostic>();
            }
            var parserDiag = new ParserDiagnostic(message, e.StartIndex + 1, e.StopIndex + 1, e.ConsumedTokens[0].Line, null, code);

            e.Diagnostics.Add(parserDiag);
        }
示例#6
0
        private void SetStatus(string format, params object[] args)
        {
            var message    = string.Format(format, args);
            var diagnostic = new ParserDiagnostic();

            diagnostic.Message    = message;
            diagnostic.LineNumber = -1;

            OnStatus.Raise(this_, message);
            OnParserDiagnostic.Raise(this_, diagnostic);
        }
示例#7
0
        internal static void AddError(Node node, string message, DataDefinitionEntry data, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            ParserDiagnostic diagnostic;

            if (data?.DataName != null)
            {
                diagnostic = new ParserDiagnostic(message, data?.DataName != null ? data.DataName.NameLiteral.Token : data.ConsumedTokens[0], null, code);
                node.AddDiagnostic(diagnostic);
            }
            else
            {
                AddError(node, message, code);
            }
        }
示例#8
0
 private ParserDiagnostic(ParserDiagnostic.Internal native)
     : this(__CopyValue(native))
 {
     __ownsNativeInstance = true;
     NativeToManagedMap[__Instance] = this;
 }
示例#9
0
 private static ParserDiagnostic.Internal* __CopyValue(ParserDiagnostic.Internal native)
 {
     var ret = Marshal.AllocHGlobal(32);
     CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(ret, new global::System.IntPtr(&native));
     return (ParserDiagnostic.Internal*) ret;
 }
示例#10
0
 public static ParserDiagnostic __CreateInstance(ParserDiagnostic.Internal native)
 {
     return new ParserDiagnostic(native);
 }
示例#11
0
 protected ParserDiagnostic(ParserDiagnostic.Internal* native, bool skipVTables = false)
 {
     if (native == null)
         return;
     __Instance = new global::System.IntPtr(native);
 }
示例#12
0
 private void Result_AddedDiagnostics(object sender, ParserDiagnostic e)
 {
     Console.WriteLine(e.Message);
 }
示例#13
0
        internal static void AddError(Node node, string message, SymbolReference symbol, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            var diagnostic = new ParserDiagnostic(message, symbol.NameLiteral.Token, null, code);

            node.AddDiagnostic(diagnostic);
        }
示例#14
0
 private static void* __CopyValue(ParserDiagnostic.__Internal native)
 {
     var ret = Marshal.AllocHGlobal(60);
     global::CppSharp.Parser.ParserDiagnostic.__Internal.cctor_1(ret, new global::System.IntPtr(&native));
     return ret.ToPointer();
 }
示例#15
0
 protected ParserDiagnostic(ParserDiagnostic.Internal* native, bool isInternalImpl = false)
 {
     __Instance = new global::System.IntPtr(native);
 }
示例#16
0
 private ParserDiagnostic(ParserDiagnostic.Internal native)
     : this(__CopyValue(native))
 {
     __ownsNativeInstance = true;
 }
示例#17
0
 internal ParserDiagnostic(ParserDiagnostic.Internal native)
     : this(__CopyValue(native))
 {
 }
示例#18
0
 internal ParserDiagnostic(ParserDiagnostic.Internal* native)
     : this(new global::System.IntPtr(native))
 {
 }
示例#19
0
 internal ParserDiagnostic(ParserDiagnostic.Internal native)
     : this(&native)
 {
 }
示例#20
0
 public static ParserDiagnostic __CreateInstance(ParserDiagnostic.__Internal native, bool skipVTables = false)
 {
     return new ParserDiagnostic(native, skipVTables);
 }
示例#21
0
        internal static void AddError(Node node, string message, Scanner.Token token, string rulestack = null, MessageCode code = MessageCode.SyntaxErrorInParser)
        {
            var diagnostic = new ParserDiagnostic(message, token, rulestack, code);

            node.AddDiagnostic(diagnostic);
        }
示例#22
0
 private ParserDiagnostic(ParserDiagnostic.__Internal native, bool skipVTables = false)
     : this(__CopyValue(native), skipVTables)
 {
     __ownsNativeInstance = true;
     NativeToManagedMap[__Instance] = this;
 }
示例#23
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);
            }
        }