codetools: implemented parsing c enum block name

git-svn-id: trunk@14541 -
This commit is contained in:
mattias 2008-03-15 21:39:50 +00:00
parent b68a8161c1
commit af9f3ff803
2 changed files with 65 additions and 38 deletions

View File

@ -52,15 +52,16 @@ const
ccnRoot = 1+ccnBase; ccnRoot = 1+ccnBase;
ccnDirective = 2+ccnBase;// e.g. "#define a" ,can be multiple lines, without line end ccnDirective = 2+ccnBase;// e.g. "#define a" ,can be multiple lines, without line end
ccnExtern = 3+ccnBase;// e.g. extern "C" {} ccnExtern = 3+ccnBase;// e.g. extern "C" {}
ccnEnums = 4+ccnBase;// e.g. enum {}; ccnEnumBlock = 4+ccnBase;// e.g. enum {};
ccnEnum = 5+ccnBase;// e.g. name = value; ccnEnumBlockName = 5+ccnBase;// e.g. enum {};
ccnConstant = 6+ccnBase;// e.g. 1 ccnEnumID = 6+ccnBase;// e.g. name = value;
ccnTypedef = 7+ccnBase;// e.g. typedef int TInt; ccnConstant = 7+ccnBase;// e.g. 1
ccnStruct = 8+ccnBase;// e.g. struct{}; ccnTypedef = 8+ccnBase;// e.g. typedef int TInt;
ccnVariable = 9+ccnBase;// e.g. int i ccnStruct = 9+ccnBase;// e.g. struct{};
ccnVariableName = 10+ccnBase;// e.g. i ccnVariable = 10+ccnBase;// e.g. int i
ccnFuncParamList = 11+ccnBase;// e.g. () ccnVariableName = 11+ccnBase;// e.g. i
ccnStatementBlock = 12+ccnBase;// e.g. {} ccnFuncParamList = 12+ccnBase;// e.g. ()
ccnStatementBlock = 13+ccnBase;// e.g. {}
type type
TCCodeParserTool = class; TCCodeParserTool = class;
@ -168,9 +169,10 @@ type
function ExtractCode(StartPos, EndPos: integer; function ExtractCode(StartPos, EndPos: integer;
WithDirectives: boolean = false): string;// extract code without comments WithDirectives: boolean = false): string;// extract code without comments
function ExtractVariableName(Node: TCodeTreeNode): string; function ExtractVariableName(VarNode: TCodeTreeNode): string;
function ExtractVariableType(Node: TCodeTreeNode; function ExtractVariableType(VarNode: TCodeTreeNode;
WithDirectives: boolean = false): string; WithDirectives: boolean = false): string;
function ExtractEnumBlockName(EnumBlockNode: TCodeTreeNode): string;
procedure Replace(FromPos, ToPos: integer; const NewSrc: string); procedure Replace(FromPos, ToPos: integer; const NewSrc: string);
@ -196,18 +198,19 @@ var
function CCNodeDescAsString(Desc: TCCodeNodeDesc): string; function CCNodeDescAsString(Desc: TCCodeNodeDesc): string;
begin begin
case Desc of case Desc of
ccnNone : Result:='None'; ccnNone : Result:='None';
ccnRoot : Result:='Root'; ccnRoot : Result:='Root';
ccnDirective: Result:='Directive'; ccnDirective : Result:='Directive';
ccnExtern : Result:='extern block'; ccnExtern : Result:='extern block';
ccnEnums : Result:='enums'; ccnEnumBlock : Result:='enum block';
ccnEnum : Result:='enum'; ccnEnumBlockName : Result:='enum block name';
ccnConstant : Result:='constant'; ccnEnumID : Result:='enum ID';
ccnTypedef : Result:='typedef'; ccnConstant : Result:='constant';
ccnStruct : Result:='struct'; ccnTypedef : Result:='typedef';
ccnVariable : Result:='variable'; ccnStruct : Result:='struct';
ccnVariableName: Result:='variable name'; ccnVariable : Result:='variable';
ccnFuncParamList: Result:='function param list'; ccnVariableName : Result:='variable name';
ccnFuncParamList : Result:='function param list';
ccnStatementBlock: Result:='statement block'; ccnStatementBlock: Result:='statement block';
else Result:='?'; else Result:='?';
end; end;
@ -324,11 +327,14 @@ procedure TCCodeParserTool.ReadEnum;
*) *)
begin begin
CreateChildNode(ccnEnums); CreateChildNode(ccnEnumBlock);
ReadNextAtom; ReadNextAtom;
// read optional name // read optional name
if AtomIsIdentifier then if AtomIsIdentifier then begin
CreateChildNode(ccnEnumBlockName);
EndChildNode;
ReadNextAtom; ReadNextAtom;
end;
if not AtomIsChar('{') then if not AtomIsChar('{') then
RaiseExpectedButAtomFound('{'); RaiseExpectedButAtomFound('{');
// read enums. Examples // read enums. Examples
@ -338,7 +344,7 @@ begin
repeat repeat
if AtomIsIdentifier then begin if AtomIsIdentifier then begin
// read enum // read enum
CreateChildNode(ccnEnum); CreateChildNode(ccnEnumID);
CurNode.EndPos:=SrcPos; CurNode.EndPos:=SrcPos;
ReadNextAtom; ReadNextAtom;
if AtomIsChar('=') then begin if AtomIsChar('=') then begin
@ -1139,27 +1145,27 @@ begin
Result:=s; Result:=s;
end; end;
function TCCodeParserTool.ExtractVariableName(Node: TCodeTreeNode): string; function TCCodeParserTool.ExtractVariableName(VarNode: TCodeTreeNode): string;
var var
NameNode: TCodeTreeNode; NameNode: TCodeTreeNode;
begin begin
NameNode:=Node.FirstChild; NameNode:=VarNode.FirstChild;
if (NameNode=nil) or (NameNode.Desc<>ccnVariableName) then if (NameNode=nil) or (NameNode.Desc<>ccnVariableName) then
Result:='' Result:=''
else else
Result:=copy(Src,NameNode.StartPos,NameNode.EndPos-NameNode.StartPos); Result:=copy(Src,NameNode.StartPos,NameNode.EndPos-NameNode.StartPos);
end; end;
function TCCodeParserTool.ExtractVariableType(Node: TCodeTreeNode; function TCCodeParserTool.ExtractVariableType(VarNode: TCodeTreeNode;
WithDirectives: boolean): string; WithDirectives: boolean): string;
var var
NameNode: TCodeTreeNode; NameNode: TCodeTreeNode;
begin begin
NameNode:=Node.FirstChild; NameNode:=VarNode.FirstChild;
if (NameNode=nil) or (NameNode.Desc<>ccnVariableName) then if (NameNode=nil) or (NameNode.Desc<>ccnVariableName) then
Result:='' Result:=''
else begin else begin
Result:=ExtractCode(Node.StartPos,NameNode.StartPos,WithDirectives); Result:=ExtractCode(VarNode.StartPos,NameNode.StartPos,WithDirectives);
if (NameNode.NextBrother<>nil) if (NameNode.NextBrother<>nil)
and (NameNode.NextBrother.Desc=ccnFuncParamList) then begin and (NameNode.NextBrother.Desc=ccnFuncParamList) then begin
// this is a function. The name is in between. // this is a function. The name is in between.
@ -1173,12 +1179,26 @@ begin
Result:=Result+ExtractCode(NameNode.EndPos,NameNode.NextBrother.StartPos, Result:=Result+ExtractCode(NameNode.EndPos,NameNode.NextBrother.StartPos,
WithDirectives); WithDirectives);
end else begin end else begin
Result:=Result+ExtractCode(NameNode.EndPos,Node.EndPos, Result:=Result+ExtractCode(NameNode.EndPos,VarNode.EndPos,
WithDirectives); WithDirectives);
end; end;
end; end;
end; end;
function TCCodeParserTool.ExtractEnumBlockName(EnumBlockNode: TCodeTreeNode
): string;
var
NameNode: TCodeTreeNode;
begin
if (EnumBlockNode.FirstChild<>nil)
and (EnumBlockNode.FirstChild.Desc=ccnEnumBlockName) then begin
NameNode:=EnumBlockNode.FirstChild;
Result:=copy(Src,NameNode.StartPos,NameNode.EndPos-NameNode.StartPos);
end else begin
Result:='';
end;
end;
function TCCodeParserTool.GetAtom: string; function TCCodeParserTool.GetAtom: string;
begin begin
Result:=copy(Src,AtomStart,SrcPos-AtomStart); Result:=copy(Src,AtomStart,SrcPos-AtomStart);

View File

@ -220,8 +220,8 @@ end;
procedure TH2PasTool.BuildH2PTree; procedure TH2PasTool.BuildH2PTree;
var var
CNode: TCodeTreeNode; CNode: TCodeTreeNode;
VarName: String; CurName: String;
VarType: String; CurType: String;
SimpleType: String; SimpleType: String;
begin begin
Tree.Clear; Tree.Clear;
@ -230,12 +230,19 @@ begin
case CNode.Desc of case CNode.Desc of
ccnVariable: ccnVariable:
begin begin
VarName:=CTool.ExtractVariableName(CNode); CurName:=CTool.ExtractVariableName(CNode);
VarType:=CTool.ExtractVariableType(CNode); CurType:=CTool.ExtractVariableType(CNode);
SimpleType:=GetSimplePascalTypeOfCVar(CNode); SimpleType:=GetSimplePascalTypeOfCVar(CNode);
DebugLn(['TH2PasTool.BuildH2PTree Variable Name="',VarName,'" Type="',VarType,'" SimpleType=',SimpleType]); DebugLn(['TH2PasTool.BuildH2PTree Variable Name="',CurName,'" Type="',CurType,'" SimpleType=',SimpleType]);
if SimpleType='' then begin
// this variable has a complex type
end;
end;
ccnEnumBlock:
begin
CurName:=CTool.ExtractEnumBlockName(CNode);
DebugLn(['TH2PasTool.BuildH2PTree EnumBlock name="',CurName,'"']);
end; end;
end; end;
CNode:=CNode.Next; CNode:=CNode.Next;
end; end;