1 /* GObject introspection: scanner
3 * Copyright (C) 2008 Johan Dahlin <johan@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "sourcescanner.h"
31 #define WIN32_LEAN_AND_MEAN
36 DL_EXPORT(void) init_giscanner(void);
38 #define NEW_CLASS(ctype, name, cname) \
39 static const PyMethodDef _Py##cname##_methods[]; \
40 PyTypeObject Py##cname##_Type = { \
41 PyObject_HEAD_INIT(NULL) \
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
47 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
52 0, 0, NULL, NULL, 0, 0, \
56 #define REGISTER_TYPE(d, name, type) \
57 type.ob_type = &PyType_Type; \
58 type.tp_alloc = PyType_GenericAlloc; \
59 type.tp_new = PyType_GenericNew; \
60 if (PyType_Ready (&type)) \
62 PyDict_SetItemString (d, name, (PyObject *)&type); \
70 static PyObject * pygi_source_type_new (GISourceType *type);
74 GISourceSymbol *symbol;
79 GISourceScanner *scanner;
82 NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
83 NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
84 NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
90 pygi_source_symbol_new (GISourceSymbol *symbol)
92 PyGISourceSymbol *self;
100 self = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
101 &PyGISourceSymbol_Type);
102 self->symbol = symbol;
103 return (PyObject*)self;
107 symbol_get_type (PyGISourceSymbol *self,
110 return PyInt_FromLong (self->symbol->type);
114 symbol_get_ident (PyGISourceSymbol *self,
118 if (!self->symbol->ident)
124 return PyString_FromString (self->symbol->ident);
128 symbol_get_base_type (PyGISourceSymbol *self,
131 return pygi_source_type_new (self->symbol->base_type);
135 symbol_get_const_int (PyGISourceSymbol *self,
138 return PyInt_FromLong (self->symbol->const_int);
142 symbol_get_const_string (PyGISourceSymbol *self,
145 if (!self->symbol->const_string)
151 return PyString_FromString (self->symbol->const_string);
155 symbol_get_source_filename (PyGISourceSymbol *self,
158 if (!self->symbol->source_filename)
164 return PyString_FromString (self->symbol->source_filename);
167 static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
169 { "type", (getter)symbol_get_type, NULL, NULL},
171 { "ident", (getter)symbol_get_ident, NULL, NULL},
172 { "base_type", (getter)symbol_get_base_type, NULL, NULL},
173 /* gboolean const_int_set; */
174 { "const_int", (getter)symbol_get_const_int, NULL, NULL},
175 { "const_string", (getter)symbol_get_const_string, NULL, NULL},
176 { "source_filename", (getter)symbol_get_source_filename, NULL, NULL},
185 pygi_source_type_new (GISourceType *type)
187 PyGISourceType *self;
195 self = (PyGISourceType *)PyObject_New (PyGISourceType,
196 &PyGISourceType_Type);
198 return (PyObject*)self;
202 type_get_type (PyGISourceType *self,
205 return PyInt_FromLong (self->type->type);
209 type_get_storage_class_specifier (PyGISourceType *self,
212 return PyInt_FromLong (self->type->storage_class_specifier);
216 type_get_type_qualifier (PyGISourceType *self,
219 return PyInt_FromLong (self->type->type_qualifier);
223 type_get_function_specifier (PyGISourceType *self,
226 return PyInt_FromLong (self->type->function_specifier);
230 type_get_name (PyGISourceType *self,
233 if (!self->type->name)
239 return PyString_FromString (self->type->name);
243 type_get_base_type (PyGISourceType *self,
246 return pygi_source_type_new (self->type->base_type);
250 type_get_child_list (PyGISourceType *self,
258 return Py_BuildValue("[]");
260 list = PyList_New (g_list_length (self->type->child_list));
262 for (l = self->type->child_list; l; l = l->next)
264 PyObject *item = pygi_source_symbol_new (l->data);
265 PyList_SetItem (list, i++, item);
274 type_get_is_bitfield (PyGISourceType *self,
277 return PyInt_FromLong (self->type->is_bitfield);
280 static const PyGetSetDef _PyGISourceType_getsets[] = {
281 { "type", (getter)type_get_type, NULL, NULL},
282 { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
283 { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
284 { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
285 { "name", (getter)type_get_name, NULL, NULL},
286 { "base_type", (getter)type_get_base_type, NULL, NULL},
287 { "child_list", (getter)type_get_child_list, NULL, NULL},
288 { "is_bitfield", (getter)type_get_is_bitfield, NULL, NULL},
297 pygi_source_scanner_init (PyGISourceScanner *self,
301 if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
304 self->scanner = gi_source_scanner_new ();
310 pygi_source_scanner_append_filename (PyGISourceScanner *self,
315 if (!PyArg_ParseTuple (args, "s:SourceScanner.append_filename", &filename))
318 self->scanner->filenames = g_list_append (self->scanner->filenames,
319 g_strdup (filename));
326 pygi_source_scanner_parse_macros (PyGISourceScanner *self,
333 list = PyTuple_GET_ITEM (args, 0);
335 if (!PyList_Check (list))
337 PyErr_SetString (PyExc_RuntimeError, "parse macro takes a list of filenames");
342 for (i = 0; i < PyList_Size (list); ++i)
347 obj = PyList_GetItem (list, i);
348 filename = PyString_AsString (obj);
350 filenames = g_list_append (filenames, filename);
353 gi_source_scanner_parse_macros (self->scanner, filenames);
354 g_list_free (filenames);
361 pygi_source_scanner_parse_file (PyGISourceScanner *self,
367 if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
371 /* The file descriptor passed to us is from the C library Python
372 * uses. That is msvcr71.dll at least for Python 2.5. This code, at
373 * least if compiled with mingw, uses msvcrt.dll, so we cannot use
374 * the file descriptor directly. So perform appropriate magic.
378 int (*p__get_osfhandle) (int);
381 msvcr71 = GetModuleHandle ("msvcr71.dll");
384 g_print ("No msvcr71.dll loaded.\n");
388 p__get_osfhandle = GetProcAddress (msvcr71, "_get_osfhandle");
389 if (!p__get_osfhandle)
391 g_print ("No _get_osfhandle found in msvcr71.dll.\n");
395 handle = p__get_osfhandle (fd);
396 if (!p__get_osfhandle)
398 g_print ("Could not get OS handle from msvcr71 fd.\n");
402 fd = _open_osfhandle (handle, _O_RDONLY);
405 g_print ("Could not open C fd from OS handle.\n");
411 fp = fdopen (fd, "r");
414 PyErr_SetFromErrno (PyExc_OSError);
418 if (!gi_source_scanner_parse_file (self->scanner, fp))
420 g_print ("Something went wrong during parsing.\n");
429 pygi_source_scanner_lex_filename (PyGISourceScanner *self,
434 if (!PyArg_ParseTuple (args, "s:SourceScanner.lex_filename", &filename))
437 self->scanner->current_filename = g_strdup (filename);
438 if (!gi_source_scanner_lex_filename (self->scanner, filename))
440 g_print ("Something went wrong during lexing.\n");
443 self->scanner->filenames =
444 g_list_append (self->scanner->filenames, g_strdup (filename));
451 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
456 if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", ¯o_scan))
459 gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
466 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
472 symbols = gi_source_scanner_get_symbols (self->scanner);
473 list = PyList_New (g_slist_length (symbols));
475 for (l = symbols; l; l = l->next)
477 PyObject *item = pygi_source_symbol_new (l->data);
478 PyList_SetItem (list, i++, item);
487 pygi_source_scanner_get_comments (PyGISourceScanner *self)
489 GSList *l, *comments;
493 comments = gi_source_scanner_get_comments (self->scanner);
494 list = PyList_New (g_slist_length (comments));
496 for (l = comments; l; l = l->next)
498 PyObject *item = PyString_FromString (l->data);
499 PyList_SetItem (list, i++, item);
507 static const PyMethodDef _PyGISourceScanner_methods[] = {
508 { "get_comments", (PyCFunction) pygi_source_scanner_get_comments, METH_NOARGS },
509 { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
510 { "append_filename", (PyCFunction) pygi_source_scanner_append_filename, METH_VARARGS },
511 { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
512 { "parse_macros", (PyCFunction) pygi_source_scanner_parse_macros, METH_VARARGS },
513 { "lex_filename", (PyCFunction) pygi_source_scanner_lex_filename, METH_VARARGS },
514 { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
519 static int calc_attrs_length(PyObject *attributes, int indent,
528 for (i = 0; i < PyList_Size (attributes); ++i)
534 tuple = PyList_GetItem (attributes, i);
535 if (PyTuple_GetItem(tuple, 1) == Py_None)
538 if (!PyArg_ParseTuple(tuple, "ss", &attr, &value))
541 escaped = g_markup_escape_text (value, -1);
542 attr_length += 2 + strlen(attr) + strlen(escaped) + 2;
546 return attr_length + indent + self_indent;
549 /* Hall of shame, wasted time debugging the code below
550 * 20min - Johan 2009-02-19
553 pygi_collect_attributes (PyObject *self,
557 PyObject *attributes;
558 int indent, indent_len, i, j, self_indent;
564 if (!PyArg_ParseTuple(args, "sO!isi",
565 &tag_name, &PyList_Type, &attributes,
566 &self_indent, &indent_char,
570 if (attributes == Py_None || !PyList_Size(attributes))
571 return PyString_FromString("");
573 len = calc_attrs_length(attributes, indent, self_indent);
577 indent_len = self_indent + strlen(tag_name) + 1;
582 attr_value = g_string_new ("");
584 for (i = 0; i < PyList_Size (attributes); ++i)
587 char *attr, *value, *escaped;
589 tuple = PyList_GetItem (attributes, i);
591 if (!PyTuple_Check (tuple))
593 PyErr_SetString(PyExc_TypeError,
594 "attribute item must be a tuple");
598 if (!PyTuple_Size (tuple) == 2)
600 PyErr_SetString(PyExc_IndexError,
601 "attribute item must be a tuple of length 2");
605 if (PyTuple_GetItem(tuple, 1) == Py_None)
608 /* this leaks, but we exit after, so */
609 if (!PyArg_ParseTuple(tuple, "ss", &attr, &value))
612 if (indent_len && !first)
614 g_string_append_c (attr_value, '\n');
615 for (j = 0; j < indent_len; j++)
616 g_string_append_c (attr_value, ' ');
618 g_string_append_c (attr_value, ' ');
619 g_string_append (attr_value, attr);
620 g_string_append_c (attr_value, '=');
621 g_string_append_c (attr_value, '\"');
622 escaped = g_markup_escape_text (value, -1);
623 g_string_append (attr_value, escaped);
624 g_string_append_c (attr_value, '\"');
629 return PyString_FromString (g_string_free (attr_value, FALSE));
634 static const PyMethodDef pyscanner_functions[] = {
635 { "collect_attributes",
636 (PyCFunction) pygi_collect_attributes, METH_VARARGS },
637 { NULL, NULL, 0, NULL }
645 m = Py_InitModule ("giscanner._giscanner",
646 (PyMethodDef*)pyscanner_functions);
647 d = PyModule_GetDict (m);
649 PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
650 PyGISourceScanner_Type.tp_methods = (PyMethodDef*)_PyGISourceScanner_methods;
651 REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
653 PyGISourceSymbol_Type.tp_getset = (PyGetSetDef*)_PyGISourceSymbol_getsets;
654 REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
656 PyGISourceType_Type.tp_getset = (PyGetSetDef*)_PyGISourceType_getsets;
657 REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);