tabnaspath

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 1 Imported by: 0

README

tabnas/path (Go port)

Go port of the @tabnas/path Tabnas parser plugin. It adds property-path tracking to the Tabnas parser so that rule actions can see the path (keys and indices) leading to the current value.

The TypeScript version is canonical; this package tracks it.

  • Module path: github.com/tabnas/path/go
  • Package name: tabnaspath

Install

go get github.com/tabnas/path/go

The Tabnas engine ships no grammar of its own — you bring a grammar that defines the val / map / pair / list / elem rules. Install the grammar first, then Path on top. A minimal grammar fixture (installGrammar) lives in path_test.go.

Tiny example

import (
    "fmt"
    "strings"

    tabnas "github.com/tabnas/parser/go"
    tabnaspath "github.com/tabnas/path/go"
)

func main() {
    j := tabnas.Make()
    installGrammar(j)               // defines val/map/pair/list/elem
    _ = j.Use(tabnaspath.Path, nil) // install Path after the grammar

    j.Rule("val", func(rs *tabnas.RuleSpec, _ *tabnas.Parser) {
        rs.AddAC(func(r *tabnas.Rule, ctx *tabnas.Context) {
            path, _ := r.K["path"].([]any)
            parts := make([]string, len(path))
            for i, p := range path {
                parts[i] = fmt.Sprintf("%v", p)
            }
            if m, ok := r.Node.(map[string]any); ok {
                m["$"] = "<" + strings.Join(parts, ",") + ">"
            }
        })
    })

    result, _ := j.Parse("{x:{a:1}}")
    m := result.(map[string]any)
    // m["$"]            == "<>"   (root path is empty)
    x := m["x"].(map[string]any)
    // x["$"]            == "<x>"  (nested map at key x)
    _, _ = m, x
}

Documentation

The docs follow the Diátaxis four-quadrant structure:

  • Tutorial — zero to a working path-tracking parser, step by step.
  • How-to guides — recipes: read the path, classify segments, use r.K["key"], seed a base path.
  • Reference — exported identifiers, the Rule.K keys, the function refs, meta input.
  • Concepts — how it works, the engine relationship, and a "Differences from the TS version" section.

The canonical TypeScript implementation lives in ../ts/ with its own docs.

License

MIT. Copyright (c) Richard Rodger.

Documentation

Index

Constants

View Source
const Version = "0.2.0"

Variables

This section is empty.

Functions

func Path

func Path(j *tabnas.Tabnas, opts map[string]any) error

Path is a Tabnas plugin that tracks the property path to the current location during parsing. The path is stored in Rule.K["path"] as a []any slice of string keys and int indices.

Types

type PathOptions

type PathOptions struct{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL