Newer
Older
#ifndef _CLI_PARSER_H_
#define _CLI_PARSER_H_
#include <ast/Param.h>
#include <ast/Statement.h>
#include <ast/StatementList.h>
#include <lexer/Lexer.h>
#include <exception/CommonException.h>
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 ( ) ) {
}
bool check ( cli::Lexer::TokenType type, cli::Lexer::TokenType type2 ) const {
return m_current.m_type == type || m_current.m_type == type2;
}
bool check ( cli::Lexer::TokenType type ) const {
return m_current.m_type == type;
}
bool check_nonreserved_kw ( const std::string & kw ) const {
return m_current.m_type == Lexer::TokenType::IDENTIFIER && m_current.m_value == kw;
}
bool match ( cli::Lexer::TokenType type, cli::Lexer::TokenType type2 ) {
if ( ! check ( type, type2 ) )
throw exception::CommonException ( "Mismatched token while matching a token." );
m_current = m_lexer.nextToken ( );
return true;
}
bool match ( cli::Lexer::TokenType type ) {
if ( ! check ( type ) )
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 ( );