Unparse models to the same order as defined in script used to parse
When a user parses kat script to a model, changes some things and unparses to kat script again, there should be an option (probably on by default) which makes the generator write the same instructions back to the file in the same order they appear in the input script.
If the user only modifies parameter values then this is trivial, but if they add or remove components this gets more complicated. At that point it should probably just fall back to adding new lines under the first or last instruction that the new lines depend on, else add it to the end.
We could have an in-place option that overwrites the input file (first writing to a temporary file and replacing, so the operation i atomic). We probably also would then need some user-configurable backup policy for this, storing the last N
versions as e.g. input_file.kat.backup-2020-10-23-10:34:30
.
Update from 2020-11-26 telecon
We discussed this and decided that retaining comments and whitespace from parsed files in unparsed files is a key feature for the final v1 release of Finesse 3, since this will be the primary paradigm we suggest users follow when modelling detectors (where you need to perform multiple simulations, change things, and run them again later - and pass files around different users who might add their own comments etc.). However, these changes will not be required for alpha 1 since they don't affect the actual kat script syntax behaviour itself - which is what we want to primarily demonstrate there. This means that when alpha 1 gets released, we should tell users that the unparser is only there for demonstration but not as something to yet rely on, since its behaviour will likely change later.
Todo
-
Retain the parsed script style (including comments, whitespace, etc.) in parsed models and use this to generate kat script from a model - requires whitespace, comments, etc. to be tokenised and retained in the AST. Probably requires a new parsing algorithm (e.g. PEG parser used by Python 3.9).