fix lexer bug + add temp method to domp tokens to file

This commit is contained in:
Basile Burg 2016-03-31 09:03:26 +02:00
parent 0cbb7f37a7
commit 6ec8fe614f
3 changed files with 83 additions and 15 deletions

View File

@ -93,8 +93,15 @@ type
const const
LexTokenKindString: array[TLexTokenKind] of string = LexTokenKindString: array[TLexTokenKind] of string =
('Illegal', 'Character', 'Comment', 'Identifier', 'Keyword', ('Illegal ',
'Number', 'Operator', 'String', 'Symbol'); 'Character ',
'Comment ',
'Identifier',
'Keyword ',
'Number ',
'Operator ',
'String ',
'Symbol ');
type type
@ -120,6 +127,7 @@ type
public public
procedure Clear; procedure Clear;
procedure addToken(aValue: PLexToken); procedure addToken(aValue: PLexToken);
procedure saveToFile(fname: string);
property token[index: integer]: PLexToken read getToken; default; property token[index: integer]: PLexToken read getToken; default;
end; end;
@ -328,6 +336,25 @@ begin
add(Pointer(aValue)); add(Pointer(aValue));
end; end;
procedure TLexTokenList.saveToFile(fname: string);
var
tok: PLexToken;
i: integer;
begin
with TStringList.Create do
try
for i:= 0 to self.count-1 do
begin
tok := getToken(i);
add(format('line %.5d - col %.3d: (%s): %s', [tok^.position.Y, tok^.position.X,
LexTokenKindString[tok^.kind], tok^.Data]));
end;
finally
SaveToFile(fname);
free;
end;
end;
function TLexTokenEnumerator.GetCurrent: PLexToken; function TLexTokenEnumerator.GetCurrent: PLexToken;
begin begin
exit(fList.token[fIndex]); exit(fList.token[fIndex]);
@ -484,6 +511,7 @@ begin
end; end;
// string 1, note: same escape error as in SynD2Syn // string 1, note: same escape error as in SynD2Syn
rstring := false;
if (reader.head^ in ['r', 'x']) then if (reader.head^ in ['r', 'x']) then
begin begin
rstring := reader.head^ = 'r'; rstring := reader.head^ = 'r';
@ -500,6 +528,7 @@ begin
if isStringPostfix(reader.Next^) then if isStringPostfix(reader.Next^) then
reader.Next; reader.Next;
addToken(ltkString); addToken(ltkString);
rstring := false;
if callBackDoStop then if callBackDoStop then
exit; exit;
continue; continue;
@ -518,20 +547,30 @@ begin
reader.Next; reader.Next;
if isOutOfBound then if isOutOfBound then
exit; exit;
end continue;
else if (reader.head^ = '"') then end;
if (reader.head^ = '"') then
begin
reader.Next;
if isOutOfBound then
exit;
break; break;
end
else
begin
identifier += reader.head^; identifier += reader.head^;
reader.Next; reader.Next;
if isOutOfBound then if isOutOfBound then
exit; exit;
end; end;
if isStringPostfix(reader.Next^) then end;
if isStringPostfix(reader.head^) then
begin begin
identifier += reader.head^; identifier += reader.head^;
reader.Next; reader.Next;
end; end;
addToken(ltkString); addToken(ltkString);
rstring := false;
if callBackDoStop then if callBackDoStop then
exit; exit;
continue; continue;
@ -585,20 +624,21 @@ begin
begin begin
if reader.head^ = '\' then if reader.head^ = '\' then
begin begin
identifier += reader.head^;
reader.Next; reader.Next;
identifier += reader.head^;
if isOutOfBound then if isOutOfBound then
exit; exit;
if reader.head^ = #10 then
exit;
reader.Next; reader.Next;
if isOutOfBound then if isOutOfBound then
exit; exit;
end; end;
if reader.head^ = #39 then
break;
reader.Next;
if isOutOfBound then if isOutOfBound then
exit; exit;
if reader.head^ = #39 then
break;
identifier += reader.head^;
reader.Next;
end; end;
reader.Next; reader.Next;
if isOutOfBound then if isOutOfBound then

View File

@ -120,5 +120,12 @@ inherited CEEditorWidget: TCEEditorWidget
Caption = 'Show ddoc' Caption = 'Show ddoc'
OnClick = mnuedDdocClick OnClick = mnuedDdocClick
end end
object MenuItem3: TMenuItem
Caption = '-'
end
object MenuItem5: TMenuItem
Caption = 'Save lexical tokens to file...'
OnClick = MenuItem5Click
end
end end
end end

View File

@ -6,7 +6,7 @@ interface
uses uses
Classes, SysUtils, FileUtil, Forms, Controls, lcltype, Graphics, SynEditKeyCmds, Classes, SysUtils, FileUtil, Forms, Controls, lcltype, Graphics, SynEditKeyCmds,
ComCtrls, SynEditHighlighter, ExtCtrls, Menus, SynMacroRecorder, ComCtrls, SynEditHighlighter, ExtCtrls, Menus, SynMacroRecorder, dialogs,
SynPluginSyncroEdit, SynEdit, SynHighlighterMulti, ce_dialogs, SynPluginSyncroEdit, SynEdit, SynHighlighterMulti, ce_dialogs,
ce_widget, ce_interfaces, ce_synmemo, ce_dlang, ce_common, ce_dcd, ce_observer, ce_widget, ce_interfaces, ce_synmemo, ce_dlang, ce_common, ce_dcd, ce_observer,
ce_sharedres, ce_controls; ce_sharedres, ce_controls;
@ -18,6 +18,8 @@ type
TCEEditorWidget = class(TCEWidget, ICEMultiDocObserver, ICEMultiDocHandler, ICEProjectObserver) TCEEditorWidget = class(TCEWidget, ICEMultiDocObserver, ICEMultiDocHandler, ICEProjectObserver)
MenuItem1: TMenuItem; MenuItem1: TMenuItem;
MenuItem2: TMenuItem; MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem5: TMenuItem;
mnEdInvAllNone: TMenuItem; mnEdInvAllNone: TMenuItem;
mneEdComm: TMenuItem; mneEdComm: TMenuItem;
mnuedPrev: TMenuItem; mnuedPrev: TMenuItem;
@ -35,6 +37,7 @@ type
macRecorder: TSynMacroRecorder; macRecorder: TSynMacroRecorder;
editorStatus: TStatusBar; editorStatus: TStatusBar;
mnuEditor: TPopupMenu; mnuEditor: TPopupMenu;
procedure MenuItem5Click(Sender: TObject);
procedure mnEdInvAllNoneClick(Sender: TObject); procedure mnEdInvAllNoneClick(Sender: TObject);
procedure mneEdCommClick(Sender: TObject); procedure mneEdCommClick(Sender: TObject);
procedure mnuedPrevClick(Sender: TObject); procedure mnuedPrevClick(Sender: TObject);
@ -595,6 +598,24 @@ begin
fDoc.CommandProcessor(ecSwapVersionAllNone, '', nil); fDoc.CommandProcessor(ecSwapVersionAllNone, '', nil);
end; end;
procedure TCEEditorWidget.MenuItem5Click(Sender: TObject);
begin
if fDoc.isNil then
exit;
with TSaveDialog.Create(nil) do
try
if execute then
begin
fTokList.Clear;
lex(fDoc.Text, fTokList, nil);
fTokList.saveToFile(FileName);
fTokList.Clear;
end;
finally
free;
end;
end;
procedure TCEEditorWidget.mnuedCutClick(Sender: TObject); procedure TCEEditorWidget.mnuedCutClick(Sender: TObject);
begin begin
if fDoc.isNotNil then if fDoc.isNotNil then