Repository navigation
Conversation
METADATA blocks may now follow SHELL in the instrument header. They are added to the metadata table with the instrument name as source. A quoted metadata name is stored without its quotes, so it is found by the same key as an identifier name. Fix --meta-list (double free, printed only the last source) and the crash on keys without a source part, e.g. "::". ADR: docs/GRAMMAR/ADR-records/ADR_20261123_INSTRUMENT_METADATA.md
|
This grammar change implements the part of I finally have a real use for it, so this proposed grammar change actually implements the rule (and regularizes keyname behavior with The PR is ready except for a decision on the ADR -- do we have a procedure for accepting proposed ADRs? |
|
Thanks @g5t, useful change and nicely small: the merged On the ADR: there is no formal procedure. The ADR README only asks to keep the status updated and to link the PR. Earlier ones say e.g. "Accepted, with #2895". I am fine with accepting this one, so please set that status with this PR number once the points below are settled. I would not call it ready except for the ADR, though: points 1 and 2 need a change first. Tested (macOS,
|
| winner | source | |
|---|---|---|
| this PR | incA | my_instr |
| mccode-antlr | incB | incB |
-
Order: collecting the blocks when the header is parsed fixes it and replaces the prepend and its comment. I built this: no conflicts, and incB wins.
instr_metadata: metadata { if (!instrument_metadata) instrument_metadata = $1; else list_cat(instrument_metadata, $1); } ;The instrument rule then uses
instr_metadatainstead ofmetadata. -
Source: you know best which is intended. I am fine with the master name here, but then please drop "as in mccode-antlr" from that sentence in the PR text and ADR, and "give the same metadata table" from the ADR (or change mccode-antlr to match).
2. mcstas-pygen loses instrument METADATA. The blocks have instance == 0, so they end up in component_definition_metadata, which the generated script never uses. Please give them their own instance value and write them out from instr->metadata (already de-duplicated).
McStasScript has no instrument-level METADATA yet (0.0.97), so an unguarded call would stop make() with an AttributeError. Suggestion:
if hasattr(instr, 'add_METADATA'):
instr.add_METADATA(name='config', type='application/json', value=...)
else:
print("Warning: this McStasScript drops instrument METADATA 'config'", file=sys.stderr)(The generated script needs import sys.) McStasScript then needs a separate PR: an instrument add_METADATA, writing the blocks before DEPENDENCY. The method name is my assumption, we should agree with @mads-bertelsen about naming.
3. pygen does not escape the block text (existing problem, but JSON instrument blocks will hit it). The text goes unescaped into """...""", so {"x": "say \"hi\"\n"} becomes invalid JSON in Python, and """ in the text would break the script.
Minor
:namedoes not segfault onmainfor--meta-type,--meta-definedor--meta-data: it exits 1 cleanly. Only::crashed for me, so please correct the PR text and ADR.- While you are in the accessors:
metadata_table_namereturns a freed pointer when the key is not found (if (!found) free(name);withoutname = NULL).File.compdoes not reach it today. - A regression test would be good. A header block in the existing
test_File.instr(McStas and McXtrace) is enough. - Please fill in the PR template, including the AI declaration.
|
Quick comment here, have not checked everything yet. It will probably be simple enough to add on the McStasScript side. Should instruments support several metadata entries as well? Was there a good reason not to use the PR template here? It forces us to record a lot of useful information and checks. |
Each instrument's METADATA keeps that instrument as its source, also when it is %include-d, and only a block with the same source and name replaces another. Before, included blocks were filed under the master instrument and a repeated name kept whichever was parsed first, so the order of %includes decided which one survived. A quoted metadata name with spaces keeps its quotes, as in mccode-antlr, so it stays one entry in the space-separated --meta-defined output. ADR: correct the crashing keys (only "::" crashed, not ":name") and note the matching mccode-antlr change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Instrument METADATA was mixed into the component-definition metadata, which the generated script does not use. Mark it with instance 2 and write it with instr.add_METADATA, guarded by hasattr since McStasScript has no instrument METADATA yet; without it the script warns and continues. METADATA text was written as a plain triple-quoted string, so Python turned escapes like \" and \n into other characters and JSON blocks stopped parsing. Write a raw string instead, as for the other code blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_File (McStas and McXtrace) gets a header METADATA block, written to third.json by a third File instance, so the instrument-level path is exercised whenever the example runs. metadata_table_name freed the name it was given when the key was not found, and returned it anyway. Return NULL instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Checks metadata-r with instrument and instance METADATA: lookup by source and by source:name, quoted names with and without spaces, the --meta-list listing, a missing key and the key "::". It exits in INITIALIZE if a check fails. McStas and McXtrace. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Link the matching mccode-antlr change, mccode-antlr#415. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No, not a good reason -- |
|
Thanks @willend and @mads-bertelsen. All points are addressed in the commits after 3300a5f, and the description now follows the template, including the AI declaration. 1. 2. pygen drops instrument METADATA. Fixed in de4a54d. Instrument blocks get 3. pygen escaping. Also in de4a54d. METADATA text is now a raw string, Minor
ADR set to "Accepted, with #3180" (f9a647f). Not tested: macOS, Windows, a real McStasScript |
|
Thanks @g5t, all points are resolved, and I am happy to approve this once it is out of draft. Rechecked at f9a647f (macOS)
Notes, not blocking
|
Thanks for clarifying, I wonder if we can make it easier to include the template when using gh, perhaps there is some repo setting to enforce it. Would like to review this and at least sketch the McStasScript interface before merge, but teaching the next couple of days. |
Free-form text area
Please describe what your PR is adding in terms of features or bugfixes:
Instrument-level
METADATA, as in mccode-antlr.METADATA type name %{ ... %}worked for component definitions and instances. The grammar docs also listed it for instruments, butinstrument.yrejected it there with a syntax error. mccode-antlr has always accepted it in the instrument header, so instruments written with it (e.g. the niess-generated ESS BIFROST model) could not be translated bymcstas.ADR (proposed):
docs/GRAMMAR/ADR-records/ADR_20261123_INSTRUMENT_METADATA.mdinstrument.y): zero or moreMETADATAblocks may followSHELLand come beforeDEPENDENCYin the instrument header, the same place as in mccode-antlr. This is the only unambiguous position: afterTRACE, aMETADATAline could belong to the last component instance or to the instrument (2 shift/reduce conflicts).metadata.c): instrument blocks are added after the component blocks. Each block has the instrument that defined it as its source, also when that instrument is%included, soinc:dupandmy_instr:dupare separate entries. Only a block with the same source and name replaces another, e.g. a name repeated in one header. mccode-antlr needs the same change: it kept the source but replaced by name alone, so later includes silently removed earlier blocks. That is fixed in mccode-antlr#415, merged but not yet released.METADATA text "notes"was stored as"notes", quotes included, socomp:notesnever found it. It is now stored asnotes. As in mccode-antlr, a name with spaces keeps its quotes (comp:"two words"), so it stays one entry in the space-separated--meta-definedoutput. The fourmetadatumalternatives collapse into one rule. No.compor.instrin the repository uses a quoted name.metadata-r.c). These bugs also hit existing component metadata:--meta-listaborted withdouble freefor any instrument with metadata. Without the crash it would have printed only the last source.::segfaulted--meta-defined,--meta-typeand--meta-data, andFile(metadatakey="::")in INITIALIZE.metadata_table_namereturned a freed pointer when the key was not found.File.compdoes not reach that path today.pygen.c.in): instrument METADATA was mixed into the component-definition metadata, which the generated script never uses. It now getsinstance == 2and is written asinstr.add_METADATA(...), guarded byhasattr. McStasScript 0.0.97 has no instrument METADATA yet, so the script warns on stderr and continues. METADATA text is now a raw string (r"""..."""), so\"and\nin JSON survive; before, a JSON block with escapes no longer parsed. Text containing"""is still not escaped, as for the other code blocks with'''. McStasScript needs a separate PR for an instrumentadd_METADATA.Test_lib_metadatainexamples/libtest/(McStas and McXtrace) checks themetadata-raccessors with instrument and instance METADATA: lookup by source and bysource:name, quoted names with and without spaces,--meta-list(metadata_table_print_all_components), a missing key, and::. It exits in INITIALIZE if a check fails;%Test: Ncount=10 Detector: PSD_I=10.test_File.instr(McStas and McXtrace) has a header METADATA block, which a thirdFilewrites tothird.json.Filestops with an error if the key is missing.INSTRUMENT_Grammar.mdnow show where instrument METADATA goes.INSTRUMENT_Grammar.htmlis not regenerated.Behaviour change: a quoted metadata name without spaces used to be looked up with its quotes (
comp:"notes"). It is now looked up without them (comp:notes). Names with spaces are unchanged.Example:
Tests:
mainapart from the date. pygen output differs only byimport sysand therprefix on METADATA strings.%included metadata (repeated names at each level) gives the same metadata table as mccode-antlr with #415. The--meta-*options, including the::key, show no AddressSanitizer or UBSan errors.test_Fileruns for McStas and McXtrace (under AddressSanitizer and UBSan), and with mccode-antlr, writing the instrument block tothird.json.add_METADATA, the JSON values round-trip exactly; onmainthe instance JSON fails to parse.Test_lib_metadatapasses for McStas and McXtrace (PSD_I=10, under AddressSanitizer and UBSan). With the 3.10.2 runtime it fails at the missing-key check and then aborts in the--meta-listcode.Declaration of use of AI-tools
Please add a checkmark here if you used AI-tools during the work for this contribution
Further, please describe how / where and for what the tools were used:
Claude Code (Claude Opus 5.5) was used throughout, directed and reviewed by me:
instrument.ywith the mccode-antlr grammar, and finding the conflict-free position for instrument METADATA;instrument.y,metadata.c,metadata-r.c,pygen.c.inandtest_File.instr, the ADR and the grammar doc;The design decisions were mine: header placement, keying by (source, name), and keeping mccode-antlr's quoted-name rule.
AI-assisted commits must carry a
Co-Authored-Byline naming the tool, see CONTRIBUTING.mdThe commits after the first carry
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>. The first commit (3300a5f) was pushed without it, and I did not rewrite the pushed history to add it.Development OS / boundary conditions
Please describe what OS you developed and tested your additions on, and if any special dependencies are required:
Linux x86_64 (devcontainer), conda-forge gcc 15.3.0, bison 3.8.2, flex 2.6.4, CMake 4.5.1. The McXtrace build used conda-forge gfortran 16.2.0. mccode-antlr was used from its
main(v0.37.0+) with the #415 change. No new dependencies.Not tested: macOS, Windows/MSVC, a real McStasScript
make(), and McXtrace's pygen. willend built and checked McStas and McXtrace on macOS at 3300a5f (see the review comment).PR Checklist for contributing to McStas/McXtrace
For a coherent and useful contribution to McStas/McXtrace, please fill in relevant parts of the checklist:
My contribution includes patches to an existing instrument file
mcdocutility and rendered a reasonable documentation page for the instrument (please attach as screenshot in comments!)mctestutility to test the instrument (please attachmcviewtestreport as screenshot in comments)mcrun --c-lint"linter" and followed advice to remove most / all warnings that are raisedMy work touches the code-generator in mccode/src
My work touches / adds to the runtime lib code (.c,.h etc in multiple locations
common/lib/share,mccode/nlib/share,mcstas-comps/shareormcxtrace-comps/sharelibrary and runtime file (.c,.h,.h.in) has aTest_lib_<name>instrument inexamples/libtest/(McStas and/or McXtrace), with a%Test:line where the result can be checked. If it cannot be standalone, say why in the free-form text (e.g. needs a component or external package)My PR is meant to fix a specific, existing issue