Skip to content

Commit

Permalink
- fixed the (x => y) option problem in the translator
Browse files Browse the repository at this point in the history
- fixed better escaping of rml comments to MetaModelica strings.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2299 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Apr 3, 2006
1 parent 1db0c7e commit 65e70b1
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 39 deletions.
6 changes: 3 additions & 3 deletions tools/rml2mod/Makefile
Expand Up @@ -4,9 +4,9 @@ RMLHOME=/c/OMDEV/tools/rml/
#RMLHOME=/c/bin/cygwin/home/adrpo/rml-2.3.0/x86-mingw32-gcc
#RMLHOME=/c/bin/cygwin/home/adrpo/rml-2.3.0/x86-cygwin-gcc
#RMLHOME=/c/dev/rml/rml-mmc-2.3.3-cygwin-mingw/x86-cygwin-gcc
CC = gcc -O2 #-g
LD = gcc -O2 #-g
RML=${RMLHOME}/bin/rmlc #-Wc,-g
CC = gcc #-g
LD = gcc #-g
RML=${RMLHOME}/bin/rmlc #-Wr,-ftrace #-Wc,-g
CCFLAGS=-I${RMLHOME}/include/plain/
LDFLAGS=-L${RMLHOME}/lib/plain/ -lrml

Expand Down
1 change: 1 addition & 0 deletions tools/rml2mod/defs.h
Expand Up @@ -4,6 +4,7 @@
#define LEXER_TOKEN_POSITION

#define LEXER_COMMENT_MAXLENGTH 2000
#define LEXER_STRING_MAXLENGTH 2000
#define LEXER_IDENT_MAXLENGTH 100
#define MAX_COMMENTINFO 6500

Expand Down
153 changes: 153 additions & 0 deletions tools/rml2mod/external.c
Expand Up @@ -543,3 +543,156 @@ RML_BEGIN_LABEL(External__string_5freal)
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(External__stringEscape)
{
char *str = RML_STRINGDATA(rmlA0);
rml_uint_t len = strlen(str);
char *strnew = (char*)malloc(2*len); /* should be enough */
int i=0, j=0, c=0, n=0;
rml_uint_t lennew = 0;
unsigned char *snew = 0;
struct rml_string *rml_strnew = 0;
while (1)
{
if (i >= len) break;
c = str[i];
if (c == '\\')
{
strnew[lennew] = c; lennew++; i++;
if (i >= len) break;
c = str[i];
switch (c)
{
case 'f':
strnew[lennew] = '\f';
break;
case 'b':
strnew[lennew] = '\b';
break;
case 'e':
strnew[lennew] = 033;
break;
case 'v':
strnew[lennew] = '\v';
break;
case 'a':
strnew[lennew] = '\a';
break;
case '?':
strnew[lennew] = '\?';
break;
case 'n':
strnew[lennew] = '\n';
break;
case 't':
strnew[lennew] = '\t';
break;
case 'r':
strnew[lennew] = '\r';
break;
case '"':
strnew[lennew] = '\"';
break;
default:
strnew[lennew] = '\\'; lennew++;
strnew[lennew] = c;
}
lennew++;
}
else if(c == '"' || c == '\'')
{
strnew[lennew++] = '\\';
strnew[lennew++] = c;
}
else /* leave the char as it is */
{
strnew[lennew++] = c;
}
i++;
}
strnew[lennew]='\0';
/* now alloc the new string */
rml_strnew = rml_prim_mkstring(lennew, 1);
snew = (unsigned char*)rml_strnew->data;
for(i = 0; i < lennew; i++)
{
*snew++ = strnew[i];
}
*snew = '\0';
free(strnew);
rmlA0 = RML_TAGPTR(rml_strnew);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(External__commentEscape)
{
char *str = RML_STRINGDATA(rmlA0);
rml_uint_t len = strlen(str);
char *strnew = (char*)malloc(2*len); /* should be enough */
int i=0, j=0, c=0, n=0;
rml_uint_t lennew = 0;
unsigned char *snew = 0;
struct rml_string *rml_strnew = 0;
while (1)
{
if (i >= len) break;
c = str[i];
if (c == '\\')
{
strnew[lennew] = c; lennew++; i++;
if (i >= len) break;
c = str[i];
switch (c)
{
/*
case 'n':
strnew[lennew] = '\n';
break;
case 't':
strnew[lennew] = '\t';
break;
case 'r':
strnew[lennew] = '\r';
break;
*/
case '\\':
strnew[lennew] = '\\';
break;
case '"':
/* already escaped */
strnew[lennew] = '"';
break;
default: /* don't know the escape sequence, escape the \ */
strnew[lennew] = '\\';
lennew++;
strnew[lennew] = c;
}
lennew++;
}
else if(c == '"' || c == '\'')
{
strnew[lennew++] = '\\';
strnew[lennew++] = c;
}
else
{
strnew[lennew++] = c;
}
i++;
}
strnew[lennew]='\0';
/* now alloc the new string */
rml_strnew = rml_prim_mkstring(lennew, 1);
snew = (unsigned char*)rml_strnew->data;
for(i = 0; i < lennew; i++)
{
*snew++ = strnew[i];
}
*snew = '\0';
free(strnew);
rmlA0 = RML_TAGPTR(rml_strnew);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
3 changes: 3 additions & 0 deletions tools/rml2mod/external.rml
Expand Up @@ -26,6 +26,9 @@ module External:
relation getFirstIdent: string => string
relation string_real: string => real

relation stringEscape: string => string
relation commentEscape: string => string

end

(* testing of external *)
Expand Down
6 changes: 3 additions & 3 deletions tools/rml2mod/lexer.c
Expand Up @@ -215,8 +215,8 @@ static int yylex0()

struct Token *val;

static char ident[100];
static char string[4100];
static char ident[LEXER_IDENT_MAXLENGTH];
static char string[LEXER_STRING_MAXLENGTH];


if(creg >= MAX_COMMENTINFO-1)
Expand Down Expand Up @@ -438,7 +438,7 @@ static int yylex0()
break;
if (c == '\\')
c = esc_char(0);
if (i < 4096)
if (i < LEXER_STRING_MAXLENGTH-1)
string[i++] = c;
else
yyerror("string buffer overflow");
Expand Down
4 changes: 1 addition & 3 deletions tools/rml2mod/main.rml
Expand Up @@ -105,10 +105,8 @@ end
relation transformFiles: (string list, string list) => Absyn.Transformed list =

rule transformFile(file, flags) => transformed &
(*
print "Writing: " &
(*
dumpFiles([transformed],"-nap-none") &
print "\n" &
*)
transformFiles(files, flags) => transformedFiles &
list_append([transformed], transformedFiles) => result
Expand Down
94 changes: 64 additions & 30 deletions tools/rml2mod/rmltomod.rml
@@ -1,5 +1,5 @@
(*
Copyright 2005, Adrian Pop, adrpo@ida.liu.se and PELAB, Link�ping University
Copyright 2005-2006, Adrian Pop, adrpo@ida.liu.se and PELAB, Link�ping University
*)

module RMLToMod:
Expand Down Expand Up @@ -413,6 +413,17 @@ relation get_specialtype_id =
get_specialtype_id(Absyn.RMLTYPE_SIGNATURE(Absyn.CALLSIGN(intype,outtype)),id,alttypes_db,b_ei) =>
(cid,b)

rule get_rml_id(nid,true) => mid &
transform_typeid(mid,false) => cid &
string_append(cid,id) => tid &
get_specialtype_id(last,cid,alttypes_db,b_ei) => (fid,_) &
string_append(fid,tid) => fid' &
get_alternative_typeid(fid',alttypes_db,b_ei) => (aid,b)
-------------------------------------------
get_specialtype_id(
Absyn.RMLTYPE_TYCONS(
(last as Absyn.RMLTYPE_SIGNATURE(_))::[],nid),id,alttypes_db,b_ei) => (aid,b)

rule get_rml_id(nid,true) => mid &
transform_typeid(mid,false) => cid &
string_append(cid,id) => tid &
Expand Down Expand Up @@ -501,10 +512,10 @@ relation get_valtype_id =

end

(**
Relations to get the types that builds special constructions like
lists,vectors,records,option..*)

(*
* Relations to get the types that builds special constructions like
* lists, vectors, records, option..
*)
relation is_unique =

rule list_member(id, list) => b &
Expand Down Expand Up @@ -552,17 +563,19 @@ end
relation get_specialtypes =

rule transform_decl_signature(sign,alttypes_db,false) => (in_out_spec,in_out_decl,il,ol,alttypes_db') &
get_specialtype_id(sign,"",alttypes_db,b_ei) => (type_id,false) &
get_specialtype_id(sign,"",alttypes_db,b_ei) => (utype_id,false) &
(*make a shorter but unique funcname*)
(*
int_add(fc,1) => fc' &
int_string(fc') => sfc &
string_append("FuncType",sfc) => utype_id &
*)
create_class_parts(utype_id,Absyn.R_FUNCTION,true,[Absyn.PUBLIC(in_out_decl),
Absyn.PUBLIC(in_out_spec)],[],true) => class &
create_standard_elementitem(Absyn.CLASSDEF(false,class)) => elementitem
-----------------------------------------------
get_specialtypes(sign,alttypes_db,b_ei,com,fc) =>
([elementitem],ATYPES(type_id,utype_id,true)::alttypes_db,fc')
([elementitem],ATYPES(utype_id,utype_id,true)::alttypes_db,fc)

rule get_specialtype_id(tyvar,"",alttypes_db,b_ei) => (type_id,false) &
get_specialtype("replaceable",type_id,com) => derived &
Expand Down Expand Up @@ -935,7 +948,7 @@ relation get_alternative_typeid =
rule get_alternative_typeid(the_typeid,rest,b_ei) => (a_typeid,b)
-----------------------------
get_alternative_typeid(the_typeid,_::rest,b_ei) => (a_typeid,b)

end

relation create_local_decl =
Expand Down Expand Up @@ -971,7 +984,6 @@ relation create_specialtypes :
-----------------------------------------------------------------------------
create_specialtypes(_::rest,alttypes_db,b_ei) => (special_types,alttypes_db')


end


Expand Down Expand Up @@ -1327,13 +1339,19 @@ end

relation transform_handle_escape =

rule External.strrplall(s, "\\", "\\\\") => s &
External.strrplall(s,"\"","\\\"") => s &
External.strrplall(s, "\n", "\\n") => s &
External.strrplall(s, "\t", "\\t") => s &
External.strrplall(s, "\b", "\\b") => s
-----------------------------------------
transform_handle_escape(s,_,_) => s
(*
rule External.stringEscape(s) => s
-----------------------------------------
transform_handle_escape(s,_,_) => s
*)

rule External.strrplall(s, "\\", "\\\\") => s &
External.strrplall(s,"\"","\\\"") => s &
External.strrplall(s, "\n", "\\n") => s &
External.strrplall(s, "\t", "\\t") => s &
External.strrplall(s, "\b", "\\b") => s
-----------------------------------------
transform_handle_escape(s,_,_) => s

(*
rule int_ge(i,l) => true
Expand Down Expand Up @@ -2392,7 +2410,13 @@ end
relation transform_type =

rule get_rml_id2(id) => mid &
transform_typeid(mid,false) => cid &
transform_typeid(mid,false) => cid &
(*
print "\n" &
debug_print("variable list",var_lst) &
debug_print("type", cid) &
print "\n" &
*)
get_alternative_typeid(cid,alttypes_db,b_ext) => (spec_id,_) &
create_components(var_lst,spec_id,dir,com) => components &
create_standard_elementitem(components) => elementitem
Expand All @@ -2401,6 +2425,13 @@ relation transform_type =
elementitem

rule get_specialtype_id(the_type,"",alttypes_db,b_ext) => (spec_id,_) &
(*
print "\n" &
debug_print("variable list",var_lst) &
debug_print("the_type", the_type) &
debug_print("type", spec_id) &
print "\n" &
*)
create_components(var_lst,spec_id,dir,com) => components &
create_standard_elementitem(components) => elementitem
--------------------------------------------------
Expand Down Expand Up @@ -2968,19 +2999,22 @@ end
*)
relation transform_comment_handle_dq =

rule External.strrplall(s,"\"","\\\"") => s &
External.strrplall(s,"\\\\\"","\\\"") => s &
External.strrplall(s,"[","{") => s &
External.strrplall(s,"]","}") => s &
External.strrplall(s,"relation","function") => s &
External.strrplall(s,"/*","/-") => s &
External.strrplall(s,"*/","-/") => s &
External.strrplall(s,"*","") => s &
External.strrplall(s,"/-","/*") => s &
External.strrplall(s,"-/","*/") => s &
External.trimstring(s," ") => s
---------------------------------------
transform_comment_handle_dq(s,i) => s
rule (*
External.strrplall(s,"\"","\\\"") => s &
External.strrplall(s,"\\\\\"","\\\"") => s &
*)
External.commentEscape(s) => s &
External.strrplall(s,"[","{") => s &
External.strrplall(s,"]","}") => s &
External.strrplall(s,"relation","function") => s &
External.strrplall(s,"/*","/-") => s &
External.strrplall(s,"*/","-/") => s &
External.strrplall(s,"*","") => s &
External.strrplall(s,"/-","/*") => s &
External.strrplall(s,"-/","*/") => s &
External.trimstring(s," ") => s
---------------------------------------
transform_comment_handle_dq(s,i) => s
end

(*
Expand Down

0 comments on commit 65e70b1

Please sign in to comment.