Parser.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Parser.h"
  8. #include "AST.h"
  9. #include "Lexer.h"
  10. #include <AK/Error.h>
  11. #include <AK/GenericLexer.h>
  12. #include <AK/JsonObject.h>
  13. #include <AK/JsonValue.h>
  14. #include <AK/Queue.h>
  15. #include <AK/RefPtr.h>
  16. namespace GUI::GML {
  17. static ErrorOr<NonnullRefPtr<Object>> parse_gml_object(Queue<Token>& tokens)
  18. {
  19. auto object = TRY(try_make_ref_counted<Object>());
  20. auto peek = [&] {
  21. if (tokens.is_empty())
  22. return Token::Type::Unknown;
  23. return tokens.head().m_type;
  24. };
  25. while (peek() == Token::Type::Comment) {
  26. dbgln("found comment {}", tokens.head().m_view);
  27. TRY(object->add_property_child(TRY(Node::from_token<Comment>(tokens.dequeue()))));
  28. }
  29. if (peek() != Token::Type::ClassMarker)
  30. return Error::from_string_literal("Expected class marker"sv);
  31. tokens.dequeue();
  32. if (peek() != Token::Type::ClassName)
  33. return Error::from_string_literal("Expected class name"sv);
  34. auto class_name = tokens.dequeue();
  35. object->set_name(class_name.m_view);
  36. if (peek() != Token::Type::LeftCurly)
  37. return Error::from_string_literal("Expected {{"sv);
  38. tokens.dequeue();
  39. NonnullRefPtrVector<Comment> pending_comments;
  40. for (;;) {
  41. if (peek() == Token::Type::RightCurly) {
  42. // End of object
  43. break;
  44. }
  45. if (peek() == Token::Type::ClassMarker) {
  46. // It's a child object.
  47. while (!pending_comments.is_empty())
  48. TRY(object->add_sub_object_child(pending_comments.take_last()));
  49. TRY(object->add_sub_object_child(TRY(parse_gml_object(tokens))));
  50. } else if (peek() == Token::Type::Identifier) {
  51. // It's a property.
  52. while (!pending_comments.is_empty())
  53. TRY(object->add_property_child(pending_comments.take_last()));
  54. auto property_name = tokens.dequeue();
  55. if (property_name.m_view.is_empty())
  56. return Error::from_string_literal("Expected non-empty property name"sv);
  57. if (peek() != Token::Type::Colon)
  58. return Error::from_string_literal("Expected ':'"sv);
  59. tokens.dequeue();
  60. RefPtr<ValueNode> value;
  61. if (peek() == Token::Type::ClassMarker)
  62. value = TRY(parse_gml_object(tokens));
  63. else if (peek() == Token::Type::JsonValue)
  64. value = TRY(try_make_ref_counted<JsonValueNode>(TRY(JsonValueNode::from_string(tokens.dequeue().m_view))));
  65. auto property = TRY(try_make_ref_counted<KeyValuePair>(property_name.m_view, value.release_nonnull()));
  66. TRY(object->add_property_child(property));
  67. } else if (peek() == Token::Type::Comment) {
  68. pending_comments.append(TRY(Node::from_token<Comment>(tokens.dequeue())));
  69. } else {
  70. return Error::from_string_literal("Expected child, property, comment, or }}"sv);
  71. }
  72. }
  73. if (peek() != Token::Type::RightCurly)
  74. return Error::from_string_literal("Expected }}"sv);
  75. tokens.dequeue();
  76. return object;
  77. }
  78. ErrorOr<NonnullRefPtr<GMLFile>> parse_gml(StringView string)
  79. {
  80. auto lexer = Lexer(string);
  81. Queue<Token> tokens;
  82. for (auto& token : lexer.lex())
  83. tokens.enqueue(token);
  84. auto file = TRY(try_make_ref_counted<GMLFile>());
  85. auto peek = [&] {
  86. if (tokens.is_empty())
  87. return Token::Type::Unknown;
  88. return tokens.head().m_type;
  89. };
  90. while (peek() == Token::Type::Comment)
  91. TRY(file->add_child(TRY(Node::from_token<Comment>(tokens.dequeue()))));
  92. TRY(file->add_child(TRY(parse_gml_object(tokens))));
  93. while (!tokens.is_empty())
  94. TRY(file->add_child(TRY(Node::from_token<Comment>(tokens.dequeue()))));
  95. return file;
  96. }
  97. }