Skip to content

Commit 6778075

Browse files
author
Peter Aronsson
committed
Added split definitions, e.g. allowing 'package A' as input.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1066 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 7cc3168 commit 6778075

File tree

11 files changed

+403
-50
lines changed

11 files changed

+403
-50
lines changed

modelica_parser/src/modelica_parser.g

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ options {
2121
tokens {
2222
ALGORITHM_STATEMENT;
2323
ARGUMENT_LIST;
24+
BEGIN_DEFINITION;
2425
CLASS_DEFINITION ;
2526
CLASS_MODIFICATION;
2627
CODE_EXPRESSION;
@@ -31,15 +32,18 @@ tokens {
3132
CODE_ALGORITHM;
3233
CODE_INITIALALGORITHM;
3334
COMMENT;
35+
COMPONENT_DEFINITION;
3436
DECLARATION ;
3537
DEFINITION ;
38+
END_DEFINITION;
3639
ENUMERATION_LITERAL;
3740
ELEMENT ;
3841
ELEMENT_MODIFICATION ;
3942
ELEMENT_REDECLARATION ;
4043
EQUATION_STATEMENT;
4144
INITIAL_EQUATION;
4245
INITIAL_ALGORITHM;
46+
IMPORT_DEFINITION;
4347
EXPRESSION_LIST;
4448
EXTERNAL_FUNCTION_CALL;
4549
FOR_INDICES ;
@@ -65,13 +69,46 @@ tokens {
6569

6670

6771
stored_definition :
72+
// To handle split models into several evaluations, "package A",
73+
// "model test end test", "end A;" as in MathModelica
74+
( (ENCAPSULATED)?
75+
(PARTIAL)?
76+
class_type
77+
IDENT EOF ) => ( (ENCAPSULATED)?
78+
(PARTIAL)?
79+
class_type
80+
IDENT EOF!)
81+
{
82+
#stored_definition = #([BEGIN_DEFINITION,"BEGIN_DEFINITION"],
83+
#stored_definition);
84+
}
85+
|
86+
END! IDENT SEMICOLON! EOF! {
87+
#stored_definition = #([END_DEFINITION,"END_DEFINITION"],
88+
#stored_definition);
89+
}
90+
|
91+
(component_clause) => component_clause SEMICOLON! EOF!
92+
{
93+
#stored_definition = #([COMPONENT_DEFINITION,"COMPONENT_DEFINITION"],
94+
#stored_definition);
95+
}
96+
|
97+
import_clause SEMICOLON! EOF!
98+
{
99+
#stored_definition = #([IMPORT_DEFINITION,"IMPORT_DEFINITION"],
100+
#stored_definition);
101+
}
102+
|
103+
// End handle split models.
68104
(within_clause SEMICOLON!)?
69105
((f:FINAL)? class_definition SEMICOLON!)*
70106
EOF!
71107
{
72108
#stored_definition = #([STORED_DEFINITION,"STORED_DEFINITION"],
73109
#stored_definition);
74110
}
111+
75112
;
76113

77114
within_clause :

modeq/absyn.rml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ module Absyn:
4040
(** - Programs *)
4141
datatype Program = PROGRAM of Class list (* List of classes *)
4242
* Within (* Within statement *)
43+
| BEGIN_DEFINITION of Path (* For split definitions*)
44+
* Restriction (* Class restriction *)
45+
* bool (* Partial *)
46+
* bool (* Encapsulated *)
47+
| END_DEFINITION of Ident (* For split definitions *)
48+
| COMP_DEFINITION of ElementSpec (* For split definitions*)
49+
* Path option (* insert into.
50+
Default, NONE *)
51+
| IMPORT_DEFINITION of ElementSpec(* For split definitions*)
52+
* Path option (* insert into.
53+
Default, NONE *)
4354

4455
(** A program is simply a list of class definitions declared at top
4556
** level in the source file, combined with a within statement that

modeq/absyn_builder/parse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ RML_BEGIN_LABEL(Parser__parsestring)
161161
parse_tree_dumper dumper(std::cout);
162162
//dumper.initializeASTFactory(factory);
163163
//dumper.setASTFactory(&factory);
164-
//dumper.dump(t);
164+
dumper.dump(t);
165165
}
166166

167167
modelica_tree_parser build;

modeq/absyn_builder/walker.g

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,37 @@ tokens {
100100
stored_definition returns [void *ast]
101101
{
102102
void *within = 0;
103+
void *restr = 0;
104+
void *imp=0;
105+
void *c=0;
103106
void *class_def = 0;
104107
l_stack el_stack;
105108
}
106109
:
110+
#(BEGIN_DEFINITION (e:ENCAPSULATED)? (p:PARTIAL)?
111+
restr = class_restriction i:IDENT)
112+
{
113+
ast = Absyn__BEGIN_5fDEFINITION(Absyn__IDENT(to_rml_str(i)),
114+
restr,
115+
RML_PRIM_MKBOOL(p != 0),
116+
RML_PRIM_MKBOOL(e != 0));
117+
}
118+
|
119+
#(END_DEFINITION i2:IDENT)
120+
{
121+
ast = Absyn__END_5fDEFINITION(to_rml_str(i2));
122+
}
123+
|
124+
#(COMPONENT_DEFINITION c=component_clause)
125+
{
126+
ast = Absyn__COMP_5fDEFINITION(c,mk_none());
127+
}
128+
|
129+
#(IMPORT_DEFINITION imp=import_clause)
130+
{
131+
ast = Absyn__IMPORT_5fDEFINITION(imp,mk_none());
132+
}
133+
|
107134
#(STORED_DEFINITION
108135
( within = within_clause )?
109136
((f:FINAL )?

modeq/ceval.rml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,12 @@ relation ceval_interactive_functions: (Env.Env, Exp.Exp, Interactive.Interactive
921921
ceval_interactive_functions (env, Exp.CALL(Absyn.IDENT("saveModel"),[Exp.SCONST(filename),Exp.CREF(cr,_)],_,_),st as Interactive.SYMBOLTABLE(p,sp,ic,iv,cf))
922922
=> (Values.BOOL(true),st)
923923

924+
rule Dump.unparse_str(p) => str &
925+
System.write_file(filename,str)
926+
--------------------------
927+
ceval_interactive_functions (env, Exp.CALL(Absyn.IDENT("saveAll"),[Exp.SCONST(filename)],_,_),st as Interactive.SYMBOLTABLE(p,sp,ic,iv,cf))
928+
=> (Values.BOOL(true),st)
929+
924930
rule (*Error writing to file *)
925931
Static.component_ref_to_path(cr) => classpath &
926932
Interactive.get_pathed_class_in_program(classpath,p) => class &

modeq/classloader.rml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ relation load_class: (Absyn.Path, string) => (Absyn.Program) =
5858
-------------------------------------
5959
load_class(Absyn.IDENT(classname),mp) => p
6060

61-
(** Qualified names: Load the complete package and then check that **)
61+
rule (** Qualified names: First check if it is defined in a file pack.mo *)
62+
System.strtok(mp,":") => mps &
63+
load_class_from_mps(pack,mps) => p
64+
-------------------------------------
65+
load_class(Absyn.QUALIFIED(pack,rest),mp) => p
66+
67+
(** Qualified names: Else, load the complete package and then check that **)
6268
(** the package contains the file **)
6369
rule System.strtok(mp,":") => mps &
6470
load_complete_package_from_mps(pack,mps,Absyn.TOP,Absyn.PROGRAM([],Absyn.TOP)) => p &

modeq/dump.rml

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ relation unparse_class_str: (int, Absyn.Class,string,string,string) => string =
141141
select_string(f, "final ","") => s2 &
142142
unparse_restriction_str (r) => s3 &
143143
int_add(i,1) => i' &
144-
unparse_class_part_str_lst(i',parts) => s4 &
144+
unparse_class_part_str_lst(i',parts,true) => s4 &
145145
unparse_string_comment_option(optcmt) => s5 &
146146
Util.string_append_list([is, s1, s2, re, io, s3, " ", n, s5, "\n", s4, is, "end ", n]) => str
147147
-----------------------------
@@ -455,67 +455,75 @@ relation print_external_decl : Absyn.ExternalDecl => () =
455455

456456
end
457457

458-
relation unparse_class_part_str_lst: (int, Absyn.ClassPart list) => string =
458+
relation unparse_class_part_str_lst: (int, Absyn.ClassPart list,bool)
459+
=> string =
459460

460-
axiom unparse_class_part_str_lst (_,[]) => ""
461+
axiom unparse_class_part_str_lst (_,[],false) => ""
461462

462-
rule unparse_class_part_str(i,x) => s1 &
463-
unparse_class_part_str_lst(i,xs) => s2 &
463+
rule unparse_class_part_str(i,x,skippublic) => s1 &
464+
unparse_class_part_str_lst(i,xs,false) => s2 &
464465
string_append(s1,s2) => res
465466
----------------------
466-
unparse_class_part_str_lst(i,x::xs) => res
467+
unparse_class_part_str_lst(i,x::xs,skippublic) => res
467468
end
468469

469-
relation unparse_class_part_str: (int, Absyn.ClassPart) => string =
470+
relation unparse_class_part_str: (int, Absyn.ClassPart,bool) => string =
470471

471-
axiom unparse_class_part_str(i, Absyn.PUBLIC([])) => ""
472-
axiom unparse_class_part_str(i, Absyn.PROTECTED([])) => ""
473-
axiom unparse_class_part_str(i, Absyn.EQUATIONS([])) => ""
474-
axiom unparse_class_part_str(i, Absyn.INITIALEQUATIONS([])) => ""
475-
axiom unparse_class_part_str(i, Absyn.ALGORITHMS([])) => ""
476-
axiom unparse_class_part_str(i, Absyn.INITIALALGORITHMS([])) => ""
472+
axiom unparse_class_part_str(i, Absyn.PUBLIC([]),_) => ""
473+
axiom unparse_class_part_str(i, Absyn.PROTECTED([]),_) => ""
474+
axiom unparse_class_part_str(i, Absyn.EQUATIONS([]),_) => ""
475+
axiom unparse_class_part_str(i, Absyn.INITIALEQUATIONS([]),_) => ""
476+
axiom unparse_class_part_str(i, Absyn.ALGORITHMS([]),_) => ""
477+
axiom unparse_class_part_str(i, Absyn.INITIALALGORITHMS([]),_) => ""
477478

478479
rule unparse_elementitem_str_lst(i,el) => s1 &
479480
int_sub(i,1) => i' &
480481
indent_str(i') => is &
481-
Util.string_append_list([is,"public \n",s1]) => str
482+
Util.string_append_list([is,s1]) => str
482483
-----------------------------------------------
483-
unparse_class_part_str(i, Absyn.PUBLIC(el)) => str
484+
unparse_class_part_str(i, Absyn.PUBLIC(el),true) => str
484485

486+
rule unparse_elementitem_str_lst(i,el) => s1 &
487+
int_sub(i,1) => i' &
488+
indent_str(i') => is &
489+
Util.string_append_list([is,"public \n",s1]) => str
490+
-----------------------------------------------
491+
unparse_class_part_str(i, Absyn.PUBLIC(el),false) => str
492+
485493
rule unparse_elementitem_str_lst(i,el) => s1 &
486494
int_sub(i,1) => i' &
487495
indent_str(i') => is &
488496
Util.string_append_list([is,"protected \n",s1]) => str
489497
---------------------------------------
490-
unparse_class_part_str(i, Absyn.PROTECTED(el)) => str
498+
unparse_class_part_str(i, Absyn.PROTECTED(el),_) => str
491499

492500
rule unparse_equationitem_str_lst(i,eqs,";\n") => s1 &
493501
int_sub(i,1) => i' &
494502
indent_str(i') => is &
495503
Util.string_append_list(["\n",is,"equation \n",s1]) => str
496504
--------------------------------------
497-
unparse_class_part_str(i, Absyn.EQUATIONS(eqs)) => str
505+
unparse_class_part_str(i, Absyn.EQUATIONS(eqs),_) => str
498506

499507
rule unparse_equationitem_str_lst(i,eqs,";\n") => s1 &
500508
int_sub(i,1) => i' &
501509
indent_str(i') => is &
502510
Util.string_append_list(["\n",is,"initial equation \n",s1]) => str
503511
--------------------------------------
504-
unparse_class_part_str(i, Absyn.INITIALEQUATIONS(eqs)) => str
512+
unparse_class_part_str(i, Absyn.INITIALEQUATIONS(eqs),_) => str
505513

506514
rule unparse_algorithm_str_lst(i,eqs,"\n") => s1 &
507515
int_sub(i,1) => i' &
508516
indent_str(i') => is &
509517
Util.string_append_list([is,"algorithm \n",s1]) => str
510518
--------------------------------------
511-
unparse_class_part_str(i, Absyn.ALGORITHMS(eqs)) => str
519+
unparse_class_part_str(i, Absyn.ALGORITHMS(eqs),_) => str
512520

513521
rule unparse_algorithm_str_lst(i,eqs,"\n") => s1 &
514522
int_sub(i,1) => i' &
515523
indent_str(i') => is &
516524
Util.string_append_list([is,"initial algorithm \n",s1]) => str
517525
-----------------------------------------------
518-
unparse_class_part_str(i, Absyn.INITIALALGORITHMS(eqs)) => str
526+
unparse_class_part_str(i, Absyn.INITIALALGORITHMS(eqs),_) => str
519527

520528
rule get_extlang_str(lang) => langstr &
521529
print_component_ref_str output => outputstr &
@@ -524,7 +532,7 @@ axiom unparse_class_part_str(i, Absyn.INITIALALGORITHMS([])) => ""
524532
indent_str(i) => is &
525533
Util.string_append_list(["\n", is, "external ",langstr," ", outputstr, "=",ident,"(",expstr,");\n"]) => str
526534
--------------------------------------
527-
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(SOME(ident),lang,SOME(output),expl)))
535+
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(SOME(ident),lang,SOME(output),expl)),_)
528536
=> str
529537

530538
rule get_extlang_str(lang) => langstr &
@@ -533,13 +541,13 @@ axiom unparse_class_part_str(i, Absyn.INITIALALGORITHMS([])) => ""
533541
indent_str(i) => is &
534542
Util.string_append_list(["\n", is, "external ", langstr," ",ident,"(",expstr,");\n"]) => str
535543
--------------------------------------
536-
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(SOME(ident),lang,NONE,expl)))
544+
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(SOME(ident),lang,NONE,expl)),_)
537545
=> str
538546

539547
rule indent_str(i) => is &
540548
Util.string_append_list(["\n",is,"external;\n"]) => res
541549
-----------------
542-
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(NONE,_,NONE,_)))
550+
unparse_class_part_str(i, Absyn.EXTERNAL(Absyn.EXTERNALDECL(NONE,_,NONE,_)),_)
543551
=> res
544552
end
545553

0 commit comments

Comments
 (0)