-
Notifications
You must be signed in to change notification settings - Fork 2
/
weggli.h
158 lines (140 loc) · 4.49 KB
/
weggli.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once
/* Generated with cbindgen:0.24.3 */
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* An opaque container for individual query results.
*/
typedef struct QueryResult QueryResult;
/**
* An opaque container for zero or more query results, as produced by
* [`weggli_matches`](weggli_matches).
*/
typedef struct QueryResults QueryResults;
/**
* An opaque container for Weggli's query tree.
*/
typedef struct QueryTree QueryTree;
typedef bool (*ResultCallback)(const struct QueryResult*, void*);
typedef bool (*CapturesCallback)(size_t, size_t, void*);
typedef bool (*VariablesCallback)(const char*, size_t, size_t, void*);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Create a new Weggli query.
*
* # Safety
*
* * `q` must be a valid, NULL-terminated string.
*/
struct QueryTree *weggli_new_query(const char *q, bool cpp);
/**
* Destroy a Weggli query produced with [`weggli_new_query`](weggli_new_query).
*
* # Safety
*
* * `qt` must have been created by `weggli_new_query`, and must not have been
* previously passed into this function. Passing in any other source of
* query trees will produce a double-free.
*/
void weggli_destroy_query(struct QueryTree *qt);
/**
* Run a Weggli query against some source code.
*
* # Safety
*
* * `qt` must have been created by `weggli_new_query`. Passing in any other
* source of query trees will produce a double-free.
*
* * `source` must point to a valid region of memory containing a string
* of at least `length` bytes, **not** including any null terminator.
*/
struct QueryResults *weggli_matches(struct QueryTree *qt,
const char *source,
uintptr_t length,
bool cpp);
/**
* Destroy the matches produced by [`weggli_matches`](weggli_matches).
*
* # Safety
*
* * `res` must have been created by `weggli_matches`, and must not have been
* previously passed into this function.
*/
void weggli_destroy_matches(struct QueryResults *res);
/**
* Yield each match in `matches` to a callback. Callbacks have the following
* signature:
*
* ```c
* bool handle_result(const QueryResult *result, void *userdata);
* ```
*
* Where `QueryResult` is an opaque pointer and `userdata` is optional,
* user-supplied callback state.
*
* Callbacks can `true` to continue iteration, and `false` to exit.
*
* # Safety
*
* * `matches` must have been created by `weggli_matches`, and must not have
* been previously freed by a call to `weggli_destroy_matches`.
*
* * Callbacks must not hold onto match results for longer than `matches`
* is alive.
*/
void weggli_iter_matches(const struct QueryResults *matches, ResultCallback callback, void *user);
/**
* Yield each capture in `result` to a callback. Callbacks have the following
* signature:
*
* ```c
* bool handle_capture(size_t start, size_t end, void *userdata);
* ```
*
* Where the two `size_t` parameters represent the start and end range of
* the capture, and `userdata` is optional, user-supplied callback state.
*
* Callbacks can `true` to continue iteration, and `false` to exit.
*
* # Safety
*
* * `result` must have been created by `weggli_iter_matches`.
*/
void weggli_iter_match_captures(const struct QueryResult *result,
CapturesCallback callback,
void *user);
/**
* Yield each variable capture in `result` to a callback. Callbacks have the following
* signature:
*
* ```c
* bool handle_variable(const char *name, size_t start, size_t end, void *userdata);
* ```
*
* Where `name` is the name of the variable, the two `size_t` parameters
* represent the start and end range of the capture, and `userdata` is
* optional, user-supplied callback state.
*
* Callbacks can `true` to continue iteration, and `false` to exit.
*
* Returns `false` if an error occurs during variable-to-capture mapping
* (e.g., if a variable has an unrepresentable name or references a
* nonexistent capture).
*
* # Safety
*
* * `result` must have been created by `weggli_iter_matches`.
*
* * Callbacks must not hold onto `name` for longer than their own lifetime.
*/
bool weggli_iter_match_variables(const struct QueryResult *result,
VariablesCallback callback,
void *user);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus