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.
26 #include "sourcescanner.h"
27 #include "grealpath.h"
32 #define WIN32_LEAN_AND_MEAN
37 DL_EXPORT(void) init_giscanner(void);
39 #define NEW_CLASS(ctype, name, cname) \
40 static const PyMethodDef _Py##cname##_methods[]; \
41 PyTypeObject Py##cname##_Type = { \
42 PyObject_HEAD_INIT(NULL) \
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
48 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
53 0, 0, NULL, NULL, 0, 0, \
57 #define REGISTER_TYPE(d, name, type) \
58 type.ob_type = &PyType_Type; \
59 type.tp_alloc = PyType_GenericAlloc; \
60 type.tp_new = PyType_GenericNew; \
61 if (PyType_Ready (&type)) \
63 PyDict_SetItemString (d, name, (PyObject *)&type); \
71 static PyObject * pygi_source_type_new (GISourceType *type);
75 GISourceSymbol *symbol;
80 GISourceScanner *scanner;
83 NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
84 NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
85 NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
91 pygi_source_symbol_new (GISourceSymbol *symbol)
93 PyGISourceSymbol *self;
101 self = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
102 &PyGISourceSymbol_Type);
103 self->symbol = symbol;
104 return (PyObject*)self;
108 symbol_get_type (PyGISourceSymbol *self,
111 return PyInt_FromLong (self->symbol->type);
115 symbol_get_line (PyGISourceSymbol *self,
118 return PyInt_FromLong (self->symbol->line);
122 symbol_get_ident (PyGISourceSymbol *self,
126 if (!self->symbol->ident)
132 return PyString_FromString (self->symbol->ident);
136 symbol_get_base_type (PyGISourceSymbol *self,
139 return pygi_source_type_new (self->symbol->base_type);
143 symbol_get_const_int (PyGISourceSymbol *self,
146 if (!self->symbol->const_int_set)
151 return PyInt_FromLong (self->symbol->const_int);
155 symbol_get_const_double (PyGISourceSymbol *self,
158 if (!self->symbol->const_double_set)
163 return PyFloat_FromDouble (self->symbol->const_double);
167 symbol_get_const_string (PyGISourceSymbol *self,
170 if (!self->symbol->const_string)
176 return PyString_FromString (self->symbol->const_string);
180 symbol_get_source_filename (PyGISourceSymbol *self,
183 if (!self->symbol->source_filename)
189 return PyString_FromString (self->symbol->source_filename);
192 static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
194 { "type", (getter)symbol_get_type, NULL, NULL},
196 { "ident", (getter)symbol_get_ident, NULL, NULL},
197 { "base_type", (getter)symbol_get_base_type, NULL, NULL},
198 /* gboolean const_int_set; */
199 { "const_int", (getter)symbol_get_const_int, NULL, NULL},
200 /* gboolean const_double_set; */
201 { "const_double", (getter)symbol_get_const_double, NULL, NULL},
202 { "const_string", (getter)symbol_get_const_string, NULL, NULL},
203 { "source_filename", (getter)symbol_get_source_filename, NULL, NULL},
204 { "line", (getter)symbol_get_line, NULL, NULL},
213 pygi_source_type_new (GISourceType *type)
215 PyGISourceType *self;
223 self = (PyGISourceType *)PyObject_New (PyGISourceType,
224 &PyGISourceType_Type);
226 return (PyObject*)self;
230 type_get_type (PyGISourceType *self,
233 return PyInt_FromLong (self->type->type);
237 type_get_storage_class_specifier (PyGISourceType *self,
240 return PyInt_FromLong (self->type->storage_class_specifier);
244 type_get_type_qualifier (PyGISourceType *self,
247 return PyInt_FromLong (self->type->type_qualifier);
251 type_get_function_specifier (PyGISourceType *self,
254 return PyInt_FromLong (self->type->function_specifier);
258 type_get_name (PyGISourceType *self,
261 if (!self->type->name)
267 return PyString_FromString (self->type->name);
271 type_get_base_type (PyGISourceType *self,
274 return pygi_source_type_new (self->type->base_type);
278 type_get_child_list (PyGISourceType *self,
286 return Py_BuildValue("[]");
288 list = PyList_New (g_list_length (self->type->child_list));
290 for (l = self->type->child_list; l; l = l->next)
292 PyObject *item = pygi_source_symbol_new (l->data);
293 PyList_SetItem (list, i++, item);
302 type_get_is_bitfield (PyGISourceType *self,
305 return PyInt_FromLong (self->type->is_bitfield);
308 static const PyGetSetDef _PyGISourceType_getsets[] = {
309 { "type", (getter)type_get_type, NULL, NULL},
310 { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
311 { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
312 { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
313 { "name", (getter)type_get_name, NULL, NULL},
314 { "base_type", (getter)type_get_base_type, NULL, NULL},
315 { "child_list", (getter)type_get_child_list, NULL, NULL},
316 { "is_bitfield", (getter)type_get_is_bitfield, NULL, NULL},
325 pygi_source_scanner_init (PyGISourceScanner *self,
329 if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
332 self->scanner = gi_source_scanner_new ();
338 pygi_source_scanner_append_filename (PyGISourceScanner *self,
343 if (!PyArg_ParseTuple (args, "s:SourceScanner.append_filename", &filename))
346 self->scanner->filenames = g_list_append (self->scanner->filenames,
347 g_realpath (filename));
354 pygi_source_scanner_parse_macros (PyGISourceScanner *self,
361 list = PyTuple_GET_ITEM (args, 0);
363 if (!PyList_Check (list))
365 PyErr_SetString (PyExc_RuntimeError, "parse macro takes a list of filenames");
370 for (i = 0; i < PyList_Size (list); ++i)
375 obj = PyList_GetItem (list, i);
376 filename = PyString_AsString (obj);
378 filenames = g_list_append (filenames, filename);
381 gi_source_scanner_parse_macros (self->scanner, filenames);
382 g_list_free (filenames);
389 pygi_source_scanner_parse_file (PyGISourceScanner *self,
395 if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
399 /* The file descriptor passed to us is from the C library Python
400 * uses. That is msvcr71.dll at least for Python 2.5. This code, at
401 * least if compiled with mingw, uses msvcrt.dll, so we cannot use
402 * the file descriptor directly. So perform appropriate magic.
406 int (*p__get_osfhandle) (int);
409 msvcr71 = GetModuleHandle ("msvcr71.dll");
412 g_print ("No msvcr71.dll loaded.\n");
416 p__get_osfhandle = GetProcAddress (msvcr71, "_get_osfhandle");
417 if (!p__get_osfhandle)
419 g_print ("No _get_osfhandle found in msvcr71.dll.\n");
423 handle = p__get_osfhandle (fd);
424 if (!p__get_osfhandle)
426 g_print ("Could not get OS handle from msvcr71 fd.\n");
430 fd = _open_osfhandle (handle, _O_RDONLY);
433 g_print ("Could not open C fd from OS handle.\n");
439 fp = fdopen (fd, "r");
442 PyErr_SetFromErrno (PyExc_OSError);
446 if (!gi_source_scanner_parse_file (self->scanner, fp))
448 g_print ("Something went wrong during parsing.\n");
457 pygi_source_scanner_lex_filename (PyGISourceScanner *self,
462 if (!PyArg_ParseTuple (args, "s:SourceScanner.lex_filename", &filename))
465 self->scanner->current_filename = g_strdup (filename);
466 if (!gi_source_scanner_lex_filename (self->scanner, filename))
468 g_print ("Something went wrong during lexing.\n");
471 self->scanner->filenames =
472 g_list_append (self->scanner->filenames, g_strdup (filename));
479 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
484 if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", ¯o_scan))
487 gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
494 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
500 symbols = gi_source_scanner_get_symbols (self->scanner);
501 list = PyList_New (g_slist_length (symbols));
503 for (l = symbols; l; l = l->next)
505 PyObject *item = pygi_source_symbol_new (l->data);
506 PyList_SetItem (list, i++, item);
515 pygi_source_scanner_get_comments (PyGISourceScanner *self)
517 GSList *l, *comments;
521 comments = gi_source_scanner_get_comments (self->scanner);
522 list = PyList_New (g_slist_length (comments));
524 for (l = comments; l; l = l->next)
526 GISourceComment *comment = l->data;
527 PyObject *item = Py_BuildValue ("(ssi)", comment->comment,
530 PyList_SET_ITEM (list, i++, item);
538 static const PyMethodDef _PyGISourceScanner_methods[] = {
539 { "get_comments", (PyCFunction) pygi_source_scanner_get_comments, METH_NOARGS },
540 { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
541 { "append_filename", (PyCFunction) pygi_source_scanner_append_filename, METH_VARARGS },
542 { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
543 { "parse_macros", (PyCFunction) pygi_source_scanner_parse_macros, METH_VARARGS },
544 { "lex_filename", (PyCFunction) pygi_source_scanner_lex_filename, METH_VARARGS },
545 { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
550 static int calc_attrs_length(PyObject *attributes, int indent,
559 for (i = 0; i < PyList_Size (attributes); ++i)
565 tuple = PyList_GetItem (attributes, i);
566 if (PyTuple_GetItem(tuple, 1) == Py_None)
569 if (!PyArg_ParseTuple(tuple, "ss", &attr, &value))
572 escaped = g_markup_escape_text (value, -1);
573 attr_length += 2 + strlen(attr) + strlen(escaped) + 2;
577 return attr_length + indent + self_indent;
580 /* Hall of shame, wasted time debugging the code below
581 * 20min - Johan 2009-02-19
584 pygi_collect_attributes (PyObject *self,
588 PyObject *attributes;
589 int indent, indent_len, i, j, self_indent;
595 if (!PyArg_ParseTuple(args, "sO!isi",
596 &tag_name, &PyList_Type, &attributes,
597 &self_indent, &indent_char,
601 if (attributes == Py_None || !PyList_Size(attributes))
602 return PyString_FromString("");
604 len = calc_attrs_length(attributes, indent, self_indent);
608 indent_len = self_indent + strlen(tag_name) + 1;
613 attr_value = g_string_new ("");
615 for (i = 0; i < PyList_Size (attributes); ++i)
618 char *attr, *value, *escaped;
620 tuple = PyList_GetItem (attributes, i);
622 if (!PyTuple_Check (tuple))
624 PyErr_SetString(PyExc_TypeError,
625 "attribute item must be a tuple");
629 if (!PyTuple_Size (tuple) == 2)
631 PyErr_SetString(PyExc_IndexError,
632 "attribute item must be a tuple of length 2");
636 if (PyTuple_GetItem(tuple, 1) == Py_None)
639 /* this leaks, but we exit after, so */
640 if (!PyArg_ParseTuple(tuple, "ss", &attr, &value))
643 if (indent_len && !first)
645 g_string_append_c (attr_value, '\n');
646 for (j = 0; j < indent_len; j++)
647 g_string_append_c (attr_value, ' ');
649 g_string_append_c (attr_value, ' ');
650 g_string_append (attr_value, attr);
651 g_string_append_c (attr_value, '=');
652 g_string_append_c (attr_value, '\"');
653 escaped = g_markup_escape_text (value, -1);
654 g_string_append (attr_value, escaped);
655 g_string_append_c (attr_value, '\"');
660 return PyString_FromString (g_string_free (attr_value, FALSE));
665 static const PyMethodDef pyscanner_functions[] = {
666 { "collect_attributes",
667 (PyCFunction) pygi_collect_attributes, METH_VARARGS },
668 { NULL, NULL, 0, NULL }
676 m = Py_InitModule ("giscanner._giscanner",
677 (PyMethodDef*)pyscanner_functions);
678 d = PyModule_GetDict (m);
680 PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
681 PyGISourceScanner_Type.tp_methods = (PyMethodDef*)_PyGISourceScanner_methods;
682 REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
684 PyGISourceSymbol_Type.tp_getset = (PyGetSetDef*)_PyGISourceSymbol_getsets;
685 REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
687 PyGISourceType_Type.tp_getset = (PyGetSetDef*)_PyGISourceType_getsets;
688 REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);