-- PPHelperImpl.mesa -- Russ Atkinson, June 9, 1982 3:49 pm DIRECTORY Ascii USING [ControlZ, CR, FF, TAB], PPHelper, PPLeaves USING [HTIndex, LTIndex], PPP1 USING [IdOfLock], PPTree USING [Handle, Link, NodeName], PPTreeOps USING [NSons, NthSon], PPUtil USING [ShowChar], Rope USING [Equal, ROPE, Text]; PPHelperImpl: CEDAR PROGRAM IMPORTS PPP1, PPTreeOps, PPUtil, Rope EXPORTS PPHelper SHARES Rope = BEGIN OPEN Ascii, PPLeaves, PPTreeOps, Tree: PPTree, Rope; LockId: HTIndex _ PPP1.IdOfLock[]; -- this handle is used to hold on to the log stream indent, position, line, page: PUBLIC INT _ 0; sizing: PUBLIC BOOL _ FALSE; lastChar: PUBLIC CHAR _ 0C; lateTrigger: PUBLIC INT _ 80; earlyTrigger: PUBLIC INT _ 64; Excess: PUBLIC ERROR = CODE; Init: PUBLIC PROC = { indent _ position _ line _ page _ 0; sizing _ FALSE; }; GetInfo: PUBLIC PROC [t: Tree.Link] RETURNS [info: INT] = { DO WITH t SELECT FROM hti: HTIndex => info _ IF hti = LockId THEN 0 ELSE hti.index; lti: LTIndex => info _ lti.index; x: Tree.Handle => {info _ x.info; IF info = 0 THEN FOR i: NAT IN [1..NSons[t]] WHILE info = 0 DO info _ GetInfo[NthSon[t, i]] ENDLOOP} ENDCASE => info _ 0; EXIT ENDLOOP}; GetAttrs: PUBLIC PROC [t: Tree.Link] RETURNS [a1,a2,a3: BOOL] = { WITH t SELECT FROM x: Tree.Handle => RETURN [x.attr[1], x.attr[2], x.attr[3]] ENDCASE => a1 _ a2 _ a3 _ FALSE}; WriteChar: PUBLIC PROC [c: CHAR] = { IF (lastChar _ c) < 40C THEN {SELECT c FROM TAB => {position _ position + 8; position _ position - position MOD 8; lastChar _ ' }; CR => {position _ 0; line _ line + 1}; FF => {position _ 0; line _ 0; page _ page + 1}; ENDCASE} ELSE position _ position + 1; IF sizing THEN {IF lastChar # ' AND position >= lateTrigger THEN ERROR Excess; RETURN}; TRUSTED {PPUtil.ShowChar[c]}}; WriteId: PUBLIC PROC [r: ROPE] = { s: Text _ NARROW[r]; c: CHAR _ lastChar _ 'A; len: NAT _ IF s = NIL THEN 0 ELSE s.length; IF len = 0 THEN RETURN; c _ s[0]; IF c IN ['B..'C] THEN {-- could be a trivial translation SELECT len FROM 7 => IF Rope.Equal[s, "BOOLEAN"] THEN {s _ "BOOL"; len _ 4}; 9 => IF Rope.Equal[s, "CHARACTER"] THEN {s _ "CHAR"; len _ 4}; ENDCASE}; position _ position + len; IF sizing THEN {IF position > lateTrigger THEN ERROR Excess; RETURN}; FOR i: NAT IN [0..len) DO TRUSTED {PPUtil.ShowChar[s[i]]}; ENDLOOP}; WriteText: PUBLIC PROC [r: ROPE] = { -- we assume that the rope has been flattened -- an error occurs if it is not IF r # NIL THEN { s: Text = NARROW[r]; FOR i: NAT IN [0..s.length) DO c: CHAR _ s[i]; IF c = Ascii.ControlZ THEN {WriteChar[Ascii.CR]; RETURN}; WriteChar[c] ENDLOOP}}; WriteQuotedText: PUBLIC PROC [r: ROPE] = { -- we assume that the rope has been flattened -- an error occurs if it is not -- we also assume that the scanner has escape marks in -- but that the leading and trailing quotes are gone WriteChar['"]; IF r # NIL THEN { s: Text = NARROW[r]; FOR i: NAT IN [0..s.length) DO WriteChar[s[i]]; ENDLOOP}; WriteChar['"]}; Indent: PUBLIC PROC = { count: INT _ indent - position; WHILE (count_count-1) >= 0 DO WriteChar[' ] ENDLOOP}; Break: PUBLIC PROC = { IF position > indent THEN WriteChar[CR]; Indent[]}; END.