From 2b304bbec73ae9afcc31f2fe84bbe97a00c100dd Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Tue, 9 Jun 2020 10:33:51 -0600 Subject: [PATCH 1/6] add readme and changelog --- .gitignore | 3 ++- CHANGELOG.md | 3 +++ README.md | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 README.md diff --git a/.gitignore b/.gitignore index f8e4b1d..5e96859 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ .antlr/ -modelicafmt \ No newline at end of file +modelicafmt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..96bfb65 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Version 0.1 + +* Support formatting of Modelica files diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ef3059 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# modelica-fmt + +The Modelica Formatter provides the ability to automatically format Modelica code providing a consistent file structure to aid in the readability of the files and the ability to compare the difference between similar files. + +## Running + +## Building + +```bash + +brew install go + +go main build +``` + + +## Updating Parser (Modelica Grammar) + + +## Known Issues + + + + + + From 4b7c1a69e6f23d1dffa8b25afb6b34a591229367 Mon Sep 17 00:00:00 2001 From: Nicholas Long <1907354+nllong@users.noreply.github.com> Date: Tue, 9 Jun 2020 10:35:53 -0600 Subject: [PATCH 2/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ef3059..640fef4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The Modelica Formatter provides the ability to automatically format Modelica cod brew install go -go main build +go build main.go ``` From 72b1b99a0c5208688ce3f805c7bd11b5640084f0 Mon Sep 17 00:00:00 2001 From: Ted Summer Date: Tue, 9 Jun 2020 12:26:18 -0600 Subject: [PATCH 3/6] refactor(readme): small improvements --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 640fef4..16226c5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,16 @@ The Modelica Formatter provides the ability to automatically format Modelica code providing a consistent file structure to aid in the readability of the files and the ability to compare the difference between similar files. -## Running +## Running + +```bash +modelicafmt [-w] [-p] [-help] ... +Options: + -w overwrite source with formatted output. If flag is not present print to stdout + -p indent on parens arguments +Arguments: + sources one or more files or directories to format +``` ## Building @@ -10,12 +19,17 @@ The Modelica Formatter provides the ability to automatically format Modelica cod brew install go -go build main.go +# in the repository root directory +go build ``` ## Updating Parser (Modelica Grammar) +If the grammar file (Modelica.g4) has been edited, you'll need to regenerate the parser by running the following commmand which runs Antlr in a Docker container. +```bash +./generate_parser.sh +``` ## Known Issues From 7f8a7de093dc131f968161dd81296ebd9bf96b8d Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Tue, 9 Jun 2020 18:54:50 -0600 Subject: [PATCH 4/6] add example --- README.md | 9 +- examples/example.mo | 224 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 examples/example.mo diff --git a/README.md b/README.md index 16226c5..6eed925 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,17 @@ Arguments: sources one or more files or directories to format ``` -## Building +To run the example: ```bash +./modelicafmt examples/example.mo > examples/example_formatted.mo +``` + +The resulting .mo file can be diffed to the previous file to compare how the modelica-fmt updates the file. +## Building + +```bash brew install go # in the repository root directory diff --git a/examples/example.mo b/examples/example.mo new file mode 100644 index 0000000..c1c19ad --- /dev/null +++ b/examples/example.mo @@ -0,0 +1,224 @@ +within a_project.B5a6b99ec37f4de7f94020090; +model building + "n-zone RC building model based on URBANopt's use of TEASER export, with distribution pumps" + extends PartialBuilding( + redeclare package Medium = MediumW, have_fan=false, have_eleHea=false, have_eleCoo=false); + + package MediumW = Buildings.Media.Water "Source side medium"; + package MediumA = Buildings.Media.Air + "Load side medium"; + parameter Integer nZon = 6 + "Number of thermal zones"; + parameter Integer facSca = 3 + "Scaling factor to be applied to on each extensive quantity"; + parameter Modelica.SIunits.TemperatureDifference delTBuiCoo=5 + "Nominal building supply and return chilled water temperature difference"; + + Buildings.Controls.OBC.CDL.Continuous.Sources.Constant minTSet[nZon]( + k=fill(293.15, nZon), + y(each final unit="K", each displayUnit="degC")) + "Minimum temperature set point" + + annotation (Placement(transformation(extent={{-290,230},{-270,250}}))); + + Buildings.Controls.OBC.CDL.Continuous.Sources.Constant maxTSet[nZon]( + k=fill(297.15, nZon), + y(each final unit="K",each displayUnit="degC")) + "Maximum temperature set point" + + + + annotation (Placement(transformation(extent={{-290,190},{-270,210}}))); + + + Meeting meeting + annotation (Placement(transformation(extent={{-160,-20},{-140,0}}))); + + Floor floor + annotation (Placement(transformation(extent={{-120,-20},{-100,0}}))); + + Storage storage + annotation (Placement(transformation(extent={{-80,-20},{-60,0}}))); + + Office office + annotation (Placement(transformation(extent={{-40,-20},{-20,0}}))); + + Restroom restroom + annotation (Placement(transformation(extent={{0,-20},{20,0}}))); + + ICT ict + annotation (Placement(transformation(extent={{40,-20},{60,0}}))); + + Buildings.Controls.OBC.CDL.Continuous.MultiSum mulSum(nin=2) if have_pum + + annotation (Placement(transformation(extent={{260,70},{280,90}}))); + + Buildings.Applications.DHC.Loads.Examples.BaseClasses.FanCoil4PipeHeatPorts + terUni[nZon]( + redeclare each package Medium1 = MediumW, + redeclare each package Medium2 = MediumA, + each facSca=facSca, + QHea_flow_nominal={10000,10000,10000, 10000,10000,10000}, + QCoo_flow_nominal={-10000,-10000, -10000,-10000,-10000,-50000}, + each T_aLoaHea_nominal=293.15, + each T_aLoaCoo_nominal=297.15, + each T_bHeaWat_nominal=35+273.15, + each T_bChiWat_nominal=12 + 273.15, + each T_aHeaWat_nominal=40 + 273.15, + each T_aChiWat_nominal=7 + 273.15, + each mLoaHea_flow_nominal=5, each mLoaCoo_flow_nominal=5) "Terminal unit" + + annotation (Placement(transformation(extent={{-200,-60},{-180,-40}}))); + + Buildings.Applications.DHC.Loads.BaseClasses.FlowDistribution disFloHea( + redeclare package Medium = MediumW, + m_flow_nominal=sum(terUni.mHeaWat_flow_nominal .* terUni.facSca), + dp_nominal(displayUnit="Pa")=100000, + have_pum=have_pum, + nPorts_a1=nZon, + nPorts_b1=nZon) + "Heating water distribution system" + + annotation (Placement(transformation(extent={{-140, -100},{-120,-80}}))); + + Buildings.Applications.DHC.Loads.BaseClasses.FlowDistribution disFloCoo( + redeclare package Medium = MediumW, + m_flow_nominal=sum(terUni.mChiWat_flow_nominal .* terUni.facSca), + typDis=Buildings.Applications.DHC.Loads.Types.DistributionType.ChilledWater, + dp_nominal(displayUnit="Pa")=100000, + have_pum=have_pum, + nPorts_a1=nZon, + nPorts_b1=nZon) + "Chilled water distribution system" + + annotation (Placement(transformation(extent={{-140, -160},{-120,-140}}))); + +equation + + connect(disFloHea.port_b, secHeaRet[1]) + annotation (Line(points={{140,-70},{240,-70},{240,32},{300,32}}, color={0,127,255})); + connect(disFloHea.port_a, secHeaSup[1]) + annotation (Line(points={{120,-70},{-242,-70},{-242,32},{-300,32}}, color={0,127,255})); + connect(disFloCoo.port_b, secCooRet[1]) + annotation (Line(points={{140,-110},{252,-110},{252,-30},{300,-30}}, color={0,127,255})); + connect(disFloCoo.port_a, secCooSup[1]) + annotation (Line(points={{120,-110},{-280,-110},{-280,-30},{-300,-30}}, color={0,127,255})); + connect(disFloHea.ports_a1, terUni.port_bHeaWat) + + + annotation (Line(points={{-120, + -80.6667},{-104,-80.6667},{-104,-58.3333},{-180,-58.3333}}, + color={0,127,255})); + connect(disFloHea.ports_b1, terUni.port_aHeaWat) annotation (Line(points={{-140, + -80.6667},{-216,-80.6667},{-216,-58.3333},{-200,-58.3333}}, + color={0,127,255})); + connect(disFloCoo.ports_a1, terUni.port_bChiWat) annotation (Line(points={{-120, + -144},{-94,-144},{-94,-56},{-180,-56},{-180,-56.6667}}, color={0,127,255})); + connect(disFloCoo.ports_b1, terUni.port_aChiWat) annotation (Line(points={{-140, + -144},{-226,-144},{-226,-56.6667},{-200,-56.6667}}, + color={0,127,255})); + + + + connect(weaBus, meeting.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[0+1].heaPorCon, meeting.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[0+1].heaPorRad, meeting.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + connect(weaBus, floor.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[1+1].heaPorCon, floor.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[1+1].heaPorRad, floor.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + connect(weaBus, storage.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[2+1].heaPorCon, storage.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[2+1].heaPorRad, storage.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + connect(weaBus, office.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[3+1].heaPorCon, office.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[3+1].heaPorRad, office.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + connect(weaBus, restroom.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[4+1].heaPorCon, restroom.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[4+1].heaPorRad, restroom.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + connect(weaBus, ict.weaBus) + annotation ( + Line(points={{1,300},{0,300},{0,20},{-66,20},{-66,-10.2},{-96,-10.2}}, color={255,204,51},thickness=0.5), + Text(string="%first",index=-1,extent={{6,3},{6,3}},horizontalAlignment=TextAlignment.Left)); + connect(terUni[5+1].heaPorCon, ict.port_a) + annotation (Line(points={{-193.333,-50},{-192,-50},{-192,0},{-90,0}}, color={191,0,0})); + connect(terUni[5+1].heaPorRad, ict.port_a) + annotation (Line(points={{-186.667,-50},{-90,-50},{-90,0}}, color={191,0,0})); + + + + connect(terUni.mReqHeaWat_flow, disFloHea.mReq_flow) + annotation (Line(points={{-179.167,-53.3333},{-179.167,-54},{-170,-54},{-170,-94},{-141,-94}}, color={0,0,127})); + connect(terUni.mReqChiWat_flow, disFloCoo.mReq_flow) + annotation (Line(points={{-179.167,-55},{-179.167,-56},{-172,-56},{-172,-154},{-141,-154}}, color={0,0,127})); + connect(mulSum.y, PPum) + annotation (Line(points={{282,80},{320,80}}, color={0,0,127})); + connect(disFloHea.PPum, mulSum.u[1]) + annotation (Line(points={{-119,-98},{240,-98},{240,81},{258,81}}, color={0,0,127})); + connect(disFloCoo.PPum, mulSum.u[2]) + annotation (Line(points={{-119,-158},{240,-158},{240,79},{258,79}}, color={0,0,127})); + connect(disFloHea.QActTot_flow, QHea_flow) + annotation (Line(points={{-119,-96},{223.5,-96},{223.5,280},{320,280}}, color={0,0,127})); + connect(disFloCoo.QActTot_flow, QCoo_flow) + annotation (Line(points={{-119,-156},{230,-156},{230,240},{320,240}}, color={0,0,127})); + connect(maxTSet.y, terUni.TSetCoo) + annotation (Line(points={{-268,200},{-240,200},{-240,-46.6667},{-200.833,-46.6667}}, color={0,0,127})); + connect(minTSet.y, terUni.TSetHea) + annotation (Line(points={{-268,240},{-220,240},{-220,-45},{-200.833,-45}}, color={0,0,127})); + + annotation ( + Documentation(info=" + +

+Building wrapper for running n-zone thermal zone models generated by TEASER. + +The heating and cooling loads are computed with a four-pipe +fan coil unit model derived from + +Buildings.Applications.DHC.Loads.BaseClasses.PartialTerminalUnit +and connected to the room model by means of heat ports. +

+", +revisions= +" +
    +
  • +May 29, 2020, by Nicholas Long:
    +Template model for use in GeoJSON to Modelica Translator. +
  • +
  • +February 21, 2020, by Antoine Gautier:
    +First implementation. +
  • +
+")); +end building; \ No newline at end of file From a512db08f6dd5b9c14f9d4afc9e3f81e857d0316 Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Tue, 9 Jun 2020 19:02:58 -0600 Subject: [PATCH 5/6] simple reorg to place antlr related code into thirdparty directory --- README.md | 2 +- examples/{example.mo => gmt-building.mo} | 0 generate_parser.sh | 4 ++-- main.go | 2 +- modelicafmt.go | 2 +- Dockerfile-Antlr => thirdparty/Dockerfile-Antlr | 0 Modelica.g4 => thirdparty/Modelica.g4 | 0 {parser => thirdparty/parser}/Modelica.interp | 0 {parser => thirdparty/parser}/Modelica.tokens | 0 {parser => thirdparty/parser}/ModelicaLexer.interp | 0 {parser => thirdparty/parser}/ModelicaLexer.tokens | 0 thirdparty/parser/README.md | 1 + {parser => thirdparty/parser}/modelica_base_listener.go | 0 {parser => thirdparty/parser}/modelica_lexer.go | 0 {parser => thirdparty/parser}/modelica_listener.go | 0 {parser => thirdparty/parser}/modelica_parser.go | 0 16 files changed, 6 insertions(+), 5 deletions(-) rename examples/{example.mo => gmt-building.mo} (100%) rename Dockerfile-Antlr => thirdparty/Dockerfile-Antlr (100%) rename Modelica.g4 => thirdparty/Modelica.g4 (100%) rename {parser => thirdparty/parser}/Modelica.interp (100%) rename {parser => thirdparty/parser}/Modelica.tokens (100%) rename {parser => thirdparty/parser}/ModelicaLexer.interp (100%) rename {parser => thirdparty/parser}/ModelicaLexer.tokens (100%) create mode 100644 thirdparty/parser/README.md rename {parser => thirdparty/parser}/modelica_base_listener.go (100%) rename {parser => thirdparty/parser}/modelica_lexer.go (100%) rename {parser => thirdparty/parser}/modelica_listener.go (100%) rename {parser => thirdparty/parser}/modelica_parser.go (100%) diff --git a/README.md b/README.md index 6eed925..195656f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Arguments: To run the example: ```bash -./modelicafmt examples/example.mo > examples/example_formatted.mo +./modelicafmt examples/gmt-building.mo > examples/gmt-building-out.mo ``` The resulting .mo file can be diffed to the previous file to compare how the modelica-fmt updates the file. diff --git a/examples/example.mo b/examples/gmt-building.mo similarity index 100% rename from examples/example.mo rename to examples/gmt-building.mo diff --git a/generate_parser.sh b/generate_parser.sh index a08c75a..cf47b3d 100755 --- a/generate_parser.sh +++ b/generate_parser.sh @@ -1,7 +1,7 @@ #! /bin/bash -docker build -t antlr4:latest -f Dockerfile-Antlr . +docker build -t antlr4:latest -f thirdparty/Dockerfile-Antlr . -docker run -v "$(pwd)":/var/antlrResult \ +docker run -v "$(pwd)/thirdparty":/var/antlrResult \ antlr4:latest \ -Dlanguage=Go -o /var/antlrResult/parser /var/antlrResult/Modelica.g4 diff --git a/main.go b/main.go index 3ccceda..f33ed2d 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( ) var ( - parens = flag.Bool("p", false, "always break and indent contents in parens") + parens = flag.Bool("p", false, "always break and indent contents in parentheses") write = flag.Bool("w", false, "overwrite the file(s)") ) diff --git a/modelicafmt.go b/modelicafmt.go index 0a4a975..c35aa53 100644 --- a/modelicafmt.go +++ b/modelicafmt.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/antlr/antlr4/runtime/Go/antlr" - "github.com/urbanopt/modelicafmt/parser" + "github.com/urbanopt/modelicafmt/thirdparty/parser" ) var alwaysIndentParens = false diff --git a/Dockerfile-Antlr b/thirdparty/Dockerfile-Antlr similarity index 100% rename from Dockerfile-Antlr rename to thirdparty/Dockerfile-Antlr diff --git a/Modelica.g4 b/thirdparty/Modelica.g4 similarity index 100% rename from Modelica.g4 rename to thirdparty/Modelica.g4 diff --git a/parser/Modelica.interp b/thirdparty/parser/Modelica.interp similarity index 100% rename from parser/Modelica.interp rename to thirdparty/parser/Modelica.interp diff --git a/parser/Modelica.tokens b/thirdparty/parser/Modelica.tokens similarity index 100% rename from parser/Modelica.tokens rename to thirdparty/parser/Modelica.tokens diff --git a/parser/ModelicaLexer.interp b/thirdparty/parser/ModelicaLexer.interp similarity index 100% rename from parser/ModelicaLexer.interp rename to thirdparty/parser/ModelicaLexer.interp diff --git a/parser/ModelicaLexer.tokens b/thirdparty/parser/ModelicaLexer.tokens similarity index 100% rename from parser/ModelicaLexer.tokens rename to thirdparty/parser/ModelicaLexer.tokens diff --git a/thirdparty/parser/README.md b/thirdparty/parser/README.md new file mode 100644 index 0000000..111506c --- /dev/null +++ b/thirdparty/parser/README.md @@ -0,0 +1 @@ +These files are automatically generated using Antlr. Do not manipulate directly. diff --git a/parser/modelica_base_listener.go b/thirdparty/parser/modelica_base_listener.go similarity index 100% rename from parser/modelica_base_listener.go rename to thirdparty/parser/modelica_base_listener.go diff --git a/parser/modelica_lexer.go b/thirdparty/parser/modelica_lexer.go similarity index 100% rename from parser/modelica_lexer.go rename to thirdparty/parser/modelica_lexer.go diff --git a/parser/modelica_listener.go b/thirdparty/parser/modelica_listener.go similarity index 100% rename from parser/modelica_listener.go rename to thirdparty/parser/modelica_listener.go diff --git a/parser/modelica_parser.go b/thirdparty/parser/modelica_parser.go similarity index 100% rename from parser/modelica_parser.go rename to thirdparty/parser/modelica_parser.go From 85477492eb9933fa10a8ad42d6116fe5b1f1d9ed Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Tue, 9 Jun 2020 19:04:17 -0600 Subject: [PATCH 6/6] code cleanup --- modelicafmt.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modelicafmt.go b/modelicafmt.go index c35aa53..89b8744 100644 --- a/modelicafmt.go +++ b/modelicafmt.go @@ -25,17 +25,17 @@ const ( func insertIndentBefore(rule antlr.ParserRuleContext) bool { switch rule.(type) { case - parser.IElementContext, - parser.IEquationsContext, - parser.IAlgorithm_statementsContext, - parser.IControl_structure_bodyContext, - parser.IString_commentContext, - parser.IAnnotationContext, - parser.IExpression_listContext: + parser.IElementContext, + parser.IEquationsContext, + parser.IAlgorithm_statementsContext, + parser.IControl_structure_bodyContext, + parser.IString_commentContext, + parser.IAnnotationContext, + parser.IExpression_listContext: return true case - parser.IArgumentContext, - parser.INamed_argumentContext: + parser.IArgumentContext, + parser.INamed_argumentContext: return alwaysIndentParens default: return false @@ -46,8 +46,8 @@ func insertIndentBefore(rule antlr.ParserRuleContext) bool { func insertNewlineBefore(rule antlr.ParserRuleContext) bool { switch rule.(type) { case - parser.ICompositionContext, - parser.IEquationsContext: + parser.ICompositionContext, + parser.IEquationsContext: return true default: return false