lantern

loom-fence-grammar

Loom Fence Grammar

Canonical source for fence addressing and language query mappings.

Address Grammar

Address Grammar

(* Loom Address Grammar v0.3 - cursor positioning for stream injection *)
(* Named beats indexed when content matters more than order *)

address     = slug [ ":" locator ] ;
slug        = identifier ;

(* Locator finds the injection point *)
locator     = section_loc                     (* section-level targeting *)
            | stream_loc ;                    (* stream position targeting *)

(* Section locator - inject relative to section *)
section_loc = section_name [ "." fence_loc ] [ anchor ] ;
section_name = identifier ;

(* Fence locator - into structured data *)
fence_loc   = fence_type [ selector ] [ "." key_path ] ;
fence_type  = identifier ;
key_path    = identifier { "." identifier } ;

(* Selector - Q specificity for targeting *)
selector    = label_sel                       (* #config - named fence *)
            | index_sel ;                     (* [0], [1] - positional *)

label_sel   = "#" identifier ;                (* named beats indexed *)
index_sel   = "[" index_expr "]" ;
index_expr  = integer                         (* position: 0, 1, -1 *)
            | attr_query ;                    (* key=value filter *)
attr_query  = identifier "=" value ;

(* Anchor - where in section to inject prose *)
anchor      = "@" position_ref ;
position_ref = "start"                        (* after section heading *)
             | "end"                          (* before next section *)
             | "after:" pattern               (* after matching text *)
             | "before:" pattern              (* before matching text *)
             | integer ;                      (* token position in section *)

(* Patterns for find semantics *)
pattern     = quoted_string                   (* literal match *)
            | "/" regex "/" ;                 (* regex match *)

(* Primitives *)
identifier  = letter { letter | digit | "-" | "_" } ;
integer     = [ "-" ] digit { digit } ;
quoted_string = '"' { any_char } '"' ;
value       = identifier | quoted_string ;

Selector Philosophy

Syntax Use When Example
#label Content matters, name is stable config:aws.yaml#credentials
[0] Order matters, first match config:aws.yaml[0]
[n] Specific position in sequence log:entries.yaml[42]
[-1] Latest/last item log:entries.yaml[-1]

Named beats indexed when the content matters more than the order.

Language Mappings

Each language has a treesitter grammar. We define common K constructs that map Q β†’ V.

Python

language: python
treesitter: tree-sitter-python
constructs:
  function:
    query: "(function_definition name: (identifier) @name)"
    selector: "@name"
    returns: body
  class:
    query: "(class_definition name: (identifier) @name)"
    selector: "@name"
    returns: body
  variable:
    query: "(assignment left: (identifier) @name)"
    selector: "@name"
    returns: right
  import:
    query: "(import_statement) @import"
    selector: position
    returns: module_name
  config_param:
    query: "(call function: (attribute object: (identifier) @obj attribute: (identifier) @method) arguments: (argument_list (string) @key))"
    filter: "@obj == 'config' and @method == 'get'"
    returns: "@key"

YAML

language: yaml
treesitter: tree-sitter-yaml
constructs:
  key:
    query: "(block_mapping_pair key: (flow_node) @key)"
    selector: "@key"
    returns: value
  list_item:
    query: "(block_sequence (block_sequence_item) @item)"
    selector: position
    returns: content
  document:
    query: "(document) @doc"
    selector: position
    returns: content

JSON

language: json
treesitter: tree-sitter-json
constructs:
  key:
    query: "(pair key: (string) @key)"
    selector: "@key"
    returns: value
  array_item:
    query: "(array (value) @item)"
    selector: position
    returns: content

Bash

language: bash
treesitter: tree-sitter-bash
constructs:
  command:
    query: "(command name: (command_name) @name)"
    selector: position
    returns: full_command
  function:
    query: "(function_definition name: (word) @name)"
    selector: "@name"
    returns: body
  variable:
    query: "(variable_assignment name: (variable_name) @name)"
    selector: "@name"
    returns: value
  pipeline:
    query: "(pipeline) @pipe"
    selector: position
    returns: commands

EBNF

language: ebnf
treesitter: tree-sitter-ebnf
constructs:
  rule:
    query: "(rule name: (identifier) @name)"
    selector: "@name"
    returns: definition
  terminal:
    query: "(terminal) @term"
    selector: content
    returns: value

Markdown

language: markdown
treesitter: tree-sitter-markdown
constructs:
  heading:
    query: "(atx_heading) @heading"
    selector: level_or_content
    returns: content
  fence:
    query: "(fenced_code_block) @fence"
    selector: language_or_position
    returns: content
  link:
    query: "(link) @link"
    selector: text_or_url
    returns: destination
  list_item:
    query: "(list_item) @item"
    selector: position
    returns: content

Query Examples

Address Grammar Construct Result
node:section.python.function[main] python function Body of function named "main"
node:section.yaml.key[name] yaml key Value of key "name"
node:section.bash.command[0] bash command First command
node:section.python.config_param python config_param All config.get() keys

Operations

set:
  description: Replace target entirely
  requires: valid address
  
append:
  description: Add after target
  requires: valid address
  
prepend:
  description: Add before target
  requires: valid address
  
insert:
  description: Splice at cursor position
  requires: address with @ anchor

Implementation Status

Utility Fences

Utility Fences

No dev logs found

Provenance

Document

  • Status: πŸ”΄ Unverified

Changelog

  • 2026-01-11 19:14: Node created by mcp - Creating canonical grammar source for fence addressing and treesitter language mappings

North

slots:
- slug: loom-dev-log-2025-01-12
  context:
  - Linking grammar to today's dev log

Utility Fences

↑ northloom-dev-log-2025-01-12