Skip to content
Snippets Groups Projects
Commit e6412fbc authored by Ondřej Štorc's avatar Ondřej Štorc
Browse files

cli: Remove old parser from enviroment

parent 19ebdac7
No related branches found
No related tags found
1 merge request!256Parser replacement with ANTLR
...@@ -19,7 +19,7 @@ public: ...@@ -19,7 +19,7 @@ public:
   
CommandResult run(Environment& environment) const override CommandResult run(Environment& environment) const override
{ {
CommandResult state = environment.execute(std::make_shared<cli::StringLineInterface>(cli::StringLineInterface(m_code))); CommandResult state = environment.execute(m_code);
   
if (state != cli::CommandResult::QUIT) if (state != cli::CommandResult::QUIT)
state = cli::CommandResult::OK; state = cli::CommandResult::OK;
......
...@@ -24,7 +24,7 @@ public: ...@@ -24,7 +24,7 @@ public:
if (!ifs.is_open()) if (!ifs.is_open())
throw exception::CommonException("File '" + m_fileName + "' not found."); throw exception::CommonException("File '" + m_fileName + "' not found.");
   
CommandResult state = environment.execute(std::make_shared<cli::IstreamLineInterface<std::ifstream>>(cli::IstreamLineInterface<std::ifstream>(std::move(ifs)))); CommandResult state = environment.execute(ifs);
   
if (state != cli::CommandResult::QUIT && state != cli::CommandResult::RETURN) if (state != cli::CommandResult::QUIT && state != cli::CommandResult::RETURN)
state = cli::CommandResult::OK; state = cli::CommandResult::OK;
......
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
   
#include <ext/exception> #include <ext/exception>
   
#include <lexer/Lexer.h> #include <parser/Parser2.h>
#include <parser/Parser.h>
   
#include <global/GlobalData.h> #include <global/GlobalData.h>
   
...@@ -43,33 +42,6 @@ std::string Environment::getBinding(const std::string& name) const ...@@ -43,33 +42,6 @@ std::string Environment::getBinding(const std::string& name) const
throw exception::CommonException("Binded value of name " + name + " not found."); throw exception::CommonException("Binded value of name " + name + " not found.");
} }
   
cli::CommandResult Environment::execute(const std::shared_ptr<cli::LineInterface>& lineInterface)
{
cli::CommandResult state = cli::CommandResult::OK;
while (state == cli::CommandResult::OK || state == cli::CommandResult::ERROR)
state = execute_line(cli::CharSequence(lineInterface));
return state;
}
cli::CommandResult Environment::execute_line(cli::CharSequence charSequence)
{
try {
cli::Parser parser = cli::Parser(cli::Lexer(std::move(charSequence)));
std::unique_ptr<CommandList> command = parser.parse();
cli::CommandResult res = command->run(*this);
if (res == CommandResult::CONTINUE || res == CommandResult::BREAK)
throw std::logic_error("There is no loop to continue/break.");
return res;
} catch (...) {
alib::ExceptionHandler::handle(common::Streams::err);
return cli::CommandResult::EXCEPTION;
}
}
Environment& Environment::getGlobalScope() Environment& Environment::getGlobalScope()
{ {
if (!static_cast<bool>(m_upper)) if (!static_cast<bool>(m_upper))
...@@ -77,10 +49,10 @@ Environment& Environment::getGlobalScope() ...@@ -77,10 +49,10 @@ Environment& Environment::getGlobalScope()
return m_upper->getGlobalScope(); return m_upper->getGlobalScope();
} }
   
cli::CommandResult Environment::execute(const std::unique_ptr<cli::Command>& ast) cli::CommandResult Environment::execute(const std::unique_ptr<cli::Command>& command)
{ {
try { try {
cli::CommandResult res = ast->run(*this); const CommandResult res = command->run(*this);
   
if (res == CommandResult::CONTINUE || res == CommandResult::BREAK) if (res == CommandResult::CONTINUE || res == CommandResult::BREAK)
throw std::logic_error("There is no loop to continue/break."); throw std::logic_error("There is no loop to continue/break.");
...@@ -88,8 +60,16 @@ cli::CommandResult Environment::execute(const std::unique_ptr<cli::Command>& ast ...@@ -88,8 +60,16 @@ cli::CommandResult Environment::execute(const std::unique_ptr<cli::Command>& ast
return res; return res;
} catch (...) { } catch (...) {
alib::ExceptionHandler::handle(common::Streams::err); alib::ExceptionHandler::handle(common::Streams::err);
return cli::CommandResult::EXCEPTION; return CommandResult::EXCEPTION;
} }
} }
cli::CommandResult Environment::execute(const std::string& input)
{
return execute(Parser2::parseString(input));
}
cli::CommandResult Environment::execute(std::istream& input)
{
return execute(Parser2::parseStream(input));
}
   
} /* namespace cli */ } /* namespace cli */
#pragma once #pragma once
   
#include <istream>
#include <ext/concepts> #include <ext/concepts>
#include <ext/memory> #include <ext/memory>
#include <ext/typeinfo> #include <ext/typeinfo>
...@@ -119,10 +121,11 @@ public: ...@@ -119,10 +121,11 @@ public:
return m_result; return m_result;
} }
   
cli::CommandResult execute(const std::shared_ptr<cli::LineInterface>& lineInterface); cli::CommandResult execute(const std::string& input);
cli::CommandResult execute(const std::unique_ptr<cli::Command>& ast);
cli::CommandResult execute(std::istream& input);
   
cli::CommandResult execute_line(cli::CharSequence charSequence); cli::CommandResult execute(const std::unique_ptr<cli::Command>& command);
   
Environment& getGlobalScope(); Environment& getGlobalScope();
}; };
......
...@@ -14,9 +14,7 @@ template <typename T> ...@@ -14,9 +14,7 @@ template <typename T>
std::unique_ptr<cli::CommandList> oldParseFile(T&& stream, bool print) std::unique_ptr<cli::CommandList> oldParseFile(T&& stream, bool print)
{ {
try { try {
auto command = cli::Parser( auto command = cli::Parser::parse(cli::Lexer(cli::CharSequence(cli::IstreamLineInterface<T>(std::forward<T>(stream)))));
cli::Lexer(cli::CharSequence(cli::IstreamLineInterface<T>(std::forward<T>(stream)))))
.parse();
   
if (print) { if (print) {
command->print(Catch::cout()); command->print(Catch::cout());
......
...@@ -15,8 +15,9 @@ TEST_CASE("Udf eval", "[unit][cli]") ...@@ -15,8 +15,9 @@ TEST_CASE("Udf eval", "[unit][cli]")
{ {
cli::Environment environment; cli::Environment environment;
   
std::string foo("function mojeFunkce ( const auto & $mojepromenna ) returning auto begin\nreturn $mojepromenna;\nend"); environment.execute("function mojeFunkce ( const auto & $mojepromenna ) returning auto begin\n"
environment.execute(std::make_shared<cli::StringLineInterface>(cli::StringLineInterface(foo))); "return $mojepromenna;\n"
"end");
   
auto input = std::make_shared<abstraction::ValueHolder<std::string>>(std::string{"neco"}, true); auto input = std::make_shared<abstraction::ValueHolder<std::string>>(std::string{"neco"}, true);
   
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment