GMLParser.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <AK/Queue.h>
  30. #include <LibGUI/GMLLexer.h>
  31. #include <LibGUI/GMLParser.h>
  32. #include <ctype.h>
  33. namespace GUI {
  34. static Optional<JsonValue> parse_core_object(Queue<GMLToken>& tokens)
  35. {
  36. JsonObject object;
  37. JsonArray children;
  38. auto peek = [&] {
  39. if (tokens.is_empty())
  40. return GMLToken::Type::Unknown;
  41. return tokens.head().m_type;
  42. };
  43. while (peek() == GMLToken::Type::Comment)
  44. tokens.dequeue();
  45. if (peek() != GMLToken::Type::ClassMarker) {
  46. dbgln("Expected class marker");
  47. return {};
  48. }
  49. tokens.dequeue();
  50. if (peek() != GMLToken::Type::ClassName) {
  51. dbgln("Expected class name");
  52. return {};
  53. }
  54. auto class_name = tokens.dequeue();
  55. object.set("class", JsonValue(class_name.m_view));
  56. if (peek() != GMLToken::Type::LeftCurly) {
  57. // Empty object
  58. return object;
  59. }
  60. tokens.dequeue();
  61. for (;;) {
  62. if (peek() == GMLToken::Type::RightCurly) {
  63. // End of object
  64. break;
  65. }
  66. if (peek() == GMLToken::Type::ClassMarker) {
  67. // It's a child object.
  68. auto value = parse_core_object(tokens);
  69. if (!value.has_value()) {
  70. dbgln("Parsing child object failed");
  71. return {};
  72. }
  73. if (!value.value().is_object()) {
  74. dbgln("Expected child to be Core::Object");
  75. return {};
  76. }
  77. children.append(value.release_value());
  78. } else if (peek() == GMLToken::Type::Identifier) {
  79. // It's a property.
  80. auto property_name = tokens.dequeue();
  81. if (property_name.m_view.is_empty()) {
  82. dbgln("Expected non-empty property name");
  83. return {};
  84. }
  85. if (peek() != GMLToken::Type::Colon) {
  86. dbgln("Expected ':'");
  87. return {};
  88. }
  89. tokens.dequeue();
  90. JsonValue value;
  91. if (peek() == GMLToken::Type::ClassMarker) {
  92. auto parsed_value = parse_core_object(tokens);
  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 if (peek() == GMLToken::Type::JsonValue) {
  101. auto value_string = tokens.dequeue();
  102. auto parsed_value = JsonValue::from_string(value_string.m_view);
  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.m_view, move(value));
  110. } else if (peek() == GMLToken::Type::Comment) {
  111. tokens.dequeue();
  112. } else {
  113. dbgln("Expected child, property, comment, or }}");
  114. return {};
  115. }
  116. }
  117. if (peek() != GMLToken::Type::RightCurly) {
  118. dbgln("Expected }}");
  119. return {};
  120. }
  121. tokens.dequeue();
  122. if (!children.is_empty())
  123. object.set("children", move(children));
  124. return object;
  125. }
  126. JsonValue parse_gml(const StringView& string)
  127. {
  128. auto lexer = GMLLexer(string);
  129. Queue<GMLToken> tokens;
  130. for (auto& token : lexer.lex())
  131. tokens.enqueue(token);
  132. auto root = parse_core_object(tokens);
  133. if (!root.has_value())
  134. return JsonValue();
  135. return root.release_value();
  136. }
  137. }