-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatter): adding a first version of an ArkScript code formatter
- Loading branch information
Showing
36 changed files
with
846 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef ARK_FORMATTER_HPP | ||
#define ARK_FORMATTER_HPP | ||
|
||
#include <string> | ||
|
||
#include <Ark/Compiler/AST/Parser.hpp> | ||
|
||
constexpr struct FormatterConfig | ||
{ | ||
static constexpr std::size_t SpacePerIndent = 2; ///< Indentation level of each node | ||
static constexpr std::size_t LongLineLength = 32; ///< Max number of characters per line segment to consider splitting | ||
} FormatterConfig; | ||
|
||
class Formatter final | ||
{ | ||
public: | ||
Formatter(std::string filename, bool dry_run); | ||
|
||
void run(); | ||
|
||
[[nodiscard]] const std::string& output() const; | ||
|
||
private: | ||
const std::string m_filename; | ||
bool m_dry_run; ///< If true, only prints the formatted file instead of saving it to disk | ||
Ark::internal::Parser m_parser; | ||
std::string m_output; | ||
|
||
bool isListStartingWithKeyword(const Ark::internal::Node& node, Ark::internal::Keyword keyword); | ||
bool isBeginBlock(const Ark::internal::Node& node); | ||
bool isFuncDef(const Ark::internal::Node& node); | ||
bool isFuncCall(const Ark::internal::Node& node); | ||
|
||
/** | ||
* @param node | ||
* @return true if the node is a String|Number|Symbol|Field | ||
* @return false | ||
*/ | ||
bool isPlainValue(const Ark::internal::Node& node); | ||
|
||
/** | ||
* @brief Compute the line on which the deepest right most node of node is at | ||
* @param node | ||
* @return | ||
*/ | ||
std::size_t lineOfLastNodeIn(const Ark::internal::Node& node); | ||
|
||
bool should_split_on_newline(const Ark::internal::Node& node); | ||
|
||
inline constexpr std::string prefix(std::size_t indent) const | ||
{ | ||
return std::string(indent * FormatterConfig.SpacePerIndent, ' '); | ||
} | ||
|
||
/** | ||
* @brief Handles all node formatting | ||
* @param node | ||
* @param indent indentation level, starting at 0, increment by 1 | ||
* @param after_newline when false, do not add prefix | ||
* @return | ||
*/ | ||
std::string format(const Ark::internal::Node& node, std::size_t indent, bool after_newline); | ||
|
||
std::string formatComment(const std::string& comment, std::size_t indent); | ||
|
||
std::string formatBlock(const Ark::internal::Node& node, std::size_t indent, bool after_newline); | ||
|
||
std::string formatFunction(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatVariable(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatCondition(const Ark::internal::Node& node, std::size_t indent, bool is_macro = false); | ||
std::string formatLoop(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatBegin(const Ark::internal::Node& node, std::size_t indent, bool after_newline); | ||
std::string formatImport(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatDel(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatCall(const Ark::internal::Node& node, std::size_t indent); | ||
std::string formatMacro(const Ark::internal::Node& node, std::size_t indent); | ||
}; | ||
|
||
#endif // ARK_FORMATTER_HPP |
Oops, something went wrong.