Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Parser.h 1.93 KiB
#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 ) 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 ) {
		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::shared_ptr < StatementList > statement_list ( );

	std::unique_ptr < Param > in_redirect_param ( );

	std::unique_ptr < Param > param ( );

	std::shared_ptr < Statement > single_statement ( );

	std::shared_ptr < StatementList > statement_list_cont ( );

	std::shared_ptr < StatementList > execute ( );

};

} /* namespace cli */

#endif /* _CLI_PARSER_H_ */