{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://komalang.org/schemas/koma-parse-v1.schema.json",
  "title": "koma parse --json, Koma AST version 1",
  "description": "What `koma parse <file> --json` prints (CLI-011, CLI-021): the Koma AST of one source (KAST-001, KAST-002) in its version 1 (KAST-004), a unist tree whose positions count UTF-16 code units of the exact input. Every node is closed: a later version of the tree, even one that only adds a field or a kind, has its own number and its own schema.",
  "type": "object",
  "properties": {
    "file": {
      "type": "string",
      "description": "The file as given on the command line."
    },
    "digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$",
      "description": "The SHA-256 of the file bytes, in lowercase hexadecimal."
    },
    "astVersion": {
      "const": 1,
      "description": "The version of the tree's promised shape (KAST-004); this schema describes version 1 alone."
    },
    "tree": {
      "$ref": "#/$defs/root"
    }
  },
  "required": ["file", "digest", "astVersion", "tree"],
  "additionalProperties": false,
  "$defs": {
    "point": {
      "description": "A unist point: line and column 1-based, offset 0-based in UTF-16 code units.",
      "type": "object",
      "properties": {
        "line": {
          "type": "integer",
          "minimum": 1
        },
        "column": {
          "type": "integer",
          "minimum": 1
        },
        "offset": {
          "type": "integer",
          "minimum": 0
        }
      },
      "required": ["line", "column", "offset"],
      "additionalProperties": false
    },
    "position": {
      "description": "A half-open unist position over the exact parser input.",
      "type": "object",
      "properties": {
        "start": {
          "$ref": "#/$defs/point"
        },
        "end": {
          "$ref": "#/$defs/point"
        }
      },
      "required": ["start", "end"],
      "additionalProperties": false
    },
    "child": {
      "oneOf": [
        {
          "$ref": "#/$defs/element"
        },
        {
          "$ref": "#/$defs/elementTag"
        },
        {
          "$ref": "#/$defs/sourceLine"
        },
        {
          "$ref": "#/$defs/raw"
        },
        {
          "$ref": "#/$defs/settings"
        },
        {
          "$ref": "#/$defs/page"
        },
        {
          "$ref": "#/$defs/notes"
        },
        {
          "$ref": "#/$defs/memo"
        }
      ]
    },
    "children": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/child"
      }
    },
    "root": {
      "description": "The whole source.",
      "type": "object",
      "properties": {
        "type": {
          "const": "root"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "children": {
          "$ref": "#/$defs/children"
        }
      },
      "required": ["type", "position", "children"],
      "additionalProperties": false
    },
    "element": {
      "description": "An element: one line, or a block with its opening and closing lines.",
      "type": "object",
      "properties": {
        "type": {
          "const": "element"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "tagName": {
          "type": ["string", "null"]
        },
        "children": {
          "$ref": "#/$defs/children"
        },
        "opening": {
          "$ref": "#/$defs/position"
        },
        "closing": {
          "type": "object",
          "properties": {
            "position": {
              "$ref": "#/$defs/position"
            },
            "bracket": {
              "type": "integer",
              "minimum": 0
            },
            "inline": {
              "type": "boolean"
            }
          },
          "required": ["position", "bracket", "inline"],
          "additionalProperties": false
        }
      },
      "required": ["type", "position", "tagName", "children"],
      "additionalProperties": false
    },
    "elementTag": {
      "description": "An element's tag, as tokens.",
      "type": "object",
      "properties": {
        "type": {
          "const": "elementTag"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/tagToken"
          }
        }
      },
      "required": ["type", "position", "children"],
      "additionalProperties": false
    },
    "tagToken": {
      "oneOf": [
        {
          "description": "A selector: the name, class or id of the element.",
          "type": "object",
          "properties": {
            "type": {
              "const": "tagToken"
            },
            "position": {
              "$ref": "#/$defs/position"
            },
            "kind": {
              "const": "selector"
            },
            "value": {
              "type": "string"
            }
          },
          "required": ["type", "position", "kind", "value"],
          "additionalProperties": false
        },
        {
          "description": "An attribute written `name=value`.",
          "type": "object",
          "properties": {
            "type": {
              "const": "tagToken"
            },
            "position": {
              "$ref": "#/$defs/position"
            },
            "kind": {
              "const": "attributeAssignment"
            },
            "value": {
              "type": "string"
            },
            "separator": {
              "type": "integer",
              "minimum": 0,
              "description": "The UTF-16 index of `=` in value."
            },
            "valueKind": {
              "enum": [
                "word",
                "number",
                "percentage",
                "dimension",
                "hash",
                "quotedString",
                "valueList",
                "invalid"
              ]
            },
            "items": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/listItem"
              }
            }
          },
          "required": ["type", "position", "kind", "value", "separator", "valueKind"],
          "additionalProperties": false
        },
        {
          "description": "A bare word, kept as written.",
          "type": "object",
          "properties": {
            "type": {
              "const": "tagToken"
            },
            "position": {
              "$ref": "#/$defs/position"
            },
            "kind": {
              "const": "bareWord"
            },
            "value": {
              "type": "string"
            }
          },
          "required": ["type", "position", "kind", "value"],
          "additionalProperties": false
        }
      ]
    },
    "listItem": {
      "description": "One item of a list value `(a, b)`.",
      "type": "object",
      "properties": {
        "valueKind": {
          "enum": [
            "word",
            "number",
            "percentage",
            "dimension",
            "hash",
            "quotedString",
            "valueList",
            "expression"
          ]
        },
        "value": {
          "type": "string"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "items": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/listItem"
          }
        }
      },
      "required": ["valueKind", "value", "position"],
      "additionalProperties": false
    },
    "sourceLine": {
      "description": "One authored line that is not an element line.",
      "type": "object",
      "properties": {
        "type": {
          "const": "sourceLine"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "kind": {
          "enum": [
            "setting",
            "setting-other",
            "front",
            "blank",
            "page",
            "attribute-line",
            "comment",
            "blocked",
            "notes",
            "memo",
            "fenced",
            "literal",
            "text"
          ]
        },
        "value": {
          "type": "string"
        },
        "finding": {
          "type": "string"
        },
        "fence": {
          "enum": ["code", "math"]
        },
        "settingValueAt": {
          "type": "integer",
          "minimum": 0
        }
      },
      "required": ["type", "position", "kind", "value"],
      "additionalProperties": false
    },
    "raw": {
      "description": "Source text kept as written.",
      "type": "object",
      "properties": {
        "type": {
          "const": "raw"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "value": {
          "type": "string"
        }
      },
      "required": ["type", "position", "value"],
      "additionalProperties": false
    },
    "settings": {
      "description": "A complete YAML settings block; its values are read separately.",
      "type": "object",
      "properties": {
        "type": {
          "const": "settings"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "scope": {
          "enum": ["document", "page"]
        },
        "body": {
          "$ref": "#/$defs/position"
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceLine"
          }
        }
      },
      "required": ["type", "position", "scope", "body", "children"],
      "additionalProperties": false
    },
    "notes": {
      "description": "Presenter notes: opaque source, including an unclosed block.",
      "type": "object",
      "properties": {
        "type": {
          "const": "notes"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "body": {
          "$ref": "#/$defs/position"
        },
        "closed": {
          "type": "boolean"
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceLine"
          }
        }
      },
      "required": ["type", "position", "body", "closed", "children"],
      "additionalProperties": false
    },
    "memo": {
      "description": "A memo on the page, kept out of every output.",
      "type": "object",
      "properties": {
        "type": {
          "const": "memo"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "attributes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/tagToken"
          }
        },
        "body": {
          "$ref": "#/$defs/position"
        },
        "closed": {
          "type": "boolean"
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceLine"
          }
        }
      },
      "required": ["type", "position", "attributes", "body", "closed", "children"],
      "additionalProperties": false
    },
    "page": {
      "description": "A page. `boundary` is absent for an implicit first page.",
      "type": "object",
      "properties": {
        "type": {
          "const": "page"
        },
        "position": {
          "$ref": "#/$defs/position"
        },
        "boundary": {
          "$ref": "#/$defs/position"
        },
        "body": {
          "$ref": "#/$defs/position"
        },
        "children": {
          "$ref": "#/$defs/children"
        }
      },
      "required": ["type", "position", "body", "children"],
      "additionalProperties": false
    }
  }
}
