Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/yaml/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 16 additions & 3 deletions src/yaml/parser.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()

Expand Down
Loading