2 * Copyright © 2009, 2010 Codethink Limited
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.
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.
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.
19 * Author: Ryan Lortie <desrt@desrt.ca>
31 #include "gstrfuncs.h"
32 #include "gtestutils.h"
34 #include "gvarianttype.h"
40 * designed by ryan lortie and william hua
41 * designed in itb-229 and at ghazi's, 2009.
45 * G_VARIANT_PARSE_ERROR:
47 * Error domain for GVariant text format parsing. Specific error codes
48 * are not currently defined for this domain. See #GError for
49 * information on error domains.
53 * @G_VARIANT_PARSE_ERROR_FAILED: generic error (unused)
54 * @G_VARIANT_PARSE_ERROR_BASIC_TYPE_EXPECTED: a non-basic #GVariantType was given where a basic type was expected
55 * @G_VARIANT_PARSE_ERROR_CANNOT_INFER_TYPE: cannot infer the #GVariantType
56 * @G_VARIANT_PARSE_ERROR_DEFINITE_TYPE_EXPECTED: an indefinite #GVariantType was given where a definite type was expected
57 * @G_VARIANT_PARSE_ERROR_INPUT_NOT_AT_END: extra data after parsing finished
58 * @G_VARIANT_PARSE_ERROR_INVALID_CHARACTER: invalid character in number or unicode escape
59 * @G_VARIANT_PARSE_ERROR_INVALID_FORMAT_STRING: not a valid #GVariant format string
60 * @G_VARIANT_PARSE_ERROR_INVALID_OBJECT_PATH: not a valid object path
61 * @G_VARIANT_PARSE_ERROR_INVALID_SIGNATURE: not a valid type signature
62 * @G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING: not a valid #GVariant type string
63 * @G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE: could not find a common type for array entries
64 * @G_VARIANT_PARSE_ERROR_NUMBER_OUT_OF_RANGE: the numerical value is out of range of the given type
65 * @G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG: the numerical value is out of range for any type
66 * @G_VARIANT_PARSE_ERROR_TYPE_ERROR: cannot parse as variant of the specified type
67 * @G_VARIANT_PARSE_ERROR_UNEXPECTED_TOKEN: an unexpected token was encountered
68 * @G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD: an unknown keyword was encountered
69 * @G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT: unterminated string constant
70 * @G_VARIANT_PARSE_ERROR_VALUE_EXPECTED: no value given
72 * Error codes returned by parsing text-format GVariants.
74 G_DEFINE_QUARK (g-variant-parse-error-quark, g_variant_parser_get_error)
83 parser_set_error_va (GError **error,
90 GString *msg = g_string_new (NULL);
92 if (location->start == location->end)
93 g_string_append_printf (msg, "%d", location->start);
95 g_string_append_printf (msg, "%d-%d", location->start, location->end);
99 g_assert (other->start != other->end);
100 g_string_append_printf (msg, ",%d-%d", other->start, other->end);
102 g_string_append_c (msg, ':');
104 g_string_append_vprintf (msg, format, ap);
105 g_set_error_literal (error, G_VARIANT_PARSE_ERROR, code, msg->str);
106 g_string_free (msg, TRUE);
111 parser_set_error (GError **error,
120 va_start (ap, format);
121 parser_set_error_va (error, location, other, code, format, ap);
137 token_stream_set_error (TokenStream *stream,
147 ref.start = stream->this - stream->start;
150 ref.end = stream->stream - stream->start;
154 va_start (ap, format);
155 parser_set_error_va (error, &ref, NULL, code, format, ap);
160 token_stream_prepare (TokenStream *stream)
165 if (stream->this != NULL)
168 while (stream->stream != stream->end && g_ascii_isspace (*stream->stream))
171 if (stream->stream == stream->end || *stream->stream == '\0')
173 stream->this = stream->stream;
177 switch (stream->stream[0])
179 case '-': case '+': case '.': case '0': case '1': case '2':
180 case '3': case '4': case '5': case '6': case '7': case '8':
182 for (end = stream->stream; end != stream->end; end++)
183 if (!g_ascii_isalnum (*end) &&
184 *end != '-' && *end != '+' && *end != '.')
189 if (stream->stream[1] == '\'' || stream->stream[1] == '"')
191 for (end = stream->stream + 2; end != stream->end; end++)
192 if (*end == stream->stream[1] || *end == '\0' ||
193 (*end == '\\' && (++end == stream->end || *end == '\0')))
196 if (end != stream->end && *end)
203 case 'a': /* 'b' */ case 'c': case 'd': case 'e': case 'f':
204 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
205 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
206 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
208 for (end = stream->stream; end != stream->end; end++)
209 if (!g_ascii_isalnum (*end))
214 for (end = stream->stream + 1; end != stream->end; end++)
215 if (*end == stream->stream[0] || *end == '\0' ||
216 (*end == '\\' && (++end == stream->end || *end == '\0')))
219 if (end != stream->end && *end)
224 /* stop at the first space, comma, colon or unmatched bracket.
225 * deals nicely with cases like (%i, %i) or {%i: %i}.
227 for (end = stream->stream + 1;
228 end != stream->end && *end != ',' &&
229 *end != ':' && *end != '>' && !g_ascii_isspace (*end);
232 if (*end == '(' || *end == '{')
235 else if ((*end == ')' || *end == '}') && !brackets--)
241 end = stream->stream + 1;
245 stream->this = stream->stream;
246 stream->stream = end;
252 token_stream_next (TokenStream *stream)
258 token_stream_peek (TokenStream *stream,
261 if (!token_stream_prepare (stream))
264 return stream->this[0] == first_char;
268 token_stream_peek2 (TokenStream *stream,
272 if (!token_stream_prepare (stream))
275 return stream->this[0] == first_char &&
276 stream->this[1] == second_char;
280 token_stream_is_keyword (TokenStream *stream)
282 if (!token_stream_prepare (stream))
285 return g_ascii_isalpha (stream->this[0]) &&
286 g_ascii_isalpha (stream->this[1]);
290 token_stream_is_numeric (TokenStream *stream)
292 if (!token_stream_prepare (stream))
295 return (g_ascii_isdigit (stream->this[0]) ||
296 stream->this[0] == '-' ||
297 stream->this[0] == '+' ||
298 stream->this[0] == '.');
302 token_stream_peek_string (TokenStream *stream,
305 gint length = strlen (token);
307 return token_stream_prepare (stream) &&
308 stream->stream - stream->this == length &&
309 memcmp (stream->this, token, length) == 0;
313 token_stream_consume (TokenStream *stream,
316 if (!token_stream_peek_string (stream, token))
319 token_stream_next (stream);
324 token_stream_require (TokenStream *stream,
326 const gchar *purpose,
330 if (!token_stream_consume (stream, token))
332 token_stream_set_error (stream, error, FALSE,
333 G_VARIANT_PARSE_ERROR_UNEXPECTED_TOKEN,
334 "expected `%s'%s", token, purpose);
342 token_stream_assert (TokenStream *stream,
345 gboolean correct_token;
347 correct_token = token_stream_consume (stream, token);
348 g_assert (correct_token);
352 token_stream_get (TokenStream *stream)
356 if (!token_stream_prepare (stream))
359 result = g_strndup (stream->this, stream->stream - stream->this);
365 token_stream_start_ref (TokenStream *stream,
368 token_stream_prepare (stream);
369 ref->start = stream->this - stream->start;
373 token_stream_end_ref (TokenStream *stream,
376 ref->end = stream->stream - stream->start;
380 pattern_copy (gchar **out,
385 while (**in == 'a' || **in == 'm' || **in == 'M')
386 *(*out)++ = *(*in)++;
390 if (**in == '(' || **in == '{')
393 else if (**in == ')' || **in == '}')
396 *(*out)++ = *(*in)++;
402 pattern_coalesce (const gchar *left,
408 /* the length of the output is loosely bound by the sum of the input
409 * lengths, not simply the greater of the two lengths.
411 * (*(iii)) + ((iii)*) ((iii)(iii))
415 out = result = g_malloc (strlen (left) + strlen (right));
417 while (*left && *right)
427 const gchar **one = &left, **the_other = &right;
430 if (**one == '*' && **the_other != ')')
432 pattern_copy (&out, the_other);
436 else if (**one == 'M' && **the_other == 'm')
438 *out++ = *(*the_other)++;
441 else if (**one == 'M' && **the_other != 'm')
446 else if (**one == 'N' && strchr ("ynqiuxthd", **the_other))
448 *out++ = *(*the_other)++;
452 else if (**one == 'S' && strchr ("sog", **the_other))
454 *out++ = *(*the_other)++;
458 else if (one == &left)
460 one = &right, the_other = &left;
480 typedef struct _AST AST;
481 typedef gchar * (*get_pattern_func) (AST *ast,
483 typedef GVariant * (*get_value_func) (AST *ast,
484 const GVariantType *type,
486 typedef GVariant * (*get_base_value_func) (AST *ast,
487 const GVariantType *type,
489 typedef void (*free_func) (AST *ast);
493 gchar * (* get_pattern) (AST *ast,
495 GVariant * (* get_value) (AST *ast,
496 const GVariantType *type,
498 GVariant * (* get_base_value) (AST *ast,
499 const GVariantType *type,
501 void (* free) (AST *ast);
506 const ASTClass *class;
507 SourceRef source_ref;
511 ast_get_pattern (AST *ast,
514 return ast->class->get_pattern (ast, error);
518 ast_get_value (AST *ast,
519 const GVariantType *type,
522 return ast->class->get_value (ast, type, error);
528 ast->class->free (ast);
533 ast_set_error (AST *ast,
542 va_start (ap, format);
543 parser_set_error_va (error, &ast->source_ref,
544 other_ast ? & other_ast->source_ref : NULL,
551 ast_type_error (AST *ast,
552 const GVariantType *type,
557 typestr = g_variant_type_dup_string (type);
558 ast_set_error (ast, error, NULL,
559 G_VARIANT_PARSE_ERROR_TYPE_ERROR,
560 "can not parse as value of type `%s'",
568 ast_resolve (AST *ast,
575 pattern = ast_get_pattern (ast, error);
580 /* choose reasonable defaults
582 * 1) favour non-maybe values where possible
583 * 2) default type for strings is 's'
584 * 3) default type for integers is 'i'
586 for (i = 0; pattern[i]; i++)
590 ast_set_error (ast, error, NULL,
591 G_VARIANT_PARSE_ERROR_CANNOT_INFER_TYPE,
592 "unable to infer type");
608 pattern[j++] = pattern[i];
613 value = ast_get_value (ast, G_VARIANT_TYPE (pattern), error);
620 static AST *parse (TokenStream *stream,
625 ast_array_append (AST ***array,
629 if ((*n_items & (*n_items - 1)) == 0)
630 *array = g_renew (AST *, *array, *n_items ? 2 ** n_items : 1);
632 (*array)[(*n_items)++] = ast;
636 ast_array_free (AST **array,
641 for (i = 0; i < n_items; i++)
647 ast_array_get_pattern (AST **array,
654 pattern = ast_get_pattern (array[0], error);
659 for (i = 1; i < n_items; i++)
663 tmp = ast_get_pattern (array[i], error);
671 merged = pattern_coalesce (pattern, tmp);
676 /* set coalescence implies pairwise coalescence (i think).
677 * we should therefore be able to trace the failure to a single
688 /* if 'j' reaches 'i' then we failed to find the pair */
691 tmp2 = ast_get_pattern (array[j], NULL);
692 g_assert (tmp2 != NULL);
694 m = pattern_coalesce (tmp, tmp2);
700 /* we found a conflict between 'i' and 'j'.
702 * report the error. note: 'j' is first.
704 ast_set_error (array[j], error, array[i],
705 G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE,
706 "unable to find a common type");
730 maybe_get_pattern (AST *ast,
733 Maybe *maybe = (Maybe *) ast;
735 if (maybe->child != NULL)
737 gchar *child_pattern;
740 child_pattern = ast_get_pattern (maybe->child, error);
742 if (child_pattern == NULL)
745 pattern = g_strdup_printf ("m%s", child_pattern);
746 g_free (child_pattern);
751 return g_strdup ("m*");
755 maybe_get_value (AST *ast,
756 const GVariantType *type,
759 Maybe *maybe = (Maybe *) ast;
762 if (!g_variant_type_is_maybe (type))
763 return ast_type_error (ast, type, error);
765 type = g_variant_type_element (type);
769 value = ast_get_value (maybe->child, type, error);
777 return g_variant_new_maybe (type, value);
781 maybe_free (AST *ast)
783 Maybe *maybe = (Maybe *) ast;
785 if (maybe->child != NULL)
786 ast_free (maybe->child);
788 g_slice_free (Maybe, maybe);
792 maybe_parse (TokenStream *stream,
796 static const ASTClass maybe_class = {
798 maybe_get_value, NULL,
804 if (token_stream_consume (stream, "just"))
806 child = parse (stream, app, error);
811 else if (!token_stream_consume (stream, "nothing"))
813 token_stream_set_error (stream, error, TRUE,
814 G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD,
819 maybe = g_slice_new (Maybe);
820 maybe->ast.class = &maybe_class;
821 maybe->child = child;
823 return (AST *) maybe;
827 maybe_wrapper (AST *ast,
828 const GVariantType *type,
831 const GVariantType *t;
835 for (depth = 0, t = type;
836 g_variant_type_is_maybe (t);
837 depth++, t = g_variant_type_element (t));
839 value = ast->class->get_base_value (ast, t, error);
845 value = g_variant_new_maybe (NULL, value);
859 array_get_pattern (AST *ast,
862 Array *array = (Array *) ast;
866 if (array->n_children == 0)
867 return g_strdup ("Ma*");
869 pattern = ast_array_get_pattern (array->children, array->n_children, error);
874 result = g_strdup_printf ("Ma%s", pattern);
881 array_get_value (AST *ast,
882 const GVariantType *type,
885 Array *array = (Array *) ast;
886 const GVariantType *childtype;
887 GVariantBuilder builder;
890 if (!g_variant_type_is_array (type))
891 return ast_type_error (ast, type, error);
893 g_variant_builder_init (&builder, type);
894 childtype = g_variant_type_element (type);
896 for (i = 0; i < array->n_children; i++)
900 if (!(child = ast_get_value (array->children[i], childtype, error)))
902 g_variant_builder_clear (&builder);
906 g_variant_builder_add_value (&builder, child);
909 return g_variant_builder_end (&builder);
913 array_free (AST *ast)
915 Array *array = (Array *) ast;
917 ast_array_free (array->children, array->n_children);
918 g_slice_free (Array, array);
922 array_parse (TokenStream *stream,
926 static const ASTClass array_class = {
928 maybe_wrapper, array_get_value,
931 gboolean need_comma = FALSE;
934 array = g_slice_new (Array);
935 array->ast.class = &array_class;
936 array->children = NULL;
937 array->n_children = 0;
939 token_stream_assert (stream, "[");
940 while (!token_stream_consume (stream, "]"))
945 !token_stream_require (stream, ",",
946 " or `]' to follow array element",
950 child = parse (stream, app, error);
955 ast_array_append (&array->children, &array->n_children, child);
959 return (AST *) array;
962 ast_array_free (array->children, array->n_children);
963 g_slice_free (Array, array);
977 tuple_get_pattern (AST *ast,
980 Tuple *tuple = (Tuple *) ast;
981 gchar *result = NULL;
985 parts = g_new (gchar *, tuple->n_children + 4);
986 parts[tuple->n_children + 1] = (gchar *) ")";
987 parts[tuple->n_children + 2] = NULL;
988 parts[0] = (gchar *) "M(";
990 for (i = 0; i < tuple->n_children; i++)
991 if (!(parts[i + 1] = ast_get_pattern (tuple->children[i], error)))
994 if (i == tuple->n_children)
995 result = g_strjoinv ("", parts);
997 /* parts[0] should not be freed */
1006 tuple_get_value (AST *ast,
1007 const GVariantType *type,
1010 Tuple *tuple = (Tuple *) ast;
1011 const GVariantType *childtype;
1012 GVariantBuilder builder;
1015 if (!g_variant_type_is_tuple (type))
1016 return ast_type_error (ast, type, error);
1018 g_variant_builder_init (&builder, type);
1019 childtype = g_variant_type_first (type);
1021 for (i = 0; i < tuple->n_children; i++)
1025 if (childtype == NULL)
1027 g_variant_builder_clear (&builder);
1028 return ast_type_error (ast, type, error);
1031 if (!(child = ast_get_value (tuple->children[i], childtype, error)))
1033 g_variant_builder_clear (&builder);
1037 g_variant_builder_add_value (&builder, child);
1038 childtype = g_variant_type_next (childtype);
1041 if (childtype != NULL)
1043 g_variant_builder_clear (&builder);
1044 return ast_type_error (ast, type, error);
1047 return g_variant_builder_end (&builder);
1051 tuple_free (AST *ast)
1053 Tuple *tuple = (Tuple *) ast;
1055 ast_array_free (tuple->children, tuple->n_children);
1056 g_slice_free (Tuple, tuple);
1060 tuple_parse (TokenStream *stream,
1064 static const ASTClass tuple_class = {
1066 maybe_wrapper, tuple_get_value,
1069 gboolean need_comma = FALSE;
1070 gboolean first = TRUE;
1073 tuple = g_slice_new (Tuple);
1074 tuple->ast.class = &tuple_class;
1075 tuple->children = NULL;
1076 tuple->n_children = 0;
1078 token_stream_assert (stream, "(");
1079 while (!token_stream_consume (stream, ")"))
1084 !token_stream_require (stream, ",",
1085 " or `)' to follow tuple element",
1089 child = parse (stream, app, error);
1094 ast_array_append (&tuple->children, &tuple->n_children, child);
1096 /* the first time, we absolutely require a comma, so grab it here
1097 * and leave need_comma = FALSE so that the code above doesn't
1098 * require a second comma.
1100 * the second and remaining times, we set need_comma = TRUE.
1104 if (!token_stream_require (stream, ",",
1105 " after first tuple element", error))
1114 return (AST *) tuple;
1117 ast_array_free (tuple->children, tuple->n_children);
1118 g_slice_free (Tuple, tuple);
1131 variant_get_pattern (AST *ast,
1134 return g_strdup ("Mv");
1138 variant_get_value (AST *ast,
1139 const GVariantType *type,
1142 Variant *variant = (Variant *) ast;
1145 g_assert (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT));
1146 child = ast_resolve (variant->value, error);
1151 return g_variant_new_variant (child);
1155 variant_free (AST *ast)
1157 Variant *variant = (Variant *) ast;
1159 ast_free (variant->value);
1160 g_slice_free (Variant, variant);
1164 variant_parse (TokenStream *stream,
1168 static const ASTClass variant_class = {
1169 variant_get_pattern,
1170 maybe_wrapper, variant_get_value,
1176 token_stream_assert (stream, "<");
1177 value = parse (stream, app, error);
1182 if (!token_stream_require (stream, ">", " to follow variant value", error))
1188 variant = g_slice_new (Variant);
1189 variant->ast.class = &variant_class;
1190 variant->value = value;
1192 return (AST *) variant;
1205 dictionary_get_pattern (AST *ast,
1208 Dictionary *dict = (Dictionary *) ast;
1209 gchar *value_pattern;
1214 if (dict->n_children == 0)
1215 return g_strdup ("Ma{**}");
1217 key_pattern = ast_array_get_pattern (dict->keys,
1218 abs (dict->n_children),
1221 if (key_pattern == NULL)
1224 /* we can not have maybe keys */
1225 if (key_pattern[0] == 'M')
1226 key_char = key_pattern[1];
1228 key_char = key_pattern[0];
1230 g_free (key_pattern);
1233 * plus undetermined number type and undetermined string type.
1235 if (!strchr ("bynqiuxthdsogNS", key_char))
1237 ast_set_error (ast, error, NULL,
1238 G_VARIANT_PARSE_ERROR_BASIC_TYPE_EXPECTED,
1239 "dictionary keys must have basic types");
1243 value_pattern = ast_get_pattern (dict->values[0], error);
1245 if (value_pattern == NULL)
1248 result = g_strdup_printf ("M%s{%c%s}",
1249 dict->n_children > 0 ? "a" : "",
1250 key_char, value_pattern);
1251 g_free (value_pattern);
1257 dictionary_get_value (AST *ast,
1258 const GVariantType *type,
1261 Dictionary *dict = (Dictionary *) ast;
1263 if (dict->n_children == -1)
1265 const GVariantType *subtype;
1266 GVariantBuilder builder;
1269 if (!g_variant_type_is_dict_entry (type))
1270 return ast_type_error (ast, type, error);
1272 g_variant_builder_init (&builder, type);
1274 subtype = g_variant_type_key (type);
1275 if (!(subvalue = ast_get_value (dict->keys[0], subtype, error)))
1277 g_variant_builder_clear (&builder);
1280 g_variant_builder_add_value (&builder, subvalue);
1282 subtype = g_variant_type_value (type);
1283 if (!(subvalue = ast_get_value (dict->values[0], subtype, error)))
1285 g_variant_builder_clear (&builder);
1288 g_variant_builder_add_value (&builder, subvalue);
1290 return g_variant_builder_end (&builder);
1294 const GVariantType *entry, *key, *val;
1295 GVariantBuilder builder;
1298 if (!g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_DICTIONARY))
1299 return ast_type_error (ast, type, error);
1301 entry = g_variant_type_element (type);
1302 key = g_variant_type_key (entry);
1303 val = g_variant_type_value (entry);
1305 g_variant_builder_init (&builder, type);
1307 for (i = 0; i < dict->n_children; i++)
1311 g_variant_builder_open (&builder, entry);
1313 if (!(subvalue = ast_get_value (dict->keys[i], key, error)))
1315 g_variant_builder_clear (&builder);
1318 g_variant_builder_add_value (&builder, subvalue);
1320 if (!(subvalue = ast_get_value (dict->values[i], val, error)))
1322 g_variant_builder_clear (&builder);
1325 g_variant_builder_add_value (&builder, subvalue);
1326 g_variant_builder_close (&builder);
1329 return g_variant_builder_end (&builder);
1334 dictionary_free (AST *ast)
1336 Dictionary *dict = (Dictionary *) ast;
1339 if (dict->n_children > -1)
1340 n_children = dict->n_children;
1344 ast_array_free (dict->keys, n_children);
1345 ast_array_free (dict->values, n_children);
1346 g_slice_free (Dictionary, dict);
1350 dictionary_parse (TokenStream *stream,
1354 static const ASTClass dictionary_class = {
1355 dictionary_get_pattern,
1356 maybe_wrapper, dictionary_get_value,
1359 gint n_keys, n_values;
1364 dict = g_slice_new (Dictionary);
1365 dict->ast.class = &dictionary_class;
1367 dict->values = NULL;
1368 n_keys = n_values = 0;
1370 token_stream_assert (stream, "{");
1372 if (token_stream_consume (stream, "}"))
1374 dict->n_children = 0;
1375 return (AST *) dict;
1378 if ((first = parse (stream, app, error)) == NULL)
1381 ast_array_append (&dict->keys, &n_keys, first);
1383 only_one = token_stream_consume (stream, ",");
1385 !token_stream_require (stream, ":",
1386 " or `,' to follow dictionary entry key",
1390 if ((first = parse (stream, app, error)) == NULL)
1393 ast_array_append (&dict->values, &n_values, first);
1397 if (!token_stream_require (stream, "}", " at end of dictionary entry",
1401 g_assert (n_keys == 1 && n_values == 1);
1402 dict->n_children = -1;
1404 return (AST *) dict;
1407 while (!token_stream_consume (stream, "}"))
1411 if (!token_stream_require (stream, ",",
1412 " or `}' to follow dictionary entry", error))
1415 child = parse (stream, app, error);
1420 ast_array_append (&dict->keys, &n_keys, child);
1422 if (!token_stream_require (stream, ":",
1423 " to follow dictionary entry key", error))
1426 child = parse (stream, app, error);
1431 ast_array_append (&dict->values, &n_values, child);
1434 g_assert (n_keys == n_values);
1435 dict->n_children = n_keys;
1437 return (AST *) dict;
1440 ast_array_free (dict->keys, n_keys);
1441 ast_array_free (dict->values, n_values);
1442 g_slice_free (Dictionary, dict);
1454 string_get_pattern (AST *ast,
1457 return g_strdup ("MS");
1461 string_get_value (AST *ast,
1462 const GVariantType *type,
1465 String *string = (String *) ast;
1467 if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
1468 return g_variant_new_string (string->string);
1470 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
1472 if (!g_variant_is_object_path (string->string))
1474 ast_set_error (ast, error, NULL,
1475 G_VARIANT_PARSE_ERROR_INVALID_OBJECT_PATH,
1476 "not a valid object path");
1480 return g_variant_new_object_path (string->string);
1483 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
1485 if (!g_variant_is_signature (string->string))
1487 ast_set_error (ast, error, NULL,
1488 G_VARIANT_PARSE_ERROR_INVALID_SIGNATURE,
1489 "not a valid signature");
1493 return g_variant_new_signature (string->string);
1497 return ast_type_error (ast, type, error);
1501 string_free (AST *ast)
1503 String *string = (String *) ast;
1505 g_free (string->string);
1506 g_slice_free (String, string);
1510 unicode_unescape (const gchar *src,
1524 g_assert (length < sizeof (buffer));
1525 strncpy (buffer, src + *src_ofs, length);
1526 buffer[length] = '\0';
1528 value = g_ascii_strtoull (buffer, &end, 0x10);
1530 if (value == 0 || end != buffer + length)
1532 parser_set_error (error, ref, NULL,
1533 G_VARIANT_PARSE_ERROR_INVALID_CHARACTER,
1534 "invalid %d-character unicode escape", length);
1538 g_assert (value <= G_MAXUINT32);
1540 *dest_ofs += g_unichar_to_utf8 (value, dest + *dest_ofs);
1547 string_parse (TokenStream *stream,
1551 static const ASTClass string_class = {
1553 maybe_wrapper, string_get_value,
1564 token_stream_start_ref (stream, &ref);
1565 token = token_stream_get (stream);
1566 token_stream_end_ref (stream, &ref);
1567 length = strlen (token);
1570 str = g_malloc (length);
1571 g_assert (quote == '"' || quote == '\'');
1574 while (token[i] != quote)
1578 parser_set_error (error, &ref, NULL,
1579 G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1580 "unterminated string constant");
1589 parser_set_error (error, &ref, NULL,
1590 G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1591 "unterminated string constant");
1597 if (!unicode_unescape (token, &i, str, &j, 4, &ref, error))
1606 if (!unicode_unescape (token, &i, str, &j, 8, &ref, error))
1614 case 'a': str[j++] = '\a'; i++; continue;
1615 case 'b': str[j++] = '\b'; i++; continue;
1616 case 'f': str[j++] = '\f'; i++; continue;
1617 case 'n': str[j++] = '\n'; i++; continue;
1618 case 'r': str[j++] = '\r'; i++; continue;
1619 case 't': str[j++] = '\t'; i++; continue;
1620 case 'v': str[j++] = '\v'; i++; continue;
1621 case '\n': i++; continue;
1625 str[j++] = token[i++];
1630 string = g_slice_new (String);
1631 string->ast.class = &string_class;
1632 string->string = str;
1634 token_stream_next (stream);
1636 return (AST *) string;
1646 bytestring_get_pattern (AST *ast,
1649 return g_strdup ("May");
1653 bytestring_get_value (AST *ast,
1654 const GVariantType *type,
1657 ByteString *string = (ByteString *) ast;
1659 g_assert (g_variant_type_equal (type, G_VARIANT_TYPE_BYTESTRING));
1661 return g_variant_new_bytestring (string->string);
1665 bytestring_free (AST *ast)
1667 ByteString *string = (ByteString *) ast;
1669 g_free (string->string);
1670 g_slice_free (ByteString, string);
1674 bytestring_parse (TokenStream *stream,
1678 static const ASTClass bytestring_class = {
1679 bytestring_get_pattern,
1680 maybe_wrapper, bytestring_get_value,
1691 token_stream_start_ref (stream, &ref);
1692 token = token_stream_get (stream);
1693 token_stream_end_ref (stream, &ref);
1694 g_assert (token[0] == 'b');
1695 length = strlen (token);
1698 str = g_malloc (length);
1699 g_assert (quote == '"' || quote == '\'');
1702 while (token[i] != quote)
1706 parser_set_error (error, &ref, NULL,
1707 G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1708 "unterminated string constant");
1716 parser_set_error (error, &ref, NULL,
1717 G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
1718 "unterminated string constant");
1722 case '0': case '1': case '2': case '3':
1723 case '4': case '5': case '6': case '7':
1725 /* up to 3 characters */
1726 guchar val = token[i++] - '0';
1728 if ('0' <= token[i] && token[i] < '8')
1729 val = (val << 3) | (token[i++] - '0');
1731 if ('0' <= token[i] && token[i] < '8')
1732 val = (val << 3) | (token[i++] - '0');
1738 case 'a': str[j++] = '\a'; i++; continue;
1739 case 'b': str[j++] = '\b'; i++; continue;
1740 case 'f': str[j++] = '\f'; i++; continue;
1741 case 'n': str[j++] = '\n'; i++; continue;
1742 case 'r': str[j++] = '\r'; i++; continue;
1743 case 't': str[j++] = '\t'; i++; continue;
1744 case 'v': str[j++] = '\v'; i++; continue;
1745 case '\n': i++; continue;
1749 str[j++] = token[i++];
1754 string = g_slice_new (ByteString);
1755 string->ast.class = &bytestring_class;
1756 string->string = str;
1758 token_stream_next (stream);
1760 return (AST *) string;
1771 number_get_pattern (AST *ast,
1774 Number *number = (Number *) ast;
1776 if (strchr (number->token, '.') ||
1777 (!g_str_has_prefix (number->token, "0x") && strchr (number->token, 'e')) ||
1778 strstr (number->token, "inf") ||
1779 strstr (number->token, "nan"))
1780 return g_strdup ("Md");
1782 return g_strdup ("MN");
1786 number_overflow (AST *ast,
1787 const GVariantType *type,
1790 ast_set_error (ast, error, NULL,
1791 G_VARIANT_PARSE_ERROR_NUMBER_OUT_OF_RANGE,
1792 "number out of range for type `%c'",
1793 g_variant_type_peek_string (type)[0]);
1798 number_get_value (AST *ast,
1799 const GVariantType *type,
1802 Number *number = (Number *) ast;
1810 token = number->token;
1812 if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
1817 dbl_val = g_ascii_strtod (token, &end);
1818 if (dbl_val != 0.0 && errno == ERANGE)
1820 ast_set_error (ast, error, NULL,
1821 G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
1822 "number too big for any type");
1826 /* silence uninitialised warnings... */
1833 negative = token[0] == '-';
1834 if (token[0] == '-')
1838 abs_val = g_ascii_strtoull (token, &end, 0);
1839 if (abs_val == G_MAXUINT64 && errno == ERANGE)
1841 ast_set_error (ast, error, NULL,
1842 G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
1843 "integer too big for any type");
1850 /* silence uninitialised warning... */
1858 ref = ast->source_ref;
1859 ref.start += end - number->token;
1860 ref.end = ref.start + 1;
1862 parser_set_error (error, &ref, NULL,
1863 G_VARIANT_PARSE_ERROR_INVALID_CHARACTER,
1864 "invalid character in number");
1869 return g_variant_new_double (dbl_val);
1871 switch (*g_variant_type_peek_string (type))
1874 if (negative || abs_val > G_MAXUINT8)
1875 return number_overflow (ast, type, error);
1876 return g_variant_new_byte (abs_val);
1879 if (abs_val - negative > G_MAXINT16)
1880 return number_overflow (ast, type, error);
1881 return g_variant_new_int16 (negative ? -abs_val : abs_val);
1884 if (negative || abs_val > G_MAXUINT16)
1885 return number_overflow (ast, type, error);
1886 return g_variant_new_uint16 (abs_val);
1889 if (abs_val - negative > G_MAXINT32)
1890 return number_overflow (ast, type, error);
1891 return g_variant_new_int32 (negative ? -abs_val : abs_val);
1894 if (negative || abs_val > G_MAXUINT32)
1895 return number_overflow (ast, type, error);
1896 return g_variant_new_uint32 (abs_val);
1899 if (abs_val - negative > G_MAXINT64)
1900 return number_overflow (ast, type, error);
1901 return g_variant_new_int64 (negative ? -abs_val : abs_val);
1905 return number_overflow (ast, type, error);
1906 return g_variant_new_uint64 (abs_val);
1909 if (abs_val - negative > G_MAXINT32)
1910 return number_overflow (ast, type, error);
1911 return g_variant_new_handle (negative ? -abs_val : abs_val);
1914 return ast_type_error (ast, type, error);
1919 number_free (AST *ast)
1921 Number *number = (Number *) ast;
1923 g_free (number->token);
1924 g_slice_free (Number, number);
1928 number_parse (TokenStream *stream,
1932 static const ASTClass number_class = {
1934 maybe_wrapper, number_get_value,
1939 number = g_slice_new (Number);
1940 number->ast.class = &number_class;
1941 number->token = token_stream_get (stream);
1942 token_stream_next (stream);
1944 return (AST *) number;
1954 boolean_get_pattern (AST *ast,
1957 return g_strdup ("Mb");
1961 boolean_get_value (AST *ast,
1962 const GVariantType *type,
1965 Boolean *boolean = (Boolean *) ast;
1967 if (!g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
1968 return ast_type_error (ast, type, error);
1970 return g_variant_new_boolean (boolean->value);
1974 boolean_free (AST *ast)
1976 Boolean *boolean = (Boolean *) ast;
1978 g_slice_free (Boolean, boolean);
1982 boolean_new (gboolean value)
1984 static const ASTClass boolean_class = {
1985 boolean_get_pattern,
1986 maybe_wrapper, boolean_get_value,
1991 boolean = g_slice_new (Boolean);
1992 boolean->ast.class = &boolean_class;
1993 boolean->value = value;
1995 return (AST *) boolean;
2006 positional_get_pattern (AST *ast,
2009 Positional *positional = (Positional *) ast;
2011 return g_strdup (g_variant_get_type_string (positional->value));
2015 positional_get_value (AST *ast,
2016 const GVariantType *type,
2019 Positional *positional = (Positional *) ast;
2022 g_assert (positional->value != NULL);
2024 if G_UNLIKELY (!g_variant_is_of_type (positional->value, type))
2025 return ast_type_error (ast, type, error);
2027 /* NOTE: if _get is called more than once then
2028 * things get messed up with respect to floating refs.
2030 * fortunately, this function should only ever get called once.
2032 g_assert (positional->value != NULL);
2033 value = positional->value;
2034 positional->value = NULL;
2040 positional_free (AST *ast)
2042 Positional *positional = (Positional *) ast;
2044 /* if positional->value is set, just leave it.
2045 * memory management doesn't matter in case of programmer error.
2047 g_slice_free (Positional, positional);
2051 positional_parse (TokenStream *stream,
2055 static const ASTClass positional_class = {
2056 positional_get_pattern,
2057 positional_get_value, NULL,
2060 Positional *positional;
2061 const gchar *endptr;
2064 token = token_stream_get (stream);
2065 g_assert (token[0] == '%');
2067 positional = g_slice_new (Positional);
2068 positional->ast.class = &positional_class;
2069 positional->value = g_variant_new_va (token + 1, &endptr, app);
2071 if (*endptr || positional->value == NULL)
2073 token_stream_set_error (stream, error, TRUE,
2074 G_VARIANT_PARSE_ERROR_INVALID_FORMAT_STRING,
2075 "invalid GVariant format string");
2076 /* memory management doesn't matter in case of programmer error. */
2080 token_stream_next (stream);
2083 return (AST *) positional;
2095 typedecl_get_pattern (AST *ast,
2098 TypeDecl *decl = (TypeDecl *) ast;
2100 return g_variant_type_dup_string (decl->type);
2104 typedecl_get_value (AST *ast,
2105 const GVariantType *type,
2108 TypeDecl *decl = (TypeDecl *) ast;
2110 return ast_get_value (decl->child, type, error);
2114 typedecl_free (AST *ast)
2116 TypeDecl *decl = (TypeDecl *) ast;
2118 ast_free (decl->child);
2119 g_variant_type_free (decl->type);
2120 g_slice_free (TypeDecl, decl);
2124 typedecl_parse (TokenStream *stream,
2128 static const ASTClass typedecl_class = {
2129 typedecl_get_pattern,
2130 typedecl_get_value, NULL,
2137 if (token_stream_peek (stream, '@'))
2141 token = token_stream_get (stream);
2143 if (!g_variant_type_string_is_valid (token + 1))
2145 token_stream_set_error (stream, error, TRUE,
2146 G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING,
2147 "invalid type declaration");
2153 type = g_variant_type_new (token + 1);
2155 if (!g_variant_type_is_definite (type))
2157 token_stream_set_error (stream, error, TRUE,
2158 G_VARIANT_PARSE_ERROR_DEFINITE_TYPE_EXPECTED,
2159 "type declarations must be definite");
2160 g_variant_type_free (type);
2166 token_stream_next (stream);
2171 if (token_stream_consume (stream, "boolean"))
2172 type = g_variant_type_copy (G_VARIANT_TYPE_BOOLEAN);
2174 else if (token_stream_consume (stream, "byte"))
2175 type = g_variant_type_copy (G_VARIANT_TYPE_BYTE);
2177 else if (token_stream_consume (stream, "int16"))
2178 type = g_variant_type_copy (G_VARIANT_TYPE_INT16);
2180 else if (token_stream_consume (stream, "uint16"))
2181 type = g_variant_type_copy (G_VARIANT_TYPE_UINT16);
2183 else if (token_stream_consume (stream, "int32"))
2184 type = g_variant_type_copy (G_VARIANT_TYPE_INT32);
2186 else if (token_stream_consume (stream, "handle"))
2187 type = g_variant_type_copy (G_VARIANT_TYPE_HANDLE);
2189 else if (token_stream_consume (stream, "uint32"))
2190 type = g_variant_type_copy (G_VARIANT_TYPE_UINT32);
2192 else if (token_stream_consume (stream, "int64"))
2193 type = g_variant_type_copy (G_VARIANT_TYPE_INT64);
2195 else if (token_stream_consume (stream, "uint64"))
2196 type = g_variant_type_copy (G_VARIANT_TYPE_UINT64);
2198 else if (token_stream_consume (stream, "double"))
2199 type = g_variant_type_copy (G_VARIANT_TYPE_DOUBLE);
2201 else if (token_stream_consume (stream, "string"))
2202 type = g_variant_type_copy (G_VARIANT_TYPE_STRING);
2204 else if (token_stream_consume (stream, "objectpath"))
2205 type = g_variant_type_copy (G_VARIANT_TYPE_OBJECT_PATH);
2207 else if (token_stream_consume (stream, "signature"))
2208 type = g_variant_type_copy (G_VARIANT_TYPE_SIGNATURE);
2212 token_stream_set_error (stream, error, TRUE,
2213 G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD,
2219 if ((child = parse (stream, app, error)) == NULL)
2221 g_variant_type_free (type);
2225 decl = g_slice_new (TypeDecl);
2226 decl->ast.class = &typedecl_class;
2228 decl->child = child;
2230 return (AST *) decl;
2234 parse (TokenStream *stream,
2238 SourceRef source_ref;
2241 token_stream_prepare (stream);
2242 token_stream_start_ref (stream, &source_ref);
2244 if (token_stream_peek (stream, '['))
2245 result = array_parse (stream, app, error);
2247 else if (token_stream_peek (stream, '('))
2248 result = tuple_parse (stream, app, error);
2250 else if (token_stream_peek (stream, '<'))
2251 result = variant_parse (stream, app, error);
2253 else if (token_stream_peek (stream, '{'))
2254 result = dictionary_parse (stream, app, error);
2256 else if (app && token_stream_peek (stream, '%'))
2257 result = positional_parse (stream, app, error);
2259 else if (token_stream_consume (stream, "true"))
2260 result = boolean_new (TRUE);
2262 else if (token_stream_consume (stream, "false"))
2263 result = boolean_new (FALSE);
2265 else if (token_stream_is_numeric (stream) ||
2266 token_stream_peek_string (stream, "inf") ||
2267 token_stream_peek_string (stream, "nan"))
2268 result = number_parse (stream, app, error);
2270 else if (token_stream_peek (stream, 'n') ||
2271 token_stream_peek (stream, 'j'))
2272 result = maybe_parse (stream, app, error);
2274 else if (token_stream_peek (stream, '@') ||
2275 token_stream_is_keyword (stream))
2276 result = typedecl_parse (stream, app, error);
2278 else if (token_stream_peek (stream, '\'') ||
2279 token_stream_peek (stream, '"'))
2280 result = string_parse (stream, app, error);
2282 else if (token_stream_peek2 (stream, 'b', '\'') ||
2283 token_stream_peek2 (stream, 'b', '"'))
2284 result = bytestring_parse (stream, app, error);
2288 token_stream_set_error (stream, error, FALSE,
2289 G_VARIANT_PARSE_ERROR_VALUE_EXPECTED,
2296 token_stream_end_ref (stream, &source_ref);
2297 result->source_ref = source_ref;
2305 * @type: (allow-none): a #GVariantType, or %NULL
2306 * @text: a string containing a GVariant in text form
2307 * @limit: (allow-none): a pointer to the end of @text, or %NULL
2308 * @endptr: (allow-none): a location to store the end pointer, or %NULL
2309 * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
2311 * Parses a #GVariant from a text representation.
2313 * A single #GVariant is parsed from the content of @text.
2315 * The format is described <link linkend='gvariant-text'>here</link>.
2317 * The memory at @limit will never be accessed and the parser behaves as
2318 * if the character at @limit is the nul terminator. This has the
2319 * effect of bounding @text.
2321 * If @endptr is non-%NULL then @text is permitted to contain data
2322 * following the value that this function parses and @endptr will be
2323 * updated to point to the first character past the end of the text
2324 * parsed by this function. If @endptr is %NULL and there is extra data
2325 * then an error is returned.
2327 * If @type is non-%NULL then the value will be parsed to have that
2328 * type. This may result in additional parse errors (in the case that
2329 * the parsed value doesn't fit the type) but may also result in fewer
2330 * errors (in the case that the type would have been ambiguous, such as
2331 * with empty arrays).
2333 * In the event that the parsing is successful, the resulting #GVariant
2336 * In case of any error, %NULL will be returned. If @error is non-%NULL
2337 * then it will be set to reflect the error that occurred.
2339 * Officially, the language understood by the parser is "any string
2340 * produced by g_variant_print()".
2342 * Returns: a reference to a #GVariant, or %NULL
2345 g_variant_parse (const GVariantType *type,
2348 const gchar **endptr,
2351 TokenStream stream = { 0, };
2352 GVariant *result = NULL;
2355 g_return_val_if_fail (text != NULL, NULL);
2356 g_return_val_if_fail (text == limit || text != NULL, NULL);
2358 stream.start = text;
2359 stream.stream = text;
2362 if ((ast = parse (&stream, NULL, error)))
2365 result = ast_resolve (ast, error);
2367 result = ast_get_value (ast, type, error);
2371 g_variant_ref_sink (result);
2375 while (stream.stream != limit &&
2376 g_ascii_isspace (*stream.stream))
2379 if (stream.stream != limit && *stream.stream != '\0')
2381 SourceRef ref = { stream.stream - text,
2382 stream.stream - text };
2384 parser_set_error (error, &ref, NULL,
2385 G_VARIANT_PARSE_ERROR_INPUT_NOT_AT_END,
2386 "expected end of input");
2387 g_variant_unref (result);
2393 *endptr = stream.stream;
2403 * g_variant_new_parsed_va:
2404 * @format: a text format #GVariant
2405 * @app: a pointer to a #va_list
2407 * Parses @format and returns the result.
2409 * This is the version of g_variant_new_parsed() intended to be used
2412 * The return value will be floating if it was a newly created GVariant
2413 * instance. In the case that @format simply specified the collection
2414 * of a #GVariant pointer (eg: @format was "%*") then the collected
2415 * #GVariant pointer will be returned unmodified, without adding any
2416 * additional references.
2418 * In order to behave correctly in all cases it is necessary for the
2419 * calling function to g_variant_ref_sink() the return result before
2420 * returning control to the user that originally provided the pointer.
2421 * At this point, the caller will have their own full reference to the
2422 * result. This can also be done by adding the result to a container,
2423 * or by passing it to another g_variant_new() call.
2425 * Returns: a new, usually floating, #GVariant
2428 g_variant_new_parsed_va (const gchar *format,
2431 TokenStream stream = { 0, };
2432 GVariant *result = NULL;
2433 GError *error = NULL;
2436 g_return_val_if_fail (format != NULL, NULL);
2437 g_return_val_if_fail (app != NULL, NULL);
2439 stream.start = format;
2440 stream.stream = format;
2443 if ((ast = parse (&stream, app, &error)))
2445 result = ast_resolve (ast, &error);
2450 g_error ("g_variant_new_parsed: %s", error->message);
2453 g_error ("g_variant_new_parsed: trailing text after value");
2459 * g_variant_new_parsed:
2460 * @format: a text format #GVariant
2461 * @...: arguments as per @format
2463 * Parses @format and returns the result.
2465 * @format must be a text format #GVariant with one extension: at any
2466 * point that a value may appear in the text, a '%' character followed
2467 * by a GVariant format string (as per g_variant_new()) may appear. In
2468 * that case, the same arguments are collected from the argument list as
2469 * g_variant_new() would have collected.
2471 * Consider this simple example:
2473 * <informalexample><programlisting>
2474 * g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three");
2475 * </programlisting></informalexample>
2477 * In the example, the variable argument parameters are collected and
2478 * filled in as if they were part of the original string to produce the
2479 * result of <code>[('one', 1), ('two', 2), ('three', 3)]</code>.
2481 * This function is intended only to be used with @format as a string
2482 * literal. Any parse error is fatal to the calling process. If you
2483 * want to parse data from untrusted sources, use g_variant_parse().
2485 * You may not use this function to return, unmodified, a single
2486 * #GVariant pointer from the argument list. ie: @format may not solely
2487 * be anything along the lines of "%*", "%?", "\%r", or anything starting
2490 * Returns: a new floating #GVariant instance
2493 g_variant_new_parsed (const gchar *format,
2499 va_start (ap, format);
2500 result = g_variant_new_parsed_va (format, &ap);
2507 * g_variant_builder_add_parsed:
2508 * @builder: a #GVariantBuilder
2509 * @format: a text format #GVariant
2510 * @...: arguments as per @format
2512 * Adds to a #GVariantBuilder.
2514 * This call is a convenience wrapper that is exactly equivalent to
2515 * calling g_variant_new_parsed() followed by
2516 * g_variant_builder_add_value().
2518 * This function might be used as follows:
2522 * make_pointless_dictionary (void)
2524 * GVariantBuilder *builder;
2527 * builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
2528 * g_variant_builder_add_parsed (builder, "{'width', <%i>}", 600);
2529 * g_variant_builder_add_parsed (builder, "{'title', <%s>}", "foo");
2530 * g_variant_builder_add_parsed (builder, "{'transparency', <0.5>}");
2531 * return g_variant_builder_end (builder);
2538 g_variant_builder_add_parsed (GVariantBuilder *builder,
2539 const gchar *format,
2544 va_start (ap, format);
2545 g_variant_builder_add_value (builder, g_variant_new_parsed_va (format, &ap));