From d4503c42cf03df07569d101910160b2027f6fdc1 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 17 Aug 2017 22:00:11 +0200
Subject: [PATCH] introspection cli commands accept env variables

---
 .../command/AlgorithmsIntrospectionCommand.h  | 18 ++++++----
 .../src/command/CastsIntrospectionCommand.h   | 12 ++++---
 .../command/DataTypesIntrospectionCommand.h   | 16 +++++----
 alib2cli/src/command/HelpCommand.h            | 16 +++++----
 .../command/OverloadsIntrospectionCommand.h   | 26 ++++++++++++++
 alib2cli/src/parser/Parser.cpp                | 36 ++++++++++++-------
 alib2cli/src/parser/Parser.h                  |  4 +--
 alib2common/src/abstraction/Registry.cpp      |  8 ++---
 alib2common/src/abstraction/Registry.h        |  3 +-
 9 files changed, 96 insertions(+), 43 deletions(-)
 create mode 100644 alib2cli/src/command/OverloadsIntrospectionCommand.h

diff --git a/alib2cli/src/command/AlgorithmsIntrospectionCommand.h b/alib2cli/src/command/AlgorithmsIntrospectionCommand.h
index 7fb1c2f979..1f6220c104 100644
--- a/alib2cli/src/command/AlgorithmsIntrospectionCommand.h
+++ b/alib2cli/src/command/AlgorithmsIntrospectionCommand.h
@@ -7,19 +7,23 @@
 namespace cli {
 
 class AlgorithmsIntrospectionCommand : public Command {
-	std::string m_param;
+	std::unique_ptr < cli::Arg > m_param;
 
 public:
-	AlgorithmsIntrospectionCommand ( std::string param ) : m_param ( std::move ( param ) ) {
+	AlgorithmsIntrospectionCommand ( std::unique_ptr < cli::Arg > param ) : m_param ( std::move ( param ) ) {
 	}
 
-	virtual Command::Result run ( Environment & ) const override {
-		if ( m_param == "" ) {
+	virtual Command::Result run ( Environment & environment ) const override {
+		std::string param;
+		if ( m_param != nullptr )
+			param = m_param->eval ( environment );
+
+		if ( param == "" ) {
 			abstraction::Registry::listAlgorithms ( );
-		} else if ( m_param.find ( "::", m_param.size ( ) - 2 ) != std::string::npos ) {
-			abstraction::Registry::listAlgorithmGroup ( m_param );
+		} else if ( param.find ( "::", param.size ( ) - 2 ) != std::string::npos ) {
+			abstraction::Registry::listAlgorithmGroup ( param );
 		} else {
-			abstraction::Registry::listAlgorithmOverloads ( m_param );
+			throw exception::CommonException ( "Invalid Algorithm introspection param" );
 		}
 		return Command::Result::OK;
 	}
diff --git a/alib2cli/src/command/CastsIntrospectionCommand.h b/alib2cli/src/command/CastsIntrospectionCommand.h
index 72fee3f740..190bd60bcd 100644
--- a/alib2cli/src/command/CastsIntrospectionCommand.h
+++ b/alib2cli/src/command/CastsIntrospectionCommand.h
@@ -7,20 +7,22 @@
 namespace cli {
 
 class CastsIntrospectionCommand : public Command {
-	std::string m_param;
+	std::unique_ptr < cli::Arg > m_param;
 	bool m_from;
 	bool m_to;
 
 public:
-	CastsIntrospectionCommand ( std::string param, bool from, bool to ) : m_param ( std::move ( param ) ), m_from ( from ), m_to ( to ) {
+	CastsIntrospectionCommand (std::unique_ptr < cli::Arg > param, bool from, bool to ) : m_param ( std::move ( param ) ), m_from ( from ), m_to ( to ) {
 	}
 
-	virtual Command::Result run ( Environment & ) const override {
+	virtual Command::Result run ( Environment & environment ) const override {
+		std::string param = m_param->eval ( environment );
+
 		if ( m_from )
-			abstraction::Registry::listCastsFrom ( m_param );
+			abstraction::Registry::listCastsFrom ( param );
 
 		if ( m_to )
-			abstraction::Registry::listCastsTo ( m_param );
+			abstraction::Registry::listCastsTo ( param );
 
 		return Command::Result::OK;
 	}
diff --git a/alib2cli/src/command/DataTypesIntrospectionCommand.h b/alib2cli/src/command/DataTypesIntrospectionCommand.h
index 0eb6686fee..a520c4dd3d 100644
--- a/alib2cli/src/command/DataTypesIntrospectionCommand.h
+++ b/alib2cli/src/command/DataTypesIntrospectionCommand.h
@@ -7,17 +7,21 @@
 namespace cli {
 
 class DataTypesIntrospectionCommand : public Command {
-	std::string m_param;
+	std::unique_ptr < cli::Arg > m_param;
 
 public:
-	DataTypesIntrospectionCommand ( std::string param ) : m_param ( std::move ( param ) ) {
+	DataTypesIntrospectionCommand ( std::unique_ptr < cli::Arg > param ) : m_param ( std::move ( param ) ) {
 	}
 
-	virtual Command::Result run ( Environment & ) const override {
-		if ( m_param == "" ) {
+	virtual Command::Result run ( Environment & environment ) const override {
+		std::string param;
+		if ( m_param != nullptr )
+			param = m_param->eval ( environment );
+
+		if ( param == "" ) {
 			abstraction::Registry::listDataTypes ( );
-		} else if ( m_param.find ( "::", m_param.size ( ) - 2 ) != std::string::npos ) {
-			abstraction::Registry::listDataTypeGroup ( m_param );
+		} else if ( param.find ( "::", param.size ( ) - 2 ) != std::string::npos ) {
+			abstraction::Registry::listDataTypeGroup ( param );
 		} else {
 			throw exception::CommonException ( "Invalid DataType introspection param" );
 		}
diff --git a/alib2cli/src/command/HelpCommand.h b/alib2cli/src/command/HelpCommand.h
index b88e3f835d..1b9bbb0876 100644
--- a/alib2cli/src/command/HelpCommand.h
+++ b/alib2cli/src/command/HelpCommand.h
@@ -7,14 +7,18 @@
 namespace cli {
 
 class HelpCommand : public Command {
-	std::string m_command;
+	std::unique_ptr < cli::Arg > m_command;
 
 public:
-	HelpCommand ( std::string command ) : m_command ( std::move ( command ) ) {
+	HelpCommand ( std::unique_ptr < cli::Arg > command ) : m_command ( std::move ( command ) ) {
 	}
 
-	virtual Command::Result run ( Environment & ) const override {
-		if ( m_command == "execute" ) {
+	virtual Command::Result run ( Environment & environment ) const override {
+		std::string command;
+		if ( m_command != nullptr )
+			command = m_command->eval ( environment );
+
+		if ( command == "execute" ) {
 			std::cout << "Execute command executes statements and either prints the result or writes the result to a file" << std::endl;
 			std::cout << "statement_list:" << std::endl;
 			std::cout << "statement ( | statement )* - at least one statement followed by a pipe separated sequence of other statements" << std::endl;
@@ -53,7 +57,7 @@ public:
 			std::cout << ">$:identifier - a value to a variable, identified by an environment variable (ResultVariableStatement)" << std::endl;
 			std::cout << ">             - a value is discarded" << std::endl;
 			std::cout << "              - an empty output specifier prints the result to the stdout (ResultPrintStatement)" << std::endl;
-		} else if ( m_command == "" ) {
+		} else if ( command == "" ) {
 			std::cout << "Simple help for the query language" << std::endl;
 			std::cout << "" << std::endl;
 			std::cout << "command quit: quits the processor." << std::endl;
@@ -63,7 +67,7 @@ public:
 			std::cout << "" << std::endl;
 			std::cout << "for details use help of individual command" << std::endl;
 		} else {
-			std::cout << "The command " << m_command << " either does not exist or does not have a help entry." << std::endl;
+			std::cout << "The command " << command << " either does not exist or does not have a help entry." << std::endl;
 		}
 		return Command::Result::OK;
 	}
diff --git a/alib2cli/src/command/OverloadsIntrospectionCommand.h b/alib2cli/src/command/OverloadsIntrospectionCommand.h
new file mode 100644
index 0000000000..a7b783aa24
--- /dev/null
+++ b/alib2cli/src/command/OverloadsIntrospectionCommand.h
@@ -0,0 +1,26 @@
+#ifndef _CLI_OVERLOADS_INTROSPECTION_COMMAND_H_
+#define _CLI_OVERLOADS_INTROSPECTION_COMMAND_H_
+
+#include <command/Command.h>
+#include <environment/Environment.h>
+
+namespace cli {
+
+class OverloadsIntrospectionCommand : public Command {
+	std::unique_ptr < cli::Arg > m_param;
+
+public:
+	OverloadsIntrospectionCommand ( std::unique_ptr < cli::Arg > param ) : m_param ( std::move ( param ) ) {
+	}
+
+	virtual Command::Result run ( Environment & environment ) const override {
+		std::string param = m_param->eval ( environment );
+
+		abstraction::Registry::listOverloads ( param );
+		return Command::Result::OK;
+	}
+};
+
+} /* namespace cli */
+
+#endif /* _CLI_OVERLOADS_INTROSPECTION_COMMAND_H_ */
diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp
index 1645afcea0..dd0be7d238 100644
--- a/alib2cli/src/parser/Parser.cpp
+++ b/alib2cli/src/parser/Parser.cpp
@@ -24,6 +24,7 @@
 #include <command/QuitCommand.h>
 #include <command/HelpCommand.h>
 #include <command/AlgorithmsIntrospectionCommand.h>
+#include <command/OverloadsIntrospectionCommand.h>
 #include <command/DataTypesIntrospectionCommand.h>
 #include <command/CastsIntrospectionCommand.h>
 #include <command/SetCommand.h>
@@ -57,6 +58,20 @@ std::unique_ptr < Arg > Parser::arg ( ) {
 	}
 }
 
+std::unique_ptr < Arg > Parser::optional_arg ( ) {
+	if ( check ( cli::Lexer::TokenType::HASH_SIGN ) ) {
+		match ( cli::Lexer::TokenType::HASH_SIGN );
+		std::string value = getTokenValue ( );
+		match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER );
+		return std::make_unique < BindedArg > ( std::move ( value ) );
+	} else if ( check ( cli::Lexer::TokenType::IDENTIFIER ) ) {
+		std::string value = matchIdentifier ( );
+		return std::make_unique < ImmediateArg > ( value );
+	} else {
+		return nullptr;
+	}
+}
+
 std::unique_ptr < Param > Parser::in_redirect_param ( ) {
 	if ( check ( cli::Lexer::TokenType::LEFT_PAREN ) ) {
 		match ( cli::Lexer::TokenType::LEFT_PAREN );
@@ -178,14 +193,6 @@ void Parser::result ( std::shared_ptr < StatementList > & list ) {
 	}
 }
 
-std::string Parser::optional_identifier ( ) {
-	if ( check ( cli::Lexer::TokenType::IDENTIFIER ) ) {
-		return matchIdentifier ( );
-	} else {
-		return "";
-	}
-}
-
 std::pair < bool, bool > Parser::introspect_cast_from_to ( ) {
 	bool from;
 	bool to;
@@ -207,18 +214,23 @@ std::pair < bool, bool > Parser::introspect_cast_from_to ( ) {
 std::unique_ptr < Command > Parser::introspect_command ( ) {
 	if ( check_nonreserved_kw ( "algorithms" ) ) {
 		match_nonreserved_kw ( "algorithms" );
-		std::string param = optional_identifier ( );
+		std::unique_ptr < cli::Arg > param = optional_arg ( );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < AlgorithmsIntrospectionCommand > ( std::move ( param ) );
+	} else if ( check_nonreserved_kw ( "overloads" ) ) {
+		match_nonreserved_kw ( "overloads" );
+		std::unique_ptr < cli::Arg > param = arg ( );
+		match ( cli::Lexer::TokenType::END );
+		return std::make_unique < OverloadsIntrospectionCommand > ( std::move ( param ) );
 	} else if ( check_nonreserved_kw ( "datatypes" ) ) {
 		match_nonreserved_kw ( "datatypes" );
-		std::string param = optional_identifier ( );
+		std::unique_ptr < cli::Arg > param = optional_arg ( );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < DataTypesIntrospectionCommand > ( std::move ( param ) );
 	} else if ( check_nonreserved_kw ( "casts" ) ) {
 		match_nonreserved_kw ( "casts" );
 		std::pair < bool, bool > from_to = introspect_cast_from_to ( );
-		std::string param = optional_identifier ( );
+		std::unique_ptr < cli::Arg > param = arg ( );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < CastsIntrospectionCommand > ( std::move ( param ), from_to.first, from_to.second );
 	} else {
@@ -239,7 +251,7 @@ std::unique_ptr < Command > Parser::parse ( ) {
 		return std::make_unique < QuitCommand > ( );
 	} else if ( check_nonreserved_kw ( "help" ) ) {
 		match_nonreserved_kw ( "help" );
-		std::string command = optional_identifier ( );
+		std::unique_ptr < cli::Arg > command = optional_arg ( );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < HelpCommand > ( std::move ( command ) );
 	} else if ( check_nonreserved_kw ( "introspect" ) ) {
diff --git a/alib2cli/src/parser/Parser.h b/alib2cli/src/parser/Parser.h
index 4fce5ef617..7300c00c15 100644
--- a/alib2cli/src/parser/Parser.h
+++ b/alib2cli/src/parser/Parser.h
@@ -75,6 +75,8 @@ public:
 
 	std::unique_ptr < Arg > arg ( );
 
+	std::unique_ptr < Arg > optional_arg ( );
+
 	std::unique_ptr < Param > in_redirect_param ( );
 
 	std::unique_ptr < Param > param ( );
@@ -89,8 +91,6 @@ public:
 
 	void result ( std::shared_ptr < StatementList > & list );
 
-	std::string optional_identifier ( );
-
 	std::pair < bool, bool > introspect_cast_from_to ( );
 
 	std::unique_ptr < Command > introspect_command ( );
diff --git a/alib2common/src/abstraction/Registry.cpp b/alib2common/src/abstraction/Registry.cpp
index 179b436b1e..eb33ceef83 100644
--- a/alib2common/src/abstraction/Registry.cpp
+++ b/alib2common/src/abstraction/Registry.cpp
@@ -22,10 +22,6 @@ void Registry::listAlgorithmGroup ( const std::string & group ) {
 	AlgorithmRegistry::listGroup ( group );
 }
 
-void Registry::listAlgorithmOverloads ( const std::string & algorithm ) {
-	AlgorithmRegistry::listOverloads ( algorithm );
-}
-
 void Registry::listAlgorithms ( ) {
 	AlgorithmRegistry::list ( );
 }
@@ -46,6 +42,10 @@ void Registry::listCastsTo ( const std::string & type ) {
 	CastRegistry::listTo ( type );
 }
 
+void Registry::listOverloads ( const std::string & algorithm ) {
+	AlgorithmRegistry::listOverloads ( algorithm );
+}
+
 std::shared_ptr < abstraction::OperationAbstraction > Registry::getAlgorithmAbstraction ( const std::string & name, const ext::vector < std::string > & paramTypes ) {
 	return AlgorithmRegistry::getAbstraction ( name, paramTypes );
 }
diff --git a/alib2common/src/abstraction/Registry.h b/alib2common/src/abstraction/Registry.h
index 677da32a05..5a9d91b7e6 100644
--- a/alib2common/src/abstraction/Registry.h
+++ b/alib2common/src/abstraction/Registry.h
@@ -15,7 +15,6 @@ namespace abstraction {
 class Registry {
 public:
 	static void listAlgorithmGroup ( const std::string & group );
-	static void listAlgorithmOverloads ( const std::string & algorithm );
 	static void listAlgorithms ( );
 
 	static void listDataTypes ( );
@@ -24,6 +23,8 @@ public:
 	static void listCastsFrom ( const std::string & type );
 	static void listCastsTo ( const std::string & type );
 
+	static void listOverloads ( const std::string & algorithm );
+
 	static std::shared_ptr < abstraction::OperationAbstraction > getAlgorithmAbstraction ( const std::string & name, const ext::vector < std::string > & paramTypes );
 	static std::shared_ptr < abstraction::OperationAbstraction > getAlgorithmAbstraction ( const std::string & name, const ext::vector < std::string > & paramTypes, bool & unwrap, bool & normalize );
 	static std::shared_ptr < abstraction::OperationAbstraction > getCastAbstraction ( const std::string & target, const std::string & param, bool & normalize );
-- 
GitLab