ArkScript
A small, fast, functional and scripting language for video games
Formatter Class Referencefinal

#include <Formatter.hpp>

Collaboration diagram for Formatter:
[legend]

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.
 

Detailed Description

Definition at line 14 of file Formatter.hpp.

Constructor & Destructor Documentation

◆ Formatter() [1/2]

Formatter::Formatter ( bool dry_run)
explicit

Definition at line 14 of file Formatter.cpp.

◆ Formatter() [2/2]

Formatter::Formatter ( std::string filename,
bool dry_run )

Definition at line 18 of file Formatter.cpp.

Member Function Documentation

◆ codeModified()

bool Formatter::codeModified ( ) const
nodiscard
Returns
true if code has been modified by the formatter

Definition at line 60 of file Formatter.cpp.

References m_updated.

Referenced by main().

◆ format()

std::string Formatter::format ( const Ark::internal::Node & node,
std::size_t indent,
bool after_newline )
private

◆ formatBegin()

std::string Formatter::formatBegin ( const Ark::internal::Node & node,
std::size_t indent,
bool after_newline )
private

◆ formatBlock()

std::string Formatter::formatBlock ( const Ark::internal::Node & node,
std::size_t indent,
bool after_newline )
private

◆ formatCall()

std::string Formatter::formatCall ( const Ark::internal::Node & node,
std::size_t indent )
private

Definition at line 477 of file Formatter.cpp.

References Ark::internal::Node::constList(), format(), output(), and prefix().

Referenced by formatBlock().

◆ formatComment()

std::string Formatter::formatComment ( const std::string & comment,
std::size_t indent ) const
private

Definition at line 226 of file Formatter.cpp.

References output(), and prefix().

Referenced by format(), and formatFunction().

◆ formatCondition()

std::string Formatter::formatCondition ( const Ark::internal::Node & node,
std::size_t indent,
bool is_macro = false )
private

Definition at line 330 of file Formatter.cpp.

References Ark::internal::Node::constList(), format(), prefix(), and shouldSplitOnNewline().

Referenced by formatBlock(), and formatMacro().

◆ formatDel()

std::string Formatter::formatDel ( const Ark::internal::Node & node,
std::size_t indent )
private

Definition at line 469 of file Formatter.cpp.

References Ark::internal::Node::constList(), and format().

Referenced by formatBlock().

◆ formatFunction()

std::string Formatter::formatFunction ( const Ark::internal::Node & node,
std::size_t indent )
private

◆ formatImport()

std::string Formatter::formatImport ( const Ark::internal::Node & node,
std::size_t indent )
private

Definition at line 419 of file Formatter.cpp.

References Ark::internal::Node::comment(), and Ark::internal::Node::constList().

Referenced by formatBlock().

◆ formatLoop()

std::string Formatter::formatLoop ( const Ark::internal::Node & node,
std::size_t indent )
private

Definition at line 364 of file Formatter.cpp.

References Ark::internal::Node::constList(), format(), and shouldSplitOnNewline().

Referenced by formatBlock().

◆ formatMacro()

std::string Formatter::formatMacro ( const Ark::internal::Node & node,
std::size_t indent )
private

◆ formatVariable()

std::string Formatter::formatVariable ( const Ark::internal::Node & node,
std::size_t indent )
private

◆ isBeginBlock()

bool Formatter::isBeginBlock ( const Ark::internal::Node & node)
staticnodiscardprivate

Check if a node is a begin block.

Parameters
node
Returns
bool

Definition at line 105 of file Formatter.cpp.

References isListStartingWithKeyword().

Referenced by processAst(), and shouldSplitOnNewline().

◆ isFuncCall()

bool Formatter::isFuncCall ( const Ark::internal::Node & node)
staticnodiscardprivate

Check if a node is a function call (foo bar egg)

Parameters
node
Returns
bool

Definition at line 115 of file Formatter.cpp.

References Ark::internal::Node::constList(), and Ark::internal::Node::isListLike().

Referenced by shouldSplitOnNewline().

◆ isFuncDef()

bool Formatter::isFuncDef ( const Ark::internal::Node & node)
staticnodiscardprivate

Check if a node is a function definition (fun (args) body)

Parameters
node
Returns
bool

Definition at line 110 of file Formatter.cpp.

References isListStartingWithKeyword().

Referenced by formatVariable().

◆ isListStartingWithKeyword()

bool Formatter::isListStartingWithKeyword ( const Ark::internal::Node & node,
Ark::internal::Keyword keyword )
staticnodiscardprivate

Check if a given node starts with a given keyword.

Parameters
node
keyword
Returns
bool

Definition at line 100 of file Formatter.cpp.

References Ark::internal::Node::constList(), and Ark::internal::Node::isListLike().

Referenced by formatMacro(), isBeginBlock(), and isFuncDef().

◆ lineOfLastNodeIn()

std::size_t Formatter::lineOfLastNodeIn ( const Ark::internal::Node & node)
staticprivate

Compute the line on which the deepest right most node of node is at.

Parameters
node
Returns

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().

◆ output()

const std::string & Formatter::output ( ) const
nodiscard

Definition at line 55 of file Formatter.cpp.

References m_output.

Referenced by format(), formatBegin(), formatCall(), formatComment(), formatMacro(), and main().

◆ prefix()

static std::string Formatter::prefix ( const std::size_t indent)
inlinestaticprivate

Compute indentation level.

Parameters
indentindentation level
Returns
std::string

Definition at line 111 of file Formatter.hpp.

References FormatterConfig::SpacePerIndent.

Referenced by format(), formatBegin(), formatCall(), formatComment(), formatCondition(), and formatFunction().

◆ processAst()

void Formatter::processAst ( const Ark::internal::Node & ast)
private

◆ run()

void Formatter::run ( )

◆ runWithString()

void Formatter::runWithString ( const std::string & code)
Parameters
codecode 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().

◆ shouldAddNewLineBetweenNodes()

bool Formatter::shouldAddNewLineBetweenNodes ( const Ark::internal::Node & node,
std::size_t at )
nodiscardprivate

Decide if we should add a newline after a node in a block.

Parameters
nodea List node
atthe node we are currently formatting
Returns

Definition at line 145 of file Formatter.cpp.

References Ark::internal::Node::constList(), and lineOfLastNodeIn().

Referenced by formatBegin(), and processAst().

◆ shouldSplitOnNewline()

bool Formatter::shouldSplitOnNewline ( const Ark::internal::Node & node)
nodiscardprivate

Decide if a node should be split on a newline or not.

Parameters
node
Returns
bool

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().

◆ warnIfCommentsWereRemoved()

void Formatter::warnIfCommentsWereRemoved ( const std::string & original_code,
const std::string & filename )
private

Given the original code, produce a warning if comments from it were removed during formatting.

Parameters
original_code
filename

Definition at line 88 of file Formatter.cpp.

References ARK_NO_NAME_FILE, and m_output.

Referenced by run(), and runWithString().

Member Data Documentation

◆ m_dry_run

bool Formatter::m_dry_run
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().

◆ m_filename

const std::string Formatter::m_filename
private

Definition at line 40 of file Formatter.hpp.

Referenced by processAst(), and run().

◆ m_output

std::string Formatter::m_output
private

Definition at line 43 of file Formatter.hpp.

Referenced by output(), processAst(), run(), runWithString(), and warnIfCommentsWereRemoved().

◆ m_parser

Ark::internal::Parser Formatter::m_parser
private

Definition at line 42 of file Formatter.hpp.

Referenced by run(), and runWithString().

◆ m_updated

bool Formatter::m_updated
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().


The documentation for this class was generated from the following files: