GMLParser.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. if (peek() != GMLToken::Type::ClassMarker) {
  44. dbgln("Expected class marker");
  45. return {};
  46. }
  47. tokens.dequeue();
  48. if (peek() != GMLToken::Type::ClassName) {
  49. dbgln("Expected class name");
  50. return {};
  51. }
  52. auto class_name = tokens.dequeue();
  53. object.set("class", JsonValue(class_name.m_view));
  54. if (peek() != GMLToken::Type::LeftCurly) {
  55. dbgln("Expected {{");
  56. return {};
  57. }
  58. tokens.dequeue();
  59. for (;;) {
  60. if (peek() == GMLToken::Type::RightCurly) {
  61. // End of object
  62. break;
  63. }
  64. if (peek() == GMLToken::Type::ClassMarker) {
  65. // It's a child object.
  66. auto value = parse_core_object(tokens);
  67. if (!value.has_value()) {
  68. dbgln("Parsing child object failed");
  69. return {};
  70. }
  71. if (!value.value().is_object()) {
  72. dbgln("Expected child to be Core::Object");
  73. return {};
  74. }
  75. children.append(value.release_value());
  76. } else if (peek() == GMLToken::Type::Identifier) {
  77. // It's a property.
  78. auto property_name = tokens.dequeue();
  79. if (property_name.m_view.is_empty()) {
  80. dbgln("Expected non-empty property name");
  81. return {};
  82. }
  83. if (peek() != GMLToken::Type::Colon) {
  84. dbgln("Expected ':'");
  85. return {};
  86. }
  87. tokens.dequeue();
  88. JsonValue value;
  89. if (peek() == GMLToken::Type::ClassMarker) {
  90. auto parsed_value = parse_core_object(tokens);
  91. if (!parsed_value.has_value())
  92. return {};
  93. if (!parsed_value.value().is_object()) {
  94. dbgln("Expected property to be Core::Object");
  95. return {};
  96. }
  97. value = parsed_value.release_value();
  98. } else if (peek() == GMLToken::Type::JsonValue) {
  99. auto value_string = tokens.dequeue();
  100. auto parsed_value = JsonValue::from_string(value_string.m_view);
  101. if (!parsed_value.has_value()) {
  102. dbgln("Expected property to be JSON value");
  103. return {};
  104. }
  105. value = parsed_value.release_value();
  106. }
  107. object.set(property_name.m_view, move(value));
  108. } else {
  109. dbgln("Expected child, property, or }}");
  110. return {};
  111. }
  112. }
  113. if (peek() != GMLToken::Type::RightCurly) {
  114. dbgln("Expected }}");
  115. return {};
  116. }
  117. tokens.dequeue();
  118. if (!children.is_empty())
  119. object.set("children", move(children));
  120. return object;
  121. }
  122. JsonValue parse_gml(const StringView& string)
  123. {
  124. auto lexer = GMLLexer(string);
  125. Queue<GMLToken> tokens;
  126. for (auto& token : lexer.lex())
  127. tokens.enqueue(token);
  128. auto root = parse_core_object(tokens);
  129. if (!root.has_value())
  130. return JsonValue();
  131. return root.release_value();
  132. }
  133. }