codetools: h2p: implemented parsing defines

git-svn-id: trunk@14586 -
This commit is contained in:
mattias 2008-03-19 14:39:07 +00:00
parent baebd44d83
commit bcc1df0450
2 changed files with 50 additions and 2 deletions

View File

@ -230,6 +230,8 @@ type
function ExtractDirectiveAction(DirectiveNode: TCodeTreeNode): string;
function ExtractDirectiveFirstAtom(DirectiveNode: TCodeTreeNode): string;
function ExtractDirectiveParams(DirectiveNode: TCodeTreeNode): string;
function ExtractDefine(DefineNode: TCodeTreeNode;
out MacroName, MacroParamList, MacroValue: string): boolean;
procedure Replace(FromPos, ToPos: integer; const NewSrc: string);
@ -1826,6 +1828,44 @@ begin
Result:=ExtractCode(AtomStart,DirectiveNode.EndPos);
end;
function TCCodeParserTool.ExtractDefine(DefineNode: TCodeTreeNode; out
MacroName, MacroParamList, MacroValue: string): boolean;
var
StartPos: LongInt;
EndPos: LongInt;
begin
Result:=false;
MacroName:='';
MacroParamList:='';
MacroValue:='';
MoveCursorToPos(DefineNode.StartPos+1);
// read action
ReadRawNextAtom;
if not AtomIs('define') then exit;
// read first atom
ReadRawNextAtom;
MacroName:=GetAtom;
StartPos:=SrcPos;
// read param list
if (SrcPos<=SrcLen) and (Src[SrcPos]='(') then begin
StartPos:=SrcPos;
AtomStart:=SrcPos;
SrcPos:=AtomStart+1;
if not ReadTilBracketClose(false) then exit;
EndPos:=SrcPos;
MacroParamList:=ExtractCode(StartPos,EndPos);
StartPos:=EndPos;
end;
// read value
while (StartPos<=SrcLen) and (IsSpaceChar[Src[StartPos]]) do
inc(StartPos);
EndPos:=DefineNode.EndPos;
while (EndPos>StartPos) and (IsSpaceChar[Src[EndPos-1]]) do
dec(EndPos);
MacroValue:=copy(Src,StartPos,EndPos-StartPos);
Result:=true;
end;
function TCCodeParserTool.GetAtom: string;
begin
Result:=copy(Src,AtomStart,SrcPos-AtomStart);

View File

@ -541,6 +541,7 @@ var
ErrorMsg: string;
StartPos: LongInt;
EndPos: LongInt;
MacroName,MacroParamList,MacroValue: string;
begin
Directive:=CTool.ExtractDirectiveAction(CNode);
if Directive='include' then begin
@ -549,8 +550,15 @@ begin
if icspInclude in IgnoreCParts then
exit;
end else if Directive='define' then begin
// #define FMAC(a,b) a here, then b
// #define NONFMAC some text here
// #define macrofunction(a,b) a here, then b
// #define simplemacro some text here
if CTool.ExtractDefine(CNode,MacroName,MacroParamList,MacroValue)
then begin
H2PNode:=CreateH2PNode('$'+Directive,'#'+Directive,CNode,ctnNone,
MacroName,ParentNode,false);
DebugLn(['TH2PasTool.ConvertDirective added: ',H2PNode.DescAsString]);
exit;
end;
end else if (Directive='undef') or (Directive='ifdef')
or (Directive='ifndef') then begin
// #undef NAME