1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
|
/*
* Copyright © 2019 Benjamin Otte
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtkcssparserprivate.h"
#include "gtkcssenums.h"
#include "gtkcsserror.h"
#include "gtkcsslocationprivate.h"
/* We cannot include gtkdebug.h, so we must keep this in sync */
extern unsigned int gtk_get_debug_flags (void);
#define DEBUG_CHECK_CSS ((gtk_get_debug_flags () & GTK_CSS_PARSER_DEBUG_CSS) != 0)
static void clear_ref (GtkCssVariableValueReference *ref);
#define GDK_ARRAY_NAME gtk_css_parser_references
#define GDK_ARRAY_TYPE_NAME GtkCssParserReferences
#define GDK_ARRAY_ELEMENT_TYPE GtkCssVariableValueReference
#define GDK_ARRAY_BY_VALUE 1
#define GDK_ARRAY_NO_MEMSET 1
#define GDK_ARRAY_FREE_FUNC clear_ref
#include "gdk/gdkarrayimpl.c"
typedef struct _GtkCssParserBlock GtkCssParserBlock;
struct _GtkCssParserBlock
{
GtkCssLocation start_location;
GtkCssTokenType end_token;
GtkCssTokenType inherited_end_token;
GtkCssTokenType alternative_token;
};
#define GDK_ARRAY_NAME gtk_css_parser_blocks
#define GDK_ARRAY_TYPE_NAME GtkCssParserBlocks
#define GDK_ARRAY_ELEMENT_TYPE GtkCssParserBlock
#define GDK_ARRAY_PREALLOC 12
#define GDK_ARRAY_NO_MEMSET 1
#include "gdk/gdkarrayimpl.c"
static inline GtkCssParserBlock *
gtk_css_parser_blocks_get_last (GtkCssParserBlocks *blocks)
{
return gtk_css_parser_blocks_index (blocks, gtk_css_parser_blocks_get_size (blocks) - 1);
}
static inline void
gtk_css_parser_blocks_drop_last (GtkCssParserBlocks *blocks)
{
gtk_css_parser_blocks_set_size (blocks, gtk_css_parser_blocks_get_size (blocks) - 1);
}
typedef struct _GtkCssTokenizerData GtkCssTokenizerData;
struct _GtkCssTokenizerData
{
GtkCssTokenizer *tokenizer;
char *var_name;
GtkCssVariableValue *variable;
};
static void
gtk_css_tokenizer_data_clear (gpointer data)
{
GtkCssTokenizerData *td = data;
gtk_css_tokenizer_unref (td->tokenizer);
if (td->var_name)
g_free (td->var_name);
if (td->variable)
gtk_css_variable_value_unref (td->variable);
}
#define GDK_ARRAY_NAME gtk_css_tokenizers
#define GDK_ARRAY_TYPE_NAME GtkCssTokenizers
#define GDK_ARRAY_ELEMENT_TYPE GtkCssTokenizerData
#define GDK_ARRAY_FREE_FUNC gtk_css_tokenizer_data_clear
#define GDK_ARRAY_BY_VALUE 1
#define GDK_ARRAY_PREALLOC 16
#define GDK_ARRAY_NO_MEMSET 1
#include "gdk/gdkarrayimpl.c"
static inline GtkCssTokenizerData *
gtk_css_tokenizers_get_last (GtkCssTokenizers *tokenizers)
{
return gtk_css_tokenizers_index (tokenizers, gtk_css_tokenizers_get_size (tokenizers) - 1);
}
static inline void
gtk_css_tokenizers_drop_last (GtkCssTokenizers *tokenizers)
{
gtk_css_tokenizers_set_size (tokenizers, gtk_css_tokenizers_get_size (tokenizers) - 1);
}
struct _GtkCssParser
{
volatile int ref_count;
GtkCssTokenizers tokenizers;
GFile *file;
GFile *directory;
GtkCssParserErrorFunc error_func;
gpointer user_data;
GDestroyNotify user_destroy;
GtkCssParserBlocks blocks;
GtkCssLocation location;
GtkCssToken token;
GtkCssVariableValue **refs;
gsize n_refs;
gsize next_ref;
gboolean var_fallback;
};
static inline GtkCssTokenizer *
get_tokenizer (GtkCssParser *self)
{
return gtk_css_tokenizers_get_last (&self->tokenizers)->tokenizer;
}
static GtkCssParser *
gtk_css_parser_new (GtkCssTokenizer *tokenizer,
GtkCssVariableValue *value,
GFile *file,
GtkCssParserErrorFunc error_func,
gpointer user_data,
GDestroyNotify user_destroy)
{
GtkCssParser *self;
self = g_new0 (GtkCssParser, 1);
self->ref_count = 1;
gtk_css_tokenizers_init (&self->tokenizers);
gtk_css_tokenizers_append (&self->tokenizers,
&(GtkCssTokenizerData) {
gtk_css_tokenizer_ref (tokenizer),
NULL,
value ? gtk_css_variable_value_ref (value) : NULL });
if (file)
self->file = g_object_ref (file);
self->error_func = error_func;
self->user_data = user_data;
self->user_destroy = user_destroy;
gtk_css_parser_blocks_init (&self->blocks);
return self;
}
GtkCssParser *
gtk_css_parser_new_for_file (GFile *file,
GtkCssParserErrorFunc error_func,
gpointer user_data,
GDestroyNotify user_destroy,
GError **error)
{
GBytes *bytes;
GtkCssParser *result;
bytes = g_file_load_bytes (file, NULL, NULL, error);
if (bytes == NULL)
return NULL;
result = gtk_css_parser_new_for_bytes (bytes, file, error_func, user_data, user_destroy);
g_bytes_unref (bytes);
return result;
}
GtkCssParser *
gtk_css_parser_new_for_bytes (GBytes *bytes,
GFile *file,
GtkCssParserErrorFunc error_func,
gpointer user_data,
GDestroyNotify user_destroy)
{
GtkCssTokenizer *tokenizer;
GtkCssParser *result;
tokenizer = gtk_css_tokenizer_new (bytes);
result = gtk_css_parser_new (tokenizer, NULL, file, error_func, user_data, user_destroy);
gtk_css_tokenizer_unref (tokenizer);
return result;
}
GtkCssParser *
gtk_css_parser_new_for_token_stream (GtkCssVariableValue *value,
GFile *file,
GtkCssVariableValue **refs,
gsize n_refs,
GtkCssParserErrorFunc error_func,
gpointer user_data,
GDestroyNotify user_destroy)
{
GtkCssTokenizer *tokenizer;
GtkCssParser *result;
tokenizer = gtk_css_tokenizer_new_for_range (value->bytes, value->offset,
value->end_offset - value->offset);
result = gtk_css_parser_new (tokenizer, value, file, error_func, user_data, user_destroy);
gtk_css_tokenizer_unref (tokenizer);
result->refs = refs;
result->n_refs = n_refs;
result->next_ref = 0;
return result;
}
static void
gtk_css_parser_finalize (GtkCssParser *self)
{
if (self->user_destroy)
self->user_destroy (self->user_data);
gtk_css_tokenizers_clear (&self->tokenizers);
g_clear_object (&self->file);
g_clear_object (&self->directory);
if (gtk_css_parser_blocks_get_size (&self->blocks) > 0)
g_critical ("Finalizing CSS parser with %" G_GSIZE_FORMAT " remaining blocks", gtk_css_parser_blocks_get_size (&self->blocks));
gtk_css_parser_blocks_clear (&self->blocks);
g_free (self);
}
GtkCssParser *
gtk_css_parser_ref (GtkCssParser *self)
{
g_atomic_int_inc (&self->ref_count);
return self;
}
void
gtk_css_parser_unref (GtkCssParser *self)
{
if (g_atomic_int_dec_and_test (&self->ref_count))
gtk_css_parser_finalize (self);
}
/**
* gtk_css_parser_get_file:
* @self: a `GtkCssParser`
*
* Gets the file being parsed. If no file is associated with @self -
* for example when raw data is parsed - %NULL is returned.
*
* Returns: (nullable) (transfer none): The file being parsed
*/
GFile *
gtk_css_parser_get_file (GtkCssParser *self)
{
return self->file;
}
GBytes *
gtk_css_parser_get_bytes (GtkCssParser *self)
{
return gtk_css_tokenizer_get_bytes (gtk_css_tokenizers_get (&self->tokenizers, 0)->tokenizer);
}
/**
* gtk_css_parser_resolve_url:
* @self: a `GtkCssParser`
* @url: the URL to resolve
*
* Resolves a given URL against the parser's location.
*
* Returns: (nullable) (transfer full): a new `GFile` for the
* resolved URL
*/
GFile *
gtk_css_parser_resolve_url (GtkCssParser *self,
const char *url)
{
char *scheme;
scheme = g_uri_parse_scheme (url);
if (scheme != NULL)
{
GFile *file = g_file_new_for_uri (url);
g_free (scheme);
return file;
}
if (self->directory == NULL)
{
if (self->file)
self->directory = g_file_get_parent (self->file);
if (self->directory == NULL)
return NULL;
}
return g_file_resolve_relative_path (self->directory, url);
}
/**
* gtk_css_parser_get_start_location:
* @self: a `GtkCssParser`
*
* Queries the location of the current token.
*
* This function will return the location of the start of the
* current token. In the case a token has been consumed, but no
* new token has been queried yet via gtk_css_parser_peek_token()
* or gtk_css_parser_get_token(), the previous token's start
* location will be returned.
*
* This function may return the same location as
* gtk_css_parser_get_end_location() - in particular at the
* beginning and end of the document.
*
* Returns: the start location
**/
const GtkCssLocation *
gtk_css_parser_get_start_location (GtkCssParser *self)
{
return &self->location;
}
/**
* gtk_css_parser_get_end_location:
* @self: a `GtkCssParser`
* @out_location: (caller-allocates) Place to store the location
*
* Queries the location of the current token.
*
* This function will return the location of the end of the
* current token. In the case a token has been consumed, but no
* new token has been queried yet via gtk_css_parser_peek_token()
* or gtk_css_parser_get_token(), the previous token's end location
* will be returned.
*
* This function may return the same location as
* gtk_css_parser_get_start_location() - in particular at the
* beginning and end of the document.
*
* Returns: the end location
**/
const GtkCssLocation *
gtk_css_parser_get_end_location (GtkCssParser *self)
{
return gtk_css_tokenizer_get_location (get_tokenizer (self));
}
/**
* gtk_css_parser_get_block_location:
* @self: a `GtkCssParser`
*
* Queries the start location of the token that started the current
* block that is being parsed.
*
* If no block is currently parsed, the beginning of the document
* is returned.
*
* Returns: The start location of the current block
*/
const GtkCssLocation *
gtk_css_parser_get_block_location (GtkCssParser *self)
{
const GtkCssParserBlock *block;
if (gtk_css_parser_blocks_get_size (&self->blocks) == 0)
{
static const GtkCssLocation start_of_document = { 0, };
return &start_of_document;
}
block = gtk_css_parser_blocks_get_last (&self->blocks);
return &block->start_location;
}
static void
gtk_css_parser_ensure_token (GtkCssParser *self)
{
GError *error = NULL;
GtkCssTokenizer *tokenizer;
if (!gtk_css_token_is (&self->token, GTK_CSS_TOKEN_EOF))
return;
tokenizer = get_tokenizer (self);
self->location = *gtk_css_tokenizer_get_location (tokenizer);
if (!gtk_css_tokenizer_read_token (tokenizer, &self->token, &error))
{
/* We ignore the error here, because the resulting token will
* likely already trigger an error in the parsing code and
* duplicate errors are rather useless.
*/
g_clear_error (&error);
}
if (gtk_css_tokenizers_get_size (&self->tokenizers) > 1 && gtk_css_token_is (&self->token, GTK_CSS_TOKEN_EOF))
{
gtk_css_tokenizers_drop_last (&self->tokenizers);
gtk_css_parser_ensure_token (self);
return;
}
/* Resolve var(--name): skip it and insert the resolved reference instead */
if (self->n_refs > 0 && gtk_css_token_is_function (&self->token, "var") && self->var_fallback == 0)
{
GtkCssVariableValue *ref;
GtkCssTokenizer *ref_tokenizer;
gtk_css_parser_start_block (self);
g_assert (gtk_css_parser_has_token (self, GTK_CSS_TOKEN_IDENT));
char *var_name = gtk_css_parser_consume_ident (self);
g_assert (var_name[0] == '-' && var_name[1] == '-');
/* If we encounter var() in a fallback when we can already resolve the
* actual variable, skip it */
self->var_fallback++;
gtk_css_parser_skip (self);
gtk_css_parser_end_block (self);
self->var_fallback--;
g_assert (self->next_ref < self->n_refs);
ref = self->refs[self->next_ref++];
ref_tokenizer = gtk_css_tokenizer_new_for_range (ref->bytes, ref->offset,
ref->end_offset - ref->offset);
gtk_css_tokenizers_append (&self->tokenizers,
&(GtkCssTokenizerData) {
ref_tokenizer,
g_strdup (var_name),
gtk_css_variable_value_ref (ref)
});
gtk_css_parser_ensure_token (self);
g_free (var_name);
}
}
const GtkCssToken *
gtk_css_parser_peek_token (GtkCssParser *self)
{
static const GtkCssToken eof_token = { GTK_CSS_TOKEN_EOF, };
gtk_css_parser_ensure_token (self);
if (gtk_css_parser_blocks_get_size (&self->blocks) > 0)
{
const GtkCssParserBlock *block = gtk_css_parser_blocks_get_last (&self->blocks);
if (gtk_css_token_is (&self->token, block->end_token) ||
gtk_css_token_is (&self->token, block->inherited_end_token) ||
gtk_css_token_is (&self->token, block->alternative_token))
return &eof_token;
}
return &self->token;
}
const GtkCssToken *
gtk_css_parser_get_token (GtkCssParser *self)
{
const GtkCssToken *token;
for (token = gtk_css_parser_peek_token (self);
gtk_css_token_is (token, GTK_CSS_TOKEN_COMMENT) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_WHITESPACE);
token = gtk_css_parser_peek_token (self))
{
gtk_css_parser_consume_token (self);
}
return token;
}
void
gtk_css_parser_consume_token (GtkCssParser *self)
{
gtk_css_parser_ensure_token (self);
/* unpreserved tokens MUST be consumed via start_block() */
g_assert (gtk_css_token_is_preserved (&self->token, NULL));
/* Don't consume any tokens at the end of a block */
if (!gtk_css_token_is (gtk_css_parser_peek_token (self), GTK_CSS_TOKEN_EOF))
gtk_css_token_clear (&self->token);
}
void
gtk_css_parser_start_block (GtkCssParser *self)
{
GtkCssParserBlock block;
gtk_css_parser_ensure_token (self);
if (gtk_css_token_is_preserved (&self->token, &block.end_token))
{
g_critical ("gtk_css_parser_start_block() may only be called for non-preserved tokens");
return;
}
block.inherited_end_token = GTK_CSS_TOKEN_EOF;
block.alternative_token = GTK_CSS_TOKEN_EOF;
block.start_location = self->location;
gtk_css_parser_blocks_append (&self->blocks, block);
gtk_css_token_clear (&self->token);
}
void
gtk_css_parser_start_semicolon_block (GtkCssParser *self,
GtkCssTokenType alternative_token)
{
GtkCssParserBlock block;
block.end_token = GTK_CSS_TOKEN_SEMICOLON;
if (gtk_css_parser_blocks_get_size (&self->blocks) > 0)
block.inherited_end_token = gtk_css_parser_blocks_get_last (&self->blocks)->end_token;
else
block.inherited_end_token = GTK_CSS_TOKEN_EOF;
block.alternative_token = alternative_token;
block.start_location = self->location;
gtk_css_parser_blocks_append (&self->blocks, block);
}
void
gtk_css_parser_end_block_prelude (GtkCssParser *self)
{
GtkCssParserBlock *block;
g_return_if_fail (gtk_css_parser_blocks_get_size (&self->blocks) > 0);
block = gtk_css_parser_blocks_get_last (&self->blocks);
if (block->alternative_token == GTK_CSS_TOKEN_EOF)
return;
gtk_css_parser_skip_until (self, GTK_CSS_TOKEN_EOF);
if (gtk_css_token_is (&self->token, block->alternative_token))
{
if (gtk_css_token_is_preserved (&self->token, &block->end_token))
{
g_critical ("alternative token is not preserved");
return;
}
block->alternative_token = GTK_CSS_TOKEN_EOF;
block->inherited_end_token = GTK_CSS_TOKEN_EOF;
gtk_css_token_clear (&self->token);
}
}
void
gtk_css_parser_end_block (GtkCssParser *self)
{
GtkCssParserBlock *block;
g_return_if_fail (gtk_css_parser_blocks_get_size (&self->blocks) > 0);
gtk_css_parser_skip_until (self, GTK_CSS_TOKEN_EOF);
block = gtk_css_parser_blocks_get_last (&self->blocks);
if (gtk_css_token_is (&self->token, GTK_CSS_TOKEN_EOF))
{
gtk_css_parser_warn (self,
GTK_CSS_PARSER_WARNING_SYNTAX,
gtk_css_parser_get_block_location (self),
gtk_css_parser_get_start_location (self),
"Unterminated block at end of document");
gtk_css_parser_blocks_drop_last (&self->blocks);
}
else if (gtk_css_token_is (&self->token, block->inherited_end_token))
{
g_assert (block->end_token == GTK_CSS_TOKEN_SEMICOLON);
gtk_css_parser_warn (self,
GTK_CSS_PARSER_WARNING_SYNTAX,
gtk_css_parser_get_block_location (self),
gtk_css_parser_get_start_location (self),
"Expected ';' at end of block");
gtk_css_parser_blocks_drop_last (&self->blocks);
}
else
{
gtk_css_parser_blocks_drop_last (&self->blocks);
if (gtk_css_token_is_preserved (&self->token, NULL))
{
gtk_css_token_clear (&self->token);
}
else
{
gtk_css_parser_start_block (self);
gtk_css_parser_end_block (self);
}
}
}
/*
* gtk_css_parser_skip:
* @self: a `GtkCssParser`
*
* Skips a component value.
*
* This means that if the token is a preserved token, only
* this token will be skipped. If the token starts a block,
* the whole block will be skipped.
**/
void
gtk_css_parser_skip (GtkCssParser *self)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is_preserved (token, NULL))
{
gtk_css_parser_consume_token (self);
}
else
{
gtk_css_parser_start_block (self);
gtk_css_parser_end_block (self);
}
}
/*
* gtk_css_parser_skip_until:
* @self: a `GtkCssParser`
* @token_type: type of token to skip to
*
* Repeatedly skips a token until a certain type is reached.
* After this called, gtk_css_parser_get_token() will either
* return a token of this type or the eof token.
*
* This function is useful for resyncing a parser after encountering
* an error.
*
* If you want to skip until the end, use %GSK_TOKEN_TYPE_EOF
* as the token type.
**/
void
gtk_css_parser_skip_until (GtkCssParser *self,
GtkCssTokenType token_type)
{
const GtkCssToken *token;
for (token = gtk_css_parser_get_token (self);
!gtk_css_token_is (token, token_type) &&
!gtk_css_token_is (token, GTK_CSS_TOKEN_EOF);
token = gtk_css_parser_get_token (self))
{
gtk_css_parser_skip (self);
}
}
void
gtk_css_parser_skip_whitespace (GtkCssParser *self)
{
const GtkCssToken *token;
for (token = gtk_css_parser_peek_token (self);
gtk_css_token_is (token, GTK_CSS_TOKEN_WHITESPACE);
token = gtk_css_parser_peek_token (self))
{
gtk_css_parser_consume_token (self);
}
}
void
gtk_css_parser_emit_error (GtkCssParser *self,
const GtkCssLocation *start,
const GtkCssLocation *end,
const GError *error)
{
if (self->error_func)
self->error_func (self, start, end, error, self->user_data);
}
void
gtk_css_parser_error (GtkCssParser *self,
GtkCssParserError code,
const GtkCssLocation *start,
const GtkCssLocation *end,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_ERROR,
code,
format, args);
gtk_css_parser_emit_error (self, start, end, error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_error_syntax (GtkCssParser *self,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_ERROR,
GTK_CSS_PARSER_ERROR_SYNTAX,
format, args);
gtk_css_parser_emit_error (self,
gtk_css_parser_get_start_location (self),
gtk_css_parser_get_end_location (self),
error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_error_value (GtkCssParser *self,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_ERROR,
GTK_CSS_PARSER_ERROR_UNKNOWN_VALUE,
format, args);
gtk_css_parser_emit_error (self,
gtk_css_parser_get_start_location (self),
gtk_css_parser_get_end_location (self),
error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_error_import (GtkCssParser *self,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_ERROR,
GTK_CSS_PARSER_ERROR_IMPORT,
format, args);
gtk_css_parser_emit_error (self,
gtk_css_parser_get_start_location (self),
gtk_css_parser_get_end_location (self),
error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_warn (GtkCssParser *self,
GtkCssParserWarning code,
const GtkCssLocation *start,
const GtkCssLocation *end,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_WARNING,
code,
format, args);
gtk_css_parser_emit_error (self, start, end, error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_warn_syntax (GtkCssParser *self,
const char *format,
...)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_WARNING,
GTK_CSS_PARSER_WARNING_SYNTAX,
format, args);
gtk_css_parser_emit_error (self,
gtk_css_parser_get_start_location (self),
gtk_css_parser_get_end_location (self),
error);
g_error_free (error);
va_end (args);
}
void
gtk_css_parser_warn_deprecated (GtkCssParser *self,
const char *format,
...)
{
if (DEBUG_CHECK_CSS)
{
va_list args;
GError *error;
va_start (args, format);
error = g_error_new_valist (GTK_CSS_PARSER_WARNING,
GTK_CSS_PARSER_WARNING_DEPRECATED,
format, args);
gtk_css_parser_emit_error (self,
gtk_css_parser_get_start_location (self),
gtk_css_parser_get_end_location (self),
error);
g_error_free (error);
va_end (args);
}
}
gboolean
gtk_css_parser_consume_function (GtkCssParser *self,
guint min_args,
guint max_args,
guint (* parse_func) (GtkCssParser *, guint, gpointer),
gpointer data)
{
const GtkCssToken *token;
gboolean result = FALSE;
char function_name[64];
guint arg;
token = gtk_css_parser_get_token (self);
g_return_val_if_fail (gtk_css_token_is (token, GTK_CSS_TOKEN_FUNCTION), FALSE);
g_strlcpy (function_name, gtk_css_token_get_string (token), 64);
gtk_css_parser_start_block (self);
arg = 0;
while (TRUE)
{
guint parse_args = parse_func (self, arg, data);
if (parse_args == 0)
break;
arg += parse_args;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
{
if (arg < min_args)
{
gtk_css_parser_error_syntax (self, "%s() requires at least %u arguments", function_name, min_args);
break;
}
else
{
result = TRUE;
break;
}
}
else if (gtk_css_token_is (token, GTK_CSS_TOKEN_COMMA))
{
if (arg >= max_args)
{
gtk_css_parser_error_syntax (self, "Expected ')' at end of %s()", function_name);
break;
}
gtk_css_parser_consume_token (self);
continue;
}
else
{
gtk_css_parser_error_syntax (self, "Unexpected data at end of %s() argument", function_name);
break;
}
}
gtk_css_parser_end_block (self);
return result;
}
/**
* gtk_css_parser_has_token:
* @self: a `GtkCssParser`
* @token_type: type of the token to check
*
* Checks if the next token is of @token_type.
*
* Returns: %TRUE if the next token is of @token_type
**/
gboolean
gtk_css_parser_has_token (GtkCssParser *self,
GtkCssTokenType token_type)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
return gtk_css_token_is (token, token_type);
}
/**
* gtk_css_parser_has_ident:
* @self: a `GtkCssParser`
* @ident: name of identifier
*
* Checks if the next token is an identifier with the given @name.
*
* Returns: %TRUE if the next token is an identifier with the given @name
**/
gboolean
gtk_css_parser_has_ident (GtkCssParser *self,
const char *ident)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
return gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) &&
g_ascii_strcasecmp (gtk_css_token_get_string (token), ident) == 0;
}
gboolean
gtk_css_parser_has_integer (GtkCssParser *self)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
return gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER);
}
gboolean
gtk_css_parser_has_percentage (GtkCssParser *self)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
return gtk_css_token_is (token, GTK_CSS_TOKEN_PERCENTAGE);
}
/**
* gtk_css_parser_has_function:
* @self: a `GtkCssParser`
* @name: name of function
*
* Checks if the next token is a function with the given @name.
*
* Returns: %TRUE if the next token is a function with the given @name
*/
gboolean
gtk_css_parser_has_function (GtkCssParser *self,
const char *name)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
return gtk_css_token_is (token, GTK_CSS_TOKEN_FUNCTION) &&
g_ascii_strcasecmp (gtk_css_token_get_string (token), name) == 0;
}
/**
* gtk_css_parser_try_delim:
* @self: a `GtkCssParser`
* @codepoint: unicode character codepoint to check
*
* Checks if the current token is a delimiter matching the given
* @codepoint. If that is the case, the token is consumed and
* %TRUE is returned.
*
* Keep in mind that not every unicode codepoint can be a delim
* token.
*
* Returns: %TRUE if the token matched and was consumed.
**/
gboolean
gtk_css_parser_try_delim (GtkCssParser *self,
gunichar codepoint)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_DELIM) ||
codepoint != token->delim.delim)
return FALSE;
gtk_css_parser_consume_token (self);
return TRUE;
}
/**
* gtk_css_parser_try_ident:
* @self: a `GtkCssParser`
* @ident: identifier to check for
*
* Checks if the current token is an identifier matching the given
* @ident string. If that is the case, the token is consumed
* and %TRUE is returned.
*
* Returns: %TRUE if the token matched and was consumed.
**/
gboolean
gtk_css_parser_try_ident (GtkCssParser *self,
const char *ident)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT) ||
g_ascii_strcasecmp (gtk_css_token_get_string (token), ident) != 0)
return FALSE;
gtk_css_parser_consume_token (self);
return TRUE;
}
/**
* gtk_css_parser_try_at_keyword:
* @self: a `GtkCssParser`
* @keyword: name of keyword to check for
*
* Checks if the current token is an at-keyword token with the
* given @keyword. If that is the case, the token is consumed
* and %TRUE is returned.
*
* Returns: %TRUE if the token matched and was consumed.
**/
gboolean
gtk_css_parser_try_at_keyword (GtkCssParser *self,
const char *keyword)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_AT_KEYWORD) ||
g_ascii_strcasecmp (gtk_css_token_get_string (token), keyword) != 0)
return FALSE;
gtk_css_parser_consume_token (self);
return TRUE;
}
/**
* gtk_css_parser_try_token:
* @self: a `GtkCssParser`
* @token_type: type of token to try
*
* Consumes the next token if it matches the given @token_type.
*
* This function can be used in loops like this:
* do {
* ... parse one element ...
* } while (gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA);
*
* Returns: %TRUE if a token was consumed
**/
gboolean
gtk_css_parser_try_token (GtkCssParser *self,
GtkCssTokenType token_type)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, token_type))
return FALSE;
gtk_css_parser_consume_token (self);
return TRUE;
}
/**
* gtk_css_parser_consume_ident:
* @self: a `GtkCssParser`
*
* If the current token is an identifier, consumes it and returns
* its name.
*
* If the current token is not an identifier, an error is emitted
* and %NULL is returned.
*
* Returns: (transfer full): the name of the consumed identifier
*/
char *
gtk_css_parser_consume_ident (GtkCssParser *self)
{
const GtkCssToken *token;
char *ident;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
{
gtk_css_parser_error_syntax (self, "Expected an identifier");
return NULL;
}
ident = g_strdup (gtk_css_token_get_string (token));
gtk_css_parser_consume_token (self);
return ident;
}
/**
* gtk_css_parser_consume_string:
* @self: a `GtkCssParser`
*
* If the current token is a string, consumes it and return the string.
*
* If the current token is not a string, an error is emitted
* and %NULL is returned.
*
* Returns: (transfer full): the name of the consumed string
**/
char *
gtk_css_parser_consume_string (GtkCssParser *self)
{
const GtkCssToken *token;
char *ident;
token = gtk_css_parser_get_token (self);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_STRING))
{
gtk_css_parser_error_syntax (self, "Expected a string");
return NULL;
}
ident = g_strdup (gtk_css_token_get_string (token));
gtk_css_parser_consume_token (self);
return ident;
}
static guint
gtk_css_parser_parse_url_arg (GtkCssParser *parser,
guint arg,
gpointer data)
{
char **out_url = data;
*out_url = gtk_css_parser_consume_string (parser);
if (*out_url == NULL)
return 0;
return 1;
}
gboolean
gtk_css_parser_has_url (GtkCssParser *self)
{
return gtk_css_parser_has_token (self, GTK_CSS_TOKEN_URL)
|| gtk_css_parser_has_token (self, GTK_CSS_TOKEN_BAD_URL)
|| gtk_css_parser_has_function (self, "url");
}
/**
* gtk_css_parser_consume_url:
* @self: a `GtkCssParser`
*
* If the parser matches the `<url>` token from the [CSS
* specification](https://drafts.csswg.org/css-values-4/#url-value),
* consumes it, resolves the URL and returns the resulting `GFile`.
* On failure, an error is emitted and %NULL is returned.
*
* Returns: (nullable) (transfer full): the resulting URL
**/
char *
gtk_css_parser_consume_url (GtkCssParser *self)
{
const GtkCssToken *token;
char *url;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_URL))
{
url = g_strdup (gtk_css_token_get_string (token));
gtk_css_parser_consume_token (self);
}
else if (gtk_css_token_is_function (token, "url"))
{
if (!gtk_css_parser_consume_function (self, 1, 1, gtk_css_parser_parse_url_arg, &url))
return NULL;
}
else
{
gtk_css_parser_error_syntax (self, "Expected a URL");
return NULL;
}
return url;
}
gboolean
gtk_css_parser_has_number (GtkCssParser *self)
{
return gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SIGNED_NUMBER)
|| gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SIGNLESS_NUMBER)
|| gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SIGNED_INTEGER)
|| gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SIGNLESS_INTEGER);
}
gboolean
gtk_css_parser_consume_number (GtkCssParser *self,
double *number)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_NUMBER) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_NUMBER) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER))
{
*number = token->number.number;
gtk_css_parser_consume_token (self);
return TRUE;
}
gtk_css_parser_error_syntax (self, "Expected a number");
/* FIXME: Implement calc() */
return FALSE;
}
gboolean
gtk_css_parser_consume_integer (GtkCssParser *self,
int *number)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER))
{
*number = token->number.number;
gtk_css_parser_consume_token (self);
return TRUE;
}
gtk_css_parser_error_syntax (self, "Expected an integer");
/* FIXME: Implement calc() */
return FALSE;
}
gboolean
gtk_css_parser_consume_percentage (GtkCssParser *self,
double *number)
{
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_PERCENTAGE))
{
*number = token->number.number;
gtk_css_parser_consume_token (self);
return TRUE;
}
gtk_css_parser_error_syntax (self, "Expected a percentage");
/* FIXME: Implement calc() */
return FALSE;
}
gboolean
gtk_css_parser_consume_number_or_percentage (GtkCssParser *parser,
double min,
double max,
double *value)
{
double number = 0;
if (gtk_css_parser_has_percentage (parser))
{
if (gtk_css_parser_consume_percentage (parser, &number))
{
*value = min + (number / 100.0) * (max - min);
return TRUE;
}
}
else if (gtk_css_parser_has_number (parser))
{
if (gtk_css_parser_consume_number (parser, &number))
{
*value = number;
return TRUE;
}
}
gtk_css_parser_error_syntax (parser, "Expected a number or percentage");
return FALSE;
}
gsize
gtk_css_parser_consume_any (GtkCssParser *parser,
const GtkCssParseOption *options,
gsize n_options,
gpointer user_data)
{
gsize result;
gsize i;
g_return_val_if_fail (parser != NULL, 0);
g_return_val_if_fail (options != NULL, 0);
g_return_val_if_fail (n_options < sizeof (gsize) * 8 - 1, 0);
result = 0;
while (result != (1u << n_options) - 1u)
{
for (i = 0; i < n_options; i++)
{
if (result & (1 << i))
continue;
if (options[i].can_parse && !options[i].can_parse (parser, options[i].data, user_data))
continue;
if (!options[i].parse (parser, options[i].data, user_data))
return 0;
result |= 1 << i;
break;
}
if (i == n_options)
break;
}
if (result == 0)
{
gtk_css_parser_error_syntax (parser, "No valid value given");
return result;
}
return result;
}
gboolean
gtk_css_parser_has_references (GtkCssParser *self)
{
GtkCssTokenizer *tokenizer = get_tokenizer (self);
gboolean ret = FALSE;
int inner_blocks = 0, i;
/* We don't want gtk_css_parser_ensure_token to expand references on us here */
g_assert (self->n_refs == 0);
gtk_css_tokenizer_save (tokenizer);
do {
const GtkCssToken *token;
token = gtk_css_parser_get_token (self);
if (inner_blocks == 0)
{
if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
break;
if (gtk_css_token_is (token, GTK_CSS_TOKEN_CLOSE_PARENS) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_CLOSE_SQUARE))
{
goto done;
}
}
if (gtk_css_token_is_preserved (token, NULL))
{
if (inner_blocks > 0 && gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
{
gtk_css_parser_end_block (self);
inner_blocks--;
}
else
{
gtk_css_parser_consume_token (self);
}
}
else
{
gboolean is_var = gtk_css_token_is_function (token, "var");
inner_blocks++;
gtk_css_parser_start_block (self);
if (is_var)
{
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
{
const char *var_name = gtk_css_token_get_string (token);
if (strlen (var_name) < 3 || var_name[0] != '-' || var_name[1] != '-')
goto done;
gtk_css_parser_consume_token (self);
if (!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_EOF) &&
!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_COMMA))
goto done;
ret = TRUE;
/* We got our answer. Now get it out as fast as possible! */
goto done;
}
}
}
}
while (!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SEMICOLON) &&
!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_CLOSE_CURLY));
done:
for (i = 0; i < inner_blocks; i++)
gtk_css_parser_end_block (self);
g_assert (tokenizer == get_tokenizer (self));
gtk_css_tokenizer_restore (tokenizer);
self->location = *gtk_css_tokenizer_get_location (tokenizer);
gtk_css_tokenizer_read_token (tokenizer, &self->token, NULL);
return ret;
}
static void
clear_ref (GtkCssVariableValueReference *ref)
{
g_free (ref->name);
if (ref->fallback)
gtk_css_variable_value_unref (ref->fallback);
}
#define GDK_ARRAY_NAME gtk_css_parser_references
#define GDK_ARRAY_TYPE_NAME GtkCssParserReferences
#define GDK_ARRAY_ELEMENT_TYPE GtkCssVariableValueReference
GtkCssVariableValue *
gtk_css_parser_parse_value_into_token_stream (GtkCssParser *self)
{
GBytes *bytes = gtk_css_tokenizer_get_bytes (get_tokenizer (self));
const GtkCssToken *token;
gsize offset;
gsize length = 0;
GtkCssParserReferences refs;
int inner_blocks = 0, i;
gboolean is_initial = FALSE;
for (token = gtk_css_parser_peek_token (self);
gtk_css_token_is (token, GTK_CSS_TOKEN_WHITESPACE);
token = gtk_css_parser_peek_token (self))
{
gtk_css_parser_consume_token (self);
}
gtk_css_parser_references_init (&refs);
offset = self->location.bytes;
do {
token = gtk_css_parser_get_token (self);
if (length == 0 && gtk_css_token_is_ident (token, "initial"))
is_initial = TRUE;
if (gtk_css_token_is (token, GTK_CSS_TOKEN_BAD_STRING) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_BAD_URL))
{
gtk_css_parser_error_syntax (self, "Invalid property value");
goto error;
}
if (inner_blocks == 0)
{
if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
break;
if (gtk_css_token_is (token, GTK_CSS_TOKEN_CLOSE_PARENS) ||
gtk_css_token_is (token, GTK_CSS_TOKEN_CLOSE_SQUARE))
{
gtk_css_parser_error_syntax (self, "Invalid property value");
goto error;
}
}
if (gtk_css_token_is_preserved (token, NULL))
{
if (inner_blocks > 0 && gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
{
length++;
gtk_css_parser_end_block (self);
inner_blocks--;
}
else
{
length++;
gtk_css_parser_consume_token (self);
}
}
else
{
gboolean is_var = gtk_css_token_is_function (token, "var");
length++;
inner_blocks++;
gtk_css_parser_start_block (self);
if (is_var)
{
token = gtk_css_parser_get_token (self);
if (gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
{
GtkCssVariableValueReference ref;
char *var_name = g_strdup (gtk_css_token_get_string (token));
if (strlen (var_name) < 3 || var_name[0] != '-' || var_name[1] != '-')
{
gtk_css_parser_error_syntax (self, "Invalid variable name: %s", var_name);
g_free (var_name);
goto error;
}
length++;
gtk_css_parser_consume_token (self);
if (!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_EOF) &&
!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_COMMA))
{
gtk_css_parser_error_syntax (self, "Invalid property value");
g_free (var_name);
goto error;
}
ref.name = var_name;
if (gtk_css_parser_has_token (self, GTK_CSS_TOKEN_EOF))
{
ref.length = 3;
ref.fallback = NULL;
}
else
{
length++;
gtk_css_parser_consume_token (self);
ref.fallback = gtk_css_parser_parse_value_into_token_stream (self);
if (ref.fallback == NULL)
{
gtk_css_parser_error_value (self, "Invalid fallback for: %s", var_name);
g_free (var_name);
goto error;
}
ref.length = 4 + ref.fallback->length;
length += ref.fallback->length;
}
gtk_css_parser_references_append (&refs, &ref);
}
else
{
if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
{
gtk_css_parser_error_syntax (self, "Missing variable name");
}
else
{
char *s = gtk_css_token_to_string (token);
gtk_css_parser_error_syntax (self, "Expected a variable name, not %s", s);
g_free (s);
}
goto error;
}
}
}
}
while (!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_SEMICOLON) &&
!gtk_css_parser_has_token (self, GTK_CSS_TOKEN_CLOSE_CURLY));
if (inner_blocks > 0)
{
gtk_css_parser_error_syntax (self, "Invalid property value");
goto error;
}
if (is_initial && length == 1)
{
gtk_css_parser_references_clear (&refs);
return gtk_css_variable_value_new_initial (bytes,
offset,
self->location.bytes);
}
else
{
GtkCssVariableValueReference *out_refs;
gsize n_refs;
n_refs = gtk_css_parser_references_get_size (&refs);
out_refs = gtk_css_parser_references_steal (&refs);
return gtk_css_variable_value_new (bytes,
offset,
self->location.bytes,
length,
out_refs,
n_refs);
}
error:
for (i = 0; i < inner_blocks; i++)
gtk_css_parser_end_block (self);
gtk_css_parser_references_clear (&refs);
return NULL;
}
void
gtk_css_parser_get_expanding_variables (GtkCssParser *self,
GtkCssVariableValue ***variables,
char ***variable_names,
gsize *n_variables)
{
gsize len = gtk_css_tokenizers_get_size (&self->tokenizers);
GtkCssVariableValue **vars = NULL;
char **names = NULL;
gsize n;
if (variables)
vars = g_new0 (GtkCssVariableValue *, len + 1);
if (variable_names)
names = g_new0 (char *, len + 1);
n = 0;
for (gsize i = 0; i < gtk_css_tokenizers_get_size (&self->tokenizers); i++)
{
GtkCssTokenizerData *data = gtk_css_tokenizers_index (&self->tokenizers, i);
if (variables && data->variable)
vars[n] = gtk_css_variable_value_ref (data->variable);
if (variable_names)
names[n] = g_strdup (data->var_name);
n++;
}
if (variables)
*variables = vars;
if (variable_names)
*variable_names = names;
if (n_variables)
*n_variables = n;
}
|