forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v3/scannerc.go

5396 lines
88 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
// Copyright (c) 2006-2010 Kirill Simonov
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Permission is hereby granted, free of charge, to any person obtaining a copy of
2024-02-18 10:42:21 +00:00
// this software and associated documentation files (the "Software"), to deal in
2024-02-18 10:42:21 +00:00
// the Software without restriction, including without limitation the rights to
2024-02-18 10:42:21 +00:00
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
2024-02-18 10:42:21 +00:00
// of the Software, and to permit persons to whom the Software is furnished to do
2024-02-18 10:42:21 +00:00
// so, subject to the following conditions:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The above copyright notice and this permission notice shall be included in all
2024-02-18 10:42:21 +00:00
// copies or substantial portions of the Software.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2024-02-18 10:42:21 +00:00
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2024-02-18 10:42:21 +00:00
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2024-02-18 10:42:21 +00:00
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2024-02-18 10:42:21 +00:00
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2024-02-18 10:42:21 +00:00
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2024-02-18 10:42:21 +00:00
// SOFTWARE.
package yaml
import (
"bytes"
"fmt"
)
// Introduction
2024-02-18 10:42:21 +00:00
// ************
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following notes assume that you are familiar with the YAML specification
2024-02-18 10:42:21 +00:00
// (http://yaml.org/spec/1.2/spec.html). We mostly follow it, although in
2024-02-18 10:42:21 +00:00
// some cases we are less restrictive that it requires.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The process of transforming a YAML stream into a sequence of events is
2024-02-18 10:42:21 +00:00
// divided on two steps: Scanning and Parsing.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The Scanner transforms the input stream into a sequence of tokens, while the
2024-02-18 10:42:21 +00:00
// parser transform the sequence of tokens produced by the Scanner into a
2024-02-18 10:42:21 +00:00
// sequence of parsing events.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The Scanner is rather clever and complicated. The Parser, on the contrary,
2024-02-18 10:42:21 +00:00
// is a straightforward implementation of a recursive-descendant parser (or,
2024-02-18 10:42:21 +00:00
// LL(1) parser, as it is usually called).
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Actually there are two issues of Scanning that might be called "clever", the
2024-02-18 10:42:21 +00:00
// rest is quite straightforward. The issues are "block collection start" and
2024-02-18 10:42:21 +00:00
// "simple keys". Both issues are explained below in details.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Here the Scanning step is explained and implemented. We start with the list
2024-02-18 10:42:21 +00:00
// of all the tokens produced by the Scanner together with short descriptions.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Now, tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(encoding) # The stream start.
2024-02-18 10:42:21 +00:00
// STREAM-END # The stream end.
2024-02-18 10:42:21 +00:00
// VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
2024-02-18 10:42:21 +00:00
// TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
2024-02-18 10:42:21 +00:00
// DOCUMENT-START # '---'
2024-02-18 10:42:21 +00:00
// DOCUMENT-END # '...'
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START # Indentation increase denoting a block
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START # sequence or a block mapping.
2024-02-18 10:42:21 +00:00
// BLOCK-END # Indentation decrease.
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-START # '['
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-END # ']'
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START # '{'
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-END # '}'
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY # '-'
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY # ','
2024-02-18 10:42:21 +00:00
// KEY # '?' or nothing (simple keys).
2024-02-18 10:42:21 +00:00
// VALUE # ':'
2024-02-18 10:42:21 +00:00
// ALIAS(anchor) # '*anchor'
2024-02-18 10:42:21 +00:00
// ANCHOR(anchor) # '&anchor'
2024-02-18 10:42:21 +00:00
// TAG(handle,suffix) # '!handle!suffix'
2024-02-18 10:42:21 +00:00
// SCALAR(value,style) # A scalar.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following two tokens are "virtual" tokens denoting the beginning and the
2024-02-18 10:42:21 +00:00
// end of the stream:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(encoding)
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// We pass the information about the input stream encoding with the
2024-02-18 10:42:21 +00:00
// STREAM-START token.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The next two tokens are responsible for tags:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// VERSION-DIRECTIVE(major,minor)
2024-02-18 10:42:21 +00:00
// TAG-DIRECTIVE(handle,prefix)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Example:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// %YAML 1.1
2024-02-18 10:42:21 +00:00
// %TAG ! !foo
2024-02-18 10:42:21 +00:00
// %TAG !yaml! tag:yaml.org,2002:
2024-02-18 10:42:21 +00:00
// ---
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The correspoding sequence of tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// VERSION-DIRECTIVE(1,1)
2024-02-18 10:42:21 +00:00
// TAG-DIRECTIVE("!","!foo")
2024-02-18 10:42:21 +00:00
// TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
2024-02-18 10:42:21 +00:00
// line.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The document start and end indicators are represented by:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// DOCUMENT-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that if a YAML stream contains an implicit document (without '---'
2024-02-18 10:42:21 +00:00
// and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
2024-02-18 10:42:21 +00:00
// produced.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// In the following examples, we present whole documents together with the
2024-02-18 10:42:21 +00:00
// produced tokens.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. An implicit document:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 'a scalar'
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// SCALAR("a scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 2. An explicit document:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// ---
2024-02-18 10:42:21 +00:00
// 'a scalar'
2024-02-18 10:42:21 +00:00
// ...
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// DOCUMENT-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 3. Several documents in a stream:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 'a scalar'
2024-02-18 10:42:21 +00:00
// ---
2024-02-18 10:42:21 +00:00
// 'another scalar'
2024-02-18 10:42:21 +00:00
// ---
2024-02-18 10:42:21 +00:00
// 'yet another scalar'
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// SCALAR("a scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("another scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("yet another scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// We have already introduced the SCALAR token above. The following tokens are
2024-02-18 10:42:21 +00:00
// used to describe aliases, anchors, tag, and scalars:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// ALIAS(anchor)
2024-02-18 10:42:21 +00:00
// ANCHOR(anchor)
2024-02-18 10:42:21 +00:00
// TAG(handle,suffix)
2024-02-18 10:42:21 +00:00
// SCALAR(value,style)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following series of examples illustrate the usage of these tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. A recursive sequence:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// &A [ *A ]
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// ANCHOR("A")
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// ALIAS("A")
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 2. A tagged scalar:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// !!float "3.14" # A good approximation.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// TAG("!!","float")
2024-02-18 10:42:21 +00:00
// SCALAR("3.14",double-quoted)
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 3. Various scalar styles:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// --- # Implicit empty plain scalars do not produce tokens.
2024-02-18 10:42:21 +00:00
// --- a plain scalar
2024-02-18 10:42:21 +00:00
// --- 'a single-quoted scalar'
2024-02-18 10:42:21 +00:00
// --- "a double-quoted scalar"
2024-02-18 10:42:21 +00:00
// --- |-
2024-02-18 10:42:21 +00:00
// a literal scalar
2024-02-18 10:42:21 +00:00
// --- >-
2024-02-18 10:42:21 +00:00
// a folded
2024-02-18 10:42:21 +00:00
// scalar
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a plain scalar",plain)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a single-quoted scalar",single-quoted)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a double-quoted scalar",double-quoted)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a literal scalar",literal)
2024-02-18 10:42:21 +00:00
// DOCUMENT-START
2024-02-18 10:42:21 +00:00
// SCALAR("a folded scalar",folded)
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Now it's time to review collection-related tokens. We will start with
2024-02-18 10:42:21 +00:00
// flow collections:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-END
2024-02-18 10:42:21 +00:00
// FLOW-MAPPING-START
2024-02-18 10:42:21 +00:00
// FLOW-MAPPING-END
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
2024-02-18 10:42:21 +00:00
// FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
2024-02-18 10:42:21 +00:00
// correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
2024-02-18 10:42:21 +00:00
// indicators '?' and ':', which are used for denoting mapping keys and values,
2024-02-18 10:42:21 +00:00
// are represented by the KEY and VALUE tokens.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following examples show flow collections:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. A flow sequence:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// [item 1, item 2, item 3]
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 3",plain)
2024-02-18 10:42:21 +00:00
// FLOW-SEQUENCE-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 2. A flow mapping:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// {
2024-02-18 10:42:21 +00:00
// a simple key: a value, # Note that the KEY token is produced.
2024-02-18 10:42:21 +00:00
// ? a complex key: another value,
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// FLOW-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a simple key",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("a value",plain)
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a complex key",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("another value",plain)
2024-02-18 10:42:21 +00:00
// FLOW-ENTRY
2024-02-18 10:42:21 +00:00
// FLOW-MAPPING-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A simple key is a key which is not denoted by the '?' indicator. Note that
2024-02-18 10:42:21 +00:00
// the Scanner still produce the KEY token whenever it encounters a simple key.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For scanning block collections, the following tokens are used (note that we
2024-02-18 10:42:21 +00:00
// repeat KEY and VALUE here):
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
2024-02-18 10:42:21 +00:00
// increase that precedes a block collection (cf. the INDENT token in Python).
2024-02-18 10:42:21 +00:00
// The token BLOCK-END denote indentation decrease that ends a block collection
2024-02-18 10:42:21 +00:00
// (cf. the DEDENT token in Python). However YAML has some syntax pecularities
2024-02-18 10:42:21 +00:00
// that makes detections of these tokens more complex.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
2024-02-18 10:42:21 +00:00
// '-', '?', and ':' correspondingly.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following examples show how the tokens BLOCK-SEQUENCE-START,
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. Block sequences:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// - item 1
2024-02-18 10:42:21 +00:00
// - item 2
2024-02-18 10:42:21 +00:00
// -
2024-02-18 10:42:21 +00:00
// - item 3.1
2024-02-18 10:42:21 +00:00
// - item 3.2
2024-02-18 10:42:21 +00:00
// -
2024-02-18 10:42:21 +00:00
// key 1: value 1
2024-02-18 10:42:21 +00:00
// key 2: value 2
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 3.1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 3.2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 1",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 1",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 2",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 2. Block mappings:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// a simple key: a value # The KEY token is produced here.
2024-02-18 10:42:21 +00:00
// ? a complex key
2024-02-18 10:42:21 +00:00
// : another value
2024-02-18 10:42:21 +00:00
// a mapping:
2024-02-18 10:42:21 +00:00
// key 1: value 1
2024-02-18 10:42:21 +00:00
// key 2: value 2
2024-02-18 10:42:21 +00:00
// a sequence:
2024-02-18 10:42:21 +00:00
// - item 1
2024-02-18 10:42:21 +00:00
// - item 2
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a simple key",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("a value",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a complex key",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("another value",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a mapping",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 1",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 1",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 2",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a sequence",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// YAML does not always require to start a new block collection from a new
2024-02-18 10:42:21 +00:00
// line. If the current line contains only '-', '?', and ':' indicators, a new
2024-02-18 10:42:21 +00:00
// block collection may start at the current line. The following examples
2024-02-18 10:42:21 +00:00
// illustrate this case:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 1. Collections in a sequence:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// - - item 1
2024-02-18 10:42:21 +00:00
// - item 2
2024-02-18 10:42:21 +00:00
// - key 1: value 1
2024-02-18 10:42:21 +00:00
// key 2: value 2
2024-02-18 10:42:21 +00:00
// - ? complex key
2024-02-18 10:42:21 +00:00
// : complex value
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 1",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 1",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 2",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("complex key")
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("complex value")
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// 2. Collections in a mapping:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// ? a sequence
2024-02-18 10:42:21 +00:00
// : - item 1
2024-02-18 10:42:21 +00:00
// - item 2
2024-02-18 10:42:21 +00:00
// ? a mapping
2024-02-18 10:42:21 +00:00
// : key 1: value 1
2024-02-18 10:42:21 +00:00
// key 2: value 2
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a sequence",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// BLOCK-SEQUENCE-START
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("a mapping",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 1",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 1",plain)
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key 2",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// SCALAR("value 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
// STREAM-END
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// YAML also permits non-indented sequences if they are included into a block
2024-02-18 10:42:21 +00:00
// mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// key:
2024-02-18 10:42:21 +00:00
// - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
2024-02-18 10:42:21 +00:00
// - item 2
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Tokens:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// STREAM-START(utf-8)
2024-02-18 10:42:21 +00:00
// BLOCK-MAPPING-START
2024-02-18 10:42:21 +00:00
// KEY
2024-02-18 10:42:21 +00:00
// SCALAR("key",plain)
2024-02-18 10:42:21 +00:00
// VALUE
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 1",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-ENTRY
2024-02-18 10:42:21 +00:00
// SCALAR("item 2",plain)
2024-02-18 10:42:21 +00:00
// BLOCK-END
2024-02-18 10:42:21 +00:00
//
// Ensure that the buffer contains the required number of characters.
2024-02-18 10:42:21 +00:00
// Return true on success, false on failure (reader error or memory error).
2024-02-18 10:42:21 +00:00
func cache(parser *yaml_parser_t, length int) bool {
2024-02-18 10:42:21 +00:00
// [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B)
2024-02-18 10:42:21 +00:00
return parser.unread >= length || yaml_parser_update_buffer(parser, length)
2024-02-18 10:42:21 +00:00
}
// Advance the buffer pointer.
2024-02-18 10:42:21 +00:00
func skip(parser *yaml_parser_t) {
2024-02-18 10:42:21 +00:00
if !is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
parser.newlines = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
parser.mark.index++
2024-02-18 10:42:21 +00:00
parser.mark.column++
2024-02-18 10:42:21 +00:00
parser.unread--
2024-02-18 10:42:21 +00:00
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
2024-02-18 10:42:21 +00:00
}
func skip_line(parser *yaml_parser_t) {
2024-02-18 10:42:21 +00:00
if is_crlf(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
parser.mark.index += 2
2024-02-18 10:42:21 +00:00
parser.mark.column = 0
2024-02-18 10:42:21 +00:00
parser.mark.line++
2024-02-18 10:42:21 +00:00
parser.unread -= 2
2024-02-18 10:42:21 +00:00
parser.buffer_pos += 2
2024-02-18 10:42:21 +00:00
parser.newlines++
2024-02-18 10:42:21 +00:00
} else if is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
parser.mark.index++
2024-02-18 10:42:21 +00:00
parser.mark.column = 0
2024-02-18 10:42:21 +00:00
parser.mark.line++
2024-02-18 10:42:21 +00:00
parser.unread--
2024-02-18 10:42:21 +00:00
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
2024-02-18 10:42:21 +00:00
parser.newlines++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Copy a character to a string buffer and advance pointers.
2024-02-18 10:42:21 +00:00
func read(parser *yaml_parser_t, s []byte) []byte {
2024-02-18 10:42:21 +00:00
if !is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
parser.newlines = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
w := width(parser.buffer[parser.buffer_pos])
2024-02-18 10:42:21 +00:00
if w == 0 {
2024-02-18 10:42:21 +00:00
panic("invalid character sequence")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(s) == 0 {
2024-02-18 10:42:21 +00:00
s = make([]byte, 0, 32)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if w == 1 && len(s)+w <= cap(s) {
2024-02-18 10:42:21 +00:00
s = s[:len(s)+1]
2024-02-18 10:42:21 +00:00
s[len(s)-1] = parser.buffer[parser.buffer_pos]
2024-02-18 10:42:21 +00:00
parser.buffer_pos++
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
2024-02-18 10:42:21 +00:00
parser.buffer_pos += w
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
parser.mark.index++
2024-02-18 10:42:21 +00:00
parser.mark.column++
2024-02-18 10:42:21 +00:00
parser.unread--
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
// Copy a line break character to a string buffer and advance pointers.
2024-02-18 10:42:21 +00:00
func read_line(parser *yaml_parser_t, s []byte) []byte {
2024-02-18 10:42:21 +00:00
buf := parser.buffer
2024-02-18 10:42:21 +00:00
pos := parser.buffer_pos
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case buf[pos] == '\r' && buf[pos+1] == '\n':
2024-02-18 10:42:21 +00:00
// CR LF . LF
2024-02-18 10:42:21 +00:00
s = append(s, '\n')
2024-02-18 10:42:21 +00:00
parser.buffer_pos += 2
2024-02-18 10:42:21 +00:00
parser.mark.index++
2024-02-18 10:42:21 +00:00
parser.unread--
2024-02-18 10:42:21 +00:00
case buf[pos] == '\r' || buf[pos] == '\n':
2024-02-18 10:42:21 +00:00
// CR|LF . LF
2024-02-18 10:42:21 +00:00
s = append(s, '\n')
2024-02-18 10:42:21 +00:00
parser.buffer_pos += 1
2024-02-18 10:42:21 +00:00
case buf[pos] == '\xC2' && buf[pos+1] == '\x85':
2024-02-18 10:42:21 +00:00
// NEL . LF
2024-02-18 10:42:21 +00:00
s = append(s, '\n')
2024-02-18 10:42:21 +00:00
parser.buffer_pos += 2
2024-02-18 10:42:21 +00:00
case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'):
2024-02-18 10:42:21 +00:00
// LS|PS . LS|PS
2024-02-18 10:42:21 +00:00
s = append(s, buf[parser.buffer_pos:pos+3]...)
2024-02-18 10:42:21 +00:00
parser.buffer_pos += 3
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
parser.mark.index++
2024-02-18 10:42:21 +00:00
parser.mark.column = 0
2024-02-18 10:42:21 +00:00
parser.mark.line++
2024-02-18 10:42:21 +00:00
parser.unread--
2024-02-18 10:42:21 +00:00
parser.newlines++
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
// Get the next token.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan(parser *yaml_parser_t, token *yaml_token_t) bool {
2024-02-18 10:42:21 +00:00
// Erase the token object.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{} // [Go] Is this necessary?
// No tokens after STREAM-END or error.
2024-02-18 10:42:21 +00:00
if parser.stream_end_produced || parser.error != yaml_NO_ERROR {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Ensure that the tokens queue contains enough tokens.
2024-02-18 10:42:21 +00:00
if !parser.token_available {
2024-02-18 10:42:21 +00:00
if !yaml_parser_fetch_more_tokens(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Fetch the next token from the queue.
2024-02-18 10:42:21 +00:00
*token = parser.tokens[parser.tokens_head]
2024-02-18 10:42:21 +00:00
parser.tokens_head++
2024-02-18 10:42:21 +00:00
parser.tokens_parsed++
2024-02-18 10:42:21 +00:00
parser.token_available = false
if token.typ == yaml_STREAM_END_TOKEN {
2024-02-18 10:42:21 +00:00
parser.stream_end_produced = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Set the scanner error and return false.
2024-02-18 10:42:21 +00:00
func yaml_parser_set_scanner_error(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string) bool {
2024-02-18 10:42:21 +00:00
parser.error = yaml_SCANNER_ERROR
2024-02-18 10:42:21 +00:00
parser.context = context
2024-02-18 10:42:21 +00:00
parser.context_mark = context_mark
2024-02-18 10:42:21 +00:00
parser.problem = problem
2024-02-18 10:42:21 +00:00
parser.problem_mark = parser.mark
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
func yaml_parser_set_scanner_tag_error(parser *yaml_parser_t, directive bool, context_mark yaml_mark_t, problem string) bool {
2024-02-18 10:42:21 +00:00
context := "while parsing a tag"
2024-02-18 10:42:21 +00:00
if directive {
2024-02-18 10:42:21 +00:00
context = "while parsing a %TAG directive"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, context, context_mark, problem)
2024-02-18 10:42:21 +00:00
}
func trace(args ...interface{}) func() {
2024-02-18 10:42:21 +00:00
pargs := append([]interface{}{"+++"}, args...)
2024-02-18 10:42:21 +00:00
fmt.Println(pargs...)
2024-02-18 10:42:21 +00:00
pargs = append([]interface{}{"---"}, args...)
2024-02-18 10:42:21 +00:00
return func() { fmt.Println(pargs...) }
2024-02-18 10:42:21 +00:00
}
// Ensure that the tokens queue contains at least one token which can be
2024-02-18 10:42:21 +00:00
// returned to the Parser.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// While we need more tokens to fetch, do it.
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// [Go] The comment parsing logic requires a lookahead of two tokens
2024-02-18 10:42:21 +00:00
// so that foot comments may be parsed in time of associating them
2024-02-18 10:42:21 +00:00
// with the tokens that are parsed before them, and also for line
2024-02-18 10:42:21 +00:00
// comments to be transformed into head comments in some edge cases.
2024-02-18 10:42:21 +00:00
if parser.tokens_head < len(parser.tokens)-2 {
2024-02-18 10:42:21 +00:00
// If a potential simple key is at the head position, we need to fetch
2024-02-18 10:42:21 +00:00
// the next token to disambiguate it.
2024-02-18 10:42:21 +00:00
head_tok_idx, ok := parser.simple_keys_by_tok[parser.tokens_parsed]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
} else if valid, ok := yaml_simple_key_is_valid(parser, &parser.simple_keys[head_tok_idx]); !ok {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
} else if !valid {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Fetch the next token.
2024-02-18 10:42:21 +00:00
if !yaml_parser_fetch_next_token(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
parser.token_available = true
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// The dispatcher for token fetchers.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_next_token(parser *yaml_parser_t) (ok bool) {
2024-02-18 10:42:21 +00:00
// Ensure that the buffer is initialized.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check if we just started scanning. Fetch STREAM-START then.
2024-02-18 10:42:21 +00:00
if !parser.stream_start_produced {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_stream_start(parser)
2024-02-18 10:42:21 +00:00
}
scan_mark := parser.mark
// Eat whitespaces and comments until we reach the next token.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_to_next_token(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// [Go] While unrolling indents, transform the head comments of prior
2024-02-18 10:42:21 +00:00
// indentation levels observed after scan_start into foot comments at
2024-02-18 10:42:21 +00:00
// the respective indexes.
// Check the indentation level against the current column.
2024-02-18 10:42:21 +00:00
if !yaml_parser_unroll_indent(parser, parser.mark.column, scan_mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Ensure that the buffer contains at least 4 characters. 4 is the length
2024-02-18 10:42:21 +00:00
// of the longest indicators ('--- ' and '... ').
2024-02-18 10:42:21 +00:00
if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Is it the end of the stream?
2024-02-18 10:42:21 +00:00
if is_z(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_stream_end(parser)
2024-02-18 10:42:21 +00:00
}
// Is it a directive?
2024-02-18 10:42:21 +00:00
if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_directive(parser)
2024-02-18 10:42:21 +00:00
}
buf := parser.buffer
2024-02-18 10:42:21 +00:00
pos := parser.buffer_pos
// Is it the document start indicator?
2024-02-18 10:42:21 +00:00
if parser.mark.column == 0 && buf[pos] == '-' && buf[pos+1] == '-' && buf[pos+2] == '-' && is_blankz(buf, pos+3) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_START_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it the document end indicator?
2024-02-18 10:42:21 +00:00
if parser.mark.column == 0 && buf[pos] == '.' && buf[pos+1] == '.' && buf[pos+2] == '.' && is_blankz(buf, pos+3) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_END_TOKEN)
2024-02-18 10:42:21 +00:00
}
comment_mark := parser.mark
2024-02-18 10:42:21 +00:00
if len(parser.tokens) > 0 && (parser.flow_level == 0 && buf[pos] == ':' || parser.flow_level > 0 && buf[pos] == ',') {
2024-02-18 10:42:21 +00:00
// Associate any following comments with the prior token.
2024-02-18 10:42:21 +00:00
comment_mark = parser.tokens[len(parser.tokens)-1].start_mark
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer func() {
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(parser.tokens) > 0 && parser.tokens[len(parser.tokens)-1].typ == yaml_BLOCK_ENTRY_TOKEN {
2024-02-18 10:42:21 +00:00
// Sequence indicators alone have no line comments. It becomes
2024-02-18 10:42:21 +00:00
// a head comment for whatever follows.
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_line_comment(parser, comment_mark) {
2024-02-18 10:42:21 +00:00
ok = false
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}()
// Is it the flow sequence start indicator?
2024-02-18 10:42:21 +00:00
if buf[pos] == '[' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it the flow mapping start indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '{' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_MAPPING_START_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it the flow sequence end indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == ']' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_collection_end(parser,
2024-02-18 10:42:21 +00:00
yaml_FLOW_SEQUENCE_END_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it the flow mapping end indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '}' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_collection_end(parser,
2024-02-18 10:42:21 +00:00
yaml_FLOW_MAPPING_END_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it the flow entry indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == ',' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_entry(parser)
2024-02-18 10:42:21 +00:00
}
// Is it the block entry indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '-' && is_blankz(parser.buffer, parser.buffer_pos+1) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_block_entry(parser)
2024-02-18 10:42:21 +00:00
}
// Is it the key indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '?' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_key(parser)
2024-02-18 10:42:21 +00:00
}
// Is it the value indicator?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == ':' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_value(parser)
2024-02-18 10:42:21 +00:00
}
// Is it an alias?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '*' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it an anchor?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '&' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN)
2024-02-18 10:42:21 +00:00
}
// Is it a tag?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '!' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_tag(parser)
2024-02-18 10:42:21 +00:00
}
// Is it a literal scalar?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '|' && parser.flow_level == 0 {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_block_scalar(parser, true)
2024-02-18 10:42:21 +00:00
}
// Is it a folded scalar?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '>' && parser.flow_level == 0 {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_block_scalar(parser, false)
2024-02-18 10:42:21 +00:00
}
// Is it a single-quoted scalar?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '\'' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_scalar(parser, true)
2024-02-18 10:42:21 +00:00
}
// Is it a double-quoted scalar?
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '"' {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_flow_scalar(parser, false)
2024-02-18 10:42:21 +00:00
}
// Is it a plain scalar?
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A plain scalar may start with any non-blank characters except
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// '-', '?', ':', ',', '[', ']', '{', '}',
2024-02-18 10:42:21 +00:00
// '#', '&', '*', '!', '|', '>', '\'', '\"',
2024-02-18 10:42:21 +00:00
// '%', '@', '`'.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// In the block context (and, for the '-' indicator, in the flow context
2024-02-18 10:42:21 +00:00
// too), it may also start with the characters
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// '-', '?', ':'
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// if it is followed by a non-space character.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The last rule is more restrictive than the specification requires.
2024-02-18 10:42:21 +00:00
// [Go] TODO Make this logic more reasonable.
2024-02-18 10:42:21 +00:00
//switch parser.buffer[parser.buffer_pos] {
2024-02-18 10:42:21 +00:00
//case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
2024-02-18 10:42:21 +00:00
//}
2024-02-18 10:42:21 +00:00
if !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '-' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '[' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '}' || parser.buffer[parser.buffer_pos] == '#' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '*' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '|' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '>' || parser.buffer[parser.buffer_pos] == '\'' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '"' || parser.buffer[parser.buffer_pos] == '%' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '@' || parser.buffer[parser.buffer_pos] == '`') ||
2024-02-18 10:42:21 +00:00
(parser.buffer[parser.buffer_pos] == '-' && !is_blank(parser.buffer, parser.buffer_pos+1)) ||
2024-02-18 10:42:21 +00:00
(parser.flow_level == 0 &&
2024-02-18 10:42:21 +00:00
(parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':') &&
2024-02-18 10:42:21 +00:00
!is_blankz(parser.buffer, parser.buffer_pos+1)) {
2024-02-18 10:42:21 +00:00
return yaml_parser_fetch_plain_scalar(parser)
2024-02-18 10:42:21 +00:00
}
// If we don't determine the token type so far, it is an error.
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser,
2024-02-18 10:42:21 +00:00
"while scanning for the next token", parser.mark,
2024-02-18 10:42:21 +00:00
"found character that cannot start any token")
2024-02-18 10:42:21 +00:00
}
func yaml_simple_key_is_valid(parser *yaml_parser_t, simple_key *yaml_simple_key_t) (valid, ok bool) {
2024-02-18 10:42:21 +00:00
if !simple_key.possible {
2024-02-18 10:42:21 +00:00
return false, true
2024-02-18 10:42:21 +00:00
}
// The 1.2 specification says:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// "If the ? indicator is omitted, parsing needs to see past the
2024-02-18 10:42:21 +00:00
// implicit key to recognize it as such. To limit the amount of
2024-02-18 10:42:21 +00:00
// lookahead required, the “:” indicator must appear at most 1024
2024-02-18 10:42:21 +00:00
// Unicode characters beyond the start of the key. In addition, the key
2024-02-18 10:42:21 +00:00
// is restricted to a single line."
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
if simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index {
2024-02-18 10:42:21 +00:00
// Check if the potential simple key to be removed is required.
2024-02-18 10:42:21 +00:00
if simple_key.required {
2024-02-18 10:42:21 +00:00
return false, yaml_parser_set_scanner_error(parser,
2024-02-18 10:42:21 +00:00
"while scanning a simple key", simple_key.mark,
2024-02-18 10:42:21 +00:00
"could not find expected ':'")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
simple_key.possible = false
2024-02-18 10:42:21 +00:00
return false, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true, true
2024-02-18 10:42:21 +00:00
}
// Check if a simple key may start at the current position and add it if
2024-02-18 10:42:21 +00:00
// needed.
2024-02-18 10:42:21 +00:00
func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// A simple key is required at the current position if the scanner is in
2024-02-18 10:42:21 +00:00
// the block context and the current column coincides with the indentation
2024-02-18 10:42:21 +00:00
// level.
required := parser.flow_level == 0 && parser.indent == parser.mark.column
//
2024-02-18 10:42:21 +00:00
// If the current position may start a simple key, save it.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
if parser.simple_key_allowed {
2024-02-18 10:42:21 +00:00
simple_key := yaml_simple_key_t{
possible: true,
required: required,
2024-02-18 10:42:21 +00:00
token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
mark: parser.mark,
2024-02-18 10:42:21 +00:00
}
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
parser.simple_keys[len(parser.simple_keys)-1] = simple_key
2024-02-18 10:42:21 +00:00
parser.simple_keys_by_tok[simple_key.token_number] = len(parser.simple_keys) - 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Remove a potential simple key at the current flow level.
2024-02-18 10:42:21 +00:00
func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
i := len(parser.simple_keys) - 1
2024-02-18 10:42:21 +00:00
if parser.simple_keys[i].possible {
2024-02-18 10:42:21 +00:00
// If the key is required, it is an error.
2024-02-18 10:42:21 +00:00
if parser.simple_keys[i].required {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser,
2024-02-18 10:42:21 +00:00
"while scanning a simple key", parser.simple_keys[i].mark,
2024-02-18 10:42:21 +00:00
"could not find expected ':'")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Remove the key from the stack.
2024-02-18 10:42:21 +00:00
parser.simple_keys[i].possible = false
2024-02-18 10:42:21 +00:00
delete(parser.simple_keys_by_tok, parser.simple_keys[i].token_number)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// max_flow_level limits the flow_level
2024-02-18 10:42:21 +00:00
const max_flow_level = 10000
// Increase the flow level and resize the simple key list if needed.
2024-02-18 10:42:21 +00:00
func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// Reset the simple key on the next level.
2024-02-18 10:42:21 +00:00
parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{
possible: false,
required: false,
2024-02-18 10:42:21 +00:00
token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
mark: parser.mark,
2024-02-18 10:42:21 +00:00
})
// Increase the flow level.
2024-02-18 10:42:21 +00:00
parser.flow_level++
2024-02-18 10:42:21 +00:00
if parser.flow_level > max_flow_level {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser,
2024-02-18 10:42:21 +00:00
"while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark,
2024-02-18 10:42:21 +00:00
fmt.Sprintf("exceeded max depth of %d", max_flow_level))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Decrease the flow level.
2024-02-18 10:42:21 +00:00
func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
if parser.flow_level > 0 {
2024-02-18 10:42:21 +00:00
parser.flow_level--
2024-02-18 10:42:21 +00:00
last := len(parser.simple_keys) - 1
2024-02-18 10:42:21 +00:00
delete(parser.simple_keys_by_tok, parser.simple_keys[last].token_number)
2024-02-18 10:42:21 +00:00
parser.simple_keys = parser.simple_keys[:last]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// max_indents limits the indents stack size
2024-02-18 10:42:21 +00:00
const max_indents = 10000
// Push the current indentation level to the stack and set the new level
2024-02-18 10:42:21 +00:00
// the current column is greater than the indentation level. In this case,
2024-02-18 10:42:21 +00:00
// append or insert the specified token into the token queue.
2024-02-18 10:42:21 +00:00
func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
2024-02-18 10:42:21 +00:00
// In the flow context, do nothing.
2024-02-18 10:42:21 +00:00
if parser.flow_level > 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
if parser.indent < column {
2024-02-18 10:42:21 +00:00
// Push the current indentation level to the stack and set the new
2024-02-18 10:42:21 +00:00
// indentation level.
2024-02-18 10:42:21 +00:00
parser.indents = append(parser.indents, parser.indent)
2024-02-18 10:42:21 +00:00
parser.indent = column
2024-02-18 10:42:21 +00:00
if len(parser.indents) > max_indents {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser,
2024-02-18 10:42:21 +00:00
"while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark,
2024-02-18 10:42:21 +00:00
fmt.Sprintf("exceeded max depth of %d", max_indents))
2024-02-18 10:42:21 +00:00
}
// Create a token and insert it into the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: typ,
2024-02-18 10:42:21 +00:00
start_mark: mark,
end_mark: mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if number > -1 {
2024-02-18 10:42:21 +00:00
number -= parser.tokens_parsed
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, number, &token)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Pop indentation levels from the indents stack until the current level
2024-02-18 10:42:21 +00:00
// becomes less or equal to the column. For each indentation level, append
2024-02-18 10:42:21 +00:00
// the BLOCK-END token.
2024-02-18 10:42:21 +00:00
func yaml_parser_unroll_indent(parser *yaml_parser_t, column int, scan_mark yaml_mark_t) bool {
2024-02-18 10:42:21 +00:00
// In the flow context, do nothing.
2024-02-18 10:42:21 +00:00
if parser.flow_level > 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
block_mark := scan_mark
2024-02-18 10:42:21 +00:00
block_mark.index--
// Loop through the indentation levels in the stack.
2024-02-18 10:42:21 +00:00
for parser.indent > column {
// [Go] Reposition the end token before potential following
2024-02-18 10:42:21 +00:00
// foot comments of parent blocks. For that, search
2024-02-18 10:42:21 +00:00
// backwards for recent comments that were at the same
2024-02-18 10:42:21 +00:00
// indent as the block that is ending now.
2024-02-18 10:42:21 +00:00
stop_index := block_mark.index
2024-02-18 10:42:21 +00:00
for i := len(parser.comments) - 1; i >= 0; i-- {
2024-02-18 10:42:21 +00:00
comment := &parser.comments[i]
if comment.end_mark.index < stop_index {
2024-02-18 10:42:21 +00:00
// Don't go back beyond the start of the comment/whitespace scan, unless column < 0.
2024-02-18 10:42:21 +00:00
// If requested indent column is < 0, then the document is over and everything else
2024-02-18 10:42:21 +00:00
// is a foot anyway.
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if comment.start_mark.column == parser.indent+1 {
2024-02-18 10:42:21 +00:00
// This is a good match. But maybe there's a former comment
2024-02-18 10:42:21 +00:00
// at that same indent level, so keep searching.
2024-02-18 10:42:21 +00:00
block_mark = comment.start_mark
2024-02-18 10:42:21 +00:00
}
// While the end of the former comment matches with
2024-02-18 10:42:21 +00:00
// the start of the following one, we know there's
2024-02-18 10:42:21 +00:00
// nothing in between and scanning is still safe.
2024-02-18 10:42:21 +00:00
stop_index = comment.scan_mark.index
2024-02-18 10:42:21 +00:00
}
// Create a token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_BLOCK_END_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: block_mark,
end_mark: block_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
// Pop the indentation level.
2024-02-18 10:42:21 +00:00
parser.indent = parser.indents[len(parser.indents)-1]
2024-02-18 10:42:21 +00:00
parser.indents = parser.indents[:len(parser.indents)-1]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Initialize the scanner and produce the STREAM-START token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
// Set the initial indentation.
2024-02-18 10:42:21 +00:00
parser.indent = -1
// Initialize the simple key stack.
2024-02-18 10:42:21 +00:00
parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
parser.simple_keys_by_tok = make(map[int]int)
// A simple key is allowed at the beginning of the stream.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
// We have started.
2024-02-18 10:42:21 +00:00
parser.stream_start_produced = true
// Create the STREAM-START token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_STREAM_START_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: parser.mark,
end_mark: parser.mark,
encoding: parser.encoding,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the STREAM-END token and shut down the scanner.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool {
// Force new line.
2024-02-18 10:42:21 +00:00
if parser.mark.column != 0 {
2024-02-18 10:42:21 +00:00
parser.mark.column = 0
2024-02-18 10:42:21 +00:00
parser.mark.line++
2024-02-18 10:42:21 +00:00
}
// Reset the indentation level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Reset simple keys.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
parser.simple_key_allowed = false
// Create the STREAM-END token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_STREAM_END_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: parser.mark,
end_mark: parser.mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_directive(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// Reset the indentation level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Reset simple keys.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
parser.simple_key_allowed = false
// Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{}
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_directive(parser, &token) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Append the token to the queue.
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the DOCUMENT-START or DOCUMENT-END token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool {
2024-02-18 10:42:21 +00:00
// Reset the indentation level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Reset simple keys.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
parser.simple_key_allowed = false
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
end_mark := parser.mark
// Create the DOCUMENT-START or DOCUMENT-END token.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: typ,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Append the token to the queue.
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool {
// The indicators '[' and '{' may start a simple key.
2024-02-18 10:42:21 +00:00
if !yaml_parser_save_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Increase the flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_increase_flow_level(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key may follow the indicators '[' and '{'.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: typ,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Append the token to the queue.
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool {
2024-02-18 10:42:21 +00:00
// Reset any potential simple key on the current flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Decrease the flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_decrease_flow_level(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// No simple keys after the indicators ']' and '}'.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
// Consume the token.
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: typ,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Append the token to the queue.
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the FLOW-ENTRY token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// Reset any potential simple keys on the current flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Simple keys are allowed after ','.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the FLOW-ENTRY token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_FLOW_ENTRY_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the BLOCK-ENTRY token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// Check if the scanner is in the block context.
2024-02-18 10:42:21 +00:00
if parser.flow_level == 0 {
2024-02-18 10:42:21 +00:00
// Check if we are allowed to start a new entry.
2024-02-18 10:42:21 +00:00
if !parser.simple_key_allowed {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "", parser.mark,
2024-02-18 10:42:21 +00:00
"block sequence entries are not allowed in this context")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Add the BLOCK-SEQUENCE-START token if needed.
2024-02-18 10:42:21 +00:00
if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// It is an error for the '-' indicator to occur in the flow context,
2024-02-18 10:42:21 +00:00
// but we let the Parser detect and report about it because the Parser
2024-02-18 10:42:21 +00:00
// is able to point to the context.
2024-02-18 10:42:21 +00:00
}
// Reset any potential simple keys on the current flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Simple keys are allowed after '-'.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the BLOCK-ENTRY token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_BLOCK_ENTRY_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the KEY token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_key(parser *yaml_parser_t) bool {
// In the block context, additional checks are required.
2024-02-18 10:42:21 +00:00
if parser.flow_level == 0 {
2024-02-18 10:42:21 +00:00
// Check if we are allowed to start a new key (not nessesary simple).
2024-02-18 10:42:21 +00:00
if !parser.simple_key_allowed {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "", parser.mark,
2024-02-18 10:42:21 +00:00
"mapping keys are not allowed in this context")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Add the BLOCK-MAPPING-START token if needed.
2024-02-18 10:42:21 +00:00
if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Reset any potential simple keys on the current flow level.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Simple keys are allowed after '?' in the block context.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = parser.flow_level == 0
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the KEY token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_KEY_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the VALUE token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
// Have we found a simple key?
2024-02-18 10:42:21 +00:00
if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok {
2024-02-18 10:42:21 +00:00
return false
} else if valid {
// Create the KEY token and insert it into the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_KEY_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: simple_key.mark,
end_mark: simple_key.mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token)
// In the block context, we may need to add the BLOCK-MAPPING-START token.
2024-02-18 10:42:21 +00:00
if !yaml_parser_roll_indent(parser, simple_key.mark.column,
2024-02-18 10:42:21 +00:00
simple_key.token_number,
2024-02-18 10:42:21 +00:00
yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Remove the simple key.
2024-02-18 10:42:21 +00:00
simple_key.possible = false
2024-02-18 10:42:21 +00:00
delete(parser.simple_keys_by_tok, simple_key.token_number)
// A simple key cannot follow another simple key.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
} else {
2024-02-18 10:42:21 +00:00
// The ':' indicator follows a complex key.
// In the block context, extra checks are required.
2024-02-18 10:42:21 +00:00
if parser.flow_level == 0 {
// Check if we are allowed to start a complex value.
2024-02-18 10:42:21 +00:00
if !parser.simple_key_allowed {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "", parser.mark,
2024-02-18 10:42:21 +00:00
"mapping values are not allowed in this context")
2024-02-18 10:42:21 +00:00
}
// Add the BLOCK-MAPPING-START token if needed.
2024-02-18 10:42:21 +00:00
if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Simple keys after ':' are allowed in the block context.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = parser.flow_level == 0
2024-02-18 10:42:21 +00:00
}
// Consume the token.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create the VALUE token and append it to the queue.
2024-02-18 10:42:21 +00:00
token := yaml_token_t{
typ: yaml_VALUE_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the ALIAS or ANCHOR token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool {
2024-02-18 10:42:21 +00:00
// An anchor or an alias could be a simple key.
2024-02-18 10:42:21 +00:00
if !yaml_parser_save_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key cannot follow an anchor or an alias.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
// Create the ALIAS or ANCHOR token and append it to the queue.
2024-02-18 10:42:21 +00:00
var token yaml_token_t
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_anchor(parser, &token, typ) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the TAG token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_tag(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// A tag could be a simple key.
2024-02-18 10:42:21 +00:00
if !yaml_parser_save_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key cannot follow a tag.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
// Create the TAG token and append it to the queue.
2024-02-18 10:42:21 +00:00
var token yaml_token_t
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag(parser, &token) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool {
2024-02-18 10:42:21 +00:00
// Remove any potential simple keys.
2024-02-18 10:42:21 +00:00
if !yaml_parser_remove_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key may follow a block scalar.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
// Create the SCALAR token and append it to the queue.
2024-02-18 10:42:21 +00:00
var token yaml_token_t
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_block_scalar(parser, &token, literal) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool {
2024-02-18 10:42:21 +00:00
// A plain scalar could be a simple key.
2024-02-18 10:42:21 +00:00
if !yaml_parser_save_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key cannot follow a flow scalar.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
// Create the SCALAR token and append it to the queue.
2024-02-18 10:42:21 +00:00
var token yaml_token_t
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_flow_scalar(parser, &token, single) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Produce the SCALAR(...,plain) token.
2024-02-18 10:42:21 +00:00
func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool {
2024-02-18 10:42:21 +00:00
// A plain scalar could be a simple key.
2024-02-18 10:42:21 +00:00
if !yaml_parser_save_simple_key(parser) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// A simple key cannot follow a flow scalar.
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = false
// Create the SCALAR token and append it to the queue.
2024-02-18 10:42:21 +00:00
var token yaml_token_t
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_plain_scalar(parser, &token) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_insert_token(parser, -1, &token)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Eat whitespaces and comments until the next token is found.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool {
scan_mark := parser.mark
// Until the next token is not found.
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// Allow the BOM mark to start a line.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
// Eat whitespaces.
2024-02-18 10:42:21 +00:00
// Tabs are allowed:
2024-02-18 10:42:21 +00:00
// - in the flow context
2024-02-18 10:42:21 +00:00
// - in the block context, but not at the beginning of the line or
2024-02-18 10:42:21 +00:00
// after '-', '?', or ':' (complex value).
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if we just had a line comment under a sequence entry that
2024-02-18 10:42:21 +00:00
// looks more like a header to the following content. Similar to this:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// - # The comment
2024-02-18 10:42:21 +00:00
// - Some data
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If so, transform the line comment to a head comment and reposition.
2024-02-18 10:42:21 +00:00
if len(parser.comments) > 0 && len(parser.tokens) > 1 {
2024-02-18 10:42:21 +00:00
tokenA := parser.tokens[len(parser.tokens)-2]
2024-02-18 10:42:21 +00:00
tokenB := parser.tokens[len(parser.tokens)-1]
2024-02-18 10:42:21 +00:00
comment := &parser.comments[len(parser.comments)-1]
2024-02-18 10:42:21 +00:00
if tokenA.typ == yaml_BLOCK_SEQUENCE_START_TOKEN && tokenB.typ == yaml_BLOCK_ENTRY_TOKEN && len(comment.line) > 0 && !is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// If it was in the prior line, reposition so it becomes a
2024-02-18 10:42:21 +00:00
// header of the follow up token. Otherwise, keep it in place
2024-02-18 10:42:21 +00:00
// so it becomes a header of the former.
2024-02-18 10:42:21 +00:00
comment.head = comment.line
2024-02-18 10:42:21 +00:00
comment.line = nil
2024-02-18 10:42:21 +00:00
if comment.start_mark.line == parser.mark.line-1 {
2024-02-18 10:42:21 +00:00
comment.token_mark = parser.mark
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Eat a comment until a line break.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '#' {
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_comments(parser, scan_mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// If it is a line break, eat it.
2024-02-18 10:42:21 +00:00
if is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip_line(parser)
// In the block context, a new line may start a simple key.
2024-02-18 10:42:21 +00:00
if parser.flow_level == 0 {
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
break // We have found a token.
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
// Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Scope:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// %YAML 1.1 # a comment \n
2024-06-14 08:41:36 +00:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-06-14 08:41:36 +00:00
// %TAG !yaml! tag:yaml.org,2002: \n
2024-06-14 08:41:36 +00:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool {
2024-02-18 10:42:21 +00:00
// Eat '%'.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
// Scan the directive name.
2024-02-18 10:42:21 +00:00
var name []byte
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_directive_name(parser, start_mark, &name) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Is it a YAML directive?
2024-02-18 10:42:21 +00:00
if bytes.Equal(name, []byte("YAML")) {
2024-02-18 10:42:21 +00:00
// Scan the VERSION directive value.
2024-02-18 10:42:21 +00:00
var major, minor int8
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create a VERSION-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_VERSION_DIRECTIVE_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
major: major,
minor: minor,
2024-02-18 10:42:21 +00:00
}
// Is it a TAG directive?
2024-02-18 10:42:21 +00:00
} else if bytes.Equal(name, []byte("TAG")) {
2024-02-18 10:42:21 +00:00
// Scan the TAG directive value.
2024-02-18 10:42:21 +00:00
var handle, prefix []byte
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create a TAG-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_TAG_DIRECTIVE_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: handle,
prefix: prefix,
2024-02-18 10:42:21 +00:00
}
// Unknown directive.
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a directive",
2024-02-18 10:42:21 +00:00
start_mark, "found unknown directive name")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Eat the rest of the line including any comments.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if parser.buffer[parser.buffer_pos] == '#' {
2024-02-18 10:42:21 +00:00
// [Go] Discard this inline comment for the time being.
2024-02-18 10:42:21 +00:00
//if !yaml_parser_scan_line_comment(parser, start_mark) {
2024-02-18 10:42:21 +00:00
// return false
2024-02-18 10:42:21 +00:00
//}
2024-02-18 10:42:21 +00:00
for !is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if we are at the end of the line.
2024-02-18 10:42:21 +00:00
if !is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a directive",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected comment or line break")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Eat a line break.
2024-02-18 10:42:21 +00:00
if is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip_line(parser)
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
// Scan the directive name.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Scope:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// %YAML 1.1 # a comment \n
2024-06-14 08:41:36 +00:00
// ^^^^
2024-06-14 08:41:36 +00:00
// %TAG !yaml! tag:yaml.org,2002: \n
2024-06-14 08:41:36 +00:00
// ^^^
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool {
2024-02-18 10:42:21 +00:00
// Consume the directive name.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
var s []byte
2024-02-18 10:42:21 +00:00
for is_alpha(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if the name is empty.
2024-02-18 10:42:21 +00:00
if len(s) == 0 {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a directive",
2024-02-18 10:42:21 +00:00
start_mark, "could not find expected directive name")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check for an blank character after the name.
2024-02-18 10:42:21 +00:00
if !is_blankz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a directive",
2024-02-18 10:42:21 +00:00
start_mark, "found unexpected non-alphabetical character")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*name = s
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan the value of VERSION-DIRECTIVE.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Scope:
2024-06-14 08:41:36 +00:00
//
2024-06-14 08:41:36 +00:00
// %YAML 1.1 # a comment \n
2024-06-14 08:41:36 +00:00
// ^^^^^^
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool {
2024-02-18 10:42:21 +00:00
// Eat whitespaces.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Consume the major version number.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_version_directive_number(parser, start_mark, major) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Eat '.'.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] != '.' {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected digit or '.' character")
2024-02-18 10:42:21 +00:00
}
skip(parser)
// Consume the minor version number.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
const max_number_length = 2
// Scan the version number of VERSION-DIRECTIVE.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Scope:
2024-06-14 08:41:36 +00:00
//
2024-06-14 08:41:36 +00:00
// %YAML 1.1 # a comment \n
2024-06-14 08:41:36 +00:00
// ^
2024-06-14 08:41:36 +00:00
// %YAML 1.1 # a comment \n
2024-06-14 08:41:36 +00:00
// ^
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool {
// Repeat while the next character is digit.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var value, length int8
2024-02-18 10:42:21 +00:00
for is_digit(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// Check if the number is too long.
2024-02-18 10:42:21 +00:00
length++
2024-02-18 10:42:21 +00:00
if length > max_number_length {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2024-02-18 10:42:21 +00:00
start_mark, "found extremely long version number")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos))
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if the number was present.
2024-02-18 10:42:21 +00:00
if length == 0 {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected version number")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*number = value
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan the value of a TAG-DIRECTIVE token.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Scope:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// %TAG !yaml! tag:yaml.org,2002: \n
2024-06-14 08:41:36 +00:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool {
2024-02-18 10:42:21 +00:00
var handle_value, prefix_value []byte
// Eat whitespaces.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Scan a handle.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Expect a whitespace.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected whitespace")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Eat whitespaces.
2024-02-18 10:42:21 +00:00
for is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Scan a prefix.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Expect a whitespace or line break.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_blankz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected whitespace or line break")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
*handle = handle_value
2024-02-18 10:42:21 +00:00
*prefix = prefix_value
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool {
2024-02-18 10:42:21 +00:00
var s []byte
// Eat the indicator character.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
// Consume the value.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for is_alpha(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
end_mark := parser.mark
/*
2024-02-18 10:42:21 +00:00
* Check if length of the anchor is greater than 0 and it is followed by
2024-02-18 10:42:21 +00:00
* a whitespace character or one of the indicators:
2024-02-18 10:42:21 +00:00
*
2024-02-18 10:42:21 +00:00
* '?', ':', ',', ']', '}', '%', '@', '`'.
2024-02-18 10:42:21 +00:00
*/
if len(s) == 0 ||
2024-02-18 10:42:21 +00:00
!(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '`') {
2024-02-18 10:42:21 +00:00
context := "while scanning an alias"
2024-02-18 10:42:21 +00:00
if typ == yaml_ANCHOR_TOKEN {
2024-02-18 10:42:21 +00:00
context = "while scanning an anchor"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, context, start_mark,
2024-02-18 10:42:21 +00:00
"did not find expected alphabetic or numeric character")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Create a token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: typ,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: s,
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
/*
2024-02-18 10:42:21 +00:00
* Scan a TAG token.
2024-02-18 10:42:21 +00:00
*/
func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool {
2024-02-18 10:42:21 +00:00
var handle, suffix []byte
start_mark := parser.mark
// Check if the tag is in the canonical form.
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
if parser.buffer[parser.buffer_pos+1] == '<' {
2024-02-18 10:42:21 +00:00
// Keep the handle as ''
// Eat '!<'
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
// Consume the tag value.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check for '>' and eat it.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] != '>' {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a tag",
2024-02-18 10:42:21 +00:00
start_mark, "did not find the expected '>'")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
skip(parser)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// The tag has either the '!suffix' or the '!handle!suffix' form.
// First, try to scan a handle.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check if it is, indeed, handle.
2024-02-18 10:42:21 +00:00
if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' {
2024-02-18 10:42:21 +00:00
// Scan the suffix now.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// It wasn't a handle after all. Scan the rest of the tag.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Set the handle to '!'.
2024-02-18 10:42:21 +00:00
handle = []byte{'!'}
// A special case: the '!' tag. Set the handle to '' and the
2024-02-18 10:42:21 +00:00
// suffix to '!'.
2024-02-18 10:42:21 +00:00
if len(suffix) == 0 {
2024-02-18 10:42:21 +00:00
handle, suffix = suffix, handle
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check the character which ends the tag.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_blankz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a tag",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected whitespace or line break")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
end_mark := parser.mark
// Create a token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_TAG_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: handle,
suffix: suffix,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan a tag handle.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool {
2024-02-18 10:42:21 +00:00
// Check the initial '!' character.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] != '!' {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected '!'")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
var s []byte
// Copy the '!' character.
2024-02-18 10:42:21 +00:00
s = read(parser, s)
// Copy all subsequent alphabetical and numerical characters.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for is_alpha(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if the trailing character is '!' and copy it.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '!' {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// It's either the '!' tag or not really a tag handle. If it's a %TAG
2024-02-18 10:42:21 +00:00
// directive, it's an error. If it's a tag token, it must be a part of URI.
2024-02-18 10:42:21 +00:00
if directive && string(s) != "!" {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected '!'")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
*handle = s
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan a tag.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
2024-02-18 10:42:21 +00:00
//size_t length = head ? strlen((char *)head) : 0
2024-02-18 10:42:21 +00:00
var s []byte
2024-02-18 10:42:21 +00:00
hasTag := len(head) > 0
// Copy the head if needed.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that we don't copy the leading '!' character.
2024-02-18 10:42:21 +00:00
if len(head) > 1 {
2024-02-18 10:42:21 +00:00
s = append(s, head[1:]...)
2024-02-18 10:42:21 +00:00
}
// Scan the tag.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// The set of characters that may appear in URI is as follows:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2024-02-18 10:42:21 +00:00
// '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2024-02-18 10:42:21 +00:00
// '%'.
2024-02-18 10:42:21 +00:00
// [Go] TODO Convert this into more reasonable logic.
2024-02-18 10:42:21 +00:00
for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '%' {
2024-02-18 10:42:21 +00:00
// Check if it is a URI-escape sequence.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '%' {
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hasTag = true
2024-02-18 10:42:21 +00:00
}
if !hasTag {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected tag URI")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*uri = s
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Decode an URI-escape sequence corresponding to a single UTF-8 character.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool {
// Decode the required number of characters.
2024-02-18 10:42:21 +00:00
w := 1024
2024-02-18 10:42:21 +00:00
for w > 0 {
2024-02-18 10:42:21 +00:00
// Check for a URI-escaped octet.
2024-02-18 10:42:21 +00:00
if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
if !(parser.buffer[parser.buffer_pos] == '%' &&
2024-02-18 10:42:21 +00:00
is_hex(parser.buffer, parser.buffer_pos+1) &&
2024-02-18 10:42:21 +00:00
is_hex(parser.buffer, parser.buffer_pos+2)) {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "did not find URI escaped octet")
2024-02-18 10:42:21 +00:00
}
// Get the octet.
2024-02-18 10:42:21 +00:00
octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2))
// If it is the leading octet, determine the length of the UTF-8 sequence.
2024-02-18 10:42:21 +00:00
if w == 1024 {
2024-02-18 10:42:21 +00:00
w = width(octet)
2024-02-18 10:42:21 +00:00
if w == 0 {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "found an incorrect leading UTF-8 octet")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// Check if the trailing octet is correct.
2024-02-18 10:42:21 +00:00
if octet&0xC0 != 0x80 {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_tag_error(parser, directive,
2024-02-18 10:42:21 +00:00
start_mark, "found an incorrect trailing UTF-8 octet")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Copy the octet and move the pointers.
2024-02-18 10:42:21 +00:00
*s = append(*s, octet)
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
w--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan a block scalar.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool {
2024-02-18 10:42:21 +00:00
// Eat the indicator '|' or '>'.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
// Scan the additional block scalar indicators.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check for a chomping indicator.
2024-02-18 10:42:21 +00:00
var chomping, increment int
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2024-02-18 10:42:21 +00:00
// Set the chomping method and eat the indicator.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '+' {
2024-02-18 10:42:21 +00:00
chomping = +1
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
chomping = -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip(parser)
// Check for an indentation indicator.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_digit(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// Check that the indentation is greater than 0.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '0' {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found an indentation indicator equal to 0")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Get the indentation level and eat the indicator.
2024-02-18 10:42:21 +00:00
increment = as_digit(parser.buffer, parser.buffer_pos)
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
} else if is_digit(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// Do the same as above, but in the opposite order.
if parser.buffer[parser.buffer_pos] == '0' {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found an indentation indicator equal to 0")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
increment = as_digit(parser.buffer, parser.buffer_pos)
2024-02-18 10:42:21 +00:00
skip(parser)
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '+' {
2024-02-18 10:42:21 +00:00
chomping = +1
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
chomping = -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Eat whitespaces and comments to the end of the line.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '#' {
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_line_comment(parser, start_mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for !is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check if we are at the end of the line.
2024-02-18 10:42:21 +00:00
if !is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected comment or line break")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Eat a line break.
2024-02-18 10:42:21 +00:00
if is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip_line(parser)
2024-02-18 10:42:21 +00:00
}
end_mark := parser.mark
// Set the indentation level if it was specified.
2024-02-18 10:42:21 +00:00
var indent int
2024-02-18 10:42:21 +00:00
if increment > 0 {
2024-02-18 10:42:21 +00:00
if parser.indent >= 0 {
2024-02-18 10:42:21 +00:00
indent = parser.indent + increment
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
indent = increment
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Scan the leading line breaks and determine the indentation level if needed.
2024-02-18 10:42:21 +00:00
var s, leading_break, trailing_breaks []byte
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Scan the block scalar content.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var leading_blank, trailing_blank bool
2024-02-18 10:42:21 +00:00
for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// We are at the beginning of a non-empty line.
// Is it a trailing whitespace?
2024-02-18 10:42:21 +00:00
trailing_blank = is_blank(parser.buffer, parser.buffer_pos)
// Check if we need to fold the leading line break.
2024-02-18 10:42:21 +00:00
if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' {
2024-02-18 10:42:21 +00:00
// Do we need to join the lines by space?
2024-02-18 10:42:21 +00:00
if len(trailing_breaks) == 0 {
2024-02-18 10:42:21 +00:00
s = append(s, ' ')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, leading_break...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
leading_break = leading_break[:0]
// Append the remaining line breaks.
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
trailing_breaks = trailing_breaks[:0]
// Is it a leading whitespace?
2024-02-18 10:42:21 +00:00
leading_blank = is_blank(parser.buffer, parser.buffer_pos)
// Consume the current line.
2024-02-18 10:42:21 +00:00
for !is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Consume the line break.
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
leading_break = read_line(parser, leading_break)
// Eat the following indentation spaces and line breaks.
2024-02-18 10:42:21 +00:00
if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Chomp the tail.
2024-02-18 10:42:21 +00:00
if chomping != -1 {
2024-02-18 10:42:21 +00:00
s = append(s, leading_break...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if chomping == 1 {
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
}
// Create a token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_SCALAR_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: s,
style: yaml_LITERAL_SCALAR_STYLE,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !literal {
2024-02-18 10:42:21 +00:00
token.style = yaml_FOLDED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan indentation spaces and line breaks for a block scalar. Determine the
2024-02-18 10:42:21 +00:00
// indentation level if needed.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool {
2024-02-18 10:42:21 +00:00
*end_mark = parser.mark
// Eat the indentation spaces and line breaks.
2024-02-18 10:42:21 +00:00
max_indent := 0
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// Eat the indentation spaces.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.mark.column > max_indent {
2024-02-18 10:42:21 +00:00
max_indent = parser.mark.column
2024-02-18 10:42:21 +00:00
}
// Check for a tab character messing the indentation.
2024-02-18 10:42:21 +00:00
if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found a tab character where an indentation space is expected")
2024-02-18 10:42:21 +00:00
}
// Have we found a non-empty line?
2024-02-18 10:42:21 +00:00
if !is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Consume the line break.
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// [Go] Should really be returning breaks instead.
2024-02-18 10:42:21 +00:00
*breaks = read_line(parser, *breaks)
2024-02-18 10:42:21 +00:00
*end_mark = parser.mark
2024-02-18 10:42:21 +00:00
}
// Determine the indentation level if needed.
2024-02-18 10:42:21 +00:00
if *indent == 0 {
2024-02-18 10:42:21 +00:00
*indent = max_indent
2024-02-18 10:42:21 +00:00
if *indent < parser.indent+1 {
2024-02-18 10:42:21 +00:00
*indent = parser.indent + 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if *indent < 1 {
2024-02-18 10:42:21 +00:00
*indent = 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan a quoted scalar.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool {
2024-02-18 10:42:21 +00:00
// Eat the left quote.
2024-02-18 10:42:21 +00:00
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
skip(parser)
// Consume the content of the quoted scalar.
2024-02-18 10:42:21 +00:00
var s, leading_break, trailing_breaks, whitespaces []byte
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// Check that there are no document indicators at the beginning of the line.
2024-02-18 10:42:21 +00:00
if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
if parser.mark.column == 0 &&
2024-02-18 10:42:21 +00:00
((parser.buffer[parser.buffer_pos+0] == '-' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+1] == '-' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+2] == '-') ||
2024-02-18 10:42:21 +00:00
(parser.buffer[parser.buffer_pos+0] == '.' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+1] == '.' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+2] == '.')) &&
2024-02-18 10:42:21 +00:00
is_blankz(parser.buffer, parser.buffer_pos+3) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found unexpected document indicator")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check for EOF.
2024-02-18 10:42:21 +00:00
if is_z(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found unexpected end of stream")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Consume non-blank characters.
2024-02-18 10:42:21 +00:00
leading_blanks := false
2024-02-18 10:42:21 +00:00
for !is_blankz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' {
2024-02-18 10:42:21 +00:00
// Is is an escaped single quote.
2024-02-18 10:42:21 +00:00
s = append(s, '\'')
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
} else if single && parser.buffer[parser.buffer_pos] == '\'' {
2024-02-18 10:42:21 +00:00
// It is a right single quote.
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
} else if !single && parser.buffer[parser.buffer_pos] == '"' {
2024-02-18 10:42:21 +00:00
// It is a right double quote.
2024-02-18 10:42:21 +00:00
break
} else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) {
2024-02-18 10:42:21 +00:00
// It is an escaped line break.
2024-02-18 10:42:21 +00:00
if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
skip_line(parser)
2024-02-18 10:42:21 +00:00
leading_blanks = true
2024-02-18 10:42:21 +00:00
break
} else if !single && parser.buffer[parser.buffer_pos] == '\\' {
2024-02-18 10:42:21 +00:00
// It is an escape sequence.
2024-02-18 10:42:21 +00:00
code_length := 0
// Check the escape character.
2024-02-18 10:42:21 +00:00
switch parser.buffer[parser.buffer_pos+1] {
2024-02-18 10:42:21 +00:00
case '0':
2024-02-18 10:42:21 +00:00
s = append(s, 0)
2024-02-18 10:42:21 +00:00
case 'a':
2024-02-18 10:42:21 +00:00
s = append(s, '\x07')
2024-02-18 10:42:21 +00:00
case 'b':
2024-02-18 10:42:21 +00:00
s = append(s, '\x08')
2024-02-18 10:42:21 +00:00
case 't', '\t':
2024-02-18 10:42:21 +00:00
s = append(s, '\x09')
2024-02-18 10:42:21 +00:00
case 'n':
2024-02-18 10:42:21 +00:00
s = append(s, '\x0A')
2024-02-18 10:42:21 +00:00
case 'v':
2024-02-18 10:42:21 +00:00
s = append(s, '\x0B')
2024-02-18 10:42:21 +00:00
case 'f':
2024-02-18 10:42:21 +00:00
s = append(s, '\x0C')
2024-02-18 10:42:21 +00:00
case 'r':
2024-02-18 10:42:21 +00:00
s = append(s, '\x0D')
2024-02-18 10:42:21 +00:00
case 'e':
2024-02-18 10:42:21 +00:00
s = append(s, '\x1B')
2024-02-18 10:42:21 +00:00
case ' ':
2024-02-18 10:42:21 +00:00
s = append(s, '\x20')
2024-02-18 10:42:21 +00:00
case '"':
2024-02-18 10:42:21 +00:00
s = append(s, '"')
2024-02-18 10:42:21 +00:00
case '\'':
2024-02-18 10:42:21 +00:00
s = append(s, '\'')
2024-02-18 10:42:21 +00:00
case '\\':
2024-02-18 10:42:21 +00:00
s = append(s, '\\')
2024-02-18 10:42:21 +00:00
case 'N': // NEL (#x85)
2024-02-18 10:42:21 +00:00
s = append(s, '\xC2')
2024-02-18 10:42:21 +00:00
s = append(s, '\x85')
2024-02-18 10:42:21 +00:00
case '_': // #xA0
2024-02-18 10:42:21 +00:00
s = append(s, '\xC2')
2024-02-18 10:42:21 +00:00
s = append(s, '\xA0')
2024-02-18 10:42:21 +00:00
case 'L': // LS (#x2028)
2024-02-18 10:42:21 +00:00
s = append(s, '\xE2')
2024-02-18 10:42:21 +00:00
s = append(s, '\x80')
2024-02-18 10:42:21 +00:00
s = append(s, '\xA8')
2024-02-18 10:42:21 +00:00
case 'P': // PS (#x2029)
2024-02-18 10:42:21 +00:00
s = append(s, '\xE2')
2024-02-18 10:42:21 +00:00
s = append(s, '\x80')
2024-02-18 10:42:21 +00:00
s = append(s, '\xA9')
2024-02-18 10:42:21 +00:00
case 'x':
2024-02-18 10:42:21 +00:00
code_length = 2
2024-02-18 10:42:21 +00:00
case 'u':
2024-02-18 10:42:21 +00:00
code_length = 4
2024-02-18 10:42:21 +00:00
case 'U':
2024-02-18 10:42:21 +00:00
code_length = 8
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found unknown escape character")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
skip(parser)
2024-02-18 10:42:21 +00:00
skip(parser)
// Consume an arbitrary escape code.
2024-02-18 10:42:21 +00:00
if code_length > 0 {
2024-02-18 10:42:21 +00:00
var value int
// Scan the character value.
2024-02-18 10:42:21 +00:00
if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for k := 0; k < code_length; k++ {
2024-02-18 10:42:21 +00:00
if !is_hex(parser.buffer, parser.buffer_pos+k) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2024-02-18 10:42:21 +00:00
start_mark, "did not find expected hexdecimal number")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k)
2024-02-18 10:42:21 +00:00
}
// Check the value and write the character.
2024-02-18 10:42:21 +00:00
if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found invalid Unicode character escape code")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if value <= 0x7F {
2024-02-18 10:42:21 +00:00
s = append(s, byte(value))
2024-02-18 10:42:21 +00:00
} else if value <= 0x7FF {
2024-02-18 10:42:21 +00:00
s = append(s, byte(0xC0+(value>>6)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+(value&0x3F)))
2024-02-18 10:42:21 +00:00
} else if value <= 0xFFFF {
2024-02-18 10:42:21 +00:00
s = append(s, byte(0xE0+(value>>12)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+((value>>6)&0x3F)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+(value&0x3F)))
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, byte(0xF0+(value>>18)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+((value>>12)&0x3F)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+((value>>6)&0x3F)))
2024-02-18 10:42:21 +00:00
s = append(s, byte(0x80+(value&0x3F)))
2024-02-18 10:42:21 +00:00
}
// Advance the pointer.
2024-02-18 10:42:21 +00:00
for k := 0; k < code_length; k++ {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// It is a non-escaped non-blank character.
2024-02-18 10:42:21 +00:00
s = read(parser, s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check if we are at the end of the scalar.
2024-02-18 10:42:21 +00:00
if single {
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '\'' {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '"' {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Consume blank characters.
2024-02-18 10:42:21 +00:00
for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if is_blank(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
// Consume a space or a tab character.
2024-02-18 10:42:21 +00:00
if !leading_blanks {
2024-02-18 10:42:21 +00:00
whitespaces = read(parser, whitespaces)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check if it is a first line break.
2024-02-18 10:42:21 +00:00
if !leading_blanks {
2024-02-18 10:42:21 +00:00
whitespaces = whitespaces[:0]
2024-02-18 10:42:21 +00:00
leading_break = read_line(parser, leading_break)
2024-02-18 10:42:21 +00:00
leading_blanks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
trailing_breaks = read_line(parser, trailing_breaks)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Join the whitespaces or fold line breaks.
2024-02-18 10:42:21 +00:00
if leading_blanks {
2024-02-18 10:42:21 +00:00
// Do we need to fold line breaks?
2024-02-18 10:42:21 +00:00
if len(leading_break) > 0 && leading_break[0] == '\n' {
2024-02-18 10:42:21 +00:00
if len(trailing_breaks) == 0 {
2024-02-18 10:42:21 +00:00
s = append(s, ' ')
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, leading_break...)
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
trailing_breaks = trailing_breaks[:0]
2024-02-18 10:42:21 +00:00
leading_break = leading_break[:0]
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, whitespaces...)
2024-02-18 10:42:21 +00:00
whitespaces = whitespaces[:0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Eat the right quote.
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Create a token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_SCALAR_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: s,
style: yaml_SINGLE_QUOTED_SCALAR_STYLE,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !single {
2024-02-18 10:42:21 +00:00
token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Scan a plain scalar.
2024-02-18 10:42:21 +00:00
func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool {
var s, leading_break, trailing_breaks, whitespaces []byte
2024-02-18 10:42:21 +00:00
var leading_blanks bool
2024-02-18 10:42:21 +00:00
var indent = parser.indent + 1
start_mark := parser.mark
2024-02-18 10:42:21 +00:00
end_mark := parser.mark
// Consume the content of the plain scalar.
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// Check for a document indicator.
2024-02-18 10:42:21 +00:00
if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.mark.column == 0 &&
2024-02-18 10:42:21 +00:00
((parser.buffer[parser.buffer_pos+0] == '-' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+1] == '-' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+2] == '-') ||
2024-02-18 10:42:21 +00:00
(parser.buffer[parser.buffer_pos+0] == '.' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+1] == '.' &&
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos+2] == '.')) &&
2024-02-18 10:42:21 +00:00
is_blankz(parser.buffer, parser.buffer_pos+3) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Check for a comment.
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos] == '#' {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Consume non-blank characters.
2024-02-18 10:42:21 +00:00
for !is_blankz(parser.buffer, parser.buffer_pos) {
// Check for indicators that may end a plain scalar.
2024-02-18 10:42:21 +00:00
if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) ||
2024-02-18 10:42:21 +00:00
(parser.flow_level > 0 &&
2024-02-18 10:42:21 +00:00
(parser.buffer[parser.buffer_pos] == ',' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
2024-02-18 10:42:21 +00:00
parser.buffer[parser.buffer_pos] == '}')) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Check if we need to join whitespaces and breaks.
2024-02-18 10:42:21 +00:00
if leading_blanks || len(whitespaces) > 0 {
2024-02-18 10:42:21 +00:00
if leading_blanks {
2024-02-18 10:42:21 +00:00
// Do we need to fold line breaks?
2024-02-18 10:42:21 +00:00
if leading_break[0] == '\n' {
2024-02-18 10:42:21 +00:00
if len(trailing_breaks) == 0 {
2024-02-18 10:42:21 +00:00
s = append(s, ' ')
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, leading_break...)
2024-02-18 10:42:21 +00:00
s = append(s, trailing_breaks...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
trailing_breaks = trailing_breaks[:0]
2024-02-18 10:42:21 +00:00
leading_break = leading_break[:0]
2024-02-18 10:42:21 +00:00
leading_blanks = false
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = append(s, whitespaces...)
2024-02-18 10:42:21 +00:00
whitespaces = whitespaces[:0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Copy the character.
2024-02-18 10:42:21 +00:00
s = read(parser, s)
end_mark = parser.mark
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Is it the end?
2024-02-18 10:42:21 +00:00
if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Consume blank characters.
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if is_blank(parser.buffer, parser.buffer_pos) {
// Check for tab characters that abuse indentation.
2024-02-18 10:42:21 +00:00
if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
2024-02-18 10:42:21 +00:00
start_mark, "found a tab character that violates indentation")
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Consume a space or a tab character.
2024-02-18 10:42:21 +00:00
if !leading_blanks {
2024-02-18 10:42:21 +00:00
whitespaces = read(parser, whitespaces)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Check if it is a first line break.
2024-02-18 10:42:21 +00:00
if !leading_blanks {
2024-02-18 10:42:21 +00:00
whitespaces = whitespaces[:0]
2024-02-18 10:42:21 +00:00
leading_break = read_line(parser, leading_break)
2024-02-18 10:42:21 +00:00
leading_blanks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
trailing_breaks = read_line(parser, trailing_breaks)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Check indentation level.
2024-02-18 10:42:21 +00:00
if parser.flow_level == 0 && parser.mark.column < indent {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Create a token.
2024-02-18 10:42:21 +00:00
*token = yaml_token_t{
typ: yaml_SCALAR_TOKEN,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: end_mark,
value: s,
style: yaml_PLAIN_SCALAR_STYLE,
2024-02-18 10:42:21 +00:00
}
// Note that we change the 'simple_key_allowed' flag.
2024-02-18 10:42:21 +00:00
if leading_blanks {
2024-02-18 10:42:21 +00:00
parser.simple_key_allowed = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_parser_scan_line_comment(parser *yaml_parser_t, token_mark yaml_mark_t) bool {
2024-02-18 10:42:21 +00:00
if parser.newlines > 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
var start_mark yaml_mark_t
2024-02-18 10:42:21 +00:00
var text []byte
for peek := 0; peek < 512; peek++ {
2024-02-18 10:42:21 +00:00
if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_blank(parser.buffer, parser.buffer_pos+peek) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.buffer[parser.buffer_pos+peek] == '#' {
2024-06-14 08:41:36 +00:00
seen := parser.mark.index + peek
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if parser.mark.index >= seen {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip_line(parser)
2024-02-18 10:42:21 +00:00
} else if parser.mark.index >= seen {
2024-02-18 10:42:21 +00:00
if len(text) == 0 {
2024-02-18 10:42:21 +00:00
start_mark = parser.mark
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
text = read(parser, text)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(text) > 0 {
2024-02-18 10:42:21 +00:00
parser.comments = append(parser.comments, yaml_comment_t{
2024-02-18 10:42:21 +00:00
token_mark: token_mark,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
line: text,
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_parser_scan_comments(parser *yaml_parser_t, scan_mark yaml_mark_t) bool {
2024-02-18 10:42:21 +00:00
token := parser.tokens[len(parser.tokens)-1]
if token.typ == yaml_FLOW_ENTRY_TOKEN && len(parser.tokens) > 1 {
2024-02-18 10:42:21 +00:00
token = parser.tokens[len(parser.tokens)-2]
2024-02-18 10:42:21 +00:00
}
var token_mark = token.start_mark
2024-02-18 10:42:21 +00:00
var start_mark yaml_mark_t
2024-02-18 10:42:21 +00:00
var next_indent = parser.indent
2024-02-18 10:42:21 +00:00
if next_indent < 0 {
2024-02-18 10:42:21 +00:00
next_indent = 0
2024-02-18 10:42:21 +00:00
}
var recent_empty = false
2024-02-18 10:42:21 +00:00
var first_empty = parser.newlines <= 1
var line = parser.mark.line
2024-02-18 10:42:21 +00:00
var column = parser.mark.column
var text []byte
// The foot line is the place where a comment must start to
2024-02-18 10:42:21 +00:00
// still be considered as a foot of the prior content.
2024-02-18 10:42:21 +00:00
// If there's some content in the currently parsed line, then
2024-02-18 10:42:21 +00:00
// the foot is the line below it.
2024-02-18 10:42:21 +00:00
var foot_line = -1
2024-02-18 10:42:21 +00:00
if scan_mark.line > 0 {
2024-06-14 08:41:36 +00:00
foot_line = parser.mark.line - parser.newlines + 1
2024-02-18 10:42:21 +00:00
if parser.newlines == 0 && parser.mark.column > 1 {
2024-02-18 10:42:21 +00:00
foot_line++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
var peek = 0
2024-02-18 10:42:21 +00:00
for ; peek < 512; peek++ {
2024-02-18 10:42:21 +00:00
if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
column++
2024-02-18 10:42:21 +00:00
if is_blank(parser.buffer, parser.buffer_pos+peek) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c := parser.buffer[parser.buffer_pos+peek]
2024-02-18 10:42:21 +00:00
var close_flow = parser.flow_level > 0 && (c == ']' || c == '}')
2024-02-18 10:42:21 +00:00
if close_flow || is_breakz(parser.buffer, parser.buffer_pos+peek) {
2024-02-18 10:42:21 +00:00
// Got line break or terminator.
2024-02-18 10:42:21 +00:00
if close_flow || !recent_empty {
2024-02-18 10:42:21 +00:00
if close_flow || first_empty && (start_mark.line == foot_line && token.typ != yaml_VALUE_TOKEN || start_mark.column-1 < next_indent) {
2024-02-18 10:42:21 +00:00
// This is the first empty line and there were no empty lines before,
2024-02-18 10:42:21 +00:00
// so this initial part of the comment is a foot of the prior token
2024-02-18 10:42:21 +00:00
// instead of being a head for the following one. Split it up.
2024-02-18 10:42:21 +00:00
// Alternatively, this might also be the last comment inside a flow
2024-02-18 10:42:21 +00:00
// scope, so it must be a footer.
2024-02-18 10:42:21 +00:00
if len(text) > 0 {
2024-02-18 10:42:21 +00:00
if start_mark.column-1 < next_indent {
2024-02-18 10:42:21 +00:00
// If dedented it's unrelated to the prior token.
2024-02-18 10:42:21 +00:00
token_mark = start_mark
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
parser.comments = append(parser.comments, yaml_comment_t{
scan_mark: scan_mark,
2024-02-18 10:42:21 +00:00
token_mark: token_mark,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: yaml_mark_t{parser.mark.index + peek, line, column},
foot: text,
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
scan_mark = yaml_mark_t{parser.mark.index + peek, line, column}
2024-02-18 10:42:21 +00:00
token_mark = scan_mark
2024-02-18 10:42:21 +00:00
text = nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if len(text) > 0 && parser.buffer[parser.buffer_pos+peek] != 0 {
2024-02-18 10:42:21 +00:00
text = append(text, '\n')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_break(parser.buffer, parser.buffer_pos+peek) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
first_empty = false
2024-02-18 10:42:21 +00:00
recent_empty = true
2024-02-18 10:42:21 +00:00
column = 0
2024-02-18 10:42:21 +00:00
line++
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
if len(text) > 0 && (close_flow || column-1 < next_indent && column != start_mark.column) {
2024-02-18 10:42:21 +00:00
// The comment at the different indentation is a foot of the
2024-02-18 10:42:21 +00:00
// preceding data rather than a head of the upcoming one.
2024-02-18 10:42:21 +00:00
parser.comments = append(parser.comments, yaml_comment_t{
scan_mark: scan_mark,
2024-02-18 10:42:21 +00:00
token_mark: token_mark,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: yaml_mark_t{parser.mark.index + peek, line, column},
foot: text,
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
scan_mark = yaml_mark_t{parser.mark.index + peek, line, column}
2024-02-18 10:42:21 +00:00
token_mark = scan_mark
2024-02-18 10:42:21 +00:00
text = nil
2024-02-18 10:42:21 +00:00
}
if parser.buffer[parser.buffer_pos+peek] != '#' {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
if len(text) == 0 {
2024-02-18 10:42:21 +00:00
start_mark = yaml_mark_t{parser.mark.index + peek, line, column}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
text = append(text, '\n')
2024-02-18 10:42:21 +00:00
}
recent_empty = false
// Consume until after the consumed comment line.
2024-06-14 08:41:36 +00:00
seen := parser.mark.index + peek
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_breakz(parser.buffer, parser.buffer_pos) {
2024-02-18 10:42:21 +00:00
if parser.mark.index >= seen {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
skip_line(parser)
2024-02-18 10:42:21 +00:00
} else if parser.mark.index >= seen {
2024-02-18 10:42:21 +00:00
text = read(parser, text)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
skip(parser)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
peek = 0
2024-02-18 10:42:21 +00:00
column = 0
2024-02-18 10:42:21 +00:00
line = parser.mark.line
2024-02-18 10:42:21 +00:00
next_indent = parser.indent
2024-02-18 10:42:21 +00:00
if next_indent < 0 {
2024-02-18 10:42:21 +00:00
next_indent = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(text) > 0 {
2024-02-18 10:42:21 +00:00
parser.comments = append(parser.comments, yaml_comment_t{
scan_mark: scan_mark,
2024-02-18 10:42:21 +00:00
token_mark: start_mark,
2024-02-18 10:42:21 +00:00
start_mark: start_mark,
end_mark: yaml_mark_t{parser.mark.index + peek - 1, line, column},
head: text,
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}