-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathast.h
136 lines (126 loc) · 3.34 KB
/
ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Abstract Syntax Tree building functions for parser
* of the C Programming Language (ISO/IEC 9899:2018).
*
* @authors: Denis Chernikov, Vladislav Kuleykin
*/
#ifndef C_PARSER_AST_BUILDER_H_INCLUDED
#define C_PARSER_AST_BUILDER_H_INCLUDED
/// Types of AST node content.
typedef enum
{
TranslationUnit,
FunctionDefinition,
DeclarationList,
Declaration,
DeclarationSpecifiers,
InitDeclaratorList,
InitDeclarator,
StorageClassSpecifier,
TypeSpecifier,
StructSpecifier,
UnionSpecifier,
StructDeclarationList,
StructDeclaration,
SpecifierQualifierList,
StructDeclaratorList,
StructDeclarator,
EnumSpecifier,
EnumeratorList,
Enumerator,
AtomicTypeSpecifier,
TypeQualifier,
FunctionSpecifier,
AlignmentSpecifier,
Declarator,
DirectDeclarator,
DirectDeclaratorBrackets,
DirectDeclaratorParen,
Pointer,
TypeQualifierList,
ParameterList,
ParameterDeclaration,
IdentifierList,
TypeName,
AbstractDeclarator,
DirectAbstractDeclarator,
DirectAbstractDeclaratorBrackets,
DirectAbstractDeclaratorParen,
Initializer,
InitializerList,
InitializerListElem,
DesignatorList,
StaticAssertDeclaration,
LabeledStatement,
BlockItemList,
SelectionStatement,
IterationStatement,
JumpStatement,
Expression,
AssignmentExpression,
AssignmentOperator,
ConditionalExpression,
ArithmeticalExpression,
CastExpression,
UnaryExpression,
UnaryOperator,
PostfixExpression,
ArgumentExpressionList,
GenericSelection,
GenericAssocList,
GenericAssociation,
Identifier,
StringLiteral,
IntegerConstant,
FloatingConstant,
CharacterConstant,
}
AST_NODE_TYPE;
typedef union
{
int token;
void *value;
}
AST_CONTENT;
/// Structure for storing AST node data.
typedef struct AST_NODE
{
AST_NODE_TYPE type;
AST_CONTENT content;
int children_number;
struct AST_NODE **children;
}
AST_NODE;
/// Create node with a given set of children.
/// Needs to be freed.
///
/// \param type Type of AST node
/// \param content Content to store in the node
/// \param n_children Number of children
/// \param ... List of children
/// \return New AST node
AST_NODE *ast_create_node(AST_NODE_TYPE type, AST_CONTENT content, int n_children, ...);
/// Append given child to the given AST node.
///
/// \param node Node to append child to
/// \param to_append Child to append to the node
/// \return New node after expansion
AST_NODE *ast_expand_node(AST_NODE *node, AST_NODE *to_append);
/// Convert enum AST_NODE_TYPE to string.
///
/// \param type Enum value to convert
/// \return Actual string representation of a value
char *ast_type_to_str(AST_NODE_TYPE type);
/// Free memory associated with node and it's children.
///
/// \param root Root of the tree to be freed recursively
void ast_free(AST_NODE *root);
/// Get JSON string representation of an AST. Needs to be freed.
///
/// \param root Root of the tree to be converted to JSON
/// \param shift Shift size at the beginning of line
/// \param tab String representation of the tabulation
/// \param cont_to_str Function for printing the content of the node
/// \return JSON representation of a tree
char *ast_to_json(AST_NODE *root, int shift, char *tab, char *(*cont_to_str)(AST_NODE *));
#endif //C_PARSER_AST_BUILDER_H_INCLUDED