GMLParser.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/GenericLexer.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <LibGUI/GMLParser.h>
  30. #include <ctype.h>
  31. namespace GUI {
  32. static bool is_valid_class_name_character(char ch)
  33. {
  34. return isalpha(ch) || ch == ':';
  35. }
  36. static bool is_valid_property_name_character(char ch)
  37. {
  38. return isalpha(ch) || ch == '_';
  39. }
  40. static void swallow_whitespace(GenericLexer& scanner)
  41. {
  42. scanner.consume_while([](auto ch) { return isspace(ch); });
  43. }
  44. static Optional<JsonValue> parse_core_object(GenericLexer& scanner)
  45. {
  46. JsonObject object;
  47. JsonArray children;
  48. // '@Foo' means new Core::Object of class Foo
  49. if (!scanner.consume_specific('@')) {
  50. dbgln("Expected '@'");
  51. return {};
  52. }
  53. auto class_name = scanner.consume_while([](auto ch) { return is_valid_class_name_character(ch); });
  54. object.set("class", JsonValue(class_name));
  55. swallow_whitespace(scanner);
  56. if (!scanner.consume_specific('{')) {
  57. dbgln("Expected '{{'");
  58. return {};
  59. }
  60. swallow_whitespace(scanner);
  61. for (;;) {
  62. swallow_whitespace(scanner);
  63. if (scanner.peek() == '}') {
  64. // End of object
  65. break;
  66. }
  67. if (scanner.peek() == '@') {
  68. // It's a child object.
  69. auto value = parse_core_object(scanner);
  70. if (!value.has_value())
  71. return {};
  72. if (!value.value().is_object()) {
  73. dbgln("Expected child to be Core::Object");
  74. return {};
  75. }
  76. children.append(value.release_value());
  77. } else {
  78. // It's a property.
  79. auto property_name = scanner.consume_while([](auto ch) { return is_valid_property_name_character(ch); });
  80. swallow_whitespace(scanner);
  81. if (property_name.is_empty()) {
  82. dbgln("Expected non-empty property name");
  83. return {};
  84. }
  85. if (!scanner.consume_specific(':')) {
  86. dbgln("Expected ':'");
  87. return {};
  88. }
  89. swallow_whitespace(scanner);
  90. JsonValue value;
  91. if (scanner.peek() == '@') {
  92. auto parsed_value = parse_core_object(scanner);
  93. if (!parsed_value.has_value())
  94. return {};
  95. if (!parsed_value.value().is_object()) {
  96. dbgln("Expected property to be Core::Object");
  97. return {};
  98. }
  99. value = parsed_value.release_value();
  100. } else {
  101. auto value_string = scanner.consume_line();
  102. auto parsed_value = JsonValue::from_string(value_string);
  103. if (!parsed_value.has_value()) {
  104. dbgln("Expected property to be JSON value");
  105. return {};
  106. }
  107. value = parsed_value.release_value();
  108. }
  109. object.set(property_name, move(value));
  110. }
  111. }
  112. if (!scanner.consume_specific('}')) {
  113. dbgln("Expected '}'");
  114. return {};
  115. }
  116. if (!children.is_empty())
  117. object.set("children", move(children));
  118. return object;
  119. }
  120. JsonValue parse_gml(const StringView& string)
  121. {
  122. GenericLexer scanner(string);
  123. auto root = parse_core_object(scanner);
  124. if (!root.has_value())
  125. return JsonValue();
  126. return root.release_value();
  127. }
  128. }