Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix component instance METADATA declaration parsing #3

Merged
merged 2 commits into from
Nov 15, 2023
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
8 changes: 6 additions & 2 deletions mccode_antlr/instr/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def visitComponent_instance(self, ctx: McInstrParser.Component_instanceContext):
# deal with definition vs instance metadata here?
for metadata_context in ctx.metadata():
mime, name, metadata = self.visit(metadata_context)
instance.add_metadata(MetaData.from_component_tokens(name, str(ctx.mime), str(ctx.name), metadata))
instance.add_metadata(MetaData.from_component_tokens(name, mime, name, metadata))
# Include this instantiated component instance in the instrument components list
if self.destination is None or not instance.removable:
# if this _is_ an included instrument, any REMOVABLE component instances should not be added
Expand Down Expand Up @@ -321,7 +321,11 @@ def visitExtend(self, ctx: McInstrParser.ExtendContext):

def visitMetadata(self, ctx: McInstrParser.MetadataContext):
filename, line_number, metadata = self.visit(ctx.unparsed_block())
return str(self.visit(ctx.mime)), str(self.visit(ctx.name)), metadata
# ctx.mime and ctx.name are _either_ identifiers (no double quotes) or string literals (double quotes)
# so we need to strip the quotes from the string literals, but not the identifiers
mime = ctx.mime.text if ctx.mime.type == McInstrParser.Identifier else ctx.mime.text[1:-1]
name = ctx.name.text if ctx.name.type == McInstrParser.Identifier else ctx.name.text[1:-1]
return mime, name, metadata

def visitUnparsed_block(self, ctx: McInstrParser.Unparsed_blockContext):
# We want to extract the source-file line number (and filename) for use in the C-preprocessor
Expand Down
2 changes: 1 addition & 1 deletion mccode_antlr/io/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class HDF5IO:
_handlers = {
'Instr': InstrIO,
'InstrumentParameter': _dataclass_io(InstrumentParameter, attrs=('name', 'unit'), required=('value',)),
'MetaData': _dataclass_io(MetaData, attrs=('name', 'unit', 'value'), required=('source',)),
'MetaData': _dataclass_io(MetaData, attrs=('name', 'mimetype', 'value'), required=('source',)),
'DataSource': DataSourceIO,
'Instance': InstanceIO,
'RawC': _dataclass_io(RawC, attrs=('filename', 'line'), required=('source',), optional=('translated',)),
Expand Down
3 changes: 3 additions & 0 deletions test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def test_one_axis(self):
AT (0, 0, 1.5) RELATIVE PREVIOUS
COMPONENT guide_end = Arm() AT (0, 0, 30) RELATIVE PREVIOUS
COMPONENT aperture = Slit(xwidth=virtual_source_x, yheight=virtual_source_y) AT (0, 0, 0.01) RELATIVE PREVIOUS
METADATA "txt" "something" %{{
This is some unparsed metadata that will be included as a literal string in the instrument.
%}}
COMPONENT split_at = Arm() AT (0, 0, 0.0001) RELATIVE PREVIOUS
COMPONENT mono_point = Arm() AT (0, 0, 0.8) RELATIVE split_at
COMPONENT mono = Monochromator_curved(zwidth = 0.02, yheight = 0.02, NH = 13, NV = 7, DM={d_spacing})
Expand Down