gidlwriter.c 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* GObject introspection: gen-introspect
 *
 * Copyright (C) 2007  Jürg Billeter
 * Copyright (C) 2007  Johan Dahlin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

#include <stdio.h>
#include <glib.h>
#include "scanner.h"
#include "gidlnode.h"

typedef struct {
  int indent;
  FILE *output;
} GIdlWriter;

static void node_generate (GIdlWriter * writer, GIdlNode * node);

static void
g_writer_write_inline (GIdlWriter * writer, const char *s)
{
  fprintf (writer->output, "%s", s);
}

static void
g_writer_write (GIdlWriter * writer, const char *s)
{
  int i;
  for (i = 0; i < writer->indent; i++)
    {
      fprintf (writer->output, "\t");
    }

  g_writer_write_inline (writer, s);
}

static void
g_writer_write_indent (GIdlWriter * writer, const char *s)
{
  g_writer_write (writer, s);
  writer->indent++;
}

static void
g_writer_write_unindent (GIdlWriter * writer, const char *s)
{
  writer->indent--;
  g_writer_write (writer, s);
}

static void
field_generate (GIdlWriter * writer, GIdlNodeField * node)
{
  char *markup =
    g_markup_printf_escaped ("<field name=\"%s\" type=\"%s\"/>\n",
			     node->node.name, node->type->unparsed);
  g_writer_write (writer, markup);
  g_free (markup);
}

static void
value_generate (GIdlWriter * writer, GIdlNodeValue * node)
{
  char *markup =
    g_markup_printf_escaped ("<member name=\"%s\" value=\"%d\"/>\n",
			     node->node.name, node->value);
  g_writer_write (writer, markup);
  g_free (markup);
}

static void
constant_generate (GIdlWriter * writer, GIdlNodeConstant * node)
{
  char *markup =
    g_markup_printf_escaped
    ("<constant name=\"%s\" type=\"%s\" value=\"%s\"/>\n", node->node.name,
     node->type->unparsed, node->value);
  g_writer_write (writer, markup);
  g_free (markup);
}

static void
property_generate (GIdlWriter * writer, GIdlNodeProperty * node)
{
  char *markup =
    g_markup_printf_escaped ("<property name=\"%s\" "
			     "type=\"%s\" "
			     "readable=\"%s\" "
			     "writable=\"%s\" "
			     "construct=\"%s\" "
			     "construct-only=\"%s\"/>\n",
			     node->node.name,
			     node->type->unparsed,
			     node->readable ? "1" : "0",
			     node->writable ? "1" : "0",
			     node->construct ? "1" : "0",
			     node->construct_only ? "1" : "0");
  g_writer_write (writer, markup);
  g_free (markup);
}

static void
function_generate (GIdlWriter * writer, GIdlNodeFunction * node)
{
  const char *tag_name;
  GString *markup_s;
  gchar *markup;
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140
  if (node->node.type == G_IDL_NODE_CALLBACK)
    tag_name = "callback";
  else if (node->is_constructor)
    tag_name = "constructor";
  else if (node->is_method)
    tag_name = "method";
  else
    tag_name = "function";

  markup_s = g_string_new ("<");
  g_string_append_printf (markup_s,
			  "%s name=\"%s\"",
			  tag_name, node->node.name);

141 142 143 144 145
  if (node->node.type != G_IDL_NODE_CALLBACK) {
    gchar *tmp = g_markup_printf_escaped (" symbol=\"%s\"", node->symbol);
    markup_s = g_string_append (markup_s, tmp);
    g_free (tmp);
  }
146 147

  if (node->deprecated)
148
    markup_s = g_string_append (markup_s, " deprecated=\"1\"");
149 150

  g_string_append (markup_s, ">\n");
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  g_writer_write_indent (writer, markup_s->str);
  g_string_free (markup_s, TRUE);

  markup =
    g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
			     node->result->type->unparsed);
  g_writer_write (writer, markup);
  g_free (markup);

  if (node->parameters != NULL)
    {
      GList *l;
      g_writer_write_indent (writer, "<parameters>\n");
      for (l = node->parameters; l != NULL; l = l->next)
	{
	  GIdlNodeParam *param = l->data;
	  markup =
	    g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
				     param->node.name, param->type->unparsed);
	  g_writer_write (writer, markup);
	  g_free (markup);
	}
      g_writer_write_unindent (writer, "</parameters>\n");
    }
  markup = g_strdup_printf ("</%s>\n", tag_name);
  g_writer_write_unindent (writer, markup);
  g_free (markup);
}

static void
vfunc_generate (GIdlWriter * writer, GIdlNodeVFunc * node)
{
  char *markup =
    g_markup_printf_escaped ("<vfunc name=\"%s\">\n", node->node.name);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  markup =
    g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
			     node->result->type->unparsed);
  g_writer_write (writer, markup);
  g_free (markup);
  if (node->parameters != NULL)
    {
      GList *l;
      g_writer_write_indent (writer, "<parameters>\n");
      for (l = node->parameters; l != NULL; l = l->next)
	{
	  GIdlNodeParam *param = l->data;
	  markup =
	    g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
				     param->node.name, param->type->unparsed);
	  g_writer_write (writer, markup);
	  g_free (markup);
	}
      g_writer_write_unindent (writer, "</parameters>\n");
    }
  g_writer_write_unindent (writer, "</vfunc>\n");
}

static void
signal_generate (GIdlWriter * writer, GIdlNodeSignal * node)
{
  char *markup;
  const char *when = "LAST";
  if (node->run_first)
    {
      when = "FIRST";
    }
  else if (node->run_cleanup)
    {
      when = "CLEANUP";
    }
  markup =
    g_markup_printf_escaped ("<signal name=\"%s\" when=\"%s\">\n",
			     node->node.name, when);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  markup =
    g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
			     node->result->type->unparsed);
  g_writer_write (writer, markup);
  g_free (markup);
  if (node->parameters != NULL)
    {
      GList *l;
      g_writer_write_indent (writer, "<parameters>\n");
      for (l = node->parameters; l != NULL; l = l->next)
	{
	  GIdlNodeParam *param = l->data;
	  markup =
	    g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
				     param->node.name, param->type->unparsed);
	  g_writer_write (writer, markup);
	  g_free (markup);
	}
      g_writer_write_unindent (writer, "</parameters>\n");
    }
  g_writer_write_unindent (writer, "</signal>\n");
}

static void
interface_generate (GIdlWriter * writer, GIdlNodeInterface * node)
{
  GList *l;
  char *markup;
  if (node->node.type == G_IDL_NODE_OBJECT)
    {
      markup =
	g_markup_printf_escaped ("<object name=\"%s\" "
				 "parent=\"%s\" "
				 "type-name=\"%s\" "
				 "get-type=\"%s\">\n",
				 node->node.name,
				 node->parent,
				 node->gtype_name,
				 node->gtype_init);
    }
  else if (node->node.type == G_IDL_NODE_INTERFACE)
    {
      markup =
	g_markup_printf_escaped
	("<interface name=\"%s\" type-name=\"%s\" get-type=\"%s\">\n",
	 node->node.name, node->gtype_name, node->gtype_init);
    }

  g_writer_write_indent (writer, markup);
  g_free (markup);
  if (node->node.type == G_IDL_NODE_OBJECT && node->interfaces != NULL)
    {
      GList *l;
      g_writer_write_indent (writer, "<implements>\n");
      for (l = node->interfaces; l != NULL; l = l->next)
	{
	  markup =
	    g_markup_printf_escaped ("<interface name=\"%s\"/>\n",
				     (char *) l->data);
	  g_writer_write (writer, markup);
	  g_free (markup);
	}
      g_writer_write_unindent (writer, "</implements>\n");
    }
  else if (node->node.type == G_IDL_NODE_INTERFACE
	   && node->prerequisites != NULL)
    {
      GList *l;
      g_writer_write_indent (writer, "<requires>\n");
      for (l = node->prerequisites; l != NULL; l = l->next)
	{
	  markup =
	    g_markup_printf_escaped ("<interface name=\"%s\"/>\n",
				     (char *) l->data);
	  g_writer_write (writer, markup);
	  g_free (markup);
	}
      g_writer_write_unindent (writer, "</requires>\n");
    }

  for (l = node->members; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }

  if (node->node.type == G_IDL_NODE_OBJECT)
    {
      g_writer_write_unindent (writer, "</object>\n");
    }
  else if (node->node.type == G_IDL_NODE_INTERFACE)
    {
      g_writer_write_unindent (writer, "</interface>\n");
    }
}

static void
struct_generate (GIdlWriter * writer, GIdlNodeStruct * node)
{
  GList *l;
  char *markup =
    g_markup_printf_escaped ("<struct name=\"%s\">\n", node->node.name);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  for (l = node->members; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }
  g_writer_write_unindent (writer, "</struct>\n");
}

static void
union_generate (GIdlWriter * writer, GIdlNodeUnion * node)
{
  GList *l;
  char *markup =
    g_markup_printf_escaped ("<union name=\"%s\">\n", node->node.name);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  for (l = node->members; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }
  g_writer_write_unindent (writer, "</union>\n");
}

static void
boxed_generate (GIdlWriter * writer, GIdlNodeBoxed * node)
{
  GList *l;
  char *markup =
    g_markup_printf_escaped
    ("<boxed name=\"%s\" type-name=\"%s\" get-type=\"%s\">\n",
     node->node.name, node->gtype_name, node->gtype_init);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  for (l = node->members; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }
  g_writer_write_unindent (writer, "</boxed>\n");
}

static void
enum_generate (GIdlWriter * writer, GIdlNodeEnum * node)
{
  GList *l;
375
  GString *markup_s;
376 377 378 379 380 381 382 383 384 385 386
  char *markup;
  const char *tag_name = NULL;

  if (node->node.type == G_IDL_NODE_ENUM)
    {
      tag_name = "enum";
    }
  else if (node->node.type == G_IDL_NODE_FLAGS)
    {
      tag_name = "flags";
    }
387 388 389 390 391 392

  markup_s = g_string_new ("<");
  g_string_append_printf (markup_s,
			  "%s name=\"%s\"",
			  tag_name, node->node.name);

393 394 395 396 397 398 399 400 401 402
  if (node->gtype_name != NULL) {
    gchar *tmp = g_markup_printf_escaped (" type-name=\"%s\"", node->gtype_name);
    markup_s = g_string_append (markup_s, tmp);
    g_free (tmp);
  }
  if (node->gtype_init != NULL) {
    gchar *tmp = g_markup_printf_escaped (" get-type=\"%s\"", node->gtype_init);
    markup_s = g_string_append (markup_s, tmp);
    g_free(tmp);
  }
403 404

  if (node->deprecated)
405
    markup_s = g_string_append (markup_s, " deprecated=\"1\"");
406

407
  markup_s = g_string_append (markup_s, ">\n");
408 409 410

  g_writer_write_indent (writer, markup_s->str);
  g_string_free (markup_s, TRUE);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

  for (l = node->values; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }

  markup = g_strdup_printf ("</%s>\n", tag_name);
  g_writer_write_unindent (writer, markup);
  g_free (markup);
}

static void
node_generate (GIdlWriter * writer, GIdlNode * node)
{
  switch (node->type)
    {
    case G_IDL_NODE_FUNCTION:
    case G_IDL_NODE_CALLBACK:
      function_generate (writer, (GIdlNodeFunction *) node);
      break;
    case G_IDL_NODE_VFUNC:
      vfunc_generate (writer, (GIdlNodeVFunc *) node);
      break;
    case G_IDL_NODE_OBJECT:
    case G_IDL_NODE_INTERFACE:
      interface_generate (writer, (GIdlNodeInterface *) node);
      break;
    case G_IDL_NODE_STRUCT:
      struct_generate (writer, (GIdlNodeStruct *) node);
      break;
    case G_IDL_NODE_UNION:
      union_generate (writer, (GIdlNodeUnion *) node);
      break;
    case G_IDL_NODE_BOXED:
      boxed_generate (writer, (GIdlNodeBoxed *) node);
      break;
    case G_IDL_NODE_ENUM:
    case G_IDL_NODE_FLAGS:
      enum_generate (writer, (GIdlNodeEnum *) node);
      break;
    case G_IDL_NODE_PROPERTY:
      property_generate (writer, (GIdlNodeProperty *) node);
      break;
    case G_IDL_NODE_FIELD:
      field_generate (writer, (GIdlNodeField *) node);
      break;
    case G_IDL_NODE_SIGNAL:
      signal_generate (writer, (GIdlNodeSignal *) node);
      break;
    case G_IDL_NODE_VALUE:
      value_generate (writer, (GIdlNodeValue *) node);
      break;
    case G_IDL_NODE_CONSTANT:
      constant_generate (writer, (GIdlNodeConstant *) node);
      break;
    default:
      g_assert_not_reached ();
    }
}

static void
g_writer_write_module (GIdlWriter * writer, GIdlModule * module)
{
  GList *l;
  char *markup =
    g_markup_printf_escaped ("<namespace name=\"%s\">\n", module->name);
  g_writer_write_indent (writer, markup);
  g_free (markup);
  for (l = module->entries; l != NULL; l = l->next)
    {
      node_generate (writer, l->data);
    }
  g_writer_write_unindent (writer, "</namespace>\n");
}

void
g_idl_writer_save_file (GIdlModule *module,
			const gchar *filename)
{
  GIdlWriter *writer;

  writer = g_new0 (GIdlWriter, 1);
493

494 495 496 497
  if (!filename)
    writer->output = stdout;
  else
    writer->output = fopen (filename, "w");
498

499 500 501 502 503 504 505
  g_writer_write (writer, "<?xml version=\"1.0\"?>\n");
  g_writer_write_indent (writer, "<api version=\"1.0\">\n");
  g_writer_write_module (writer, module);
  g_writer_write_unindent (writer, "</api>\n");

  if (filename)
    fclose (writer->output);
506 507

  g_free (writer);
508
}