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); \
67 GISourceDirective *directive;
68 } PyGISourceDirective;
75 static PyObject * pygi_source_type_new (GISourceType *type);
79 GISourceSymbol *symbol;
85 GISourceScanner *scanner;
88 NEW_CLASS (PyGISourceDirective, "SourceDirective", GISourceDirective);
89 NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
90 NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
91 NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
97 pygi_source_directive_new (GISourceDirective *directive)
99 PyGISourceDirective *self;
101 if (directive == NULL)
107 self = (PyGISourceDirective *)PyObject_New (PyGISourceDirective,
108 &PyGISourceDirective_Type);
109 self->directive = directive;
110 return (PyObject*)self;
114 directive_get_name (PyGISourceDirective *self,
117 return PyString_FromString (self->directive->name);
121 directive_get_value (PyGISourceDirective *self,
124 return PyString_FromString (self->directive->value);
128 directive_get_options (PyGISourceDirective *self,
135 if (!self->directive)
136 return Py_BuildValue("[]");
138 list = PyList_New (g_slist_length (self->directive->options));
140 for (l = self->directive->options; l; l = l->next)
142 PyObject *item = PyString_FromString (l->data);
143 PyList_SetItem (list, i++, item);
151 static const PyGetSetDef _PyGISourceDirective_getsets[] = {
152 { "name", (getter)directive_get_name, NULL, NULL},
153 { "value", (getter)directive_get_value, NULL, NULL},
154 { "options", (getter)directive_get_options, NULL, NULL},
161 pygi_source_symbol_new (GISourceSymbol *symbol)
163 PyGISourceSymbol *self;
171 self = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
172 &PyGISourceSymbol_Type);
173 self->symbol = symbol;
174 return (PyObject*)self;
178 symbol_get_type (PyGISourceSymbol *self,
181 return PyInt_FromLong (self->symbol->type);
185 symbol_get_ident (PyGISourceSymbol *self,
189 if (!self->symbol->ident)
195 return PyString_FromString (self->symbol->ident);
199 symbol_get_base_type (PyGISourceSymbol *self,
202 return pygi_source_type_new (self->symbol->base_type);
206 symbol_get_const_int (PyGISourceSymbol *self,
209 return PyInt_FromLong (self->symbol->const_int);
213 symbol_get_const_string (PyGISourceSymbol *self,
216 if (!self->symbol->const_string)
222 return PyString_FromString (self->symbol->const_string);
226 symbol_get_directives (PyGISourceSymbol *self,
229 if (!self->directives)
230 self->directives = Py_BuildValue("[]");
231 Py_INCREF (self->directives);
232 return self->directives;
236 symbol_set_directives (PyGISourceSymbol *self,
240 self->directives = value;
245 static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
247 { "type", (getter)symbol_get_type, NULL, NULL},
249 { "ident", (getter)symbol_get_ident, NULL, NULL},
250 { "base_type", (getter)symbol_get_base_type, NULL, NULL},
251 /* gboolean const_int_set; */
252 { "const_int", (getter)symbol_get_const_int, NULL, NULL},
253 { "const_string", (getter)symbol_get_const_string, NULL, NULL},
254 { "directives", (getter)symbol_get_directives,
255 (setter)symbol_set_directives, NULL},
264 pygi_source_type_new (GISourceType *type)
266 PyGISourceType *self;
274 self = (PyGISourceType *)PyObject_New (PyGISourceType,
275 &PyGISourceType_Type);
277 return (PyObject*)self;
281 type_get_type (PyGISourceType *self,
284 return PyInt_FromLong (self->type->type);
288 type_get_storage_class_specifier (PyGISourceType *self,
291 return PyInt_FromLong (self->type->storage_class_specifier);
295 type_get_type_qualifier (PyGISourceType *self,
298 return PyInt_FromLong (self->type->type_qualifier);
302 type_get_function_specifier (PyGISourceType *self,
305 return PyInt_FromLong (self->type->function_specifier);
309 type_get_name (PyGISourceType *self,
312 if (!self->type->name)
318 return PyString_FromString (self->type->name);
322 type_get_base_type (PyGISourceType *self,
325 return pygi_source_type_new (self->type->base_type);
329 type_get_child_list (PyGISourceType *self,
337 return Py_BuildValue("[]");
339 list = PyList_New (g_list_length (self->type->child_list));
341 for (l = self->type->child_list; l; l = l->next)
343 PyObject *item = pygi_source_symbol_new (l->data);
344 PyList_SetItem (list, i++, item);
352 static const PyGetSetDef _PyGISourceType_getsets[] = {
353 { "type", (getter)type_get_type, NULL, NULL},
354 { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
355 { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
356 { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
357 { "name", (getter)type_get_name, NULL, NULL},
358 { "base_type", (getter)type_get_base_type, NULL, NULL},
359 { "child_list", (getter)type_get_child_list, NULL, NULL},
368 pygi_source_scanner_init (PyGISourceScanner *self,
372 if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
375 self->scanner = gi_source_scanner_new ();
381 pygi_source_scanner_append_filename (PyGISourceScanner *self,
386 if (!PyArg_ParseTuple (args, "s:SourceScanner.append_filename", &filename))
389 self->scanner->filenames = g_list_append (self->scanner->filenames,
390 g_strdup (filename));
397 pygi_source_scanner_parse_macros (PyGISourceScanner *self,
404 list = PyTuple_GET_ITEM (args, 0);
406 if (!PyList_Check (list))
408 PyErr_SetString (PyExc_RuntimeError, "parse macro takes a list of filenames");
413 for (i = 0; i < PyList_Size (list); ++i)
418 obj = PyList_GetItem (list, i);
419 filename = PyString_AsString (obj);
421 filenames = g_list_append (filenames, filename);
424 gi_source_scanner_parse_macros (self->scanner, filenames);
425 g_list_free (filenames);
432 pygi_source_scanner_parse_file (PyGISourceScanner *self,
438 if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
442 /* The file descriptor passed to us is from the C library Python
443 * uses. That is msvcr71.dll at least for Python 2.5. This code, at
444 * least if compiled with mingw, uses msvcrt.dll, so we cannot use
445 * the file descriptor directly. So perform appropriate magic.
449 int (*p__get_osfhandle) (int);
452 msvcr71 = GetModuleHandle ("msvcr71.dll");
455 g_print ("No msvcr71.dll loaded.\n");
459 p__get_osfhandle = GetProcAddress (msvcr71, "_get_osfhandle");
460 if (!p__get_osfhandle)
462 g_print ("No _get_osfhandle found in msvcr71.dll.\n");
466 handle = p__get_osfhandle (fd);
467 if (!p__get_osfhandle)
469 g_print ("Could not get OS handle from msvcr71 fd.\n");
473 fd = _open_osfhandle (handle, _O_RDONLY);
476 g_print ("Could not open C fd from OS handle.\n");
482 fp = fdopen (fd, "r");
485 PyErr_SetFromErrno (PyExc_OSError);
489 if (!gi_source_scanner_parse_file (self->scanner, fp))
491 g_print ("Something went wrong during parsing.\n");
500 pygi_source_scanner_lex_filename (PyGISourceScanner *self,
505 if (!PyArg_ParseTuple (args, "s:SourceScanner.lex_filename", &filename))
508 self->scanner->current_filename = g_strdup (filename);
509 if (!gi_source_scanner_lex_filename (self->scanner, filename))
511 g_print ("Something went wrong during lexing.\n");
514 self->scanner->filenames =
515 g_list_append (self->scanner->filenames, g_strdup (filename));
522 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
527 if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", ¯o_scan))
530 gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
537 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
543 symbols = gi_source_scanner_get_symbols (self->scanner);
544 list = PyList_New (g_slist_length (symbols));
546 for (l = symbols; l; l = l->next)
548 PyObject *item = pygi_source_symbol_new (l->data);
549 PyList_SetItem (list, i++, item);
558 pygi_source_scanner_get_directives (PyGISourceScanner *self,
561 GSList *l, *directives;
566 if (!PyArg_ParseTuple (args, "s:SourceScanner.get_directives", &name))
569 directives = gi_source_scanner_get_directives (self->scanner, name);
570 list = PyList_New (g_slist_length (directives));
572 for (l = directives; l; l = l->next)
574 PyObject *item = pygi_source_directive_new (l->data);
575 PyList_SetItem (list, i++, item);
583 static const PyMethodDef _PyGISourceScanner_methods[] = {
584 { "get_directives", (PyCFunction) pygi_source_scanner_get_directives, METH_VARARGS },
585 { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
586 { "append_filename", (PyCFunction) pygi_source_scanner_append_filename, METH_VARARGS },
587 { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
588 { "parse_macros", (PyCFunction) pygi_source_scanner_parse_macros, METH_VARARGS },
589 { "lex_filename", (PyCFunction) pygi_source_scanner_lex_filename, METH_VARARGS },
590 { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
595 static int calc_attrs_length(PyObject *attributes, int indent,
604 for (i = 0; i < PyList_Size (attributes); ++i)
610 tuple = PyList_GetItem (attributes, i);
611 if (PyTuple_GetItem(tuple, 1) == Py_None)
614 g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value));
616 escaped = g_markup_escape_text (value, -1);
617 attr_length += 2 + strlen(attr) + strlen(escaped) + 2;
621 return attr_length + indent + self_indent;
625 pygi_collect_attributes (PyObject *self,
629 PyObject *attributes;
630 int indent, indent_len, i, j, self_indent;
635 if (!PyArg_ParseTuple(args, "sOisi",
636 &tag_name, &attributes,
637 &self_indent, &indent_char,
641 if (attributes == Py_None || !PyList_Size(attributes))
642 return PyString_FromString("");
644 if (calc_attrs_length(attributes, indent, self_indent) > 79)
645 indent_len = self_indent + strlen(tag_name) + 1;
650 attr_value = g_string_new ("");
652 for (i = 0; i < PyList_Size (attributes); ++i)
655 char *attr, *value, *escaped;
657 tuple = PyList_GetItem (attributes, i);
658 g_assert(tuple != NULL);
659 g_assert(PyTuple_Size(tuple) == 2);
660 if (PyTuple_GetItem(tuple, 1) == Py_None)
663 g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value));
665 if (indent_len && !first)
667 g_string_append_c (attr_value, '\n');
668 for (j = 0; j < indent_len; j++)
669 g_string_append_c (attr_value, ' ');
671 g_string_append_c (attr_value, ' ');
672 g_string_append (attr_value, attr);
673 g_string_append_c (attr_value, '=');
674 g_string_append_c (attr_value, '\"');
675 escaped = g_markup_escape_text (value, -1);
676 g_string_append (attr_value, escaped);
677 g_string_append_c (attr_value, '\"');
682 return PyString_FromString (g_string_free (attr_value, FALSE));
687 static const PyMethodDef pyscanner_functions[] = {
688 { "collect_attributes",
689 (PyCFunction) pygi_collect_attributes, METH_VARARGS },
690 { NULL, NULL, 0, NULL }
698 m = Py_InitModule ("giscanner._giscanner",
699 (PyMethodDef*)pyscanner_functions);
700 d = PyModule_GetDict (m);
702 PyGISourceDirective_Type.tp_getset = (PyGetSetDef*)_PyGISourceDirective_getsets;
703 REGISTER_TYPE (d, "SourceDirective", PyGISourceDirective_Type);
705 PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
706 PyGISourceScanner_Type.tp_methods = (PyMethodDef*)_PyGISourceScanner_methods;
707 REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
709 PyGISourceSymbol_Type.tp_getset = (PyGetSetDef*)_PyGISourceSymbol_getsets;
710 REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
712 PyGISourceType_Type.tp_getset = (PyGetSetDef*)_PyGISourceType_getsets;
713 REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);