From d46acb06d50d394d9e23b7a207e74bfcf7ff2e79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0torc?= <storcond@fit.cvut.cz>
Date: Thu, 13 Apr 2023 11:14:53 +0200
Subject: [PATCH] cli: Fix REPL handling of exceptions

---
 aql2/src/REPL.cpp | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/aql2/src/REPL.cpp b/aql2/src/REPL.cpp
index 30e52fe06..b875d010b 100644
--- a/aql2/src/REPL.cpp
+++ b/aql2/src/REPL.cpp
@@ -15,7 +15,6 @@ cli::CommandResult REPL::run(cli::Environment& env)
         // numbers
         {"[\\-|+]{0,1}[0-9]+", cl::BLUE}, // integers
         {"[\\-|+]{0,1}[0-9]*\\.[0-9]+", cl::BLUE}, // decimals
-        {"[\\-|+]{0,1}[0-9]+e[\\-|+]{0,1}[0-9]+", cl::BLUE}, // scientific notation
 
         // strings
         {"\"(.|\n)*?\"", cl::YELLOW}, // double quotes
@@ -76,8 +75,24 @@ cli::CommandResult REPL::run(cli::Environment& env)
 
         const auto& suggestions = complete.getSuggestions(context, prefix);
 
-        for (const auto& suggestion : suggestions) {
-            completions.emplace_back(suggestion.second);
+        for (const auto& [kind, suggestion] : suggestions) {
+            replxx::Replxx::Color color;
+            switch (kind) {
+            case cli::Autocomplete::SuggestionKind::Keyword:
+                color = Replxx::Color::BRIGHTBLUE;
+                break;
+            case cli::Autocomplete::SuggestionKind::Variable:
+                color = Replxx::Color::BRIGHTGREEN;
+                break;
+            case cli::Autocomplete::SuggestionKind::Identifier:
+                color = Replxx::Color::GREEN;
+                break;
+            default:
+                color = Replxx::Color::BRIGHTCYAN;
+                break;
+            }
+
+            completions.emplace_back(suggestion, color);
         }
 
         return completions;
@@ -88,7 +103,7 @@ cli::CommandResult REPL::run(cli::Environment& env)
     cli::CommandResult state = cli::CommandResult::OK;
     // display the prompt and retrieve input from the user
     char const* cInput;
-    while (state != cli::CommandResult::QUIT) {
+    while (state == cli::CommandResult::OK || state == cli::CommandResult::ERROR) {
         do {
             cInput = rx.input(prompt);
         } while ((cInput == nullptr) && (errno == EAGAIN));
@@ -103,12 +118,13 @@ cli::CommandResult REPL::run(cli::Environment& env)
         if (input.empty()) {
             continue;
         }
-
-        const std::unique_ptr<cli::Command> ast = cli::Parser2::parseString(input);
-        state = env.execute(ast);
-
-        if (state == cli::CommandResult::OK)
-            rx.history_add(input);
+        try {
+            state = env.execute(input);
+            if (state == cli::CommandResult::OK)
+                rx.history_add(input);
+        } catch (const std::exception& e) {
+            std::cerr << e.what() << '\n';
+        }
     }
 
     if (REPL::historyFile.has_value())
-- 
GitLab