Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ if(BUILD_TESTING)
--tool $<TARGET_FILE:matrix_lex_dump>
--root "${CMAKE_CURRENT_SOURCE_DIR}")
set_tests_properties(matrix.lexer PROPERTIES LABELS "matrix;lexer" TIMEOUT 60)
add_test(NAME matrix.annotations
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/matrix/annotation_contract.py"
--compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter>
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(matrix.annotations PROPERTIES LABELS "matrix;reflection;routes" TIMEOUT 180)
# Matrix execution conformance: each page lowers to NIL and runs on the
# interpreter with exact expected output. Acceptance is covered by the
# lexer test; this checks that the lowering means what the surface says.
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ do not assume every documented combination is accepted by every backend yet.
- [Statements](language-reference/statements.md)
- [Faults and cleanup](language-reference/faults.md)
- [Functions](language-reference/functions.md)
- [Annotations](language-reference/annotations.md)
- [Modules](language-reference/modules.md)
- [Traits](language-reference/traits.md)

Expand Down
9 changes: 9 additions & 0 deletions docs/advanced/nil-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ declaration-line record/header. Misplaced, malformed and orphan records reject;
older readers ignore this optional annotation comment rather than treating
its contents as instructions.

Record, class, and enum annotations containing the same line controls or
trailing whitespace use
`# declaration_annotation "<NIL-escaped exact annotation>"` before their schema
declaration. This additive text record preserves metadata that cannot fit
losslessly on an ordinary `@name(...)` line. `function_annotation` remains
specific to functions; annotation strings that fit ordinary text records keep
their existing spelling. Binary schema annotations retain their existing
encoding.

## Binary executable v3 layout

The source ownership profile uses `OWN_ENTER`/`OWN_LEAVE` lexical scopes,
Expand Down
65 changes: 65 additions & 0 deletions docs/analysis-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ publish diagnostics or make edits. See [NIL-0012](design/NIL-0012-signature-quer
- `declaration_info`: `kind`, `name`, `qualified_name`, `type_name`,
`source_path`, `line`, `parent_id`, `is_public`, `semantic_checked`.
- `annotation_info`: `owner_id`, `name`, `arguments`, `line`.
- `annotation_argument_info`: `annotation_id`, `name`, `is_named`, `kind`,
`value`. Matrix spells this record `annotation-argument-info` and its fields
`annotation-id` and `is-named`.

The JSON snapshot assigns each declaration an `id`. IDs are local to one
snapshot; never persist them across edits. A missing parent is `-1`, an
Expand All @@ -484,6 +487,56 @@ control-flow graph, or runtime object reflection. `semantic_checked` means the
whole compilation passed checking; it does not promise that every type string
has been fully elaborated.

### Typed annotation arguments

The `nil.reflection.v1` JSON annotation records retain `owner_id`, `name`,
`arguments`, and `line`, and add `arguments_parsed` and `parsed_arguments`:

```json
{
"owner_id": 0,
"name": "Company::review",
"arguments": "\"runtime\",revision=2,approved=true",
"line": 3,
"arguments_parsed": true,
"parsed_arguments": [
{"name": null, "kind": "text", "value": "runtime"},
{"name": "revision", "kind": "integer", "value": "2"},
{"name": "approved", "kind": "boolean", "value": "true"}
]
}
```

`arguments` remains the retained argument text for existing consumers; it is
not a source-exact formatting contract. `parsed_arguments` preserves argument
order. Its `name` is null for positional values and a string for named
properties. Every `value` is a string: decoded text or the validated scalar or
qualified-symbol spelling. The kinds are `text`, `boolean`, `integer`,
`floating`, and `symbol`. Symbols describe metadata and are not resolved as
program variables.

Positional arguments precede named properties, duplicate keys are rejected,
integers fit signed 64-bit storage, and floating-point values must be finite
and representable as `f64`. Lists, maps, and computed expressions are outside
the scalar profile. Legacy Ny composite and nested payloads remain available
for any annotation name, including custom names. For example,
`@custom(outer(inner("payload")))` retains inert argument text; no nested call
is executed. Such payloads report `arguments_parsed: false` with an empty
`parsed_arguments` array. Matrix remains scalar-only and rejects these nested
forms. A successfully parsed marker annotation with no arguments reports true
and an empty array.
Consumers must check the flag rather than interpreting an empty array as a
guarantee that no metadata exists.

These fields are additive; the schema remains `nil.reflection.v1` and the
existing `annotation_info` record is unchanged. Older compiler output may omit
the new JSON fields; treat that as unavailable parsed metadata. The annotation
ID is its index in `reflection.annotations`, while `owner_id` indexes
`reflection.declarations`. See the [annotation reference](language-reference/annotations.md)
for source syntax and language-specific string rules.

### Generated snapshot functions

Successful `--emit-reflection` writes an ordinary Ny module named
`analysis_snapshot`, atomically, with these exported functions:

Expand All @@ -492,8 +545,20 @@ count() -> i32
declaration(id: i32) -> declaration_info
annotation_count() -> i32
annotation(id: i32) -> annotation_info
annotation_arguments_parsed(id: i32) -> bool
annotation_argument_count() -> i32
annotation_argument(id: i32) -> annotation_argument_info
```

`annotation_arguments_parsed` takes an annotation ID, not a declaration ID.
`annotation_argument_count` counts parsed arguments across the entire snapshot.
`annotation_argument` addresses that flat sequence, ordered first by annotation
ID and then by argument order. Its `annotation_id` refers back to `annotation`.
For positional arguments the value record has `name: ""` and `is_named: false`;
named properties have `is_named: true`. `kind` and `value` use the same strings
as the JSON representation. Opaque annotations contribute no entries, so use
`annotation_arguments_parsed` to distinguish them from empty parsed annotations.

Invalid IDs panic. Truncated snapshots cannot be exported. A failed analysis
does not replace an existing snapshot, so build scripts must check exit status.
The generated module can be consumed by either frontend on VM or native routes.
Expand Down
89 changes: 89 additions & 0 deletions docs/design/NIL-0020-declaration-annotations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# NIL-0020: Declaration annotations as shared metadata

- **Status:** Accepted; initial scalar metadata profile
- **Scope:** Ny and Matrix source syntax, shared annotation parsing,
declaration reflection, and editor support

## Decision

Expose declaration metadata through stackable `@name` and
`@name(positional, property=value)` annotations in both languages. Matrix keeps
`note name ...` and `context "..."`, and adds the structured prose form
`note name with positional, property being value`.

The familiar `@` prefix provides a compact, recognizable declaration boundary.
It does not introduce Python's executable decorator semantics. Annotation
names are metadata identities, not calls or references to runtime functions.
Existing compiler-recognized annotations keep their established effects.

## Scalar argument profile

Arguments contain text, booleans, signed integers, floating-point numbers, or
symbols. Positional arguments and named properties retain their order. Symbols
are metadata values and are not evaluated. Arrays, maps, general expressions,
and executable transformations are deferred.

Positional values precede named properties, duplicate property keys are
rejected, integers fit signed 64-bit storage, and floating-point values must
be finite and representable as `f64`.

Parenthesized annotations may span lines. Matrix's prose `with` form remains
single-line. Each frontend retains its string rules: Ny recognizes its usual
escapes, while Matrix quoted text remains raw. Qualified annotation names
accept `.` and `::` and use `::` in their shared identity. Matrix canonicalizes
known built-in names and maps hyphens to underscores while preserving custom
name case.

## Attachment and retention

Matrix attaches annotations to named paragraphs, records, classes, choices,
and supported methods. It rejects unsupported attachments rather than dropping
metadata. Fields, parameters, local bindings, contracts, aliases, tests, and
implementation or extension containers are not part of this profile.

The shared representation retains raw annotation arguments for existing
consumers and adds parsed scalar arguments for reflection. Legacy Ny composite
and nested payloads remain available as opaque metadata for any annotation
name, including custom names, with `arguments_parsed: false`. For example,
`@custom(outer(inner("payload")))` is retained as inert argument text; its
nested forms are not calls. Consumers cannot assume every historical
annotation is representable as scalar properties. Matrix keeps the strict
scalar profile and does not inherit this Ny compatibility path.

Annotations remain independent of preprocessing. Both frontends run shared
preprocessing directives before declaration parsing. Matrix literal regions
and complete parenthesized annotation payloads are protected from macro
expansion. Directive and macro names retain exact case-sensitive spelling;
macro expansion remains single-pass. Inactive branches cannot apply includes,
macro changes, pragmas, or diagnostics.

Workspace analysis uses its source snapshot and existing include/output
bounds. Includes and nontrivial transformations still lack a full original
source map, so compiler source-edit spans and signature-query offsets are
withheld when correspondence cannot be established. Neither metadata support
nor preprocessing adds automatic decorator execution.

## Editor contract

Highlight annotation names, named properties, scalar values, and delimiters
within a bounded annotation region. Matrix's ordinary expressions continue to
use their existing grammar. Completion and snippets expose common metadata
forms without treating the suggested vocabulary as an allowlist.

The document-local index ignores annotation payloads when discovering symbols
and resolving code references. In particular, multiline properties containing
words such as `value`, `begin`, or `returns` must not create declarations or
change the following declaration's source location. This metadata does not
yet populate compiler signature help or hover documentation.

## Acceptance scope

Regression coverage must compare both source frontends' scalar argument
representations; validate malformed syntax and unsupported attachments;
preserve legacy metadata and executable-context behavior; and verify exact
following-declaration locations, multiline highlighting, raw Matrix strings,
completion boundaries, and metadata exclusion from code navigation.

See the [annotation reference](../language-reference/annotations.md) for public
syntax. This record defines the profile; test results are reported separately
and do not imply general decorator execution or arbitrary metadata values.
1 change: 1 addition & 0 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ not assigned architecture identifiers.
| NIL-0016 | [Shared text patterns and collected queries](NIL-0016-patterns-and-queries.md) | Implemented initial byte-pattern and eager array-query profiles |
| NIL-0017 | [Versioned NILData and one shared value model](NIL-0017-nildata.md) | Implemented initial bounded document profile |
| NIL-0019 | [Explicit binding-reference parameters](NIL-0019-binding-reference-parameters.md) | Accepted; initial scalar/text implementation |
| NIL-0020 | [Declaration annotations as shared metadata](NIL-0020-declaration-annotations.md) | Accepted; initial scalar metadata profile |
| NIL-0093 | [Executable context is artifact self-description](NIL-0093-executable-context.md) | Implemented within the documented inspection profile |

## Retained Ny feature references
Expand Down
Loading
Loading