Newer
Older
#ifndef _CLI_PARSER_H_
#define _CLI_PARSER_H_
#include <ast/Param.h>
#include <ast/Statement.h>
#include <ast/StatementList.h>
#include <command/Command.h>
#include <lexer/Lexer.h>
#include <exception/CommonException.h>
#include <algorithm>
namespace cli {
class Parser {
cli::Lexer m_lexer;
cli::Lexer::Token m_current;
public:
Parser ( cli::Lexer lexer ) : m_lexer ( std::move ( lexer ) ), m_current ( m_lexer.nextToken ( ) ) {
}
template < class ... Tokens >
bool check ( Tokens ... tokens ) const {
// TODO repace in c++17 with fold expressions
return std::orAll ( m_current.m_type == tokens ... );
}
bool check_nonreserved_kw ( const std::string & kw ) const {
return m_current.m_type == Lexer::TokenType::IDENTIFIER && m_current.m_value == kw;
}
template < class ... Tokens >
bool match ( Tokens ... tokens ) {
if ( ! check ( tokens ... ) )
throw exception::CommonException ( "Mismatched token while matching a token." );
m_current = m_lexer.nextToken ( );
return true;
}
bool match_nonreserved_kw ( const std::string & kw ) {
if ( ! check_nonreserved_kw ( kw ) )
throw exception::CommonException ( "Mismatched token while matching a token." );
m_current = m_lexer.nextToken ( );
return true;
}
std::string matchIdentifier ( ) {
if ( ! check ( Lexer::TokenType::IDENTIFIER ) )
throw exception::CommonException ( "Mismatched token while matching a token." );
std::string res = m_current.m_value;
m_current = m_lexer.nextToken ( );
return res;
}
int matchInteger ( ) {
if ( ! check ( Lexer::TokenType::INTEGER ) )
throw exception::CommonException ( "Mismatched token while matching a token." );
int res = std::from_string < int > ( m_current.m_value );
m_current = m_lexer.nextToken ( );
return res;
}
std::string getTokenValue ( ) {
return m_current.m_value;
}
std::shared_ptr < StatementList > statement_list ( );
std::unique_ptr < Param > in_redirect_param ( );
std::unique_ptr < Param > param ( );
std::shared_ptr < Statement > single_statement ( );
std::unique_ptr < Statement > out_redirect ( );
std::shared_ptr < StatementList > statement_list_cont ( );
std::unique_ptr < Command > parse ( );
};
} /* namespace cli */
#endif /* _CLI_PARSER_H_ */