Skip to content

Add directive plugin for jsonic Go implementation#1

Merged
rjrodger merged 3 commits intomainfrom
claude/add-go-implementation-SFYNg
Mar 19, 2026
Merged

Add directive plugin for jsonic Go implementation#1
rjrodger merged 3 commits intomainfrom
claude/add-go-implementation-SFYNg

Conversation

@rjrodger
Copy link
Contributor

Summary

This PR introduces a new directive plugin for the jsonic Go library that enables custom syntax extensions through configurable open/close token sequences. Directives allow users to define custom parsing behaviors that transform parsed content.

Key Changes

  • New directive.go module: Core directive implementation with:

    • Action callback type for processing directive content
    • RuleMod struct for configuring grammar rule integration
    • RulesOption for specifying which rules detect open/close tokens
    • DirectiveOptions configuration struct
    • Apply() helper function for easy plugin registration
  • New plugin.go module: Main Directive() plugin function that:

    • Registers custom open/close tokens with the jsonic parser
    • Modifies existing grammar rules ("val", "list", "elem", "map", "pair") to detect directive tokens
    • Creates a directive rule that parses content between open/close boundaries
    • Supports optional close tokens (single-value directives when close is omitted)
    • Handles implicit list/map creation within directive boundaries
    • Provides CustomFunc callback for advanced rule customization
    • Implements proper node resolution through replacement chains
  • Comprehensive test suite (directive_test.go):

    • Happy path tests for single values, lists, maps, and implicit lists
    • Close token handling with nesting and multiple directives
    • Custom action callbacks (adder, multiplier examples)
    • Edge cases and error conditions
    • Advanced custom rule modifications for shorthand syntax

Notable Implementation Details

  • Directives can have optional close tokens; without a close token, they consume a single value
  • Multiple directives can share the same close token
  • Duplicate open tokens are detected and panic to prevent conflicts
  • Counter-based mechanism (dlist, dmap) controls implicit list/map creation within directive boundaries
  • Node replacement chain walking ensures correct child node resolution after implicit list creation
  • Custom callbacks receive resolved token Tins for advanced grammar modifications
  • Default rules can be overridden or disabled via RulesOption

https://claude.ai/code/session_01RiMHE2zSU49VFuFzWygBBF

claude added 3 commits March 19, 2026 14:31
Port of the TypeScript @jsonic/directive plugin to Go, following the
pattern established by github.com/jsonicjs/ini/go. Includes:

- directive.go: types (DirectiveOptions, Action, RuleMod, etc.) and Apply() helper
- plugin.go: Directive plugin function with open/close token registration,
  grammar rule modification, and replacement chain resolution for Go slices
- directive_test.go: tests covering happy path, close tokens, adder/multiplier
  directives, edge cases, and custom rule modifications

https://claude.ai/code/session_01RiMHE2zSU49VFuFzWygBBF
- Replace Node 18/20/22 matrix with 24.x and latest
- Add Go job that builds and tests the go/ directory
- Update actions/checkout to v4, actions/setup-node to v4

https://claude.ai/code/session_01RiMHE2zSU49VFuFzWygBBF
@rjrodger rjrodger merged commit 7f24e5b into main Mar 19, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants