Skip to content

Extending frame application on already-defined structures and JSON strings#31

Open
luca-anzalone wants to merge 6 commits into
devfrom
refactor/frame_apply
Open

Extending frame application on already-defined structures and JSON strings#31
luca-anzalone wants to merge 6 commits into
devfrom
refactor/frame_apply

Conversation

@luca-anzalone

@luca-anzalone luca-anzalone commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

Frame application ({Frame} ... / ... {Frame}) previously only worked on a struct literal written in the same statement — the grammar hard-wired frame_apply to a list_literal. This meant you could write [[v] = (name:"x", age:27){Person}] but could not apply a frame to a value already held in a variable ([[r] = [my_struct]{Person}]).

This PR lets a frame be applied to already-defined operands — variables (including path access like [x:field]) and strings containing JSON — while preserving the existing prefix/postfix = strict/loose convention and the existing nested-frame semantics.

Type of change

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • Other (describe below)

Solution

  • Grammar (nxs_v2_grammar.lark): the structure rule now accepts a variable operand in addition to list_literal, in both prefix and postfix positions. Operands are inlined (rather than via an intermediate frameable nonterminal) to avoid a reduce/reduce collision with atom; this mirrors the existing list_literal-with-optional-frame_apply pattern, so it stays LALR-safe.
  • AST (node.py): SchemedCollection.value widened to Collection | Variable; to_nxs()/__str__() emit a Variable operand without wrapping parens ([x] {Person}) so round-trips stay exact. Collection.__init__'s value type was widened to include Variable to resolve the type-checker warning at the forwarded super().__init__ call.
  • Runtime (runtime.py): added Struct.from_python(), a recursive dict/list → nested-Struct converter. Also removed the now-unnecessary _extract_struct "__"-unwrapping workaround from apply_prefix/apply_postfix.
  • Interpreter (interpreter.py): eval_schemed_collection now evaluates a literal via eval_collection as before, or resolves a variable via interpret_expression, then coerces the result to a Struct. A string operand is parsed as JSON, with an LLM-repair fallback for quasi-JSON (e.g. trailing commas); a JSON scalar (non-object/array) raises a runtime error.

Strict vs loose stays tied to prefix/postfix: {Frame} x is strict (all slots required, none on mismatch), x {Frame} is loose (defaults + coercion). Nested frames validate automatically via the existing apply_prefix/apply_postfix recursion, for literals, variables, and JSON alike.

Testing

  • Added or updated unit/integration tests
  • All existing tests pass (pytest)

Full suite: 906 passed, 30 skipped. New tests cover: parsing [x]{Frame} / {Frame}[x] / path-access operands; to_nxs round-trip for variable operands; runtime Struct.from_python (dict/list/nested); interpreter eval of frame-on-variable (strict + loose), frame-on-path-access, frame-on-JSON-string, the quasi-JSON LLM-repair path, the scalar-JSON error path, and nested-frame validation on both a variable and a JSON string (Person with a nested Date frame).

Checklist

  • My branch is rebased on the latest dev
  • Commit messages are clear and reference the related issue where applicable

Release Notes

Frames can now be applied to already-defined structures: variables (including nested-field access) and strings holding JSON. JSON payloads are parsed automatically, with malformed/quasi-JSON repaired via an LLM before application. Nested frames are validated recursively in all cases.

Closes: kebula-it/nemantix-enterprise#30

@luca-anzalone luca-anzalone added this to the Release 09/26 milestone Jul 7, 2026
@luca-anzalone luca-anzalone self-assigned this Jul 7, 2026
@luca-anzalone luca-anzalone added enhancement New feature or request priority: low priority: medium status: confirmed Issue is confirmed, validated, and ready to be worked on documentation labels Jul 7, 2026
Comment thread src/nemantix/core/runtime.py Outdated
luca-anzalone added a commit that referenced this pull request Jul 9, 2026
* fix: access expression for programmatic struct field access + updated docs and tests

* prompt: added example about expression access in struct (#33)

* fix: struct key ambiguity of integer-looking strings (#31, #33)

* ruff format
@luca-anzalone luca-anzalone force-pushed the refactor/frame_apply branch from 1233c4b to ee307ff Compare July 9, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation enhancement New feature or request priority: medium status: confirmed Issue is confirmed, validated, and ready to be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants