1 #include "parser/parser.h"
2 #include "ast/ast_node_type.h"
7 #include "ast/context.h"
8 #include "ast/intrinsic.h"
9 #include "source_file/token.h"
12 using namespace tanlang;
13 using tanlang::TokenType;
17 static bool check_typename_token(
Token *token);
18 static bool check_terminal_token(
Token *token);
30 _variable = scoped_val;
51 ASTBase *node = next_expression(PREC_LOWEST);
53 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Unexpected terminal token");
55 _root->append_child(node);
67 Token *token = at(_curr);
69 str tok = token->get_value();
71 if (tok ==
"pub" || tok ==
"extern") {
74 error(ErrorType::SYNTAX_ERROR, _curr, _curr, fmt::format(
"Repeated `{}`", tok));
77 if (tok ==
"extern") {
79 error(ErrorType::SYNTAX_ERROR, _curr, _curr, fmt::format(
"Repeated `{}`", tok));
87 auto node_type = ret->get_node_type();
88 bool is_func = node_type == ASTNodeType::FUNC_DECL;
89 bool is_struct = node_type == ASTNodeType::STRUCT_DECL;
90 if (tok ==
"pub" && !is_func && !is_struct)
91 error(ErrorType::SYNTAX_ERROR,
95 if (tok ==
"extern" && !is_func)
96 error(ErrorType::SYNTAX_ERROR,
105 else if (tok ==
"var")
106 ret = VarDecl::Create(_src);
107 else if (tok ==
"import")
109 else if (tok ==
"if")
110 ret = If::Create(_src);
111 else if (tok ==
"return")
112 ret = Return::Create(_src);
113 else if (tok ==
"while" || tok ==
"for")
115 else if (tok ==
"fn")
116 ret = FunctionDecl::Create(_src, _extern, _pub);
117 else if (tok ==
"struct")
118 ret = StructDecl::Create(_src, _extern, _pub);
119 else if (tok ==
"break")
120 ret = Break::Create(_src);
121 else if (tok ==
"continue")
122 ret = Continue::Create(_src);
123 else if (tok ==
"as")
124 ret = Cast::Create(_src);
125 else if (tok ==
"true")
126 ret = BoolLiteral::Create(_src,
true);
127 else if (tok ==
"false")
128 ret = BoolLiteral::Create(_src,
false);
129 else if (tok ==
"package")
130 ret = PackageDecl::Create(_src);
137 str s = at(_curr)->get_value();
139 if (!std::isalpha(s[0])) {
140 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Invalid type name");
143 return Type::GetTypeRef(s);
151 Token *token = at(_curr);
154 while (token && token->get_type() == TokenType::COMMENTS) {
165 if (token->get_value() ==
"@") {
166 node = Intrinsic::Create(_src);
167 }
else if (token->get_value() ==
"=" && token->get_type() == TokenType::BOP) {
168 node = Assignment::Create(_src);
169 }
else if (token->get_value() ==
"!") {
170 node = UnaryOperator::Create(UnaryOpKind::LNOT, _src);
171 }
else if (token->get_value() ==
"~") {
172 node = UnaryOperator::Create(UnaryOpKind::BNOT, _src);
173 }
else if (token->get_value() ==
"[") {
174 Token *prev_token = at(_curr - 1);
175 if (prev_token->get_type() != TokenType::ID && prev_token->get_value() !=
"]" && prev_token->get_value() !=
")") {
177 node = ArrayLiteral::Create(_src);
182 }
else if (token->get_type() == TokenType::RELOP) {
184 str tok = token->get_value();
186 op = BinaryOpKind::GT;
187 else if (tok ==
">=")
188 op = BinaryOpKind::GE;
190 op = BinaryOpKind::LT;
191 else if (tok ==
"<=")
192 op = BinaryOpKind::LE;
193 else if (tok ==
"==")
194 op = BinaryOpKind::EQ;
195 else if (tok ==
"!=")
196 op = BinaryOpKind::NE;
197 else if (tok ==
"&&")
198 op = BinaryOpKind::LAND;
199 else if (tok ==
"||")
200 op = BinaryOpKind::LOR;
202 error(ErrorType::NOT_IMPLEMENTED,
205 fmt::format(
"Binary relational operator not implemented: {}", token->get_value().c_str()));
207 node = BinaryOperator::Create(op, _src);
208 }
else if (token->get_type() == TokenType::INT) {
209 node = IntegerLiteral::Create(_src, (uint64_t)std::stol(token->get_value()), token->is_unsigned());
210 }
else if (token->get_type() == TokenType::FLOAT) {
211 node = FloatLiteral::Create(_src, std::stod(token->get_value()));
212 }
else if (token->get_type() == TokenType::STRING) {
213 node = StringLiteral::Create(_src, token->get_value());
214 }
else if (token->get_type() == TokenType::CHAR) {
215 node = CharLiteral::Create(_src,
static_cast<uint8_t
>(token->get_value()[0]));
216 }
else if (check_typename_token(token)) {
217 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Unexpected type name");
218 }
else if (token->get_type() == TokenType::ID) {
220 Token *next_token = at(++next);
221 if (next_token->get_value() ==
"(") {
226 node = Identifier::Create(_src, token->get_value());
228 }
else if (token->get_type() == TokenType::PUNCTUATION && token->get_value() ==
"(") {
230 }
else if (token->get_type() == TokenType::KEYWORD) {
231 node = peek_keyword();
233 error(ErrorType::NOT_IMPLEMENTED, _curr, _curr,
"Keyword not implemented: " + token->get_value());
235 }
else if (token->get_type() == TokenType::BOP && token->get_value() ==
".") {
237 }
else if (token->get_value() ==
"&") {
240 }
else if (token->get_type() == TokenType::PUNCTUATION && token->get_value() ==
"{") {
242 }
else if (token->get_type() == TokenType::BOP) {
243 TAN_ASSERT(token->get_value().length());
244 switch (token->get_value()[0]) {
246 node = BinaryOperator::Create(BinaryOpKind::DIVIDE, _src);
249 node = BinaryOperator::Create(BinaryOpKind::MOD, _src);
252 node = BinaryOperator::Create(BinaryOpKind::BOR, _src);
255 node = BinaryOperator::Create(BinaryOpKind::XOR, _src);
275 }
else if (check_terminal_token(token)) {
278 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Unknown token " + token->get_value());
281 node->set_start(_curr);
282 node->set_end(_curr);
286 ASTBase *next_expression(
int rbp) {
298 while (rbp < node->get_bp()) {
314 if (p->get_node_type() == ASTNodeType::BOP_OR_UOP) {
315 auto *pp = pcast<BinaryOrUnary>(p);
317 str tok = _src->get_token_str(p->start());
318 TAN_ASSERT(tok.length());
321 actual = UnaryOperator::Create(UnaryOpKind::PTR_DEREF, _src);
324 actual = UnaryOperator::Create(UnaryOpKind::ADDRESS_OF, _src);
327 actual = UnaryOperator::Create(UnaryOpKind::PLUS, _src);
330 actual = UnaryOperator::Create(UnaryOpKind::MINUS, _src);
338 parse_node(pp->get_expr_ptr());
340 pp->set_end(pp->get_expr_ptr()->end());
345 auto it = NUD_PARSING_FUNC_TABLE.find(p->get_node_type());
346 if (it == NUD_PARSING_FUNC_TABLE.end()) {
347 error(ErrorType::SYNTAX_ERROR,
352 nud_parsing_func_t func = it->second;
359 if (p->get_node_type() == ASTNodeType::BOP_OR_UOP) {
360 auto *pp = pcast<BinaryOrUnary>(p);
362 str tok = _src->get_token_str(p->start());
363 TAN_ASSERT(tok.length());
366 actual = BinaryOperator::Create(BinaryOpKind::MULTIPLY, _src);
369 actual = BinaryOperator::Create(BinaryOpKind::BAND, _src);
372 actual = BinaryOperator::Create(BinaryOpKind::SUM, _src);
375 actual = BinaryOperator::Create(BinaryOpKind::SUBTRACT, _src);
382 parse_node(left, pp->get_expr_ptr());
384 pp->set_start(pp->get_expr_ptr()->start());
385 pp->set_end(pp->get_expr_ptr()->end());
390 auto it = LED_PARSING_FUNC_TABLE.find(p->get_node_type());
391 if (it == LED_PARSING_FUNC_TABLE.end()) {
392 error(ErrorType::SYNTAX_ERROR,
397 led_parsing_func_t func = it->second;
398 (this->*func)(left, p);
402 [[nodiscard]]
Token *at(uint32_t loc)
const {
403 if (this->eof(loc)) {
404 Error(ErrorType::SYNTAX_ERROR, _src->get_last_token(), _src->get_last_token(),
"Unexpected EOF").raise();
406 return _src->get_token(loc);
409 [[nodiscard]]
bool eof(uint32_t loc)
const {
return _src->is_eof(loc); }
411 [[noreturn]]
void error(ErrorType type,
ASTBase *node,
const str &error_message)
const {
412 Error(type, at(node->start()), at(node->end()), error_message).raise();
415 [[noreturn]]
void error(ErrorType type, uint32_t start, uint32_t end,
const str &error_message)
const {
416 Error(type, at(start), at(end), error_message).raise();
422 error(ErrorType::SYNTAX_ERROR, p,
"Expect an expression");
424 return pcast<Expr>(p);
430 error(ErrorType::SYNTAX_ERROR, p,
"Expect a statement");
432 return pcast<Stmt>(p);
435 void expect_token(
const str &value) {
436 Token *token = at(_curr);
437 if (token->get_value() != value) {
438 Error(ErrorType::SYNTAX_ERROR,
441 fmt::format(
"Expect '{}' but got '{}' instead", value, token->get_value()))
447 void parse_compound_stmt(
ASTBase *_p) {
448 auto p = pcast<CompoundStmt>(_p);
454 while ((node = next_expression(PREC_LOWEST)) !=
nullptr) {
455 p->append_child(node);
457 str s = at(_curr)->get_value();
467 auto p = pcast<Assignment>(_p);
475 auto rhs = next_expression(PREC_LOWEST);
476 p->set_rhs(expect_expression(rhs));
477 p->set_end(_curr - 1);
481 auto lhs = pcast<Expr>(left);
482 auto p = pcast<Cast>(_p);
490 auto *ty = peek_type();
491 p->set_type(parse_ty(ty));
493 p->set_end(_curr - 1);
496 void parse_generic_token(
ASTBase *p) { p->set_end(_curr++); }
499 auto p = pcast<If>(_p);
504 parse_if_then_branch(p);
508 while (at(_curr)->get_value() ==
"else") {
510 if (at(_curr)->get_value() ==
"if") {
511 parse_if_then_branch(p);
512 }
else if (at(_curr)->get_value() ==
"{") {
513 auto else_clause = peek();
514 parse_node(else_clause);
515 p->add_else_branch(expect_stmt(else_clause));
517 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Unexpected token");
524 TAN_ASSERT(at(_curr)->get_value() ==
"}");
527 void parse_if_then_branch(
If *p) {
532 auto *_pred = peek();
533 if (_pred->get_node_type() != ASTNodeType::PARENTHESIS) {
534 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect a parenthesis expression");
536 parse_parenthesis(_pred);
537 Expr *pred = pcast<Expr>(_pred);
541 auto *_then = peek();
543 Stmt *then_clause = expect_stmt(_then);
545 p->add_if_then_branch(pred, then_clause);
549 auto p = pcast<Loop>(_p);
552 if (at(_curr)->get_value() ==
"for") {
553 p->_loop_type = ASTLoopType::FOR;
554 }
else if (at(_curr)->get_value() ==
"while") {
555 p->_loop_type = ASTLoopType::WHILE;
564 switch (p->_loop_type) {
565 case ASTLoopType::WHILE: {
568 auto _pred = next_expression(PREC_LOWEST);
569 Expr *pred = expect_expression(_pred);
570 p->_predicate = pred;
573 case ASTLoopType::FOR:
578 auto *tmp = next_expression(PREC_LOWEST);
579 Expr *init = expect_expression(tmp);
580 p->_initialization = init;
586 tmp = next_expression(PREC_LOWEST);
587 Expr *pred = expect_expression(tmp);
588 p->_predicate = pred;
594 tmp = next_expression(PREC_LOWEST);
595 Expr *iteration = expect_expression(tmp);
596 p->_iteration = iteration;
606 auto *_body = next_expression(PREC_LOWEST);
607 Stmt *body = expect_stmt(_body);
611 void parse_array_literal(
ASTBase *_p) {
612 auto *p = pcast<ArrayLiteral>(_p);
616 if (at(_curr)->get_value() ==
"]") {
618 error(ErrorType::SEMANTIC_ERROR, p->start(), _curr,
"Empty array literal");
621 vector<Literal *> elements{};
622 while (!eof(_curr)) {
623 if (at(_curr)->get_value() ==
",") {
626 }
else if (at(_curr)->get_value() ==
"]") {
632 auto *expr = expect_expression(node);
633 if (!expr->is_comptime_known()) {
634 error(ErrorType::SEMANTIC_ERROR, _curr, _curr,
"Expected a compile-time known value");
638 elements.push_back(pcast<Literal>(node));
641 p->set_elements(elements);
642 p->set_end(_curr - 1);
646 Expr *lhs = pcast<Expr>(_lhs);
648 Token *token = at(_p->start());
649 if (token->get_value() ==
"." || token->get_value() ==
"[") {
650 parse_member_access(lhs, pcast<MemberAccess>(_p));
654 auto *p = pcast<BinaryOperator>(_p);
660 auto rhs = next_expression(p->get_bp());
661 p->set_rhs(expect_expression(rhs));
663 p->set_start(lhs->start());
664 p->set_end(_curr - 1);
668 auto *p = pcast<UnaryOperator>(_p);
671 auto rhs = pcast<Expr>(next_expression(p->get_bp()));
673 error(ErrorType::SEMANTIC_ERROR, rhs,
"Invalid operand");
676 p->set_end(_curr - 1);
679 void parse_parenthesis(
ASTBase *_p) {
680 auto *p = pcast<Parenthesis>(_p);
685 if (t->get_type() == TokenType::PUNCTUATION && t->get_value() ==
")") {
693 auto _sub = next_expression(PREC_LOWEST);
694 Expr *sub = expect_expression(_sub);
698 p->set_end(_curr - 1);
701 void parse_func_decl(
ASTBase *_p) {
702 auto *p = pcast<FunctionDecl>(_p);
709 Token *id_token = at(_curr);
710 auto id = Identifier::Create(_src, id_token->get_value());
711 id->set_start(_curr);
713 if (id->get_node_type() != ASTNodeType::ID) {
714 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect a function name");
717 p->set_name(id->get_name());
726 vector<str> arg_names{};
727 vector<Type *> arg_types{};
728 vector<ArgDecl *> arg_decls{};
729 if (at(_curr)->get_value() !=
")") {
730 while (!eof(_curr)) {
731 auto arg = ArgDecl::Create(_src);
732 arg->set_start(_curr);
735 arg_names.push_back(arg->get_name());
736 arg_types.push_back(arg->get_type());
737 arg_decls.push_back(arg);
739 if (at(_curr)->get_value() ==
",") {
750 p->set_arg_names(arg_names);
751 p->set_arg_decls(arg_decls);
757 auto *ret_type = peek_type();
758 auto *func_type = Type::GetFunctionType(parse_ty(ret_type), arg_types);
759 p->set_type(func_type);
761 p->set_end(_curr - 1);
764 if (!p->is_external()) {
768 p->set_body(expect_stmt(body));
773 void parse_func_call(
ASTBase *_p) {
774 auto *p = pcast<FunctionCall>(_p);
776 p->set_name(at(_curr)->get_value());
784 while (!eof(_curr) && at(_curr)->get_value() !=
")") {
785 auto _arg = next_expression(PREC_LOWEST);
786 Expr *arg = expect_expression(_arg);
787 p->_args.push_back(arg);
789 if (at(_curr)->get_value() ==
",") {
802 void parse_test_comp_error_intrinsic(
Intrinsic *p) {
806 if (e->get_node_type() != ASTNodeType::PARENTHESIS) {
807 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect a parenthesis");
811 auto *test_name = pcast<Parenthesis>(e)->get_sub();
813 p->set_end(_curr - 1);
815 if (test_name->get_node_type() != ASTNodeType::ID) {
816 error(ErrorType::SEMANTIC_ERROR, test_name,
"Expect a test name");
819 p->set_name(
"test_comp_error");
820 p->set_intrinsic_type(IntrinsicType::TEST_COMP_ERROR);
824 if (body->get_node_type() != ASTNodeType::COMPOUND_STATEMENT) {
825 error(ErrorType::SYNTAX_ERROR, body,
"Expect a compound statement");
835 std::cerr << fmt::format(
"Caught expected compile error: {}\nContinue compilation...\n", e.what());
837 while (at(_curr)->get_value() !=
"}")
840 sub->_caught = caught;
843 for (
auto *c : body->get_children()) {
844 sub->append_child(c);
851 void parse_intrinsic(
ASTBase *_p) {
852 auto *p = pcast<Intrinsic>(_p);
856 if (_src->get_token_str(_curr) == Intrinsic::TEST_COMP_ERROR_NAME) {
857 parse_test_comp_error_intrinsic(p);
864 if (e->get_node_type() != ASTNodeType::ID && e->get_node_type() != ASTNodeType::FUNC_CALL) {
865 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Unexpected token");
871 switch (e->get_node_type()) {
872 case ASTNodeType::FUNC_CALL:
873 name = pcast<FunctionCall>(e)->get_name();
875 case ASTNodeType::ID:
876 name = pcast<Identifier>(e)->get_name();
882 TAN_ASSERT(!name.empty());
888 error(ErrorType::UNKNOWN_SYMBOL, _curr, _curr, fmt::format(
"Unknown intrinsic {}", name));
890 p->set_intrinsic_type(q->second);
893 switch (p->get_intrinsic_type()) {
894 case IntrinsicType::LINENO:
895 case IntrinsicType::FILENAME:
896 if (e->get_node_type() != ASTNodeType::ID)
897 error(ErrorType::SEMANTIC_ERROR, _curr, _curr,
"Invalid usage of intrinsic");
899 case IntrinsicType::NOOP:
900 case IntrinsicType::STACK_TRACE:
901 case IntrinsicType::TEST_COMP_ERROR:
902 if (e->get_node_type() != ASTNodeType::FUNC_CALL)
903 error(ErrorType::SEMANTIC_ERROR, _curr, _curr,
"Invalid usage of intrinsic");
905 case IntrinsicType::INVALID:
912 p->set_end(_curr - 1);
915 void parse_import(
ASTBase *_p) {
916 auto *p = pcast<Import>(_p);
922 if (rhs->get_node_type() != ASTNodeType::STRING_LITERAL) {
923 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Invalid import statement");
926 str name = pcast<StringLiteral>(rhs)->get_value();
930 void parse_package_stmt(
ASTBase *_p) {
931 auto *p = pcast<PackageDecl>(_p);
935 if (rhs->get_node_type() != ASTNodeType::STRING_LITERAL) {
936 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Invalid package statement");
939 str name = pcast<StringLiteral>(rhs)->get_value();
943 p->set_end(_curr - 1);
947 if (at(_curr)->get_value() ==
"[") {
948 p->_access_type = MemberAccess::MemberAccessBracket;
957 auto _right = peek();
958 Expr *right = expect_expression(_right);
962 if (p->_access_type == MemberAccess::MemberAccessBracket) {
966 p->set_end(_curr - 1);
969 void parse_return(
ASTBase *_p) {
970 auto *p = pcast<Return>(_p);
974 auto _rhs = next_expression(PREC_LOWEST);
976 Expr *rhs = expect_expression(_rhs);
980 p->set_end(_curr - 1);
983 void parse_struct_decl(
ASTBase *_p) {
984 auto *p = pcast<StructDecl>(_p);
990 if (_id->get_node_type() != ASTNodeType::ID) {
991 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expecting a typename");
994 auto id = pcast<Identifier>(_id);
995 p->set_name(id->get_name());
997 p->set_end(_curr - 1);
1000 if (at(_curr)->get_value() !=
"{") {
1001 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect struct body");
1006 auto _comp_stmt = next_expression(PREC_LOWEST);
1007 if (!_comp_stmt || _comp_stmt->get_node_type() != ASTNodeType::COMPOUND_STATEMENT) {
1008 error(ErrorType::SEMANTIC_ERROR, _curr, _curr,
"struct definition requires a valid body");
1010 auto comp_stmt = pcast<CompoundStmt>(_comp_stmt);
1013 auto children = comp_stmt->get_children();
1014 vector<Expr *> member_decls{};
1015 for (
const auto &c : children) {
1017 c->get_node_type() == ASTNodeType::VAR_DECL
1018 || c->get_node_type() == ASTNodeType::ASSIGN
1019 || c->get_node_type() == ASTNodeType::FUNC_DECL
1021 error(ErrorType::SEMANTIC_ERROR, c,
"Invalid struct member");
1023 member_decls.push_back(pcast<Expr>(c));
1025 p->set_member_decls(member_decls);
1035 if (_size->get_node_type() != ASTNodeType::INTEGER_LITERAL) {
1036 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect an unsigned integer as the array size");
1040 auto size = pcast<IntegerLiteral>(_size);
1041 size_t array_size = size->get_value();
1042 if (
static_cast<int64_t
>(array_size) < 0) {
1043 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect an unsigned integer as the array size");
1046 ret = Type::GetArrayType(p, (
int)array_size);
1053 if (at(_curr)->get_value() ==
"[") {
1068 while (!eof(_curr)) {
1069 Token *token = at(_curr);
1071 auto it = PrimitiveType::TYPENAME_TO_KIND.find(token->get_value());
1072 if (it != PrimitiveType::TYPENAME_TO_KIND.end()) {
1073 ret = PrimitiveType::Create(it->second);
1074 }
else if (token->get_value() ==
"*") {
1076 ret = Type::GetPointerType(ret);
1077 }
else if (token->get_value() ==
"str") {
1078 ret = Type::GetStringType();
1079 }
else if (token->get_type() == TokenType::ID) {
1088 Token *token = at(_curr);
1089 if (token->get_value() ==
"[") {
1091 ret = parse_ty_array(ret);
1097 void parse_var_decl(
ASTBase *_p) {
1098 auto *p = pcast<VarDecl>(_p);
1103 auto name_token = at(_curr);
1104 p->set_name(name_token->get_value());
1108 if (at(_curr)->get_value() ==
":") {
1110 Type *ty = peek_type();
1111 p->set_type(parse_ty(ty));
1114 p->set_end(_curr - 1);
1117 void parse_arg_decl(
ASTBase *_p) {
1118 auto *p = pcast<ArgDecl>(_p);
1121 auto name_token = at(_curr);
1122 p->set_name(name_token->get_value());
1125 if (at(_curr)->get_value() !=
":") {
1126 error(ErrorType::SYNTAX_ERROR, _curr, _curr,
"Expect a type being specified");
1131 Type *ty = peek_type();
1132 p->set_type(parse_ty(ty));
1134 p->set_end(_curr - 1);
1142 ASTBase *_curr_scope =
nullptr;
1145 bool _extern =
false;
1148 const static umap<ASTNodeType, nud_parsing_func_t> NUD_PARSING_FUNC_TABLE;
1149 const static umap<ASTNodeType, led_parsing_func_t> LED_PARSING_FUNC_TABLE;
1154 Program *Parser::parse() {
return _impl->parse(); }
1156 Parser::~Parser() {
delete _impl; }
1158 const umap<ASTNodeType, nud_parsing_func_t> ParserImpl::NUD_PARSING_FUNC_TABLE = {
1159 {ASTNodeType::COMPOUND_STATEMENT, &ParserImpl::parse_compound_stmt},
1160 {ASTNodeType::PARENTHESIS, &ParserImpl::parse_parenthesis },
1161 {ASTNodeType::IMPORT, &ParserImpl::parse_import },
1162 {ASTNodeType::INTRINSIC, &ParserImpl::parse_intrinsic },
1163 {ASTNodeType::IF, &ParserImpl::parse_if },
1164 {ASTNodeType::LOOP, &ParserImpl::parse_loop },
1165 {ASTNodeType::UOP, &ParserImpl::parse_uop },
1166 {ASTNodeType::RET, &ParserImpl::parse_return },
1167 {ASTNodeType::FUNC_CALL, &ParserImpl::parse_func_call },
1168 {ASTNodeType::ARRAY_LITERAL, &ParserImpl::parse_array_literal},
1169 {ASTNodeType::STRUCT_DECL, &ParserImpl::parse_struct_decl },
1170 {ASTNodeType::VAR_DECL, &ParserImpl::parse_var_decl },
1171 {ASTNodeType::ARG_DECL, &ParserImpl::parse_arg_decl },
1172 {ASTNodeType::FUNC_DECL, &ParserImpl::parse_func_decl },
1173 {ASTNodeType::BREAK, &ParserImpl::parse_generic_token},
1174 {ASTNodeType::CONTINUE, &ParserImpl::parse_generic_token},
1175 {ASTNodeType::ID, &ParserImpl::parse_generic_token},
1176 {ASTNodeType::INTEGER_LITERAL, &ParserImpl::parse_generic_token},
1177 {ASTNodeType::FLOAT_LITERAL, &ParserImpl::parse_generic_token},
1178 {ASTNodeType::CHAR_LITERAL, &ParserImpl::parse_generic_token},
1179 {ASTNodeType::STRING_LITERAL, &ParserImpl::parse_generic_token},
1180 {ASTNodeType::BOOL_LITERAL, &ParserImpl::parse_generic_token},
1181 {ASTNodeType::PACKAGE_DECL, &ParserImpl::parse_package_stmt },
1185 const umap<ASTNodeType, led_parsing_func_t> ParserImpl::LED_PARSING_FUNC_TABLE = {
1186 {ASTNodeType::BOP, &ParserImpl::parse_bop },
1187 {ASTNodeType::ASSIGN, &ParserImpl::parse_assignment},
1188 {ASTNodeType::CAST, &ParserImpl::parse_cast }
1193 static bool check_typename_token(
Token *token) {
1194 return is_string_in(token->get_value(), tanlang::Type::ALL_TYPE_NAMES);
1197 static bool check_terminal_token(
Token *token) {
1199 return is_string_in(token->get_value(), TERMINAL_TOKENS);
static umap< ASTNodeType, str > ASTTypeNames
string representation of ASTNodeType
virtual str terminal_token() const
Which terminal token is expected immediately after this node.
static umap< BinaryOpKind, int > BOPPrecedence
binary operator precedence
static CompoundStmt * Create(TokenizedSourceFile *src)
static FunctionCall * Create(TokenizedSourceFile *src)
Represent if-[else] or if-elif-[else] statements.
static Import * Create(TokenizedSourceFile *src)
A generic representation of Intrinsic variables/functions.
static umap< str, IntrinsicType > INTRINSIC_NAME_TO_TYPES
A mapping from intrinsic names to IntrinsicType.
static Loop * Create(TokenizedSourceFile *src)
static MemberAccess * Create(TokenizedSourceFile *src)
static Parenthesis * Create(TokenizedSourceFile *src)
static Program * Create(TokenizedSourceFile *src)
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...