prettier.pas

来自「一个Pascal语言分析器」· PAS 代码 · 共 104 行

PAS
104
字号
UNIT Prettier;
(* Pretty printing auxiliary functions for use with Modula-2 pretty printing
   program generated by COCO from an attributed grammar
   P.D. Terry, Rhodes University, 1995 *)

INTERFACE

  VAR
    results : TEXT;

  PROCEDURE Append (Str : STRING);
  (* Append String to output buffer *)

  PROCEDURE IndentNextLine;
  (* If output buffer has characters other than spaces, copy output buffer and
     line mark to results file.  Prepare to indent further lines by two
     spaces more than before *)

  PROCEDURE ExdentNextLine;
  (* If output buffer has characters other than spaces, copy output buffer and
     line mark to results file.  Prepare to indent further lines by two
     spaces less than before *)

  PROCEDURE NewLine;
  (* If output buffer has characters other than spaces, copy output buffer and
     line mark to results file  *)

  PROCEDURE BlankLine;
  (* NewLine, followed by line mark *)

  PROCEDURE Indent;
  (* Increment indentation level by two spaces *)

  PROCEDURE Exdent;
  (* Decrement indentation level by two spaces *)

IMPLEMENTATION
  USES Mod2S;

  VAR
    AllBlanks : BOOLEAN;
    Last, Indentation : INTEGER;
    Buffer : Mod2S.CommentStr;

  PROCEDURE NewLine;
    VAR
      i : INTEGER;
    BEGIN
      IF NOT AllBlanks THEN BEGIN (* first strip trailing spaces *)
        WHILE (Last > 0) AND (Buffer[Last-1] = ' ') DO DEC(Last);
        FOR i := 0 TO Last - 1 DO Write(results, Buffer[i]);
        WriteLn(results);
      END;
      Last := 0;
      WHILE Last < Indentation DO BEGIN Buffer[Last] := ' '; INC(Last) END;
      Buffer[Last] := CHR(0); AllBlanks := TRUE;
    END;

  PROCEDURE Append (Str : STRING);
    VAR
      l : INTEGER;
    BEGIN
      FOR l := 1 TO Length(Str) DO BEGIN
        Buffer[Last] := Str[l];
        IF Str[l] <> ' ' THEN AllBlanks := FALSE;
        INC(Last)
      END;
      Buffer[Last] := CHR(0);
      IF Mod2S.seenComment THEN BEGIN
        NewLine; AllBlanks := FALSE;
        Mod2S.GetComment(Buffer, Last, l); Last := Last + l;
        Buffer[Last] := ' '; INC(Last); Buffer[Last] := CHR(0);
      END
    END;

  PROCEDURE IndentNextLine;
    BEGIN
      INC(Indentation, 2); NewLine
    END;

  PROCEDURE ExdentNextLine;
    BEGIN
      IF Indentation >= 2 THEN DEC(Indentation, 2); NewLine
    END;

  PROCEDURE Indent;
    BEGIN
      INC(Indentation, 2)
    END;

  PROCEDURE Exdent;
    BEGIN
      IF Indentation >= 2 THEN DEC(Indentation, 2)
    END;

  PROCEDURE BlankLine;
    BEGIN
      NewLine; WriteLn(results);
    END;

  BEGIN
    Indentation := 0; AllBlanks := TRUE; Last := 0;
  END.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?