You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: import/code/README.md
+96-59
Original file line number
Diff line number
Diff line change
@@ -17,99 +17,135 @@ An import file that provides codes to make it easier to build an AST (abstract s
17
17
The usage procedure is shown below.
18
18
19
19
1. Import `code/pcc_ast.peg`**after the last `%header` section** in the PEG file if any.
20
-
```
20
+
```c
21
21
%import "code/pcc_ast.peg"
22
22
```
23
23
2. Set the designated data types as follows:
24
-
```
24
+
```c
25
25
%value "pcc_ast_node_t *"
26
26
27
27
%auxil "pcc_ast_manager_t *"
28
28
```
29
+
30
+
If the prefix is set with `%prefix`, all symbols starting with <code><b><i>pcc</i></b>\_</code> are changed to those with the specified prefix as below.
31
+
```c
32
+
%prefix "my"
33
+
34
+
%value "my_ast_node_t *"
35
+
36
+
%auxil "my_ast_manager_t *"
37
+
```
29
38
3. Create an AST node using either of the following functions in every rule action.
As written above, if the prefix is set with `%prefix`, all symbols starting with <code><b><i>pcc</i></b>\_</code> are changed to those with the specified prefix.
57
+
58
+
There are the variants of the node creation functions that enable setting a label as an `int` value.
59
+
The label can be used for specifying node kinds in order to make it easier to analyze the AST in the later parsing steps.
4. Call the generated parser API functions as follows:
50
76
```c
51
-
pcc_ast_manager_t mgr;
52
-
pcc_ast_manager__initialize(&mgr);
77
+
my_ast_manager_t mgr;
78
+
my_ast_manager__initialize(&mgr);
53
79
{
54
-
pcc_context_t*ctx = pcc_create(&mgr);
55
-
pcc_ast_node_t*ast = NULL; /* ast: the root node of the AST */
56
-
while (pcc_parse(ctx, &ast)) {
80
+
my_context_t *ctx = my_create(&mgr);
81
+
my_ast_node_t *ast = NULL; /* ast: the root node of the AST */
82
+
while (my_parse(ctx, &ast)) {
57
83
/* ... do something needed here */
58
-
pcc_ast_node__destroy(ast);
84
+
my_ast_node__destroy(ast);
59
85
}
60
-
pcc_destroy(ctx);
86
+
my_destroy(ctx);
61
87
}
62
-
pcc_ast_manager__finalize(&mgr);
88
+
my_ast_manager__finalize(&mgr);
63
89
```
64
90
This code can be executed safely with no memory leak (if "_do something needed here_" does not bring memory leaks).
65
91
66
92
#### Customization
67
93
68
94
To build a meaningful AST, customization of the node is needed.
69
-
By defining the macro `PCC_AST_NODE_CUSTOM_DATA_DEFINED` in a `%header` section before `%import "code/pcc_ast.peg"`,
70
-
the node member variable `custom` whose data type is `pcc_ast_node_custom_data_t` is enabled for storing node custom data.
95
+
By defining the macro <code><b><i>PCC</i></b>\_AST_NODE_CUSTOM_DATA_DEFINED</code> in a `%header` section before `%import "code/pcc_ast.peg"`,
96
+
the node member variable `custom` whose data type is <code><b><i>pcc</i></b>\_ast_node_custom_data_t</code> is enabled for storing node custom data.
97
+
If the prefix is set with `%prefix`, the macro name <code><b><i>PCC</i></b>\_AST_NODE_CUSTOM_DATA_DEFINED</code> is changed to those with the uppercased prefix as below.
98
+
```c
99
+
%prefix "my"
100
+
101
+
%header {
102
+
#define MY_AST_NODE_CUSTOM_DATA_DEFINED
103
+
...
104
+
}
105
+
```
106
+
71
107
The concrete usage procedure is shown below.
72
108
73
109
1. Define the data type of the node custom data in a PEG file.
74
110
```c
75
111
%header {
76
-
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */
112
+
#define MY_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */
77
113
78
114
typedef struct node_custom_data_tag { /* <-- node custom data type */
79
115
/* ... define member variables as needed */
80
-
} pcc_ast_node_custom_data_t;
116
+
} my_ast_node_custom_data_t;
81
117
}
82
118
```
83
119
An example is as follows.
84
120
```c
85
121
%header {
86
-
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED
122
+
#define MY_AST_NODE_CUSTOM_DATA_DEFINED
87
123
88
124
typedef struct text_data_tag {
89
125
char *text;
90
-
} pcc_ast_node_custom_data_t;
126
+
} my_ast_node_custom_data_t;
91
127
}
92
128
```
93
129
Make sure that this `%header` section is located before `%import "code/pcc_ast.peg"`.
94
130
2. Set a node custom data value in every rule action as needed.
@@ -120,11 +156,12 @@ Some macros are prepared to customize the behavior of memory allocation for AST
120
156
The macro definition should be **in `%source` section** in the PEG source.
121
157
122
158
The following macros are available.
159
+
Note that, unlike other symbols, the prefix of these macro names is never changed even when a different prefix is set with `%prefix`.
123
160
124
161
**`PCC_AST_MALLOC(`**_mgr_**`,`**_size_**`)`**
125
162
126
163
The function macro to allocate a memory block.
127
-
The pointer to the instance of `pcc_ast_manager_t` that was passed to the API function `pcc_create()` can be retrieved from the argument _auxil_.
164
+
The pointer to the instance of <code><b><i>pcc</i></b>\_ast_manager_t</code> that was passed to the API function <code><b><i>pcc</i></b>\_create()</code> can be retrieved from the argument _auxil_.
128
165
It can be ignored if the instance does not concern memory allocation.
129
166
The argument _size_ is the number of bytes to allocate.
130
167
This macro must return a pointer to the allocated memory block, or `NULL` if no sufficient memory is available.
@@ -134,7 +171,7 @@ The default is defined as `PCC_MALLOC(mgr, size)`, which is used in the generate
The function macro to reallocate the existing memory block.
137
-
The pointer to the instance of `pcc_ast_manager_t` that was passed to the API function `pcc_create()` can be retrieved from the argument _auxil_.
174
+
The pointer to the instance of <code><b><i>pcc</i></b>\_ast_manager_t</code> that was passed to the API function <code><b><i>pcc</i></b>\_create()</code> can be retrieved from the argument _auxil_.
138
175
It can be ignored if the instance does not concern memory allocation.
139
176
The argument _ptr_ is the pointer to the previously allocated memory block.
140
177
The argument _size_ is the new number of bytes to reallocate.
@@ -146,7 +183,7 @@ The default is defined as `PCC_REALLOC(mgr, ptr, size)`, which is used in the ge
146
183
**`PCC_AST_FREE(`**_mgr_**`,`**_ptr_**`)`**
147
184
148
185
The function macro to free the existing memory block.
149
-
The pointer to the instance of `pcc_ast_manager_t` that was passed to the API function `pcc_create()` can be retrieved from the argument _auxil_.
186
+
The pointer to the instance of <code><b><i>pcc</i></b>\_ast_manager_t</code> that was passed to the API function <code><b><i>pcc</i></b>\_create()</code> can be retrieved from the argument _auxil_.
150
187
It can be ignored if the instance does not concern memory allocation.
151
188
The argument _ptr_ is the pointer to the previously allocated memory block.
152
189
This macro need not return a value.
@@ -167,16 +204,16 @@ This example accepts the same inputs as [*Desktop Calculator*](../../examples/ca
167
204
```c
168
205
%prefix "calc"
169
206
170
-
%value "pcc_ast_node_t *" # <-- must be set
207
+
%value "calc_ast_node_t *" # <-- must be set
171
208
172
-
%auxil "pcc_ast_manager_t *" # <-- must be set
209
+
%auxil "calc_ast_manager_t *" # <-- must be set
173
210
174
211
%header {
175
-
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */
212
+
#define CALC_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */
176
213
177
214
typedef struct text_data_tag { /* <-- node custom data type */
0 commit comments