Skip to content

Add pragmas, and a schema comment they can set - #19

Merged
xadaemon merged 2 commits into
trunkfrom
compiler-pragmas
Nov 8, 2026
Merged

xadaemon merged 2 commits into
trunkfrom
compiler-pragmas

Conversation

@xadaemon

@xadaemon xadaemon commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Adds pragmas to the compiler: comments in the ASN.1 that reach plugins as key/value pairs in the .asnst. They are the way to pass information into plugins for the RPC work planned on top of asnkit. The first key asnkit reads itself is schema_comment, for the comment the schema generators write.

--@ rpc.package = greeter.v1
Greeter DEFINITIONS AUTOMATIC TAGS ::= BEGIN

--@ rpc.message
--@ go-name = HelloRequest
HelloRequest ::= SEQUENCE {
    --@ json.name = the_name
    name UTF8String
}

END

Pragmas (faf6599)

  • Syntax: --@ key = value, or --@ key with the value "", on a line of its own. A key is a letter followed by letters, digits, ., - and _. A plain -- @, or a --@ inside /* */, is still an ordinary comment.
  • Attachment: a pragma belongs to the item right after it, with only whitespace or comments in between. That item can be a module, any assignment, a member (also in [[ ]], in nested types and in SEQUENCE OF elements) or an object class field.
  • Errors: these are parse errors that give the line and column:
    • a pragma after code on the same line, which would otherwise silently attach to the next line;
    • a pragma before something that can't hold one, such as END, ... or an enum item;
    • a bad key;
    • the same key twice on one item.
  • In the .asnst: the eight node kinds above get a new last field, pragmas: a list of [key, value] pairs in source order, left out when there are none. It is a list of pairs rather than a map because a map in the readable form is a node. asnkitrt.syntax reads it as a tuple of pairs, so dict(node.pragmas or ()) works.
  • Formats: .asni files never contain pragmas, and every .asni fixture is byte-identical. .asnst goes to version 1.1, which 1.0 readers still load. The kinds tables in the compiler and in the Rust, JVM and Python runtimes have the new field. The .asnst fixtures and examples were regenerated for the version.

Schema comment (def0cfe)

  • Default comment: schemas now start with schema generated by asnkit from foo.asn, or schema generated by asnkit when the file names aren't known. This is the $comment of a JSON Schema and the first comment of an XML Schema or DTD. It replaces the old sentence, which named the encoding rules and the compiler version.
  • --@ schema_comment = ... before a module: replaces the default for schemas of that module's types. When several of those modules have one, each distinct value is one line.
  • Where the pragma is read: an .asni has no pragmas, so asnkitcc schema reads them from the .asn inputs. An .asni input gets the default comment.
  • WebAssembly: schema() takes a new option, specification, the ASN.1 text that was compiled. The playground passes it.
  • Library change: in the asnkitcc library, schema::Request takes comments (from schema::comments_of()) instead of generator.
  • Fix: the XML comment escaping now repeats until no -- is left. Before, it left -- in a name like a---b.asn.

Testing

  • cargo test --workspace, cargo clippy --workspace --all-targets and cargo fmt --check pass. New tests: packages/asnkit-compiler/tests/pragmas.rs and a schema-comment test in packages/asnkitcc/tests/schema.rs.
  • uv run python -m unittest passes, with ASNKIT_RUST_BINARY set (547 tests), including the new test_pragmas. tests.test_schema also passes.
  • WebAssembly: npm run build && npm test in packages/asnkit-compiler-wasm passes (14 tests, one of them new).
  • The fixtures were regenerated the way RELEASING.md describes.
  • mkdocs build --strict passes.
  • Not run: the JVM KindsTest, because Java isn't installed on the machine this was made on. It's a one-line table change per kind.

A pragma is a single line comment --@ key = value, or --@ key, on a
line of its own before a module, an assignment, a member or a field of
an object class. It is for what a plugin needs to be told that the
ASN.1 has no way to say, as what a type is to an RPC framework built
on asnkit.

The comment scanner gives the comments that begin with --@, and once
the tree is parsed each is given to what has the location of the first
character after it, as the next item with nothing but whitespace and
comments between. A pragma after something on its line, before what has
no location (END, an extension marker, an item of an ENUMERATED), a key
that is not a letter followed by letters, digits, '.', '-' and '_', and
a key given twice to the same item are parse errors that tell where.

The syntax tree has them as pragmas, a list of [key, value] pairs in
the order they are written, last in the fields of Module,
TypeAssignment, ValueAssignment, ObjectAssignment,
ObjectClassAssignment, ObjectClassField, ObjectSetAssignment and
Member, and left out when there are none. It is a list of pairs and
not a map, as a map in the readable form is a node. A compiled
specification has none, so .asni files are as they were, and .asnst
files are of version 1.1. asnkitrt.syntax reads them as a tuple of
pairs. The tables of kinds of the compiler and of the runtimes for
Rust, the JVM and Python have the field.

The .asnst fixtures and examples are made again for the version.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
A schema of asnkitcc schema began with what it is of and what made it,
as "The JSON Encoding Rules (X.697) of the types of foo.asn, as asnkit
writes them. Made by asnkitcc 2.0.0-rc7.". It is now "schema generated
by asnkit from foo.asn", with the names of the ASN.1 files, or "schema
generated by asnkit" when they are not known, in the $comment of a JSON
Schema and the first comment of an XML Schema and a DTD.

The pragma schema_comment of a module, --@ schema_comment = Greeter v1,
is the comment instead, of the schemas of its types: those of the
modules of the types of a schema are a line each. An .asni file has no
pragmas, so asnkitcc schema reads them from the ASN.1 files it is
given, with schema::comments_of(), which schema::Request takes as
comments rather than generator. schema() of the compiler as WebAssembly
takes the ASN.1 text as the option specification, which the playground
gives it.

A comment of XML had "--" replaced once, which left "--" of "---"; it
is now replaced until there is none, as the name of a file can have it.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
@xadaemon
xadaemon merged commit 8f85937 into trunk Nov 8, 2026
22 checks passed
@xadaemon
xadaemon deleted the compiler-pragmas branch November 8, 2026 02:05
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.

1 participant