Skip to content
Snippets Groups Projects
Commit a81219a2 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

add support for type hinting in file statement

parent 9490756c
No related branches found
No related tags found
No related merge requests found
Showing
with 77 additions and 16 deletions
......@@ -5,6 +5,7 @@
#include <ast/statements/SingleStatement.h>
#include <ast/statements/ContainerStatement.h>
#include <ast/statements/ContainerFileStatement.h>
#include <ast/statements/FileStatement.h>
 
namespace cli {
 
......@@ -16,6 +17,7 @@ public:
virtual void eval ( SingleStatement & statement ) const = 0;
virtual void eval ( ContainerStatement & statement ) const = 0;
virtual void eval ( ContainerFileStatement & statement ) const = 0;
virtual void eval ( FileStatement & statement ) const = 0;
};
 
} /* namespace cli */
......
......@@ -25,6 +25,10 @@ public:
virtual void eval ( ContainerFileStatement & ) const override {
throw exception::CommonException ( "ContainerFileStatement cannot be categorized." );
}
virtual void eval ( FileStatement & ) const override {
throw exception::CommonException ( "FileStatement cannot be categorized." );
}
};
 
} /* namespace cli */
......
......@@ -18,6 +18,10 @@ public:
virtual void eval ( ContainerFileStatement & ) const override {
std::cout << "The cake is a lie, as well as the release date of HL3. GLaDOS told me." << std::endl;
}
virtual void eval ( FileStatement & ) const override {
std::cout << "The cake is a lie, as well as the release date of HL3. GLaDOS told me." << std::endl;
}
};
 
} /* namespace cli */
......
......@@ -24,6 +24,10 @@ public:
virtual void eval ( ContainerFileStatement & statement ) const override {
statement.setType ( m_type );
}
virtual void eval ( FileStatement & statement ) const override {
statement.setType ( m_type );
}
};
 
} /* namespace cli */
......
......@@ -2,7 +2,6 @@
#define _CLI_CONTAINER_FILE_STATEMENT_H_
 
#include <ast/Statement.h>
#include <abstraction/Registry.h>
 
namespace cli {
 
......
......@@ -3,6 +3,8 @@
#include <ast/Param.h>
#include <ast/Arg.h>
#include <abstraction/common/CastHelper.h>
#include <exception/CommonException.h>
#include <iostream>
 
namespace cli {
 
......
......@@ -2,9 +2,6 @@
#define _CLI_CONTAINER_STATEMENT_H_
 
#include <ast/Statement.h>
#include <abstraction/Registry.h>
#include <exception/CommonException.h>
#include <iostream>
 
namespace cli {
 
......
#include <ast/statements/FileStatement.h>
#include <ast/Statement.h>
#include <ast/Option.h>
#include <ast/Arg.h>
#include <abstraction/Registry.h>
namespace cli {
FileStatement::FileStatement ( ext::vector < std::unique_ptr < Option > > options, std::unique_ptr < Arg > file ) : m_file ( std::move ( file ) ), m_options ( std::move ( options ) ) {
for ( const std::unique_ptr < Option > & option : m_options ) {
option->eval ( * this );
}
}
std::shared_ptr < abstraction::OperationAbstraction > FileStatement::translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > &, Environment & environment ) const {
ext::deque < sax::Token > tokens = sax::FromXMLParserHelper::parseInput ( m_file->eval ( environment ) );
std::string type = m_type;
if ( type == "" )
type = tokens [ 0 ].getData ( );
return abstraction::Registry::getXmlParserAbstraction ( type, tokens );
}
void FileStatement::setType ( std::string type ) {
m_type = std::move ( type );
}
} /* namespace cli */
......@@ -2,22 +2,21 @@
#define _CLI_FILE_STATEMENT_H_
 
#include <ast/Statement.h>
#include <abstraction/Registry.h>
 
namespace cli {
 
class FileStatement final : public Statement {
std::unique_ptr < cli::Arg > m_file;
ext::vector < std::unique_ptr < Option > > m_options;
std::string m_type;
 
public:
FileStatement ( std::unique_ptr < Arg > file ) : m_file ( std::move ( file ) ) {
}
virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > &, Environment & environment ) const override {
ext::deque < sax::Token > tokens = sax::FromXMLParserHelper::parseInput ( m_file->eval ( environment ) );
std::string type = tokens [ 0 ].getData ( );
return abstraction::Registry::getXmlParserAbstraction ( type, tokens );
}
FileStatement ( ext::vector < std::unique_ptr < Option > > options, std::unique_ptr < Arg > file );
virtual std::shared_ptr < abstraction::OperationAbstraction > translateAndEval ( const std::shared_ptr < abstraction::OperationAbstraction > &, Environment & environment ) const override;
void setType ( std::string type );
};
 
} /* namespace cli */
......
......@@ -200,8 +200,19 @@ std::shared_ptr < Statement > Parser::in_redirect_statement ( ) {
std::unique_ptr < Arg > file = arg ( );
return std::make_shared < ContainerFileStatement > ( "Set", std::move ( options ), std::move ( file ) );
} else {
ext::vector < std::unique_ptr < Option > > options;
while ( check ( cli::Lexer::TokenType::COLON_SIGN ) ) {
match ( cli::Lexer::TokenType::COLON_SIGN );
std::unique_ptr < Option > res = type_option ( );
if ( ! res )
res = option ( );
if ( ! res )
throw exception::CommonException ( "Option not recognised" );
options.emplace_back ( std::move ( res ) );
}
std::unique_ptr < Arg > file = arg ( );
return std::make_shared < FileStatement > ( std::move ( file ) );
return std::make_shared < FileStatement > ( std::move ( options ), std::move ( file ) );
}
}
 
......
......@@ -96,6 +96,9 @@ void CliTest::testCreateUnique ( ) {
parser = cli::Parser ( cli::Lexer ( "execute One | Add <( Add (int) <#2 <(One) ) - | Neg (double) - | Divide (double) - <(One | (double) Add <(One) - )" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute <:type int #2" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute One > $res" ) );
parser.parse ( )->run ( environment );
 
......
......@@ -7,6 +7,7 @@
 
#include <abstraction/CastRegistry.hpp>
#include <abstraction/XmlFileWriterRegistry.hpp>
#include <abstraction/XmlParserRegistry.hpp>
#include <abstraction/NormalizeRegistry.hpp>
#include <abstraction/ValuePrinterRegistry.hpp>
#include <abstraction/ImmediateRegistry.hpp>
......@@ -44,6 +45,8 @@ public:
 
abstraction::ContainerRegistry::registerSet < int > ( );
 
abstraction::XmlParserRegistry::registerXmlParser < int > ( "int" );
abstraction::XmlFileWriterRegistry::registerXmlFileWriter < int > ( );
abstraction::XmlFileWriterRegistry::registerXmlFileWriter < double > ( );
abstraction::XmlFileWriterRegistry::registerXmlFileWriter < std::string > ( );
......
......@@ -38,11 +38,15 @@ class XmlParserRegistry {
}
 
public:
template < class ReturnType >
static void registerXmlParser ( std::string result ) {
getEntries ( ).insert ( std::make_pair ( result, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) );
}
template < class ReturnType >
static void registerXmlParser ( ) {
std::string ret = alib::xmlApi < ReturnType >::xmlTagName ( );
getEntries ( ).insert ( std::make_pair ( ret, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) );
registerXmlParser < ReturnType > ( ret );
}
 
static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & typeName, ext::deque < sax::Token > tokens );
......
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