From ca17166256c74bee7dc2d23a780dbd3092ea2c28 Mon Sep 17 00:00:00 2001 From: tervicke Date: Mon, 7 Apr 2025 23:55:34 +0530 Subject: [PATCH 1/3] Added exception handling to handle incorrect keyword. Related to #28 --- src/Syntax.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Syntax.cpp b/src/Syntax.cpp index 3a2f666..1f0841a 100644 --- a/src/Syntax.cpp +++ b/src/Syntax.cpp @@ -46,10 +46,28 @@ void Syntax::loadSyntaxRules(const YAML::Node &config) // Iterate through each rule in the category for (const auto &rule : rules) { - QString regex = QString::fromStdString(rule["regex"].as()); - QColor color(QString::fromStdString(rule["color"].as())); + + QString regex; + try{ + std::string regexStr = rule["regex"].as(); //will throw exception if the key does not exist + regex = QString::fromStdString(regexStr); + }catch(const YAML::Exception e){ + qWarning() << " YAML exception when parsion the regex in syntax file" << e.what(); + continue; + } + qDebug() << "regex: " << regex; + QColor color; + try{ + std::string colorStr = rule["color"].as(); + color = QColor(QString::fromStdString(colorStr)); + }catch(const YAML::Exception e){ + qWarning() << " YAML exception when parsion the color in syntax file" << e.what(); + continue; + } + + // Create a QTextCharFormat for the rule QTextCharFormat format; format.setForeground(color); From f5f5598e68cdb2fca1c4061dabc635f0166656c3 Mon Sep 17 00:00:00 2001 From: tervicke Date: Tue, 8 Apr 2025 21:51:23 +0530 Subject: [PATCH 2/3] Added if condition to check if the color is valid --- src/Syntax.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Syntax.cpp b/src/Syntax.cpp index 1f0841a..faa6c92 100644 --- a/src/Syntax.cpp +++ b/src/Syntax.cpp @@ -67,6 +67,11 @@ void Syntax::loadSyntaxRules(const YAML::Node &config) continue; } + //checks if the color is a valid color + if(!color.isValid()){ + qWarning() << "Invalid COlor : Skipping..."; + continue; + } // Create a QTextCharFormat for the rule QTextCharFormat format; From 6b9014c310536d1e3f41f0c82e3abeb2711078b0 Mon Sep 17 00:00:00 2001 From: tervicke Date: Tue, 8 Apr 2025 22:34:58 +0530 Subject: [PATCH 3/3] fixed nits from review --- src/Syntax.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Syntax.cpp b/src/Syntax.cpp index faa6c92..e3aab1c 100644 --- a/src/Syntax.cpp +++ b/src/Syntax.cpp @@ -48,10 +48,13 @@ void Syntax::loadSyntaxRules(const YAML::Node &config) { QString regex; - try{ + try + { std::string regexStr = rule["regex"].as(); //will throw exception if the key does not exist - regex = QString::fromStdString(regexStr); - }catch(const YAML::Exception e){ + regex = QString::fromStdString(regexStr); + } + catch(const YAML::Exception e) + { qWarning() << " YAML exception when parsion the regex in syntax file" << e.what(); continue; } @@ -59,16 +62,20 @@ void Syntax::loadSyntaxRules(const YAML::Node &config) qDebug() << "regex: " << regex; QColor color; - try{ + try + { std::string colorStr = rule["color"].as(); - color = QColor(QString::fromStdString(colorStr)); - }catch(const YAML::Exception e){ + color = QColor(QString::fromStdString(colorStr)); + } + catch(const YAML::Exception e) + { qWarning() << " YAML exception when parsion the color in syntax file" << e.what(); continue; } //checks if the color is a valid color - if(!color.isValid()){ + if(!color.isValid()) + { qWarning() << "Invalid COlor : Skipping..."; continue; }