kdl

    KDL document language parser, formatter, query engine, and schema validation library for MoonBit

    kdl
    parser
    formatter
    query
    kql
    schema
    configuration
    document
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    6 months ago
    Downloads
    63

    #lenitain/kdl

    mooncake

    A MoonBit library for parsing, formatting, querying, and validating KDL documents.

    #Installation

    Add the dependency to your moon.mod.json:

    { "deps": { "lenitain/kdl": "0.1.0" } }

    #Quick Start

    let doc = @parser.parse(r#"node "hello" { child key=42 }"#)? println(doc.to_kdl_string()) let node = doc.get("node") println(node)

    #Parsing

    // Parse KDL v2 (default)
    let doc = @parser.parse(input)?

    // Parse KDL v1
    let doc = @parser.parse_v1(input)?

    // Access document nodes
    let nodes = doc.nodes()
    let first = doc.get("node-name")

    Returns Result[KdlDocument, KdlError]. Errors contain KdlDiagnostic entries with span info, severity, and messages.

    #Types

    TypeDescription
    KdlDocumentTop-level document containing an array of nodes
    KdlNodeA named node with entries and optional children
    KdlEntryA value argument or key=value property
    KdlValueEnum: String, Integer(Int64), Float(Double), Bool, Null
    KdlIdentifierA node/property key name with optional repr
    KdlErrorCollection of KdlDiagnostic errors
    KdlDiagnosticSingle diagnostic with span, message, label, severity

    #Building documents programmatically

    let node = @KdlNode::new(@KdlIdentifier::new("server"))
    let doc = @KdlDocument::new()
    doc.push(node)
    println(doc.to_kdl_string())

    #Accessing values

    match entry.value {
    String(s) => println(s)
    Integer(n) => println(n.to_string())
    Float(f) => println(f.to_string())
    Bool(b) => println(b.to_string())
    Null => println("null")
    }

    #Formatting

    // Compact output
    println(doc.to_kdl_string())

    // Pretty-printed with 2-space indentation
    println(doc.to_kdl_string_formatted(2))

    #Querying (KQL)

    The query subpackage implements KDL Query Language (KQL):

    let results = @query.query(doc, "node > child")?

    #Schema Validation

    Validate documents against a KDL schema:

    let schema_doc = @parser.parse(schema_input)?
    let schema = @schema.parse_schema(schema_doc)?
    @schema.validate(doc, schema)?

    #CLI Tool

    Build the CLI from cmd/main/:

    moon build cmd/main

    Subcommands:

    kdl parse <file> Parse and print document summary kdl check <file> [schema] Validate against optional schema kdl format <file> Pretty-print a KDL file kdl help Show usage

    #Subpackage Import Paths

    ImportPurpose
    lenitain/kdlCore types
    lenitain/kdl/parserParsing functions
    lenitain/kdl/queryKQL queries
    lenitain/kdl/schemaSchema validation

    #License

    Apache-2.0

    KdlDiagnostic

    pub struct KdlDiagnostic {
    input : String
    span_start : Int
    span_len : Int
    message : String?
    label : String?
    help : String?
    severity : KdlSeverity
    } derive(Eq)

    KdlDiagnostic::new

    fn KdlDiagnostic::new(input : String, span_start : Int, span_len : Int, message : String?, label : String?, help : String?, severity : KdlSeverity) -> KdlDiagnostic

    KdlDocument

    pub struct KdlDocument {
    nodes : Array[KdlNode]
    version : Int?
    }

    impl Eq for KdlDocument
    impl Show for KdlDocument

    KdlDocument::get

    fn KdlDocument::get(self : KdlDocument, name : String) -> KdlNode?

    KdlDocument::get_version

    fn KdlDocument::get_version(self : KdlDocument) -> Int?

    KdlDocument::new

    KdlDocument::nodes

    fn KdlDocument::nodes(self : KdlDocument) -> Array[KdlNode]

    KdlDocument::push

    fn KdlDocument::push(self : KdlDocument, node : KdlNode) -> Unit

    KdlDocument::set_version

    fn KdlDocument::set_version(self : KdlDocument, version : Int?) -> Unit

    KdlDocument::to_kdl_string

    fn KdlDocument::to_kdl_string(self : KdlDocument) -> String

    KdlDocument::to_kdl_string_formatted

    fn KdlDocument::to_kdl_string_formatted(self : KdlDocument, indent : Int) -> String

    KdlEntry

    pub struct KdlEntry {
    ty : KdlIdentifier?
    value : KdlValue
    name : KdlIdentifier?
    } derive(Eq)

    impl Show for KdlEntry

    KdlEntry::is_prop

    fn KdlEntry::is_prop(self : KdlEntry) -> Bool

    KdlEntry::new

    fn KdlEntry::new(value : KdlValue) -> KdlEntry

    KdlEntry::new_prop

    fn KdlEntry::new_prop(name : KdlIdentifier, value : KdlValue) -> KdlEntry

    KdlEntry::new_prop_with_ty

    fn KdlEntry::new_prop_with_ty(ty : KdlIdentifier?, name : KdlIdentifier, value : KdlValue) -> KdlEntry

    KdlEntry::to_kdl_string

    fn KdlEntry::to_kdl_string(self : KdlEntry) -> String

    KdlEntry::with_ty

    fn KdlEntry::with_ty(ty : KdlIdentifier?, value : KdlValue) -> KdlEntry

    KdlError

    pub struct KdlError {
    input : String
    diagnostics : Array[KdlDiagnostic]
    } derive(Eq)

    impl Show for KdlError

    KdlError::is_empty

    fn KdlError::is_empty(self : KdlError) -> Bool

    KdlError::new

    fn KdlError::new(input : String) -> KdlError

    KdlError::push

    fn KdlError::push(self : KdlError, diag : KdlDiagnostic) -> Unit

    KdlIdentifier

    pub struct KdlIdentifier {
    value : String
    repr : String?
    } derive(Eq)

    KdlIdentifier::new

    fn KdlIdentifier::new(value : String) -> KdlIdentifier

    KdlIdentifier::repr

    fn KdlIdentifier::repr(self : KdlIdentifier) -> String?

    KdlIdentifier::to_string

    fn KdlIdentifier::to_string(self : KdlIdentifier) -> String

    KdlIdentifier::value

    fn KdlIdentifier::value(self : KdlIdentifier) -> String

    KdlIdentifier::with_repr

    fn KdlIdentifier::with_repr(value : String, repr : String) -> KdlIdentifier

    KdlNode

    pub struct KdlNode {
    ty : KdlIdentifier?
    name : KdlIdentifier
    entries : Array[KdlEntry]
    children : Array[KdlNode]?
    }

    impl Eq for KdlNode
    impl Show for KdlNode

    KdlNode::full

    fn KdlNode::full(ty : KdlIdentifier?, name : KdlIdentifier, entries : Array[KdlEntry], children : Array[KdlNode]?) -> KdlNode

    KdlNode::new

    fn KdlNode::new(name : KdlIdentifier) -> KdlNode

    KdlNode::to_kdl_string

    fn KdlNode::to_kdl_string(self : KdlNode) -> String

    KdlNode::to_kdl_string_formatted

    fn KdlNode::to_kdl_string_formatted(self : KdlNode, indent : Int) -> String

    KdlNode::with_children

    fn KdlNode::with_children(name : KdlIdentifier, children : Array[KdlNode]) -> KdlNode

    KdlNode::with_entries

    fn KdlNode::with_entries(name : KdlIdentifier, entries : Array[KdlEntry]) -> KdlNode

    KdlSeverity

    pub(all) enum KdlSeverity {
    Error
    Warning
    } derive(Eq)

    impl Show for KdlSeverity

    KdlValue

    pub(all) enum KdlValue {
    String(String)
    Integer(Int64)
    Float(Double)
    Bool(Bool)
    Null
    }

    impl Eq for KdlValue
    impl Show for KdlValue

    KdlValue::as_bool

    fn KdlValue::as_bool(self : KdlValue) -> Bool?

    KdlValue::as_float

    fn KdlValue::as_float(self : KdlValue) -> Double?

    KdlValue::as_integer

    fn KdlValue::as_integer(self : KdlValue) -> Int64?

    KdlValue::as_string

    fn KdlValue::as_string(self : KdlValue) -> String?

    KdlValue::is_bool

    fn KdlValue::is_bool(self : KdlValue) -> Bool

    KdlValue::is_float

    fn KdlValue::is_float(self : KdlValue) -> Bool

    KdlValue::is_integer

    fn KdlValue::is_integer(self : KdlValue) -> Bool

    KdlValue::is_null

    fn KdlValue::is_null(self : KdlValue) -> Bool

    KdlValue::is_string

    fn KdlValue::is_string(self : KdlValue) -> Bool

    KdlValue::to_kdl_string

    fn KdlValue::to_kdl_string(self : KdlValue) -> String