/* * Copyright (c) 2015 Andrew Kelley * * This file is part of zig, which is MIT licensed. * See http://opensource.org/licenses/MIT */ #ifndef ZIG_PARSER_HPP #define ZIG_PARSER_HPP #include "list.hpp" #include "buffer.hpp" #include "tokenizer.hpp" struct AstNode; struct CodeGenNode; enum NodeType { NodeTypeRoot, NodeTypeFnProto, NodeTypeFnDef, NodeTypeFnDecl, NodeTypeParamDecl, NodeTypeType, NodeTypeBlock, NodeTypeStatement, NodeTypeExpression, NodeTypeFnCall, NodeTypeExternBlock, }; struct AstNodeRoot { ZigList top_level_decls; }; struct AstNodeFnProto { Buf name; ZigList params; AstNode *return_type; }; struct AstNodeFnDef { AstNode *fn_proto; AstNode *body; }; struct AstNodeFnDecl { AstNode *fn_proto; }; struct AstNodeParamDecl { Buf name; AstNode *type; }; enum AstNodeTypeType { AstNodeTypeTypePrimitive, AstNodeTypeTypePointer, }; struct AstNodeType { AstNodeTypeType type; Buf primitive_name; AstNode *child_type; bool is_const; }; struct AstNodeBlock { ZigList statements; }; 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, }; struct AstNodeExpression { AstNodeExpressionType type; union { Buf number; Buf string; AstNode *fn_call; } data; }; struct AstNodeFnCall { Buf name; ZigList params; }; struct AstNodeExternBlock { ZigList fn_decls; }; struct AstNode { enum NodeType type; AstNode *parent; int line; int column; CodeGenNode *codegen_node; union { AstNodeRoot root; AstNodeFnDef fn_def; AstNodeFnDecl fn_decl; AstNodeFnProto fn_proto; AstNodeType type; AstNodeParamDecl param_decl; AstNodeBlock block; AstNodeStatement statement; AstNodeExpression expression; AstNodeFnCall fn_call; AstNodeExternBlock extern_block; } data; }; __attribute__ ((format (printf, 2, 3))) void ast_token_error(Token *token, const char *format, ...); void ast_invalid_token_error(Buf *buf, Token *token); // This function is provided by generated code, generated by parsergen.cpp AstNode * ast_parse(Buf *buf, ZigList *tokens); const char *node_type_str(NodeType node_type); void ast_print(AstNode *node, int indent); #endif