ParseTreePrinting.Mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Spreitzer, February 22, 1986 7:08:38 pm PST
ParseTreePrinting:
CEDAR
PROGRAM
IMPORTS Convert, IO, StructuredStreams, UnparserBuffer
= {OPEN EDIFfing, SS: StructuredStreams, UB: UnparserBuffer;
LORA: TYPE = LIST OF REF ANY;
ROPE: TYPE = Rope.ROPE;
breakLast: BOOL ← TRUE;
lastOffset: INT ← 0;
PrettyPrintParseTree:
PROC [to:
IO.
STREAM, pt: ParseTree] = {
WITH pt
SELECT
FROM
x:
REF ParseTreePrivate.string => {
to.PutRope[Convert.RopeFromRope[x.s]];
};
x:
REF ParseTreePrivate.integer => {
to.PutF["%g", [integer[x.i]]];
};
x:
REF ParseTreePrivate.identifier => {
to.PutF["%g", [atom[x.id]]];
};
form:
REF ParseTreePrivate.list => {
to.PutChar['(];
SS.Begin[to];
{ENABLE UNWIND => SS.End[to];
FOR l: ParseTreeList ← form.children, l.rest
WHILE l #
NIL
DO
PrettyPrintParseTree[to, l.first];
IF l.rest # NIL THEN {to.PutChar[' ]; SS.Bp[to, FALSE, 0]}
ELSE IF breakLast THEN SS.Bp[to, FALSE, lastOffset];
ENDLOOP;
to.PutChar[')];
};
SS.End[to];
};
ENDCASE => ERROR;
};
Print:
PROC [to:
IO.
STREAM, pt: ParseTree] = {
ubh: UB.Handle = UB.NewHandle[[stream[to]]];
ss: IO.STREAM = SS.Create[ubh];
PrettyPrintParseTree[ss, pt];
ss.PutRope["\n"];
ss.Close[];
};
}.