Exemplo n.º 1
0
        /// <summary>Gets the source input text for a <see cref="ParserRuleContext"/> produced by an <see cref="IRecognizer"/></summary>
        /// <param name="ruleContext">Rule context to get the source text from</param>
        /// <param name="charStream">The stream the rule was parsed from</param>
        /// <returns>Source contents for the rule or an empty string if the source is not available</returns>
        public static string GetSourceText(this ParserRuleContext ruleContext, ICharStream?charStream)
        {
            if (charStream == null)
            {
                return(string.Empty);
            }

            var span = ruleContext.GetCharInterval( );

            return(span.a < 0 ? string.Empty : charStream.GetText(span));
        }
Exemplo n.º 2
0
        public override void ExitEveryRule([NotNull] ParserRuleContext context)
        {
            base.ExitEveryRule(context);
            var span       = context.GetCharInterval( );
            var charStream = (( ITokenStream )Recognizer.InputStream).TokenSource.InputStream;

            ActiveNode.Add(new XAttribute("Text", charStream.GetText(span)));
            ActiveNode.Add(new XAttribute("RuleIndex", context.RuleIndex));
            ActiveNode.Add(new XAttribute("SourceInterval", context.SourceInterval.ToString( )));
            if (context.exception != null)
            {
                ActiveNode.Add(new XAttribute("Exception", context.exception));
            }

            Pop( );
        }