diff --git a/src/yaml/__init__.mojo b/src/yaml/__init__.mojo index cef9a5a..8467200 100644 --- a/src/yaml/__init__.mojo +++ b/src/yaml/__init__.mojo @@ -6,11 +6,7 @@ Example: ```mojo from yaml import parse - var config = parse(""" - database: - host: localhost - port: 5432 - """) + var config = parse("database:\\n host: localhost\\n port: 5432") var db = config.get("database") print(db.get("host").as_string()) # "localhost" diff --git a/src/yaml/parser.mojo b/src/yaml/parser.mojo index 35bd439..9e286b9 100644 --- a/src/yaml/parser.mojo +++ b/src/yaml/parser.mojo @@ -205,14 +205,23 @@ struct Parser: var token = self.current() - # Stop at DEDENT or EOF - if token.kind == TokenKind.DEDENT() or token.kind == TokenKind.EOF(): + # Stop at EOF + if token.kind == TokenKind.EOF(): + break + + # Stop at DEDENT (but we might see DEDENT from nested values that we should skip) + if token.kind == TokenKind.DEDENT(): break # Check for dash (sequence at same level - stop here) if token.kind == TokenKind.DASH(): break + # Handle INDENT - this continues the mapping at a deeper level + if token.kind == TokenKind.INDENT(): + _ = self.advance() + continue + # Parse key if token.kind != TokenKind.STRING(): raise Error("Expected key at line " + String(token.pos.line)) @@ -279,9 +288,13 @@ struct Parser: if self.current().kind == TokenKind.DEDENT(): _ = self.advance() else: - # Item on same line + # Item on same line (but might have nested INDENT internally) var item = self.parse_value() result.append(item^) + + # If the item had nested content, consume the DEDENT + if self.current().kind == TokenKind.DEDENT(): + _ = self.advance() self.skip_newlines()