浏览代码

+ bugfixes with C++ comments, they are now placed above the definition
* some bugfixes with the _label reserved word.

carl 21 年之前
父节点
当前提交
31a3763f12
共有 5 个文件被更改,包括 194 次插入49 次删除
  1. 44 19
      utils/h2pas/h2pas.pas
  2. 46 18
      utils/h2pas/h2pas.y
  3. 6 2
      utils/h2pas/options.pas
  4. 49 5
      utils/h2pas/scan.l
  5. 49 5
      utils/h2pas/scan.pas

+ 44 - 19
utils/h2pas/h2pas.pas

@@ -133,10 +133,33 @@ program h2pas;
       end;
 
 
+    { This converts pascal reserved words to
+      the correct syntax.
+    }
     function FixId(const s:string):string;
+    const
+     maxtokens = 14;
+     reservedid: array[1..maxtokens] of string[14] = 
+       (
+         'CLASS',
+         'DISPOSE',
+         'FUNCTION',
+         'FALSE',
+         'LABEL',
+         'NEW',
+         'PROPERTY',
+         'PROCEDURE',
+         'RECORD',
+         'REPEAT',
+         'STRING',
+         'TYPE',
+         'TRUE',
+         'UNTIL'
+       );  
       var
         b : boolean;
         up : string;
+        i: integer;
       begin
         if s='' then
          begin
@@ -145,17 +168,14 @@ program h2pas;
          end;
         b:=false;
         up:=Uppercase(s);
-        case up[1] of
-          'C' : b:=(up='CLASS');
-          'D' : b:=(up='DISPOSE');
-          'F' : b:=(up='FUNCTION') or (up='FALSE');
-          'N' : b:=(up='NEW');
-          'P' : b:=(up='PROPERTY') or (up='PROCEDURE');
-          'R' : b:=(up='RECORD') or (up='REPEAT');
-          'S' : b:=(up='STRING');
-          'T' : b:=(up='TYPE') or (up='TRUE');
-          'U' : b:=(up='UNTIL');
-        end;
+        for i:=1 to maxtokens do
+          begin
+            if up=reservedid[i] then
+               begin
+                  b:=true;
+                  break;
+                end;  
+          end;
         if b then
          FixId:='_'+s
         else
@@ -207,6 +227,7 @@ program h2pas;
          reset(tempfile);
          is_sized:=false;
          flag_index:=0;
+         writeln(outfile);
          writeln(outfile,aktspace,'const');
          shift(3);
          while not eof(tempfile) do
@@ -217,6 +238,7 @@ program h2pas;
                 line:=copy(line,1,ps-1)+ph+'_'+copy(line,ps+1,255);
               writeln(outfile,aktspace,line);
            end;
+         writeln(outfile);  
          close(tempfile);
          rewrite(tempfile);
          popshift;
@@ -1639,7 +1661,7 @@ begin
          end;
          if not assigned(yyv[yysp-1]^.p3) then
          begin
-         writeln(outfile,' : longint;');
+         writeln(outfile,' : longint;',aktspace,commentstr);
          writeln(extfile,' : longint;');
          flush(outfile);
          end
@@ -1647,7 +1669,7 @@ begin
          begin
          write(outfile,' : ');
          write_type_specifier(outfile,yyv[yysp-1]^.p3);
-         writeln(outfile,';');
+         writeln(outfile,';',aktspace,commentstr);
          flush(outfile);
          write(extfile,' : ');
          write_type_specifier(extfile,yyv[yysp-1]^.p3);
@@ -1665,7 +1687,7 @@ begin
   29 : begin
          
          (* DEFINE dname SPACE_DEFINE NEW_LINE *)
-         writeln(outfile,'{$define ',yyv[yysp-2]^.p,'}');
+         writeln(outfile,'{$define ',yyv[yysp-2]^.p,'}',aktspace,commentstr);
          flush(outfile);
          if assigned(yyv[yysp-2])then
          dispose(yyv[yysp-2],done);
@@ -1673,7 +1695,7 @@ begin
        end;
   30 : begin
          
-         writeln(outfile,'{$define ',yyv[yysp-1]^.p,'}');
+         writeln(outfile,'{$define ',yyv[yysp-1]^.p,'}',aktspace,commentstr);
          flush(outfile);
          if assigned(yyv[yysp-1])then
          dispose(yyv[yysp-1],done);
@@ -1697,7 +1719,7 @@ begin
          write(outfile,' = ');
          flush(outfile);
          write_expr(outfile,yyv[yysp-1]^.p1);
-         writeln(outfile,';');
+         writeln(outfile,';',aktspace,commentstr);
          popshift;
          if assigned(yyv[yysp-3]) then
          dispose(yyv[yysp-3],done);
@@ -1727,7 +1749,7 @@ begin
          begin
          write(outfile,' : ');
          write_type_specifier(outfile,yyv[yysp-1]^.p3);
-         writeln(outfile,';');
+         writeln(outfile,';',aktspace,commentstr);
          flush(outfile);
          write(extfile,' : ');
          write_type_specifier(extfile,yyv[yysp-1]^.p3);
@@ -7843,7 +7865,9 @@ begin
      Writeln(outfile,aktspace,'  PDouble   = ^Double;');
      Writeln(outfile);
    end;
+  writeln(outfile,'{$IFDEF FPC}');
   writeln(outfile,'{$PACKRECORDS C}');
+  writeln(outfile,'{$ENDIF}');
   writeln(outfile);
 { Open tempfiles }
   Assign(extfile,'ext.tmp');
@@ -7880,8 +7904,9 @@ end.
 
 {
   $Log$
-  Revision 1.10  2004-06-23 14:45:37  peter
-    * regenerated
+  Revision 1.11  2004-08-13 02:35:29  carl
+    + bugfixes with C++ comments, they are now placed above the definition
+    * some bugfixes with the _label reserved word.
 
   Revision 1.7  2004/06/20 17:56:05  marco
    Patch from Christian Iversen. ioresult check when opening for output

+ 46 - 18
utils/h2pas/h2pas.y

@@ -129,10 +129,33 @@ program h2pas;
       end;
 
 
+    { This converts pascal reserved words to
+      the correct syntax.
+    }
     function FixId(const s:string):string;
+    const
+     maxtokens = 14;
+     reservedid: array[1..maxtokens] of string[14] = 
+       (
+         'CLASS',
+         'DISPOSE',
+         'FUNCTION',
+         'FALSE',
+         'LABEL',
+         'NEW',
+         'PROPERTY',
+         'PROCEDURE',
+         'RECORD',
+         'REPEAT',
+         'STRING',
+         'TYPE',
+         'TRUE',
+         'UNTIL'
+       );  
       var
         b : boolean;
         up : string;
+        i: integer;
       begin
         if s='' then
          begin
@@ -141,17 +164,14 @@ program h2pas;
          end;
         b:=false;
         up:=Uppercase(s);
-        case up[1] of
-          'C' : b:=(up='CLASS');
-          'D' : b:=(up='DISPOSE');
-          'F' : b:=(up='FUNCTION') or (up='FALSE');
-          'N' : b:=(up='NEW');
-          'P' : b:=(up='PROPERTY') or (up='PROCEDURE');
-          'R' : b:=(up='RECORD') or (up='REPEAT');
-          'S' : b:=(up='STRING');
-          'T' : b:=(up='TYPE') or (up='TRUE');
-          'U' : b:=(up='UNTIL');
-        end;
+        for i:=1 to maxtokens do
+          begin
+            if up=reservedid[i] then
+               begin
+                  b:=true;
+                  break;
+                end;  
+          end;
         if b then
          FixId:='_'+s
         else
@@ -203,6 +223,7 @@ program h2pas;
          reset(tempfile);
          is_sized:=false;
          flag_index:=0;
+         writeln(outfile);
          writeln(outfile,aktspace,'const');
          shift(3);
          while not eof(tempfile) do
@@ -213,6 +234,7 @@ program h2pas;
                 line:=copy(line,1,ps-1)+ph+'_'+copy(line,ps+1,255);
               writeln(outfile,aktspace,line);
            end;
+         writeln(outfile);  
          close(tempfile);
          rewrite(tempfile);
          popshift;
@@ -1566,7 +1588,7 @@ define_dec :
          end;
        if not assigned($6^.p3) then
          begin
-            writeln(outfile,' : longint;');
+            writeln(outfile,' : longint;',aktspace,commentstr);
             writeln(extfile,' : longint;');
             flush(outfile);
          end
@@ -1574,7 +1596,7 @@ define_dec :
          begin
             write(outfile,' : ');
             write_type_specifier(outfile,$6^.p3);
-            writeln(outfile,';');
+            writeln(outfile,';',aktspace,commentstr);
             flush(outfile);
             write(extfile,' : ');
             write_type_specifier(extfile,$6^.p3);
@@ -1591,14 +1613,14 @@ define_dec :
      DEFINE dname SPACE_DEFINE NEW_LINE
      {
        (* DEFINE dname SPACE_DEFINE NEW_LINE *)
-       writeln(outfile,'{$define ',$2^.p,'}');
+       writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
        flush(outfile);
        if assigned($2)then
         dispose($2,done);
      }|
      DEFINE dname NEW_LINE
      {
-       writeln(outfile,'{$define ',$2^.p,'}');
+       writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
        flush(outfile);
        if assigned($2)then
         dispose($2,done);
@@ -1621,7 +1643,7 @@ define_dec :
             write(outfile,' = ');
             flush(outfile);
             write_expr(outfile,$4^.p1);
-            writeln(outfile,';');
+            writeln(outfile,';',aktspace,commentstr);
             popshift;
             if assigned($2) then
             dispose($2,done);
@@ -1651,7 +1673,7 @@ define_dec :
               begin
                  write(outfile,' : ');
                  write_type_specifier(outfile,$4^.p3);
-                 writeln(outfile,';');
+                 writeln(outfile,';',aktspace,commentstr);
                  flush(outfile);
                  write(extfile,' : ');
                  write_type_specifier(extfile,$4^.p3);
@@ -2427,7 +2449,9 @@ begin
      Writeln(outfile,aktspace,'  PDouble   = ^Double;');
      Writeln(outfile);
    end;
+  writeln(outfile,'{$IFDEF FPC}');
   writeln(outfile,'{$PACKRECORDS C}');
+  writeln(outfile,'{$ENDIF}');
   writeln(outfile);
 { Open tempfiles }
   Assign(extfile,'ext.tmp');
@@ -2464,7 +2488,11 @@ end.
 
 {
   $Log$
-  Revision 1.7  2004-06-20 17:56:05  marco
+  Revision 1.8  2004-08-13 02:35:29  carl
+    + bugfixes with C++ comments, they are now placed above the definition
+    * some bugfixes with the _label reserved word.
+
+  Revision 1.7  2004/06/20 17:56:05  marco
    Patch from Christian Iversen. ioresult check when opening for output
 
   Revision 1.6  2003/02/13 22:20:24  michael

+ 6 - 2
utils/h2pas/options.pas

@@ -21,7 +21,7 @@ unit options;
 interface
 
 const
-   version = '0.99.15';
+   version = '0.99.16';
 
 var
    inputfilename, outputfilename : string; { Filenames }
@@ -226,7 +226,11 @@ end;
 end.
 {
    $Log$
-   Revision 1.2  2002-09-07 15:40:34  peter
+   Revision 1.3  2004-08-13 02:35:30  carl
+     + bugfixes with C++ comments, they are now placed above the definition
+     * some bugfixes with the _label reserved word.
+
+   Revision 1.2  2002/09/07 15:40:34  peter
      * old logs removed and tabs fixed
 
 }

+ 49 - 5
utils/h2pas/scan.l

@@ -29,7 +29,7 @@ unit scan;
    lexlib,yacclib;
 
     const
-       version = '0.99.15';
+       version = '0.99.16';
 
     type
        Char=system.char;
@@ -146,6 +146,7 @@ unit scan;
        c : char;
        aktspace : string;
        block_type : tblocktype;
+       commentstr: string;
 
     const
        in_define : boolean = false;
@@ -384,14 +385,20 @@ D [0-9]
                                    if c='/' then
                                     begin
                                       if not stripcomment then
-                                       writeln(outfile,' }');
+                                       write(outfile,' }');
+                                      c:=get_char;
+                                      if (c=newline) then
+                                      begin
+                                        writeln(outfile);
+                                        unget_char(c); 
+                                      end;
                                       flush(outfile);
                                       exit;
                                     end
                                    else
                                     begin
                                       if not stripcomment then
-                                       write(outfile,' ');
+                                       write(outfile,'*');
                                       unget_char(c)
                                     end;
                                   end;
@@ -403,6 +410,12 @@ D [0-9]
                                        write(outfile,aktspace);
                                      end;
                                   end;
+                                { Don't write this thing out, to 
+                                  avoid nested comments.
+                                }  
+                              '{','}' :
+                                  begin
+                                  end;
                                 #0 :
                                   commenteof;
                                 else
@@ -413,8 +426,15 @@ D [0-9]
                           flush(outfile);
                         end;
 "//"                    begin
+                          commentstr:='';
+                          if (in_define) and not (stripcomment) then
+                          begin
+                             commentstr:='{';
+                          end
+                          else
                           If not stripcomment then
                             write(outfile,aktspace,'{');
+                            
                           repeat
                             c:=get_char;
                             case c of
@@ -422,15 +442,38 @@ D [0-9]
                                 begin
                                   unget_char(c);
                                   if not stripcomment then
-                                   writeln(outfile,' }');
+                                    begin
+                                      if in_define then
+                                        begin
+                                          commentstr:=commentstr+' }';  
+                                        end
+                                      else
+                                        begin
+                                          write(outfile,' }'); 
+                                          writeln(outfile); 
+                                        end;
+                                    end;  
                                   flush(outfile);
                                   exit;
                                 end;
+                              { Don't write this comment out, 
+                                to avoid nested comment problems
+                              }  
+                              '{','}' :
+                                  begin
+                                  end;
                               #0 :
                                 commenteof;
                               else
                                 if not stripcomment then
-                                 write(outfile,c);
+                                  begin
+                                    if in_define then
+                                     begin
+                                       commentstr:=commentstr+c;  
+                                     end
+                                    else
+                                      write(outfile,c);
+                                  end;
                             end;
                           until false;
                           flush(outfile);
@@ -629,6 +672,7 @@ D [0-9]
                            block_type:=bt_no;
                         end;
 "#"[ \t]*"define"       begin
+                           commentstr:='';
                            in_define:=true;
                            in_space_define:=1;
                            return(DEFINE);

+ 49 - 5
utils/h2pas/scan.pas

@@ -32,7 +32,7 @@ unit scan;
    lexlib,yacclib;
 
     const
-       version = '0.99.15';
+       version = '0.99.16';
 
     type
        Char=system.char;
@@ -149,6 +149,7 @@ unit scan;
        c : char;
        aktspace : string;
        block_type : tblocktype;
+       commentstr: string;
 
     const
        in_define : boolean = false;
@@ -393,14 +394,20 @@ begin
                                    if c='/' then
                                     begin
                                       if not stripcomment then
-                                       writeln(outfile,' }');
+                                       write(outfile,' }');
+                                      c:=get_char;
+                                      if (c=newline) then
+                                      begin
+                                        writeln(outfile);
+                                        unget_char(c); 
+                                      end;
                                       flush(outfile);
                                       exit;
                                     end
                                    else
                                     begin
                                       if not stripcomment then
-                                       write(outfile,' ');
+                                       write(outfile,'*');
                                       unget_char(c)
                                     end;
                                   end;
@@ -412,6 +419,12 @@ begin
                                        write(outfile,aktspace);
                                      end;
                                   end;
+                                { Don't write this thing out, to 
+                                  avoid nested comments.
+                                }  
+                              '{','}' :
+                                  begin
+                                  end;
                                 #0 :
                                   commenteof;
                                 else
@@ -423,8 +436,15 @@ begin
                         end;
   2:
                         begin
+                          commentstr:='';
+                          if (in_define) and not (stripcomment) then
+                          begin
+                             commentstr:='{';
+                          end
+                          else
                           If not stripcomment then
                             write(outfile,aktspace,'{');
+                            
                           repeat
                             c:=get_char;
                             case c of
@@ -432,15 +452,38 @@ begin
                                 begin
                                   unget_char(c);
                                   if not stripcomment then
-                                   writeln(outfile,' }');
+                                    begin
+                                      if in_define then
+                                        begin
+                                          commentstr:=commentstr+' }';  
+                                        end
+                                      else
+                                        begin
+                                          write(outfile,' }'); 
+                                          writeln(outfile); 
+                                        end;
+                                    end;  
                                   flush(outfile);
                                   exit;
                                 end;
+                              { Don't write this comment out, 
+                                to avoid nested comment problems
+                              }  
+                              '{','}' :
+                                  begin
+                                  end;
                               #0 :
                                 commenteof;
                               else
                                 if not stripcomment then
-                                 write(outfile,c);
+                                  begin
+                                    if in_define then
+                                     begin
+                                       commentstr:=commentstr+c;  
+                                     end
+                                    else
+                                      write(outfile,c);
+                                  end;
                             end;
                           until false;
                           flush(outfile);
@@ -698,6 +741,7 @@ begin
                         end;
   61:
                         begin
+                           commentstr:='';
                            in_define:=true;
                            in_space_define:=1;
                            return(DEFINE);