Skip to content

Commit

Permalink
Merge pull request #3295 from modelica/MCP/0031+source-locations
Browse files Browse the repository at this point in the history
Web Meeting (Hans, Henrik, Gerd, Martin, Oliver)
approved
  • Loading branch information
olivleno committed Apr 18, 2023
2 parents 5a996ba + 415d4b5 commit 820dad4
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
2 changes: 1 addition & 1 deletion RationaleMCP/0031/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ These are subtopics that are considered necessary to resolve for a first version
- [x] Figure out what to do with synchronous features. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsynchronous/RationaleMCP/0031/differences.md#clock-partitions), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3240)
- [x] Event handling semantics is preserved as in Modelica.
- [x] Get rid of `when initial()` and `when`-equations inside `if` and `for`. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsimplify-when/RationaleMCP/0031/differences.md#when-equations), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3258)
- [ ] Source locations pointing back to the original Modelica code. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsource-locations/RationaleMCP/0031/source-locations.md), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3295)
- [x] Source locations pointing back to the original Modelica code. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsource-locations/RationaleMCP/0031/source-locations.md), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3295)
- [ ] Settle the name (currently _Flat Modelica_), considering that scalarization isn't mandatory. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bname-of-the-game/RationaleMCP/0031/name-of-the-game.md), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3224)

### Flat Modelica 0.1+…1.0 (future MCPs)
Expand Down
31 changes: 19 additions & 12 deletions RationaleMCP/0031/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ The _S-CHAR_ accepts Unicode other than " and \\:
> _flat-modelica_\
>   _VERSION-HEADER_\
>   **package** _IDENT_\
>    ( _class-definition_ **;**\
>    | _global-constant_ **;**\
>    ( _decoration_? _class-definition_ **;**\
>    | _decoration_? _global-constant_ **;**\
>    )*\
>    **model** _long-class-specifier_ **;**\
>    _decoration_? **model** _long-class-specifier_ **;**\
>    ( _annotation-comment_ **;** )?
>   **end** _IDENT_ **;**
Here, the _VERSION-HEADER_ is a Flat Modelica variant of the not yet standardized language version header for Modelica proposed in [MCP-0015](https://github.com/modelica/ModelicaSpecification/tree/MCP/0015/RationaleMCP/0015):
Expand Down Expand Up @@ -129,14 +130,14 @@ end _F;
> _enumeration-literal__IDENT_ _comment_
> _composition_\
>   (_generic-element_ **;**)* \
>   (_decoration_? _generic-element_ **;**)* \
>   ( **equation** ( _equation_ **;** )* \
>   | **initial** **equation** ( _initial-equation_ **;** )* \
>   | **initial**? **algorithm** ( _statement_ **;** )* \
>   ~~| **public** (_generic-element_ **;**)*~~ \
>   ~~| **protected** (_generic-element_ **;**)*~~ \
>   )* \
>   ( **external** _language-specification_?\
>   ( _decoration_? **external** _language-specification_?\
>    _external-function-call_? _annotation-comment_? **;**\
>   )?\
>   _base-partition_* \
Expand Down Expand Up @@ -189,7 +190,7 @@ end _F;
>   | **algorithm** ( _statement_ **;** )* \
>   )*
> _clock-clause_**Clock** _IDENT_ **=** _expression_ _comment_
> _clock-clause__decoration_? **Clock** _IDENT_ **=** _expression_ _comment_

## B23 Extends
Expand Down Expand Up @@ -230,7 +231,7 @@ end _F;
> _argument-list__argument_ ( **,** _argument_ )*
> _argument_\
>  _element-modification-or-replaceable_\
>  _decoration_? _element-modification-or-replaceable_\
>   ~~| _element-redeclaration_~~
> _element-modification-or-replaceable_\
Expand Down Expand Up @@ -265,7 +266,8 @@ end _F;
## B26 Equations

> _equation_\
>   ( _simple-expression_ ( **=** _expression_ )?\
>   _decoration_?\
>   ( _simple-expression_ _decoration_? ( **=** _expression_ )?\
>   | _if-equation_\
>   | _for-equation_\
>   ~~| _connect-clause_~~\
Expand All @@ -276,6 +278,7 @@ end _F;
> _initial-equation__equation_ | _prioritize-equation_
> _statement_\
>   _decoration_?\
>   ( _component-reference_ ( **:=** _expression_ | _function-call-args_ )\
>   | `[(]` _output-expression-list_ `[)]` **:=** _component-reference_ _function-call-args_\
>   | **break**\
Expand Down Expand Up @@ -355,12 +358,16 @@ end _F;

## Expressions

> _expression__simple-expression_ | _if-expression_
> _decoration_**@** UNSIGNED-INTEGER
> _expression__expression-no-decoration_ _decoration_?
> _expression-no-decoration__simple-expression_ | _if-expression_
> _if-expression_\
>   **if** _expression_ **then** _expression_\
>   ( **elseif** _expression_ **then** _expression_ )* \
>   **else** _expression_
>   **if** _expression-no-decoration_ **then** _expression-no-decoration_\
>   ( **elseif** _expression-no-decoration_ **then** _expression-no-decoration_ )* \
>   **else** _expression-no-decoration_
> _simple-expression__logical-expression_ ( **:** _logical-expression_ ( **:** _logical-expression_ )? )?
Expand Down
123 changes: 123 additions & 0 deletions RationaleMCP/0031/source-locations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Source locations

To fully serve as a format for intermediate representation of translation of full Modelica, the Flat Modelica code can be decorated with source location information pointing back to the original full Modelica model (or other source of the Flat Modelica code).

Each _source location decoration_ (or simply _decoration_ in this context) attaches some kind of source location information to a specific construct in the Flat Modelica code.

Design decisions include:
- Details of what a source location consists of: standardized or open for tool-specific choices?
- Syntax of information included directly in a decoration.
- Syntax for how a decoration is attached to a Flat Modelica construct.
- How to handle need for (optional) big tables of verbose source location data.


## Design goals

Some design goals to keep in mind when comparing the design alternatives:
- The detailed format of a source location needs to be flexible to serve the needs of different tools.
- A tool should be able to ignore source locations originating from other tools.
- Source locations may be needed on many levels of the Flat Modelica code, including component declarations, equations, modifications, and expressions.
- Reduced Flat Modelica soruce code human readability is a concern when soruce locations are added.
- Simple compression of the source locations could be handy.



## Tool-specific table of source location details

The Flat Modelica package level annotation may have a `SourceLocations`-annotation.
There is no standardized content for this annotation, so it is currently only possible to use with vendor-specific annotations inside.
For example:
```
package 'MyModel'
model 'MyModel'
end 'MyModel';
annotation(SourceLocations(__Wolfram(sourceLocationsFile = "MyModel.loc")));
end 'MyModel';
```

As an alternative to pointing to a vendor-specific external file, it is also possible to include information directly in the annotation:
```
annotation(SourceLocations(__OpenModelica(
table = {
SourceLocation(file = "MoModel.mo", start = {1, 2}, end = {1, 7}),
SourceLocation(…),
}));
```

In the future, there could be standardized contents for the `SourceLocations` that all Flat Modelica tools are expected to recognize.


## Syntax of information present in a source location decoration

### Design alternatives

Alternatives include:
- Just an integer to be used with a separate table of details provided by `SourceLocations`-annotation.
- Predefined record to avoid need for tool-specific table. (Details to be designed).
- Use `annotation(SourceLocation(…))` to avoid need for attachment operator.

The alternatives above could also be combined in different ways:
- Number of supported formats:
- Standardize on just one of the alternatives.
- Allow more than one of the alternatives.
- Number of informations included in a single source location decoration
- Just a single piece of information.
- Multiple pieces of information (possibly in different formats).

### Conclusion

For now, only allow a single integer. It would be easy to extend this in future versions of Flat Modelica.


## Syntax for source location decoration attachment

Given a decision on what information that should be present in a decoration, the next question is which syntax to use for attaching it to a Flat Modelica construct.

### Design alternatives

#### Low precedence infix `@` operator

Use infix `@` as decoration attachment operator:
```
package 'MyModel'
model 'MyModel'
parameter Real 'p' = 42 @21; // Attach 21 to binding expression "42"
Real 'x'(min = 0 @22); // Attach 22 to modification "0"
Real 'y' = @23 'x'; // Attach 23 to component declaration
equation
der('x') = @24 1; // Attach 24 to equation
der('y') = 1 @25; // Attach 25 to "1"
end 'MyModel';
annotation(SourceLocations(…));
end 'MyModel';
```

The precedence of `@` is lower than the expression operators, so that no parentheses are needed when attaching a source location to a modification or binding.

#### Low precedence infix `!` operator

Similar to the `@` operator above, but using `!` instead.

#### Allow use of annotations in new places

Instead of using an operator, one could use annotations if allowing them in new places:
```
package 'MyModel'
model 'MyModel'
parameter Real 'p' = (42 annotation(SourceLocation = 21)); // Attach 21 to binding expression "42"
Real 'x'(min = 0 annotation(SourceLocation = 22)); // Attach 22 to modification "0"
Real 'y' = 'x' annotation(SourceLocation = 23)); // Attach 23 to component declaration
equation
der('x') = 1 annotation(SourceLocation = 24); // Attach 24 to equation
der('y') = (1 annotation(SourceLocation = 25)); // Attach 25 to "1"
end 'MyModel';
annotation(SourceLocations(…));
end 'MyModel';
```


### Conclusions

None yet.

0 comments on commit 820dad4

Please sign in to comment.