1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #include <wx/txtstrm.h>
16 #include <wx/dynarray.h>
17 #include <wx/xml/xml.h>
20 #define LogMessage(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
21 #define LogWarning(fmt, ...) { wxPrintf("WARNING: " fmt "\n", __VA_ARGS__); fflush(stdout); }
22 #define LogError(fmt, ...) { wxPrintf("ERROR: " fmt "\n", __VA_ARGS__); fflush(stdout); }
23 #define wxPrint(str) { wxPrintf(str); fflush(stdout); }
26 // ----------------------------------------------------------------------------
27 // Represents a type with or without const/static/ qualifier
28 // and with or without & and * operators
29 // ----------------------------------------------------------------------------
34 wxType(const wxString
& type
)
35 { SetFromString(type
); }
37 void SetFromString(const wxString
& t
);
38 wxString
GetAsString() const
41 wxString
GetClean() const;
44 { return m_strType
.Contains("const"); }
46 { return m_strType
.Contains("static"); }
47 bool IsPointer() const
48 { return m_strType
.Contains("*"); }
49 bool IsReference() const
50 { return m_strType
.Contains("&"); }
52 bool operator==(const wxType
& m
) const;
53 bool operator!=(const wxType
& m
) const
54 { return !(*this == m
); }
62 extern wxType wxEmptyType
;
63 WX_DECLARE_OBJARRAY(wxType
, wxTypeArray
);
67 // ----------------------------------------------------------------------------
68 // Represents a single prototype of a class' member.
69 // ----------------------------------------------------------------------------
73 wxMethod() { m_bConst
=m_bVirtual
=m_bStatic
=false; m_nLine
=-1; }
74 wxMethod(const wxType
& rettype
, const wxString
& name
,
75 const wxTypeArray
& arguments
, const wxArrayString
& defaults
,
76 bool isConst
, bool isStatic
, bool isVirtual
)
77 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
78 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
79 { SetArgumentTypes(arguments
,defaults
); m_nLine
=-1; }
84 //void SetFromString(const wxString& proto);
85 wxString
GetAsString() const;
87 // parser of the prototype:
88 // all these functions return strings with spaces stripped
89 wxType
GetReturnType() const
91 wxString
GetName() const
93 wxTypeArray
GetArgumentTypes() const
95 wxArrayString
GetArgumentDefaults() const
96 { return m_argDefaults
; }
97 int GetLocation() const
102 bool IsStatic() const
103 { return m_bStatic
; }
104 bool IsVirtual() const
105 { return m_bVirtual
; }
109 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
111 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
116 void SetReturnType(const wxType
& t
)
118 void SetName(const wxString
& name
)
120 void SetArgumentTypes(const wxTypeArray
& arr
, const wxArrayString
& defaults
);
121 void SetConst(bool c
= true)
123 void SetStatic(bool c
= true)
125 void SetVirtual(bool c
= true)
127 void SetLocation(int lineNumber
)
128 { m_nLine
=lineNumber
; }
132 bool operator==(const wxMethod
&) const;
133 bool operator!=(const wxMethod
& m
) const
134 { return !(*this == m
); }
136 void Dump(wxTextOutputStream
& stream
) const;
142 wxArrayString m_argDefaults
;
149 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
150 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
153 // ----------------------------------------------------------------------------
154 // Represents a class of the wx API/interface.
155 // ----------------------------------------------------------------------------
160 wxClass(const wxString
& name
, const wxString
& headername
)
161 : m_strName(name
), m_strHeader(headername
) {}
163 void AddMethod(const wxMethod
& func
)
164 { m_methods
.Add(func
); }
166 void SetHeader(const wxString
& header
)
167 { m_strHeader
=header
; }
168 void SetName(const wxString
& name
)
170 wxString
GetName() const
171 { return m_strName
; }
172 wxString
GetHeader() const
173 { return m_strHeader
; }
174 wxString
GetNameWithoutTemplate() const;
177 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
179 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
180 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
182 unsigned int GetMethodCount() const
183 { return m_methods
.GetCount(); }
184 wxMethod
& GetMethod(unsigned int n
) const
185 { return m_methods
[n
]; }
186 wxMethod
& GetLastMethod() const
187 { return m_methods
.Last(); }
189 // returns a single result (the first, which is also the only
190 // one if CheckConsistency() return true)
191 const wxMethod
* FindMethod(const wxMethod
& m
) const;
193 // returns an array of pointers to the overloaded methods with the
195 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
197 // dumps all methods to the given output stream
198 void Dump(wxTextOutputStream
& stream
) const;
201 bool CheckConsistency() const;
205 wxString m_strHeader
;
206 wxMethodArray m_methods
;
209 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
210 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
222 const wxClass
* FindClass(const wxString
& classname
) const
224 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
225 if (m_classes
[i
].GetName() == classname
)
226 return &m_classes
[i
];
230 void Dump(const wxString
& filename
);
232 const wxClassArray
& GetClasses() const
233 { return m_classes
; }
235 unsigned int GetClassesCount() const
236 { return m_classes
.GetCount(); }
238 unsigned int GetMethodCount() const
240 unsigned int methods
= 0;
241 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
242 methods
+= m_classes
[i
].GetMethodCount();
247 { /*wxPrint(".");*/ }
249 bool CheckParseResults() const;
252 wxClassArray m_classes
;
256 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
257 wxIntegerHash
, wxIntegerEqual
,
261 typedef std::basic_string
<char> stlString
;
262 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
266 // ----------------------------------------------------------------------------
267 // Represents the real interface of wxWidgets
268 // Loads it from the XML produced by gccXML: http://www.gccxml.org
269 // ----------------------------------------------------------------------------
270 class wxXmlGccInterface
: public wxXmlInterface
273 wxXmlGccInterface() {}
276 // Using wxXmlDocument::Load as is, all the types contained in the
277 // the entire gccXML file are stored in memory while parsing;
278 // however we are only interested to wx's own structs/classes/funcs/etc
279 // so that we could use the file IDs to avoid loading stuff which does
280 // not belong to wx. See the very end of the gccXML file: it contains
281 // a set of <File> nodes referenced by all nodes above.
283 bool Parse(const wxString
& filename
);
284 bool ParseMethod(const wxXmlNode
*p
,
285 const wxTypeIdHashMap
& types
,
290 // ----------------------------------------------------------------------------
291 // Represents the interface of the doxygen headers of wxWidgets
292 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
293 // ----------------------------------------------------------------------------
294 class wxXmlDoxygenInterface
: public wxXmlInterface
297 wxXmlDoxygenInterface() {}
300 // Using wxXmlDocument::Load as is, the entire XML file is parsed
301 // and an entire tree of wxXmlNodes is built in memory.
302 // We need however only small portions of the Doxygen-generated XML: to speedup the
303 // processing we could detach the expat callbacks when we detect the beginning of a
304 // node we're not interested about, or just don't create a wxXmlNode for it!
305 // This needs a modification of wxXml API.
307 bool Parse(const wxString
& filename
);
308 bool ParseCompoundDefinition(const wxString
& filename
);
309 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
314 #endif // _XMLPARSER_H_