aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/ryml/api/ryml.i
blob: ac226142afdeed301a20e870b59c3ab9a1f81fd9 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
%module ryml


//-----------------------------------------------------------------------------
// this block will be pasted verbatim in the generated C++ source file

%{
// specifies that the resulting C file should be built as a python
// extension, inserting the module init code
#define SWIG_FILE_WITH_INIT

#include <c4/yml/yml.hpp>

namespace c4 {
namespace yml {

using substr = c4::substr;
using csubstr = c4::csubstr;

} /* namespace yml */
} /* namespace c4 */

%}

//-----------------------------------------------------------------------------


%apply (const char *STRING, size_t LENGTH) { (const char *str, size_t len) };
%apply (char *STRING, size_t LENGTH) { (char *str, size_t len) };
%newobject emit_malloc;

%typemap(in) c4::substr {
#if defined(SWIGPYTHON)
  Py_buffer view;
  int ok = PyObject_CheckBuffer($input);
  if(ok)
  {
      ok = (0 == PyObject_GetBuffer($input, &view, PyBUF_SIMPLE|PyBUF_WRITABLE));
  }
  if(ok)
  {
      $1 = c4::substr((char*)view.buf, view.len);
      PyBuffer_Release(&view);
  }
  else
  {
      PyErr_SetString(PyExc_TypeError, "could not get mutable memory for c4::csubstr - have you passed a str?");
      SWIG_fail;
  }
#else
#error no "in" typemap defined for this export language
#endif
};

%typemap(in) c4::csubstr {
#if defined(SWIGPYTHON)
  Py_buffer view;
  view.buf = nullptr;
  int ok = PyObject_CheckBuffer($input);
  if(ok)
  {
      ok = (0 == PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO));
  }
  if(ok)
  {
      $1 = c4::csubstr((const char*)view.buf, view.len);
      PyBuffer_Release(&view);
  }
  else
  {
      // https://stackoverflow.com/questions/36098984/python-3-3-c-api-and-utf-8-strings
      Py_ssize_t sz = 0;
      const char *buf = PyUnicode_AsUTF8AndSize($input, &sz);
      if(buf || sz == 0)
      {
          $1 = c4::csubstr(buf, sz);
      }
      else
      {
          PyErr_SetString(PyExc_TypeError, "c4::csubstr: could not get readonly memory from python object");
          SWIG_fail;
      }
  }
#else
#error no "in" typemap defined for this export language
#endif
};
// Copy the typecheck code for "char *".
%typemap(typecheck) c4::substr = char *;
%typemap(typecheck) c4::csubstr = const char *;


%typemap(out) c4::csubstr {
#if defined(SWIGPYTHON)
  if($1.str == nullptr) {
    $result = Py_None;
    Py_INCREF($result);
  } else {
    PyObject *obj = PyMemoryView_FromMemory((char*)$1.str, $1.len, PyBUF_READ);
    if( ! obj)
    {
        PyErr_SetString(PyExc_TypeError, "could not get readonly memory from c4::csubstr - have you passed a str?");
        SWIG_fail;
    }
    $result = obj;
  }
#else
#error no "out" typemap defined for this export language
#endif
};


%inline %{

void parse_csubstr(c4::csubstr s, c4::yml::Tree *t)
{
    c4::yml::parse_in_arena(s, t);
}

void parse_substr(c4::substr s, c4::yml::Tree *t)
{
    c4::yml::parse_in_place(s, t);
}

char * emit_yaml_malloc(c4::yml::Tree const& t, size_t id)
{
    c4::substr buf;
    c4::substr ret = c4::yml::emit_yaml(t, id, buf, /*error_on_excess*/false);
    if(ret.str == nullptr && ret.len > 0)
    {
        // Use new[] to parse with delete[] in SWIG.
        char * alloc = new char[ret.len + 1]; // we'll return a c-string and not a csubstr
        c4::substr alloced_buf(alloc, ret.len);
        ret = c4::yml::emit_yaml(t, id, alloced_buf, /*error_on_excess*/true);
        ret.str[ret.len] = 0;
    }
    return ret.str;
}

char * emit_json_malloc(c4::yml::Tree const& t, size_t id)
{
    c4::substr buf;
    c4::substr ret = c4::yml::emit_json(t, id, buf, /*error_on_excess*/false);
    if(ret.str == nullptr && ret.len > 0)
    {
        // Use new[] to parse with delete[] in SWIG.
        char * alloc = new char[ret.len + 1]; // we'll return a c-string and not a csubstr
        c4::substr alloced_buf(alloc, ret.len);
        ret = c4::yml::emit_json(t, id, alloced_buf, /*error_on_excess*/true);
        ret.str[ret.len] = 0;
    }
    return ret.str;
}

size_t emit_yaml_length(const c4::yml::Tree &t, size_t id)
{
    c4::substr buf;
    c4::substr ret = c4::yml::emit_yaml(t, id, buf, /*error_on_excess*/false);
    return ret.len;
}

size_t emit_json_length(const c4::yml::Tree &t, size_t id)
{
    c4::substr buf;
    c4::substr ret = c4::yml::emit_json(t, id, buf, /*error_on_excess*/false);
    return ret.len;
}

bool emit_yaml_to_substr(const c4::yml::Tree &t, size_t id, c4::substr s, size_t *OUTPUT)
{
    c4::substr result = c4::yml::emit_yaml(t, id, s, /*error_on_excess*/false);
    *OUTPUT = result.len;
    return result.str == nullptr;
}

bool emit_json_to_substr(const c4::yml::Tree &t, size_t id, c4::substr s, size_t *OUTPUT)
{
    c4::substr result = c4::yml::emit_json(t, id, s, /*error_on_excess*/false);
    *OUTPUT = result.len;
    return result.str == nullptr;
}


// force a roundtrip to C++, which triggers a conversion to csubstr and returns it as a memoryview
c4::csubstr _get_as_csubstr(c4::csubstr s)
{
    //printf("_get_as_csubstr: %p[%zu]'%.*s'\n", s.str, s.len, (int)s.len, s.str);
    return s;
}

c4::csubstr  _get_as_substr(c4::substr s)
{
    //printf("_get_as_substr: %p[%zu]'%.*s'\n", s.str, s.len, (int)s.len, s.str);
    return s;
}


// utilities for testing
bool _same_ptr(c4::csubstr l, c4::csubstr r)
{
    return l.str == r.str;
}

bool _same_mem(c4::csubstr l, c4::csubstr r)
{
    return l.str == r.str && l.len == r.len;
}


%}


//-----------------------------------------------------------------------------

%pythoncode %{

from deprecation import deprecated


def as_csubstr(s):
    return _get_as_csubstr(s)

def as_substr(s):
    return _get_as_substr(s)

def u(memview):
    return str(memview, "utf8")


def children(tree, node=None):
    assert tree is not None
    if node is None:
        node = tree.root_id()
    ch = tree.first_child(node)
    while ch != NONE:
        yield ch
        ch = tree.next_sibling(ch)


def siblings(tree, node):
    assert tree is not None
    if node is None:
        return
    ch = tree.first_sibling(node)
    while ch != NONE:
        yield ch
        ch = tree.next_sibling(ch)


def walk(tree, node=None, indentation_level=0):
    assert tree is not None
    if node is None: node = tree.root_id()
    yield node, indentation_level
    ch = tree.first_child(node)
    while ch != NONE:
       for gc, il in walk(tree, ch, indentation_level + 1):
           yield gc, il
       ch = tree.next_sibling(ch)


@deprecated(deprecated_in="0.5.0", details="Use parse_in_arena() instead")
def parse(buf, **kwargs):
    return parse_in_arena(tree, id)
def parse_in_arena(buf, **kwargs):
    return _call_parse(parse_csubstr, buf, **kwargs)
def parse_in_place(buf, **kwargs):
    _check_valid_for_in_situ(buf)
    return _call_parse(parse_substr, buf, **kwargs)



def _call_parse(parse_fn, buf, **kwargs):
    tree = kwargs.get("tree", Tree())
    parse_fn(buf, tree)
    return tree


def _check_valid_for_in_situ(obj):
    if type(obj) in (str, bytes):
        raise TypeError("cannot parse in situ: " + type(obj).__name__)



@deprecated(deprecated_in="0.5.0", details="Use emit_yaml() instead")
def emit(tree, id=None):
    return emit_yaml(tree, id)
def emit_yaml(tree, id=None):
    if id is None:
        id = tree.root_id()
    return emit_yaml_malloc(tree, id)
def emit_json(tree, id=None):
    if id is None:
        id = tree.root_id()
    return emit_json_malloc(tree, id)


@deprecated(deprecated_in="0.5.0", details="Use compute_emit_yaml_length() instead")
def compute_emit_length(tree, id=None):
    return compute_emit_yaml_length(tree, id)
def compute_emit_yaml_length(tree, id=None):
    if id is None:
        id = tree.root_id()
    return emit_yaml_length(tree, id)
def compute_emit_json_length(tree, id=None):
    if id is None:
        id = tree.root_id()
    return emit_json_length(tree, id)


@deprecated(deprecated_in="0.5.0", details="Use emit_yaml_in_place() instead")
def emit_in_place(tree, buf, id=None):
    return emit_yaml_in_place(tree, buf, id)
def emit_yaml_in_place(tree, buf, id=None):
    return _emit_fn_in_place(tree, buf, id, emit_yaml_to_substr)
def emit_json_in_place(tree, buf, id=None):
    return _emit_fn_in_place(tree, buf, id, emit_json_to_substr)
def _emit_fn_in_place(tree, buf, id, fn):
    if id is None:
        id = tree.root_id()
    (failed, expected_size) = fn(tree, id, buf)
    if failed:
        raise IndexError("Output buffer has {} bytes, but emit requires {} bytes".format(
            len(buf), expected_size))
    return memoryview(buf)[:expected_size]

%}

//-----------------------------------------------------------------------------

namespace c4 {
namespace yml {

constexpr const size_t NONE = (size_t)-1;

typedef enum {
    NOTYPE  = 0,          ///< no type is set
    VAL     = (1<<0),     ///< a leaf node, has a (possibly empty) value
    KEY     = (1<<1),     ///< is member of a map, must have non-empty key
    MAP     = (1<<2),     ///< a map: a parent of keyvals
    SEQ     = (1<<3),     ///< a seq: a parent of vals
    DOC     = (1<<4),     ///< a document
    STREAM  = (1<<5)|SEQ, ///< a stream: a seq of docs
    KEYREF  = (1<<6),     ///< a *reference: the key references an &anchor
    VALREF  = (1<<7),     ///< a *reference: the val references an &anchor
    KEYANCH = (1<<8),     ///< the key has an &anchor
    VALANCH = (1<<9),     ///< the val has an &anchor
    KEYTAG  = (1<<10),    ///< the key has an explicit tag/type
    VALTAG  = (1<<11),    ///< the val has an explicit tag/type
} NodeType_e;


struct NodeType
{
    NodeType_e type;

    NodeType();
    NodeType(NodeType_e t);
    ~NodeType();

    const char *type_str();
    static const char* type_str(NodeType_e t);

    void set(NodeType_e t);
    void add(NodeType_e t);
    void rem(NodeType_e t);

    bool is_stream() const;
    bool is_doc() const;
    bool is_container() const;
    bool is_map() const;
    bool is_seq() const;
    bool has_val() const;
    bool has_key() const;
    bool is_val() const;
    bool is_keyval() const;
    bool has_key_tag() const;
    bool has_val_tag() const;
    bool has_key_anchor() const;
    bool has_val_anchor() const;
    bool has_anchor() const;
    bool is_key_ref() const;
    bool is_val_ref() const;
    bool is_ref() const;
    bool is_anchor_or_ref() const;
    bool is_key_quoted() const;
    bool is_val_quoted() const;
    bool is_quoted() const;
};


struct Tree
{
    Tree();
    ~Tree();

    void reserve(size_t node_capacity);
    void reserve_arena(size_t node_capacity);
    void clear();
    void clear_arena();

    size_t size() const;
    size_t capacity() const;
    size_t slack() const;

    size_t arena_size() const;
    size_t arena_capacity() const;
    size_t arena_slack() const;

    void resolve();

public:

    // getters

    NodeType_e  type(size_t node) const;
    const char* type_str(size_t node) const;

    c4::csubstr key       (size_t node) const;
    c4::csubstr key_tag   (size_t node) const;
    c4::csubstr key_ref   (size_t node) const;
    c4::csubstr key_anchor(size_t node) const;
    c4::yml::NodeScalar keysc(size_t node) const;

    c4::csubstr val       (size_t node) const;
    c4::csubstr val_tag   (size_t node) const;
    c4::csubstr val_ref   (size_t node) const;
    c4::csubstr val_anchor(size_t node) const;
    c4::yml::NodeScalar valsc(size_t node) const;

public:

    // node predicates

    bool is_root(size_t node) const;
    bool is_stream(size_t node) const;
    bool is_doc(size_t node) const;
    bool is_container(size_t node) const;
    bool is_map(size_t node) const;
    bool is_seq(size_t node) const;
    bool has_val(size_t node) const;
    bool has_key(size_t node) const;
    bool is_val(size_t node) const;
    bool is_keyval(size_t node) const;
    bool has_key_tag(size_t node) const;
    bool has_val_tag(size_t node) const;
    bool has_key_anchor(size_t node) const;
    bool has_val_anchor(size_t node) const;
    bool is_key_ref(size_t node) const;
    bool is_val_ref(size_t node) const;
    bool is_ref(size_t node) const;
    bool is_anchor_or_ref(size_t node) const;
    bool is_key_quoted(size_t node) const;
    bool is_val_quoted(size_t node) const;
    bool is_quoted(size_t node) const;
    bool is_anchor(size_t node) const;
    bool parent_is_seq(size_t node) const;
    bool parent_is_map(size_t node) const;
    bool empty(size_t node) const;
    bool has_anchor(size_t node, c4::csubstr a) const;

public:

    // hierarchy predicates

    bool has_parent(size_t node) const;
    bool has_child(size_t node, c4::csubstr key) const;
    //bool has_child(size_t node, size_t ch) const;
    bool has_children(size_t node) const;
    bool has_sibling(size_t node, c4::csubstr key) const;
    //bool has_sibling(size_t node, size_t sib) const;
    bool has_other_siblings(size_t node) const;

public:

    // hierarchy getters

    size_t root_id() const;

    size_t parent(size_t node) const;
    size_t prev_sibling(size_t node) const;
    size_t next_sibling(size_t node) const;
    size_t num_children(size_t node) const;
    size_t child_pos(size_t node, size_t ch) const;
    size_t first_child(size_t node) const;
    size_t last_child(size_t node) const;
    size_t child(size_t node, size_t pos) const;
    size_t find_child(size_t node, c4::csubstr key) const;
    size_t num_siblings(size_t node) const;
    size_t num_other_siblings(size_t node) const;
    size_t sibling_pos(size_t node, size_t sib) const;
    size_t first_sibling(size_t node) const;
    size_t last_sibling(size_t node) const;
    size_t sibling(size_t node, size_t pos) const;
    size_t find_sibling(size_t node, c4::csubstr key) const;

public:

    void to_keyval(size_t node, c4::csubstr key, c4::csubstr val, int more_flags=0);
    void to_map(size_t node, c4::csubstr key, int more_flags=0);
    void to_seq(size_t node, c4::csubstr key, int more_flags=0);
    void to_val(size_t node, c4::csubstr val, int more_flags=0);
    void to_stream(size_t node, int more_flags=0);
    void to_map(size_t node, int more_flags=0);
    void to_seq(size_t node, int more_flags=0);
    void to_doc(size_t node, int more_flags=0);

    void set_key_tag(size_t node, c4::csubstr tag);
    void set_key_anchor(size_t node, c4::csubstr anchor);
    void set_val_anchor(size_t node, c4::csubstr anchor);
    void set_key_ref   (size_t node, c4::csubstr ref   );
    void set_val_ref   (size_t node, c4::csubstr ref   );

    void _set_key(size_t node, c4::csubstr key, int more_flags=0);
    void _set_val(size_t node, c4::csubstr val, int more_flags=0);

    void set_val_tag(size_t node, c4::csubstr tag);
    void rem_key_anchor(size_t node);
    void rem_val_anchor(size_t node);
    void rem_key_ref   (size_t node);
    void rem_val_ref   (size_t node);
    void rem_anchor_ref(size_t node);

public:

    /** create and insert a new child of "parent". insert after the (to-be)
     * sibling "after", which must be a child of "parent". To insert as the
     * first child, set after to NONE */
    size_t insert_child(size_t parent, size_t after);
    size_t prepend_child(size_t parent);
    size_t  append_child(size_t parent);

public:

    //! create and insert a new sibling of n. insert after "after"
    size_t insert_sibling(size_t node, size_t after);
    size_t prepend_sibling(size_t node);
    size_t  append_sibling(size_t node);

public:

    //! remove an entire branch at once: ie remove the children and the node itself
    void remove(size_t node);

    //! remove all the node's children, but keep the node itself
    void remove_children(size_t node);

public:

    void reorder();

    /** change the node's position in the parent */
    void move(size_t node, size_t after);

    /** change the node's parent and position */
    void move(size_t node, size_t new_parent, size_t after);
    /** change the node's parent and position */
    size_t move(Tree * src, size_t node, size_t new_parent, size_t after);

    /** recursively duplicate the node */
    size_t duplicate(size_t node, size_t new_parent, size_t after);
    /** recursively duplicate a node from a different tree */
    size_t duplicate(Tree const* src, size_t node, size_t new_parent, size_t after);

    /** recursively duplicate the node's children (but not the node) */
    void duplicate_children(size_t node, size_t parent, size_t after);
    /** recursively duplicate the node's children (but not the node), where the node is from a different tree */
    void duplicate_children(Tree const* src, size_t node, size_t parent, size_t after);

    void duplicate_contents(size_t node, size_t where);

    /** duplicate the node's children (but not the node) in a new parent, but
     * omit repetitions where a duplicated node has the same key (in maps) or
     * value (in seqs). If one of the duplicated children has the same key
     * (in maps) or value (in seqs) as one of the parent's children, the one
     * that is placed closest to the end will prevail. */
    void duplicate_children_no_rep(size_t node, size_t parent, size_t after);

};

/*
%extend Tree {

    bool has_anchor(size_t node, const char *str, size_t len) const
    {
        return $self->has_anchor(node, c4::csubstr(str, len));
    }

    bool has_child(size_t node, const char *str, size_t len) const
    {
        return $self->has_child(node, c4::csubstr(str, len));
    }

    bool has_sibling(size_t node, const char *str, size_t len) const
    {
        return $self->has_sibling(node, c4::csubstr(str, len));
    }

    size_t find_child(size_t node, const char *str, size_t len) const
    {
        return $self->find_child(node, c4::csubstr(str, len));
    }

    size_t find_sibling(size_t node, const char *str, size_t len) const
    {
        return $self->find_sibling(node, c4::csubstr(str, len));
    }

    void to_keyval(size_t node, const char *keystr, size_t keylen, const char *valstr, size_t vallen, int more_flags=0)
    {
        return $self->to_keyval(node, c4::csubstr(keystr, keylen), c4::csubstr(valstr, vallen), more_flags);
    }

    void to_map(size_t node, const char *keystr, size_t keylen, int more_flags=0)
    {
        return $self->to_map(node, c4::csubstr(keystr, keylen), more_flags);
    }

    void to_seq(size_t node, const char *keystr, size_t keylen, int more_flags=0)
    {
        return $self->to_seq(node, c4::csubstr(keystr, keylen), more_flags);
    }

    void to_val(size_t node, const char *valstr, size_t vallen, int more_flags=0)
    {
        return $self->to_val(node, c4::csubstr(valstr, vallen), more_flags);
    }

    void set_key_tag(size_t node, const char *str, size_t len)
    {
        return $self->set_key_tag(node, c4::csubstr(str, len));
    }
    void set_val_tag(size_t node, const char *str, size_t len)
    {
        return $self->set_val_tag(node, c4::csubstr(str, len));
    }

    void set_key_anchor(size_t node, const char *str, size_t len)
    {
        return $self->set_key_anchor(node, c4::csubstr(str, len));
    }
    void set_val_anchor(size_t node, const char *str, size_t len)
    {
        return $self->set_val_anchor(node, c4::csubstr(str, len));
    }

    void set_key_ref(size_t node, const char *str, size_t len)
    {
        return $self->set_key_ref(node, c4::csubstr(str, len));
    }
    void set_val_ref(size_t node, const char *str, size_t len)
    {
        return $self->set_val_ref(node, c4::csubstr(str, len));
    }

};
*/

} // namespace yml
} // namespace c4

//-----------------------------------------------------------------------------