diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index d5afe1fb8c..53b7fad972 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -53,6 +53,7 @@ namespace { using LCompilers::endswith; using LCompilers::CompilerOptions; using LCompilers::LPython::parse_python_file; +using LCompilers::LPython::parse_python_source; enum class Backend { llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64 @@ -190,16 +191,17 @@ int emit_asr(const std::string &infile, Allocator al(4*1024); LCompilers::diag::Diagnostics diagnostics; LCompilers::LocationManager lm; + std::string input; { LCompilers::LocationManager::FileLocations fl; fl.in_filename = infile; lm.files.push_back(fl); - std::string input = LCompilers::read_file(infile); + input = LCompilers::read_file(infile); lm.init_simple(input); lm.file_ends.push_back(input.size()); } - LCompilers::Result r1 = parse_python_file( - al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser); + LCompilers::Result r1 = parse_python_source( + al, runtime_library_dir, input, diagnostics, 0, compiler_options.new_parser); std::cerr << diagnostics.render(lm, compiler_options); if (!r1.ok) { return 1; diff --git a/src/lpython/parser/parser.cpp b/src/lpython/parser/parser.cpp index 92ab5023a7..a13cc410f6 100644 --- a/src/lpython/parser/parser.cpp +++ b/src/lpython/parser/parser.cpp @@ -112,9 +112,9 @@ std::string unique_filename(const std::string &prefix) { return filename; } -Result parse_python_file(Allocator &al, +Result parse_python_source(Allocator &al, const std::string &/*runtime_library_dir*/, - const std::string &infile, + const std::string &source_code, diag::Diagnostics &diagnostics, uint32_t prev_loc, [[maybe_unused]] bool new_parser) { @@ -122,8 +122,7 @@ Result parse_python_file(Allocator &al, // We will be using the new parser from now on new_parser = true; LCOMPILERS_ASSERT(new_parser) - std::string input = read_file(infile); - Result res = parse(al, input, prev_loc, diagnostics); + Result res = parse(al, source_code, prev_loc, diagnostics); if (res.ok) { ast = (LPython::AST::ast_t*)res.result; } else { @@ -133,5 +132,15 @@ Result parse_python_file(Allocator &al, return ast; } +Result parse_python_file(Allocator &al, + const std::string &runtime_library_dir, + const std::string &infile, + diag::Diagnostics &diagnostics, + uint32_t prev_loc, + [[maybe_unused]] bool new_parser) { + std::string input_source_code = read_file(infile); + return parse_python_source(al, runtime_library_dir, input_source_code, diagnostics, prev_loc, new_parser); +} + } // namespace LCompilers::LPython diff --git a/src/lpython/parser/parser.h b/src/lpython/parser/parser.h index e9f6b92cba..889b890bb2 100644 --- a/src/lpython/parser/parser.h +++ b/src/lpython/parser/parser.h @@ -41,6 +41,12 @@ Result parse_python_file(Allocator &al, diag::Diagnostics &diagnostics, uint32_t prev_loc, bool new_parser); +Result parse_python_source(Allocator &al, + const std::string &runtime_library_dir, + const std::string &infile, + diag::Diagnostics &diagnostics, + uint32_t prev_loc, bool new_parser); + } // namespace LCompilers::LPython #endif