11 std::vector<AST::StmtPtr> stmts;
110 std::stringstream ss;
111 ss <<
"Expected statement but got \033[0m'" <<
peek().
value <<
"'\033[31m. Please check statement to mistakes";
121 std::stringstream ss;
122 ss <<
"Expected variable name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
128 return std::make_unique<AST::VarDeclStmt>(access, type, std::move(expr), name, first_token.
line);
133 bool is_deref =
false;
149 return std::make_unique<AST::VarAsgnStmt>(var_token.
value, std::move(expr), is_deref, var_token.
line);
155 std::stringstream ss;
156 ss <<
"Expected function name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
158 std::vector<AST::Argument> args;
164 ss <<
"Expected \033[0m','\033[31m between function arguments.\nPlease replace \033[0m'";
165 ss << args[args.size() - 1].name <<
": " << args[args.size() - 1].type.to_str() <<
" " <<
peek().
value <<
"'\033[31m with: \033[0m'"
166 << args[args.size() - 1].name <<
": " << args[args.size() - 1].type.to_str() <<
", " <<
peek().
value <<
"'";
177 std::vector<AST::StmtPtr> block;
178 consume(
TOK_OP_LBRACE,
"Expected \033[0m'{'\033[31m after funtion arguments. Prototypes of functions is unsupported in current Topaz compiler version",
peek().line);
182 return std::make_unique<AST::FuncDeclStmt>(access, name, std::move(args), ret_type, std::move(block), first_token.
line);
187 std::vector<AST::ExprPtr> args;
191 std::stringstream ss;
192 ss <<
"Expected \033[0m','\033[31m between function arguments.\nPlease replace \033[0m'";
198 return std::make_unique<AST::FuncCallStmt>(name_token.
value, std::move(args), name_token.
line);
202 std::stringstream ss;
203 ss <<
"Expected function argument name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
207 ss <<
"Expected \033[0m':'\033[31m between function argument name and type.\nPlease replace \033[0m'";
208 ss << name <<
"'\033[31m with: \033[0m'" << name <<
": '";
221 return std::make_unique<AST::ReturnStmt>(std::move(ret_expr), first_token.
line);
227 std::vector<AST::StmtPtr> then_block;
232 std::vector<AST::StmtPtr> else_block;
243 return std::make_unique<AST::IfElseStmt>(std::move(cond), std::move(then_block), std::move(else_block), first_token.
line);
249 std::vector<AST::StmtPtr> block;
254 return std::make_unique<AST::WhileCycleStmt>(std::move(cond), std::move(block), first_token.
line);
259 std::vector<AST::StmtPtr> block;
264 consume(
TOK_WHILE,
"Expected \033[0m'while'\033[31m after block in the do-while cycle",
peek().line);
266 return std::make_unique<AST::DoWhileCycleStmt>(std::move(cond), std::move(block), first_token.
line);
276 std::vector<AST::StmtPtr> block;
282 return std::make_unique<AST::ForCycleStmt>(std::move(indexator), std::move(cond), std::move(iteration), std::move(block), first_token.
line);
287 return std::make_unique<AST::BreakStmt>(first_token.
line);
292 return std::make_unique<AST::ContinueStmt>(first_token.
line);
298 std::stringstream ss;
299 ss <<
"Expected module name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
301 std::vector<AST::StmtPtr> block;
306 return std::make_unique<AST::ModuleStmt>(access, first_token.
file_name, name, std::move(block), first_token.
line);
311 std::vector<std::string> path;
313 std::stringstream ss;
314 ss <<
"Expected module name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
317 return std::make_unique<AST::UseModuleStmt>(std::move(path), first_token.
line);
322 std::stringstream ss;
323 ss <<
"Expected function name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
325 std::vector<AST::Argument> args;
331 ss <<
"Expected \033[0m','\033[31m between function arguments.\nPlease replace \033[0m'";
332 ss << args[args.size() - 1].name <<
": " << args[args.size() - 1].type.to_str() <<
" " <<
peek().
value <<
"'\033[31m with: \033[0m'"
333 << args[args.size() - 1].name <<
": " << args[args.size() - 1].type.to_str() <<
", " <<
peek().
value <<
"'";
344 return std::make_unique<AST::FuncDeclStmt>(
AST::ACCESS_NONE, name, std::move(args), ret_type, std::vector<AST::StmtPtr>{}, first_token.
line);
350 std::vector<AST::StmtPtr> block;
355 return std::make_unique<AST::ExternStmt>(lang_name_lit, std::move(block), first_token.
line);
366 expr = std::make_unique<AST::BinaryExpr>(token, std::move(expr),
parse_l_or_expr(), token.
line);
442 expr = std::make_unique<AST::BinaryExpr>(token, std::move(expr),
parse_unary_expr(), token.
line);
445 expr = std::make_unique<AST::BinaryExpr>(token, std::move(expr),
parse_unary_expr(), token.
line);
448 expr = std::make_unique<AST::BinaryExpr>(token, std::move(expr),
parse_unary_expr(), token.
line);
481 switch (token.
type) {
491 std::vector<AST::ExprPtr> args;
492 uint32_t c_pos =
pos;
496 std::stringstream ss;
497 ss <<
"Expected \033[0m','\033[31m between function arguments.\nPlease replace \033[0m'";
507 return std::make_unique<AST::FuncCallExpr>(token.
value, std::move(args), token.
line);
516 return std::make_unique<AST::VarExpr>(token.
value, token.
line);
519 return std::make_unique<AST::CharacterLiteral>(token.
value[0], token.
line);
522 return std::make_unique<AST::ShortLiteral>(std::stoll(token.
value), token.
line);
525 return std::make_unique<AST::IntLiteral>(std::stoll(token.
value), token.
line);
528 return std::make_unique<AST::LongLiteral>(std::stoll(token.
value), token.
line);
531 return std::make_unique<AST::FloatLiteral>(std::stold(token.
value), token.
line);
534 return std::make_unique<AST::DoubleLiteral>(std::stold(token.
value), token.
line);
537 return std::make_unique<AST::BoolLiteral>(token.
value ==
"true", token.
line);
540 return std::make_unique<AST::StringLiteral>(token.
value, token.
line);
542 std::stringstream ss;
543 ss <<
"Expected expression, but got \033[0m'" <<
peek().
value <<
"'\033[31m. Please check expression to mistakes";
550 std::vector<AST::ExprPtr> chain;
552 std::stringstream ss;
553 ss <<
"Expected object name. Token \033[0m'" <<
peek().
value <<
"'\033[31m is keyword or operator. Please replase it with unique identifier";
556 std::vector<AST::ExprPtr> args;
560 std::stringstream ss;
561 ss <<
"Expected \033[0m','\033[31m between function arguments.\nPlease replace \033[0m'";
567 chain.push_back(std::make_unique<AST::FuncCallExpr>(token.
value, std::move(args), token.
line));
570 chain.push_back(std::make_unique<AST::VarExpr>(token.
value, token.
line));
573 return std::make_unique<AST::ChainObjects>(std::move(chain), first_token.
line);
578 std::stringstream ss;
586 if (
peek().type == type) {
603 bool is_const =
false;
605 bool is_nullable =
false;
609 switch (
peek().type) {
629 std::stringstream ss;
630 ss <<
"Token \033[0m'" <<
peek().
value <<
"'\033[31m is not type. Please replase it to exists type";
637 std::stringstream ss;
638 ss <<
"Expected \033[0m';'\033[31m in the end of variable definition. ";
640 ss <<
"Please add \033[0m';'\033[31m into the end of variable definition";
643 ss <<
"Please replace \033[0m'" <<
peek().
value <<
"'\033[31m with \033[0m';'";
667 std::stringstream ss;
668 ss <<
"Token \033[0m'" <<
peek().
value <<
"'\033[31m is not type. Please replase it to exists types";
674 switch (token.
type) {
689 switch (token.
type) {
701 std::stringstream ss;
702 ss <<
"Unsupported compound assignment operator: \033[0m'" << token.
value <<
"'\033[31m. Please check your Topaz compiler version and fix the problematic section of the code";
711 switch (token.
type) {
717 std::stringstream ss;
718 ss <<
"Unsupported increment/decrement operator: \033[0m'" << token.
value <<
"'\033[31m. Please check your Topaz compiler version and fix the problematic section of the code";
void consume_semicolon()
Method for skipping semicolon in the end of statement.
AST::ExprPtr parse_unary_expr()
Method for parsing expression as 'unary'.
AST::ExprPtr parse_expr()
Method for parsing expressions.
AST::StmtPtr parse_func_decl_proto_stmt()
Method for parsing of functions prototypes.
AST::StmtPtr parse_while_cycle_stmt()
Method for parsing of while cycle.
AST::ExprPtr parse_l_or_expr()
Method for parsing expression as 'logical or'.
AST::ExprPtr parse_additive_expr()
Method for parsing expression as 'additive'.
AST::StmtPtr parse_func_call_stmt()
Method for parsing of functions calling.
AST::StmtPtr parse_do_while_cycle_stmt()
Method for parsing of do-while cycle.
AST::StmtPtr parse_if_else_stmt()
Method for parsing of control flow operators (aka if-else).
std::vector< Token > tokens
AST::ExprPtr parse_primary_expr()
Method for parsing expression as 'primary'.
AST::StmtPtr parse_return_stmt()
Method for parsing of 'return'.
AST::StmtPtr parse_use_module_stmt()
Method for parsing of import the module.
AST::StmtPtr parse_var_asgn_stmt()
Method for parsing of variable assignment.
AST::Argument parse_argument()
Method for parsing of function argument.
AST::StmtPtr parse_stmt(bool from_for=false)
Method for parsing only one statement.
AST::Type consume_type()
Method for verifyng the Topaz type by current tokens.
std::vector< AST::StmtPtr > parse()
Method for parsing tokens into AST tree.
AST::StmtPtr parse_var_decl_stmt()
Method for parsing of variable declaration.
AST::ExprPtr create_inc_dec_operator(std::string var_name)
Method for creating increment/decrement operator and returns expression of assignment.
bool is_compound_asgn_operator(Token token)
Method for checking whether the passed token is a compound assignment operator.
AST::StmtPtr parse_func_decl_stmt()
Method for parsing of functions declaration.
AST::StmtPtr parse_break_stmt()
Method for parsing of break statement.
AST::ExprPtr parse_equality_expr()
Method for parsing expression as 'equality'.
AST::ExprPtr parse_comparation_expr()
Method for parsing expression as 'comparation'.
AST::StmtPtr parse_continue_stmt()
Method for parsing of continue statement.
AST::StmtPtr parse_extern_stmt()
Method for parsing of declaration functions prototypes as extern.
AST::ExprPtr parse_l_and_expr()
Method for parsing expression as 'logical and'.
AST::StmtPtr parse_for_cycle_stmt()
Method for parsing of for cycle.
Token peek(int32_t rpos=0) const
Method for getting token from tokens by parser pos and passed offset.
AST::ExprPtr create_compound_asgn_operator(std::string var_name)
Method for creating compound assignment operator and returns expression of assignment.
AST::ExprPtr parse_multiplicative_expr()
Method for parsing expression as 'multiplicative'.
Token consume(TokenType type, std::string err_msg, uint32_t line)
Method for verifying the type of the current token.
AST::StmtPtr parse_module_stmt()
Method for parsing of module definition.
AST::TypeValue ttype_to_tvalue(TokenType type)
Method for convert type of token to type of Topaz value.
AST::ExprPtr parse_obj_chain_expr()
Method for parsing expresion as chain of objects.
bool match(TokenType type)
Method for skipping the current token if its type is equal to the passed one.
void reset()
Method for resetting all parameters in parser.
void throw_exception(SubsystemType type, std::string msg, uint32_t line, std::string file_name, bool is_debug)
Function for throwing exception.
Header file for defining thrown exceptions by the compiler.
AccessModifier
Access modifier for statements.
std::unique_ptr< Stmt > StmtPtr
TypeValue
Type values enum.
std::unique_ptr< Expr > ExprPtr
AST::AccessModifier current_access
Header file for defining parser.
Structure for describing the argument (in function or method).
Structure for describing the type.
TokenType
All tokens types.