This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
indent_queue.go
85 lines (71 loc) · 1.77 KB
/
indent_queue.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package parser
import (
"fmt"
doc "github.com/demizer/go-rst/pkg/document"
tok "github.com/demizer/go-rst/pkg/token"
)
type indent struct {
token *tok.Item
node doc.Node
}
// indentQueue is a LIFO stack
type indentQueue []*indent
func (i *indentQueue) len() int { return len(*i) }
func (i *indentQueue) add(t *tok.Item, n doc.Node) doc.Node {
ni := &indent{t, n}
*i = append(*i, ni)
return n
}
func (i *indentQueue) pop() { *i = (*i)[:len(*i)-1] }
func (i *indentQueue) lastStartPosition() (int, error) {
var sp int
var n doc.Node
var nl *doc.NodeList
// Get the NodeList of the indent item
switch nt := (*i)[len(*i)-1].node.(type) {
case *doc.BulletListItemNode:
nl = &nt.NodeList
if len(*nl) == 0 {
return sp, nil
}
default:
return sp, fmt.Errorf("Unhandled type = %T", nt)
}
// Get node that isn't a comment
for i := len(*nl) - 1; i >= 0; i-- {
n = (*nl)[i]
if n.NodeType() != doc.NodeComment {
break
}
}
// Return the StartPosition of the last NodeList item
switch nt := n.(type) {
case *doc.ParagraphNode:
if len(nt.NodeList) == 0 {
return sp, nil
}
// Get the first start position in the paragraph node list
switch nt2 := nt.NodeList[0].(type) {
case *doc.TextNode:
sp = nt2.StartPosition
default:
return sp, fmt.Errorf("Unhandled sub Node type = %T", nt)
}
default:
return sp, fmt.Errorf("Unhandled child NodeList type = %T", n)
}
return sp, nil
}
func (i *indentQueue) topNodeList() (*doc.NodeList, error) {
l := (*i)[len(*i)-1]
switch n := l.node.(type) {
case *doc.BulletListItemNode:
return &n.NodeList, nil
// case *ParagraphNode:
// return &n.NodeList
default:
return nil, fmt.Errorf("Unhandled type = %T", n)
}
return nil, nil
}
func (i *indentQueue) topNode() doc.Node { return (*i)[len(*i)-1].node }