added a callback to lex()

This commit is contained in:
Basile Burg 2014-11-30 02:16:34 +01:00
parent 6411f4fd65
commit b9d13630c8
1 changed files with 30 additions and 2 deletions

View File

@ -107,6 +107,8 @@ type
data: string; data: string;
end; end;
TLexFoundEvent = procedure(const aToken: PLexToken; out doStop: boolean) of Object;
(***************************************************************************** (*****************************************************************************
* List of lexer tokens * List of lexer tokens
*) *)
@ -162,7 +164,7 @@ type
(***************************************************************************** (*****************************************************************************
* Lexes aText and fills aList with the TLexToken found. * Lexes aText and fills aList with the TLexToken found.
*) *)
procedure lex(const aText: string; const aList: TLexTokenList); procedure lex(const aText: string; aList: TLexTokenList; aCallBack: TLexFoundEvent = nil);
(***************************************************************************** (*****************************************************************************
* Detects various syntactic errors in a TLexTokenList * Detects various syntactic errors in a TLexTokenList
@ -326,7 +328,7 @@ begin
end; end;
{$BOOLEVAL ON} {$BOOLEVAL ON}
procedure lex(const aText: string; const aList: TLexTokenList); procedure lex(const aText: string; aList: TLexTokenList; aCallBack: TLexFoundEvent = nil);
var var
reader: TReaderHead; reader: TReaderHead;
identifier: string; identifier: string;
@ -347,6 +349,13 @@ var
aList.Add(ptk); aList.Add(ptk);
end; end;
function callBackDoStop: boolean;
begin
result := false;
if aCallBack <> nil then
aCallBack(PLexToken(aList.Items[aList.Count-1]), result);
end;
begin begin
reader.create(@aText[1], Point(0,0)); reader.create(@aText[1], Point(0,0));
@ -379,6 +388,7 @@ begin
end; end;
reader.next; reader.next;
addToken(ltkComment); addToken(ltkComment);
if callBackDoStop then exit;
continue; continue;
end end
else else
@ -395,6 +405,7 @@ begin
if isOutOfBound then exit; if isOutOfBound then exit;
reader.next; reader.next;
addToken(ltkComment); addToken(ltkComment);
if callBackDoStop then exit;
continue; continue;
end end
else else
@ -411,6 +422,7 @@ begin
if isOutOfBound then exit; if isOutOfBound then exit;
reader.next; reader.next;
addToken(ltkComment); addToken(ltkComment);
if callBackDoStop then exit;
continue; continue;
end end
else else
@ -431,6 +443,7 @@ begin
begin begin
reader.next; reader.next;
addToken(ltkString); addToken(ltkString);
if callBackDoStop then exit;
continue; continue;
end; end;
while (true) do while (true) do
@ -453,6 +466,7 @@ begin
if isStringPostfix(reader.next^) then if isStringPostfix(reader.next^) then
reader.next; reader.next;
addToken(ltkString); addToken(ltkString);
if callBackDoStop then exit;
continue; continue;
end; end;
@ -471,6 +485,7 @@ begin
reader.next; reader.next;
if isOutOfBound then exit; if isOutOfBound then exit;
addToken(ltkString); addToken(ltkString);
if callBackDoStop then exit;
continue; continue;
end; end;
@ -487,6 +502,7 @@ begin
end; end;
reader.next; reader.next;
addToken(ltkString); addToken(ltkString);
if callBackDoStop then exit;
continue; continue;
end else reader.previous; end else reader.previous;
@ -499,6 +515,7 @@ begin
begin begin
reader.next; reader.next;
addToken(ltkString); addToken(ltkString);
if callBackDoStop then exit;
continue; continue;
end; end;
while (true) do while (true) do
@ -520,6 +537,7 @@ begin
end; end;
reader.next; reader.next;
addToken(ltkChar); addToken(ltkChar);
if callBackDoStop then exit;
continue; continue;
end; end;
@ -562,6 +580,7 @@ begin
identifier += reader.head^; identifier += reader.head^;
end; end;
addToken(ltkNumber); addToken(ltkNumber);
if callBackDoStop then exit;
continue; continue;
end end
else reader.previous; else reader.previous;
@ -574,6 +593,7 @@ begin
identifier += reader.head^; identifier += reader.head^;
end; end;
addToken(ltkNumber); addToken(ltkNumber);
if callBackDoStop then exit;
continue; continue;
end end
else reader.previous; else reader.previous;
@ -586,6 +606,7 @@ begin
identifier += reader.head^; identifier += reader.head^;
end; end;
addToken(ltkNumber); addToken(ltkNumber);
if callBackDoStop then exit;
continue; continue;
end end
else reader.previous; else reader.previous;
@ -613,6 +634,7 @@ begin
identifier += reader.head^; identifier += reader.head^;
end; end;
addToken(ltkNumber); addToken(ltkNumber);
if callBackDoStop then exit;
continue; continue;
end; end;
@ -622,6 +644,7 @@ begin
identifier += reader.head^; identifier += reader.head^;
reader.next; reader.next;
addToken(ltkSymbol); addToken(ltkSymbol);
if callBackDoStop then exit;
if isOutOfBound then exit; if isOutOfBound then exit;
continue; continue;
end; end;
@ -641,6 +664,7 @@ begin
isOperator4(identifier) then isOperator4(identifier) then
begin begin
addToken(ltkOperator); addToken(ltkOperator);
if callBackDoStop then exit;
continue; continue;
end; end;
end; end;
@ -649,6 +673,7 @@ begin
isOperator3(identifier) then isOperator3(identifier) then
begin begin
addToken(ltkOperator); addToken(ltkOperator);
if callBackDoStop then exit;
continue; continue;
end; end;
end; end;
@ -657,6 +682,7 @@ begin
isOperator2(identifier) then isOperator2(identifier) then
begin begin
addToken(ltkOperator); addToken(ltkOperator);
if callBackDoStop then exit;
continue; continue;
end; end;
end; end;
@ -665,6 +691,7 @@ begin
then then
begin begin
addToken(ltkOperator); addToken(ltkOperator);
if callBackDoStop then exit;
continue; continue;
end; end;
end; end;
@ -684,6 +711,7 @@ begin
addToken(ltkKeyword) addToken(ltkKeyword)
else else
addToken(ltkIdentifier); addToken(ltkIdentifier);
if callBackDoStop then exit;
continue; continue;
end; end;