prettier.mod

来自「一个Modula-2语言分析器」· MOD 代码 · 共 73 行

MOD
73
字号
IMPLEMENTATION MODULE 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 *)

  IMPORT FileIO, Mod2S;

  CONST
    BufMax = 12000;
  VAR
    AllBlanks : BOOLEAN;
    Last, Indentation : CARDINAL;
    Buffer : ARRAY [0 .. BufMax] OF CHAR;

  PROCEDURE NewLine;
    BEGIN
      IF NOT AllBlanks THEN (* first strip trailing spaces *)
        WHILE (Last > 0) & (Buffer[Last-1] = ' ') DO DEC(Last) END;
        FileIO.WriteBytes(results, Buffer, Last);
        FileIO.WriteLn(results);
      END;
      Last := 0;
      WHILE Last < Indentation DO Buffer[Last] := ' '; INC(Last) END;
      Buffer[Last] := 0C; AllBlanks := TRUE;
    END NewLine;

  PROCEDURE Append (Str : ARRAY OF CHAR);
    VAR
      l : CARDINAL;
    BEGIN
      l := 0;
      WHILE (l <= HIGH(Str)) AND (Str[l] # 0C) DO
        Buffer[Last] := Str[l];
        IF Str[l] # ' ' THEN AllBlanks := FALSE END;
        INC(l); INC(Last)
      END;
      Buffer[Last] := 0C;
      IF Mod2S.seenComment THEN
        NewLine; AllBlanks := FALSE;
        Mod2S.GetComment(Buffer, Last, l); INC(Last, l);
        Buffer[Last] := ' '; INC(Last); Buffer[Last] := CHR(0);
      END
    END Append;

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

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

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

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

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

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

⌨️ 快捷键说明

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