/// <summary>
        /// Register a ParserDiagnostic for each syntax error encountered by the parser
        /// </summary>
        public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
        {
            // Build a string representing the current grammar rules being recognized
            StringBuilder ruleStack = new StringBuilder();
            IList<String> stack = ((Antlr4.Runtime.Parser)recognizer).GetRuleInvocationStack();
            bool isFirst = true;
            foreach (string ruleInvocation in stack.Reverse())
            {
                if(isFirst) { isFirst = false;  }
                else
                {
                    ruleStack.Append('>');
                }
                ruleStack.Append(ruleInvocation);
            }

            // Register a new diagnostic
            ParserDiagnostic diagnostic = new ParserDiagnostic(msg, offendingSymbol, ruleStack.ToString());
            Diagnostics.Add(diagnostic);
        }
示例#2
0
        /// <summary>
        /// Register a ParserDiagnostic for each syntax error encountered by the parser
        /// </summary>
        public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
        {
            // Build a string representing the current grammar rules being recognized
            StringBuilder  ruleStack = new StringBuilder();
            IList <String> stack     = ((Antlr4.Runtime.Parser)recognizer).GetRuleInvocationStack();
            bool           isFirst   = true;

            foreach (string ruleInvocation in stack.Reverse())
            {
                // Hide the root rule which is useful for error recovery and perf, but does not exist in the pure Cobol grammar
                if (ruleInvocation == "cobolCodeElements")
                {
                    continue;
                }

                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    ruleStack.Append('>');
                }
                ruleStack.Append(ruleInvocation);
            }

            // Create a new diagnostic object
            ParserDiagnostic diagnostic = new ParserDiagnostic(msg, offendingSymbol, ruleStack.ToString());

            // Attach this diagnostic to the current parse tree rule
            var parser = (Antlr4.Runtime.Parser)recognizer;

            if (parser != null && parser.Context is ParserRuleContextWithDiagnostics)
            {
                var currentRuleContext = (ParserRuleContextWithDiagnostics)parser.Context;
                currentRuleContext.AttachDiagnostic(diagnostic);
            }
        }