From 6c1c6891b007d6e7549ffb9caa0ff2e1530624c0 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Mon, 6 Aug 2018 16:36:30 +0200
Subject: [PATCH] add cli grammar

---
 alib2cli/src/parser/Parser.cpp | 18 ++-----
 docs/userGuide/userGuide.tex   | 98 ++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp
index db203ae77f..cde0bcf342 100644
--- a/alib2cli/src/parser/Parser.cpp
+++ b/alib2cli/src/parser/Parser.cpp
@@ -65,14 +65,8 @@ 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 );
+	if ( check ( cli::Lexer::TokenType::HASH_SIGN, cli::Lexer::TokenType::IDENTIFIER ) ) {
+		return arg ( );
 	} else {
 		return nullptr;
 	}
@@ -281,7 +275,6 @@ std::unique_ptr < Command > Parser::introspect_command ( ) {
 	if ( check_nonreserved_kw ( "algorithms" ) ) {
 		match_nonreserved_kw ( "algorithms" );
 		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" );
@@ -292,18 +285,15 @@ std::unique_ptr < Command > Parser::introspect_command ( ) {
 			templateArgs.emplace_back ( template_arg ( ) );
 		}
 
-		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < OverloadsIntrospectionCommand > ( std::move ( param ), std::move ( templateArgs ) );
 	} else if ( check_nonreserved_kw ( "datatypes" ) ) {
 		match_nonreserved_kw ( "datatypes" );
 		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::unique_ptr < cli::Arg > param = optional_arg ( );
-		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < CastsIntrospectionCommand > ( std::move ( param ), from_to.first, from_to.second );
 	} else {
 		throw exception::CommonException ( "Mismatched set while expanding param introspect_command." );
@@ -328,7 +318,9 @@ std::unique_ptr < Command > Parser::parse ( ) {
 		return std::make_unique < HelpCommand > ( std::move ( command ) );
 	} else if ( check_nonreserved_kw ( "introspect" ) ) {
 		match_nonreserved_kw ( "introspect" );
-		return introspect_command ( );
+		std::unique_ptr < Command > command = introspect_command ( );
+		match ( cli::Lexer::TokenType::END );
+		return command;
 	} else if ( check_nonreserved_kw ( "set" ) ) {
 		match_nonreserved_kw ( "set" );
 		std::string param = getTokenValue ( );
diff --git a/docs/userGuide/userGuide.tex b/docs/userGuide/userGuide.tex
index c61771fed7..ba9d4d2b85 100644
--- a/docs/userGuide/userGuide.tex
+++ b/docs/userGuide/userGuide.tex
@@ -523,6 +523,104 @@ This chapter will mostly focus on describing current status of the command line
 
 \section{Language description}
 
+The language is described by syntax similar to EBNF. The grammar's initial symbol is parse. The terminal symbols are in uppercase, nonterminals in lowercase. Terminal symbols starting with KW\_ prefix represent nonreserved keywords of value what is after the underscore. Integer is a sequence of digits, identifier is letter followed by letters and digits and string is double quoted character sequence. End is a special terminal symbol representing the end of the input.
+
+\begin{lstlisting}
+category_option
+	:	COLON_SIGN ( INTEGER | IDENTIFIER )
+	|
+;
+
+type_option
+	:	COLON_SIGN ( INTEGER | IDENTIFIER )
+	|
+;
+
+arg
+	:	HASH_SIGN ( INTEGER | IDENTIFIER )
+	|	IDENTIFIER
+	;
+
+optional_arg
+	:	arg
+	|
+	;
+
+template_arg
+	:	AT_SIGN arg
+	;
+
+in_redirect_file
+	:	( LEFT_BRACKET arg RIGHT_BRACKET )? type_option template_arg* arg
+	;
+
+in_redirect
+	:	LEFT_PAREN statement_list RIGHT_PAREN
+	|	in_redirect_file
+	;
+
+common
+	:	DOLAR_SIGN arg
+	|	IN_REDIRECT in_redirect
+	|	STRING
+	|	INTEGER
+	|	HASH_SIGN ( INTEGER | IDENTIFIER )
+	|	LEFT_BRACE type_option ( CARET_SIGN? param ) * RIGHT_BRACE
+	;
+
+param
+	:	common
+	|	DASH_SIGN
+	|	IDENTIFIER
+	|	LEFT_PAREN arg RIGHT_PAREN CARET_SIGN? param
+	;
+
+statement
+	:	common ( );
+	|	IDENTIFIER template_arg category_option ( CARET_SIGN? param )*
+	|	LEFT_PAREN arg RIGHT_PAREN CARET_SIGN? move_arg statement
+	;
+
+statement_list
+	:	statement ( PIPE_SIGN statement )*
+	;
+
+out_redirect_file
+	:	( LEFT_BRACKET arg RIGHT_BRACKET )? arg
+	;
+
+out_redirect
+	:	DOLAR_SIGN arg
+	|
+	|	out_redirect_file
+	;
+
+result
+	|	out_redirect
+	|
+	;
+
+introspect_cast_from_to
+	:	( COLON_SIGN ( KW_FROM | KW_TO ) )*
+	;
+
+introspect_command
+	:	KW_ALGORITHMS optional_arg END
+	|	KW_OVERLOADS arg template_arg* END
+	|	KW_DATATYPES optional_arg END
+	|	KW_CASTS introspect_cast_from_to optional_arg END
+	;
+
+parse
+	:	EXECUTE statement_list result END
+	|	QUIT END
+	|	HELP optional_arg END
+	|	INTROSPECT introspect_command END
+	|	SET ( INTEGER | IDENTIFIER ) ( INTEGER | IDENTIFIER | STRING ) END
+	|	LOAD ( INTEGER | IDENTIFIER | STRING ) END
+	;
+\end{lstlisting}
+
 \section{Builtin commands}
 
 
-- 
GitLab