zig/src/parser.hpp

127 lines
2.4 KiB
C++
Raw Normal View History

2015-11-04 13:31:27 +08:00
#ifndef ZIG_PARSER_HPP
#define ZIG_PARSER_HPP
#include "list.hpp"
#include "buffer.hpp"
#include "tokenizer.hpp"
struct AstNode;
enum NodeType {
NodeTypeRoot,
NodeTypeFnDecl,
NodeTypeParamDecl,
NodeTypeType,
NodeTypePointerType,
NodeTypeBlock,
NodeTypeStatement,
NodeTypeExpressionStatement,
NodeTypeReturnStatement,
NodeTypeExpression,
NodeTypeFnCall,
};
struct AstNodeRoot {
ZigList<AstNode *> fn_decls;
};
struct AstNodeFnDecl {
Buf name;
ZigList<AstNode *> params;
AstNode *return_type;
AstNode *body;
};
struct AstNodeParamDecl {
Buf name;
AstNode *type;
};
enum AstNodeTypeType {
AstNodeTypeTypePrimitive,
AstNodeTypeTypePointer,
};
struct AstNodeType {
AstNodeTypeType type;
Buf primitive_name;
AstNode *child_type;
bool is_const;
2015-11-04 13:31:27 +08:00
};
struct AstNodeBlock {
ZigList<AstNode *> statements;
2015-11-04 13:31:27 +08:00
};
enum AstNodeStatementType {
AstNodeStatementTypeExpression,
AstNodeStatementTypeReturn,
};
struct AstNodeStatementExpression {
AstNode *expression;
};
struct AstNodeStatementReturn {
AstNode *expression;
};
struct AstNodeStatement {
AstNodeStatementType type;
union {
AstNodeStatementExpression expr;
AstNodeStatementReturn retrn;
} data;
};
enum AstNodeExpressionType {
AstNodeExpressionTypeNumber,
AstNodeExpressionTypeString,
AstNodeExpressionTypeFnCall,
2015-11-04 13:31:27 +08:00
};
struct AstNodeExpression {
AstNodeExpressionType type;
union {
Buf number;
Buf string;
AstNode *fn_call;
} data;
2015-11-04 13:31:27 +08:00
};
struct AstNodeFnCall {
Buf name;
ZigList<AstNode *> params;
};
struct AstNode {
enum NodeType type;
AstNode *parent;
int line;
int column;
2015-11-04 13:31:27 +08:00
union {
AstNodeRoot root;
AstNodeFnDecl fn_decl;
AstNodeType type;
AstNodeParamDecl param_decl;
AstNodeBlock block;
AstNodeStatement statement;
2015-11-04 13:31:27 +08:00
AstNodeExpression expression;
AstNodeFnCall fn_call;
} data;
};
__attribute__ ((format (printf, 2, 3)))
void ast_token_error(Token *token, const char *format, ...);
2015-11-07 19:50:48 +08:00
void ast_invalid_token_error(Buf *buf, Token *token);
2015-11-04 13:31:27 +08:00
// This function is provided by generated code, generated by parsergen.cpp
2015-11-04 13:31:27 +08:00
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens);
const char *node_type_str(NodeType node_type);
void ast_print(AstNode *node, int indent);
2015-11-04 13:31:27 +08:00
#endif