A Zig parser for View Markup Language (VML), an XML-based markup language for describing SwiftUI user interface hierarchies.
VML provides a static, declarative representation of SwiftUI view hierarchies suitable for server-side generation and client-side rendering. This implementation is a Zig-based parser that validates and processes VML documents according to the VML 1.0 specification.
- Complete VML 1.0 Parser: Full implementation of the VML specification
- Zero Dependencies: Pure Zig implementation with no external dependencies
- Entity Decoding: Support for named and numeric HTML entities
- Error Tracking: Line and column tracking for parse errors
- Memory Safe: Proper allocation and deallocation with Zig's allocator system
- Comment Stripping: Optional comment removal during parsing
- Comprehensive Testing: Extensive test suite covering all parser features
Add VML to your build.zig.zon:
.dependencies = .{
.vml = .{
.url = "https://github.com/<your-repo>/vml/archive/<commit>.tar.gz",
.hash = "...",
},
},const std = @import("std");
const vml = @import("vml");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const input =
\\<!doctype swiftui+vml>
\\<vml>
\\ <body>
\\ <Text style="font(.title)">Hello World</Text>
\\ </body>
\\</vml>
;
var parser = vml.Parser.init(allocator, input);
var doc = try parser.parse();
defer doc.deinit(allocator);
std.debug.print("Doctype: {s}\n", .{doc.doctype});
}const options = vml.ParserOptions{
.strip_comments = true, // Remove comments during parsing
};
var parser = vml.Parser.initWithOptions(allocator, input, options);var parser = vml.Parser.init(allocator, input);
const doc = parser.parse() catch |err| {
if (parser.getErrorLocation()) |loc| {
std.debug.print("Parse error at line {}, column {}: {s}\n",
.{loc.line, loc.column, loc.message});
}
return err;
};
defer doc.deinit(allocator);This parser implements the View Markup Language Specification v1.0. Key features include:
- XML-Based Syntax: Standard XML document structure
- SwiftUI Mapping: Direct 1:1 mapping to SwiftUI view types
- Style Attribute: Semicolon-separated SwiftUI modifiers
- Template/Slot System: Support for ViewBuilder content
- Entity Encoding: Full HTML entity support
<!doctype swiftui+vml>
<vml>
<head>
<VStack template="launch" spacing="16">
<ProgressView/>
<Text style="font(.headline)">Loading...</Text>
</VStack>
</head>
<body>
<NavigationStack>
<VStack spacing="20" style="padding(.top, 50)">
<Image systemName="lock.circle.fill"
style="resizable();frame(width: 80, height: 80);foregroundStyle(.blue)"/>
<TextField style="textFieldStyle(.roundedBorder)">Email</TextField>
<Button style="buttonStyle(.borderedProminent)">Sign In</Button>
</VStack>
</NavigationStack>
</body>
</vml>vml/
├── src/
│ ├── main.zig # CLI executable entry point
│ ├── root.zig # Library root module
│ └── parser.zig # VML parser implementation
├── build.zig # Zig build configuration
├── build.zig.zon # Package metadata
├── spec.md # VML 1.0 specification
├── grammar.abnf # ABNF grammar definition
└── README.md # This file
Build the project:
zig buildRun tests:
zig build testRun the executable:
zig build runThe parser implements the full ABNF grammar defined in grammar.abnf, supporting:
- Document structure (
<!doctype>,<vml>,<head>,<body>) - Elements (self-closing and container)
- Attributes (quoted strings, entities)
- Style syntax (modifiers, arguments, expressions)
- Templates and slots
- Comments
- Whitespace handling
- Zig 0.15.1 or later
[Your License Here]
Contributions are welcome! Please ensure all tests pass before submitting a pull request.