function TFuzzyLogicTokenStream.FinishToken(AChar: Integer;
const AString: String): IFuzzyStreamToken;
{ This function works because the ordinals of TFuzzyStreamTokenType are not equal to any
characters or single-character tokens
The first argument is a hint about what kind of token this is.
'0' means an atom or number; -1 means EOF was hit; '"' means a
String; '\n' means return CRLF; anything else is a one-character token.
}
var
SCode: Integer;
VI: Integer;
VD: Double;
Token: TFuzzyStreamToken;
Len: Integer;
begin
// create the token
Token := TFuzzyStreamToken.Create;
result := Token;
// set the token's line number to our current location
Token.LineNumber := LineNumber;
Len := Length(AString);
// use the AChar hint to determine what type of token the AString is
case AChar of
-1: // EOF
begin
if Len = 0 then
begin
Token.DataType := Ord(fstNone);
Token.Text := 'EOF';
end
else
begin
result := FinishToken( 0, AString );
Exit;
end;
end;
0:
begin
if AString[1] = '?' then // Variables
begin
Token.DataType := Ord(fstVariable);
if Len > 1 then
Token.Text := Copy(AString,2,Len);
{ else
Token.Text := (Owner as TInferenceEngine).GenerateSymbol( SBlankPrefix );}
Token.WhiteSpace := WhiteSpace;
Exit;
end
else if (Len = 1) and (AString[1] in ['-', '.', '+']) then
// Atoms that look like numbers
begin
Token.DataType := Ord(fstAtom);
Token.Text := AString;
Token.WhiteSpace := WhiteSpace;
Exit;
end;
// A Number
if ((AString[1] >= '0') and (AString[1] <= '9')) or
(AString[1] in ['-', '.', '+']) then
begin
// See if it is an integer
Val( AString, vi, SCode );
if SCode <> 0 then // ok, not an int, a float?
begin
Val( AString, VD, SCode );
if SCode <> 0 then // it must be an atom
begin
Token.DataType := Ord(fstAtom);
Token.Text := AString;
end
else
begin
Token.Number := VD;
Token.DataType := Ord(fstFloat);
end;
end
else
begin
Token.Number := vi;
Token.DataType := Ord(fstInteger);
end;
end
else // Plain Atom
begin
Token.DataType := Ord(fstAtom);
Token.Text := AString;
end;
end;
13: // Carraige return
begin
Token.DataType := Ord(fstAtom);
Token.Text := 'CRLF';
end;
Ord('"'): // Quoted String
begin
Token.DataType := Ord(fstString);
Token.Text := AString;
end;
Ord('&'), Ord('('), Ord(')'), Ord('='),
Ord('['), Ord(']'), Ord('~'), Ord('|'): // Single character tokens
begin
Token.DataType := AChar;
Token.Text := AString;
end;
else
raise EFzyParseException.CreateFmt( SInvalidBaseTokenHint, [IntToStr(AChar)] );
end;
// set the token's whitespace to all the whitespace we have found
// before this token and then reset the whitespace to empty
Token.WhiteSpace := WhiteSpace;
FWhiteSpace := '';
end;
|