stride3d/stride · error · SemanticErrorException

While parsing a block mapping, did not find expected key.

Error message

While parsing a block mapping, did not find expected key.

What it means

Stride.Core.Yaml's parser reached a position inside a block mapping where the next token is not a valid key (e.g. a scalar or tag where 'key:' was expected). The parser builds block mappings from BLOCK-MAPPING-START/KEY tokens produced by the scanner; when ParseBlockMappingKey sees a token that cannot be a key it aborts with this SemanticErrorException.

Solutions

  1. Check the reported line/column and add the missing ':' after the key
  2. Verify indentation of all lines in the block mapping is consistent (spaces, not tabs)
  3. Quote keys containing special characters (:, -, ?, {, etc.) so the scanner emits a scalar token
  4. Validate the YAML with a linter (yamllint) before loading

Example fix

// before
config:
  enabled true
// after
config:
  enabled: true
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate with a lenient parse
try { new YamlStream().Load(new StringReader(yamlText)); } catch (YamlException) { /* reject before real use */ }

Type guard

bool LooksLikeYamlMapping(string line) => !string.IsNullOrWhiteSpace(line) && (line.Contains(": ") || line.TrimEnd().EndsWith(":"));

Try / catch

try { var yaml = new YamlStream(); yaml.Load(reader); } catch (YamlException ex) { logger.LogError(ex, "YAML syntax error at {Line}:{Col}", ex.Start.Line, ex.Start.Column); throw new ConfigFormatException(ex.Start.Line, ex.Start.Column, ex.Message); }

Prevention

When it happens

Trigger: Parsing a YAML document where a block mapping entry is malformed: missing colon after a key, a value appearing at the wrong indentation, or a non-scalar token (anchor, tag, alias) where a key is required.

Common situations: Hand-edited config files with a forgotten ':' after a key, copy-pasted YAML where indentation shifted so a value sits where a key should be, or keys written with invalid characters for plain scalars.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/2278bc8932731466. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Parser.cs:712

                else
                {
                    state = ParserState.YAML_PARSE_BLOCK_MAPPING_VALUE_STATE;
                    return ProcessEmptyScalar(mark);
                }
            }

            else if (GetCurrentToken() is BlockEnd)
            {
                state = states.Pop();
                Event evt = new Events.MappingEnd(GetCurrentToken().Start, GetCurrentToken().End);
                Skip();
                return evt;
            }

            else
            {
                var current = GetCurrentToken();
                throw new SemanticErrorException(current.Start, current.End, "While parsing a block mapping, did not find expected key.");
            }
        }

        /// <summary>
        /// Parse the productions:
        /// block_mapping        ::= BLOCK-MAPPING_START
        ///
        ///                          ((KEY block_node_or_indentless_sequence?)?
        ///
        ///                          (VALUE block_node_or_indentless_sequence?)?)*
        ///                           ***** *
        ///                          BLOCK-END
        ///
        /// </summary>
        private Event ParseBlockMappingValue()
        {
            if (GetCurrentToken() is Value)
            {

View on GitHub (pinned to 96fad776d2)