From f7f358f10049783a0fc20a43597a19a4042e6d59 Mon Sep 17 00:00:00 2001 From: syheliel Date: Fri, 16 Feb 2024 11:49:59 +0800 Subject: [PATCH 1/3] refine input file missing msg --- src/bin/lpython.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 6f0fc85b24..3a0fabb28b 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1735,7 +1735,8 @@ int main(int argc, char *argv[]) // the first: std::string arg_file = arg_files[0]; if (CLI::NonexistentPath(arg_file).empty()){ - throw LCompilers::LCompilersException("No such file or directory: " + arg_file); + std::cerr << "Your input file doesn't exist. file name is: " << arg_file << std::endl; + return 1; } std::string outfile; From 602285c1a44ee8972e42b1d09c8fdfd4f1750c9a Mon Sep 17 00:00:00 2001 From: syheliel <45957390+syheliel@users.noreply.github.com> Date: Sat, 17 Feb 2024 11:28:16 +0800 Subject: [PATCH 2/3] Update src/bin/lpython.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Čertík --- src/bin/lpython.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 3a0fabb28b..d5afe1fb8c 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1735,7 +1735,7 @@ int main(int argc, char *argv[]) // the first: std::string arg_file = arg_files[0]; if (CLI::NonexistentPath(arg_file).empty()){ - std::cerr << "Your input file doesn't exist. file name is: " << arg_file << std::endl; + std::cerr << "The input file does not exist: " << arg_file << std::endl; return 1; } From 117197230609ebe8ce1bdcf8a5ecd1c438f31586 Mon Sep 17 00:00:00 2001 From: syheliel Date: Sat, 17 Feb 2024 22:23:26 +0800 Subject: [PATCH 3/3] Fix twice read when parsing --- src/bin/lpython.cpp | 8 +++++--- src/lpython/parser/parser.cpp | 17 +++++++++++++---- src/lpython/parser/parser.h | 6 ++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index 3a0fabb28b..69499211c5 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