InitClassArray:
PROC = { Assert[classArray=
NIL];
classArray ← zone.NEW[ClassArray ← ALL[oth]]; -- default is oth
FOR c: CHARACTER IN ['0..'9] DO classArray[c] ← num ENDLOOP; -- Numeric (0 to 9)
classArray[0C] ← dlm;
classArray[Ascii.SP] ← dlm;
classArray[Ascii.TAB] ← dlm;
classArray[Ascii.LF] ← dlm;
classArray[',] ← dlm; -- Delimiters
classArray['.] ← dot; -- Period, decimal point
classArray['+] ← classArray['-] ← sgn; -- Signs
classArray['B] ← classArray['b] ← oct; -- Octal suffix
classArray['E] ← classArray['e] ← exp; -- Exponent
classArray['(] ← lpr; -- Left paren
classArray[')] ← rpr; -- Right paren
classArray['{] ← lbr; -- Left brace
classArray['}] ← rbr; -- Right brace
classArray['"] ← quo; -- String quote
classArray['\\] ← bsl; -- Backslash; escape char in quoted strings
classArray['%] ← com; -- Comment
classArray[Ascii.CR] ← crt; -- Return; end of comment
};
Transition:
PROC[state: State, class: Class]
RETURNS[State,Action] = INLINE { SELECT state FROM
start =>
SELECT class
FROM
eos => { RETURN[null,null] }; -- no more tokens
crt,dlm => { RETURN[start,skip] }; -- ignore
com => { RETURN[comnt,skip] }; -- begin comment
quo => { RETURN[qstr,skip] }; -- begin string with next char
lpr => { RETURN[paren,skip] }; -- begin string with next char
rpr => { RETURN[oops,take] }; -- extra right paren
lbr => { RETURN[abeg,take] }; -- begin array
rbr => { RETURN[aend,take] }; -- end array
num => { RETURN[ipart,take] }; -- begin integer
sgn => { RETURN[msign,take] }; -- mantissa may follow
dot => { RETURN[point,take] }; -- fraction may follow
oct,exp,bsl,oth => { RETURN[ident,take] }; -- begin a name
ENDCASE;
comnt =>
SELECT class
FROM
eos => { RETURN[null,null] }; -- no more tokens
crt => { RETURN[start,skip] }; -- end of comment
dlm,com,lpr,rpr,lbr,rbr,num,sgn,dot,oct,exp,bsl,quo,oth => { RETURN[comnt,skip] }; -- ignore
ENDCASE;
msign =>
SELECT class
FROM
So far: sign for mantissa
eos,crt,dlm => { RETURN[name,null] }; -- finish as a name
com,lpr,rpr,lbr,rbr,quo => { RETURN[name,back] }; -- back up and finish
num => { RETURN[ipart,take] }; -- begin integer
dot => { RETURN[point,take] }; -- fraction may follow
sgn,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
point =>
SELECT class
FROM
So far: (sign and`) decimal point
eos,crt,dlm => { RETURN[name,null] }; -- finish as a name
com,lpr,rpr,lbr,rbr,quo => { RETURN[name,back] }; -- back up and finish
num => { RETURN[fpart,take] }; -- begin fraction
dot,sgn,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
ipart =>
SELECT class
FROM
So far: valid integer
eos,crt,dlm => { RETURN[inum,null] }; -- finish integer
com,lpr,rpr,lbr,rbr,quo => { RETURN[inum,back] }; -- back up and finish integer
num => { RETURN[ipart,take] }; -- add integer digit
dot => { RETURN[fpart,take] }; -- fraction follows
oct => { RETURN[octal,take] }; -- octal number
exp => { RETURN[expon,take] }; -- exponent follows
sgn,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
octal =>
SELECT class
FROM
So far: valid integer followed by "B"
eos,crt,dlm => { RETURN[onum,null] }; -- finish octal
com,lpr,rpr,lbr,rbr,quo => { RETURN[onum,back] }; -- back up and finish octal
num,dot,sgn,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
fpart =>
SELECT class
FROM
So far: valid mantissa containing decimal point
eos,crt,dlm => { RETURN[rnum,null] }; -- finish real
com,lpr,rpr,lbr,rbr,quo => { RETURN[rnum,back] }; -- back up and finish real
num => { RETURN[fpart,take] }; -- add fraction digit
exp => { RETURN[expon,take] }; -- exponent follows
dot,sgn,oct,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
expon =>
SELECT class
FROM
So far: valid mantissa followed by "E"
eos,crt,dlm => { RETURN[name,null] }; -- finish as a name
com,lpr,rpr,lbr,rbr,quo => { RETURN[name,back] }; -- back up and finish
num => { RETURN[epart,take] }; -- first exponent digit
sgn => { RETURN[esign,take] }; -- exponent sign; digit should follow
dot,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
esign =>
SELECT class
FROM
So far: valid mantissa followed by "E" and a sign
eos,crt,dlm => { RETURN[name,null] }; -- finish as a name
com,lpr,rpr,lbr,rbr,quo => { RETURN[name,back] }; -- back up and finish
num => { RETURN[epart,take] }; -- first exponent digit
sgn,dot,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
epart =>
SELECT class
FROM
So far: valid real with exponent
eos,crt,dlm => { RETURN[rnum,null] }; -- finish real
com,lpr,rpr,lbr,rbr,quo => { RETURN[rnum,back] }; -- back up and finish real
num => { RETURN[epart,take] }; -- add exponent digit
sgn,dot,oct,exp,bsl,oth => { RETURN[ident,take] }; -- not a number
ENDCASE;
ident =>
SELECT class
FROM
So far: an identifier
eos,crt,dlm => { RETURN[name,null] }; -- finish name
com,lpr,rpr,lbr,rbr,quo => { RETURN[name,back] }; -- back up and finish name
num,sgn,dot,oct,exp,bsl,oth => { RETURN[ident,take] }; -- append
ENDCASE;
qstr =>
SELECT class
FROM
eos => { RETURN[part,null] }; -- premature end
bsl => { RETURN[qstr3,take] }; -- read char after \
quo => { RETURN[strg,null] }; -- finish string
crt,dlm,com,lbr,rbr,num,sgn,dot,oct,exp,lpr,rpr,oth => { RETURN[qstr,take] }; -- append
ENDCASE;
qstr2 =>
SELECT class
FROM
eos => { RETURN[part,null] }; -- premature end
bsl => { RETURN[qstr3,take] }; -- read char after \
quo => { RETURN[strg2,null] }; -- finish string with \'s
crt,dlm,com,lbr,rbr,num,sgn,dot,oct,exp,lpr,rpr,oth => { RETURN[qstr2,take] }; -- append
ENDCASE;
qstr3 =>
SELECT class
FROM
eos => { RETURN[part,null] }; -- premature end
crt,dlm,com,lbr,rbr,num,sgn,dot,oct,exp,lpr,rpr,bsl,quo,oth => { RETURN[qstr2,take] }; -- append
ENDCASE;
paren =>
SELECT class
FROM
eos => { RETURN[part,null] }; -- premature end
lpr => { RETURN[SUCC[paren],take] }; -- up a level
rpr => { RETURN[strg,null] }; -- finish string
crt,dlm,com,lbr,rbr,num,sgn,dot,oct,exp,quo,bsl,oth => { RETURN[paren,take] }; -- append
ENDCASE;
ENDCASE =>
SELECT class
FROM
eos => { RETURN[part,null] }; -- premature end
lpr => { RETURN[SUCC[state],take] }; -- open
rpr => { RETURN[PRED[state],take] }; -- close
crt,dlm,com,lbr,rbr,num,sgn,dot,oct,exp,quo,bsl,oth => { RETURN[state,take] }; -- append
ENDCASE;
ERROR Bug; -- unknown state or class
};
}.