topazc
Parser Class Reference

Parser class. More...

#include <parser.hpp>

Public Member Functions

 Parser (std::vector< Token > t, bool id)
std::vector< AST::StmtPtrparse ()
 Method for parsing tokens into AST tree.
void reset ()
 Method for resetting all parameters in parser.

Private Member Functions

AST::StmtPtr parse_stmt (bool from_for=false)
 Method for parsing only one statement.
AST::StmtPtr parse_var_decl_stmt ()
 Method for parsing of variable declaration.
AST::StmtPtr parse_var_asgn_stmt ()
 Method for parsing of variable assignment.
AST::StmtPtr parse_func_decl_stmt ()
 Method for parsing of functions declaration.
AST::StmtPtr parse_func_call_stmt ()
 Method for parsing of functions calling.
AST::Argument parse_argument ()
 Method for parsing of function argument.
AST::StmtPtr parse_return_stmt ()
 Method for parsing of 'return'.
AST::StmtPtr parse_if_else_stmt ()
 Method for parsing of control flow operators (aka if-else).
AST::StmtPtr parse_while_cycle_stmt ()
 Method for parsing of while cycle.
AST::StmtPtr parse_do_while_cycle_stmt ()
 Method for parsing of do-while cycle.
AST::StmtPtr parse_for_cycle_stmt ()
 Method for parsing of for cycle.
AST::StmtPtr parse_break_stmt ()
 Method for parsing of break statement.
AST::StmtPtr parse_continue_stmt ()
 Method for parsing of continue statement.
AST::StmtPtr parse_module_stmt ()
 Method for parsing of module definition.
AST::StmtPtr parse_use_module_stmt ()
 Method for parsing of import the module.
AST::StmtPtr parse_func_decl_proto_stmt ()
 Method for parsing of functions prototypes.
AST::StmtPtr parse_extern_stmt ()
 Method for parsing of declaration functions prototypes as extern.
AST::ExprPtr parse_expr ()
 Method for parsing expressions.
AST::ExprPtr parse_l_and_expr ()
 Method for parsing expression as 'logical and'.
AST::ExprPtr parse_l_or_expr ()
 Method for parsing expression as 'logical or'.
AST::ExprPtr parse_equality_expr ()
 Method for parsing expression as 'equality'.
AST::ExprPtr parse_comparation_expr ()
 Method for parsing expression as 'comparation'.
AST::ExprPtr parse_additive_expr ()
 Method for parsing expression as 'additive'.
AST::ExprPtr parse_multiplicative_expr ()
 Method for parsing expression as 'multiplicative'.
AST::ExprPtr parse_unary_expr ()
 Method for parsing expression as 'unary'.
AST::ExprPtr parse_primary_expr ()
 Method for parsing expression as 'primary'.
AST::ExprPtr parse_obj_chain_expr ()
 Method for parsing expresion as chain of objects.
Token peek (int32_t rpos=0) const
 Method for getting token from tokens by parser pos and passed offset.
bool match (TokenType type)
 Method for skipping the current token if its type is equal to the passed one.
Token consume (TokenType type, std::string err_msg, uint32_t line)
 Method for verifying the type of the current token.
AST::Type consume_type ()
 Method for verifyng the Topaz type by current tokens.
void consume_semicolon ()
 Method for skipping semicolon in the end of statement.
AST::TypeValue ttype_to_tvalue (TokenType type)
 Method for convert type of token to type of Topaz value.
bool is_compound_asgn_operator (Token token)
 Method for checking whether the passed token is a compound assignment operator.
AST::ExprPtr create_compound_asgn_operator (std::string var_name)
 Method for creating compound assignment operator and returns expression of assignment.
AST::ExprPtr create_inc_dec_operator (std::string var_name)
 Method for creating increment/decrement operator and returns expression of assignment.

Private Attributes

std::vector< Tokentokens
size_t tokens_count
uint32_t pos
bool is_debug

Detailed Description

Parser class.

Definition at line 14 of file parser.hpp.

Constructor & Destructor Documentation

◆ Parser()

Parser::Parser ( std::vector< Token > t,
bool id )
inline

Definition at line 22 of file parser.hpp.

Member Function Documentation

◆ consume()

Token Parser::consume ( TokenType type,
std::string err_msg,
uint32_t line )
private

Method for verifying the type of the current token.

This method checks whether the type of the current token is equal to the passed type. If equal, it skips and returns it, otherwise it throws an exception with the passed message

Parameters
typeType of token for verification
err_msgError message if verification is fault
lineLine coordinate. For exception
Returns
Verificated token

Definition at line 593 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ consume_semicolon()

void Parser::consume_semicolon ( )
private

Method for skipping semicolon in the end of statement.

This method checks the current token and if it is the semicolon, then skipping it, otherwise throwing exception

Definition at line 636 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ consume_type()

AST::Type Parser::consume_type ( )
private

Method for verifyng the Topaz type by current tokens.

This method checks the current keywords to collect the final Topaz type and returns it. If the tokens are in the wrong sequence or are not part of the type, an exception is thrown

Returns
Topaz type

Definition at line 601 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_compound_asgn_operator()

AST::ExprPtr Parser::create_compound_asgn_operator ( std::string var_name)
private

Method for creating compound assignment operator and returns expression of assignment.

This method checking current token and if it is compound assignment operator, then generating expression for assignment. For example 'a += 1' is equal 'a = a + 1' If current token isnt compound assignment operator, then throwing exception

Parameters
var_nameName of variable for assignment
Returns
Assingment expression

Definition at line 686 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_inc_dec_operator()

AST::ExprPtr Parser::create_inc_dec_operator ( std::string var_name)
private

Method for creating increment/decrement operator and returns expression of assignment.

This method checking current token and if it is increment/decrement operator, then generating expression of assignment. For example 'a++' is equal 'a = a + 1' If current token isnt increment/decrement operator, then throwing exception

Parameters
var_nameName of variable for assignment
Returns
Increment/Decrement expression

Definition at line 708 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_compound_asgn_operator()

bool Parser::is_compound_asgn_operator ( Token token)
private

Method for checking whether the passed token is a compound assignment operator.

This method checks whether the passed token is a compound assignment operator. If yes, it returns true, otherwise it returns false

Parameters
tokenToken for checking
Returns
True if token is compound assignment operator, and false otherwise

Definition at line 673 of file parser.cpp.

Here is the caller graph for this function:

◆ match()

bool Parser::match ( TokenType type)
private

Method for skipping the current token if its type is equal to the passed one.

If type of current token is equal to the passed type, then this token will be skipped and returns 'true'. Otherwise returns 'false'

Parameters
typeType of token wich need to skip
Returns
'true' if passed type is equal to type of current token and 'false' otherwise

Definition at line 585 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse()

std::vector< AST::StmtPtr > Parser::parse ( )

Method for parsing tokens into AST tree.

This method parsing all tokens, creates AST tree and returns it

Returns
AST tree

Definition at line 10 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_additive_expr()

AST::ExprPtr Parser::parse_additive_expr ( )
private

Method for parsing expression as 'additive'.

This method parsing all expressions as 'additive'. If type of current token isnt TOK_OP_PLUS (aka +) or TOK_OP_MINUS (aka -), then returns created expression

Returns
Parsed expression as 'additive'

Definition at line 420 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_argument()

AST::Argument Parser::parse_argument ( )
private

Method for parsing of function argument.

This method sets the syntax for defining a function argument, creates the AST element of the Argument and returns it

Returns
Argument

Definition at line 201 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_break_stmt()

AST::StmtPtr Parser::parse_break_stmt ( )
private

Method for parsing of break statement.

This method sets the syntax for break statement, creates the AST element of the BreakStmt and returns it

Returns
BreakStmt

Definition at line 285 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_comparation_expr()

AST::ExprPtr Parser::parse_comparation_expr ( )
private

Method for parsing expression as 'comparation'.

This method parsing all expressions as 'comparation'. If type of current token isnt TOK_OP_GT (aka >) or TOK_OP_GT_EQ (aka >=) or TOK_OP_LS (aka <) or TOK_OP_LS_EQ (aka <=), then returns created expression

Returns
Parsed expression as 'comparation'

Definition at line 397 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_continue_stmt()

AST::StmtPtr Parser::parse_continue_stmt ( )
private

Method for parsing of continue statement.

This method sets the syntax for continue statement, creates the AST element of the ContinueStmt and returns it

Returns
ContinueStmt

Definition at line 290 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_do_while_cycle_stmt()

AST::StmtPtr Parser::parse_do_while_cycle_stmt ( )
private

Method for parsing of do-while cycle.

This method sets the syntax for do-while cycle, creates the AST element of the DoWhileCycleStmt and returns it

Returns
DoWhileCycleStmt

Definition at line 257 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_equality_expr()

AST::ExprPtr Parser::parse_equality_expr ( )
private

Method for parsing expression as 'equality'.

This method parsing all expressions as 'equality'. If type of current token isnt TOK_OP_EQ_EQ (aka ==) or TOK_OP_NOT_EQ_EQ (aka !=), then returns created expression

Returns
Parsed expression as 'equality'

Definition at line 380 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_expr()

AST::ExprPtr Parser::parse_expr ( )
private

Method for parsing expressions.

This method parsing expression, creates the AST element and returns it

Returns
Parsed expression

Definition at line 358 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_extern_stmt()

AST::StmtPtr Parser::parse_extern_stmt ( )
private

Method for parsing of declaration functions prototypes as extern.

This method sets the syntax for extern functions declaration, creates the AST element of the ExternStmt and returns it

Returns
ExternStmt

Definition at line 347 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_for_cycle_stmt()

AST::StmtPtr Parser::parse_for_cycle_stmt ( )
private

Method for parsing of for cycle.

This method sets the syntax for for cycle, creates the AST element of the ForCycleStmt and returns it

Returns
ForCycleStmt

Definition at line 269 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_func_call_stmt()

AST::StmtPtr Parser::parse_func_call_stmt ( )
private

Method for parsing of functions calling.

This method sets the syntax for calling a function, creates the AST element of the FuncCallStmt and returns it

Returns
FuncCallStmt

Definition at line 185 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_func_decl_proto_stmt()

AST::StmtPtr Parser::parse_func_decl_proto_stmt ( )
private

Method for parsing of functions prototypes.

This method sets the syntax for function prototype declaration, creates the AST element of the FuncDeclStmt without body and returns it

Returns
FuncDeclStmt without body

Definition at line 320 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_func_decl_stmt()

AST::StmtPtr Parser::parse_func_decl_stmt ( )
private

Method for parsing of functions declaration.

This method sets the syntax for defining a function, creates the AST element of the FuncDeclStmt and returns it

Returns
FuncDeclStmt

Definition at line 152 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_if_else_stmt()

AST::StmtPtr Parser::parse_if_else_stmt ( )
private

Method for parsing of control flow operators (aka if-else).

This method sets the syntax for control flow operators, creates the AST element of the IfElseStmt and returns it

Returns
IfElseStmt

Definition at line 224 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_l_and_expr()

AST::ExprPtr Parser::parse_l_and_expr ( )
private

Method for parsing expression as 'logical and'.

This method parsing all expressions as 'logical and'. If type of current token isnt TOK_OP_L_AND (aka &&), then returns created expression

Returns
Parsed expression as 'logical and'

Definition at line 362 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_l_or_expr()

AST::ExprPtr Parser::parse_l_or_expr ( )
private

Method for parsing expression as 'logical or'.

This method parsing all expressions as 'logical or'. If type of current token isnt TOK_OP_L_OR (aka ||), then returns created expression

Returns
Parsed expression as 'logical or'

Definition at line 371 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_module_stmt()

AST::StmtPtr Parser::parse_module_stmt ( )
private

Method for parsing of module definition.

This method sets the syntax for module definition, creates the AST element of the ModuleStmt and returns it

Returns
ModuleStmt

Definition at line 295 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_multiplicative_expr()

AST::ExprPtr Parser::parse_multiplicative_expr ( )
private

Method for parsing expression as 'multiplicative'.

This method parsing all expressions as 'multiplicative'. If type of current token isnt TOK_OP_MULT (aka *) or TOK_OP_DIV (aka /) or TOK_OP_MODULO (aka %), then returns created expression

Returns
Parsed expression as 'multiplicative'

Definition at line 437 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_obj_chain_expr()

AST::ExprPtr Parser::parse_obj_chain_expr ( )
private

Method for parsing expresion as chain of objects.

This method parsing all expressions as chain of objects. Elements of chain separated of '.' (for example: obj1.obj2)

Returns
Parsed expression as chain of objects

Definition at line 548 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_primary_expr()

AST::ExprPtr Parser::parse_primary_expr ( )
private

Method for parsing expression as 'primary'.

This method parsing all expressions as 'primary'. If type of current token isnt literal or identifier, then throwing exception

Returns
Parsed expression as 'primary'

Definition at line 479 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_return_stmt()

AST::StmtPtr Parser::parse_return_stmt ( )
private

Method for parsing of 'return'.

This method sets the syntax for 'return' statement, creates the AST element of the ReturnStmt and returns it

Returns
ReturnStmt

Definition at line 215 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_stmt()

AST::StmtPtr Parser::parse_stmt ( bool from_for = false)
private

Method for parsing only one statement.

This method parsing one statement and returns it

Parameters
from_forFlag that indicates that the method is being called to process the for loop. If true, then processing for the for loop, otherwise not
Returns
Parsed statement

Definition at line 26 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_unary_expr()

AST::ExprPtr Parser::parse_unary_expr ( )
private

Method for parsing expression as 'unary'.

This method parsing all expressions as 'unary'. If type of current token isnt TOK_OP_L_NOT (aka !) or TOK_OP_MINUS (aka -), then returns created expression

Returns
Parsed expression as 'unary'

Definition at line 457 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_use_module_stmt()

AST::StmtPtr Parser::parse_use_module_stmt ( )
private

Method for parsing of import the module.

This method sets the syntax for import the module, creates the AST element of the UseModuleStmt and returns it

Returns
UseModuleStmt

Definition at line 309 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_var_asgn_stmt()

AST::StmtPtr Parser::parse_var_asgn_stmt ( )
private

Method for parsing of variable assignment.

This method sets the syntax for assignment a variable, creates the AST element of the VarAsgnStmt and returns it

Returns
VarAsgnStmt

Definition at line 131 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_var_decl_stmt()

AST::StmtPtr Parser::parse_var_decl_stmt ( )
private

Method for parsing of variable declaration.

This method sets the syntax for defining a variable, creates the AST element of the VarDeclStmt and returns it

Returns
VarDeclStmt

Definition at line 117 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_while_cycle_stmt()

AST::StmtPtr Parser::parse_while_cycle_stmt ( )
private

Method for parsing of while cycle.

This method sets the syntax for while cycle, creates the AST element of the WhileCycleStmt and returns it

Returns
WhileCycleStmt

Definition at line 246 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peek()

Token Parser::peek ( int32_t rpos = 0) const
private

Method for getting token from tokens by parser pos and passed offset.

This method getting getting token from tokens by parser pos and passed offset If pos + offset out of bounds of range tokens, then throwing exception

Parameters
rposOffset
Returns
Token from tokens

Definition at line 576 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void Parser::reset ( )

Method for resetting all parameters in parser.

Definition at line 20 of file parser.cpp.

Here is the caller graph for this function:

◆ ttype_to_tvalue()

AST::TypeValue Parser::ttype_to_tvalue ( TokenType type)
private

Method for convert type of token to type of Topaz value.

This method converts the passed token type to the Topaz value type. If the conversion fails, an exception is thrown

Parameters
typeType of token for convertion
Returns
Converted type of Topaz value

Definition at line 648 of file parser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ is_debug

bool Parser::is_debug
private

Flag for debug exception

Definition at line 19 of file parser.hpp.

◆ pos

uint32_t Parser::pos
private

Current position in tokens

Definition at line 18 of file parser.hpp.

◆ tokens

std::vector<Token> Parser::tokens
private

Tokens (from Lexer)

Definition at line 16 of file parser.hpp.

◆ tokens_count

size_t Parser::tokens_count
private

Count of tokens

Definition at line 17 of file parser.hpp.


The documentation for this class was generated from the following files:
  • /mnt/disk_ext4_0/topazlang/include/parser/parser.hpp
  • /mnt/disk_ext4_0/topazlang/src/parser/parser.cpp