| nbsp; SetPointer(FOutBuff, fBuffPos-1) ;
end; { ConvertReadStream }
The code for ConvertReadStream is now much smaller, and also easier to understand. We can then take all the code that used to be in ConvertReadStream that did the tokenizing and create a new subroutine - the GetToken function that just does the recognizing and labelling of the individual tokens. In the process we also loose a huge number of repeated lines of code, as well as a number of sub-routines such as HandleBorCom and HandleString.
// // My Get Token routine //
function TPasConversion.GetToken(Run: PChar; var aTokenState: TTokenState; var aTokenStr: string):PChar; begin
aTokenState := tsUnknown; aTokenStr := ''''''''; TokenPtr := Run; // Mark were we started
Case Run^ of
#13 :
begin
aTokenState := tsCRLF; inc(Run, 2);
end;
#1..#9, #11, #12, #14..#32:
begin
while Run^ in [#1..#9, #11, #12, #14..#32] do inc(Run); aTokenState:= tsSpace;
end;
''''A''''..''''Z'''', ''''a''''..''''z'''', ''''_'''':
begin
aTokenState:= tsIdentifier; inc(Run); while Run^ in [''''A''''..''''Z'''', ''''a''''..''''z'''', ''''0''''..''''9'''', ''''_''''] do inc(Run); TokenLen:= Run - TokenPtr; SetString(aTokenStr, TokenPtr, TokenLen);
if IsKeyWord(aTokenStr) then begin
if IsDirective(aTokenStr) then aTokenState:= tsDirective else aTokenState:= tsKeyWord;
end;
end;
''''0''''..''''9'''':
begin
inc(Run); aTokenState:= tsNumber; while Run^ in [''''0''''..''''9'''', ''''.'''', ''''e'''', ''''E''''] do inc(Run);
end;
''''{'''':
begin
FComment := csBor; aTokenState := tsComment; while not ((Run^ = ''''}'''') or (Run^ = #0)) do inc(Run); inc(Run);
end;
''''!'''',''''"'''', ''''%'''', ''''&'''', ''''(''''..''''/'''', '''':''''..''''@'''', ''''[''''..''''^'''', ''''`'''', ''''~'''' :
begin
aTokenState:= tsUnknown; while Run^ in [''''!'''',''''"'''', ''''%'''', ''''&'''', ''''(''''..''''/'''', '''':''''..''''@'''', ''''[''''..''''^'''', ''''`'''', ''''~''''] do begin
Case Run^ of
''''/'''': if (Run + 1)^ = ''''/'''' then begin
if (aTokenState = tsUnknown) then begin
while (Run^ <> #13) and (Run^ <> #0) do inc(Run); FComment:= csSlashes; aTokenState := tsComment; break;
end
else
begin
aTokenState := tsSymbol; break;
end;
end;
''''('''': if (Run + 1)^ = ''''*'''' then begin
if (aTokenState = tsUnknown) then begin
while (Run^ <> #0) and not ( (Run^ = '''')'''') and ((Run - 1)^ = ''''*'''') ) do inc(Run);
FComment:= csAnsi; aTokenState := tsComment;
inc(Run); break;
end
else
begin
aTokenState := tsSymbol; break;
end;
end;
end;
aTokenState := tsSymbol; inc(Run);
end;
if aTokenState = tsUnknown then aTokenState := tsSymbol;
end;
#39:
begin
aTokenState:= tsString; FComment:= csNo;
repeat
Case Run^ of #0, #10, #13: raise exception.Create(''''Invalid string''''); end;
inc(Run);
until Run^ = #39;
inc(Run);
end;
''''#'''':
begin
aTokenState:= tsString; while Run^ in [''''#'''', ''''0''''..''''9''''] do inc(Run);
end;
''''$'''':
begin
FTokenState:= tsNumber; while Run^ in [''''$'''',''''0''''..''''9'''', ''''A''''..''''F'''', ''''a''''..''''f''''] do inc(Run);
end;
else
if Run^ <> #0 then inc(Run);
end;
上一页 [1] [2] [3] 下一页 |