/// <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)); }
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( ); }