Commit 13dd2319 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

unified error output

parent 49e55849
......@@ -76,6 +76,7 @@ add_executable(in20
tools/in20/data.cpp tools/in20/data.h
tools/in20/plot.cpp tools/in20/plot.h
tools/in20/command.cpp tools/in20/command.h
tools/in20/globals.cpp tools/in20/globals.h
${BISON_cliparser_OUTPUT_SOURCE} ${BISON_cliparser_OUTPUT_HEADER}
${FLEX_clilexer_OUTPUTS} ${FLEX_clilexer_OUTPUT_HEADER}
......
......@@ -237,14 +237,14 @@ std::shared_ptr<Symbol> CliASTIdent::Eval(CliParserContext& ctx) const
auto *workspace = ctx.GetWorkspace();
if(!workspace)
{
std::cerr << "No workspace linked to parser." << std::endl;
ctx.PrintError("No workspace linked to parser.");
return nullptr;
}
auto iter = workspace->find(m_val);
if(iter == workspace->end())
{
std::cerr << "Variable \"" << m_val << "\" is not in workspace." << std::endl;
ctx.PrintError("Variable \"", m_val, "\" is not in workspace.");
return nullptr;
}
......@@ -260,7 +260,7 @@ std::shared_ptr<Symbol> CliASTAssign::Eval(CliParserContext& ctx) const
auto *workspace = ctx.GetWorkspace();
if(!workspace)
{
std::cerr << "No workspace linked to parser." << std::endl;
ctx.PrintError("No workspace linked to parser.");
return nullptr;
}
......@@ -268,7 +268,7 @@ std::shared_ptr<Symbol> CliASTAssign::Eval(CliParserContext& ctx) const
return nullptr;
if(m_left->GetType() != CliASTType::IDENT && m_left->GetType() != CliASTType::STRING)
{
std::cerr << "Left-hand side of assignment has to be an identifier." << std::endl;
ctx.PrintError("Left-hand side of assignment has to be an identifier.");
return nullptr;
}
......
......@@ -33,7 +33,7 @@ void CliLexer::LexerError(const char *err)
// Parser
// ----------------------------------------------------------------------------
void CliParserContext::PrintError(const std::string &err)
void CliParserContext::PrintErrorString(const std::string &err)
{
m_errors.push_back(err);
//std::cerr << err << "." << std::endl;
......
......@@ -74,7 +74,15 @@ private:
public:
CliLexer& GetLexer() { return m_lex; }
void PrintError(const std::string &err);
void PrintErrorString(const std::string &err);
template<typename ...T> void PrintError(T&&... msgs)
{
std::ostringstream ostr;
(ostr << ... << std::forward<T>(msgs));
PrintErrorString(ostr.str());
}
const std::vector<std::string>& GetErrors() const { return m_errors; }
void ClearErrors() { m_errors.clear(); }
......
......@@ -22,6 +22,7 @@ CommandLineWidget::CommandLineWidget(QWidget *pParent, QSettings *pSettings)
m_pEditCLI->setInsertPolicy(QComboBox::NoInsert);
m_pEditCLI->setEditable(true);
m_pEditCLI->lineEdit()->setPlaceholderText("Enter Command");
m_pEditCLI->lineEdit()->setFocus();
// ------------------------------------------------------------------------
......@@ -75,13 +76,13 @@ void CommandLineWidget::CommandEntered()
// write error log
for(const auto& err : m_parsectx.GetErrors())
m_pEditHistory->insertHtml((("<b><font color=\"#ff0000\">" + err + "</font></b><br>").c_str()));
PrintOutput(1, err.c_str());
m_parsectx.ClearErrors();
if(parse_state != 0)
{
m_pEditHistory->insertHtml("<b><font color=\"#ff0000\">Error: Could not parse command.</font></b><br>");
PrintOutput(1, "Error: Could not parse command.");
}
else
{
......@@ -92,6 +93,12 @@ void CommandLineWidget::CommandEntered()
//ast->Print(); std::cout.flush();
auto sym = ast->Eval(m_parsectx);
// write error log
for(const auto& err : m_parsectx.GetErrors())
PrintOutput(1, err.c_str());
m_parsectx.ClearErrors();
if(sym)
{
std::ostringstream ostrRes;
......@@ -103,24 +110,36 @@ void CommandLineWidget::CommandEntered()
else if(sym->GetType() == SymbolType::DATASET)
ostrRes << "&lt;Dataset&gt;";
m_pEditHistory->insertHtml((("<font color=\"#000000\">" + ostrRes.str() + "</font><br>").c_str()));
PrintOutput(0, ostrRes.str().c_str());
}
else
{
m_pEditHistory->insertHtml("<b><font color=\"#ff0000\">"
"Unable to evaluate expression."
"</font></b><br>");
PrintOutput(1, "Unable to evaluate expression.");
}
}
}
}
void CommandLineWidget::ScrollToEnd()
{
// scroll command list to last command
auto caret = m_pEditHistory->textCursor();
caret.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 1);
m_pEditHistory->setTextCursor(caret);
}
void CommandLineWidget::PrintOutputString(bool is_err, const QString &str)
{
if(is_err)
m_pEditHistory->insertHtml("<b><font color=\"#ff0000\">" + str + "</font></b><br>");
else
m_pEditHistory->insertHtml("<font color=\"#000000\">" + str + "</font><br>");
ScrollToEnd();
}
// ----------------------------------------------------------------------------
......
......@@ -35,12 +35,22 @@ private:
protected:
void CommandEntered();
void ScrollToEnd();
public:
CommandLineWidget(QWidget *pParent = nullptr, QSettings *pSettings = nullptr);
virtual ~CommandLineWidget();
CliParserContext& GetParserContext() { return m_parsectx; }
void PrintOutputString(bool is_err, const QString &str);
template<typename ...T> void PrintOutput(bool is_err, T&&... msgs)
{
std::ostringstream ostr;
(ostr << ... << std::forward<T>(msgs));
PrintOutputString(is_err, ostr.str().c_str());
}
};
......
/**
* globals
* @author Tobias Weber <tweber@ill.fr>
* @date 19-Jun-2018
* @license see 'LICENSE' file
*/
#include "globals.h"
/**
* globals
* @author Tobias Weber <tweber@ill.fr>
* @date 19-Jun-2018
* @license see 'LICENSE' file
*/
#ifndef __GLOBALS_H__
#define __GLOBALS_H__
#include "command.h"
// the GUI's command line widget
static CommandLine *g_pCLI = nullptr;
/**
* print output
*/
template<typename ...T> void print_out(T&&... msgs)
{
g_pCLI->GetWidget()->PrintOutput(false, msgs...);
}
/**
* print error messages
*/
template<typename ...T> void print_err(T&&... msgs)
{
g_pCLI->GetWidget()->PrintOutput(true, msgs...);
}
#endif
......@@ -6,7 +6,7 @@
*/
#include "mainwnd.h"
//#include <iostream>
#include "globals.h"
MainWnd::MainWnd(QSettings* pSettings)
......@@ -15,6 +15,9 @@ MainWnd::MainWnd(QSettings* pSettings)
m_pWS(new WorkSpace(this, pSettings)),
m_pCLI(new CommandLine(this, pSettings))
{
// the command line widget has to be accessible globally for error output
g_pCLI = m_pCLI;
this->setObjectName("in20");
this->setWindowTitle("IN20 Tool");
this->resize(800, 600);
......@@ -44,7 +47,7 @@ MainWnd::MainWnd(QSettings* pSettings)
// ------------------------------------------------------------------------
this->setStatusBar(m_pStatus);
//this->setStatusBar(m_pStatus);
this->setCentralWidget(m_pMDI);
this->addDockWidget(Qt::LeftDockWidgetArea, m_pBrowser);
this->addDockWidget(Qt::RightDockWidgetArea, m_pWS);
......
......@@ -25,7 +25,7 @@ private:
QSettings *m_pSettings = nullptr;
QMenuBar *m_pMenu = new QMenuBar(this);
QStatusBar *m_pStatus = new QStatusBar(this);
//QStatusBar *m_pStatus = new QStatusBar(this);
QMdiArea *m_pMDI = new QMdiArea(this);
FileBrowser *m_pBrowser = nullptr;
......
......@@ -12,7 +12,7 @@
#include <QtWidgets/QPushButton>
#include <QtWidgets/QFileDialog>
#include <iostream>
#include "globals.h"
using t_real = t_real_dat;
......@@ -64,14 +64,14 @@ void WorkSpaceWidget::ItemSelected(QListWidgetItem* pCur)
auto iter = m_workspace.find(ident);
if(iter == m_workspace.end())
{
std::cerr << "Variable \"" << ident << "\" is not in workspace." << std::endl;
print_err("Variable \"", ident, "\" is not in workspace.");
return;
}
auto symdataset = iter->second;
if(symdataset->GetType() != SymbolType::DATASET)
{
std::cerr << "Variable \"" << ident << "\" is not of dataset type." << std::endl;
print_err("Variable \"", ident, "\" is not of dataset type.");
return;
}
......@@ -99,14 +99,14 @@ void WorkSpaceWidget::ReceiveFiles(const std::vector<std::string> &files)
QFileInfo filepath(file.c_str());
if(!filepath.exists())
{
std::cerr << "File \"" << file << "\" does not exist." << std::endl;
print_err("File \"", file, "\" does not exist.");
//continue;
}
auto [ok, dataset] = Dataset::convert_instr_file(file.c_str());
if(!ok)
{
std::cerr << "File \"" << file << "\" cannot be converted." << std::endl;
print_err("File \"", file, "\" cannot be converted.");
continue;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment