/
context.go
134 lines (118 loc) · 3.51 KB
/
context.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
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
package parser
import (
"sync"
"github.com/bytesparadise/libasciidoc/pkg/configuration"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
)
type ParseContext struct {
filename string
opts []Option
levelOffsets levelOffsets
attributes *contextAttributes
userMacros map[string]configuration.MacroTemplate
counters map[string]interface{}
}
func NewParseContext(config *configuration.Configuration, options ...Option) *ParseContext {
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("new parser context with attributes: %s", spew.Sdump(config.Attributes))
}
opts := []Option{
Entrypoint("DocumentFragment"),
GlobalStore(frontMatterKey, true),
GlobalStore(documentHeaderKey, true),
GlobalStore(usermacrosKey, config.Macros),
GlobalStore(enabledSubstitutionsKey, normalSubstitutions()),
}
opts = append(opts, options...)
return &ParseContext{
filename: config.Filename,
opts: opts,
levelOffsets: []*levelOffset{},
attributes: newContextAttributes(config.Attributes),
userMacros: config.Macros,
counters: map[string]interface{}{},
}
}
func (c *ParseContext) Clone() *ParseContext {
return &ParseContext{
filename: c.filename,
opts: options(c.opts).clone(),
levelOffsets: c.levelOffsets.clone(),
attributes: c.attributes.clone(),
userMacros: c.userMacros,
counters: c.counters,
}
}
type options []Option
func (o options) clone() []Option {
result := make([]Option, len(o))
copy(result, o)
return result
}
type contextAttributes struct {
immutableAttributes types.Attributes
attributes types.Attributes
mutex *sync.RWMutex
}
func newContextAttributes(attrs types.Attributes) *contextAttributes {
return &contextAttributes{
immutableAttributes: attrs,
attributes: types.Attributes{},
mutex: &sync.RWMutex{},
}
}
func (a *contextAttributes) clone() *contextAttributes {
a.mutex.Lock()
defer a.mutex.Unlock()
return &contextAttributes{
mutex: &sync.RWMutex{},
immutableAttributes: a.immutableAttributes.Clone(),
attributes: a.attributes.Clone(),
}
}
func (a *contextAttributes) allAttributes() map[string]interface{} {
result := make(map[string]interface{}, len(a.attributes)+len(a.immutableAttributes))
for k, v := range a.attributes {
result[k] = v
}
// imautables attributes should not be overridden, hence adding them after
for k, v := range a.immutableAttributes {
result[k] = v
}
return result
}
func (a *contextAttributes) get(k string) (interface{}, bool) {
if v, found := a.immutableAttributes[k]; found {
return v, true
}
v, found := a.attributes[k]
return v, found
}
func (a *contextAttributes) getAsIntWithDefault(k string, defaultValue int) int {
if a.immutableAttributes.Has(k) {
return a.immutableAttributes.GetAsIntWithDefault(k, defaultValue)
}
return a.attributes.GetAsIntWithDefault(k, defaultValue)
}
func (a *contextAttributes) set(k string, v interface{}) {
a.mutex.RLock() // TODO: needed? each go routine has its own context
defer a.mutex.RUnlock()
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("setting context attribute: %s -> %s", k, spew.Sdump(v))
}
a.attributes[k] = v
}
func (a *contextAttributes) unset(k string) {
a.mutex.RLock()
defer a.mutex.RUnlock()
delete(a.attributes, k)
}
func (a *contextAttributes) setAll(attrs map[string]interface{}) {
a.mutex.RLock()
defer a.mutex.RUnlock()
for k, v := range attrs {
a.attributes[k] = v
}
}