| 1 | package scanner |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/goccy/go-yaml/token" |
| 10 | ) |
| 11 | |
| 12 | // Context context at scanning |
| 13 | type Context struct { |
| 14 | idx int |
| 15 | size int |
| 16 | notSpaceCharPos int |
| 17 | notSpaceOrgCharPos int |
| 18 | src []rune |
| 19 | buf []rune |
| 20 | obuf []rune |
| 21 | tokens token.Tokens |
| 22 | mstate *MultiLineState |
| 23 | } |
| 24 | |
| 25 | type MultiLineState struct { |
| 26 | opt string |
| 27 | firstLineIndentColumn int |
| 28 | prevLineIndentColumn int |
| 29 | lineIndentColumn int |
| 30 | lastNotSpaceOnlyLineIndentColumn int |
| 31 | spaceOnlyIndentColumn int |
| 32 | foldedNewLine bool |
| 33 | isRawFolded bool |
| 34 | isLiteral bool |
| 35 | isFolded bool |
| 36 | } |
| 37 | |
| 38 | var ( |
| 39 | ctxPool = sync.Pool{ |
| 40 | New: func() interface{} { |
| 41 | return createContext() |
| 42 | }, |
| 43 | } |
| 44 | ) |
| 45 | |
| 46 | func createContext() *Context { |
| 47 | return &Context{ |
| 48 | idx: 0, |
| 49 | tokens: token.Tokens{}, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func newContext(src []rune) *Context { |
| 54 | ctx, _ := ctxPool.Get().(*Context) |
| 55 | ctx.reset(src) |
| 56 | return ctx |
| 57 | } |
| 58 | |
| 59 | func (c *Context) release() { |
| 60 | ctxPool.Put(c) |
| 61 | } |
| 62 | |
| 63 | func (c *Context) clear() { |
| 64 | c.resetBuffer() |
| 65 | c.mstate = nil |
| 66 | } |
| 67 | |
| 68 | func (c *Context) reset(src []rune) { |
| 69 | c.idx = 0 |
| 70 | c.size = len(src) |
| 71 | c.src = src |
| 72 | c.tokens = c.tokens[:0] |
| 73 | c.resetBuffer() |
| 74 | c.mstate = nil |
| 75 | } |
| 76 | |
| 77 | func (c *Context) resetBuffer() { |
| 78 | c.buf = c.buf[:0] |
| 79 | c.obuf = c.obuf[:0] |
| 80 | c.notSpaceCharPos = 0 |
| 81 | c.notSpaceOrgCharPos = 0 |
| 82 | } |
| 83 | |
| 84 | func (c *Context) breakMultiLine() { |
| 85 | c.mstate = nil |
| 86 | } |
| 87 | |
| 88 | func (c *Context) getMultiLineState() *MultiLineState { |
| 89 | return c.mstate |
| 90 | } |
| 91 | |
| 92 | func (c *Context) setLiteral(lastDelimColumn int, opt string) { |
| 93 | mstate := &MultiLineState{ |
| 94 | isLiteral: true, |
| 95 | opt: opt, |
| 96 | } |
| 97 | indent := firstLineIndentColumnByOpt(opt) |
| 98 | if indent > 0 { |
| 99 | mstate.firstLineIndentColumn = lastDelimColumn + indent |
| 100 | } |
| 101 | c.mstate = mstate |
| 102 | } |
| 103 | |
| 104 | func (c *Context) setFolded(lastDelimColumn int, opt string) { |
| 105 | mstate := &MultiLineState{ |
| 106 | isFolded: true, |
| 107 | opt: opt, |
| 108 | } |
| 109 | indent := firstLineIndentColumnByOpt(opt) |
| 110 | if indent > 0 { |
| 111 | mstate.firstLineIndentColumn = lastDelimColumn + indent |
| 112 | } |
| 113 | c.mstate = mstate |
| 114 | } |
| 115 | |
| 116 | func (c *Context) setRawFolded(column int) { |
| 117 | mstate := &MultiLineState{ |
| 118 | isRawFolded: true, |
| 119 | } |
| 120 | mstate.updateIndentColumn(column) |
| 121 | c.mstate = mstate |
| 122 | } |
| 123 | |
| 124 | func firstLineIndentColumnByOpt(opt string) int { |
| 125 | opt = strings.TrimPrefix(opt, "-") |
| 126 | opt = strings.TrimPrefix(opt, "+") |
| 127 | opt = strings.TrimSuffix(opt, "-") |
| 128 | opt = strings.TrimSuffix(opt, "+") |
| 129 | i, _ := strconv.ParseInt(opt, 10, 64) |
| 130 | return int(i) |
| 131 | } |
| 132 | |
| 133 | func (s *MultiLineState) lastDelimColumn() int { |
| 134 | if s.firstLineIndentColumn == 0 { |
| 135 | return 0 |
| 136 | } |
| 137 | return s.firstLineIndentColumn - 1 |
| 138 | } |
| 139 | |
| 140 | func (s *MultiLineState) updateIndentColumn(column int) { |
| 141 | if s.firstLineIndentColumn == 0 { |
| 142 | s.firstLineIndentColumn = column |
| 143 | } |
| 144 | if s.lineIndentColumn == 0 { |
| 145 | s.lineIndentColumn = column |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func (s *MultiLineState) updateSpaceOnlyIndentColumn(column int) { |
| 150 | if s.firstLineIndentColumn != 0 { |
| 151 | return |
| 152 | } |
| 153 | s.spaceOnlyIndentColumn = column |
| 154 | } |
| 155 | |
| 156 | func (s *MultiLineState) validateIndentAfterSpaceOnly(column int) error { |
| 157 | if s.firstLineIndentColumn != 0 { |
| 158 | return nil |
| 159 | } |
| 160 | if s.spaceOnlyIndentColumn > column { |
| 161 | return errors.New("invalid number of indent is specified after space only") |
| 162 | } |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func (s *MultiLineState) validateIndentColumn() error { |
| 167 | if firstLineIndentColumnByOpt(s.opt) == 0 { |
| 168 | return nil |
| 169 | } |
| 170 | if s.firstLineIndentColumn > s.lineIndentColumn { |
| 171 | return errors.New("invalid number of indent is specified in the multi-line header") |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | func (s *MultiLineState) updateNewLineState() { |
| 177 | s.prevLineIndentColumn = s.lineIndentColumn |
| 178 | if s.lineIndentColumn != 0 { |
| 179 | s.lastNotSpaceOnlyLineIndentColumn = s.lineIndentColumn |
| 180 | } |
| 181 | s.foldedNewLine = true |
| 182 | s.lineIndentColumn = 0 |
| 183 | } |
| 184 | |
| 185 | func (s *MultiLineState) isIndentColumn(column int) bool { |
| 186 | if s.firstLineIndentColumn == 0 { |
| 187 | return column == 1 |
| 188 | } |
| 189 | return s.firstLineIndentColumn > column |
| 190 | } |
| 191 | |
| 192 | func (s *MultiLineState) addIndent(ctx *Context, column int) { |
| 193 | if s.firstLineIndentColumn == 0 { |
| 194 | return |
| 195 | } |
| 196 | |
| 197 | // If the first line of the document has already been evaluated, the number is treated as the threshold, since the `firstLineIndentColumn` is a positive number. |
| 198 | if column < s.firstLineIndentColumn { |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | // `c.foldedNewLine` is a variable that is set to true for every newline. |
| 203 | if !s.isLiteral && s.foldedNewLine { |
| 204 | s.foldedNewLine = false |
| 205 | } |
| 206 | // Since addBuf ignore space character, add to the buffer directly. |
| 207 | ctx.buf = append(ctx.buf, ' ') |
| 208 | ctx.notSpaceCharPos = len(ctx.buf) |
| 209 | } |
| 210 | |
| 211 | // updateNewLineInFolded if Folded or RawFolded context and the content on the current line starts at the same column as the previous line, |
| 212 | // treat the new-line-char as a space. |
| 213 | func (s *MultiLineState) updateNewLineInFolded(ctx *Context, column int) { |
| 214 | if s.isLiteral { |
| 215 | return |
| 216 | } |
| 217 | |
| 218 | // Folded or RawFolded. |
| 219 | |
| 220 | if !s.foldedNewLine { |
| 221 | return |
| 222 | } |
| 223 | var ( |
| 224 | lastChar rune |
| 225 | prevLastChar rune |
| 226 | ) |
| 227 | if len(ctx.buf) != 0 { |
| 228 | lastChar = ctx.buf[len(ctx.buf)-1] |
| 229 | } |
| 230 | if len(ctx.buf) > 1 { |
| 231 | prevLastChar = ctx.buf[len(ctx.buf)-2] |
| 232 | } |
| 233 | if s.lineIndentColumn == s.prevLineIndentColumn { |
| 234 | // --- |
| 235 | // > |
| 236 | // a |
| 237 | // b |
| 238 | if lastChar == '\n' { |
| 239 | ctx.buf[len(ctx.buf)-1] = ' ' |
| 240 | } |
| 241 | } else if s.prevLineIndentColumn == 0 && s.lastNotSpaceOnlyLineIndentColumn == column { |
| 242 | // if previous line is indent-space and new-line-char only, prevLineIndentColumn is zero. |
| 243 | // In this case, last new-line-char is removed. |
| 244 | // --- |
| 245 | // > |
| 246 | // a |
| 247 | // |
| 248 | // b |
| 249 | if lastChar == '\n' && prevLastChar == '\n' { |
| 250 | ctx.buf = ctx.buf[:len(ctx.buf)-1] |
| 251 | ctx.notSpaceCharPos = len(ctx.buf) |
| 252 | } |
| 253 | } |
| 254 | s.foldedNewLine = false |
| 255 | } |
| 256 | |
| 257 | func (s *MultiLineState) hasTrimAllEndNewlineOpt() bool { |
| 258 | return strings.HasPrefix(s.opt, "-") || strings.HasSuffix(s.opt, "-") || s.isRawFolded |
| 259 | } |
| 260 | |
| 261 | func (s *MultiLineState) hasKeepAllEndNewlineOpt() bool { |
| 262 | return strings.HasPrefix(s.opt, "+") || strings.HasSuffix(s.opt, "+") |
| 263 | } |
| 264 | |
| 265 | func (c *Context) addToken(tk *token.Token) { |
| 266 | if tk == nil { |
| 267 | return |
| 268 | } |
| 269 | c.tokens = append(c.tokens, tk) |
| 270 | } |
| 271 | |
| 272 | func (c *Context) addBuf(r rune) { |
| 273 | if len(c.buf) == 0 && (r == ' ' || r == '\t') { |
| 274 | return |
| 275 | } |
| 276 | c.buf = append(c.buf, r) |
| 277 | if r != ' ' && r != '\t' { |
| 278 | c.notSpaceCharPos = len(c.buf) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func (c *Context) addBufWithTab(r rune) { |
| 283 | if len(c.buf) == 0 && r == ' ' { |
| 284 | return |
| 285 | } |
| 286 | c.buf = append(c.buf, r) |
| 287 | if r != ' ' { |
| 288 | c.notSpaceCharPos = len(c.buf) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | func (c *Context) addOriginBuf(r rune) { |
| 293 | c.obuf = append(c.obuf, r) |
| 294 | if r != ' ' && r != '\t' { |
| 295 | c.notSpaceOrgCharPos = len(c.obuf) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func (c *Context) removeRightSpaceFromBuf() { |
| 300 | trimmedBuf := c.obuf[:c.notSpaceOrgCharPos] |
| 301 | buflen := len(trimmedBuf) |
| 302 | diff := len(c.obuf) - buflen |
| 303 | if diff > 0 { |
| 304 | c.obuf = c.obuf[:buflen] |
| 305 | c.buf = c.bufferedSrc() |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func (c *Context) isEOS() bool { |
| 310 | return len(c.src)-1 <= c.idx |
| 311 | } |
| 312 | |
| 313 | func (c *Context) isNextEOS() bool { |
| 314 | return len(c.src) <= c.idx+1 |
| 315 | } |
| 316 | |
| 317 | func (c *Context) next() bool { |
| 318 | return c.idx < c.size |
| 319 | } |
| 320 | |
| 321 | func (c *Context) source(s, e int) string { |
| 322 | return string(c.src[s:e]) |
| 323 | } |
| 324 | |
| 325 | func (c *Context) previousChar() rune { |
| 326 | if c.idx > 0 { |
| 327 | return c.src[c.idx-1] |
| 328 | } |
| 329 | return rune(0) |
| 330 | } |
| 331 | |
| 332 | func (c *Context) currentChar() rune { |
| 333 | if c.size > c.idx { |
| 334 | return c.src[c.idx] |
| 335 | } |
| 336 | return rune(0) |
| 337 | } |
| 338 | |
| 339 | func (c *Context) nextChar() rune { |
| 340 | if c.size > c.idx+1 { |
| 341 | return c.src[c.idx+1] |
| 342 | } |
| 343 | return rune(0) |
| 344 | } |
| 345 | |
| 346 | func (c *Context) repeatNum(r rune) int { |
| 347 | cnt := 0 |
| 348 | for i := c.idx; i < c.size; i++ { |
| 349 | if c.src[i] == r { |
| 350 | cnt++ |
| 351 | } else { |
| 352 | break |
| 353 | } |
| 354 | } |
| 355 | return cnt |
| 356 | } |
| 357 | |
| 358 | func (c *Context) progress(num int) { |
| 359 | c.idx += num |
| 360 | } |
| 361 | |
| 362 | func (c *Context) existsBuffer() bool { |
| 363 | return len(c.bufferedSrc()) != 0 |
| 364 | } |
| 365 | |
| 366 | func (c *Context) isMultiLine() bool { |
| 367 | return c.mstate != nil |
| 368 | } |
| 369 | |
| 370 | func (c *Context) bufferedSrc() []rune { |
| 371 | src := c.buf[:c.notSpaceCharPos] |
| 372 | if c.isMultiLine() { |
| 373 | mstate := c.getMultiLineState() |
| 374 | // remove end '\n' character and trailing empty lines. |
| 375 | // https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator |
| 376 | if mstate.hasTrimAllEndNewlineOpt() { |
| 377 | // If the '-' flag is specified, all trailing newline characters will be removed. |
| 378 | src = []rune(strings.TrimRight(string(src), "\n")) |
| 379 | } else if !mstate.hasKeepAllEndNewlineOpt() { |
| 380 | // Normally, all but one of the trailing newline characters are removed. |
| 381 | var newLineCharCount int |
| 382 | for i := len(src) - 1; i >= 0; i-- { |
| 383 | if src[i] == '\n' { |
| 384 | newLineCharCount++ |
| 385 | continue |
| 386 | } |
| 387 | break |
| 388 | } |
| 389 | removedNewLineCharCount := newLineCharCount - 1 |
| 390 | for removedNewLineCharCount > 0 { |
| 391 | src = []rune(strings.TrimSuffix(string(src), "\n")) |
| 392 | removedNewLineCharCount-- |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // If the text ends with a space character, remove all of them. |
| 397 | if mstate.hasTrimAllEndNewlineOpt() { |
| 398 | src = []rune(strings.TrimRight(string(src), " ")) |
| 399 | } |
| 400 | if string(src) == "\n" { |
| 401 | // If the content consists only of a newline, |
| 402 | // it can be considered as the document ending without any specified value, |
| 403 | // so it is treated as an empty string. |
| 404 | src = []rune{} |
| 405 | } |
| 406 | if mstate.hasKeepAllEndNewlineOpt() && len(src) == 0 { |
| 407 | src = []rune{'\n'} |
| 408 | } |
| 409 | } |
| 410 | return src |
| 411 | } |
| 412 | |
| 413 | func (c *Context) bufferedToken(pos *token.Position) *token.Token { |
| 414 | if c.idx == 0 { |
| 415 | return nil |
| 416 | } |
| 417 | source := c.bufferedSrc() |
| 418 | if len(source) == 0 { |
| 419 | c.buf = c.buf[:0] // clear value's buffer only. |
| 420 | return nil |
| 421 | } |
| 422 | var tk *token.Token |
| 423 | if c.isMultiLine() { |
| 424 | tk = token.String(string(source), string(c.obuf), pos) |
| 425 | } else { |
| 426 | tk = token.New(string(source), string(c.obuf), pos) |
| 427 | } |
| 428 | c.setTokenTypeByPrevTag(tk) |
| 429 | c.resetBuffer() |
| 430 | return tk |
| 431 | } |
| 432 | |
| 433 | func (c *Context) setTokenTypeByPrevTag(tk *token.Token) { |
| 434 | lastTk := c.lastToken() |
| 435 | if lastTk == nil { |
| 436 | return |
| 437 | } |
| 438 | if lastTk.Type != token.TagType { |
| 439 | return |
| 440 | } |
| 441 | tag := token.ReservedTagKeyword(lastTk.Value) |
| 442 | if _, exists := token.ReservedTagKeywordMap[tag]; !exists { |
| 443 | tk.Type = token.StringType |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | func (c *Context) lastToken() *token.Token { |
| 448 | if len(c.tokens) != 0 { |
| 449 | return c.tokens[len(c.tokens)-1] |
| 450 | } |
| 451 | return nil |
| 452 | } |