codetools: implemented parsing typedef functions

git-svn-id: trunk@14386 -
This commit is contained in:
mattias 2008-03-03 16:49:19 +00:00
parent 92375726b6
commit 735da60bb7
2 changed files with 59 additions and 36 deletions

View File

@ -93,7 +93,7 @@ type
procedure CloseNodes; procedure CloseNodes;
procedure ReadEnum; procedure ReadEnum;
procedure ReadStruct(NeedIdentifier: boolean); procedure ReadStruct;
procedure ReadConstant; procedure ReadConstant;
procedure ReadVariable; procedure ReadVariable;
@ -353,26 +353,30 @@ begin
EndChildNode; EndChildNode;
end; end;
procedure TCCodeParserTool.ReadStruct(NeedIdentifier: boolean); procedure TCCodeParserTool.ReadStruct;
(* Example for NeedIdentifier=false: (* Example for NeedIdentifier=false:
typedef struct { As typedef:
uint8_t b[6]; // implicit type typedef struct {
} __attribute__((packed)) bdaddr_t; uint8_t b[6]; // implicit type
} __attribute__((packed)) bdaddr_t;
typedef struct _sdp_list sdp_list_t;
Example for NeedIdentifier=true: As implicit typedef:
struct hidp_connadd_req { struct hidp_connadd_req {
int ctrl_sock; int ctrl_sock;
} }
As variable:
struct hidp_conninfo *ci; struct hidp_conninfo *ci;
typedef struct _sdp_list sdp_list_t;
*) *)
begin begin
CreateChildNode(ccnStruct); CreateChildNode(ccnStruct);
ReadNextAtom; ReadNextAtom;
if NeedIdentifier then begin if CurNode.Parent.Desc=ccnVariable then begin
// read type name // read variable name
if not AtomIsIdentifier then if not AtomIsIdentifier then
RaiseExpectedButAtomFound('identifier'); RaiseExpectedButAtomFound('identifier');
ReadNextAtom; ReadNextAtom;
@ -417,25 +421,28 @@ begin
end; end;
function TCCodeParserTool.TypedefToken: boolean; function TCCodeParserTool.TypedefToken: boolean;
{ examples:
typedef type name;
}
begin begin
Result:=true; Result:=true;
CreateChildNode(ccnTypedef); CreateChildNode(ccnTypedef);
// read type // read type
ReadNextAtom; ReadNextAtom;
if AtomIs('enum') then if AtomIs('typedef') then
ReadEnum RaiseExpectedButAtomFound('declaration')
else if AtomIs('struct') then else if AtomIs('struct') then
ReadStruct(false) ReadStruct
else if AtomIsIdentifier then begin else if AtomIs('enum') then
ReadEnum
end else else if SrcPos>SrcLen then
RaiseExpectedButAtomFound('identifier'); RaiseException('missing declaration')
// read typedef name else
ReadNextAtom; ReadVariable;
if not AtomIsIdentifier then DebugLn(['TCCodeParserTool.TypedefToken AAA1 ',GetAtom]);
RaiseExpectedButAtomFound('identifier');
// read semicolon // read semicolon
ReadNextAtom; ReadNextAtom;
DebugLn(['TCCodeParserTool.TypedefToken AAA2 ',GetAtom]);
if not AtomIsChar(';') then if not AtomIsChar(';') then
RaiseExpectedButAtomFound(';'); RaiseExpectedButAtomFound(';');
EndChildNode; EndChildNode;
@ -444,7 +451,7 @@ end;
function TCCodeParserTool.StructToken: boolean; function TCCodeParserTool.StructToken: boolean;
begin begin
Result:=true; Result:=true;
ReadStruct(true); ReadStruct;
end; end;
procedure TCCodeParserTool.InitKeyWordList; procedure TCCodeParserTool.InitKeyWordList;
@ -489,12 +496,12 @@ begin
NewNode.Desc:=Desc; NewNode.Desc:=Desc;
CurNode:=NewNode; CurNode:=NewNode;
CurNode.StartPos:=AtomStart; CurNode.StartPos:=AtomStart;
DebugLn([GetIndentStr(CurNode.GetLevel*2),'TCCodeParserTool.CreateChildNode ']); DebugLn([GetIndentStr(CurNode.GetLevel*2),'TCCodeParserTool.CreateChildNode ',CCNodeDescAsString(Desc)]);
end; end;
procedure TCCodeParserTool.EndChildNode; procedure TCCodeParserTool.EndChildNode;
begin begin
DebugLn([GetIndentStr(CurNode.GetLevel*2),'TCCodeParserTool.EndChildNode ']); DebugLn([GetIndentStr(CurNode.GetLevel*2),'TCCodeParserTool.EndChildNode ',CCNodeDescAsString(CurNode.Desc)]);
if CurNode.EndPos<=0 then if CurNode.EndPos<=0 then
CurNode.EndPos:=SrcPos; CurNode.EndPos:=SrcPos;
CurNode:=CurNode.Parent; CurNode:=CurNode.Parent;
@ -545,9 +552,13 @@ procedure TCCodeParserTool.ReadVariable;
return memcmp(ba1, ba2, sizeof(bdaddr_t)); return memcmp(ba1, ba2, sizeof(bdaddr_t));
} }
bdaddr_t *strtoba(const char *str); bdaddr_t *strtoba(const char *str);
*)
{
int (*fp)(char*); // pointer to function taking a char* argument; returns an int
int * f(char*); // function taking a char* argument; returns a pointer to int
complex operator+(complex, complex); complex operator+(complex, complex);
*) }
var var
IsFunction: Boolean; IsFunction: Boolean;
NeedEnd: Boolean; NeedEnd: Boolean;
@ -642,18 +653,23 @@ begin
ReadTilBracketClose(true); ReadTilBracketClose(true);
CurNode.EndPos:=SrcPos; CurNode.EndPos:=SrcPos;
EndChildNode; EndChildNode;
ReadNextAtom; if CurNode.Parent.Desc=ccnTypedef then begin
if AtomIsChar('{') then begin if AtomIsChar('{') then
// read statements {} RaiseException('typedef can not have a statement block');
CreateChildNode(ccnStatementBlock); end else begin
ReadTilBracketClose(true); ReadNextAtom;
CurNode.EndPos:=SrcPos; if AtomIsChar('{') then begin
EndChildNode; // read statements {}
end else if not AtomIsChar(';') then begin CreateChildNode(ccnStatementBlock);
// functions without statements are external and must end with a semicolon ReadTilBracketClose(true);
RaiseExpectedButAtomFound(';'); CurNode.EndPos:=SrcPos;
EndChildNode;
end else if not AtomIsChar(';') then begin
// functions without statements are external and must end with a semicolon
RaiseExpectedButAtomFound(';');
end;
NeedEnd:=false;
end; end;
NeedEnd:=false;
ReadNextAtom; ReadNextAtom;
end else if AtomIsChar('[') then begin end else if AtomIsChar('[') then begin
// read array brackets // read array brackets
@ -665,6 +681,8 @@ begin
// read initial constant // read initial constant
if AtomIsChar('=') then begin if AtomIsChar('=') then begin
if CurNode.HasParentOfType(ccnTypedef) then
RaiseException('typedef can not have an initial value');
ReadNextAtom; ReadNextAtom;
ReadConstant; ReadConstant;
ReadNextAtom; ReadNextAtom;

View File

@ -116,6 +116,8 @@ struct _sdp_list {
void *data; void *data;
}; };
typedef void(*sdp_list_func_t)(void *, void *);
complex operator+(complex, complex); complex operator+(complex, complex);
int y = 7; int y = 7;
@ -132,7 +134,10 @@ int *pi; // pointer to int
char ** ppc; // pointer to pointer to char char ** ppc; // pointer to pointer to char
int* ap[15]; // array of 15 pointers to ints int* ap[15]; // array of 15 pointers to ints
int (*fp)(char*); // pointer to function taking a char* argument; returns an int int (*fp)(char*); // pointer to function taking a char* argument; returns an int
int * f(char*); // fucntion taking a char* argument; returns a pointer to int int * f(char*); // function taking a char* argument; returns a pointer to int
unsigned short unsigned_short;
unsigned long long unsigned_long_long;
#define MACRO_CONCATENATION(a,b) a##b #define MACRO_CONCATENATION(a,b) a##b