Refactoring
This commit is contained in:
@@ -12,34 +12,34 @@ uses
|
||||
type
|
||||
{$IFDEF FPC}TFpcList=specialize TFPGList<String>;{$ENDIF}
|
||||
|
||||
TTokenizerPos = record
|
||||
TAGTokenizerPos = record
|
||||
x, y: integer;
|
||||
end;
|
||||
|
||||
TToken = record
|
||||
TAGToken = record
|
||||
Text: string;
|
||||
&begin, &end: TTokenizerPos;
|
||||
&begin, &end: TAGTokenizerPos;
|
||||
ended: boolean;
|
||||
{$IFNDEF FPC}constructor Create(Text: string; &begin, &end: TTokenizerPos;ended:boolean);{$ENDIF}
|
||||
{$IFNDEF FPC}constructor Create(Text: string; &begin, &end: TAGTokenizerPos;ended:boolean);{$ENDIF}
|
||||
end;
|
||||
|
||||
TPasTokenizer = class
|
||||
private
|
||||
TAGPasTokenizer = class
|
||||
strict protected
|
||||
s: TStrings;
|
||||
y: integer;
|
||||
x: integer;
|
||||
ended: boolean;
|
||||
function _do_readable(): boolean;
|
||||
function _is_readable(): boolean;
|
||||
function _next_readable(): boolean;
|
||||
procedure _skip_spaces();
|
||||
function _get_pos(): TTokenizerPos;
|
||||
procedure _set_pos(i0: integer; i1: integer);
|
||||
function DoReadable():boolean;
|
||||
function IsReadable():boolean;
|
||||
function NextReadable():boolean;
|
||||
procedure SkipSpaces();
|
||||
function GetPos():TAGTokenizerPos;
|
||||
procedure SetPos(pos:TAGTokenizerPos);
|
||||
public
|
||||
function get_next(): TToken;
|
||||
ended: boolean;
|
||||
function GetNext(): TAGToken;
|
||||
// procedure read_next();
|
||||
// procedure is_ended();
|
||||
constructor Create(input:TStrings);
|
||||
property Pos:TAGTokenizerPos read GetPos write SetPos;
|
||||
end;
|
||||
|
||||
{PasTokenizerStack = class
|
||||
@@ -55,14 +55,14 @@ type
|
||||
procedure is_ended();
|
||||
end;}
|
||||
|
||||
function is_comment(s: string): boolean;
|
||||
function is_name(s: string): boolean;
|
||||
function is_string(s: string): boolean;
|
||||
function IsComment(s: string): boolean;
|
||||
function IsName(s: string): boolean;
|
||||
function IsString(s: string): boolean;
|
||||
|
||||
implementation
|
||||
|
||||
{$IFNDEF FPC}
|
||||
constructor TToken.Create(Text: string; &begin, &end: TTokenizerPos;
|
||||
constructor TAGToken.Create(Text: string; &begin, &end: TAGTokenizerPos;
|
||||
ended: boolean);
|
||||
begin
|
||||
Self.Text := Text;
|
||||
@@ -83,12 +83,12 @@ const
|
||||
var
|
||||
SYMS2:{$IFDEF FPC}TFpcList{$ELSE}TList<string>{$ENDIF}; // array[0..8]of string=();
|
||||
|
||||
function is_comment(s: string): boolean;
|
||||
function IsComment(s:string):boolean;
|
||||
begin
|
||||
Result:=(s.startswith('{') or s.startswith('(*') or s.startswith('//'));
|
||||
end;
|
||||
|
||||
function is_name(s: string): boolean;
|
||||
function IsName(s:string):boolean;
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
@@ -103,16 +103,17 @@ begin
|
||||
if not CHARS_ID.Contains(s[i]) then
|
||||
Exit(False);
|
||||
end;
|
||||
Result:=True;
|
||||
end;
|
||||
|
||||
function is_string(s: string): boolean;
|
||||
function IsString(s: string):boolean;
|
||||
begin
|
||||
Result:=s.StartsWith(#39);
|
||||
end;
|
||||
|
||||
function TPasTokenizer._do_readable(): boolean;
|
||||
function TAGPasTokenizer.DoReadable(): boolean;
|
||||
begin
|
||||
if not _is_readable() then
|
||||
if not IsReadable() then
|
||||
begin
|
||||
if (y + 1 = s.Count) then
|
||||
ended := True
|
||||
@@ -136,54 +137,53 @@ begin
|
||||
Exit(False);
|
||||
end;
|
||||
|
||||
function TPasTokenizer._is_readable(): boolean;
|
||||
function TAGPasTokenizer.IsReadable(): boolean;
|
||||
begin
|
||||
Exit(length(s[y])+1+Fix > x);
|
||||
end;
|
||||
|
||||
function TPasTokenizer._next_readable(): boolean;
|
||||
function TAGPasTokenizer.NextReadable(): boolean;
|
||||
begin
|
||||
inc(x);
|
||||
Result := _do_readable();
|
||||
Result := DoReadable();
|
||||
end;
|
||||
|
||||
procedure TPasTokenizer._skip_spaces();
|
||||
procedure TAGPasTokenizer.SkipSpaces();
|
||||
begin
|
||||
_do_readable();
|
||||
DoReadable();
|
||||
if not ended then
|
||||
begin
|
||||
while SPACES.Contains(s[y][x]) do
|
||||
_next_readable();
|
||||
NextReadable();
|
||||
end;
|
||||
end;
|
||||
|
||||
function TPasTokenizer._get_pos(): TTokenizerPos;
|
||||
function TAGPasTokenizer.GetPos(): TAGTokenizerPos;
|
||||
begin
|
||||
Result.x := x;
|
||||
Result.y := y;
|
||||
end;
|
||||
|
||||
procedure TPasTokenizer._set_pos(i0: integer; i1: integer);
|
||||
procedure TAGPasTokenizer.SetPos(pos:TAGTokenizerPos);
|
||||
begin
|
||||
y := i0;
|
||||
x := i1;
|
||||
ended := False;
|
||||
_do_readable();
|
||||
y:=Pos.x;
|
||||
x:=Pos.y;
|
||||
ended:=False;
|
||||
DoReadable();
|
||||
end;
|
||||
|
||||
function TPasTokenizer.get_next(): TToken;
|
||||
function TAGPasTokenizer.GetNext(): TAGToken;
|
||||
var
|
||||
l,i,last_i0:integer;
|
||||
ml,ss,line:string;
|
||||
now_sym,next_sym:char;
|
||||
f,{$IFDEF FPC}ff,{$ENDIF}str_changed:boolean;
|
||||
begin_pos:TTokenizerPos;
|
||||
f{$IFDEF FPC},ff{$ENDIF}:boolean;
|
||||
begin_pos:TAGTokenizerPos;
|
||||
begin
|
||||
ml := '';
|
||||
ss := '';
|
||||
f := True;
|
||||
str_changed := True;
|
||||
begin_pos := _get_pos();
|
||||
begin_pos := GetPos();
|
||||
while f and not ended do
|
||||
begin
|
||||
line := s[y];
|
||||
@@ -261,7 +261,7 @@ begin
|
||||
while line[x] <> #39 do
|
||||
begin
|
||||
inc(x);
|
||||
if not _is_readable() then
|
||||
if not IsReadable() then
|
||||
begin
|
||||
dec(x);
|
||||
break;
|
||||
@@ -278,7 +278,7 @@ begin
|
||||
begin
|
||||
ss := ss + line[x];
|
||||
inc(x);
|
||||
if not _is_readable() then
|
||||
if not IsReadable() then
|
||||
break;
|
||||
end;
|
||||
break;
|
||||
@@ -305,26 +305,26 @@ begin
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
_next_readable();
|
||||
NextReadable();
|
||||
end;
|
||||
{$IFDEF FPC}
|
||||
{$IFDEF FPC}
|
||||
Result.Text:=ss;
|
||||
Result.&begin:=begin_pos;
|
||||
Result.&end:=_get_pos;
|
||||
Result.&end:=GetPos;
|
||||
Result.ended:=ended;
|
||||
{$ELSE}
|
||||
Result := TToken.Create(ss, begin_pos, _get_pos, ended);
|
||||
Result := TAGToken.Create(ss, begin_pos, GetPos, ended);
|
||||
{$ENDIF}
|
||||
_skip_spaces;
|
||||
SkipSpaces;
|
||||
end;
|
||||
|
||||
constructor TPasTokenizer.Create(input:TStrings);
|
||||
constructor TAGPasTokenizer.Create(input:TStrings);
|
||||
begin
|
||||
s:=input;
|
||||
y:=0;
|
||||
x:=1+fix;
|
||||
ended:=False;
|
||||
_skip_spaces;
|
||||
SkipSpaces;
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
@@ -28,16 +28,16 @@ implementation
|
||||
procedure TMyTestObject.Test1;
|
||||
var
|
||||
input:TStrings;
|
||||
tokenizer:TPasTokenizer;
|
||||
token:TToken;
|
||||
tokenizer:TAGPasTokenizer;
|
||||
token:TAGToken;
|
||||
begin
|
||||
input:= TStringList.Create();
|
||||
input.LoadFromFile('..\..\MainTest.pas');
|
||||
tokenizer:=TPasTokenizer.Create(input);
|
||||
tokenizer:=TAGPasTokenizer.Create(input);
|
||||
token.ended:=False;
|
||||
while not token.ended do
|
||||
begin
|
||||
token:=tokenizer.get_next;
|
||||
token:=tokenizer.GetNext;
|
||||
TDUnitX.CurrentRunner.Log(TLogLevel.Information, token.Text);
|
||||
end;
|
||||
//sleep(10000);
|
||||
@@ -48,10 +48,10 @@ var
|
||||
s:string;
|
||||
begin
|
||||
s:=#39'kek'#39;
|
||||
if not is_string(s) then
|
||||
if not IsString(s) then
|
||||
raise Exception.Create('Is string error 1');
|
||||
s:='s:=12334;';
|
||||
if is_string(s) then
|
||||
if IsString(s) then
|
||||
raise Exception.Create('Is string error 2');
|
||||
end;
|
||||
|
||||
@@ -60,13 +60,13 @@ var
|
||||
s:string;
|
||||
begin
|
||||
s:='{ asdasdasd }';
|
||||
if not is_comment(s) then
|
||||
if not IsComment(s) then
|
||||
raise Exception.Create('Is comment error 1');
|
||||
s:='(* s:=12334;*)';
|
||||
if not is_comment(s) then
|
||||
if not IsComment(s) then
|
||||
raise Exception.Create('Is comment error 2');
|
||||
s:='// s:=12334;*)';
|
||||
if not is_comment(s) then
|
||||
if not IsComment(s) then
|
||||
raise Exception.Create('Is comment error 3');
|
||||
end;
|
||||
|
||||
|
||||
@@ -9,11 +9,7 @@
|
||||
<Title Value="fpcunitproject1"/>
|
||||
<ResourceType Value="res"/>
|
||||
<UseXPManifest Value="True"/>
|
||||
<Icon Value="0"/>
|
||||
</General>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
@@ -36,7 +32,7 @@
|
||||
<PackageName Value="FCL"/>
|
||||
</Item3>
|
||||
</RequiredPackages>
|
||||
<Units Count="2">
|
||||
<Units Count="3">
|
||||
<Unit0>
|
||||
<Filename Value="fpcunitproject1.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
@@ -46,6 +42,10 @@
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="TestCase1"/>
|
||||
</Unit1>
|
||||
<Unit2>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit2>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
<Filename Value="testcase1.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="TestCase1"/>
|
||||
<IsVisibleTab Value="True"/>
|
||||
<EditorIndex Value="1"/>
|
||||
<TopLine Value="7"/>
|
||||
<CursorPos X="68" Y="18"/>
|
||||
<TopLine Value="13"/>
|
||||
<CursorPos X="23" Y="31"/>
|
||||
<UsageCount Value="20"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit1>
|
||||
@@ -32,11 +33,11 @@
|
||||
</Unit2>
|
||||
<Unit3>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<IsVisibleTab Value="True"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<EditorIndex Value="2"/>
|
||||
<TopLine Value="278"/>
|
||||
<CursorPos X="9" Y="295"/>
|
||||
<UsageCount Value="10"/>
|
||||
<TopLine Value="299"/>
|
||||
<CursorPos X="20" Y="313"/>
|
||||
<UsageCount Value="20"/>
|
||||
<Loaded Value="True"/>
|
||||
</Unit3>
|
||||
<Unit4>
|
||||
@@ -51,123 +52,123 @@
|
||||
<JumpHistory Count="30" HistoryIndex="29">
|
||||
<Position1>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="211" TopLine="191"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
</Position1>
|
||||
<Position2>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="212" TopLine="191"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
</Position2>
|
||||
<Position3>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="312" TopLine="295"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
</Position3>
|
||||
<Position4>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
</Position4>
|
||||
<Position5>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
</Position5>
|
||||
<Position6>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
</Position6>
|
||||
<Position7>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
<Caret Line="295" TopLine="278"/>
|
||||
</Position7>
|
||||
<Position8>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
<Caret Line="300" TopLine="278"/>
|
||||
</Position8>
|
||||
<Position9>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
<Caret Line="312" TopLine="284"/>
|
||||
</Position9>
|
||||
<Position10>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="295" TopLine="278"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
</Position10>
|
||||
<Position11>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="300" TopLine="278"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
</Position11>
|
||||
<Position12>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="312" TopLine="284"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
</Position12>
|
||||
<Position13>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
</Position13>
|
||||
<Position14>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
</Position14>
|
||||
<Position15>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
</Position15>
|
||||
<Position16>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
<Caret Line="295" TopLine="278"/>
|
||||
</Position16>
|
||||
<Position17>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
<Caret Line="300" TopLine="278"/>
|
||||
</Position17>
|
||||
<Position18>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
<Caret Line="312" TopLine="284"/>
|
||||
</Position18>
|
||||
<Position19>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="295" TopLine="278"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
</Position19>
|
||||
<Position20>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="300" TopLine="278"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
</Position20>
|
||||
<Position21>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="312" TopLine="284"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
</Position21>
|
||||
<Position22>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="189" TopLine="171"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
</Position22>
|
||||
<Position23>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="190" TopLine="171"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
</Position23>
|
||||
<Position24>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="191" TopLine="171"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
</Position24>
|
||||
<Position25>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="192" TopLine="171"/>
|
||||
<Caret Line="295" Column="7" TopLine="278"/>
|
||||
</Position25>
|
||||
<Position26>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="193" TopLine="171"/>
|
||||
<Filename Value="testcase1.pas"/>
|
||||
<Caret Line="28" Column="11" TopLine="4"/>
|
||||
</Position26>
|
||||
<Position27>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="196" TopLine="171"/>
|
||||
<Filename Value="testcase1.pas"/>
|
||||
<Caret Line="29" Column="15" TopLine="4"/>
|
||||
</Position27>
|
||||
<Position28>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="295" Column="7" TopLine="278"/>
|
||||
<Caret Line="295" Column="9"/>
|
||||
</Position28>
|
||||
<Position29>
|
||||
<Filename Value="testcase1.pas"/>
|
||||
<Caret Line="28" Column="11" TopLine="4"/>
|
||||
<Filename Value="..\AG.PascalTokenizer.pas"/>
|
||||
<Caret Line="313" Column="20" TopLine="299"/>
|
||||
</Position29>
|
||||
<Position30>
|
||||
<Filename Value="testcase1.pas"/>
|
||||
<Caret Line="29" Column="15" TopLine="4"/>
|
||||
<Caret Line="23" Column="12" TopLine="8"/>
|
||||
</Position30>
|
||||
</JumpHistory>
|
||||
</ProjectSession>
|
||||
|
||||
@@ -19,16 +19,16 @@ implementation
|
||||
procedure TTestCase1.TestHookUp;
|
||||
var
|
||||
input:TStrings;
|
||||
tokenizer:TPasTokenizer;
|
||||
token:TToken;
|
||||
tokenizer:TAGPasTokenizer;
|
||||
token:TAGToken;
|
||||
begin
|
||||
input:= TStringList.Create();
|
||||
input.LoadFromFile('testcase1.pas');
|
||||
tokenizer:=TPasTokenizer.Create(input);
|
||||
tokenizer:=TAGPasTokenizer.Create(input);
|
||||
token.ended:=False;
|
||||
while not token.ended do
|
||||
begin
|
||||
token:=tokenizer.get_next;
|
||||
token:=tokenizer.GetNext;
|
||||
TestRunner.MemoLog.Append(token.Text);
|
||||
end;
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user