Formatter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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. #pragma once
  27. #include "NodeVisitor.h"
  28. #include <AK/Forward.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/StringView.h>
  31. #include <AK/Types.h>
  32. #include <AK/Vector.h>
  33. #include <ctype.h>
  34. class Formatter final : public AST::NodeVisitor {
  35. public:
  36. Formatter(const StringView& source, ssize_t cursor = -1)
  37. : m_builder(round_up_to_power_of_two(source.length(), 16))
  38. , m_source(source)
  39. , m_cursor(cursor)
  40. {
  41. size_t offset = 0;
  42. for (auto ptr = m_source.end() - 1; ptr >= m_source.begin() && isspace(*ptr); --ptr)
  43. ++offset;
  44. m_trivia = m_source.substring_view(m_source.length() - offset, offset);
  45. }
  46. String format();
  47. size_t cursor() const { return m_output_cursor; }
  48. private:
  49. virtual void visit(const AST::PathRedirectionNode*) override;
  50. virtual void visit(const AST::And*) override;
  51. virtual void visit(const AST::ListConcatenate*) override;
  52. virtual void visit(const AST::Background*) override;
  53. virtual void visit(const AST::BarewordLiteral*) override;
  54. virtual void visit(const AST::CastToCommand*) override;
  55. virtual void visit(const AST::CastToList*) override;
  56. virtual void visit(const AST::CloseFdRedirection*) override;
  57. virtual void visit(const AST::CommandLiteral*) override;
  58. virtual void visit(const AST::Comment*) override;
  59. virtual void visit(const AST::DynamicEvaluate*) override;
  60. virtual void visit(const AST::DoubleQuotedString*) override;
  61. virtual void visit(const AST::Fd2FdRedirection*) override;
  62. virtual void visit(const AST::FunctionDeclaration*) override;
  63. virtual void visit(const AST::ForLoop*) override;
  64. virtual void visit(const AST::Glob*) override;
  65. virtual void visit(const AST::Execute*) override;
  66. virtual void visit(const AST::IfCond*) override;
  67. virtual void visit(const AST::Join*) override;
  68. virtual void visit(const AST::MatchExpr*) override;
  69. virtual void visit(const AST::Or*) override;
  70. virtual void visit(const AST::Pipe*) override;
  71. virtual void visit(const AST::ReadRedirection*) override;
  72. virtual void visit(const AST::ReadWriteRedirection*) override;
  73. virtual void visit(const AST::Sequence*) override;
  74. virtual void visit(const AST::Subshell*) override;
  75. virtual void visit(const AST::SimpleVariable*) override;
  76. virtual void visit(const AST::SpecialVariable*) override;
  77. virtual void visit(const AST::Juxtaposition*) override;
  78. virtual void visit(const AST::StringLiteral*) override;
  79. virtual void visit(const AST::StringPartCompose*) override;
  80. virtual void visit(const AST::SyntaxError*) override;
  81. virtual void visit(const AST::Tilde*) override;
  82. virtual void visit(const AST::VariableDeclarations*) override;
  83. virtual void visit(const AST::WriteAppendRedirection*) override;
  84. virtual void visit(const AST::WriteRedirection*) override;
  85. void test_and_update_output_cursor(const AST::Node*);
  86. void insert_separator();
  87. void insert_indent();
  88. ALWAYS_INLINE void with_added_indent(int indent, Function<void()>);
  89. ALWAYS_INLINE void in_new_block(Function<void()>);
  90. StringBuilder& current_builder() { return m_builder; }
  91. struct Options {
  92. size_t max_line_length_hint { 80 };
  93. bool explicit_parentheses { false };
  94. bool explicit_braces { false };
  95. bool in_double_quotes { false };
  96. } m_options;
  97. size_t m_current_line_length { 0 };
  98. size_t m_current_indent { 0 };
  99. StringBuilder m_builder;
  100. StringView m_source;
  101. size_t m_output_cursor { 0 };
  102. ssize_t m_cursor { -1 };
  103. AST::Node* m_hit_node { nullptr };
  104. const AST::Node* m_parent_node { nullptr };
  105. StringView m_trivia;
  106. };