Skip to content

Commit

Permalink
- Added support to parse line/block-comments as class elements (simil…
Browse files Browse the repository at this point in the history
…ar to annotations)

  - Any comment in an expression is moved out of the element and put on its own line


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11213 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 27, 2012
1 parent 0cbef56 commit e045539
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 143 deletions.
5 changes: 4 additions & 1 deletion Compiler/FrontEnd/Absyn.mo
Expand Up @@ -208,7 +208,6 @@ uniontype ClassDef
list<Ident> vars "derived variables" ;
Option<Comment> comment "comment";
end PDER;

end ClassDef;

public
Expand Down Expand Up @@ -299,6 +298,10 @@ uniontype ElementItem "An element item is either an element or an annotation"
Annotation annotation_ ;
end ANNOTATIONITEM;

record LEXER_COMMENT
String comment;
end LEXER_COMMENT;

end ElementItem;

public
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ClassLoader.mo
Expand Up @@ -392,7 +392,7 @@ algorithm
// faliling
case _
equation
Debug.fprint(Flags.FAILTRACE, "ClassLoader.loadFile failed\n");
Debug.fprint(Flags.FAILTRACE, "ClassLoader.loadFile failed: "+&inString+&"\n");
then
fail();
end matchcontinue;
Expand Down
16 changes: 14 additions & 2 deletions Compiler/FrontEnd/Dependency.mo
Expand Up @@ -48,6 +48,7 @@ public import Interactive;

// protected imports
protected import BaseHashTable;
protected import Dump;
protected import HashTable2;
protected import ComponentReference;
protected import Connect;
Expand Down Expand Up @@ -1201,8 +1202,9 @@ algorithm
local
list<Absyn.ElementItem> elemis;
HashTable2.HashTable table1,table2;
String id;
String id,str;
Absyn.ElementSpec spec;
Absyn.ElementItem elit;
case({},inTable) then inTable;
case((Absyn.ELEMENTITEM(Absyn.ELEMENT(name = id,specification=spec)))::elemis,inTable)
equation
Expand All @@ -1219,7 +1221,17 @@ algorithm
table1 = createLocalVariableStruct2(elemis,inTable);
then
table1;
case(_,_) equation print("createLocalVariableStruct2 failed\n"); then fail();
case((Absyn.LEXER_COMMENT(_))::elemis,inTable)
equation
table1 = createLocalVariableStruct2(elemis,inTable);
then
table1;
case(elit::_,_)
equation
print("createLocalVariableStruct2 failed: ");
print(Dump.unparseElementitemStr(0,elit));
print("\n");
then fail();
end matchcontinue;
end createLocalVariableStruct2;

Expand Down
6 changes: 6 additions & 0 deletions Compiler/FrontEnd/Dump.mo
Expand Up @@ -56,6 +56,7 @@ protected import Error;
protected import Flags;
protected import List;
protected import Print;
protected import System;
protected import Util;

public function dumpExpStr
Expand Down Expand Up @@ -1484,6 +1485,11 @@ algorithm
str = stringAppend(s1, ";");
then
str;
case (i,Absyn.LEXER_COMMENT(comment=str))
equation
str = System.trimChar(str,"\n");
str = indentStr(i) +& str;
then str;
end match;
end unparseElementitemStr;

Expand Down
4 changes: 4 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -56,6 +56,7 @@ protected import Config;
protected import DAEUtil;
protected import Debug;
protected import Env;
protected import ErrorExt;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
Expand Down Expand Up @@ -361,7 +362,10 @@ algorithm
case (exp,n,_)
equation
// print("simplify1 start: " +& ExpressionDump.printExpStr(exp) +& "\n");
ErrorExt.setCheckpoint("ExpressionSimplify");
((exp,b)) = Expression.traverseExp(exp,simplifyWork,false);
Debug.bcall1(b,ErrorExt.rollBack,"ExpressionSimplify");
Debug.bcall1(not b,ErrorExt.delCheckpoint,"ExpressionSimplify");
// print("simplify1 iter: " +& ExpressionDump.printExpStr(exp) +& "\n");
(exp,_) = simplify1FixP(exp,n-1,b);
then (exp,b);
Expand Down
112 changes: 9 additions & 103 deletions Compiler/FrontEnd/MetaUtil.mo
Expand Up @@ -126,109 +126,6 @@ algorithm
end matchcontinue;
end getTypeFromProp;

/*
public function typeMatching
input DAE.Type t;
input list<DAE.Properties> propList;
output Boolean outBool;
algorithm
outBool :=
matchcontinue (t,propList)
local
Boolean b;
DAE.Type tLocal;
list<DAE.Properties> restList;
case (_,{}) then true;
case (tLocal as DAE.T_INTEGER(varLst = _),DAE.PROP(DAE.T_INTEGER(varLst = _),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as DAE.T_REAL(varLst = _),DAE.PROP(DAE.T_REAL(varLst = _),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as DAE.T_STRING(varLst = _),DAE.PROP(DAE.T_STRING(varLst = _),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as DAE.T_BOOL(varLst = _),DAE.PROP(DAE.T_BOOL(varLst = _),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_UNKNOWN(),_),DAE.PROP((DAE.T_UNKNOWN(),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_COMPLEX(ClassInf.RECORD(s1),_,_),_),
DAE.PROP((DAE.T_COMPLEX(ClassInf.RECORD(s2),_,_),_),_) :: restList)
local String s1,s2;
equation
true = (s1 ==& s2);
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_METALIST(t1),_),
DAE.PROP((DAE.T_METALIST(t2),_),_) :: restList)
local String s1,s2;
equation
true = (s1 ==& s2);
b = typeMatching(tLocal,restList);
then b;
case (_,_) then false;
end matchcontinue;
end typeMatching; */
/*
public function createMatchcontinueResultVars "function: createMatchcontinueResultVars"
input Env.Cache cache;
input Env.Env env;
input list<Absyn.Exp> refList;
input Integer num;
input list<Absyn.ElementItem> accDeclList;
input list<Absyn.Exp> accVarList;
output Env.Cache outCache;
output list<Absyn.ElementItem> outDecls;
output list<Absyn.Exp> outVarList;
algorithm
(outCache,outDecls,outVarList) :=
matchcontinue (cache,env,refList,num)
local
Env.Cache localCache;
Env.Env localEnv;
list<Absyn.ElementItem> localAccDeclList;
list<Absyn.Exp> localAccVarList;
case (localCache,_,{},_,localAccDeclList,localAccVarList)
then (localCache,localAccDeclList,localAccVarList);
case (localCache,localEnv,Absyn.CREF(Absyn.CREF_IDENT(c,{})) :: restExp,n,localAccDeclList,localAccVarList)
local
Absyn.Ident varName,c;
list<Absyn.ElementItem> varList;
list<Absyn.Exp> restExp;
Integer n;
DAE.Type t;
Absyn.TypeSpec t2;
equation
(localCache,DAE.TYPES_VAR(_,_,_,(t,_),_),_,_) = Lookup.lookupIdent(localCache,localEnv,c);
t2 = typeConvert(t);
varName = stringAppend("RES__",intString(n));
varList = List.create(Absyn.ELEMENTITEM(Absyn.ELEMENT(
false,NONE(),Absyn.NOT_INNER_OUTER(),"component",
Absyn.COMPONENTS(Absyn.ATTR(false,false,Absyn.VAR(),Absyn.BIDIR(),{}),
t2,
{Absyn.COMPONENTITEM(Absyn.COMPONENT(varName,{},NONE()),NONE(),NONE())}),
Absyn.INFO("f",false,0,0,0,0),NONE())));
localAccVarList = listAppend(localAccVarList,{Absyn.CREF(Absyn.CREF_IDENT(varName,{}))});
localAccDeclList = listAppend(localAccDeclList,varList);
(localCache,localAccDeclList,localAccVarList) = createMatchcontinueResultVars(
localCache,localEnv,restExp,n+1,localAccDeclList,localAccVarList);
then (localCache,localAccDeclList,localAccVarList);
case (_,_,_,_,_,_) then fail();
end matchcontinue;
end createMatchcontinueResultVars;
*/

public function getListOfStrings
input list<SCode.Element> els;
output list<String> outStrings;
Expand Down Expand Up @@ -410,6 +307,8 @@ algorithm
Boolean replaceable_;
case (Absyn.ELEMENTITEM(Absyn.ELEMENT(specification=Absyn.CLASSDEF(replaceable_=replaceable_),finalPrefix=finalPrefix,redeclareKeywords=redeclareKeywords,innerOuter=innerOuter,name=name,info=info,constrainClass=constrainClass)),class_)
then Absyn.ELEMENTITEM(Absyn.ELEMENT(finalPrefix,redeclareKeywords,innerOuter,name,Absyn.CLASSDEF(replaceable_,class_),info,constrainClass));
/* Annotations, comments */
else elementItem;
end match;
end setElementItemClass;

Expand Down Expand Up @@ -463,6 +362,10 @@ algorithm
clst = convertElementsToClasses(rest);
then
c::clst;

/* Strip annotation, comment */
case(_::rest)
then convertElementsToClasses(rest);
end match;
end convertElementsToClasses;

Expand Down Expand Up @@ -606,6 +509,9 @@ algorithm
equation
element = fixElement(element,name,index,singleton);
then Absyn.ELEMENTITEM(element);
// Don't fix comments, annotations
case(_,name,index,singleton)
then elementItemin;
end match;
end fixElementItem;

Expand Down
16 changes: 15 additions & 1 deletion Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -198,7 +198,7 @@ algorithm
// Print out an internal error msg only if no other errors have already
// been printed.
true = intEq(Error.getNumMessages(), inNumMessages);
n = "SCodeUtil.translateAbsyn2SCodeClass failed: " +& n;
n = "SCodeUtil.translateClass2 failed: " +& n;
Error.addSourceMessage(Error.INTERNAL_ERROR,{n},file_info);
then
fail();
Expand Down Expand Up @@ -517,6 +517,12 @@ algorithm
scodeCmt = translateComment(cmt);
then
SCode.PDER(path,vars,scodeCmt);

else
equation
Error.addMessage(Error.INTERNAL_ERROR,{"SCodeUtil.translateClassdef failed"});
then
fail();
end match;
end translateClassdef;

Expand Down Expand Up @@ -1019,6 +1025,14 @@ algorithm
l = listAppend(e_1, es_1);
then
l;

case ((Absyn.LEXER_COMMENT(comment = _) :: es),vis)
then translateEitemlist(es, vis);

case ((_ :: es),vis)
equation
Error.addMessage(Error.INTERNAL_ERROR,{"SCodeUtil.translateEitemlist failed"});
then translateEitemlist(es, vis);
end match;
end translateEitemlist;

Expand Down
15 changes: 15 additions & 0 deletions Compiler/OpenModelicaBootstrappingHeader.h
Expand Up @@ -1291,6 +1291,21 @@ extern struct record_description Absyn_ElementItem_ANNOTATIONITEM__desc;
#define Absyn__ANNOTATIONITEM_3dBOX1 4
#define Absyn__ANNOTATIONITEM(annotation_) (mmc_mk_box2(4,&Absyn_ElementItem_ANNOTATIONITEM__desc,annotation_))
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_ElementItem_LEXER__COMMENT__desc_added
#define Absyn_ElementItem_LEXER__COMMENT__desc_added
ADD_METARECORD_DEFINTIONS const char* Absyn_ElementItem_LEXER__COMMENT__desc__fields[1] = {"comment"};
ADD_METARECORD_DEFINTIONS struct record_description Absyn_ElementItem_LEXER__COMMENT__desc = {
"Absyn_ElementItem_LEXER__COMMENT",
"Absyn.ElementItem.LEXER_COMMENT",
Absyn_ElementItem_LEXER__COMMENT__desc__fields
};
#endif
#else /* Only use the file as a header */
extern struct record_description Absyn_ElementItem_LEXER__COMMENT__desc;
#endif
#define Absyn__LEXER_5fCOMMENT_3dBOX1 5
#define Absyn__LEXER_5fCOMMENT(comment) (mmc_mk_box2(5,&Absyn_ElementItem_LEXER__COMMENT__desc,comment))
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_Element_ELEMENT__desc_added
#define Absyn_Element_ELEMENT__desc_added
ADD_METARECORD_DEFINTIONS const char* Absyn_Element_ELEMENT__desc__fields[7] = {"finalPrefix","redeclareKeywords","innerOuter","name","specification","info","constrainClass"};
Expand Down
1 change: 1 addition & 0 deletions Compiler/Script/Interactive.mo
Expand Up @@ -5525,6 +5525,7 @@ algorithm
", replaceable=",repl,", inout=",inout_str,", ",element_str});
then
str;
case (Absyn.LEXER_COMMENT(comment=_)) then "elementtype=comment";
case (_) then "elementtype=annotation"; /* for annotations we don\'t care */
end matchcontinue;
end getElementInfo;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Error.mo
Expand Up @@ -580,7 +580,7 @@ public constant Message INVALID_STREAM_CONNECTOR = MESSAGE(216, TRANSLATION(), E
public constant Message CONDITION_TYPE_ERROR = MESSAGE(217, TRANSLATION(), ERROR(),
"Type mismatch in condition '%s' of component %s. Expected a Boolean expression, but got an expression of type %s");
public constant Message SIMPLIFY_CONSTANT_ERROR = MESSAGE(218, TRANSLATION(), NOTIFICATION(),
"The compiler tried to perform constant folding on expression %s. Please report this bug to the developers and we will fix it as soon as possible (using the +t compiler option if possible).");
"The compiler failed to perform constant folding on expression %s. Please report this bug to the developers and we will fix it as soon as possible (using the +t compiler option if possible).");

public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(),
"Parameter %s has neither value nor start value, and is fixed during initialization (fixed=true)");
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/System.mo
Expand Up @@ -47,7 +47,7 @@ public function removeFirstAndLastChar
end removeFirstAndLastChar;

public function trim
"removes chars in charsToRemove from inString"
"removes chars in charsToRemove from begin and end of inString"
input String inString;
input String charsToRemove;
output String outString;
Expand Down
7 changes: 7 additions & 0 deletions Compiler/runtime/System_omc.cpp
Expand Up @@ -164,6 +164,13 @@ extern const char* System_trim(const char* str, const char* chars_to_remove)
return SystemImpl__trim(str,chars_to_remove);
}

extern const char* System_trimChar(const char* str, const char* char_to_remove)
{
if (!char_to_remove[0] || char_to_remove[1])
MMC_THROW();
return MMC_STRINGDATA(SystemImpl__trimChar(str,*char_to_remove));
}

extern const char* System_basename(const char* str)
{
return strdup(SystemImpl__basename(str));
Expand Down
29 changes: 2 additions & 27 deletions Compiler/runtime/System_rml.c
Expand Up @@ -70,33 +70,8 @@ RML_BEGIN_LABEL(System__trimChar)
{
char* str = RML_STRINGDATA(rmlA0);
char char_to_be_trimmed = (char)RML_STRINGDATA(rmlA1)[0];
int length=strlen(str);
int start_pos = 0;
int end_pos = length - 1;
char* res;
if (length == 0) {
rmlA0 = (void*) mk_scon("");
RML_TAILCALLK(rmlSC);
}
while(start_pos < end_pos){
if(str[start_pos] == char_to_be_trimmed)
start_pos++;
if(str[end_pos] == char_to_be_trimmed)
end_pos--;
if(str[start_pos] != char_to_be_trimmed && str[end_pos] != char_to_be_trimmed)
break;
}
if(end_pos >= start_pos){
res= (char*)malloc(end_pos - start_pos +2);
strncpy(res,&str[start_pos],end_pos - start_pos+1);
res[end_pos - start_pos+1] = '\0';
rmlA0 = (void*) mk_scon(res);
free(res);
RML_TAILCALLK(rmlSC);
}else{
rmlA0 = (void*) mk_scon("");
RML_TAILCALLK(rmlSC);
}
rmlA0 = SystemImpl__trimChar(str,char_to_be_trimmed);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

Expand Down

0 comments on commit e045539

Please sign in to comment.