File ParserUtilities.mesa
March 6, 1980 5:56 PM
Last Edited by: McCreight, January 28, 1985 4:12:05 pm PST
DIRECTORY
ParserTypeDefs, ParserUtilityDefs, ParserInputDefs;
ParserUtilities: CEDAR PROGRAM
IMPORTS ParserInputDefs, ParserTypeDefs EXPORTS ParserUtilityDefs =
BEGIN OPEN ParserUtilityDefs;
RecoverFromError: PUBLIC ERROR [errorCode: PossibleError] = CODE;
InitUtilities: PUBLIC PROCEDURE RETURNS [BOOLEAN] =
BEGIN
RETURN [TRUE];
END;
FinishUtilities: PUBLIC PROCEDURE RETURNS [BOOLEAN] =
BEGIN
RETURN [TRUE];
END;
Blank: PUBLIC PROCEDURE =
BEGIN
WHILE TRUE DO
SELECT ParserInputDefs.Peek[] FROM
'(,'),';,'-,ParserInputDefs.EOF,IN ['0..'9],IN ['A..'Z] => RETURN;
ENDCASE => [] ← ParserInputDefs.GetChar[];
ENDLOOP;
END;
Sep: PUBLIC PROCEDURE =
BEGIN
WHILE TRUE DO
SELECT ParserInputDefs.Peek[] FROM
'(,'),';,'-,ParserInputDefs.EOF,IN ['0..'9] => RETURN;
ENDCASE => [] ← ParserInputDefs.GetChar[];
ENDLOOP;
END;
Semi: PUBLIC PROCEDURE RETURNS [BOOLEAN] =
BEGIN
answer: BOOLEANFALSE;
Blank[];
IF ParserInputDefs.Peek[] = '; THEN
BEGIN [] ← ParserInputDefs.GetChar[]; answer ← TRUE; Blank[]; END;
RETURN[answer];
END;
GetPath: PUBLIC PROCEDURE [a: ParserTypeDefs.Path] =
BEGIN
temp: CHARACTER;
Sep[];
WHILE (temp ← ParserInputDefs.Peek[]) IN ['0..'9] OR temp = '- DO
ParserTypeDefs.AppendPoint[a,GetPoint[]];
Sep[];
ENDLOOP;
IF ParserTypeDefs.PathLength[a] = 0 THEN ERROR RecoverFromError[NullPath];
END;
Unsigned: PUBLIC PROCEDURE RETURNS [CARDINAL] =
BEGIN
someDigit: BOOLEANFALSE;
ans: CARDINAL ← 0;
BigNum: CARDINAL = (LAST[CARDINAL]-9)/10;
Sep[];
WHILE (ans < BigNum) AND ParserInputDefs.Peek[] IN ['0..'9] DO
ans ← ans * 10 + (ParserInputDefs.GetChar[]-'0);
someDigit ← TRUE;
ENDLOOP;
IF ~someDigit THEN ERROR RecoverFromError[MissingUnsigned];
IF ParserInputDefs.Peek[] IN ['0..'9] THEN
ERROR RecoverFromError[NumberTooBig]
ELSE
RETURN[ans];
END;
UnsignedLong: PUBLIC PROCEDURE RETURNS [LONG CARDINAL] =
BEGIN
someDigit: BOOLEANFALSE;
ans: LONG CARDINAL ← 0;
BigNum: LONG CARDINAL = (LAST[LONG CARDINAL]-9)/10;
Sep[];
WHILE (ans < BigNum) AND ParserInputDefs.Peek[] IN ['0..'9] DO
ans ← ans * 10 + (ParserInputDefs.GetChar[]-'0);
someDigit ← TRUE;
ENDLOOP;
IF ~someDigit THEN ERROR RecoverFromError[MissingUnsigned];
IF ParserInputDefs.Peek[] IN ['0..'9] THEN
ERROR RecoverFromError[NumberTooBig]
ELSE
RETURN[ans];
END;
Signed: PUBLIC PROCEDURE RETURNS [INTEGER] =
BEGIN
sign, someDigit: BOOLEANFALSE;
ans: INTEGER ← 0;
BigNum: INTEGER = (LAST[INTEGER]-9)/10;
Sep[];
IF ParserInputDefs.Peek[] = '- THEN
BEGIN
sign ← TRUE;
[] ← ParserInputDefs.GetChar[];
END;
WHILE (ans < BigNum) AND ParserInputDefs.Peek[] IN ['0..'9] DO
ans ← ans * 10 + (ParserInputDefs.GetChar[]-'0);
someDigit ← TRUE;
ENDLOOP;
IF ~someDigit THEN ERROR RecoverFromError[MissingSigned];
IF ParserInputDefs.Peek[] IN ['0..'9] THEN
ERROR RecoverFromError[NumberTooBig]
ELSE
RETURN[IF sign THEN -ans ELSE ans];
END;
SignedLong: PUBLIC PROCEDURE RETURNS [INT] =
BEGIN
sign, someDigit: BOOLEANFALSE;
ans: INT ← 0;
BigNum: INT = (LAST[INT]-9)/10;
Sep[];
IF ParserInputDefs.Peek[] = '- THEN
BEGIN
sign ← TRUE;
[] ← ParserInputDefs.GetChar[];
END;
WHILE (ans < BigNum) AND ParserInputDefs.Peek[] IN ['0..'9] DO
ans ← ans * 10 + (ParserInputDefs.GetChar[]-'0);
someDigit ← TRUE;
ENDLOOP;
IF ~someDigit THEN ERROR RecoverFromError[MissingSigned];
IF ParserInputDefs.Peek[] IN ['0..'9] THEN
ERROR RecoverFromError[NumberTooBig]
ELSE
RETURN[IF sign THEN -ans ELSE ans];
END;
GetPoint: PUBLIC PROCEDURE RETURNS [ParserTypeDefs.Point] =
BEGIN
ans: ParserTypeDefs.Point;
ans.x ← SignedLong[];
ans.y ← SignedLong[];
RETURN[ans];
END;
END.