lexer, option to exclude comments + more reliable version all/none inverter

This commit is contained in:
Basile Burg 2016-04-16 22:10:52 +02:00
parent 264f1afbe4
commit 5d3d8fe0f0
2 changed files with 22 additions and 17 deletions

View File

@ -70,6 +70,9 @@ type
Data: string;
end;
TLexOption = (lxoNoComments);
TLexOptions = set of TLexOption;
TLexFoundEvent = procedure(const aToken: PLexToken; out doStop: boolean) of Object;
(**
@ -129,7 +132,7 @@ operator enumerator(list: TLexErrorList): TLexErrorEnumerator;
(**
* Lexes text and fills list with the TLexToken found.
*)
procedure lex(const text: string; list: TLexTokenList; clbck: TLexFoundEvent = nil);
procedure lex(const text: string; list: TLexTokenList; clbck: TLexFoundEvent = nil; Options: TLexOptions = []);
(**
* Outputs the module name from a tokenized D source.
@ -265,7 +268,7 @@ begin
end;
procedure lex(const text: string; list: TLexTokenList; clbck: TLexFoundEvent = nil);
procedure lex(const text: string; list: TLexTokenList; clbck: TLexFoundEvent = nil; Options: TLexOptions = []);
var
reader: TReaderHead;
identifier: string;
@ -273,11 +276,14 @@ var
rstring: boolean;
decSet: boolean;
expSet: boolean;
noComment: boolean;
procedure addToken(aTk: TLexTokenKind);
var
ptk: PLexToken;
begin
if (aTk = ltkComment) and (noComment) then
exit;
ptk := new(PLexToken);
ptk^.kind := aTk;
ptk^.position.X := reader.SavedColumn;
@ -304,6 +310,8 @@ begin
if text = '' then exit;
noComment := lxoNoComments in Options;
reader.Create(@text[1], Point(0, 0));
while (True) do
begin
@ -331,7 +339,8 @@ begin
exit;
while (reader.head^ <> #10) do
begin
identifier += reader.head^;
if not noComment then
identifier += reader.head^;
reader.Next;
if isOutOfBound then
exit;
@ -357,7 +366,8 @@ begin
reader.Next;
while (reader.head^ <> '/') or ((reader.head - 1)^ <> '*') do
begin
identifier += reader.head^;
if not noComment then
identifier += reader.head^;
reader.Next;
if isOutOfBound then
exit;
@ -387,6 +397,8 @@ begin
begin
if isOutOfBound then
exit;
if not noComment then
identifier += reader.head^;
if ((reader.head-1)^ = '/') and (reader.head^ = '+') then
begin
nestedCom += 1;

View File

@ -946,12 +946,12 @@ procedure TCESynMemo.invertVersionAllNone;
var
i: integer;
c: char;
tok, tok1, tok2, tok3: PLexToken;
tok, tok1, tok2: PLexToken;
pt, cp, st, nd: TPoint;
sel: boolean;
begin
fLexToks.Clear;
lex(lines.Text, fLexToks);
lex(lines.Text, fLexToks, nil, [lxoNoComments]);
cp := CaretXY;
if SelAvail then
begin
@ -975,18 +975,11 @@ begin
or (tok^.kind <> ltkIdentifier) or (i < 2) then
continue;
//
if i = 2 then
tok1 := nil
else
tok1 := PLexToken(fLexToks[i-3]);
tok2 := PLexToken(fLexToks[i-2]);
tok3 := PLexToken(fLexToks[i-1]);
tok1 := PLexToken(fLexToks[i-2]);
tok2 := PLexToken(fLexToks[i-1]);
//
if ((tok2^.kind = ltkKeyword) and (tok2^.data = 'version')
and (tok3^.kind = ltkSymbol) and (tok3^.data = '('))
or ((tok1 <> nil) and (tok1^.kind = ltkKeyword) and (tok1^.data = 'version')
and (tok3^.kind = ltkComment) and
(tok2^.kind = ltkSymbol) and (tok2^.data = '(')) then
if ((tok1^.kind = ltkKeyword) and (tok1^.data = 'version')
and (tok2^.kind = ltkSymbol) and (tok2^.data = '(')) then
begin
pt := tok^.position;
pt.X += 1;