Re: issue-#95 TextMapper Scanner & SETS
Posted: Tue Jan 19, 2016 6:37 pm
I have found the solution with well readable logic. So it does not have main disadvantage of state machines.
It is based on the Robert's version and passing his test.
Code: Select all
PROCEDURE Set (VAR s: Scanner);
CONST nothing = 0; digit = 1; comma = 2;
VAR n, m: INTEGER; ch: CHAR; waiting: BYTE;
BEGIN
s.type := set; Get(s, ch); s.Skip(ch); s.set := {};
IF ch = "}" THEN Get(s, ch)
ELSE
waiting := digit;
WHILE (s.type = set) & (waiting # nothing) DO
IF waiting = digit THEN
IF ("0" <= ch) & (ch <= "9") THEN
Cardinal(s,n); s.Skip(ch);
IF (MIN(SET) <= n) & (n <= MAX(SET)) THEN
INCL(s.set, n)
ELSE s.type := invalid END;
waiting := comma
ELSE s.type := invalid END
ELSIF waiting = comma THEN
IF ch = "," THEN Get(s, ch); s.Skip(ch); waiting := digit
ELSIF ch = "." THEN
Get(s, ch);
IF ch = "." THEN
Get(s, ch); s.Skip(ch); Cardinal(s, m); s.Skip(ch);
IF (n <= m) & (m <= MAX(SET)) THEN
WHILE m > n DO INCL(s.set, m); DEC(m) END
ELSE s.type := invalid END
ELSE s.type := invalid END
ELSIF ch = "}" THEN Get(s, ch); waiting := nothing
ELSE s.type := invalid END
END
END
END
END Set;