urbanopt / modelica-fmt

BSD 3-Clause "New" or "Revised" License
20 stars 6 forks source link

Research possibility of formatting template files #5

Closed macintoshpie closed 4 years ago

macintoshpie commented 4 years ago

Would be nice to be able to format the modelica code in the template files used by geojson-modelica-translator (e.g. https://github.com/urbanopt/geojson-modelica-translator/blob/develop/geojson_modelica_translator/model_connectors/templates/CoolingIndirect.mot)

macintoshpie commented 4 years ago

TLDR; This is possible, but not trivial. After discussion we decided to not further pursue this at this time.

Findings

Antlr provides the ability to switch between parsing "modes", s.t. when a certain token is "seen" by the lexer, it switches to a different collection of lexer rules. So in order to format the modelica code between {% raw %} and {% endraw %}, we could have our lexer grammar look something like this

lexer grammar ModelicaLexer;

// This is the default mode, it should scan until we encounter the start of raw modelica
START_RAW
   : '{% raw %}' -> mode(RAW_MODELICA_MODE)
   ;

mode RAW_MODELICA_MODE;
END_RAW
   : '{% endraw %}' -> mode(DEFAULT_MODE)
   ;

// put all existing modelica lexer rules here
// ...

The issue here is that we'd need to refactor our current grammar file, as it uses a "combined grammars" file (both parser and lexer rules in single file). To use the modes it MUST be in a separate lexer grammar file. If I remember correctly the issue then becomes that we have literals defined in our "parser" grammar file (e.g. the text 'annotation' in the parser rule), though this should be reconfirmed if trying to make this happen.

If you manage to refactor the grammar files, the issue then is how to use those grammars when parsing both template files and actual modelica files. I believe the lexer method SetMode can fix this, where if we are parsing an actual modelica file then we just force the file to enter the "raw mode".

Resources

See ANTLR the definitive guide section "Islands in the stream" for more info on island grammars Also see https://github.com/antlr/antlr4/tree/master/doc for more general info