Skip to content

Commit 7e4db04

Browse files
feat(wheel_parser): add function declaration parser
1 parent adc951b commit 7e4db04

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#if !defined (FUNCTIONS_DECLARATION_HXX)
2+
#define FUNCTIONS_DECLARATION_HXX
3+
#include "contracts_declaration.hxx"
4+
#include "expression_declaration.hxx"
5+
#include "wheel_parser/parser_utils.hxx"
6+
#include "wheel_parser/config.hxx"
7+
#include "wheel_parser/ast/nodes.hxx"
8+
#include "wheel_parser/ast/symbol.hxx"
9+
#include "wheel_parser/functions/emit_error.hxx"
10+
#include "wheel_parser/ast/keywords.hxx"
11+
#include <wheel_lexer/kind.hxx>
12+
#include <wheel_memory/vec.hxx>
13+
#include <wheel_utils/logging.hxx>
14+
15+
WHEEL_PARSER_FUNCTIONS_BEGIN_NAMESPACE
16+
using wheel_parser::ast::StatementNode;
17+
using wheel_parser::ast::SymbolID;
18+
using wheel_parser::ast::Keyword;
19+
using wheel_parser::ast::FunctionDeclaration;
20+
using wheel_parser::ast::CallExpression;
21+
using wheel_parser::ast::ExpressionNode;
22+
using wheel_parser::ast::IdentifierExpression;
23+
using wheel_parser::ast::ExpressionStatement;
24+
using wheel_parser::functions::parse_expression_declaration;
25+
using wheel_parser::functions::ParserFuncContract;
26+
using wheel_parser::ParseErrorCode;
27+
using wheel_lexer::TokenKind;
28+
29+
#if defined (WHEEL_EXPERIMENT) && defined (WHEEL_SMALL_VEC)
30+
using wheel_memory::SmallVec;
31+
using FuncArgs = SmallVec<ExpressionNode*>;
32+
33+
#else
34+
using FuncArgs = std::vector<ExpressionNode*>;
35+
#endif
36+
37+
inline void parse_function_argument(ParserFuncContract contract, FuncArgs &arguments) {
38+
while(true) {
39+
ExpressionNode *argument = parse_expression_declaration(contract);
40+
41+
arguments.push_back(argument);
42+
skip_spaces(contract.lexer, contract.state.current_token);
43+
44+
45+
if (!token_matches(contract.state.current_token.kind, TokenKind::LEFT_PARENT)) {
46+
abort();
47+
}
48+
49+
if (token_matches(contract.state.current_token.kind, TokenKind::RIGHT_PARENT)) {
50+
break;
51+
}
52+
}
53+
}
54+
55+
/// TODO !!! Implement parse for functiond declaration.
56+
PARSE_BEGIN(parse_function_declaration, StatementNode)
57+
consume(contract.lexer, contract.state.current_token);
58+
auto current_token = contract.state.current_token;
59+
auto arena = &contract.arena;
60+
auto interner = &contract.interner;
61+
62+
if (!token_matches(current_token.kind, TokenKind::IDENT)) {
63+
return nullptr;
64+
}
65+
66+
/* Matches if
67+
function foo() -> void: ...
68+
*/
69+
if (!keyword_matches(current_token, Keyword::Function)) {
70+
return nullptr;
71+
}
72+
73+
skip_spaces(contract.lexer, current_token);
74+
const auto func_token = copy_token(*arena, current_token);
75+
76+
if (matches_any_keywords(*func_token)) {
77+
return emit_error(ERROR_CONTEXT(ParseErrorCode::FunctionNameNotAllowed));
78+
}
79+
80+
return nullptr;
81+
PARSE_END
82+
83+
WHEEL_PARSER_FUNCTIONS_END_NAMESPACE
84+
85+
#endif // FUNCTIONS_DECLARATION_HXX

0 commit comments

Comments
 (0)