Skip to content

Commit 0fc6d26

Browse files
committed
fix: minimal yaml_translate.h — no system headers, avoids cross-compile failures
Replace _INC_STRING guard with fully self-contained header that declares only types/functions used by src/yaml.zig. Eliminates: - MSVC wcscat_s/wcscpy_s (via <string.h> -> <wchar.h>) - NetBSD #pragma GCC visibility pop (via <stdio.h>/<stdlib.h> -> sys/cdefs.h) - Other translator-hostile system header constructs Source: libyaml 0.2.5 include/yaml.h, copied verbatim for ABI match.
1 parent 85f92b5 commit 0fc6d26

1 file changed

Lines changed: 324 additions & 10 deletions

File tree

lib/yaml/yaml_translate.h

Lines changed: 324 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,336 @@
11
/*
2-
* Wrapper header for Zig translateC that avoids the MSVC secure CRT trap.
2+
* Minimal yaml.h subset for Zig translateC.
33
*
4-
* yaml.h includes <string.h> which on MSVC pulls in <wchar.h> declaring
5-
* wcscat_s/wcscpy_s. Zig 0.16's translateC generates bindings for all
6-
* declarations, but libyaml never calls these, causing "unused local
7-
* constant" errors.
4+
* Contains ONLY the types and functions used by src/yaml.zig.
5+
* NO system headers included — avoids MSVC wcscat_s, NetBSD #pragma GCC visibility,
6+
* and other translator-hostile constructs.
87
*
9-
* Pre-define _INC_STRING to skip <string.h> (we provide size_t via <stddef.h>).
10-
* The C compilation step uses the real yaml.h with string.h intact.
8+
* Source: libyaml 0.2.5 include/yaml.h — copied verbatim for ABI compatibility.
119
*/
1210

1311
#ifndef YAML_TRANSLATE_H
1412
#define YAML_TRANSLATE_H
1513

16-
#include <stddef.h>
14+
/* Basic types — use built-in C types, no stddef.h needed */
15+
typedef unsigned char yaml_char_t;
16+
typedef unsigned long size_t;
1717

18-
#define _INC_STRING 1
18+
/* Version directives */
19+
typedef struct yaml_version_directive_s {
20+
int major;
21+
int minor;
22+
} yaml_version_directive_t;
1923

20-
#include "yaml.h"
24+
/* Tag directives */
25+
typedef struct yaml_tag_directive_s {
26+
yaml_char_t *handle;
27+
yaml_char_t *prefix;
28+
} yaml_tag_directive_t;
29+
30+
/* Encoding */
31+
typedef enum yaml_encoding_e {
32+
YAML_ANY_ENCODING,
33+
YAML_UTF8_ENCODING,
34+
YAML_UTF16LE_ENCODING,
35+
YAML_UTF16BE_ENCODING
36+
} yaml_encoding_t;
37+
38+
/* Line breaks */
39+
typedef enum yaml_break_e {
40+
YAML_ANY_BREAK,
41+
YAML_CR_BREAK,
42+
YAML_LN_BREAK,
43+
YAML_CRLN_BREAK
44+
} yaml_break_t;
45+
46+
/* Error types */
47+
typedef enum yaml_error_type_e {
48+
YAML_NO_ERROR,
49+
YAML_MEMORY_ERROR,
50+
YAML_READER_ERROR,
51+
YAML_SCANNER_ERROR,
52+
YAML_PARSER_ERROR,
53+
YAML_COMPOSER_ERROR,
54+
YAML_WRITER_ERROR,
55+
YAML_EMITTER_ERROR
56+
} yaml_error_type_t;
57+
58+
/* Mark (position) */
59+
typedef struct yaml_mark_s {
60+
size_t index;
61+
size_t line;
62+
size_t column;
63+
} yaml_mark_t;
64+
65+
/* Scalar styles */
66+
typedef enum yaml_scalar_style_e {
67+
YAML_ANY_SCALAR_STYLE,
68+
YAML_PLAIN_SCALAR_STYLE,
69+
YAML_SINGLE_QUOTED_SCALAR_STYLE,
70+
YAML_DOUBLE_QUOTED_SCALAR_STYLE,
71+
YAML_LITERAL_SCALAR_STYLE,
72+
YAML_FOLDED_SCALAR_STYLE
73+
} yaml_scalar_style_t;
74+
75+
/* Sequence styles */
76+
typedef enum yaml_sequence_style_e {
77+
YAML_ANY_SEQUENCE_STYLE,
78+
YAML_BLOCK_SEQUENCE_STYLE,
79+
YAML_FLOW_SEQUENCE_STYLE
80+
} yaml_sequence_style_t;
81+
82+
/* Mapping styles */
83+
typedef enum yaml_mapping_style_e {
84+
YAML_ANY_MAPPING_STYLE,
85+
YAML_BLOCK_MAPPING_STYLE,
86+
YAML_FLOW_MAPPING_STYLE
87+
} yaml_mapping_style_t;
88+
89+
/* Token types */
90+
typedef enum yaml_token_type_e {
91+
YAML_NO_TOKEN,
92+
YAML_STREAM_START_TOKEN,
93+
YAML_STREAM_END_TOKEN,
94+
YAML_VERSION_DIRECTIVE_TOKEN,
95+
YAML_TAG_DIRECTIVE_TOKEN,
96+
YAML_DOCUMENT_START_TOKEN,
97+
YAML_DOCUMENT_END_TOKEN,
98+
YAML_BLOCK_SEQUENCE_START_TOKEN,
99+
YAML_BLOCK_MAPPING_START_TOKEN,
100+
YAML_BLOCK_END_TOKEN,
101+
YAML_FLOW_SEQUENCE_START_TOKEN,
102+
YAML_FLOW_SEQUENCE_END_TOKEN,
103+
YAML_FLOW_MAPPING_START_TOKEN,
104+
YAML_FLOW_MAPPING_END_TOKEN,
105+
YAML_BLOCK_ENTRY_TOKEN,
106+
YAML_FLOW_ENTRY_TOKEN,
107+
YAML_KEY_TOKEN,
108+
YAML_VALUE_TOKEN,
109+
YAML_ALIAS_TOKEN,
110+
YAML_ANCHOR_TOKEN,
111+
YAML_TAG_TOKEN,
112+
YAML_SCALAR_TOKEN
113+
} yaml_token_type_t;
114+
115+
/* Token structure */
116+
typedef struct yaml_token_s {
117+
yaml_token_type_t type;
118+
union {
119+
struct { yaml_encoding_t encoding; } stream_start;
120+
struct { yaml_char_t *value; } alias;
121+
struct { yaml_char_t *value; } anchor;
122+
struct { yaml_char_t *handle; yaml_char_t *suffix; } tag;
123+
struct { yaml_char_t *value; size_t length; yaml_scalar_style_t style; } scalar;
124+
struct { int major; int minor; } version_directive;
125+
struct { yaml_char_t *handle; yaml_char_t *prefix; } tag_directive;
126+
} data;
127+
yaml_mark_t start_mark;
128+
yaml_mark_t end_mark;
129+
} yaml_token_t;
130+
131+
/* Event types */
132+
typedef enum yaml_event_type_e {
133+
YAML_NO_EVENT,
134+
YAML_STREAM_START_EVENT,
135+
YAML_STREAM_END_EVENT,
136+
YAML_DOCUMENT_START_EVENT,
137+
YAML_DOCUMENT_END_EVENT,
138+
YAML_ALIAS_EVENT,
139+
YAML_SCALAR_EVENT,
140+
YAML_SEQUENCE_START_EVENT,
141+
YAML_SEQUENCE_END_EVENT,
142+
YAML_MAPPING_START_EVENT,
143+
YAML_MAPPING_END_EVENT
144+
} yaml_event_type_t;
145+
146+
/* Event structure */
147+
typedef struct yaml_event_s {
148+
yaml_event_type_t type;
149+
union {
150+
struct { yaml_encoding_t encoding; } stream_start;
151+
struct {
152+
yaml_version_directive_t *version_directive;
153+
struct { yaml_tag_directive_t *start; yaml_tag_directive_t *end; } tag_directives;
154+
int implicit;
155+
} document_start;
156+
struct { int implicit; } document_end;
157+
struct { yaml_char_t *anchor; } alias;
158+
struct {
159+
yaml_char_t *anchor;
160+
yaml_char_t *tag;
161+
yaml_char_t *value;
162+
size_t length;
163+
int plain_implicit;
164+
int quoted_implicit;
165+
yaml_scalar_style_t style;
166+
} scalar;
167+
struct {
168+
yaml_char_t *anchor;
169+
yaml_char_t *tag;
170+
int implicit;
171+
yaml_sequence_style_t style;
172+
} sequence_start;
173+
struct {
174+
yaml_char_t *anchor;
175+
yaml_char_t *tag;
176+
int implicit;
177+
yaml_mapping_style_t style;
178+
} mapping_start;
179+
} data;
180+
yaml_mark_t start_mark;
181+
yaml_mark_t end_mark;
182+
} yaml_event_t;
183+
184+
/* Read handler prototype */
185+
typedef int yaml_read_handler_t(void *data, unsigned char *buffer, size_t size,
186+
size_t *size_read);
187+
188+
/* Parser states (subset needed for struct layout) */
189+
typedef enum yaml_parser_state_e {
190+
YAML_PARSE_STREAM_START_STATE,
191+
YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE,
192+
YAML_PARSE_DOCUMENT_START_STATE,
193+
YAML_PARSE_DOCUMENT_CONTENT_STATE,
194+
YAML_PARSE_DOCUMENT_END_STATE,
195+
YAML_PARSE_BLOCK_NODE_STATE,
196+
YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE,
197+
YAML_PARSE_FLOW_NODE_STATE,
198+
YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE,
199+
YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE,
200+
YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE,
201+
YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE,
202+
YAML_PARSE_BLOCK_MAPPING_KEY_STATE,
203+
YAML_PARSE_BLOCK_MAPPING_VALUE_STATE,
204+
YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE,
205+
YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE,
206+
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE,
207+
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE,
208+
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE,
209+
YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE,
210+
YAML_PARSE_FLOW_MAPPING_KEY_STATE,
211+
YAML_PARSE_FLOW_MAPPING_VALUE_STATE,
212+
YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE,
213+
YAML_PARSE_END_STATE
214+
} yaml_parser_state_t;
215+
216+
/* Simple key */
217+
typedef struct yaml_simple_key_s {
218+
int possible;
219+
int required;
220+
size_t token_number;
221+
yaml_mark_t mark;
222+
} yaml_simple_key_t;
223+
224+
/* Alias data */
225+
typedef struct yaml_alias_data_s {
226+
yaml_char_t *anchor;
227+
int index;
228+
yaml_mark_t mark;
229+
} yaml_alias_data_t;
230+
231+
/* Document structure (forward for parser) */
232+
typedef struct yaml_document_s yaml_document_t;
233+
typedef struct yaml_node_s yaml_node_t;
234+
235+
/* Full parser structure — must match libyaml ABI exactly */
236+
typedef struct yaml_parser_s {
237+
/* Error handling */
238+
yaml_error_type_t error;
239+
const char *problem;
240+
size_t problem_offset;
241+
int problem_value;
242+
yaml_mark_t problem_mark;
243+
const char *context;
244+
yaml_mark_t context_mark;
245+
246+
/* Reader */
247+
yaml_read_handler_t *read_handler;
248+
void *read_handler_data;
249+
union {
250+
struct {
251+
const unsigned char *start;
252+
const unsigned char *end;
253+
const unsigned char *current;
254+
} string;
255+
void *file;
256+
} input;
257+
int eof;
258+
struct {
259+
yaml_char_t *start;
260+
yaml_char_t *end;
261+
yaml_char_t *pointer;
262+
yaml_char_t *last;
263+
} buffer;
264+
size_t unread;
265+
struct {
266+
unsigned char *start;
267+
unsigned char *end;
268+
unsigned char *pointer;
269+
unsigned char *last;
270+
} raw_buffer;
271+
yaml_encoding_t encoding;
272+
size_t offset;
273+
yaml_mark_t mark;
274+
275+
/* Scanner */
276+
int stream_start_produced;
277+
int stream_end_produced;
278+
int flow_level;
279+
struct {
280+
yaml_token_t *start;
281+
yaml_token_t *end;
282+
yaml_token_t *head;
283+
yaml_token_t *tail;
284+
} tokens;
285+
size_t tokens_parsed;
286+
int token_available;
287+
struct {
288+
int *start;
289+
int *end;
290+
int *top;
291+
} indents;
292+
int indent;
293+
int simple_key_allowed;
294+
struct {
295+
yaml_simple_key_t *start;
296+
yaml_simple_key_t *end;
297+
yaml_simple_key_t *top;
298+
} simple_keys;
299+
300+
/* Parser */
301+
struct {
302+
yaml_parser_state_t *start;
303+
yaml_parser_state_t *end;
304+
yaml_parser_state_t *top;
305+
} states;
306+
yaml_parser_state_t state;
307+
struct {
308+
yaml_mark_t *start;
309+
yaml_mark_t *end;
310+
yaml_mark_t *top;
311+
} marks;
312+
struct {
313+
yaml_tag_directive_t *start;
314+
yaml_tag_directive_t *end;
315+
yaml_tag_directive_t *top;
316+
} tag_directives;
317+
318+
/* Dumper (unused but needed for struct layout) */
319+
struct {
320+
yaml_alias_data_t *start;
321+
yaml_alias_data_t *end;
322+
yaml_alias_data_t *top;
323+
} aliases;
324+
yaml_document_t *document;
325+
} yaml_parser_t;
326+
327+
/* Function declarations */
328+
int yaml_parser_initialize(yaml_parser_t *parser);
329+
void yaml_parser_delete(yaml_parser_t *parser);
330+
void yaml_parser_set_input_string(yaml_parser_t *parser,
331+
const unsigned char *input, size_t size);
332+
int yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event);
333+
void yaml_event_delete(yaml_event_t *event);
334+
void yaml_token_delete(yaml_token_t *token);
21335

22336
#endif /* YAML_TRANSLATE_H */

0 commit comments

Comments
 (0)