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: BOOLEAN ← FALSE;
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: BOOLEAN ← FALSE;
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]
END;
UnsignedLong:
PUBLIC
PROCEDURE
RETURNS [
LONG
CARDINAL] =
BEGIN
someDigit: BOOLEAN ← FALSE;
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]
END;
Signed:
PUBLIC
PROCEDURE
RETURNS [
INTEGER] =
BEGIN
sign, someDigit: BOOLEAN ← FALSE;
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: BOOLEAN ← FALSE;
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.