File: context.go

package info (click to toggle)
golang-github-goccy-go-yaml 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,968 kB
  • sloc: javascript: 505; makefile: 57
file content (187 lines) | stat: -rw-r--r-- 3,561 bytes parent folder | download
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
package parser

import (
	"fmt"
	"strings"

	"github.com/goccy/go-yaml/token"
)

// context context at parsing
type context struct {
	tokenRef *tokenRef
	path     string
	isFlow   bool
}

type tokenRef struct {
	tokens []*Token
	size   int
	idx    int
}

var pathSpecialChars = []string{
	"$", "*", ".", "[", "]",
}

func containsPathSpecialChar(path string) bool {
	for _, char := range pathSpecialChars {
		if strings.Contains(path, char) {
			return true
		}
	}
	return false
}

func normalizePath(path string) string {
	if containsPathSpecialChar(path) {
		return fmt.Sprintf("'%s'", path)
	}
	return path
}

func (c *context) currentToken() *Token {
	if c.tokenRef.idx >= c.tokenRef.size {
		return nil
	}
	return c.tokenRef.tokens[c.tokenRef.idx]
}

func (c *context) isComment() bool {
	return c.currentToken().Type() == token.CommentType
}

func (c *context) nextToken() *Token {
	if c.tokenRef.idx+1 >= c.tokenRef.size {
		return nil
	}
	return c.tokenRef.tokens[c.tokenRef.idx+1]
}

func (c *context) nextNotCommentToken() *Token {
	for i := c.tokenRef.idx + 1; i < c.tokenRef.size; i++ {
		tk := c.tokenRef.tokens[i]
		if tk.Type() == token.CommentType {
			continue
		}
		return tk
	}
	return nil
}

func (c *context) isTokenNotFound() bool {
	return c.currentToken() == nil
}

func (c *context) withGroup(g *TokenGroup) *context {
	ctx := *c
	ctx.tokenRef = &tokenRef{
		tokens: g.Tokens,
		size:   len(g.Tokens),
	}
	return &ctx
}

func (c *context) withChild(path string) *context {
	ctx := *c
	ctx.path = c.path + "." + normalizePath(path)
	return &ctx
}

func (c *context) withIndex(idx uint) *context {
	ctx := *c
	ctx.path = c.path + "[" + fmt.Sprint(idx) + "]"
	return &ctx
}

func (c *context) withFlow(isFlow bool) *context {
	ctx := *c
	ctx.isFlow = isFlow
	return &ctx
}

func newContext() *context {
	return &context{
		path: "$",
	}
}

func (c *context) goNext() {
	ref := c.tokenRef
	if ref.size <= ref.idx+1 {
		ref.idx = ref.size
	} else {
		ref.idx++
	}
}

func (c *context) next() bool {
	return c.tokenRef.idx < c.tokenRef.size
}

func (c *context) insertNullToken(tk *Token) *Token {
	nullToken := c.createImplicitNullToken(tk)
	c.insertToken(nullToken)
	c.goNext()

	return nullToken
}

func (c *context) addNullValueToken(tk *Token) *Token {
	nullToken := c.createImplicitNullToken(tk)
	rawTk := nullToken.RawToken()

	// add space for map or sequence value.
	rawTk.Position.Column++

	c.addToken(nullToken)
	c.goNext()

	return nullToken
}

func (c *context) createImplicitNullToken(base *Token) *Token {
	pos := *(base.RawToken().Position)
	pos.Column++
	tk := token.New("null", " null", &pos)
	tk.Type = token.ImplicitNullType
	return &Token{Token: tk}
}

func (c *context) insertToken(tk *Token) {
	ref := c.tokenRef
	idx := ref.idx
	if ref.size < idx {
		return
	}
	if ref.size == idx {
		curToken := ref.tokens[ref.size-1]
		tk.RawToken().Next = curToken.RawToken()
		curToken.RawToken().Prev = tk.RawToken()

		ref.tokens = append(ref.tokens, tk)
		ref.size = len(ref.tokens)
		return
	}

	curToken := ref.tokens[idx]
	tk.RawToken().Next = curToken.RawToken()
	curToken.RawToken().Prev = tk.RawToken()

	ref.tokens = append(ref.tokens[:idx+1], ref.tokens[idx:]...)
	ref.tokens[idx] = tk
	ref.size = len(ref.tokens)
}

func (c *context) addToken(tk *Token) {
	ref := c.tokenRef
	lastTk := ref.tokens[ref.size-1]
	if lastTk.Group != nil {
		lastTk = lastTk.Group.Last()
	}
	lastTk.RawToken().Next = tk.RawToken()
	tk.RawToken().Prev = lastTk.RawToken()

	ref.tokens = append(ref.tokens, tk)
	ref.size = len(ref.tokens)
}