| 1 |
package parser |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/goccy/go-yaml/token" |
| 8 |
) |
| 9 |
|
| 10 |
// context context at parsing |
| 11 |
type context struct { |
| 12 |
parent *context |
| 13 |
idx int |
| 14 |
size int |
| 15 |
tokens token.Tokens |
| 16 |
path string |
| 17 |
} |
| 18 |
|
| 19 |
var pathSpecialChars = []string{ |
| 20 |
"$", "*", ".", "[", "]", |
| 21 |
} |
| 22 |
|
| 23 |
func containsPathSpecialChar(path string) bool { |
| 24 |
for _, char := range pathSpecialChars { |
| 25 |
if strings.Contains(path, char) { |
| 26 |
return true |
| 27 |
} |
| 28 |
} |
| 29 |
return false |
| 30 |
} |
| 31 |
|
| 32 |
func normalizePath(path string) string { |
| 33 |
if containsPathSpecialChar(path) { |
| 34 |
return fmt.Sprintf("'%s'", path) |
| 35 |
} |
| 36 |
return path |
| 37 |
} |
| 38 |
|
| 39 |
func (c *context) withChild(path string) *context { |
| 40 |
ctx := c.copy() |
| 41 |
path = normalizePath(path) |
| 42 |
ctx.path += fmt.Sprintf(".%s", path) |
| 43 |
return ctx |
| 44 |
} |
| 45 |
|
| 46 |
func (c *context) withIndex(idx uint) *context { |
| 47 |
ctx := c.copy() |
| 48 |
ctx.path += fmt.Sprintf("[%d]", idx) |
| 49 |
return ctx |
| 50 |
} |
| 51 |
|
| 52 |
func (c *context) copy() *context { |
| 53 |
return &context{ |
| 54 |
parent: c, |
| 55 |
idx: c.idx, |
| 56 |
size: c.size, |
| 57 |
tokens: append(token.Tokens{}, c.tokens...), |
| 58 |
path: c.path, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (c *context) next() bool { |
| 63 |
return c.idx < c.size |
| 64 |
} |
| 65 |
|
| 66 |
func (c *context) previousToken() *token.Token { |
| 67 |
if c.idx > 0 { |
| 68 |
return c.tokens[c.idx-1] |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
func (c *context) insertToken(idx int, tk *token.Token) { |
| 74 |
if c.parent != nil { |
| 75 |
c.parent.insertToken(idx, tk) |
| 76 |
} |
| 77 |
if c.size < idx { |
| 78 |
return |
| 79 |
} |
| 80 |
if c.size == idx { |
| 81 |
curToken := c.tokens[c.size-1] |
| 82 |
tk.Next = curToken |
| 83 |
curToken.Prev = tk |
| 84 |
|
| 85 |
c.tokens = append(c.tokens, tk) |
| 86 |
c.size = len(c.tokens) |
| 87 |
return |
| 88 |
} |
| 89 |
|
| 90 |
curToken := c.tokens[idx] |
| 91 |
tk.Next = curToken |
| 92 |
curToken.Prev = tk |
| 93 |
|
| 94 |
c.tokens = append(c.tokens[:idx+1], c.tokens[idx:]...) |
| 95 |
c.tokens[idx] = tk |
| 96 |
c.size = len(c.tokens) |
| 97 |
} |
| 98 |
|
| 99 |
func (c *context) currentToken() *token.Token { |
| 100 |
if c.idx >= c.size { |
| 101 |
return nil |
| 102 |
} |
| 103 |
return c.tokens[c.idx] |
| 104 |
} |
| 105 |
|
| 106 |
func (c *context) nextToken() *token.Token { |
| 107 |
if c.idx+1 >= c.size { |
| 108 |
return nil |
| 109 |
} |
| 110 |
return c.tokens[c.idx+1] |
| 111 |
} |
| 112 |
|
| 113 |
func (c *context) nextNotCommentToken() *token.Token { |
| 114 |
for i := c.idx + 1; i < c.size; i++ { |
| 115 |
tk := c.tokens[i] |
| 116 |
if tk.Type == token.CommentType { |
| 117 |
continue |
| 118 |
} |
| 119 |
return tk |
| 120 |
} |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
func (c *context) afterNextNotCommentToken() *token.Token { |
| 125 |
notCommentTokenCount := 0 |
| 126 |
for i := c.idx + 1; i < c.size; i++ { |
| 127 |
tk := c.tokens[i] |
| 128 |
if tk.Type == token.CommentType { |
| 129 |
continue |
| 130 |
} |
| 131 |
notCommentTokenCount++ |
| 132 |
if notCommentTokenCount == 2 { |
| 133 |
return tk |
| 134 |
} |
| 135 |
} |
| 136 |
return nil |
| 137 |
} |
| 138 |
|
| 139 |
func (c *context) isCurrentCommentToken() bool { |
| 140 |
tk := c.currentToken() |
| 141 |
if tk == nil { |
| 142 |
return false |
| 143 |
} |
| 144 |
return tk.Type == token.CommentType |
| 145 |
} |
| 146 |
|
| 147 |
func (c *context) progressIgnoreComment(num int) { |
| 148 |
if c.parent != nil { |
| 149 |
c.parent.progressIgnoreComment(num) |
| 150 |
} |
| 151 |
if c.size <= c.idx+num { |
| 152 |
c.idx = c.size |
| 153 |
} else { |
| 154 |
c.idx += num |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func (c *context) progress(num int) { |
| 159 |
if c.isCurrentCommentToken() { |
| 160 |
return |
| 161 |
} |
| 162 |
c.progressIgnoreComment(num) |
| 163 |
} |
| 164 |
|
| 165 |
func newContext(tokens token.Tokens, mode Mode) *context { |
| 166 |
filteredTokens := []*token.Token{} |
| 167 |
if mode&ParseComments != 0 { |
| 168 |
filteredTokens = tokens |
| 169 |
} else { |
| 170 |
for _, tk := range tokens { |
| 171 |
if tk.Type == token.CommentType { |
| 172 |
continue |
| 173 |
} |
| 174 |
// keep prev/next reference between tokens containing comments |
| 175 |
// https://github.com/goccy/go-yaml/issues/254 |
| 176 |
filteredTokens = append(filteredTokens, tk) |
| 177 |
} |
| 178 |
} |
| 179 |
return &context{ |
| 180 |
idx: 0, |
| 181 |
size: len(filteredTokens), |
| 182 |
tokens: token.Tokens(filteredTokens), |
| 183 |
path: "$", |
| 184 |
} |
| 185 |
} |