2e35d05044c5e6ee2ce5fd333e4ddf545a6d7f11
[platform/upstream/glib.git] / glib / gvariant-parser.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <glib.h>
26
27 #include "galias.h"
28
29 /*
30  * two-pass algorithm
31  * designed by ryan lortie and william hua
32  * designed in itb-229 and at ghazi's, 2009.
33  */
34
35 GQuark
36 g_variant_parser_get_error_quark (void)
37 {
38   static GQuark the_quark;
39
40   if (the_quark == 0)
41     the_quark = g_quark_from_static_string ("g-variant-parse-error-quark");
42
43   return the_quark;
44 }
45
46 typedef struct
47 {
48   gint start, end;
49 } SourceRef;
50
51 static void
52 parser_set_error_va (GError      **error,
53                      SourceRef    *location,
54                      SourceRef    *other,
55                      const gchar  *format,
56                      va_list       ap)
57 {
58   GString *msg = g_string_new (NULL);
59
60   if (location->start == location->end)
61     g_string_append_printf (msg, "%d", location->start);
62   else
63     g_string_append_printf (msg, "%d-%d", location->start, location->end);
64
65   if (other != NULL)
66     {
67       g_assert (other->start != other->end);
68       g_string_append_printf (msg, ",%d-%d", other->start, other->end);
69     }
70   g_string_append_c (msg, ':');
71
72   g_string_append_vprintf (msg, format, ap);
73   g_set_error_literal (error, G_VARIANT_PARSE_ERROR, 0, msg->str);
74   g_string_free (msg, TRUE);
75 }
76
77 static void
78 parser_set_error (GError      **error,
79                   SourceRef    *location,
80                   SourceRef    *other,
81                   const gchar  *format,
82                   ...)
83 {
84   va_list ap;
85
86   va_start (ap, format);
87   parser_set_error_va (error, location, other, format, ap);
88   va_end (ap);
89 }
90
91 typedef struct
92 {
93   const gchar *start;
94   const gchar *stream;
95   const gchar *end;
96
97   const gchar *this;
98 } TokenStream;
99
100
101 static void
102 token_stream_set_error (TokenStream  *stream,
103                         GError      **error,
104                         gboolean      this_token,
105                         const gchar  *format,
106                         ...)
107 {
108   SourceRef ref;
109   va_list ap;
110
111   ref.start = stream->this - stream->start;
112
113   if (this_token)
114     ref.end = stream->stream - stream->start;
115   else
116     ref.end = ref.start;
117
118   va_start (ap, format);
119   parser_set_error_va (error, &ref, NULL, format, ap);
120   va_end (ap);
121 }
122
123 static void
124 token_stream_prepare (TokenStream *stream)
125 {
126   gint brackets = 0;
127   const gchar *end;
128
129   if (stream->this != NULL)
130     return;
131
132   while (stream->stream != stream->end && g_ascii_isspace (*stream->stream))
133     stream->stream++;
134
135   if (stream->stream == stream->end || *stream->stream == '\0')
136     {
137       stream->this = stream->stream;
138       return;
139     }
140
141   switch (stream->stream[0])
142     {
143     case '-': case '+': case '.': case '0': case '1': case '2':
144     case '3': case '4': case '5': case '6': case '7': case '8':
145     case '9':
146       for (end = stream->stream; end != stream->end; end++)
147         if (!g_ascii_isalnum (*end) &&
148             *end != '-' && *end != '+' && *end != '.')
149           break;
150       break;
151
152     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
153     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
154     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
155     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
156     case 'y': case 'z':
157       for (end = stream->stream; end != stream->end; end++)
158         if (!g_ascii_isalnum (*end))
159           break;
160       break;
161
162     case '@': case '%':
163       /* stop at the first space, comma or unmatched bracket.
164        * deals nicely with cases like (%i, %i).
165        */
166       for (end = stream->stream + 1;
167            end != stream->end && *end != ',' && !g_ascii_isspace (*end);
168            end++)
169
170         if (*end == '(' || *end == '{')
171           brackets++;
172
173         else if ((*end == ')' || *end == '}') && !brackets--)
174           break;
175
176       break;
177
178     case '\'': case '"':
179       for (end = stream->stream + 1; end != stream->end; end++)
180         if (*end == stream->stream[0] || *end == '\0' ||
181             (*end == '\\' && (++end == stream->end || *end == '\0')))
182           break;
183
184       if (end != stream->end && *end)
185         end++;
186       break;
187
188     default:
189       end = stream->stream + 1;
190       break;
191     }
192
193   stream->this = stream->stream;
194   stream->stream = end;
195 }
196
197 static void
198 token_stream_next (TokenStream *stream)
199 {
200   stream->this = NULL;
201 }
202
203 static gboolean
204 token_stream_peek (TokenStream *stream,
205                    gchar        first_char)
206 {
207   token_stream_prepare (stream);
208
209   return stream->this[0] == first_char;
210 }
211
212 static gboolean
213 token_stream_is_keyword (TokenStream *stream)
214 {
215   token_stream_prepare (stream);
216
217   return g_ascii_isalpha (stream->this[0]);
218 }
219
220 static gboolean
221 token_stream_is_numeric (TokenStream *stream)
222 {
223   token_stream_prepare (stream);
224
225   return (g_ascii_isdigit (stream->this[0]) ||
226           stream->this[0] == '-' ||
227           stream->this[0] == '+' ||
228           stream->this[0] == '.');
229 }
230
231 static gboolean
232 token_stream_consume (TokenStream *stream,
233                       const gchar *token)
234 {
235   gint length = strlen (token);
236
237   token_stream_prepare (stream);
238
239   if (stream->stream - stream->this == length &&
240       memcmp (stream->this, token, length) == 0)
241     {
242       token_stream_next (stream);
243       return TRUE;
244     }
245
246   return FALSE;
247 }
248
249 static gboolean
250 token_stream_require (TokenStream  *stream,
251                       const gchar  *token,
252                       const gchar  *purpose,
253                       GError      **error)
254 {
255
256   if (!token_stream_consume (stream, token))
257     {
258       token_stream_set_error (stream, error, FALSE,
259                               "expected `%s'%s", token, purpose);
260       return FALSE;
261     }
262
263   return TRUE;
264 }
265
266 static void
267 token_stream_assert (TokenStream *stream,
268                      const gchar *token)
269 {
270   gboolean correct_token;
271
272   correct_token = token_stream_consume (stream, token);
273   g_assert (correct_token);
274 }
275
276 static gchar *
277 token_stream_get (TokenStream *stream)
278 {
279   gchar *result;
280
281   token_stream_prepare (stream);
282
283   result = g_strndup (stream->this, stream->stream - stream->this);
284
285   return result;
286 }
287
288 static void
289 token_stream_start_ref (TokenStream *stream,
290                         SourceRef   *ref)
291 {
292   token_stream_prepare (stream);
293   ref->start = stream->this - stream->start;
294 }
295
296 static void
297 token_stream_end_ref (TokenStream *stream,
298                       SourceRef   *ref)
299 {
300   ref->end = stream->stream - stream->start;
301 }
302
303 void
304 pattern_copy (gchar       **out,
305               const gchar **in)
306 {
307   gint brackets = 0;
308
309   while (**in == 'a' || **in == 'm' || **in == 'M')
310     *(*out)++ = *(*in)++;
311
312   do
313     {
314       if (**in == '(' || **in == '{')
315         brackets++;
316
317       else if (**in == ')' || **in == '}')
318         brackets--;
319
320       *(*out)++ = *(*in)++;
321     }
322   while (brackets);
323 }
324
325 static gchar *
326 pattern_coalesce (const gchar *left,
327                   const gchar *right)
328 {
329   gchar *result;
330   gchar *out;
331
332   /* the length of the output is loosely bound by the sum of the input
333    * lengths, not simply the greater of the two lengths.
334    *
335    *   (*(iii)) + ((iii)*) ((iii)(iii))
336    *
337    *      8     +    8    =  12
338    */
339   out = result = g_malloc (strlen (left) + strlen (right));
340
341   while (*left && *right)
342     {
343       if (*left == *right)
344         {
345           *out++ = *left++;
346           right++;
347         }
348
349       else
350         {
351           const gchar **one = &left, **the_other = &right;
352
353          again:
354           if (**one == '*' && **the_other != ')')
355             {
356               pattern_copy (&out, the_other);
357               (*one)++;
358             }
359
360           else if (**one == 'M' && **the_other == 'm')
361             {
362               *out++ = *(*the_other)++;
363             }
364
365           else if (**one == 'M' && **the_other != 'm')
366             {
367               (*one)++;
368             }
369
370           else if (**one == 'N' && strchr ("ynqiuxthd", **the_other))
371             {
372               *out++ = *(*the_other)++;
373               (*one)++;
374             }
375
376           else if (**one == 'S' && strchr ("sog", **the_other))
377             {
378               *out++ = *(*the_other)++;
379               (*one)++;
380             }
381
382           else if (one == &left)
383             {
384               one = &right, the_other = &left;
385               goto again;
386             }
387
388           else
389             break;
390         }
391     }
392
393   if (*left || *right)
394     {
395       g_free (result);
396       result = NULL;
397     }
398   else
399     *out++ = '\0';
400
401   return result;
402 }
403
404 typedef struct _AST AST;
405 typedef gchar *    (*get_pattern_func)    (AST                 *ast,
406                                            GError             **error);
407 typedef GVariant * (*get_value_func)      (AST                 *ast,
408                                            const GVariantType  *type,
409                                            GError             **error);
410 typedef GVariant * (*get_base_value_func) (AST                 *ast,
411                                            const GVariantType  *type,
412                                            GError             **error);
413 typedef void       (*free_func)           (AST                 *ast);
414
415 typedef struct
416 {
417   gchar *    (* get_pattern)    (AST                 *ast,
418                                  GError             **error);
419   GVariant * (* get_value)      (AST                 *ast,
420                                  const GVariantType  *type,
421                                  GError             **error);
422   GVariant * (* get_base_value) (AST                 *ast,
423                                  const GVariantType  *type,
424                                  GError             **error);
425   void       (* free)           (AST                 *ast);
426 } ASTClass;
427
428 struct _AST
429 {
430   const ASTClass *class;
431   SourceRef source_ref;
432 };
433
434 static gchar *
435 ast_get_pattern (AST     *ast,
436                  GError **error)
437 {
438   return ast->class->get_pattern (ast, error);
439 }
440
441 static GVariant *
442 ast_get_value (AST                 *ast,
443                const GVariantType  *type,
444                GError             **error)
445 {
446   return ast->class->get_value (ast, type, error);
447 }
448
449 static void
450 ast_free (AST *ast)
451 {
452   ast->class->free (ast);
453 }
454
455 static void
456 ast_set_error (AST          *ast,
457                GError      **error,
458                AST          *other_ast,
459                const gchar  *format,
460                ...)
461 {
462   va_list ap;
463
464   va_start (ap, format);
465   parser_set_error_va (error, &ast->source_ref,
466                        other_ast ? & other_ast->source_ref : NULL,
467                        format, ap);
468   va_end (ap);
469 }
470
471 static GVariant *
472 ast_type_error (AST                 *ast,
473                 const GVariantType  *type,
474                 GError             **error)
475 {
476   gchar *typestr;
477
478   typestr = g_variant_type_dup_string (type);
479   ast_set_error (ast, error, NULL,
480                  "can not parse as value of type `%s'",
481                  typestr);
482   g_free (typestr);
483
484   return NULL;
485 }
486
487 static GVariant *
488 ast_resolve (AST     *ast,
489              GError **error)
490 {
491   GVariant *value;
492   gchar *pattern;
493   gint i, j = 0;
494
495   pattern = ast_get_pattern (ast, error);
496
497   if (pattern == NULL)
498     return NULL;
499
500   /* choose reasonable defaults
501    *
502    *   1) favour non-maybe values where possible
503    *   2) default type for strings is 's'
504    *   3) default type for integers is 'i'
505    */
506   for (i = 0; pattern[i]; i++)
507     switch (pattern[i])
508       {
509       case '*':
510         ast_set_error (ast, error, NULL, "unable to infer type");
511         g_free (pattern);
512         return NULL;
513
514       case 'M':
515         break;
516
517       case 'S':
518         pattern[j++] = 's';
519         break;
520
521       case 'N':
522         pattern[j++] = 'i';
523         break;
524
525       default:
526         pattern[j++] = pattern[i];
527         break;
528       }
529   pattern[j++] = '\0';
530
531   value = ast_get_value (ast, G_VARIANT_TYPE (pattern), error);
532   g_free (pattern);
533
534   return value;
535 }
536
537
538 static AST *parse (TokenStream  *stream,
539                    va_list      *app,
540                    GError      **error);
541
542 static void
543 ast_array_append (AST  ***array,
544                   gint   *n_items,
545                   AST    *ast)
546 {
547   if ((*n_items & (*n_items - 1)) == 0)
548     *array = g_renew (AST *, *array, *n_items ? 2 ** n_items : 1);
549
550   (*array)[(*n_items)++] = ast;
551 }
552
553 static void
554 ast_array_free (AST  **array,
555                 gint   n_items)
556 {
557   gint i;
558
559   for (i = 0; i < n_items; i++)
560     ast_free (array[i]);
561   g_free (array);
562 }
563
564 static gchar *
565 ast_array_get_pattern (AST    **array,
566                        gint     n_items,
567                        GError **error)
568 {
569   gchar *pattern;
570   gint i;
571
572   pattern = ast_get_pattern (array[0], error);
573
574   if (pattern == NULL)
575     return NULL;
576
577   for (i = 1; i < n_items; i++)
578     {
579       gchar *tmp, *merged;
580
581       tmp = ast_get_pattern (array[i], error);
582
583       if (tmp == NULL)
584         {
585           g_free (pattern);
586           return NULL;
587         }
588
589       merged = pattern_coalesce (pattern, tmp);
590       g_free (pattern);
591       pattern = merged;
592
593       if (merged == NULL)
594         /* set coalescence implies pairwise coalescence (i think).
595          * we should therefore be able to trace the failure to a single
596          * pair of values.
597          */
598         {
599           int j = 0;
600
601           while (TRUE)
602             {
603               gchar *tmp2;
604               gchar *m;
605
606               /* if 'j' reaches 'i' then we failed to find the pair */
607               g_assert (j < i);
608
609               tmp2 = ast_get_pattern (array[j], NULL);
610               g_assert (tmp2 != NULL);
611
612               m = pattern_coalesce (tmp, tmp2);
613               g_free (tmp2);
614               g_free (m);
615
616               if (m == NULL)
617                 {
618                   /* we found a conflict between 'i' and 'j'.
619                    *
620                    * report the error.  note: 'j' is first.
621                    */
622                   ast_set_error (array[j], error, array[i],
623                                  "unable to find a common type");
624                   g_free (tmp);
625                   return NULL;
626                 }
627
628               j++;
629             }
630         }
631     }
632
633   return pattern;
634 }
635
636 typedef struct
637 {
638   AST ast;
639
640   AST *child;
641 } Maybe;
642
643 static gchar *
644 maybe_get_pattern (AST     *ast,
645                    GError **error)
646 {
647   Maybe *maybe = (Maybe *) ast;
648
649   if (maybe->child != NULL)
650     {
651       gchar *child_pattern;
652       gchar *pattern;
653
654       child_pattern = ast_get_pattern (maybe->child, error);
655
656       if (child_pattern == NULL)
657         return NULL;
658
659       pattern = g_strdup_printf ("m%s", child_pattern);
660       g_free (child_pattern);
661
662       return pattern;
663     }
664
665   return g_strdup ("m*");
666 }
667
668 static GVariant *
669 maybe_get_value (AST                 *ast,
670                  const GVariantType  *type,
671                  GError             **error)
672 {
673   Maybe *maybe = (Maybe *) ast;
674   GVariant *value;
675
676   if (!g_variant_type_is_maybe (type))
677     return ast_type_error (ast, type, error);
678
679   type = g_variant_type_element (type);
680
681   if (maybe->child)
682     {
683       value = ast_get_value (maybe->child, type, error);
684
685       if (value == NULL)
686         return NULL;
687     }
688   else
689     value = NULL;
690
691   return g_variant_new_maybe (type, value);
692 }
693
694 static void
695 maybe_free (AST *ast)
696 {
697   Maybe *maybe = (Maybe *) ast;
698
699   if (maybe->child != NULL)
700     ast_free (maybe->child);
701
702   g_slice_free (Maybe, maybe);
703 }
704
705 static AST *
706 maybe_parse (TokenStream  *stream,
707              va_list      *app,
708              GError      **error)
709 {
710   static const ASTClass maybe_class = {
711     maybe_get_pattern,
712     maybe_get_value, NULL,
713     maybe_free
714   };
715   AST *child = NULL;
716   Maybe *maybe;
717
718   if (token_stream_consume (stream, "just"))
719     {
720       child = parse (stream, app, error);
721       if (child == NULL)
722         return NULL;
723     }
724
725   else if (!token_stream_consume (stream, "nothing"))
726     {
727       token_stream_set_error (stream, error, TRUE, "unknown keyword");
728       return NULL;
729     }
730
731   maybe = g_slice_new (Maybe);
732   maybe->ast.class = &maybe_class;
733   maybe->child = child;
734
735   return (AST *) maybe;
736 }
737
738 static GVariant *
739 maybe_wrapper (AST                 *ast,
740                const GVariantType  *type,
741                GError             **error)
742 {
743   const GVariantType *t;
744   GVariant *value;
745   int depth;
746
747   for (depth = 0, t = type;
748        g_variant_type_is_maybe (t);
749        depth++, t = g_variant_type_element (t));
750
751   value = ast->class->get_base_value (ast, t, error);
752
753   if (value == NULL)
754     return NULL;
755
756   while (depth--)
757     value = g_variant_new_maybe (NULL, value);
758
759   return value;
760 }
761
762 typedef struct
763 {
764   AST ast;
765
766   AST **children;
767   gint n_children;
768 } Array;
769
770 static gchar *
771 array_get_pattern (AST     *ast,
772                    GError **error)
773 {
774   Array *array = (Array *) ast;
775   gchar *pattern;
776   gchar *result;
777
778   if (array->n_children == 0)
779     return g_strdup ("Ma*");
780
781   pattern = ast_array_get_pattern (array->children, array->n_children, error);
782
783   if (pattern == NULL)
784     return NULL;
785
786   result = g_strdup_printf ("Ma%s", pattern);
787   g_free (pattern);
788
789   return result;
790 }
791
792 static GVariant *
793 array_get_value (AST                 *ast,
794                  const GVariantType  *type,
795                  GError             **error)
796 {
797   Array *array = (Array *) ast;
798   const GVariantType *childtype;
799   GVariantBuilder builder;
800   gint i;
801
802   if (!g_variant_type_is_array (type))
803     return ast_type_error (ast, type, error);
804
805   g_variant_builder_init (&builder, type);
806   childtype = g_variant_type_element (type);
807
808   for (i = 0; i < array->n_children; i++)
809     {
810       GVariant *child;
811
812       if (!(child = ast_get_value (array->children[i], childtype, error)))
813         {
814           g_variant_builder_clear (&builder);
815           return NULL;
816         }
817
818       g_variant_builder_add_value (&builder, child);
819     }
820
821   return g_variant_builder_end (&builder);
822 }
823
824 static void
825 array_free (AST *ast)
826 {
827   Array *array = (Array *) ast;
828
829   ast_array_free (array->children, array->n_children);
830   g_slice_free (Array, array);
831 }
832
833 static AST *
834 array_parse (TokenStream  *stream,
835              va_list      *app,
836              GError      **error)
837 {
838   static const ASTClass array_class = {
839     array_get_pattern,
840     maybe_wrapper, array_get_value,
841     array_free
842   };
843   gboolean need_comma = FALSE;
844   Array *array;
845
846   array = g_slice_new (Array);
847   array->ast.class = &array_class;
848   array->children = NULL;
849   array->n_children = 0;
850
851   token_stream_assert (stream, "[");
852   while (!token_stream_consume (stream, "]"))
853     {
854       AST *child;
855
856       if (need_comma &&
857           !token_stream_require (stream, ",",
858                                  " or `]' to follow array element",
859                                  error))
860         goto error;
861
862       child = parse (stream, app, error);
863
864       if (!child)
865         goto error;
866
867       ast_array_append (&array->children, &array->n_children, child);
868       need_comma = TRUE;
869     }
870
871   return (AST *) array;
872
873  error:
874   ast_array_free (array->children, array->n_children);
875   g_slice_free (Array, array);
876
877   return NULL;
878 }
879
880 typedef struct
881 {
882   AST ast;
883
884   AST **children;
885   gint n_children;
886 } Tuple;
887
888 static gchar *
889 tuple_get_pattern (AST     *ast,
890                    GError **error)
891 {
892   Tuple *tuple = (Tuple *) ast;
893   gchar *result = NULL;
894   gchar **parts;
895   gint i;
896
897   parts = g_new (gchar *, tuple->n_children + 4);
898   parts[tuple->n_children + 1] = (gchar *) ")";
899   parts[tuple->n_children + 2] = NULL;
900   parts[0] = (gchar *) "M(";
901
902   for (i = 0; i < tuple->n_children; i++)
903     if (!(parts[i + 1] = ast_get_pattern (tuple->children[i], error)))
904       break;
905
906   if (i == tuple->n_children)
907     result = g_strjoinv ("", parts);
908
909   /* parts[0] should not be freed */
910   while (i)
911     g_free (parts[i--]);
912   g_free (parts);
913
914   return result;
915 }
916
917 static GVariant *
918 tuple_get_value (AST                 *ast,
919                  const GVariantType  *type,
920                  GError             **error)
921 {
922   Tuple *tuple = (Tuple *) ast;
923   const GVariantType *childtype;
924   GVariantBuilder builder;
925   gint i;
926
927   if (!g_variant_type_is_tuple (type))
928     return ast_type_error (ast, type, error);
929
930   g_variant_builder_init (&builder, type);
931   childtype = g_variant_type_first (type);
932
933   for (i = 0; i < tuple->n_children; i++)
934     {
935       GVariant *child;
936
937       if (!(child = ast_get_value (tuple->children[i], childtype, error)))
938         {
939           g_variant_builder_clear (&builder);
940           return FALSE;
941         }
942
943       g_variant_builder_add_value (&builder, child);
944       childtype = g_variant_type_next (childtype);
945     }
946
947   return g_variant_builder_end (&builder);
948 }
949
950 static void
951 tuple_free (AST *ast)
952 {
953   Tuple *tuple = (Tuple *) ast;
954
955   ast_array_free (tuple->children, tuple->n_children);
956   g_slice_free (Tuple, tuple);
957 }
958
959 static AST *
960 tuple_parse (TokenStream  *stream,
961              va_list      *app,
962              GError      **error)
963 {
964   static const ASTClass tuple_class = {
965     tuple_get_pattern,
966     maybe_wrapper, tuple_get_value,
967     tuple_free
968   };
969   gboolean need_comma = FALSE;
970   gboolean first = TRUE;
971   Tuple *tuple;
972
973   tuple = g_slice_new (Tuple);
974   tuple->ast.class = &tuple_class;
975   tuple->children = NULL;
976   tuple->n_children = 0;
977
978   token_stream_assert (stream, "(");
979   while (!token_stream_consume (stream, ")"))
980     {
981       AST *child;
982
983       if (need_comma &&
984           !token_stream_require (stream, ",",
985                                  " or `)' to follow tuple element",
986                                  error))
987         goto error;
988
989       child = parse (stream, app, error);
990
991       if (!child)
992         goto error;
993
994       ast_array_append (&tuple->children, &tuple->n_children, child);
995
996       /* the first time, we absolutely require a comma, so grab it here
997        * and leave need_comma = FALSE so that the code above doesn't
998        * require a second comma.
999        *
1000        * the second and remaining times, we set need_comma = TRUE.
1001        */
1002       if (first)
1003         {
1004           if (!token_stream_require (stream, ",",
1005                                      " after first tuple element", error))
1006             goto error;
1007
1008           first = FALSE;
1009         }
1010       else
1011         need_comma = TRUE;
1012     }
1013
1014   return (AST *) tuple;
1015
1016  error:
1017   ast_array_free (tuple->children, tuple->n_children);
1018   g_slice_free (Tuple, tuple);
1019
1020   return NULL;
1021 }
1022
1023 typedef struct
1024 {
1025   AST ast;
1026
1027   AST *value;
1028 } Variant;
1029
1030 static gchar *
1031 variant_get_pattern (AST     *ast,
1032                      GError **error)
1033 {
1034   return g_strdup ("Mv");
1035 }
1036
1037 static GVariant *
1038 variant_get_value (AST                 *ast,
1039                    const GVariantType  *type,
1040                    GError             **error)
1041 {
1042   Variant *variant = (Variant *) ast;
1043   GVariant *child;
1044
1045   g_assert (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT));
1046   child = ast_resolve (variant->value, error);
1047
1048   if (child == NULL)
1049     return NULL;
1050
1051   return g_variant_new_variant (child);
1052 }
1053
1054 static void
1055 variant_free (AST *ast)
1056 {
1057   Variant *variant = (Variant *) ast;
1058
1059   ast_free (variant->value);
1060   g_slice_free (Variant, variant);
1061 }
1062
1063 static AST *
1064 variant_parse (TokenStream  *stream,
1065                va_list      *app,
1066                GError      **error)
1067 {
1068   static const ASTClass variant_class = {
1069     variant_get_pattern,
1070     maybe_wrapper, variant_get_value,
1071     variant_free
1072   };
1073   Variant *variant;
1074   AST *value;
1075
1076   token_stream_assert (stream, "<");
1077   value = parse (stream, app, error);
1078
1079   if (!value)
1080     return NULL;
1081
1082   if (!token_stream_require (stream, ">", " to follow variant value", error))
1083     {
1084       ast_free (value);
1085       return NULL;
1086     }
1087
1088   variant = g_slice_new (Variant);
1089   variant->ast.class = &variant_class;
1090   variant->value = value;
1091
1092   return (AST *) variant;
1093 }
1094
1095 typedef struct
1096 {
1097   AST ast;
1098
1099   AST **keys;
1100   AST **values;
1101   gint n_children;
1102 } Dictionary;
1103
1104 static gchar *
1105 dictionary_get_pattern (AST     *ast,
1106                         GError **error)
1107 {
1108   Dictionary *dict = (Dictionary *) ast;
1109   gchar *value_pattern;
1110   gchar *key_pattern;
1111   gchar key_char;
1112   gchar *result;
1113
1114   if (dict->n_children == 0)
1115     return g_strdup ("Ma{**}");
1116
1117   key_pattern = ast_array_get_pattern (dict->keys,
1118                                        abs (dict->n_children),
1119                                        error);
1120
1121   if (key_pattern == NULL)
1122     return NULL;
1123
1124   /* we can not have maybe keys */
1125   if (key_pattern[0] == 'M')
1126     key_char = key_pattern[1];
1127   else
1128     key_char = key_pattern[0];
1129
1130   g_free (key_pattern);
1131
1132   /* the basic types,
1133    * plus undetermined number type and undetermined string type.
1134    */
1135   if (!strchr ("bynqiuxthdsogNS", key_char))
1136     {
1137       ast_set_error (ast, error, NULL,
1138                      "dictionary keys must have basic types");
1139       return NULL;
1140     }
1141
1142   value_pattern = ast_get_pattern (dict->values[0], error);
1143
1144   if (value_pattern == NULL)
1145     return NULL;
1146
1147   result = g_strdup_printf ("M%s{%c%s}",
1148                             dict->n_children > 0 ? "a" : "",
1149                             key_char, value_pattern);
1150   g_free (value_pattern);
1151
1152   return result;
1153 }
1154
1155 static GVariant *
1156 dictionary_get_value (AST                 *ast,
1157                       const GVariantType  *type,
1158                       GError             **error)
1159 {
1160   Dictionary *dict = (Dictionary *) ast;
1161
1162   if (dict->n_children == -1)
1163     {
1164       const GVariantType *subtype;
1165       GVariantBuilder builder;
1166       GVariant *subvalue;
1167
1168       if (!g_variant_type_is_dict_entry (type))
1169         return ast_type_error (ast, type, error);
1170
1171       g_variant_builder_init (&builder, type);
1172
1173       subtype = g_variant_type_key (type);
1174       if (!(subvalue = ast_get_value (dict->keys[0], subtype, error)))
1175         {
1176           g_variant_builder_clear (&builder);
1177           return FALSE;
1178         }
1179       g_variant_builder_add_value (&builder, subvalue);
1180
1181       subtype = g_variant_type_value (type);
1182       if (!(subvalue = ast_get_value (dict->values[0], subtype, error)))
1183         {
1184           g_variant_builder_clear (&builder);
1185           return FALSE;
1186         }
1187       g_variant_builder_add_value (&builder, subvalue);
1188
1189       return g_variant_builder_end (&builder);
1190     }
1191   else
1192     {
1193       const GVariantType *entry, *key, *val;
1194       GVariantBuilder builder;
1195       gint i;
1196
1197       if (!g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_DICTIONARY))
1198         return ast_type_error (ast, type, error);
1199
1200       entry = g_variant_type_element (type);
1201       key = g_variant_type_key (entry);
1202       val = g_variant_type_value (entry);
1203
1204       g_variant_builder_init (&builder, type);
1205
1206       for (i = 0; i < dict->n_children; i++)
1207         {
1208           GVariant *subvalue;
1209
1210           g_variant_builder_open (&builder, entry);
1211
1212           if (!(subvalue = ast_get_value (dict->keys[i], key, error)))
1213             {
1214               g_variant_builder_clear (&builder);
1215               return FALSE;
1216             }
1217           g_variant_builder_add_value (&builder, subvalue);
1218
1219           if (!(subvalue = ast_get_value (dict->values[i], val, error)))
1220             {
1221               g_variant_builder_clear (&builder);
1222               return FALSE;
1223             }
1224           g_variant_builder_add_value (&builder, subvalue);
1225           g_variant_builder_close (&builder);
1226         }
1227
1228       return g_variant_builder_end (&builder);
1229     }
1230 }
1231
1232 static void
1233 dictionary_free (AST *ast)
1234 {
1235   Dictionary *dict = (Dictionary *) ast;
1236   gint n_children;
1237
1238   if (dict->n_children > -1)
1239     n_children = dict->n_children;
1240   else
1241     n_children = 1;
1242
1243   ast_array_free (dict->keys, n_children);
1244   ast_array_free (dict->values, n_children);
1245   g_slice_free (Dictionary, dict);
1246 }
1247
1248 static AST *
1249 dictionary_parse (TokenStream  *stream,
1250                   va_list      *app,
1251                   GError      **error)
1252 {
1253   static const ASTClass dictionary_class = {
1254     dictionary_get_pattern,
1255     maybe_wrapper, dictionary_get_value,
1256     dictionary_free
1257   };
1258   gint n_keys, n_values;
1259   gboolean only_one;
1260   Dictionary *dict;
1261   AST *first;
1262
1263   dict = g_slice_new (Dictionary);
1264   dict->ast.class = &dictionary_class;
1265   dict->keys = NULL;
1266   dict->values = NULL;
1267   n_keys = n_values = 0;
1268
1269   token_stream_assert (stream, "{");
1270
1271   if (token_stream_consume (stream, "}"))
1272     {
1273       dict->n_children = 0;
1274       return (AST *) dict;
1275     }
1276
1277   if ((first = parse (stream, app, error)) == NULL)
1278     goto error;
1279
1280   ast_array_append (&dict->keys, &n_keys, first);
1281
1282   only_one = token_stream_consume (stream, ",");
1283   if (!only_one &&
1284       !token_stream_require (stream, ":",
1285                              " or `,' to follow dictionary entry key",
1286                              error))
1287     goto error;
1288
1289   if ((first = parse (stream, app, error)) == NULL)
1290     goto error;
1291
1292   ast_array_append (&dict->values, &n_values, first);
1293
1294   if (only_one)
1295     {
1296       if (!token_stream_require (stream, "}", " at end of dictionary entry",
1297                                  error))
1298         goto error;
1299
1300       g_assert (n_keys == 1 && n_values == 1);
1301       dict->n_children = -1;
1302
1303       return (AST *) dict;
1304     }
1305
1306   while (!token_stream_consume (stream, "}"))
1307     {
1308       AST *child;
1309
1310       if (!token_stream_require (stream, ",",
1311                                  " or `}' to follow dictionary entry", error))
1312         goto error;
1313
1314       child = parse (stream, app, error);
1315
1316       if (!child)
1317         goto error;
1318
1319       ast_array_append (&dict->keys, &n_keys, child);
1320
1321       if (!token_stream_require (stream, ":",
1322                                  " to follow dictionary entry key", error))
1323         goto error;
1324
1325       child = parse (stream, app, error);
1326
1327       if (!child)
1328         goto error;
1329
1330       ast_array_append (&dict->values, &n_values, child);
1331     }
1332
1333   g_assert (n_keys == n_values);
1334   dict->n_children = n_keys;
1335
1336   return (AST *) dict;
1337
1338  error:
1339   ast_array_free (dict->keys, n_keys);
1340   ast_array_free (dict->values, n_values);
1341   g_slice_free (Dictionary, dict);
1342
1343   return NULL;
1344 }
1345
1346 typedef struct
1347 {
1348   AST ast;
1349   gchar *string;
1350 } String;
1351
1352 static gchar *
1353 string_get_pattern (AST     *ast,
1354                     GError **error)
1355 {
1356   return g_strdup ("MS");
1357 }
1358
1359 static GVariant *
1360 string_get_value (AST                 *ast,
1361                   const GVariantType  *type,
1362                   GError             **error)
1363 {
1364   String *string = (String *) ast;
1365
1366   if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
1367     return g_variant_new_string (string->string);
1368
1369   else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
1370     {
1371       if (!g_variant_is_object_path (string->string))
1372         {
1373           ast_set_error (ast, error, NULL, "not a valid object path");
1374           return NULL;
1375         }
1376
1377       return g_variant_new_object_path (string->string);
1378     }
1379
1380   else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
1381     {
1382       if (!g_variant_is_signature (string->string))
1383         {
1384           ast_set_error (ast, error, NULL, "not a valid signature");
1385           return NULL;
1386         }
1387
1388       return g_variant_new_signature (string->string);
1389     }
1390
1391   else
1392     return ast_type_error (ast, type, error);
1393 }
1394
1395 static void
1396 string_free (AST *ast)
1397 {
1398   String *string = (String *) ast;
1399
1400   g_free (string->string);
1401   g_slice_free (String, string);
1402 }
1403
1404 static AST *
1405 string_parse (TokenStream  *stream,
1406               va_list      *app,
1407               GError      **error)
1408 {
1409   static const ASTClass string_class = {
1410     string_get_pattern,
1411     maybe_wrapper, string_get_value,
1412     string_free
1413   };
1414   String *string;
1415   SourceRef ref;
1416   gchar *token;
1417   gsize length;
1418   gchar quote;
1419   gchar *str;
1420   gint i, j;
1421
1422   token_stream_start_ref (stream, &ref);
1423   token = token_stream_get (stream);
1424   token_stream_end_ref (stream, &ref);
1425   length = strlen (token);
1426   quote = token[0];
1427
1428   str = g_malloc (length);
1429   g_assert (quote == '"' || quote == '\'');
1430   j = 0;
1431   i = 1;
1432   while (token[i] != quote)
1433     switch (token[i])
1434       {
1435       case '\0':
1436         parser_set_error (error, &ref, NULL,
1437                           "unterminated string constant");
1438         g_free (token);
1439         return NULL;
1440
1441       case '\\':
1442         switch (token[++i])
1443           {
1444           case '\0':
1445             parser_set_error (error, &ref, NULL,
1446                               "unterminated string constant");
1447             g_free (token);
1448             return NULL;
1449
1450           case '0': case '1': case '2': case '3':
1451           case '4': case '5': case '6': case '7':
1452             {
1453               /* up to 3 characters */
1454               guchar val = token[i++] - '0';
1455
1456               if ('0' <= token[i] && token[i] < '8')
1457                 val = (val << 3) | (token[i++] - '0');
1458
1459               if ('0' <= token[i] && token[i] < '8')
1460                 val = (val << 3) | (token[i++] - '0');
1461
1462               str[j++] = val;
1463             }
1464             continue;
1465
1466           case 'b': str[j++] = '\b'; i++; continue;
1467           case 'f': str[j++] = '\f'; i++; continue;
1468           case 'n': str[j++] = '\n'; i++; continue;
1469           case 'r': str[j++] = '\r'; i++; continue;
1470           case 't': str[j++] = '\t'; i++; continue;
1471           case '\n': i++; continue;
1472           }
1473
1474       default:
1475         str[j++] = token[i++];
1476       }
1477   str[j++] = '\0';
1478   g_free (token);
1479
1480   string = g_slice_new (String);
1481   string->ast.class = &string_class;
1482   string->string = str;
1483
1484   token_stream_next (stream);
1485
1486   return (AST *) string;
1487 }
1488
1489 typedef struct
1490 {
1491   AST ast;
1492
1493   gchar *token;
1494 } Number;
1495
1496 static gchar *
1497 number_get_pattern (AST     *ast,
1498                     GError **error)
1499 {
1500   Number *number = (Number *) ast;
1501
1502   if (strchr (number->token, '.') ||
1503       (!g_str_has_prefix (number->token, "0x") &&
1504        strchr (number->token, 'e')))
1505     return g_strdup ("Md");
1506
1507   return g_strdup ("MN");
1508 }
1509
1510 static GVariant *
1511 number_overflow (AST                 *ast,
1512                  const GVariantType  *type,
1513                  GError             **error)
1514 {
1515   ast_set_error (ast, error, NULL, "number out of range for type `%c'",
1516                  g_variant_type_peek_string (type)[0]);
1517   return NULL;
1518 }
1519
1520 static GVariant *
1521 number_get_value (AST                 *ast,
1522                   const GVariantType  *type,
1523                   GError             **error)
1524 {
1525   Number *number = (Number *) ast;
1526   const gchar *token;
1527   gboolean negative;
1528   gboolean floating;
1529   guint64 abs_val;
1530   gdouble dbl_val;
1531   gchar *end;
1532
1533   token = number->token;
1534
1535   if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
1536     {
1537       floating = TRUE;
1538
1539       errno = 0;
1540       dbl_val = g_ascii_strtod (token, &end);
1541       if (dbl_val != 0.0 && errno == ERANGE)
1542         {
1543           ast_set_error (ast, error, NULL, "number too big for any type");
1544           return NULL;
1545         }
1546     }
1547   else
1548     {
1549       floating = FALSE;
1550       negative = token[0] == '-';
1551       if (token[0] == '-')
1552         token++;
1553
1554       errno = 0;
1555       abs_val = g_ascii_strtoull (token, &end, 0);
1556       if (abs_val == G_MAXUINT64 && errno == ERANGE)
1557         {
1558           ast_set_error (ast, error, NULL, "integer too big for any type");
1559           return NULL;
1560         }
1561
1562       if (abs_val == 0)
1563         negative = FALSE;
1564     }
1565
1566   if (*end != '\0')
1567     {
1568       SourceRef ref;
1569
1570       ref = ast->source_ref;
1571       ref.start += end - number->token;
1572       ref.end = ref.start + 1;
1573
1574       parser_set_error (error, &ref, NULL,
1575                         "invalid character in number");
1576       return NULL;
1577      }
1578
1579   if (floating)
1580     return g_variant_new_double (dbl_val);
1581
1582   switch (*g_variant_type_peek_string (type))
1583     {
1584     case 'y':
1585       if (negative || abs_val > G_MAXUINT8)
1586         return number_overflow (ast, type, error);
1587       return g_variant_new_byte (abs_val);
1588
1589     case 'n':
1590       if (abs_val - negative > G_MAXINT16)
1591         return number_overflow (ast, type, error);
1592       return g_variant_new_int16 (negative ? -abs_val : abs_val);
1593
1594     case 'q':
1595       if (negative || abs_val > G_MAXUINT16)
1596         return number_overflow (ast, type, error);
1597       return g_variant_new_uint16 (negative ? -abs_val : abs_val);
1598
1599     case 'i':
1600       if (abs_val - negative > G_MAXINT32)
1601         return number_overflow (ast, type, error);
1602       return g_variant_new_int32 (negative ? -abs_val : abs_val);
1603
1604     case 'u':
1605       if (negative || abs_val > G_MAXUINT32)
1606         return number_overflow (ast, type, error);
1607       return g_variant_new_uint32 (negative ? -abs_val : abs_val);
1608
1609     case 'x':
1610       if (abs_val - negative > G_MAXINT64)
1611         return number_overflow (ast, type, error);
1612       return g_variant_new_int64 (negative ? -abs_val : abs_val);
1613
1614     case 't':
1615       if (negative)
1616         return number_overflow (ast, type, error);
1617       return g_variant_new_uint64 (negative ? -abs_val : abs_val);
1618
1619     case 'h':
1620       if (abs_val - negative > G_MAXINT32)
1621         return number_overflow (ast, type, error);
1622       return g_variant_new_handle (negative ? -abs_val : abs_val);
1623
1624     default:
1625       return ast_type_error (ast, type, error);
1626     }
1627 }
1628
1629 static void
1630 number_free (AST *ast)
1631 {
1632   Number *number = (Number *) ast;
1633
1634   g_free (number->token);
1635   g_slice_free (Number, number);
1636 }
1637
1638 static AST *
1639 number_parse (TokenStream  *stream,
1640               va_list      *app,
1641               GError      **error)
1642 {
1643   static const ASTClass number_class = {
1644     number_get_pattern,
1645     maybe_wrapper, number_get_value,
1646     number_free
1647   };
1648   Number *number;
1649
1650   number = g_slice_new (Number);
1651   number->ast.class = &number_class;
1652   number->token = token_stream_get (stream);
1653   token_stream_next (stream);
1654
1655   return (AST *) number;
1656 }
1657
1658 typedef struct
1659 {
1660   AST ast;
1661   gboolean value;
1662 } Boolean;
1663
1664 static gchar *
1665 boolean_get_pattern (AST     *ast,
1666                      GError **error)
1667 {
1668   return g_strdup ("Mb");
1669 }
1670
1671 static GVariant *
1672 boolean_get_value (AST                 *ast,
1673                    const GVariantType  *type,
1674                    GError             **error)
1675 {
1676   Boolean *boolean = (Boolean *) ast;
1677
1678   if (!g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
1679     return ast_type_error (ast, type, error);
1680
1681   return g_variant_new_boolean (boolean->value);
1682 }
1683
1684 static void
1685 boolean_free (AST *ast)
1686 {
1687   Boolean *boolean = (Boolean *) ast;
1688
1689   g_slice_free (Boolean, boolean);
1690 }
1691
1692 static AST *
1693 boolean_new (gboolean value)
1694 {
1695   static const ASTClass boolean_class = {
1696     boolean_get_pattern,
1697     maybe_wrapper, boolean_get_value,
1698     boolean_free
1699   };
1700   Boolean *boolean;
1701
1702   boolean = g_slice_new (Boolean);
1703   boolean->ast.class = &boolean_class;
1704   boolean->value = value;
1705
1706   return (AST *) boolean;
1707 }
1708
1709 typedef struct
1710 {
1711   AST ast;
1712
1713   GVariant *value;
1714 } Positional;
1715
1716 static gchar *
1717 positional_get_pattern (AST     *ast,
1718                         GError **error)
1719 {
1720   Positional *positional = (Positional *) ast;
1721
1722   return g_strdup (g_variant_get_type_string (positional->value));
1723 }
1724
1725 static GVariant *
1726 positional_get_value (AST                 *ast,
1727                       const GVariantType  *type,
1728                       GError             **error)
1729 {
1730   Positional *positional = (Positional *) ast;
1731   GVariant *value;
1732
1733   g_assert (positional->value != NULL);
1734
1735   if G_UNLIKELY (!g_variant_is_of_type (positional->value, type))
1736     return ast_type_error (ast, type, error);
1737
1738   /* NOTE: if _get is called more than once then
1739    * things get messed up with respect to floating refs.
1740    *
1741    * fortunately, this function should only ever get called once.
1742    */
1743   g_assert (positional->value != NULL);
1744   value = positional->value;
1745   positional->value = NULL;
1746
1747   return value;
1748 }
1749
1750 static void
1751 positional_free (AST *ast)
1752 {
1753   Positional *positional = (Positional *) ast;
1754
1755   /* if positional->value is set, just leave it.
1756    * memory management doesn't matter in case of programmer error.
1757    */
1758   g_slice_free (Positional, positional);
1759 }
1760
1761 static AST *
1762 positional_parse (TokenStream  *stream,
1763                   va_list      *app,
1764                   GError      **error)
1765 {
1766   static const ASTClass positional_class = {
1767     positional_get_pattern,
1768     positional_get_value, NULL,
1769     positional_free
1770   };
1771   Positional *positional;
1772   const gchar *endptr;
1773   gchar *token;
1774
1775   token = token_stream_get (stream);
1776   g_assert (token[0] == '%');
1777
1778   positional = g_slice_new (Positional);
1779   positional->ast.class = &positional_class;
1780   positional->value = g_variant_new_va (token + 1, &endptr, app);
1781
1782   if (*endptr || positional->value == NULL)
1783     {
1784       token_stream_set_error (stream, error, TRUE,
1785                               "invalid GVariant format string");
1786       /* memory management doesn't matter in case of programmer error. */
1787       return NULL;
1788     }
1789
1790   token_stream_next (stream);
1791   g_free (token);
1792
1793   return (AST *) positional;
1794 }
1795
1796 typedef struct
1797 {
1798   AST ast;
1799
1800   GVariantType *type;
1801   AST *child;
1802 } TypeDecl;
1803
1804 static gchar *
1805 typedecl_get_pattern (AST     *ast,
1806                       GError **error)
1807 {
1808   TypeDecl *decl = (TypeDecl *) ast;
1809
1810   return g_variant_type_dup_string (decl->type);
1811 }
1812
1813 static GVariant *
1814 typedecl_get_value (AST                 *ast,
1815                     const GVariantType  *type,
1816                     GError             **error)
1817 {
1818   TypeDecl *decl = (TypeDecl *) ast;
1819
1820   return ast_get_value (decl->child, type, error);
1821 }
1822
1823 static void
1824 typedecl_free (AST *ast)
1825 {
1826   TypeDecl *decl = (TypeDecl *) ast;
1827
1828   ast_free (decl->child);
1829   g_variant_type_free (decl->type);
1830   g_slice_free (TypeDecl, decl);
1831 }
1832
1833 static AST *
1834 typedecl_parse (TokenStream  *stream,
1835                 va_list      *app,
1836                 GError      **error)
1837 {
1838   static const ASTClass typedecl_class = {
1839     typedecl_get_pattern,
1840     typedecl_get_value, NULL,
1841     typedecl_free
1842   };
1843   GVariantType *type;
1844   TypeDecl *decl;
1845   AST *child;
1846
1847   if (token_stream_peek (stream, '@'))
1848     {
1849       gchar *token;
1850
1851       token = token_stream_get (stream);
1852
1853       if (!g_variant_type_string_is_valid (token + 1))
1854         {
1855           token_stream_set_error (stream, error, TRUE,
1856                                   "invalid type declaration");
1857           g_free (token);
1858
1859           return NULL;
1860         }
1861
1862       type = g_variant_type_new (token + 1);
1863
1864       if (!g_variant_type_is_definite (type))
1865         {
1866           token_stream_set_error (stream, error, TRUE,
1867                                   "type declarations must be definite");
1868           g_variant_type_free (type);
1869           g_free (token);
1870
1871           return NULL;
1872         }
1873
1874       token_stream_next (stream);
1875       g_free (token);
1876     }
1877   else
1878     {
1879       if (token_stream_consume (stream, "boolean"))
1880         type = g_variant_type_copy (G_VARIANT_TYPE_BOOLEAN);
1881
1882       else if (token_stream_consume (stream, "byte"))
1883         type = g_variant_type_copy (G_VARIANT_TYPE_BYTE);
1884
1885       else if (token_stream_consume (stream, "int16"))
1886         type = g_variant_type_copy (G_VARIANT_TYPE_INT16);
1887
1888       else if (token_stream_consume (stream, "uint16"))
1889         type = g_variant_type_copy (G_VARIANT_TYPE_UINT16);
1890
1891       else if (token_stream_consume (stream, "int32"))
1892         type = g_variant_type_copy (G_VARIANT_TYPE_INT32);
1893
1894       else if (token_stream_consume (stream, "handle"))
1895         type = g_variant_type_copy (G_VARIANT_TYPE_HANDLE);
1896
1897       else if (token_stream_consume (stream, "uint32"))
1898         type = g_variant_type_copy (G_VARIANT_TYPE_UINT32);
1899
1900       else if (token_stream_consume (stream, "int64"))
1901         type = g_variant_type_copy (G_VARIANT_TYPE_INT64);
1902
1903       else if (token_stream_consume (stream, "uint64"))
1904         type = g_variant_type_copy (G_VARIANT_TYPE_UINT64);
1905
1906       else if (token_stream_consume (stream, "double"))
1907         type = g_variant_type_copy (G_VARIANT_TYPE_DOUBLE);
1908
1909       else if (token_stream_consume (stream, "string"))
1910         type = g_variant_type_copy (G_VARIANT_TYPE_STRING);
1911
1912       else if (token_stream_consume (stream, "objectpath"))
1913         type = g_variant_type_copy (G_VARIANT_TYPE_OBJECT_PATH);
1914
1915       else if (token_stream_consume (stream, "signature"))
1916         type = g_variant_type_copy (G_VARIANT_TYPE_SIGNATURE);
1917
1918       else
1919         {
1920           token_stream_set_error (stream, error, TRUE, "unknown keyword");
1921           return NULL;
1922         }
1923     }
1924
1925   if ((child = parse (stream, app, error)) == NULL)
1926     {
1927       g_variant_type_free (type);
1928       return NULL;
1929     }
1930
1931   decl = g_slice_new (TypeDecl);
1932   decl->ast.class = &typedecl_class;
1933   decl->type = type;
1934   decl->child = child;
1935
1936   return (AST *) decl;
1937 }
1938
1939 static AST *
1940 parse (TokenStream  *stream,
1941        va_list      *app,
1942        GError      **error)
1943 {
1944   SourceRef source_ref;
1945   AST *result;
1946
1947   token_stream_prepare (stream);
1948   token_stream_start_ref (stream, &source_ref);
1949
1950   if (token_stream_peek (stream, '['))
1951     result = array_parse (stream, app, error);
1952
1953   else if (token_stream_peek (stream, '('))
1954     result = tuple_parse (stream, app, error);
1955
1956   else if (token_stream_peek (stream, '<'))
1957     result = variant_parse (stream, app, error);
1958
1959   else if (token_stream_peek (stream, '{'))
1960     result = dictionary_parse (stream, app, error);
1961
1962   else if (app && token_stream_peek (stream, '%'))
1963     result = positional_parse (stream, app, error);
1964
1965   else if (token_stream_consume (stream, "true"))
1966     result = boolean_new (TRUE);
1967
1968   else if (token_stream_consume (stream, "false"))
1969     result = boolean_new (FALSE);
1970
1971   else if (token_stream_peek (stream, 'n') ||
1972            token_stream_peek (stream, 'j'))
1973     result = maybe_parse (stream, app, error);
1974
1975   else if (token_stream_peek (stream, '@') ||
1976            token_stream_is_keyword (stream))
1977     result = typedecl_parse (stream, app, error);
1978
1979   else if (token_stream_is_numeric (stream))
1980     result = number_parse (stream, app, error);
1981
1982   else if (token_stream_peek (stream, '\'') ||
1983            token_stream_peek (stream, '"'))
1984     result = string_parse (stream, app, error);
1985
1986   else
1987     {
1988       token_stream_set_error (stream, error, FALSE, "expected value");
1989       return NULL;
1990     }
1991
1992   if (result != NULL)
1993     {
1994       token_stream_end_ref (stream, &source_ref);
1995       result->source_ref = source_ref;
1996     }
1997
1998   return result;
1999 }
2000
2001 /**
2002  * g_variant_parse:
2003  * @type: a #GVariantType, or %NULL
2004  * @text: a string containing a GVariant in text form
2005  * @limit: a pointer to the end of @text, or %NULL
2006  * @endptr: a location to store the end pointer, or %NULL
2007  * @error: a pointer to a %NULL #GError pointer, or %NULL
2008  * @Returns: a reference to a #GVariant, or %NULL
2009  *
2010  * Parses a #GVariant from a text representation.
2011  *
2012  * A single #GVariant is parsed from the content of @text.
2013  *
2014  * The memory at @limit will never be accessed and the parser behaves as
2015  * if the character at @limit is the nul terminator.  This has the
2016  * effect of bounding @text.
2017  *
2018  * If @endptr is non-%NULL then @text is permitted to contain data
2019  * following the value that this function parses and @endptr will be
2020  * updated to point to the first character past the end of the text
2021  * parsed by this function.  If @endptr is %NULL and there is extra data
2022  * then an error is returned.
2023  *
2024  * If @type is non-%NULL then the value will be parsed to have that
2025  * type.  This may result in additional parse errors (in the case that
2026  * the parsed value doesn't fit the type) but may also result in fewer
2027  * errors (in the case that the type would have been ambiguous, such as
2028  * with empty arrays).
2029  *
2030  * In the event that the parsing is successful, the resulting #GVariant
2031  * is returned.
2032  *
2033  * In case of any error, %NULL will be returned.  If @error is non-%NULL
2034  * then it will be set to reflect the error that occured.
2035  *
2036  * Officially, the language understood by the parser is "any string
2037  * produced by g_variant_print()".
2038  **/
2039 GVariant *
2040 g_variant_parse (const GVariantType  *type,
2041                  const gchar         *text,
2042                  const gchar         *limit,
2043                  const gchar        **endptr,
2044                  GError             **error)
2045 {
2046   TokenStream stream = {  };
2047   GVariant *result = NULL;
2048   AST *ast;
2049
2050   g_return_val_if_fail (text != NULL, NULL);
2051   g_return_val_if_fail (text == limit || text != NULL, NULL);
2052
2053   stream.start = text;
2054   stream.stream = text;
2055   stream.end = limit;
2056
2057   if ((ast = parse (&stream, NULL, error)))
2058     {
2059       if (type == NULL)
2060         result = ast_resolve (ast, error);
2061       else
2062         result = ast_get_value (ast, type, error);
2063
2064       if (result != NULL)
2065         {
2066           g_variant_ref_sink (result);
2067
2068           if (endptr == NULL)
2069             {
2070               while (stream.stream != limit &&
2071                      g_ascii_isspace (*stream.stream))
2072                 stream.stream++;
2073
2074               if (stream.stream != limit && *stream.stream != '\0')
2075                 {
2076                   SourceRef ref = { stream.stream - text,
2077                                     stream.stream - text };
2078
2079                   parser_set_error (error, &ref, NULL,
2080                                     "expected end of input");
2081                   g_variant_unref (result);
2082
2083                   result = NULL;
2084                 }
2085             }
2086           else
2087             *endptr = stream.stream;
2088         }
2089
2090       ast_free (ast);
2091     }
2092
2093   return result;
2094 }
2095
2096 /**
2097  * g_variant_new_parsed_va:
2098  * @format: a text format #GVariant
2099  * @app: a pointer to a #va_list
2100  * @returns: a new floating #GVariant instance
2101  *
2102  * Parses @format and returns the result.
2103  *
2104  * This is the version of g_variant_new_parsed() intended to be used
2105  * from libraries.
2106  **/
2107 GVariant *
2108 g_variant_new_parsed_va (const gchar *format,
2109                          va_list     *app)
2110 {
2111   TokenStream stream = {  };
2112   GVariant *result = NULL;
2113   GError *error = NULL;
2114   AST *ast;
2115
2116   g_return_val_if_fail (format != NULL, NULL);
2117   g_return_val_if_fail (app != NULL, NULL);
2118
2119   stream.start = format;
2120   stream.stream = format;
2121   stream.end = NULL;
2122
2123   if ((ast = parse (&stream, app, &error)))
2124     {
2125       result = ast_resolve (ast, &error);
2126       ast_free (ast);
2127     }
2128
2129   if (result == NULL)
2130     g_error ("g_variant_new_parsed: %s", error->message);
2131
2132   if (*stream.stream)
2133     g_error ("g_variant_new_parsed: trailing text after value");
2134
2135   return result;
2136 }
2137
2138 /**
2139  * g_variant_new_parsed:
2140  * @format: a text format #GVariant
2141  * @...: arguments as per @format
2142  *
2143  * Parses @format and returns the result.
2144  *
2145  * @format must be a text format #GVariant with one extention: at any
2146  * point that a value may appear in the text, a '%' character followed
2147  * by a GVariant format string (as per g_variant_new()) may appear.  In
2148  * that case, the same arguments are collected from the argument list as
2149  * g_variant_new() would have collected.
2150  *
2151  * Consider this simple example:
2152  *
2153  * <informalexample><programlisting>
2154  *  g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three");
2155  * </programlisting></informalexample>
2156  *
2157  * In the example, the variable argument parameters are collected and
2158  * filled in as if they were part of the original string to produce the
2159  * result of <code>[('one', 1), ('two', 2), ('three', 3)]</code>.
2160  *
2161  * This function is intended only to be used with @format as a string
2162  * literal.  Any parse error is fatal to the calling process.  If you
2163  * want to parse data from untrusted sources, use g_variant_parse().
2164  *
2165  * You may not use this function to return, unmodified, a single
2166  * #GVariant pointer from the argument list.  ie: @format may not solely
2167  * be anything along the lines of "%*", "%?", "%r", or anything starting
2168  * with "%@".
2169  **/
2170 GVariant *
2171 g_variant_new_parsed (const gchar *format,
2172                       ...)
2173 {
2174   GVariant *result;
2175   va_list ap;
2176
2177   va_start (ap, format);
2178   result = g_variant_new_parsed_va (format, &ap);
2179   va_end (ap);
2180
2181   return result;
2182 }
2183
2184 #define __G_VARIANT_PARSER_C__
2185 #include "galiasdef.c"