-
Notifications
You must be signed in to change notification settings - Fork 6
/
iter.go
62 lines (52 loc) · 1.22 KB
/
iter.go
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
package commonmark
/*
#include <stdlib.h>
#include "cmark.h"
*/
import "C"
import (
"runtime"
)
type CMarkEvent int
const (
CMARK_EVENT_NONE CMarkEvent = iota
CMARK_EVENT_DONE
CMARK_EVENT_ENTER
CMARK_EVENT_EXIT
)
//Wraps a cmark_iter
type CMarkIter struct {
iter *C.cmark_iter
}
//Creates a new iterator starting with the given node.
func NewCMarkIter(node *CMarkNode) *CMarkIter {
iter := &CMarkIter{
iter: C.cmark_iter_new(node.node),
}
runtime.SetFinalizer(iter, (*CMarkIter).Free)
return iter
}
//Returns the event type for the next node
func (iter *CMarkIter) Next() CMarkEvent {
ne := C.cmark_iter_next(iter.iter)
return CMarkEvent(ne)
}
//Returns the next node in the sequence
func (iter *CMarkIter) GetNode() *CMarkNode {
return &CMarkNode{
node: C.cmark_iter_get_node(iter.iter),
}
}
//Reset the iterator so the current node is 'current' and the
//event type is 'event'. Use this to resume after
//desctructively modifying the tree structure
func (iter *CMarkIter) Reset(current *CMarkNode, event CMarkEvent) {
C.cmark_iter_reset(iter.iter, current.node, C.cmark_event_type(event))
}
//Frees an iterator
func (iter *CMarkIter) Free() {
if iter.iter != nil {
C.cmark_iter_free(iter.iter)
}
iter.iter = nil
}