mycroforge/MycroForge.CLI/CodeGen/PythonSourceVisitor.cs

27 lines
922 B
C#

using Antlr4.Runtime;
using MycroForge.Parsing;
namespace MycroForge.CLI.CodeGen;
public abstract class PythonSourceVisitor : PythonParserBaseVisitor<object?>
{
protected CommonTokenStream Stream { get; }
protected PythonParser Parser { get; }
protected TokenStreamRewriter Rewriter { get; }
protected PythonSourceVisitor(string source)
{
var input = new AntlrInputStream(source);
var lexer = new PythonLexer(input);
Stream = new CommonTokenStream(lexer);
Parser = new PythonParser(Stream);
Rewriter = new TokenStreamRewriter(Stream);
}
protected string GetOriginalText(ParserRuleContext context)
{
// The parser does not necessarily return the original source,
// so we return the text from Rewriter.TokenStream, since this is unmodified.
return Rewriter.TokenStream.GetText(context);
}
}