![]() |
ArkScript
A small, fast, functional and scripting language for video games
|
#include <Formatter.hpp>
Public Member Functions | |
Formatter (bool dry_run) | |
Formatter (std::string filename, bool dry_run) | |
void | run () |
Read the file and process it. The file isn't modified. | |
void | runWithString (const std::string &code) |
const std::string & | output () const |
bool | codeModified () const |
Private Member Functions | |
void | processAst (const Ark::internal::Node &ast) |
void | warnIfCommentsWereRemoved (const std::string &original_code, const std::string &filename) |
Given the original code, produce a warning if comments from it were removed during formatting. | |
bool | shouldSplitOnNewline (const Ark::internal::Node &node) |
Decide if a node should be split on a newline or not. | |
bool | shouldAddNewLineBetweenNodes (const Ark::internal::Node &node, std::size_t at) |
Decide if we should add a newline after a node in a block. | |
std::string | format (const Ark::internal::Node &node, std::size_t indent, bool after_newline) |
Handles all node formatting. | |
std::string | formatComment (const std::string &comment, std::size_t indent) const |
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) |
Static Private Member Functions | |
static bool | isListStartingWithKeyword (const Ark::internal::Node &node, Ark::internal::Keyword keyword) |
Check if a given node starts with a given keyword. | |
static bool | isBeginBlock (const Ark::internal::Node &node) |
Check if a node is a begin block. | |
static bool | isFuncDef (const Ark::internal::Node &node) |
Check if a node is a function definition (fun (args) body) | |
static bool | isFuncCall (const Ark::internal::Node &node) |
Check if a node is a function call (foo bar egg) | |
static std::size_t | lineOfLastNodeIn (const Ark::internal::Node &node) |
Compute the line on which the deepest right most node of node is at. | |
static std::string | prefix (const std::size_t indent) |
Compute indentation level. | |
Private Attributes | |
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 | m_updated |
True if the original code now difer from the formatted one. | |
Definition at line 14 of file Formatter.hpp.
|
explicit |
Definition at line 14 of file Formatter.cpp.
Formatter::Formatter | ( | std::string | filename, |
bool | dry_run ) |
Definition at line 18 of file Formatter.cpp.
|
nodiscard |
Definition at line 60 of file Formatter.cpp.
References m_updated.
Referenced by main().
|
private |
Handles all node formatting.
node | |
indent | indentation level, starting at 0, increment by 1 |
after_newline | when false, do not add prefix |
Definition at line 168 of file Formatter.cpp.
References Ark::internal::Node::comment(), Ark::internal::Node::commentAfter(), Ark::internal::Node::constList(), format(), formatBlock(), formatComment(), formatMacro(), Ark::internal::Node::keyword(), Ark::internal::keywords, Ark::internal::Node::nodeType(), Ark::internal::Node::number(), output(), prefix(), and Ark::internal::Node::string().
Referenced by format(), formatBegin(), formatCall(), formatCondition(), formatDel(), formatFunction(), formatLoop(), formatMacro(), formatVariable(), processAst(), and shouldSplitOnNewline().
|
private |
Definition at line 386 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), output(), prefix(), and shouldAddNewLineBetweenNodes().
Referenced by formatBlock().
|
private |
Definition at line 239 of file Formatter.cpp.
References Ark::internal::Node::constList(), formatBegin(), formatCall(), formatCondition(), formatDel(), formatFunction(), formatImport(), formatLoop(), formatVariable(), Ark::internal::Node::keyword(), and Ark::internal::Node::nodeType().
Referenced by format().
|
private |
Definition at line 477 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), output(), and prefix().
Referenced by formatBlock().
|
private |
Definition at line 226 of file Formatter.cpp.
References output(), and prefix().
Referenced by format(), and formatFunction().
|
private |
Definition at line 330 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), prefix(), and shouldSplitOnNewline().
Referenced by formatBlock(), and formatMacro().
|
private |
Definition at line 469 of file Formatter.cpp.
References Ark::internal::Node::constList(), and format().
Referenced by formatBlock().
|
private |
Definition at line 274 of file Formatter.cpp.
References Ark::internal::Node::comment(), Ark::internal::Node::constList(), format(), formatComment(), Ark::internal::Node::isListLike(), prefix(), and shouldSplitOnNewline().
Referenced by formatBlock().
|
private |
Definition at line 419 of file Formatter.cpp.
References Ark::internal::Node::comment(), and Ark::internal::Node::constList().
Referenced by formatBlock().
|
private |
Definition at line 364 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), and shouldSplitOnNewline().
Referenced by formatBlock().
|
private |
Definition at line 510 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), formatCondition(), isListStartingWithKeyword(), and output().
Referenced by format().
|
private |
Definition at line 315 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), isFuncDef(), Ark::internal::keywords, and shouldSplitOnNewline().
Referenced by formatBlock().
|
staticnodiscardprivate |
Check if a node is a begin block.
node |
Definition at line 105 of file Formatter.cpp.
References isListStartingWithKeyword().
Referenced by processAst(), and shouldSplitOnNewline().
|
staticnodiscardprivate |
Check if a node is a function call (foo bar egg)
node |
Definition at line 115 of file Formatter.cpp.
References Ark::internal::Node::constList(), and Ark::internal::Node::isListLike().
Referenced by shouldSplitOnNewline().
|
staticnodiscardprivate |
Check if a node is a function definition (fun (args) body)
node |
Definition at line 110 of file Formatter.cpp.
References isListStartingWithKeyword().
Referenced by formatVariable().
|
staticnodiscardprivate |
Check if a given node starts with a given keyword.
node | |
keyword |
Definition at line 100 of file Formatter.cpp.
References Ark::internal::Node::constList(), and Ark::internal::Node::isListLike().
Referenced by formatMacro(), isBeginBlock(), and isFuncDef().
|
staticprivate |
Compute the line on which the deepest right most node of node is at.
node |
Definition at line 120 of file Formatter.cpp.
References Ark::internal::Node::constList(), Ark::internal::Node::isListLike(), Ark::internal::Node::line(), and lineOfLastNodeIn().
Referenced by lineOfLastNodeIn(), and shouldAddNewLineBetweenNodes().
|
nodiscard |
Definition at line 55 of file Formatter.cpp.
References m_output.
Referenced by format(), formatBegin(), formatCall(), formatComment(), formatMacro(), and main().
|
inlinestaticprivate |
Compute indentation level.
indent | indentation level |
Definition at line 111 of file Formatter.hpp.
References FormatterConfig::SpacePerIndent.
Referenced by format(), formatBegin(), formatCall(), formatComment(), formatCondition(), and formatFunction().
|
private |
Definition at line 65 of file Formatter.cpp.
References Ark::internal::Node::constList(), format(), isBeginBlock(), m_dry_run, m_filename, m_output, and shouldAddNewLineBetweenNodes().
Referenced by run(), and runWithString().
void Formatter::run | ( | ) |
Read the file and process it. The file isn't modified.
Definition at line 22 of file Formatter.cpp.
References ARK_NO_NAME_FILE, Ark::internal::Parser::ast(), Ark::Diagnostics::generate(), m_filename, m_output, m_parser, m_updated, Ark::internal::Parser::process(), processAst(), Ark::Utils::readFile(), and warnIfCommentsWereRemoved().
Referenced by main().
void Formatter::runWithString | ( | const std::string & | code | ) |
code | code to process (bypass reading the file if initialized with a filename) |
Definition at line 39 of file Formatter.cpp.
References ARK_NO_NAME_FILE, Ark::internal::Parser::ast(), Ark::Diagnostics::generate(), m_output, m_parser, m_updated, Ark::internal::Parser::process(), processAst(), and warnIfCommentsWereRemoved().
|
nodiscardprivate |
Decide if we should add a newline after a node in a block.
node | a List node |
at | the node we are currently formatting |
Definition at line 145 of file Formatter.cpp.
References Ark::internal::Node::constList(), and lineOfLastNodeIn().
Referenced by formatBegin(), and processAst().
|
nodiscardprivate |
Decide if a node should be split on a newline or not.
node |
Definition at line 132 of file Formatter.cpp.
References Ark::internal::Node::comment(), Ark::internal::Node::constList(), format(), isBeginBlock(), isFuncCall(), Ark::internal::Node::isListLike(), and FormatterConfig::LongLineLength.
Referenced by formatCondition(), formatFunction(), formatLoop(), and formatVariable().
|
private |
Given the original code, produce a warning if comments from it were removed during formatting.
original_code | |
filename |
Definition at line 88 of file Formatter.cpp.
References ARK_NO_NAME_FILE, and m_output.
Referenced by run(), and runWithString().
|
private |
If true, only prints the formatted file instead of saving it to disk.
Definition at line 41 of file Formatter.hpp.
Referenced by processAst().
|
private |
Definition at line 40 of file Formatter.hpp.
Referenced by processAst(), and run().
|
private |
Definition at line 43 of file Formatter.hpp.
Referenced by output(), processAst(), run(), runWithString(), and warnIfCommentsWereRemoved().
|
private |
Definition at line 42 of file Formatter.hpp.
Referenced by run(), and runWithString().
|
private |
True if the original code now difer from the formatted one.
Definition at line 44 of file Formatter.hpp.
Referenced by codeModified(), run(), and runWithString().