Skip to content

Commit 70521cf

Browse files
authored
Modelica language support #1122 (#1129)
Add XML lexer and test files for Modelica language support
1 parent 09cd573 commit 70521cf

File tree

3 files changed

+1833
-0
lines changed

3 files changed

+1833
-0
lines changed

lexers/embedded/modelica.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<lexer>
2+
<config>
3+
<name>Modelica</name>
4+
<alias>modelica</alias>
5+
<filename>*.mo</filename>
6+
<mime_type>text/x-modelica</mime_type>
7+
<dot_all>true</dot_all>
8+
<ensure_nl>true</ensure_nl>
9+
</config>
10+
<rules>
11+
<state name="root">
12+
<!-- Comments -->
13+
<rule pattern="//[^\n\r]*">
14+
<token type="CommentSingle"/>
15+
</rule>
16+
<rule pattern="/[*].*?[*]/">
17+
<token type="CommentMultiline"/>
18+
</rule>
19+
20+
<!-- Whitespace and newlines -->
21+
<rule pattern="\s+">
22+
<token type="Text"/>
23+
</rule>
24+
<rule pattern="\n">
25+
<token type="Text"/>
26+
</rule>
27+
28+
<!-- Keywords (Modelica specific) -->
29+
<rule pattern="\b(model|equation|end|extends|function|type|record|connector|parameter|constant|if|then|else|for|in|when|assert|outer|algorithm|flow|discrete|input|output|loop|elseif|return|public|protected|external|real|integer|boolean|string|array|complex|union|tuple|class|der|time|tstart|tstop)\b">
30+
<token type="Keyword"/>
31+
</rule>
32+
33+
<!-- Type definitions -->
34+
<rule pattern="\b(boolean|integer|real|string|array|record|complex|union|tuple)\b">
35+
<token type="KeywordType"/>
36+
</rule>
37+
38+
<!-- Preprocessor directives -->
39+
<rule pattern="#[ \t]*(if|else|endif|define|include|error)\b">
40+
<token type="CommentPreproc"/>
41+
</rule>
42+
43+
<!-- Literals -->
44+
<rule pattern="\\&quot;[^&quot;\n]*\\&quot;">
45+
<token type="LiteralString"/>
46+
</rule>
47+
<rule pattern="'[^'\n]'">
48+
<token type="LiteralStringChar"/>
49+
</rule>
50+
<rule pattern="\d+(\.\d+)?([eE][+-]?\d+)?">
51+
<token type="LiteralNumber"/>
52+
</rule>
53+
54+
<!-- Identifiers and names -->
55+
<rule pattern="@[A-Za-z_]\w*">
56+
<token type="Name"/>
57+
</rule>
58+
<rule pattern="[A-Za-z_]\w*">
59+
<token type="Name"/>
60+
</rule>
61+
62+
<!-- Punctuation -->
63+
<rule pattern="[{}(),;=.+\-*/&amp;|!&lt;&gt;^%]">
64+
<token type="Punctuation"/>
65+
</rule>
66+
67+
<!-- Operators -->
68+
<rule pattern="(\+|\-|\*|\/|\^|\&amp;|\||\&lt;|\&gt;|\%|\=|\!=|\&lt;\=|\&gt;\=)">
69+
<token type="Operator"/>
70+
</rule>
71+
</state>
72+
73+
<state name="function">
74+
<!-- Function name -->
75+
<rule pattern="[A-Za-z_]\w*">
76+
<token type="NameFunction"/>
77+
<pop depth="1"/>
78+
</rule>
79+
<rule>
80+
<pop depth="1"/>
81+
</rule>
82+
</state>
83+
84+
<state name="type">
85+
<!-- Type definition -->
86+
<rule pattern="[A-Za-z_]\w*">
87+
<token type="NameClass"/>
88+
<pop depth="1"/>
89+
</rule>
90+
<rule>
91+
<pop depth="1"/>
92+
</rule>
93+
</state>
94+
95+
<state name="record">
96+
<!-- Record definition -->
97+
<rule pattern="[A-Za-z_]\w*">
98+
<token type="NameClass"/>
99+
<pop depth="1"/>
100+
</rule>
101+
<rule>
102+
<pop depth="1"/>
103+
</rule>
104+
</state>
105+
</rules>
106+
</lexer>

lexers/testdata/modelica.actual

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
2+
within TestKeywords;
3+
4+
package TestKeywords
5+
// -----------------------------------------
6+
// Basic classes and types
7+
// -----------------------------------------
8+
record MyRecord
9+
parameter Real r = 1.0; // 'parameter', 'Real'
10+
Integer i; // 'Integer'
11+
end MyRecord; // 'record', 'end'
12+
13+
connector MyConnector
14+
flow Real f; // 'connector', 'flow', 'Real'
15+
stream String s; // 'stream', 'String'
16+
input Real inVar; // 'input'
17+
output Real outVar; // 'output'
18+
end MyConnector;
19+
20+
type MyType = Real; // 'type', 'Real'
21+
22+
// -----------------------------------------
23+
// Reserved words
24+
// -----------------------------------------
25+
model FullModel
26+
extends TestKeywords; // 'extends'
27+
inner MyConnector mc; // 'inner'
28+
outer MyConnector mcOuter; // 'outer'
29+
constant Real c = 3.14; // 'constant', 'Real'
30+
public Real pubVar; // 'public'
31+
protected Real protVar; // 'protected'
32+
discrete Integer dInt; // 'discrete', 'Integer'
33+
Boolean flag = true; // 'Boolean', 'true'
34+
Boolean off = false; // 'false'
35+
parameter Integer p = 2; // 'parameter', 'Integer'
36+
MyRecord rec; // 'record'
37+
input Real inputVar; // 'input'
38+
output Real outputVar; // 'output'
39+
Real arr[2]; // arrays (no keyword)
40+
equation
41+
der(inputVar) = -p * inputVar + c; // 'der'
42+
when time > 1.0 then // 'when', 'then'
43+
outputVar = inputVar * 2.0;
44+
end when; // 'end' (when)
45+
46+
// Ejemplos de funciones matemáticas comunes
47+
Real minVal = Modelica.Math.min(inputVar, outputVar);
48+
Real maxVal = Modelica.Math.max(inputVar, outputVar);
49+
Real absVal = abs(inputVar);
50+
Real sinVal = sin(inputVar);
51+
Real cosVal = cos(inputVar);
52+
Real tanVal = tan(inputVar);
53+
Real expVal = exp(inputVar);
54+
Real logVal = log(inputVar);
55+
Real sqrtVal = sqrt(inputVar);
56+
end FullModel;
57+
58+
// -----------------------------------------
59+
// Functions, operators, and control flow
60+
// -----------------------------------------
61+
function myFunc
62+
input Real x; // 'input'
63+
output Real y; // 'output'
64+
algorithm
65+
if x > 0 then // 'if', 'then'
66+
y := x;
67+
elseif x < 0 then // 'elseif', 'then'
68+
y := -x;
69+
else // 'else'
70+
y := 0;
71+
end if; // 'end' (if)
72+
end myFunc; // 'end', 'function'
73+
74+
operator function addOp "operator example"
75+
input Real a;
76+
input Real b;
77+
output Real r;
78+
algorithm
79+
r := a + b;
80+
return; // 'return'
81+
end addOp; // 'operator', 'function', 'return'
82+
83+
impure function impureFunc
84+
input Real x;
85+
output Real y;
86+
algorithm
87+
y := x + rand(); // 'impure'
88+
end impureFunc; // 'impure', 'function'
89+
90+
// -----------------------------------------
91+
// Algorithm with loop / for / while / break
92+
// -----------------------------------------
93+
model AlgoModel
94+
Real sum;
95+
algorithm
96+
sum := 0;
97+
for i in 1:10 loop // 'for', 'in', 'loop'
98+
if i == 5 then
99+
break; // 'break'
100+
end if; // 'end' (if)
101+
sum := sum + i;
102+
end for; // 'end' (for)
103+
while sum < 10 loop // 'while', 'loop'
104+
sum := sum + 1;
105+
end while; // 'end' (while)
106+
end AlgoModel;
107+
108+
// -----------------------------------------
109+
// Package, encapsulated, import, partial, final
110+
// -----------------------------------------
111+
package InnerPackage encapsulated
112+
import Modelica.Math; // 'import'
113+
partial model AbstractModel // 'partial'
114+
end AbstractModel;
115+
model Concrete final // 'final'
116+
end Concrete;
117+
end InnerPackage;
118+
119+
// -----------------------------------------
120+
// Use of connect, constrainedby, expandable, protected, public
121+
// -----------------------------------------
122+
model ConnExample
123+
MyConnector c1, c2;
124+
MyRecord r1;
125+
equation
126+
connect(c1, c2); // 'connect'
127+
end ConnExample;
128+
129+
// -----------------------------------------
130+
// Redeclare / replaceable / constrainedby
131+
// -----------------------------------------
132+
model RedeclareExample
133+
replaceable model M = FullModel; // 'replaceable'
134+
redeclare model R = FullModel; // 'redeclare'
135+
constrainedby MyRecord cb; // 'constrainedby'
136+
end RedeclareExample;
137+
138+
// -----------------------------------------
139+
// External, annotation, within usage
140+
// -----------------------------------------
141+
function externalExample
142+
input Real x;
143+
output Real y;
144+
external "C" y = "c_func"(x); // 'external'
145+
end externalExample;
146+
147+
// -----------------------------------------
148+
// Operator/flow/pure/each/annotation/initial/expandable/type/true/false
149+
// -----------------------------------------
150+
model Misc
151+
pure function pureFunc
152+
input Real a;
153+
output Real b;
154+
algorithm
155+
b := a * 2;
156+
end pureFunc; // 'pure'
157+
each Integer eachVar[2]; // 'each', 'Integer'
158+
expandable model ExpModel // 'expandable'
159+
end ExpModel;
160+
initial equation // 'initial'
161+
; // empty initial equation
162+
end Misc;
163+
164+
// -----------------------------------------
165+
// When / elsewhen / then / return example
166+
// -----------------------------------------
167+
model WhenExample
168+
Real x;
169+
equation
170+
when x > 1 then // 'when', 'then'
171+
x = 2;
172+
elsewhen x < -1 then // 'elsewhen'
173+
x = -2;
174+
end when;
175+
end WhenExample;
176+
177+
// -----------------------------------------
178+
// Classes and additional language keywords
179+
// -----------------------------------------
180+
class MyClass
181+
end MyClass; // 'class'
182+
183+
encapsulated package MoreKeywords encapsulated
184+
final model FinalModel end FinalModel; // 'final'
185+
partial model PartialModel end PartialModel;// 'partial'
186+
expandable model ExpandableModel end ExpandableModel; // 'expandable'
187+
import Modelica.Constants; // 'import'
188+
end MoreKeywords;
189+
190+
model VarTypes
191+
constant Real cReal = 3.14; // 'constant', 'Real'
192+
parameter Integer pInt = 10; // 'parameter', 'Integer'
193+
discrete Boolean dBool = true; // 'discrete', 'Boolean'
194+
enumeration Choices = {first, second}; // 'enumeration'
195+
end VarTypes;
196+
197+
// -----------------------------------------
198+
// Example of annotation with graphical icons
199+
// -----------------------------------------
200+
model IconExample
201+
Real x;
202+
annotation (
203+
Icon =
204+
graphics = {
205+
// Línea simple
206+
line(
207+
origin={0,0},
208+
points={{-100,0},{100,0}},
209+
color={255,0,0},
210+
thickness=2
211+
),
212+
213+
// Polígono cerrado
214+
polygon(
215+
origin={0,0},
216+
points={{-50,50},{50,50},{0,-50}},
217+
fillColor={0,255,0},
218+
pattern=LinePattern.Solid
219+
),
220+
221+
// Elipse
222+
ellipse(
223+
origin={0,0},
224+
extent={{-20,-10},{20,10}},
225+
fillColor={0,0,255},
226+
lineColor={0,0,0}
227+
),
228+
229+
// Texto
230+
text(
231+
origin={0,60},
232+
string="Ejemplo de icono",
233+
fontSize=12,
234+
color={0,0,0}
235+
),
236+
237+
// dynamicSelect con opciones
238+
dynamicSelect(
239+
origin={0,-60},
240+
extent={{-30,-20},{30,20}},
241+
choices={"Opción1", "Opción2", "Opción3"},
242+
selected=1
243+
)
244+
}
245+
);
246+
end IconExample;
247+
248+
end TestKeywords;

0 commit comments

Comments
 (0)