| 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 | tokenRef *tokenRef |
| 13 | path string |
| 14 | isFlow bool |
| 15 | } |
| 16 | |
| 17 | type tokenRef struct { |
| 18 | tokens []*Token |
| 19 | size int |
| 20 | idx int |
| 21 | } |
| 22 | |
| 23 | var pathSpecialChars = []string{ |
| 24 | "$", "*", ".", "[", "]", |
| 25 | } |
| 26 | |
| 27 | func containsPathSpecialChar(path string) bool { |
| 28 | for _, char := range pathSpecialChars { |
| 29 | if strings.Contains(path, char) { |
| 30 | return true |
| 31 | } |
| 32 | } |
| 33 | return false |
| 34 | } |
| 35 | |
| 36 | func normalizePath(path string) string { |
| 37 | if containsPathSpecialChar(path) { |
| 38 | return fmt.Sprintf("'%s'", path) |
| 39 | } |
| 40 | return path |
| 41 | } |
| 42 | |
| 43 | func (c *context) currentToken() *Token { |
| 44 | if c.tokenRef.idx >= c.tokenRef.size { |
| 45 | return nil |
| 46 | } |
| 47 | return c.tokenRef.tokens[c.tokenRef.idx] |
| 48 | } |
| 49 | |
| 50 | func (c *context) isComment() bool { |
| 51 | return c.currentToken().Type() == token.CommentType |
| 52 | } |
| 53 | |
| 54 | func (c *context) nextToken() *Token { |
| 55 | if c.tokenRef.idx+1 >= c.tokenRef.size { |
| 56 | return nil |
| 57 | } |
| 58 | return c.tokenRef.tokens[c.tokenRef.idx+1] |
| 59 | } |
| 60 | |
| 61 | func (c *context) nextNotCommentToken() *Token { |
| 62 | for i := c.tokenRef.idx + 1; i < c.tokenRef.size; i++ { |
| 63 | tk := c.tokenRef.tokens[i] |
| 64 | if tk.Type() == token.CommentType { |
| 65 | continue |
| 66 | } |
| 67 | return tk |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func (c *context) isTokenNotFound() bool { |
| 73 | return c.currentToken() == nil |
| 74 | } |
| 75 | |
| 76 | func (c *context) withGroup(g *TokenGroup) *context { |
| 77 | ctx := *c |
| 78 | ctx.tokenRef = &tokenRef{ |
| 79 | tokens: g.Tokens, |
| 80 | size: len(g.Tokens), |
| 81 | } |
| 82 | return &ctx |
| 83 | } |
| 84 | |
| 85 | func (c *context) withChild(path string) *context { |
| 86 | ctx := *c |
| 87 | ctx.path = c.path + "." + normalizePath(path) |
| 88 | return &ctx |
| 89 | } |
| 90 | |
| 91 | func (c *context) withIndex(idx uint) *context { |
| 92 | ctx := *c |
| 93 | ctx.path = c.path + "[" + fmt.Sprint(idx) + "]" |
| 94 | return &ctx |
| 95 | } |
| 96 | |
| 97 | func (c *context) withFlow(isFlow bool) *context { |
| 98 | ctx := *c |
| 99 | ctx.isFlow = isFlow |
| 100 | return &ctx |
| 101 | } |
| 102 | |
| 103 | func newContext() *context { |
| 104 | return &context{ |
| 105 | path: "$", |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func (c *context) goNext() { |
| 110 | ref := c.tokenRef |
| 111 | if ref.size <= ref.idx+1 { |
| 112 | ref.idx = ref.size |
| 113 | } else { |
| 114 | ref.idx++ |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func (c *context) next() bool { |
| 119 | return c.tokenRef.idx < c.tokenRef.size |
| 120 | } |
| 121 | |
| 122 | func (c *context) insertNullToken(tk *Token) *Token { |
| 123 | nullToken := c.createImplicitNullToken(tk) |
| 124 | c.insertToken(nullToken) |
| 125 | c.goNext() |
| 126 | |
| 127 | return nullToken |
| 128 | } |
| 129 | |
| 130 | func (c *context) addNullValueToken(tk *Token) *Token { |
| 131 | nullToken := c.createImplicitNullToken(tk) |
| 132 | rawTk := nullToken.RawToken() |
| 133 | |
| 134 | // add space for map or sequence value. |
| 135 | rawTk.Position.Column++ |
| 136 | |
| 137 | c.addToken(nullToken) |
| 138 | c.goNext() |
| 139 | |
| 140 | return nullToken |
| 141 | } |
| 142 | |
| 143 | func (c *context) createImplicitNullToken(base *Token) *Token { |
| 144 | pos := *(base.RawToken().Position) |
| 145 | pos.Column++ |
| 146 | tk := token.New("null", " null", &pos) |
| 147 | tk.Type = token.ImplicitNullType |
| 148 | return &Token{Token: tk} |
| 149 | } |
| 150 | |
| 151 | func (c *context) insertToken(tk *Token) { |
| 152 | ref := c.tokenRef |
| 153 | idx := ref.idx |
| 154 | if ref.size < idx { |
| 155 | return |
| 156 | } |
| 157 | if ref.size == idx { |
| 158 | curToken := ref.tokens[ref.size-1] |
| 159 | tk.RawToken().Next = curToken.RawToken() |
| 160 | curToken.RawToken().Prev = tk.RawToken() |
| 161 | |
| 162 | ref.tokens = append(ref.tokens, tk) |
| 163 | ref.size = len(ref.tokens) |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | curToken := ref.tokens[idx] |
| 168 | tk.RawToken().Next = curToken.RawToken() |
| 169 | curToken.RawToken().Prev = tk.RawToken() |
| 170 | |
| 171 | ref.tokens = append(ref.tokens[:idx+1], ref.tokens[idx:]...) |
| 172 | ref.tokens[idx] = tk |
| 173 | ref.size = len(ref.tokens) |
| 174 | } |
| 175 | |
| 176 | func (c *context) addToken(tk *Token) { |
| 177 | ref := c.tokenRef |
| 178 | lastTk := ref.tokens[ref.size-1] |
| 179 | if lastTk.Group != nil { |
| 180 | lastTk = lastTk.Group.Last() |
| 181 | } |
| 182 | lastTk.RawToken().Next = tk.RawToken() |
| 183 | tk.RawToken().Prev = lastTk.RawToken() |
| 184 | |
| 185 | ref.tokens = append(ref.tokens, tk) |
| 186 | ref.size = len(ref.tokens) |
| 187 | } |