home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TPF.ZIP / TPFCOM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-05-20  |  9.4 KB  |  312 lines

  1. {[B+]}
  2. {*-------------------------*
  3.  | Block Comment Character |
  4.  *-------------------------*}
  5.  
  6. var
  7.     Stat_Break: Integer;                {character where line can be broken}
  8.     Stat_Blanks: Boolean;               {set if blank was last char}
  9.     First_Input_Line: Boolean;          {set if first input line}
  10.  
  11.  
  12. procedure Block_Com_Char {(Character: Char)} ;
  13. {
  14. ! Write a character for a block comment. The comment formatting must be
  15. ! terminated with a call to adjust_block_comment. The comment is copied
  16. ! exactly, and if it will not fit within the out_line_len a message will
  17. ! be printed.
  18. }
  19.     begin
  20.     if End_File then Abort(Syntax);
  21.  
  22.     if Formatting then
  23.         if New_Input_Line and (Character = ' ') then
  24.             begin
  25.             if Write_Col > Out_Line_Len then Comment_Overflow;
  26.             Print_Line(Column);
  27.             First_Input_Line := false;
  28.             New_Input_Line := false;
  29.             end
  30.         else Write_A(Character);
  31.     end; {Block_Com_Char}
  32.  
  33.  
  34. procedure Break_Stat_Comment;
  35. {
  36. ! Break a statement comment at the last break. Assumes (stat_break <> 0)
  37. ! and (char_count - stat_break < Buf_Size)
  38. }
  39.  
  40.     var
  41.         Extra_Len: Integer;             {length from last break}
  42.         Com_Indent: Integer;            {amount to indent the extra}
  43.  
  44.     begin
  45.     Extra_Len := Char_Count - Stat_Break + 1;
  46.  
  47.     if Write_Col - Extra_Len > Max_Line_Len then Abort(Com_Format)
  48.     else
  49.         begin {we can at least write it}
  50.         if Write_Col - Extra_Len > Out_Line_Len then Comment_Overflow;
  51.         Com_Indent := Out_Line_Len - Extra_Len;
  52.         if Com_Indent < 0 then Com_Indent := 0
  53.         else if Com_Indent > Remark_Col then Com_Indent := Remark_Col;
  54.  
  55.         with Unwritten[Stat_Break mod Buf_Size] do
  56.             begin
  57.             Action_Is := Begin_Line;
  58.             Spacing := Com_Indent;
  59.             end;
  60.  
  61.         Current_Line := Current_Line + 1;
  62.         Write_Col := Com_Indent + Extra_Len;
  63.         end;
  64.     end; {Break_Stat_Comment}
  65.  
  66.  
  67. procedure Stat_Com_Char {(Character: Char)} ;
  68. {
  69. ! Take a statement character and format it. assumes that stat_break
  70. ! and stat_blank are initialized before the first character and
  71. ! are unchanged thereafter. The procedure adjust_stat_comment must
  72. ! be called after the comment is done
  73. }
  74.     begin
  75.     if End_File then Abort(Syntax);
  76.  
  77.     if Formatting then
  78.         if Character = ' ' then
  79.             begin
  80.  
  81.             if not Stat_Blanks then
  82.                 begin
  83.                 if (Write_Col > Out_Line_Len) and (Stat_Break <> 0) then
  84.                     Break_Stat_Comment;
  85.                 Write_A(' ');
  86.                 Stat_Break := Char_Count;
  87.                 Stat_Blanks := true;
  88.                 end;
  89.             end
  90.         else
  91.             begin
  92.             Write_A(Character);
  93.             Stat_Blanks := false;
  94.             end;
  95.     end; {Stat_Com_Char}
  96.  
  97.  
  98. procedure Do_Comment(Block: Boolean; {true if block comment}
  99.                      Init_Col: Line_Index; {starting column}
  100.                      Init_Char: Char {starting char} );
  101. {
  102. ! Handles all comments.
  103. !
  104. ! Comments are split into two classes which are handled separately.
  105. !
  106. ! Comments which begin a line are treated as "block comments" and
  107. ! are not formatted.  At most, it will be folded to fit on the
  108. ! output line.
  109. !
  110. ! Comments which follow other statements on a line are formatted
  111. ! like any other statement.
  112. }
  113.  
  114.  
  115.     procedure Adjust_Block_Comment(Start: Integer);
  116.     {
  117.     ! If the comment is all on one line, adjust it to line up with
  118.     ! the indentation if possible, otherwise just try to fit it somehow.
  119.     ! In any case, if the comment extends beyond the allowable length,
  120.     ! bitch about it.
  121.     }
  122.  
  123.         var
  124.             Com_Length: Integer;        {length of comment if on one line}
  125.             Com_Indent: Integer;        {amount to indent comment}
  126.  
  127.         begin
  128.  
  129.         if Formatting then
  130.             begin
  131.  
  132.             if First_Input_Line then
  133.                 begin
  134.                 Com_Length := Char_Count - Start;
  135.                 Com_Indent := Out_Line_Len - Com_Length;
  136.                 if Com_Indent < 0 then Com_Indent := 0
  137.                 else if Com_Indent > Stat_Indent then
  138.                     Com_Indent := Stat_Indent;
  139.                 Unwritten[Start mod Buf_Size].Spacing := Com_Indent;
  140.                 Write_Col := Com_Indent + Com_Length;
  141.                 end;
  142.             if Write_Col > Out_Line_Len then Comment_Overflow;
  143.             end; {if formatting}
  144.         end; {Adjust_Block_Comment}
  145.  
  146.  
  147.     procedure Adjust_Stat_Comment;
  148.     {
  149.     ! Called after the last character of a statment comment has been
  150.     ! written to ensure that it all fits on a line
  151.     }
  152.         begin
  153.  
  154.         if Formatting then
  155.             if Write_Col > Out_Line_Len then
  156.                 if Stat_Break = 0 then
  157.                     if Write_Col <= Max_Line_Len then Comment_Overflow
  158.                     else Abort(Com_Format)
  159.                 else Break_Stat_Comment;
  160.         end; {Adjust_Stat_Comment}
  161.  
  162.  
  163.     procedure Block_Comment(Column: Line_Index; {starting column}
  164.                             Init_Char: Char);
  165.     {
  166.     ! Format a block comment: If the comment is all on one input line
  167.     ! it will be indented to the current statement level unless it
  168.     ! won't fit, in which case it is shifted left until it will fit.
  169.     ! If any part of a block comment will not fit in the output line,
  170.     ! the output line will be extended and a message printed.
  171.     }
  172.  
  173.         var
  174.             Term_Char1, Term_Char2: Char;
  175.             Com_Start: Integer;         {start of comment}
  176.  
  177.         begin
  178.         Print_Line(Column - 1);
  179.         Com_Start := Char_Count;
  180.         First_Input_Line := true;
  181.  
  182.         if Init_Char = '{' then
  183.             begin
  184.             Term_Char1 := '}';
  185.             Term_Char2 := '}';
  186.             Block_Com_Char('{')
  187.             end
  188.         else
  189.             begin
  190.             Term_Char1 := '*';
  191.             Term_Char2 := ')';
  192.             Block_Com_Char('(');
  193.             Block_Com_Char('*');
  194.             end;
  195.  
  196.         Get_Char;
  197.         if Ch = '[' then Do_Formatter_Directives(1);
  198.  
  199.         repeat
  200.             while Ch <> Term_Char1 do
  201.                 begin
  202.                 Block_Com_Char(Ch);
  203.                 Get_Char;
  204.                 end;
  205.  
  206.             if Ch = '*' then
  207.                 begin
  208.                 Get_Char;
  209.                 if Ch <> ')' then Block_Com_Char('*');
  210.                 end;
  211.         until Ch = Term_Char2;
  212.  
  213.         if Ch = '}' then Block_Com_Char('}')
  214.         else
  215.             begin
  216.             Block_Com_Char('*');
  217.             Block_Com_Char(')');
  218.             end;
  219.  
  220.         if Block then Adjust_Block_Comment(Com_Start);
  221.         end; {Block_Comment}
  222.  
  223.  
  224.     procedure Stat_Comment(Init_Char: Char);
  225.     {
  226.     ! Format a statement comment: These are inserted in the line at the place
  227.     ! found, and subsequent lines are indented to the start of the comment. If
  228.     ! the start of the comment is too far to the right, it will be indented on
  229.     ! the next line. Text will be moved as necessary to fill lines. All breaks
  230.     ! will be at blanks, and if it is not possible to break a comment properly
  231.     ! the output line will be extended and a message printed
  232.     }
  233.  
  234.         var
  235.             Term_Char1, Term_Char2: Char;
  236.  
  237.         begin
  238.         {initialize stat_com_char}
  239.         Stat_Break := 0;
  240.         Stat_Blanks := false;
  241.         Indent_Plus(Write_Col + Comment_Spaces + 1 - Indent);
  242.  
  243.         if Indent > Three_Fourth_Line then
  244.             begin
  245.             Undent;
  246.             Indent_Plus(Tab_Spaces);
  247.             end;
  248.  
  249.         if Write_Col < Out_Line_Len - Comment_Spaces - 1 then
  250.             if In_Declaration then
  251.                 if (Remark_Col - Write_Col) > Comment_Spaces then
  252.                     Space(Remark_Col - Write_Col)
  253.                 else Space(Comment_Spaces)
  254.             else Space(Comment_Spaces);
  255.  
  256.         if Init_Char = '{' then
  257.             begin
  258.             Term_Char1 := '}';
  259.             Term_Char2 := '}';
  260.             Stat_Com_Char('{')
  261.             end
  262.         else
  263.             begin
  264.             Term_Char1 := '*';
  265.             Term_Char2 := ')';
  266.             Stat_Com_Char('(');
  267.             Stat_Com_Char('*');
  268.             end;
  269.  
  270.         Get_Char;
  271.         if Ch = '[' then Do_Formatter_Directives(2);
  272.  
  273.         repeat
  274.             while Ch <> Term_Char1 do
  275.                 begin
  276.                 Stat_Com_Char(Ch);
  277.                 Get_Char;
  278.                 end;
  279.  
  280.             if Ch = '*' then
  281.                 begin
  282.                 Get_Char;
  283.                 if Ch <> ')' then Stat_Com_Char('*');
  284.                 end;
  285.         until Ch = Term_Char2;
  286.  
  287.         if Ch = '}' then Stat_Com_Char('}')
  288.         else
  289.             begin
  290.             Stat_Com_Char('*');
  291.             Stat_Com_Char(')');
  292.             end;
  293.  
  294.         Adjust_Stat_Comment;
  295.         Undent;
  296.         Blank_Lines := 0;
  297.         New_Input_Line := false;
  298.         end; {Stat_Comment}
  299.  
  300.     begin {Do_Comment}
  301.     New_Input_Line := false;
  302.     if Block then Block_Comment(Init_Col, Init_Char)
  303.     else Stat_Comment(Init_Char);
  304.     Formatting := New_Formatting;
  305.     New_Input_Line := false;
  306.     Get_Char;
  307.     while (Ch = ' ') and not New_Input_Line do Get_Char;
  308.     if Formatting and New_Input_Line then End_Line := true;
  309.     Symbol_Found := false;
  310.     Last_Sym := Comment;
  311.     end; {Do_Comment}
  312.