]>
git.saurik.com Git - apple/libc.git/blob - regex/TRE/lib/tre-ast.c
12f8cdc6ba95d161f041c7a85ece5a140eed9d7b
2 tre-ast.c - Abstract syntax tree (AST) routines
4 This software is released under a BSD-style license.
5 See the file LICENSE for details and copyright.
11 #endif /* HAVE_CONFIG_H */
18 tre_ast_new_node(tre_mem_t mem
, tre_ast_type_t type
, size_t size
)
22 node
= tre_mem_calloc(mem
, sizeof(*node
));
25 node
->obj
= tre_mem_calloc(mem
, size
);
30 node
->submatch_id
= -1;
36 tre_ast_new_literal(tre_mem_t mem
, int code_min
, int code_max
, int position
)
41 node
= tre_ast_new_node(mem
, LITERAL
, sizeof(tre_literal_t
));
45 lit
->code_min
= code_min
;
46 lit
->code_max
= code_max
;
47 lit
->position
= position
;
53 tre_ast_new_iter(tre_mem_t mem
, tre_ast_node_t
*arg
, int min
, int max
,
57 tre_iteration_t
*iter
;
59 node
= tre_ast_new_node(mem
, ITERATION
, sizeof(tre_iteration_t
));
66 iter
->minimal
= minimal
;
67 node
->num_submatches
= arg
->num_submatches
;
73 tre_ast_new_union(tre_mem_t mem
, tre_ast_node_t
*left
, tre_ast_node_t
*right
)
77 node
= tre_ast_new_node(mem
, UNION
, sizeof(tre_union_t
));
80 ((tre_union_t
*)node
->obj
)->left
= left
;
81 ((tre_union_t
*)node
->obj
)->right
= right
;
82 node
->num_submatches
= left
->num_submatches
+ right
->num_submatches
;
88 tre_ast_new_catenation(tre_mem_t mem
, tre_ast_node_t
*left
,
89 tre_ast_node_t
*right
)
93 node
= tre_ast_new_node(mem
, CATENATION
, sizeof(tre_catenation_t
));
96 ((tre_catenation_t
*)node
->obj
)->left
= left
;
97 ((tre_catenation_t
*)node
->obj
)->right
= right
;
98 node
->num_submatches
= left
->num_submatches
+ right
->num_submatches
;
106 tre_findent(FILE *stream
, int i
)
113 tre_print_params(int *params
)
118 DPRINT(("params ["));
119 for (i
= 0; i
< TRE_PARAM_LAST
; i
++)
121 if (params
[i
] == TRE_PARAM_UNSET
)
123 else if (params
[i
] == TRE_PARAM_DEFAULT
)
126 DPRINT(("%d", params
[i
]));
127 if (i
< TRE_PARAM_LAST
- 1)
135 tre_do_print(FILE *stream
, tre_ast_node_t
*ast
, int indent
)
137 int code_min
, code_max
, pos
;
138 int num_tags
= ast
->num_tags
;
140 tre_iteration_t
*iter
;
142 tre_findent(stream
, indent
);
147 code_min
= lit
->code_min
;
148 code_max
= lit
->code_max
;
152 fprintf(stream
, "literal empty\n");
154 else if (IS_ASSERTION(lit
))
157 char *assertions
[] = { "bol", "eol", "bracket",
158 "bow", "eow", "wb", "!wb", "backref" };
159 if (code_max
>= ASSERT_LAST
<< 1)
161 fprintf(stream
, "assertions: ");
162 for (i
= 0; (1 << i
) <= ASSERT_LAST
; i
++)
163 if (code_max
& (1 << i
))
164 fprintf(stream
, "%s ", assertions
[i
]);
165 fprintf(stream
, "\n");
167 else if (IS_TAG(lit
))
169 fprintf(stream
, "tag %d\n", code_max
);
171 else if (IS_BACKREF(lit
))
173 fprintf(stream
, "backref %d, pos %d\n", code_max
, pos
);
175 else if (IS_PARAMETER(lit
))
177 tre_print_params(lit
->u
.params
);
178 fprintf(stream
, "\n");
182 fprintf(stream
, "literal (%c, %c) (%d, %d), pos %d, sub %d, "
183 "%d tags\n", code_min
, code_max
, code_min
, code_max
, pos
,
184 ast
->submatch_id
, num_tags
);
189 fprintf(stream
, "iteration {%d, %d}, sub %d, %d tags, %s\n",
190 iter
->min
, iter
->max
, ast
->submatch_id
, num_tags
,
191 iter
->minimal
? "minimal" : "greedy");
192 tre_do_print(stream
, iter
->arg
, indent
+ 2);
195 fprintf(stream
, "union, sub %d, %d tags\n", ast
->submatch_id
, num_tags
);
196 tre_do_print(stream
, ((tre_union_t
*)ast
->obj
)->left
, indent
+ 2);
197 tre_do_print(stream
, ((tre_union_t
*)ast
->obj
)->right
, indent
+ 2);
200 fprintf(stream
, "catenation, sub %d, %d tags\n", ast
->submatch_id
,
202 tre_do_print(stream
, ((tre_catenation_t
*)ast
->obj
)->left
, indent
+ 2);
203 tre_do_print(stream
, ((tre_catenation_t
*)ast
->obj
)->right
, indent
+ 2);
212 tre_ast_fprint(FILE *stream
, tre_ast_node_t
*ast
)
214 tre_do_print(stream
, ast
, 0);
218 tre_ast_print(tre_ast_node_t
*tree
)
221 tre_ast_fprint(stdout
, tree
);
224 #endif /* TRE_DEBUG */