-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.pas
63 lines (49 loc) · 1009 Bytes
/
tokens.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
unit Tokens;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
Procedure InitToken();
function Tokenize(s : string):integer;
const
MAX_TOKEN=255;
var
token : array [1..MAX_TOKEN] of string;
tokenpos : array [1..MAX_TOKEN] of integer;
analyzeStr : String;
//------------------------------------------------------------------------------
implementation
Procedure InitToken();
var i:integer;
begin
for i:=1 to MAX_TOKEN do
Token[i]:='';
end;
function Tokenize(s : string):integer;
var i,n,step:integer;
begin
//-- Suppress beginning and ending spaces
s:=Trim(s);
analyzeStr:=s;
n:=0;
step:=0;
for i:=1 to length(s) do
begin
if (s[i]<>#32) and (s[i]<>#9) then
begin //A char differente than space and tab
if (step=0) or (step=2) then begin
inc(n);
token[n]:='';
tokenpos[n]:=i;
step:=1;
end;
token[n]:=token[n]+s[i];
end
else
begin
step:=2;
end;
end;
Tokenize:=n;
end;
end.