//
// Copyright (c) 2011-2019 Canonical Ltd
// Copyright (c) 2006-2010 Kirill Simonov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package yaml

import (
	
	
)

// Introduction
// ************
//
// The following notes assume that you are familiar with the YAML specification
// (http://yaml.org/spec/1.2/spec.html).  We mostly follow it, although in
// some cases we are less restrictive that it requires.
//
// The process of transforming a YAML stream into a sequence of events is
// divided on two steps: Scanning and Parsing.
//
// The Scanner transforms the input stream into a sequence of tokens, while the
// parser transform the sequence of tokens produced by the Scanner into a
// sequence of parsing events.
//
// The Scanner is rather clever and complicated. The Parser, on the contrary,
// is a straightforward implementation of a recursive-descendant parser (or,
// LL(1) parser, as it is usually called).
//
// Actually there are two issues of Scanning that might be called "clever", the
// rest is quite straightforward.  The issues are "block collection start" and
// "simple keys".  Both issues are explained below in details.
//
// Here the Scanning step is explained and implemented.  We start with the list
// of all the tokens produced by the Scanner together with short descriptions.
//
// Now, tokens:
//
//      STREAM-START(encoding)          # The stream start.
//      STREAM-END                      # The stream end.
//      VERSION-DIRECTIVE(major,minor)  # The '%YAML' directive.
//      TAG-DIRECTIVE(handle,prefix)    # The '%TAG' directive.
//      DOCUMENT-START                  # '---'
//      DOCUMENT-END                    # '...'
//      BLOCK-SEQUENCE-START            # Indentation increase denoting a block
//      BLOCK-MAPPING-START             # sequence or a block mapping.
//      BLOCK-END                       # Indentation decrease.
//      FLOW-SEQUENCE-START             # '['
//      FLOW-SEQUENCE-END               # ']'
//      BLOCK-SEQUENCE-START            # '{'
//      BLOCK-SEQUENCE-END              # '}'
//      BLOCK-ENTRY                     # '-'
//      FLOW-ENTRY                      # ','
//      KEY                             # '?' or nothing (simple keys).
//      VALUE                           # ':'
//      ALIAS(anchor)                   # '*anchor'
//      ANCHOR(anchor)                  # '&anchor'
//      TAG(handle,suffix)              # '!handle!suffix'
//      SCALAR(value,style)             # A scalar.
//
// The following two tokens are "virtual" tokens denoting the beginning and the
// end of the stream:
//
//      STREAM-START(encoding)
//      STREAM-END
//
// We pass the information about the input stream encoding with the
// STREAM-START token.
//
// The next two tokens are responsible for tags:
//
//      VERSION-DIRECTIVE(major,minor)
//      TAG-DIRECTIVE(handle,prefix)
//
// Example:
//
//      %YAML   1.1
//      %TAG    !   !foo
//      %TAG    !yaml!  tag:yaml.org,2002:
//      ---
//
// The correspoding sequence of tokens:
//
//      STREAM-START(utf-8)
//      VERSION-DIRECTIVE(1,1)
//      TAG-DIRECTIVE("!","!foo")
//      TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
//      DOCUMENT-START
//      STREAM-END
//
// Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
// line.
//
// The document start and end indicators are represented by:
//
//      DOCUMENT-START
//      DOCUMENT-END
//
// Note that if a YAML stream contains an implicit document (without '---'
// and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
// produced.
//
// In the following examples, we present whole documents together with the
// produced tokens.
//
//      1. An implicit document:
//
//          'a scalar'
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          SCALAR("a scalar",single-quoted)
//          STREAM-END
//
//      2. An explicit document:
//
//          ---
//          'a scalar'
//          ...
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          DOCUMENT-START
//          SCALAR("a scalar",single-quoted)
//          DOCUMENT-END
//          STREAM-END
//
//      3. Several documents in a stream:
//
//          'a scalar'
//          ---
//          'another scalar'
//          ---
//          'yet another scalar'
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          SCALAR("a scalar",single-quoted)
//          DOCUMENT-START
//          SCALAR("another scalar",single-quoted)
//          DOCUMENT-START
//          SCALAR("yet another scalar",single-quoted)
//          STREAM-END
//
// We have already introduced the SCALAR token above.  The following tokens are
// used to describe aliases, anchors, tag, and scalars:
//
//      ALIAS(anchor)
//      ANCHOR(anchor)
//      TAG(handle,suffix)
//      SCALAR(value,style)
//
// The following series of examples illustrate the usage of these tokens:
//
//      1. A recursive sequence:
//
//          &A [ *A ]
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          ANCHOR("A")
//          FLOW-SEQUENCE-START
//          ALIAS("A")
//          FLOW-SEQUENCE-END
//          STREAM-END
//
//      2. A tagged scalar:
//
//          !!float "3.14"  # A good approximation.
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          TAG("!!","float")
//          SCALAR("3.14",double-quoted)
//          STREAM-END
//
//      3. Various scalar styles:
//
//          --- # Implicit empty plain scalars do not produce tokens.
//          --- a plain scalar
//          --- 'a single-quoted scalar'
//          --- "a double-quoted scalar"
//          --- |-
//            a literal scalar
//          --- >-
//            a folded
//            scalar
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          DOCUMENT-START
//          DOCUMENT-START
//          SCALAR("a plain scalar",plain)
//          DOCUMENT-START
//          SCALAR("a single-quoted scalar",single-quoted)
//          DOCUMENT-START
//          SCALAR("a double-quoted scalar",double-quoted)
//          DOCUMENT-START
//          SCALAR("a literal scalar",literal)
//          DOCUMENT-START
//          SCALAR("a folded scalar",folded)
//          STREAM-END
//
// Now it's time to review collection-related tokens. We will start with
// flow collections:
//
//      FLOW-SEQUENCE-START
//      FLOW-SEQUENCE-END
//      FLOW-MAPPING-START
//      FLOW-MAPPING-END
//      FLOW-ENTRY
//      KEY
//      VALUE
//
// The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
// FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
// correspondingly.  FLOW-ENTRY represent the ',' indicator.  Finally the
// indicators '?' and ':', which are used for denoting mapping keys and values,
// are represented by the KEY and VALUE tokens.
//
// The following examples show flow collections:
//
//      1. A flow sequence:
//
//          [item 1, item 2, item 3]
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          FLOW-SEQUENCE-START
//          SCALAR("item 1",plain)
//          FLOW-ENTRY
//          SCALAR("item 2",plain)
//          FLOW-ENTRY
//          SCALAR("item 3",plain)
//          FLOW-SEQUENCE-END
//          STREAM-END
//
//      2. A flow mapping:
//
//          {
//              a simple key: a value,  # Note that the KEY token is produced.
//              ? a complex key: another value,
//          }
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          FLOW-MAPPING-START
//          KEY
//          SCALAR("a simple key",plain)
//          VALUE
//          SCALAR("a value",plain)
//          FLOW-ENTRY
//          KEY
//          SCALAR("a complex key",plain)
//          VALUE
//          SCALAR("another value",plain)
//          FLOW-ENTRY
//          FLOW-MAPPING-END
//          STREAM-END
//
// A simple key is a key which is not denoted by the '?' indicator.  Note that
// the Scanner still produce the KEY token whenever it encounters a simple key.
//
// For scanning block collections, the following tokens are used (note that we
// repeat KEY and VALUE here):
//
//      BLOCK-SEQUENCE-START
//      BLOCK-MAPPING-START
//      BLOCK-END
//      BLOCK-ENTRY
//      KEY
//      VALUE
//
// The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
// increase that precedes a block collection (cf. the INDENT token in Python).
// The token BLOCK-END denote indentation decrease that ends a block collection
// (cf. the DEDENT token in Python).  However YAML has some syntax pecularities
// that makes detections of these tokens more complex.
//
// The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
// '-', '?', and ':' correspondingly.
//
// The following examples show how the tokens BLOCK-SEQUENCE-START,
// BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
//
//      1. Block sequences:
//
//          - item 1
//          - item 2
//          -
//            - item 3.1
//            - item 3.2
//          -
//            key 1: value 1
//            key 2: value 2
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          SCALAR("item 1",plain)
//          BLOCK-ENTRY
//          SCALAR("item 2",plain)
//          BLOCK-ENTRY
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          SCALAR("item 3.1",plain)
//          BLOCK-ENTRY
//          SCALAR("item 3.2",plain)
//          BLOCK-END
//          BLOCK-ENTRY
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("key 1",plain)
//          VALUE
//          SCALAR("value 1",plain)
//          KEY
//          SCALAR("key 2",plain)
//          VALUE
//          SCALAR("value 2",plain)
//          BLOCK-END
//          BLOCK-END
//          STREAM-END
//
//      2. Block mappings:
//
//          a simple key: a value   # The KEY token is produced here.
//          ? a complex key
//          : another value
//          a mapping:
//            key 1: value 1
//            key 2: value 2
//          a sequence:
//            - item 1
//            - item 2
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("a simple key",plain)
//          VALUE
//          SCALAR("a value",plain)
//          KEY
//          SCALAR("a complex key",plain)
//          VALUE
//          SCALAR("another value",plain)
//          KEY
//          SCALAR("a mapping",plain)
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("key 1",plain)
//          VALUE
//          SCALAR("value 1",plain)
//          KEY
//          SCALAR("key 2",plain)
//          VALUE
//          SCALAR("value 2",plain)
//          BLOCK-END
//          KEY
//          SCALAR("a sequence",plain)
//          VALUE
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          SCALAR("item 1",plain)
//          BLOCK-ENTRY
//          SCALAR("item 2",plain)
//          BLOCK-END
//          BLOCK-END
//          STREAM-END
//
// YAML does not always require to start a new block collection from a new
// line.  If the current line contains only '-', '?', and ':' indicators, a new
// block collection may start at the current line.  The following examples
// illustrate this case:
//
//      1. Collections in a sequence:
//
//          - - item 1
//            - item 2
//          - key 1: value 1
//            key 2: value 2
//          - ? complex key
//            : complex value
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          SCALAR("item 1",plain)
//          BLOCK-ENTRY
//          SCALAR("item 2",plain)
//          BLOCK-END
//          BLOCK-ENTRY
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("key 1",plain)
//          VALUE
//          SCALAR("value 1",plain)
//          KEY
//          SCALAR("key 2",plain)
//          VALUE
//          SCALAR("value 2",plain)
//          BLOCK-END
//          BLOCK-ENTRY
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("complex key")
//          VALUE
//          SCALAR("complex value")
//          BLOCK-END
//          BLOCK-END
//          STREAM-END
//
//      2. Collections in a mapping:
//
//          ? a sequence
//          : - item 1
//            - item 2
//          ? a mapping
//          : key 1: value 1
//            key 2: value 2
//
//      Tokens:
//
//          STREAM-START(utf-8)
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("a sequence",plain)
//          VALUE
//          BLOCK-SEQUENCE-START
//          BLOCK-ENTRY
//          SCALAR("item 1",plain)
//          BLOCK-ENTRY
//          SCALAR("item 2",plain)
//          BLOCK-END
//          KEY
//          SCALAR("a mapping",plain)
//          VALUE
//          BLOCK-MAPPING-START
//          KEY
//          SCALAR("key 1",plain)
//          VALUE
//          SCALAR("value 1",plain)
//          KEY
//          SCALAR("key 2",plain)
//          VALUE
//          SCALAR("value 2",plain)
//          BLOCK-END
//          BLOCK-END
//          STREAM-END
//
// YAML also permits non-indented sequences if they are included into a block
// mapping.  In this case, the token BLOCK-SEQUENCE-START is not produced:
//
//      key:
//      - item 1    # BLOCK-SEQUENCE-START is NOT produced here.
//      - item 2
//
// Tokens:
//
//      STREAM-START(utf-8)
//      BLOCK-MAPPING-START
//      KEY
//      SCALAR("key",plain)
//      VALUE
//      BLOCK-ENTRY
//      SCALAR("item 1",plain)
//      BLOCK-ENTRY
//      SCALAR("item 2",plain)
//      BLOCK-END
//

// Ensure that the buffer contains the required number of characters.
// Return true on success, false on failure (reader error or memory error).
func cache( *yaml_parser_t,  int) bool {
	// [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B)
	return .unread >=  || yaml_parser_update_buffer(, )
}

// Advance the buffer pointer.
func skip( *yaml_parser_t) {
	if !is_blank(.buffer, .buffer_pos) {
		.newlines = 0
	}
	.mark.index++
	.mark.column++
	.unread--
	.buffer_pos += width(.buffer[.buffer_pos])
}

func skip_line( *yaml_parser_t) {
	if is_crlf(.buffer, .buffer_pos) {
		.mark.index += 2
		.mark.column = 0
		.mark.line++
		.unread -= 2
		.buffer_pos += 2
		.newlines++
	} else if is_break(.buffer, .buffer_pos) {
		.mark.index++
		.mark.column = 0
		.mark.line++
		.unread--
		.buffer_pos += width(.buffer[.buffer_pos])
		.newlines++
	}
}

// Copy a character to a string buffer and advance pointers.
func read( *yaml_parser_t,  []byte) []byte {
	if !is_blank(.buffer, .buffer_pos) {
		.newlines = 0
	}
	 := width(.buffer[.buffer_pos])
	if  == 0 {
		panic("invalid character sequence")
	}
	if len() == 0 {
		 = make([]byte, 0, 32)
	}
	if  == 1 && len()+ <= cap() {
		 = [:len()+1]
		[len()-1] = .buffer[.buffer_pos]
		.buffer_pos++
	} else {
		 = append(, .buffer[.buffer_pos:.buffer_pos+]...)
		.buffer_pos += 
	}
	.mark.index++
	.mark.column++
	.unread--
	return 
}

// Copy a line break character to a string buffer and advance pointers.
func read_line( *yaml_parser_t,  []byte) []byte {
	 := .buffer
	 := .buffer_pos
	switch {
	case [] == '\r' && [+1] == '\n':
		// CR LF . LF
		 = append(, '\n')
		.buffer_pos += 2
		.mark.index++
		.unread--
	case [] == '\r' || [] == '\n':
		// CR|LF . LF
		 = append(, '\n')
		.buffer_pos += 1
	case [] == '\xC2' && [+1] == '\x85':
		// NEL . LF
		 = append(, '\n')
		.buffer_pos += 2
	case [] == '\xE2' && [+1] == '\x80' && ([+2] == '\xA8' || [+2] == '\xA9'):
		// LS|PS . LS|PS
		 = append(, [.buffer_pos:+3]...)
		.buffer_pos += 3
	default:
		return 
	}
	.mark.index++
	.mark.column = 0
	.mark.line++
	.unread--
	.newlines++
	return 
}

// Get the next token.
func yaml_parser_scan( *yaml_parser_t,  *yaml_token_t) bool {
	// Erase the token object.
	* = yaml_token_t{} // [Go] Is this necessary?

	// No tokens after STREAM-END or error.
	if .stream_end_produced || .error != yaml_NO_ERROR {
		return true
	}

	// Ensure that the tokens queue contains enough tokens.
	if !.token_available {
		if !yaml_parser_fetch_more_tokens() {
			return false
		}
	}

	// Fetch the next token from the queue.
	* = .tokens[.tokens_head]
	.tokens_head++
	.tokens_parsed++
	.token_available = false

	if .typ == yaml_STREAM_END_TOKEN {
		.stream_end_produced = true
	}
	return true
}

// Set the scanner error and return false.
func yaml_parser_set_scanner_error( *yaml_parser_t,  string,  yaml_mark_t,  string) bool {
	.error = yaml_SCANNER_ERROR
	.context = 
	.context_mark = 
	.problem = 
	.problem_mark = .mark
	return false
}

func yaml_parser_set_scanner_tag_error( *yaml_parser_t,  bool,  yaml_mark_t,  string) bool {
	 := "while parsing a tag"
	if  {
		 = "while parsing a %TAG directive"
	}
	return yaml_parser_set_scanner_error(, , , )
}

func trace( ...interface{}) func() {
	 := append([]interface{}{"+++"}, ...)
	fmt.Println(...)
	 = append([]interface{}{"---"}, ...)
	return func() { fmt.Println(...) }
}

// Ensure that the tokens queue contains at least one token which can be
// returned to the Parser.
func yaml_parser_fetch_more_tokens( *yaml_parser_t) bool {
	// While we need more tokens to fetch, do it.
	for {
		// [Go] The comment parsing logic requires a lookahead of two tokens
		// so that foot comments may be parsed in time of associating them
		// with the tokens that are parsed before them, and also for line
		// comments to be transformed into head comments in some edge cases.
		if .tokens_head < len(.tokens)-2 {
			// If a potential simple key is at the head position, we need to fetch
			// the next token to disambiguate it.
			,  := .simple_keys_by_tok[.tokens_parsed]
			if ! {
				break
			} else if ,  := yaml_simple_key_is_valid(, &.simple_keys[]); ! {
				return false
			} else if ! {
				break
			}
		}
		// Fetch the next token.
		if !yaml_parser_fetch_next_token() {
			return false
		}
	}

	.token_available = true
	return true
}

// The dispatcher for token fetchers.
func yaml_parser_fetch_next_token( *yaml_parser_t) ( bool) {
	// Ensure that the buffer is initialized.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	// Check if we just started scanning.  Fetch STREAM-START then.
	if !.stream_start_produced {
		return yaml_parser_fetch_stream_start()
	}

	 := .mark

	// Eat whitespaces and comments until we reach the next token.
	if !yaml_parser_scan_to_next_token() {
		return false
	}

	// [Go] While unrolling indents, transform the head comments of prior
	// indentation levels observed after scan_start into foot comments at
	// the respective indexes.

	// Check the indentation level against the current column.
	if !yaml_parser_unroll_indent(, .mark.column, ) {
		return false
	}

	// Ensure that the buffer contains at least 4 characters.  4 is the length
	// of the longest indicators ('--- ' and '... ').
	if .unread < 4 && !yaml_parser_update_buffer(, 4) {
		return false
	}

	// Is it the end of the stream?
	if is_z(.buffer, .buffer_pos) {
		return yaml_parser_fetch_stream_end()
	}

	// Is it a directive?
	if .mark.column == 0 && .buffer[.buffer_pos] == '%' {
		return yaml_parser_fetch_directive()
	}

	 := .buffer
	 := .buffer_pos

	// Is it the document start indicator?
	if .mark.column == 0 && [] == '-' && [+1] == '-' && [+2] == '-' && is_blankz(, +3) {
		return yaml_parser_fetch_document_indicator(, yaml_DOCUMENT_START_TOKEN)
	}

	// Is it the document end indicator?
	if .mark.column == 0 && [] == '.' && [+1] == '.' && [+2] == '.' && is_blankz(, +3) {
		return yaml_parser_fetch_document_indicator(, yaml_DOCUMENT_END_TOKEN)
	}

	 := .mark
	if len(.tokens) > 0 && (.flow_level == 0 && [] == ':' || .flow_level > 0 && [] == ',') {
		// Associate any following comments with the prior token.
		 = .tokens[len(.tokens)-1].start_mark
	}
	defer func() {
		if ! {
			return
		}
		if len(.tokens) > 0 && .tokens[len(.tokens)-1].typ == yaml_BLOCK_ENTRY_TOKEN {
			// Sequence indicators alone have no line comments. It becomes
			// a head comment for whatever follows.
			return
		}
		if !yaml_parser_scan_line_comment(, ) {
			 = false
			return
		}
	}()

	// Is it the flow sequence start indicator?
	if [] == '[' {
		return yaml_parser_fetch_flow_collection_start(, yaml_FLOW_SEQUENCE_START_TOKEN)
	}

	// Is it the flow mapping start indicator?
	if .buffer[.buffer_pos] == '{' {
		return yaml_parser_fetch_flow_collection_start(, yaml_FLOW_MAPPING_START_TOKEN)
	}

	// Is it the flow sequence end indicator?
	if .buffer[.buffer_pos] == ']' {
		return yaml_parser_fetch_flow_collection_end(,
			yaml_FLOW_SEQUENCE_END_TOKEN)
	}

	// Is it the flow mapping end indicator?
	if .buffer[.buffer_pos] == '}' {
		return yaml_parser_fetch_flow_collection_end(,
			yaml_FLOW_MAPPING_END_TOKEN)
	}

	// Is it the flow entry indicator?
	if .buffer[.buffer_pos] == ',' {
		return yaml_parser_fetch_flow_entry()
	}

	// Is it the block entry indicator?
	if .buffer[.buffer_pos] == '-' && is_blankz(.buffer, .buffer_pos+1) {
		return yaml_parser_fetch_block_entry()
	}

	// Is it the key indicator?
	if .buffer[.buffer_pos] == '?' && (.flow_level > 0 || is_blankz(.buffer, .buffer_pos+1)) {
		return yaml_parser_fetch_key()
	}

	// Is it the value indicator?
	if .buffer[.buffer_pos] == ':' && (.flow_level > 0 || is_blankz(.buffer, .buffer_pos+1)) {
		return yaml_parser_fetch_value()
	}

	// Is it an alias?
	if .buffer[.buffer_pos] == '*' {
		return yaml_parser_fetch_anchor(, yaml_ALIAS_TOKEN)
	}

	// Is it an anchor?
	if .buffer[.buffer_pos] == '&' {
		return yaml_parser_fetch_anchor(, yaml_ANCHOR_TOKEN)
	}

	// Is it a tag?
	if .buffer[.buffer_pos] == '!' {
		return yaml_parser_fetch_tag()
	}

	// Is it a literal scalar?
	if .buffer[.buffer_pos] == '|' && .flow_level == 0 {
		return yaml_parser_fetch_block_scalar(, true)
	}

	// Is it a folded scalar?
	if .buffer[.buffer_pos] == '>' && .flow_level == 0 {
		return yaml_parser_fetch_block_scalar(, false)
	}

	// Is it a single-quoted scalar?
	if .buffer[.buffer_pos] == '\'' {
		return yaml_parser_fetch_flow_scalar(, true)
	}

	// Is it a double-quoted scalar?
	if .buffer[.buffer_pos] == '"' {
		return yaml_parser_fetch_flow_scalar(, false)
	}

	// Is it a plain scalar?
	//
	// A plain scalar may start with any non-blank characters except
	//
	//      '-', '?', ':', ',', '[', ']', '{', '}',
	//      '#', '&', '*', '!', '|', '>', '\'', '\"',
	//      '%', '@', '`'.
	//
	// In the block context (and, for the '-' indicator, in the flow context
	// too), it may also start with the characters
	//
	//      '-', '?', ':'
	//
	// if it is followed by a non-space character.
	//
	// The last rule is more restrictive than the specification requires.
	// [Go] TODO Make this logic more reasonable.
	//switch parser.buffer[parser.buffer_pos] {
	//case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
	//}
	if !(is_blankz(.buffer, .buffer_pos) || .buffer[.buffer_pos] == '-' ||
		.buffer[.buffer_pos] == '?' || .buffer[.buffer_pos] == ':' ||
		.buffer[.buffer_pos] == ',' || .buffer[.buffer_pos] == '[' ||
		.buffer[.buffer_pos] == ']' || .buffer[.buffer_pos] == '{' ||
		.buffer[.buffer_pos] == '}' || .buffer[.buffer_pos] == '#' ||
		.buffer[.buffer_pos] == '&' || .buffer[.buffer_pos] == '*' ||
		.buffer[.buffer_pos] == '!' || .buffer[.buffer_pos] == '|' ||
		.buffer[.buffer_pos] == '>' || .buffer[.buffer_pos] == '\'' ||
		.buffer[.buffer_pos] == '"' || .buffer[.buffer_pos] == '%' ||
		.buffer[.buffer_pos] == '@' || .buffer[.buffer_pos] == '`') ||
		(.buffer[.buffer_pos] == '-' && !is_blank(.buffer, .buffer_pos+1)) ||
		(.flow_level == 0 &&
			(.buffer[.buffer_pos] == '?' || .buffer[.buffer_pos] == ':') &&
			!is_blankz(.buffer, .buffer_pos+1)) {
		return yaml_parser_fetch_plain_scalar()
	}

	// If we don't determine the token type so far, it is an error.
	return yaml_parser_set_scanner_error(,
		"while scanning for the next token", .mark,
		"found character that cannot start any token")
}

func yaml_simple_key_is_valid( *yaml_parser_t,  *yaml_simple_key_t) (,  bool) {
	if !.possible {
		return false, true
	}

	// The 1.2 specification says:
	//
	//     "If the ? indicator is omitted, parsing needs to see past the
	//     implicit key to recognize it as such. To limit the amount of
	//     lookahead required, the “:” indicator must appear at most 1024
	//     Unicode characters beyond the start of the key. In addition, the key
	//     is restricted to a single line."
	//
	if .mark.line < .mark.line || .mark.index+1024 < .mark.index {
		// Check if the potential simple key to be removed is required.
		if .required {
			return false, yaml_parser_set_scanner_error(,
				"while scanning a simple key", .mark,
				"could not find expected ':'")
		}
		.possible = false
		return false, true
	}
	return true, true
}

// Check if a simple key may start at the current position and add it if
// needed.
func yaml_parser_save_simple_key( *yaml_parser_t) bool {
	// A simple key is required at the current position if the scanner is in
	// the block context and the current column coincides with the indentation
	// level.

	 := .flow_level == 0 && .indent == .mark.column

	//
	// If the current position may start a simple key, save it.
	//
	if .simple_key_allowed {
		 := yaml_simple_key_t{
			possible:     true,
			required:     ,
			token_number: .tokens_parsed + (len(.tokens) - .tokens_head),
			mark:         .mark,
		}

		if !yaml_parser_remove_simple_key() {
			return false
		}
		.simple_keys[len(.simple_keys)-1] = 
		.simple_keys_by_tok[.token_number] = len(.simple_keys) - 1
	}
	return true
}

// Remove a potential simple key at the current flow level.
func yaml_parser_remove_simple_key( *yaml_parser_t) bool {
	 := len(.simple_keys) - 1
	if .simple_keys[].possible {
		// If the key is required, it is an error.
		if .simple_keys[].required {
			return yaml_parser_set_scanner_error(,
				"while scanning a simple key", .simple_keys[].mark,
				"could not find expected ':'")
		}
		// Remove the key from the stack.
		.simple_keys[].possible = false
		delete(.simple_keys_by_tok, .simple_keys[].token_number)
	}
	return true
}

// max_flow_level limits the flow_level
const max_flow_level = 10000

// Increase the flow level and resize the simple key list if needed.
func yaml_parser_increase_flow_level( *yaml_parser_t) bool {
	// Reset the simple key on the next level.
	.simple_keys = append(.simple_keys, yaml_simple_key_t{
		possible:     false,
		required:     false,
		token_number: .tokens_parsed + (len(.tokens) - .tokens_head),
		mark:         .mark,
	})

	// Increase the flow level.
	.flow_level++
	if .flow_level > max_flow_level {
		return yaml_parser_set_scanner_error(,
			"while increasing flow level", .simple_keys[len(.simple_keys)-1].mark,
			fmt.Sprintf("exceeded max depth of %d", max_flow_level))
	}
	return true
}

// Decrease the flow level.
func yaml_parser_decrease_flow_level( *yaml_parser_t) bool {
	if .flow_level > 0 {
		.flow_level--
		 := len(.simple_keys) - 1
		delete(.simple_keys_by_tok, .simple_keys[].token_number)
		.simple_keys = .simple_keys[:]
	}
	return true
}

// max_indents limits the indents stack size
const max_indents = 10000

// Push the current indentation level to the stack and set the new level
// the current column is greater than the indentation level.  In this case,
// append or insert the specified token into the token queue.
func yaml_parser_roll_indent( *yaml_parser_t, ,  int,  yaml_token_type_t,  yaml_mark_t) bool {
	// In the flow context, do nothing.
	if .flow_level > 0 {
		return true
	}

	if .indent <  {
		// Push the current indentation level to the stack and set the new
		// indentation level.
		.indents = append(.indents, .indent)
		.indent = 
		if len(.indents) > max_indents {
			return yaml_parser_set_scanner_error(,
				"while increasing indent level", .simple_keys[len(.simple_keys)-1].mark,
				fmt.Sprintf("exceeded max depth of %d", max_indents))
		}

		// Create a token and insert it into the queue.
		 := yaml_token_t{
			typ:        ,
			start_mark: ,
			end_mark:   ,
		}
		if  > -1 {
			 -= .tokens_parsed
		}
		yaml_insert_token(, , &)
	}
	return true
}

// Pop indentation levels from the indents stack until the current level
// becomes less or equal to the column.  For each indentation level, append
// the BLOCK-END token.
func yaml_parser_unroll_indent( *yaml_parser_t,  int,  yaml_mark_t) bool {
	// In the flow context, do nothing.
	if .flow_level > 0 {
		return true
	}

	 := 
	.index--

	// Loop through the indentation levels in the stack.
	for .indent >  {

		// [Go] Reposition the end token before potential following
		//      foot comments of parent blocks. For that, search
		//      backwards for recent comments that were at the same
		//      indent as the block that is ending now.
		 := .index
		for  := len(.comments) - 1;  >= 0; -- {
			 := &.comments[]

			if .end_mark.index <  {
				// Don't go back beyond the start of the comment/whitespace scan, unless column < 0.
				// If requested indent column is < 0, then the document is over and everything else
				// is a foot anyway.
				break
			}
			if .start_mark.column == .indent+1 {
				// This is a good match. But maybe there's a former comment
				// at that same indent level, so keep searching.
				 = .start_mark
			}

			// While the end of the former comment matches with
			// the start of the following one, we know there's
			// nothing in between and scanning is still safe.
			 = .scan_mark.index
		}

		// Create a token and append it to the queue.
		 := yaml_token_t{
			typ:        yaml_BLOCK_END_TOKEN,
			start_mark: ,
			end_mark:   ,
		}
		yaml_insert_token(, -1, &)

		// Pop the indentation level.
		.indent = .indents[len(.indents)-1]
		.indents = .indents[:len(.indents)-1]
	}
	return true
}

// Initialize the scanner and produce the STREAM-START token.
func yaml_parser_fetch_stream_start( *yaml_parser_t) bool {

	// Set the initial indentation.
	.indent = -1

	// Initialize the simple key stack.
	.simple_keys = append(.simple_keys, yaml_simple_key_t{})

	.simple_keys_by_tok = make(map[int]int)

	// A simple key is allowed at the beginning of the stream.
	.simple_key_allowed = true

	// We have started.
	.stream_start_produced = true

	// Create the STREAM-START token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_STREAM_START_TOKEN,
		start_mark: .mark,
		end_mark:   .mark,
		encoding:   .encoding,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the STREAM-END token and shut down the scanner.
func yaml_parser_fetch_stream_end( *yaml_parser_t) bool {

	// Force new line.
	if .mark.column != 0 {
		.mark.column = 0
		.mark.line++
	}

	// Reset the indentation level.
	if !yaml_parser_unroll_indent(, -1, .mark) {
		return false
	}

	// Reset simple keys.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	.simple_key_allowed = false

	// Create the STREAM-END token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_STREAM_END_TOKEN,
		start_mark: .mark,
		end_mark:   .mark,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
func yaml_parser_fetch_directive( *yaml_parser_t) bool {
	// Reset the indentation level.
	if !yaml_parser_unroll_indent(, -1, .mark) {
		return false
	}

	// Reset simple keys.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	.simple_key_allowed = false

	// Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
	 := yaml_token_t{}
	if !yaml_parser_scan_directive(, &) {
		return false
	}
	// Append the token to the queue.
	yaml_insert_token(, -1, &)
	return true
}

// Produce the DOCUMENT-START or DOCUMENT-END token.
func yaml_parser_fetch_document_indicator( *yaml_parser_t,  yaml_token_type_t) bool {
	// Reset the indentation level.
	if !yaml_parser_unroll_indent(, -1, .mark) {
		return false
	}

	// Reset simple keys.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	.simple_key_allowed = false

	// Consume the token.
	 := .mark

	skip()
	skip()
	skip()

	 := .mark

	// Create the DOCUMENT-START or DOCUMENT-END token.
	 := yaml_token_t{
		typ:        ,
		start_mark: ,
		end_mark:   ,
	}
	// Append the token to the queue.
	yaml_insert_token(, -1, &)
	return true
}

// Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
func yaml_parser_fetch_flow_collection_start( *yaml_parser_t,  yaml_token_type_t) bool {

	// The indicators '[' and '{' may start a simple key.
	if !yaml_parser_save_simple_key() {
		return false
	}

	// Increase the flow level.
	if !yaml_parser_increase_flow_level() {
		return false
	}

	// A simple key may follow the indicators '[' and '{'.
	.simple_key_allowed = true

	// Consume the token.
	 := .mark
	skip()
	 := .mark

	// Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
	 := yaml_token_t{
		typ:        ,
		start_mark: ,
		end_mark:   ,
	}
	// Append the token to the queue.
	yaml_insert_token(, -1, &)
	return true
}

// Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
func yaml_parser_fetch_flow_collection_end( *yaml_parser_t,  yaml_token_type_t) bool {
	// Reset any potential simple key on the current flow level.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	// Decrease the flow level.
	if !yaml_parser_decrease_flow_level() {
		return false
	}

	// No simple keys after the indicators ']' and '}'.
	.simple_key_allowed = false

	// Consume the token.

	 := .mark
	skip()
	 := .mark

	// Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
	 := yaml_token_t{
		typ:        ,
		start_mark: ,
		end_mark:   ,
	}
	// Append the token to the queue.
	yaml_insert_token(, -1, &)
	return true
}

// Produce the FLOW-ENTRY token.
func yaml_parser_fetch_flow_entry( *yaml_parser_t) bool {
	// Reset any potential simple keys on the current flow level.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	// Simple keys are allowed after ','.
	.simple_key_allowed = true

	// Consume the token.
	 := .mark
	skip()
	 := .mark

	// Create the FLOW-ENTRY token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_FLOW_ENTRY_TOKEN,
		start_mark: ,
		end_mark:   ,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the BLOCK-ENTRY token.
func yaml_parser_fetch_block_entry( *yaml_parser_t) bool {
	// Check if the scanner is in the block context.
	if .flow_level == 0 {
		// Check if we are allowed to start a new entry.
		if !.simple_key_allowed {
			return yaml_parser_set_scanner_error(, "", .mark,
				"block sequence entries are not allowed in this context")
		}
		// Add the BLOCK-SEQUENCE-START token if needed.
		if !yaml_parser_roll_indent(, .mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, .mark) {
			return false
		}
	} else {
		// It is an error for the '-' indicator to occur in the flow context,
		// but we let the Parser detect and report about it because the Parser
		// is able to point to the context.
	}

	// Reset any potential simple keys on the current flow level.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	// Simple keys are allowed after '-'.
	.simple_key_allowed = true

	// Consume the token.
	 := .mark
	skip()
	 := .mark

	// Create the BLOCK-ENTRY token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_BLOCK_ENTRY_TOKEN,
		start_mark: ,
		end_mark:   ,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the KEY token.
func yaml_parser_fetch_key( *yaml_parser_t) bool {

	// In the block context, additional checks are required.
	if .flow_level == 0 {
		// Check if we are allowed to start a new key (not nessesary simple).
		if !.simple_key_allowed {
			return yaml_parser_set_scanner_error(, "", .mark,
				"mapping keys are not allowed in this context")
		}
		// Add the BLOCK-MAPPING-START token if needed.
		if !yaml_parser_roll_indent(, .mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, .mark) {
			return false
		}
	}

	// Reset any potential simple keys on the current flow level.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	// Simple keys are allowed after '?' in the block context.
	.simple_key_allowed = .flow_level == 0

	// Consume the token.
	 := .mark
	skip()
	 := .mark

	// Create the KEY token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_KEY_TOKEN,
		start_mark: ,
		end_mark:   ,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the VALUE token.
func yaml_parser_fetch_value( *yaml_parser_t) bool {

	 := &.simple_keys[len(.simple_keys)-1]

	// Have we found a simple key?
	if ,  := yaml_simple_key_is_valid(, ); ! {
		return false

	} else if  {

		// Create the KEY token and insert it into the queue.
		 := yaml_token_t{
			typ:        yaml_KEY_TOKEN,
			start_mark: .mark,
			end_mark:   .mark,
		}
		yaml_insert_token(, .token_number-.tokens_parsed, &)

		// In the block context, we may need to add the BLOCK-MAPPING-START token.
		if !yaml_parser_roll_indent(, .mark.column,
			.token_number,
			yaml_BLOCK_MAPPING_START_TOKEN, .mark) {
			return false
		}

		// Remove the simple key.
		.possible = false
		delete(.simple_keys_by_tok, .token_number)

		// A simple key cannot follow another simple key.
		.simple_key_allowed = false

	} else {
		// The ':' indicator follows a complex key.

		// In the block context, extra checks are required.
		if .flow_level == 0 {

			// Check if we are allowed to start a complex value.
			if !.simple_key_allowed {
				return yaml_parser_set_scanner_error(, "", .mark,
					"mapping values are not allowed in this context")
			}

			// Add the BLOCK-MAPPING-START token if needed.
			if !yaml_parser_roll_indent(, .mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, .mark) {
				return false
			}
		}

		// Simple keys after ':' are allowed in the block context.
		.simple_key_allowed = .flow_level == 0
	}

	// Consume the token.
	 := .mark
	skip()
	 := .mark

	// Create the VALUE token and append it to the queue.
	 := yaml_token_t{
		typ:        yaml_VALUE_TOKEN,
		start_mark: ,
		end_mark:   ,
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the ALIAS or ANCHOR token.
func yaml_parser_fetch_anchor( *yaml_parser_t,  yaml_token_type_t) bool {
	// An anchor or an alias could be a simple key.
	if !yaml_parser_save_simple_key() {
		return false
	}

	// A simple key cannot follow an anchor or an alias.
	.simple_key_allowed = false

	// Create the ALIAS or ANCHOR token and append it to the queue.
	var  yaml_token_t
	if !yaml_parser_scan_anchor(, &, ) {
		return false
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the TAG token.
func yaml_parser_fetch_tag( *yaml_parser_t) bool {
	// A tag could be a simple key.
	if !yaml_parser_save_simple_key() {
		return false
	}

	// A simple key cannot follow a tag.
	.simple_key_allowed = false

	// Create the TAG token and append it to the queue.
	var  yaml_token_t
	if !yaml_parser_scan_tag(, &) {
		return false
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
func yaml_parser_fetch_block_scalar( *yaml_parser_t,  bool) bool {
	// Remove any potential simple keys.
	if !yaml_parser_remove_simple_key() {
		return false
	}

	// A simple key may follow a block scalar.
	.simple_key_allowed = true

	// Create the SCALAR token and append it to the queue.
	var  yaml_token_t
	if !yaml_parser_scan_block_scalar(, &, ) {
		return false
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
func yaml_parser_fetch_flow_scalar( *yaml_parser_t,  bool) bool {
	// A plain scalar could be a simple key.
	if !yaml_parser_save_simple_key() {
		return false
	}

	// A simple key cannot follow a flow scalar.
	.simple_key_allowed = false

	// Create the SCALAR token and append it to the queue.
	var  yaml_token_t
	if !yaml_parser_scan_flow_scalar(, &, ) {
		return false
	}
	yaml_insert_token(, -1, &)
	return true
}

// Produce the SCALAR(...,plain) token.
func yaml_parser_fetch_plain_scalar( *yaml_parser_t) bool {
	// A plain scalar could be a simple key.
	if !yaml_parser_save_simple_key() {
		return false
	}

	// A simple key cannot follow a flow scalar.
	.simple_key_allowed = false

	// Create the SCALAR token and append it to the queue.
	var  yaml_token_t
	if !yaml_parser_scan_plain_scalar(, &) {
		return false
	}
	yaml_insert_token(, -1, &)
	return true
}

// Eat whitespaces and comments until the next token is found.
func yaml_parser_scan_to_next_token( *yaml_parser_t) bool {

	 := .mark

	// Until the next token is not found.
	for {
		// Allow the BOM mark to start a line.
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
		if .mark.column == 0 && is_bom(.buffer, .buffer_pos) {
			skip()
		}

		// Eat whitespaces.
		// Tabs are allowed:
		//  - in the flow context
		//  - in the block context, but not at the beginning of the line or
		//  after '-', '?', or ':' (complex value).
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}

		for .buffer[.buffer_pos] == ' ' || ((.flow_level > 0 || !.simple_key_allowed) && .buffer[.buffer_pos] == '\t') {
			skip()
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}

		// Check if we just had a line comment under a sequence entry that
		// looks more like a header to the following content. Similar to this:
		//
		// - # The comment
		//   - Some data
		//
		// If so, transform the line comment to a head comment and reposition.
		if len(.comments) > 0 && len(.tokens) > 1 {
			 := .tokens[len(.tokens)-2]
			 := .tokens[len(.tokens)-1]
			 := &.comments[len(.comments)-1]
			if .typ == yaml_BLOCK_SEQUENCE_START_TOKEN && .typ == yaml_BLOCK_ENTRY_TOKEN && len(.line) > 0 && !is_break(.buffer, .buffer_pos) {
				// If it was in the prior line, reposition so it becomes a
				// header of the follow up token. Otherwise, keep it in place
				// so it becomes a header of the former.
				.head = .line
				.line = nil
				if .start_mark.line == .mark.line-1 {
					.token_mark = .mark
				}
			}
		}

		// Eat a comment until a line break.
		if .buffer[.buffer_pos] == '#' {
			if !yaml_parser_scan_comments(, ) {
				return false
			}
		}

		// If it is a line break, eat it.
		if is_break(.buffer, .buffer_pos) {
			if .unread < 2 && !yaml_parser_update_buffer(, 2) {
				return false
			}
			skip_line()

			// In the block context, a new line may start a simple key.
			if .flow_level == 0 {
				.simple_key_allowed = true
			}
		} else {
			break // We have found a token.
		}
	}

	return true
}

// Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
//
// Scope:
//      %YAML    1.1    # a comment \n
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//      %TAG    !yaml!  tag:yaml.org,2002:  \n
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
func yaml_parser_scan_directive( *yaml_parser_t,  *yaml_token_t) bool {
	// Eat '%'.
	 := .mark
	skip()

	// Scan the directive name.
	var  []byte
	if !yaml_parser_scan_directive_name(, , &) {
		return false
	}

	// Is it a YAML directive?
	if bytes.Equal(, []byte("YAML")) {
		// Scan the VERSION directive value.
		var ,  int8
		if !yaml_parser_scan_version_directive_value(, , &, &) {
			return false
		}
		 := .mark

		// Create a VERSION-DIRECTIVE token.
		* = yaml_token_t{
			typ:        yaml_VERSION_DIRECTIVE_TOKEN,
			start_mark: ,
			end_mark:   ,
			major:      ,
			minor:      ,
		}

		// Is it a TAG directive?
	} else if bytes.Equal(, []byte("TAG")) {
		// Scan the TAG directive value.
		var ,  []byte
		if !yaml_parser_scan_tag_directive_value(, , &, &) {
			return false
		}
		 := .mark

		// Create a TAG-DIRECTIVE token.
		* = yaml_token_t{
			typ:        yaml_TAG_DIRECTIVE_TOKEN,
			start_mark: ,
			end_mark:   ,
			value:      ,
			prefix:     ,
		}

		// Unknown directive.
	} else {
		yaml_parser_set_scanner_error(, "while scanning a directive",
			, "found unknown directive name")
		return false
	}

	// Eat the rest of the line including any comments.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	for is_blank(.buffer, .buffer_pos) {
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	if .buffer[.buffer_pos] == '#' {
		// [Go] Discard this inline comment for the time being.
		//if !yaml_parser_scan_line_comment(parser, start_mark) {
		//	return false
		//}
		for !is_breakz(.buffer, .buffer_pos) {
			skip()
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}
	}

	// Check if we are at the end of the line.
	if !is_breakz(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a directive",
			, "did not find expected comment or line break")
		return false
	}

	// Eat a line break.
	if is_break(.buffer, .buffer_pos) {
		if .unread < 2 && !yaml_parser_update_buffer(, 2) {
			return false
		}
		skip_line()
	}

	return true
}

// Scan the directive name.
//
// Scope:
//      %YAML   1.1     # a comment \n
//       ^^^^
//      %TAG    !yaml!  tag:yaml.org,2002:  \n
//       ^^^
//
func yaml_parser_scan_directive_name( *yaml_parser_t,  yaml_mark_t,  *[]byte) bool {
	// Consume the directive name.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	var  []byte
	for is_alpha(.buffer, .buffer_pos) {
		 = read(, )
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Check if the name is empty.
	if len() == 0 {
		yaml_parser_set_scanner_error(, "while scanning a directive",
			, "could not find expected directive name")
		return false
	}

	// Check for an blank character after the name.
	if !is_blankz(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a directive",
			, "found unexpected non-alphabetical character")
		return false
	}
	* = 
	return true
}

// Scan the value of VERSION-DIRECTIVE.
//
// Scope:
//      %YAML   1.1     # a comment \n
//           ^^^^^^
func yaml_parser_scan_version_directive_value( *yaml_parser_t,  yaml_mark_t, ,  *int8) bool {
	// Eat whitespaces.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	for is_blank(.buffer, .buffer_pos) {
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Consume the major version number.
	if !yaml_parser_scan_version_directive_number(, , ) {
		return false
	}

	// Eat '.'.
	if .buffer[.buffer_pos] != '.' {
		return yaml_parser_set_scanner_error(, "while scanning a %YAML directive",
			, "did not find expected digit or '.' character")
	}

	skip()

	// Consume the minor version number.
	if !yaml_parser_scan_version_directive_number(, , ) {
		return false
	}
	return true
}

const max_number_length = 2

// Scan the version number of VERSION-DIRECTIVE.
//
// Scope:
//      %YAML   1.1     # a comment \n
//              ^
//      %YAML   1.1     # a comment \n
//                ^
func yaml_parser_scan_version_directive_number( *yaml_parser_t,  yaml_mark_t,  *int8) bool {

	// Repeat while the next character is digit.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	var ,  int8
	for is_digit(.buffer, .buffer_pos) {
		// Check if the number is too long.
		++
		if  > max_number_length {
			return yaml_parser_set_scanner_error(, "while scanning a %YAML directive",
				, "found extremely long version number")
		}
		 = *10 + int8(as_digit(.buffer, .buffer_pos))
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Check if the number was present.
	if  == 0 {
		return yaml_parser_set_scanner_error(, "while scanning a %YAML directive",
			, "did not find expected version number")
	}
	* = 
	return true
}

// Scan the value of a TAG-DIRECTIVE token.
//
// Scope:
//      %TAG    !yaml!  tag:yaml.org,2002:  \n
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
func yaml_parser_scan_tag_directive_value( *yaml_parser_t,  yaml_mark_t, ,  *[]byte) bool {
	var ,  []byte

	// Eat whitespaces.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	for is_blank(.buffer, .buffer_pos) {
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Scan a handle.
	if !yaml_parser_scan_tag_handle(, true, , &) {
		return false
	}

	// Expect a whitespace.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	if !is_blank(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a %TAG directive",
			, "did not find expected whitespace")
		return false
	}

	// Eat whitespaces.
	for is_blank(.buffer, .buffer_pos) {
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Scan a prefix.
	if !yaml_parser_scan_tag_uri(, true, nil, , &) {
		return false
	}

	// Expect a whitespace or line break.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	if !is_blankz(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a %TAG directive",
			, "did not find expected whitespace or line break")
		return false
	}

	* = 
	* = 
	return true
}

func yaml_parser_scan_anchor( *yaml_parser_t,  *yaml_token_t,  yaml_token_type_t) bool {
	var  []byte

	// Eat the indicator character.
	 := .mark
	skip()

	// Consume the value.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	for is_alpha(.buffer, .buffer_pos) {
		 = read(, )
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	 := .mark

	/*
	 * Check if length of the anchor is greater than 0 and it is followed by
	 * a whitespace character or one of the indicators:
	 *
	 *      '?', ':', ',', ']', '}', '%', '@', '`'.
	 */

	if len() == 0 ||
		!(is_blankz(.buffer, .buffer_pos) || .buffer[.buffer_pos] == '?' ||
			.buffer[.buffer_pos] == ':' || .buffer[.buffer_pos] == ',' ||
			.buffer[.buffer_pos] == ']' || .buffer[.buffer_pos] == '}' ||
			.buffer[.buffer_pos] == '%' || .buffer[.buffer_pos] == '@' ||
			.buffer[.buffer_pos] == '`') {
		 := "while scanning an alias"
		if  == yaml_ANCHOR_TOKEN {
			 = "while scanning an anchor"
		}
		yaml_parser_set_scanner_error(, , ,
			"did not find expected alphabetic or numeric character")
		return false
	}

	// Create a token.
	* = yaml_token_t{
		typ:        ,
		start_mark: ,
		end_mark:   ,
		value:      ,
	}

	return true
}

/*
 * Scan a TAG token.
 */

func yaml_parser_scan_tag( *yaml_parser_t,  *yaml_token_t) bool {
	var ,  []byte

	 := .mark

	// Check if the tag is in the canonical form.
	if .unread < 2 && !yaml_parser_update_buffer(, 2) {
		return false
	}

	if .buffer[.buffer_pos+1] == '<' {
		// Keep the handle as ''

		// Eat '!<'
		skip()
		skip()

		// Consume the tag value.
		if !yaml_parser_scan_tag_uri(, false, nil, , &) {
			return false
		}

		// Check for '>' and eat it.
		if .buffer[.buffer_pos] != '>' {
			yaml_parser_set_scanner_error(, "while scanning a tag",
				, "did not find the expected '>'")
			return false
		}

		skip()
	} else {
		// The tag has either the '!suffix' or the '!handle!suffix' form.

		// First, try to scan a handle.
		if !yaml_parser_scan_tag_handle(, false, , &) {
			return false
		}

		// Check if it is, indeed, handle.
		if [0] == '!' && len() > 1 && [len()-1] == '!' {
			// Scan the suffix now.
			if !yaml_parser_scan_tag_uri(, false, nil, , &) {
				return false
			}
		} else {
			// It wasn't a handle after all.  Scan the rest of the tag.
			if !yaml_parser_scan_tag_uri(, false, , , &) {
				return false
			}

			// Set the handle to '!'.
			 = []byte{'!'}

			// A special case: the '!' tag.  Set the handle to '' and the
			// suffix to '!'.
			if len() == 0 {
				,  = , 
			}
		}
	}

	// Check the character which ends the tag.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	if !is_blankz(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a tag",
			, "did not find expected whitespace or line break")
		return false
	}

	 := .mark

	// Create a token.
	* = yaml_token_t{
		typ:        yaml_TAG_TOKEN,
		start_mark: ,
		end_mark:   ,
		value:      ,
		suffix:     ,
	}
	return true
}

// Scan a tag handle.
func yaml_parser_scan_tag_handle( *yaml_parser_t,  bool,  yaml_mark_t,  *[]byte) bool {
	// Check the initial '!' character.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	if .buffer[.buffer_pos] != '!' {
		yaml_parser_set_scanner_tag_error(, ,
			, "did not find expected '!'")
		return false
	}

	var  []byte

	// Copy the '!' character.
	 = read(, )

	// Copy all subsequent alphabetical and numerical characters.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	for is_alpha(.buffer, .buffer_pos) {
		 = read(, )
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}

	// Check if the trailing character is '!' and copy it.
	if .buffer[.buffer_pos] == '!' {
		 = read(, )
	} else {
		// It's either the '!' tag or not really a tag handle.  If it's a %TAG
		// directive, it's an error.  If it's a tag token, it must be a part of URI.
		if  && string() != "!" {
			yaml_parser_set_scanner_tag_error(, ,
				, "did not find expected '!'")
			return false
		}
	}

	* = 
	return true
}

// Scan a tag.
func yaml_parser_scan_tag_uri( *yaml_parser_t,  bool,  []byte,  yaml_mark_t,  *[]byte) bool {
	//size_t length = head ? strlen((char *)head) : 0
	var  []byte
	 := len() > 0

	// Copy the head if needed.
	//
	// Note that we don't copy the leading '!' character.
	if len() > 1 {
		 = append(, [1:]...)
	}

	// Scan the tag.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	// The set of characters that may appear in URI is as follows:
	//
	//      '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
	//      '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
	//      '%'.
	// [Go] TODO Convert this into more reasonable logic.
	for is_alpha(.buffer, .buffer_pos) || .buffer[.buffer_pos] == ';' ||
		.buffer[.buffer_pos] == '/' || .buffer[.buffer_pos] == '?' ||
		.buffer[.buffer_pos] == ':' || .buffer[.buffer_pos] == '@' ||
		.buffer[.buffer_pos] == '&' || .buffer[.buffer_pos] == '=' ||
		.buffer[.buffer_pos] == '+' || .buffer[.buffer_pos] == '$' ||
		.buffer[.buffer_pos] == ',' || .buffer[.buffer_pos] == '.' ||
		.buffer[.buffer_pos] == '!' || .buffer[.buffer_pos] == '~' ||
		.buffer[.buffer_pos] == '*' || .buffer[.buffer_pos] == '\'' ||
		.buffer[.buffer_pos] == '(' || .buffer[.buffer_pos] == ')' ||
		.buffer[.buffer_pos] == '[' || .buffer[.buffer_pos] == ']' ||
		.buffer[.buffer_pos] == '%' {
		// Check if it is a URI-escape sequence.
		if .buffer[.buffer_pos] == '%' {
			if !yaml_parser_scan_uri_escapes(, , , &) {
				return false
			}
		} else {
			 = read(, )
		}
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
		 = true
	}

	if ! {
		yaml_parser_set_scanner_tag_error(, ,
			, "did not find expected tag URI")
		return false
	}
	* = 
	return true
}

// Decode an URI-escape sequence corresponding to a single UTF-8 character.
func yaml_parser_scan_uri_escapes( *yaml_parser_t,  bool,  yaml_mark_t,  *[]byte) bool {

	// Decode the required number of characters.
	 := 1024
	for  > 0 {
		// Check for a URI-escaped octet.
		if .unread < 3 && !yaml_parser_update_buffer(, 3) {
			return false
		}

		if !(.buffer[.buffer_pos] == '%' &&
			is_hex(.buffer, .buffer_pos+1) &&
			is_hex(.buffer, .buffer_pos+2)) {
			return yaml_parser_set_scanner_tag_error(, ,
				, "did not find URI escaped octet")
		}

		// Get the octet.
		 := byte((as_hex(.buffer, .buffer_pos+1) << 4) + as_hex(.buffer, .buffer_pos+2))

		// If it is the leading octet, determine the length of the UTF-8 sequence.
		if  == 1024 {
			 = width()
			if  == 0 {
				return yaml_parser_set_scanner_tag_error(, ,
					, "found an incorrect leading UTF-8 octet")
			}
		} else {
			// Check if the trailing octet is correct.
			if &0xC0 != 0x80 {
				return yaml_parser_set_scanner_tag_error(, ,
					, "found an incorrect trailing UTF-8 octet")
			}
		}

		// Copy the octet and move the pointers.
		* = append(*, )
		skip()
		skip()
		skip()
		--
	}
	return true
}

// Scan a block scalar.
func yaml_parser_scan_block_scalar( *yaml_parser_t,  *yaml_token_t,  bool) bool {
	// Eat the indicator '|' or '>'.
	 := .mark
	skip()

	// Scan the additional block scalar indicators.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}

	// Check for a chomping indicator.
	var ,  int
	if .buffer[.buffer_pos] == '+' || .buffer[.buffer_pos] == '-' {
		// Set the chomping method and eat the indicator.
		if .buffer[.buffer_pos] == '+' {
			 = +1
		} else {
			 = -1
		}
		skip()

		// Check for an indentation indicator.
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
		if is_digit(.buffer, .buffer_pos) {
			// Check that the indentation is greater than 0.
			if .buffer[.buffer_pos] == '0' {
				yaml_parser_set_scanner_error(, "while scanning a block scalar",
					, "found an indentation indicator equal to 0")
				return false
			}

			// Get the indentation level and eat the indicator.
			 = as_digit(.buffer, .buffer_pos)
			skip()
		}

	} else if is_digit(.buffer, .buffer_pos) {
		// Do the same as above, but in the opposite order.

		if .buffer[.buffer_pos] == '0' {
			yaml_parser_set_scanner_error(, "while scanning a block scalar",
				, "found an indentation indicator equal to 0")
			return false
		}
		 = as_digit(.buffer, .buffer_pos)
		skip()

		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
		if .buffer[.buffer_pos] == '+' || .buffer[.buffer_pos] == '-' {
			if .buffer[.buffer_pos] == '+' {
				 = +1
			} else {
				 = -1
			}
			skip()
		}
	}

	// Eat whitespaces and comments to the end of the line.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	for is_blank(.buffer, .buffer_pos) {
		skip()
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
	}
	if .buffer[.buffer_pos] == '#' {
		if !yaml_parser_scan_line_comment(, ) {
			return false
		}
		for !is_breakz(.buffer, .buffer_pos) {
			skip()
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}
	}

	// Check if we are at the end of the line.
	if !is_breakz(.buffer, .buffer_pos) {
		yaml_parser_set_scanner_error(, "while scanning a block scalar",
			, "did not find expected comment or line break")
		return false
	}

	// Eat a line break.
	if is_break(.buffer, .buffer_pos) {
		if .unread < 2 && !yaml_parser_update_buffer(, 2) {
			return false
		}
		skip_line()
	}

	 := .mark

	// Set the indentation level if it was specified.
	var  int
	if  > 0 {
		if .indent >= 0 {
			 = .indent + 
		} else {
			 = 
		}
	}

	// Scan the leading line breaks and determine the indentation level if needed.
	var , ,  []byte
	if !yaml_parser_scan_block_scalar_breaks(, &, &, , &) {
		return false
	}

	// Scan the block scalar content.
	if .unread < 1 && !yaml_parser_update_buffer(, 1) {
		return false
	}
	var ,  bool
	for .mark.column ==  && !is_z(.buffer, .buffer_pos) {
		// We are at the beginning of a non-empty line.

		// Is it a trailing whitespace?
		 = is_blank(.buffer, .buffer_pos)

		// Check if we need to fold the leading line break.
		if ! && ! && ! && len() > 0 && [0] == '\n' {
			// Do we need to join the lines by space?
			if len() == 0 {
				 = append(, ' ')
			}
		} else {
			 = append(, ...)
		}
		 = [:0]

		// Append the remaining line breaks.
		 = append(, ...)
		 = [:0]

		// Is it a leading whitespace?
		 = is_blank(.buffer, .buffer_pos)

		// Consume the current line.
		for !is_breakz(.buffer, .buffer_pos) {
			 = read(, )
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}

		// Consume the line break.
		if .unread < 2 && !yaml_parser_update_buffer(, 2) {
			return false
		}

		 = read_line(, )

		// Eat the following indentation spaces and line breaks.
		if !yaml_parser_scan_block_scalar_breaks(, &, &, , &) {
			return false
		}
	}

	// Chomp the tail.
	if  != -1 {
		 = append(, ...)
	}
	if  == 1 {
		 = append(, ...)
	}

	// Create a token.
	* = yaml_token_t{
		typ:        yaml_SCALAR_TOKEN,
		start_mark: ,
		end_mark:   ,
		value:      ,
		style:      yaml_LITERAL_SCALAR_STYLE,
	}
	if ! {
		.style = yaml_FOLDED_SCALAR_STYLE
	}
	return true
}

// Scan indentation spaces and line breaks for a block scalar.  Determine the
// indentation level if needed.
func yaml_parser_scan_block_scalar_breaks( *yaml_parser_t,  *int,  *[]byte,  yaml_mark_t,  *yaml_mark_t) bool {
	* = .mark

	// Eat the indentation spaces and line breaks.
	 := 0
	for {
		// Eat the indentation spaces.
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}
		for (* == 0 || .mark.column < *) && is_space(.buffer, .buffer_pos) {
			skip()
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}
		if .mark.column >  {
			 = .mark.column
		}

		// Check for a tab character messing the indentation.
		if (* == 0 || .mark.column < *) && is_tab(.buffer, .buffer_pos) {
			return yaml_parser_set_scanner_error(, "while scanning a block scalar",
				, "found a tab character where an indentation space is expected")
		}

		// Have we found a non-empty line?
		if !is_break(.buffer, .buffer_pos) {
			break
		}

		// Consume the line break.
		if .unread < 2 && !yaml_parser_update_buffer(, 2) {
			return false
		}
		// [Go] Should really be returning breaks instead.
		* = read_line(, *)
		* = .mark
	}

	// Determine the indentation level if needed.
	if * == 0 {
		* = 
		if * < .indent+1 {
			* = .indent + 1
		}
		if * < 1 {
			* = 1
		}
	}
	return true
}

// Scan a quoted scalar.
func yaml_parser_scan_flow_scalar( *yaml_parser_t,  *yaml_token_t,  bool) bool {
	// Eat the left quote.
	 := .mark
	skip()

	// Consume the content of the quoted scalar.
	var , , ,  []byte
	for {
		// Check that there are no document indicators at the beginning of the line.
		if .unread < 4 && !yaml_parser_update_buffer(, 4) {
			return false
		}

		if .mark.column == 0 &&
			((.buffer[.buffer_pos+0] == '-' &&
				.buffer[.buffer_pos+1] == '-' &&
				.buffer[.buffer_pos+2] == '-') ||
				(.buffer[.buffer_pos+0] == '.' &&
					.buffer[.buffer_pos+1] == '.' &&
					.buffer[.buffer_pos+2] == '.')) &&
			is_blankz(.buffer, .buffer_pos+3) {
			yaml_parser_set_scanner_error(, "while scanning a quoted scalar",
				, "found unexpected document indicator")
			return false
		}

		// Check for EOF.
		if is_z(.buffer, .buffer_pos) {
			yaml_parser_set_scanner_error(, "while scanning a quoted scalar",
				, "found unexpected end of stream")
			return false
		}

		// Consume non-blank characters.
		 := false
		for !is_blankz(.buffer, .buffer_pos) {
			if  && .buffer[.buffer_pos] == '\'' && .buffer[.buffer_pos+1] == '\'' {
				// Is is an escaped single quote.
				 = append(, '\'')
				skip()
				skip()

			} else if  && .buffer[.buffer_pos] == '\'' {
				// It is a right single quote.
				break
			} else if ! && .buffer[.buffer_pos] == '"' {
				// It is a right double quote.
				break

			} else if ! && .buffer[.buffer_pos] == '\\' && is_break(.buffer, .buffer_pos+1) {
				// It is an escaped line break.
				if .unread < 3 && !yaml_parser_update_buffer(, 3) {
					return false
				}
				skip()
				skip_line()
				 = true
				break

			} else if ! && .buffer[.buffer_pos] == '\\' {
				// It is an escape sequence.
				 := 0

				// Check the escape character.
				switch .buffer[.buffer_pos+1] {
				case '0':
					 = append(, 0)
				case 'a':
					 = append(, '\x07')
				case 'b':
					 = append(, '\x08')
				case 't', '\t':
					 = append(, '\x09')
				case 'n':
					 = append(, '\x0A')
				case 'v':
					 = append(, '\x0B')
				case 'f':
					 = append(, '\x0C')
				case 'r':
					 = append(, '\x0D')
				case 'e':
					 = append(, '\x1B')
				case ' ':
					 = append(, '\x20')
				case '"':
					 = append(, '"')
				case '\'':
					 = append(, '\'')
				case '\\':
					 = append(, '\\')
				case 'N': // NEL (#x85)
					 = append(, '\xC2')
					 = append(, '\x85')
				case '_': // #xA0
					 = append(, '\xC2')
					 = append(, '\xA0')
				case 'L': // LS (#x2028)
					 = append(, '\xE2')
					 = append(, '\x80')
					 = append(, '\xA8')
				case 'P': // PS (#x2029)
					 = append(, '\xE2')
					 = append(, '\x80')
					 = append(, '\xA9')
				case 'x':
					 = 2
				case 'u':
					 = 4
				case 'U':
					 = 8
				default:
					yaml_parser_set_scanner_error(, "while parsing a quoted scalar",
						, "found unknown escape character")
					return false
				}

				skip()
				skip()

				// Consume an arbitrary escape code.
				if  > 0 {
					var  int

					// Scan the character value.
					if .unread <  && !yaml_parser_update_buffer(, ) {
						return false
					}
					for  := 0;  < ; ++ {
						if !is_hex(.buffer, .buffer_pos+) {
							yaml_parser_set_scanner_error(, "while parsing a quoted scalar",
								, "did not find expected hexdecimal number")
							return false
						}
						 = ( << 4) + as_hex(.buffer, .buffer_pos+)
					}

					// Check the value and write the character.
					if ( >= 0xD800 &&  <= 0xDFFF) ||  > 0x10FFFF {
						yaml_parser_set_scanner_error(, "while parsing a quoted scalar",
							, "found invalid Unicode character escape code")
						return false
					}
					if  <= 0x7F {
						 = append(, byte())
					} else if  <= 0x7FF {
						 = append(, byte(0xC0+(>>6)))
						 = append(, byte(0x80+(&0x3F)))
					} else if  <= 0xFFFF {
						 = append(, byte(0xE0+(>>12)))
						 = append(, byte(0x80+((>>6)&0x3F)))
						 = append(, byte(0x80+(&0x3F)))
					} else {
						 = append(, byte(0xF0+(>>18)))
						 = append(, byte(0x80+((>>12)&0x3F)))
						 = append(, byte(0x80+((>>6)&0x3F)))
						 = append(, byte(0x80+(&0x3F)))
					}

					// Advance the pointer.
					for  := 0;  < ; ++ {
						skip()
					}
				}
			} else {
				// It is a non-escaped non-blank character.
				 = read(, )
			}
			if .unread < 2 && !yaml_parser_update_buffer(, 2) {
				return false
			}
		}

		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}

		// Check if we are at the end of the scalar.
		if  {
			if .buffer[.buffer_pos] == '\'' {
				break
			}
		} else {
			if .buffer[.buffer_pos] == '"' {
				break
			}
		}

		// Consume blank characters.
		for is_blank(.buffer, .buffer_pos) || is_break(.buffer, .buffer_pos) {
			if is_blank(.buffer, .buffer_pos) {
				// Consume a space or a tab character.
				if ! {
					 = read(, )
				} else {
					skip()
				}
			} else {
				if .unread < 2 && !yaml_parser_update_buffer(, 2) {
					return false
				}

				// Check if it is a first line break.
				if ! {
					 = [:0]
					 = read_line(, )
					 = true
				} else {
					 = read_line(, )
				}
			}
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}

		// Join the whitespaces or fold line breaks.
		if  {
			// Do we need to fold line breaks?
			if len() > 0 && [0] == '\n' {
				if len() == 0 {
					 = append(, ' ')
				} else {
					 = append(, ...)
				}
			} else {
				 = append(, ...)
				 = append(, ...)
			}
			 = [:0]
			 = [:0]
		} else {
			 = append(, ...)
			 = [:0]
		}
	}

	// Eat the right quote.
	skip()
	 := .mark

	// Create a token.
	* = yaml_token_t{
		typ:        yaml_SCALAR_TOKEN,
		start_mark: ,
		end_mark:   ,
		value:      ,
		style:      yaml_SINGLE_QUOTED_SCALAR_STYLE,
	}
	if ! {
		.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
	}
	return true
}

// Scan a plain scalar.
func yaml_parser_scan_plain_scalar( *yaml_parser_t,  *yaml_token_t) bool {

	var , , ,  []byte
	var  bool
	var  = .indent + 1

	 := .mark
	 := .mark

	// Consume the content of the plain scalar.
	for {
		// Check for a document indicator.
		if .unread < 4 && !yaml_parser_update_buffer(, 4) {
			return false
		}
		if .mark.column == 0 &&
			((.buffer[.buffer_pos+0] == '-' &&
				.buffer[.buffer_pos+1] == '-' &&
				.buffer[.buffer_pos+2] == '-') ||
				(.buffer[.buffer_pos+0] == '.' &&
					.buffer[.buffer_pos+1] == '.' &&
					.buffer[.buffer_pos+2] == '.')) &&
			is_blankz(.buffer, .buffer_pos+3) {
			break
		}

		// Check for a comment.
		if .buffer[.buffer_pos] == '#' {
			break
		}

		// Consume non-blank characters.
		for !is_blankz(.buffer, .buffer_pos) {

			// Check for indicators that may end a plain scalar.
			if (.buffer[.buffer_pos] == ':' && is_blankz(.buffer, .buffer_pos+1)) ||
				(.flow_level > 0 &&
					(.buffer[.buffer_pos] == ',' ||
						.buffer[.buffer_pos] == '?' || .buffer[.buffer_pos] == '[' ||
						.buffer[.buffer_pos] == ']' || .buffer[.buffer_pos] == '{' ||
						.buffer[.buffer_pos] == '}')) {
				break
			}

			// Check if we need to join whitespaces and breaks.
			if  || len() > 0 {
				if  {
					// Do we need to fold line breaks?
					if [0] == '\n' {
						if len() == 0 {
							 = append(, ' ')
						} else {
							 = append(, ...)
						}
					} else {
						 = append(, ...)
						 = append(, ...)
					}
					 = [:0]
					 = [:0]
					 = false
				} else {
					 = append(, ...)
					 = [:0]
				}
			}

			// Copy the character.
			 = read(, )

			 = .mark
			if .unread < 2 && !yaml_parser_update_buffer(, 2) {
				return false
			}
		}

		// Is it the end?
		if !(is_blank(.buffer, .buffer_pos) || is_break(.buffer, .buffer_pos)) {
			break
		}

		// Consume blank characters.
		if .unread < 1 && !yaml_parser_update_buffer(, 1) {
			return false
		}

		for is_blank(.buffer, .buffer_pos) || is_break(.buffer, .buffer_pos) {
			if is_blank(.buffer, .buffer_pos) {

				// Check for tab characters that abuse indentation.
				if  && .mark.column <  && is_tab(.buffer, .buffer_pos) {
					yaml_parser_set_scanner_error(, "while scanning a plain scalar",
						, "found a tab character that violates indentation")
					return false
				}

				// Consume a space or a tab character.
				if ! {
					 = read(, )
				} else {
					skip()
				}
			} else {
				if .unread < 2 && !yaml_parser_update_buffer(, 2) {
					return false
				}

				// Check if it is a first line break.
				if ! {
					 = [:0]
					 = read_line(, )
					 = true
				} else {
					 = read_line(, )
				}
			}
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
		}

		// Check indentation level.
		if .flow_level == 0 && .mark.column <  {
			break
		}
	}

	// Create a token.
	* = yaml_token_t{
		typ:        yaml_SCALAR_TOKEN,
		start_mark: ,
		end_mark:   ,
		value:      ,
		style:      yaml_PLAIN_SCALAR_STYLE,
	}

	// Note that we change the 'simple_key_allowed' flag.
	if  {
		.simple_key_allowed = true
	}
	return true
}

func yaml_parser_scan_line_comment( *yaml_parser_t,  yaml_mark_t) bool {
	if .newlines > 0 {
		return true
	}

	var  yaml_mark_t
	var  []byte

	for  := 0;  < 512; ++ {
		if .unread < +1 && !yaml_parser_update_buffer(, +1) {
			break
		}
		if is_blank(.buffer, .buffer_pos+) {
			continue
		}
		if .buffer[.buffer_pos+] == '#' {
			 := .mark.index+
			for {
				if .unread < 1 && !yaml_parser_update_buffer(, 1) {
					return false
				}
				if is_breakz(.buffer, .buffer_pos) {
					if .mark.index >=  {
						break
					}
					if .unread < 2 && !yaml_parser_update_buffer(, 2) {
						return false
					}
					skip_line()
				} else if .mark.index >=  {
					if len() == 0 {
						 = .mark
					}
					 = read(, )
				} else {
					skip()
				}
			}
		}
		break
	}
	if len() > 0 {
		.comments = append(.comments, yaml_comment_t{
			token_mark: ,
			start_mark: ,
			line: ,
		})
	}
	return true
}

func yaml_parser_scan_comments( *yaml_parser_t,  yaml_mark_t) bool {
	 := .tokens[len(.tokens)-1]

	if .typ == yaml_FLOW_ENTRY_TOKEN && len(.tokens) > 1 {
		 = .tokens[len(.tokens)-2]
	}

	var  = .start_mark
	var  yaml_mark_t
	var  = .indent
	if  < 0 {
		 = 0
	}

	var  = false
	var  = .newlines <= 1

	var  = .mark.line
	var  = .mark.column

	var  []byte

	// The foot line is the place where a comment must start to
	// still be considered as a foot of the prior content.
	// If there's some content in the currently parsed line, then
	// the foot is the line below it.
	var  = -1
	if .line > 0 {
		 = .mark.line-.newlines+1
		if .newlines == 0 && .mark.column > 1 {
			++
		}
	}

	var  = 0
	for ;  < 512; ++ {
		if .unread < +1 && !yaml_parser_update_buffer(, +1) {
			break
		}
		++
		if is_blank(.buffer, .buffer_pos+) {
			continue
		}
		 := .buffer[.buffer_pos+]
		var  = .flow_level > 0 && ( == ']' ||  == '}')
		if  || is_breakz(.buffer, .buffer_pos+) {
			// Got line break or terminator.
			if  || ! {
				if  ||  && (.line ==  && .typ != yaml_VALUE_TOKEN || .column-1 < ) {
					// This is the first empty line and there were no empty lines before,
					// so this initial part of the comment is a foot of the prior token
					// instead of being a head for the following one. Split it up.
					// Alternatively, this might also be the last comment inside a flow
					// scope, so it must be a footer.
					if len() > 0 {
						if .column-1 <  {
							// If dedented it's unrelated to the prior token.
							 = 
						}
						.comments = append(.comments, yaml_comment_t{
							scan_mark:  ,
							token_mark: ,
							start_mark: ,
							end_mark:   yaml_mark_t{.mark.index + , , },
							foot:       ,
						})
						 = yaml_mark_t{.mark.index + , , }
						 = 
						 = nil
					}
				} else {
					if len() > 0 && .buffer[.buffer_pos+] != 0 {
						 = append(, '\n')
					}
				}
			}
			if !is_break(.buffer, .buffer_pos+) {
				break
			}
			 = false
			 = true
			 = 0
			++
			continue
		}

		if len() > 0 && ( || -1 <  &&  != .column) {
			// The comment at the different indentation is a foot of the
			// preceding data rather than a head of the upcoming one.
			.comments = append(.comments, yaml_comment_t{
				scan_mark:  ,
				token_mark: ,
				start_mark: ,
				end_mark:   yaml_mark_t{.mark.index + , , },
				foot:       ,
			})
			 = yaml_mark_t{.mark.index + , , }
			 = 
			 = nil
		}

		if .buffer[.buffer_pos+] != '#' {
			break
		}

		if len() == 0 {
			 = yaml_mark_t{.mark.index + , , }
		} else {
			 = append(, '\n')
		}

		 = false

		// Consume until after the consumed comment line.
		 := .mark.index+
		for {
			if .unread < 1 && !yaml_parser_update_buffer(, 1) {
				return false
			}
			if is_breakz(.buffer, .buffer_pos) {
				if .mark.index >=  {
					break
				}
				if .unread < 2 && !yaml_parser_update_buffer(, 2) {
					return false
				}
				skip_line()
			} else if .mark.index >=  {
				 = read(, )
			} else {
				skip()
			}
		}

		 = 0
		 = 0
		 = .mark.line
		 = .indent
		if  < 0 {
			 = 0
		}
	}

	if len() > 0 {
		.comments = append(.comments, yaml_comment_t{
			scan_mark:  ,
			token_mark: ,
			start_mark: ,
			end_mark:   yaml_mark_t{.mark.index +  - 1, , },
			head:       ,
		})
	}
	return true
}