Skip to content

Commit

Permalink
- Fix indentation of list() command for if-statements
Browse files Browse the repository at this point in the history
- Add comments to algorithmitem


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13798 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 5, 2012
1 parent a06d72b commit 1972b14
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -478,6 +478,10 @@ uniontype AlgorithmItem "Info specific for an algorithm item."
Annotation annotation_ "annotation" ;
end ALGORITHMITEMANN;

record ALGORITHMITEMCOMMENT "A comment from the lexer"
String comment;
end ALGORITHMITEMCOMMENT;

end AlgorithmItem;

public
Expand Down
56 changes: 31 additions & 25 deletions Compiler/FrontEnd/Dump.mo
Expand Up @@ -1290,7 +1290,7 @@ algorithm
s1 = unparseAlgorithmStrLst(i, als, "\n");
i_1 = i - 1;
is = indentStr(i_1);
str = stringAppendList({is,"algorithm\n",s1});
str = stringAppendList({is,"algorithm\n",s1,"\n"});
then
str;

Expand All @@ -1299,7 +1299,7 @@ algorithm
s1 = unparseAlgorithmStrLst(i, als, "\n");
i_1 = i - 1;
is = indentStr(i_1);
str = stringAppendList({is,"initial algorithm\n",s1});
str = stringAppendList({is,"initial algorithm\n",s1,"\n"});
then
str;

Expand Down Expand Up @@ -1494,7 +1494,7 @@ algorithm
str;
case (i,Absyn.LEXER_COMMENT(comment=str))
equation
str = System.trimChar(str,"\n");
str = System.trimWhitespace(str);
str = indentStr(i) +& str;
then str;
end match;
Expand Down Expand Up @@ -3161,13 +3161,15 @@ algorithm

case (_,{},_) then "";

case (i,{x},sep)
then unparseAlgorithmStr(i, x);

case (i,(x :: xs),sep)
equation
s1 = unparseAlgorithmStr(i, x);
s2 = unparseAlgorithmStrLst(i, xs, sep);
res = stringAppendList({s1,sep,s2});
then
res;
then res;
end match;
end unparseAlgorithmStrLst;

Expand Down Expand Up @@ -3230,13 +3232,13 @@ algorithm
equation
s1 = printExpStr(e);
i_1 = i + 1;
s2 = unparseAlgorithmStrLst(i, tb, "\n");
s3 = unparseAlgElseifStrLst(i_1, eb, "\n");
s4 = unparseAlgorithmStrLst(i, fb, "\n");
s2 = unparseAlgorithmStrLst(i_1, tb, "\n");
s3 = unparseAlgElseifStrLst(i, eb, "\n");
s4 = unparseAlgorithmStrLst(i_1, fb, "\n");
s5 = unparseCommentOption(optcmt);
is = indentStr(i);
str = stringAppendList(
{is,"if ",s1," then \n",is,s2,s3,"\n",is,"else ",s4,"\n",is,
{is,"if ",s1," then \n",s2,s3,"\n",is,"else\n",s4,"\n",is,
"end if",s5,";"});
then
str;
Expand Down Expand Up @@ -3296,13 +3298,6 @@ algorithm
then
str;

case (i,Absyn.ALGORITHMITEMANN(annotation_ = ann))
equation
str = unparseAnnotationOption(i, SOME(ann));
str_1 = stringAppend(str, ";");
then
str_1;

case (i,Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_RETURN(),comment = optcmt)) /* ALG_RETURN */
equation
s3 = unparseCommentOption(optcmt);
Expand Down Expand Up @@ -3361,11 +3356,21 @@ algorithm
{is,"catch\n",is,s2,is,"end catch",s3,";"});
then
str;
case (_,_)

case (i,Absyn.ALGORITHMITEMANN(annotation_ = ann))
equation
Print.printErrorBuf("#Error, unparse_algorithm_str failed\n");
str = unparseAnnotationOption(i, SOME(ann));
str_1 = stringAppend(str, ";");
then
"";
str_1;

case (i,Absyn.ALGORITHMITEMCOMMENT(comment = str))
then indentStr(i) +& System.trimWhitespace(str);

case (_,_)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"Dump.unparseAlgorithmStr failed"});
then fail();
end matchcontinue;
end unparseAlgorithmStr;

Expand Down Expand Up @@ -3531,13 +3536,15 @@ algorithm

case (_,{},_) then "";

case (i,{x},sep)
then unparseAlgElseifStr(i, x);

case (i,(x :: xs),sep)
equation
s2 = unparseAlgElseifStrLst(i, xs, sep);
s1 = unparseAlgElseifStr(i, x);
res = stringAppendList({s1,sep,s2});
then
res;
then res;
end match;
end unparseAlgElseifStrLst;

Expand All @@ -3557,10 +3564,9 @@ algorithm
case (i,(e,el))
equation
s1 = printExpStr(e);
s2 = unparseAlgorithmStrLst(i, el, "\n");
i_1 = i - 1;
is = indentStr(i_1);
str = stringAppendList({is,"elseif ",s1," then\n",s2});
s2 = unparseAlgorithmStrLst(i+1, el, "\n");
is = indentStr(i);
str = stringAppendList({"\n",is,"elseif ",s1," then\n",s2});
then
str;
end match;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -899,7 +899,7 @@ algorithm
res = translateClassdefAlgorithmitems(rest);
then
(stmt :: res);
case (Absyn.ALGORITHMITEMANN(annotation_ = _) :: rest)
case (_ :: rest)
equation
res = translateClassdefAlgorithmitems(rest);
then
Expand Down
15 changes: 15 additions & 0 deletions Compiler/OpenModelicaBootstrappingHeader.h
Expand Up @@ -1897,6 +1897,21 @@ extern struct record_description Absyn_AlgorithmItem_ALGORITHMITEMANN__desc;
#define Absyn__ALGORITHMITEMANN_3dBOX1 4
#define Absyn__ALGORITHMITEMANN(annotation_) (mmc_mk_box2(4,&Absyn_AlgorithmItem_ALGORITHMITEMANN__desc,annotation_))
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc_added
#define Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc_added
ADD_METARECORD_DEFINTIONS const char* Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc__fields[1] = {"comment"};
ADD_METARECORD_DEFINTIONS struct record_description Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc = {
"Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT",
"Absyn.AlgorithmItem.ALGORITHMITEMCOMMENT",
Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc__fields
};
#endif
#else /* Only use the file as a header */
extern struct record_description Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc;
#endif
#define Absyn__ALGORITHMITEMCOMMENT_3dBOX1 5
#define Absyn__ALGORITHMITEMCOMMENT(comment) (mmc_mk_box2(5,&Absyn_AlgorithmItem_ALGORITHMITEMCOMMENT__desc,comment))
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_Equation_EQ__IF__desc_added
#define Absyn_Equation_EQ__IF__desc_added
ADD_METARECORD_DEFINTIONS const char* Absyn_Equation_EQ__IF__desc__fields[4] = {"ifExp","equationTrueItems","elseIfBranches","equationElseItems"};
Expand Down
30 changes: 27 additions & 3 deletions Parser/Modelica.g
Expand Up @@ -677,10 +677,34 @@ initial_algorithm_clause returns [void* ast] :
INITIAL T_ALGORITHM as=algorithm_annotation_list {ast = Absyn__INITIALALGORITHMS(as);}
;

algorithm_annotation_list returns [void* ast] :
{ LA(1) == END_IDENT || LA(1) == EQUATION || LA(1) == T_ALGORITHM || LA(1)==INITIAL || LA(1) == PROTECTED || LA(1) == PUBLIC }? {ast = mk_nil();}
algorithm_annotation_list returns [void* ast] @init {
int first,last;
$ast = 0;
first = omc_first_comment;
last = LT(1)->getTokenIndex(LT(1));
omc_first_comment = last;
} :
{ LA(1) == END_IDENT || LA(1) == EQUATION || LA(1) == T_ALGORITHM || LA(1)==INITIAL || LA(1) == PROTECTED || LA(1) == PUBLIC }?
{
ast = mk_nil();
for (;first<last;last--) {
pANTLR3_COMMON_TOKEN tok = INPUT->get(INPUT,last-1);
if (tok->getChannel(tok) == HIDDEN && (tok->type == LINE_COMMENT || tok->type == ML_COMMENT)) {
ast = mk_cons(Absyn__ALGORITHMITEMCOMMENT(mk_scon((char*)tok->getText(tok)->chars)),ast);
}
}
}
|
( al=algorithm SEMICOLON {a = al.ast;} | a=annotation SEMICOLON {a = Absyn__ALGORITHMITEMANN(a);}) as=algorithm_annotation_list {ast = mk_cons(a,as);}
( al=algorithm SEMICOLON {a = al.ast;} | a=annotation SEMICOLON {a = Absyn__ALGORITHMITEMANN(a);}) as=algorithm_annotation_list
{
ast = mk_cons(a,as);
for (;first<last;last--) {
pANTLR3_COMMON_TOKEN tok = INPUT->get(INPUT,last-1);
if (tok->getChannel(tok) == HIDDEN && (tok->type == LINE_COMMENT || tok->type == ML_COMMENT)) {
ast = mk_cons(Absyn__ALGORITHMITEMCOMMENT(mk_scon((char*)tok->getText(tok)->chars)),ast);
}
}
}
;

equation returns [void* ast] :
Expand Down

0 comments on commit 1972b14

Please sign in to comment.