ArkScript
A small, fast, functional and scripting language for video games
Ark::internal::Parser Class Referencefinal

#include <Parser.hpp>

Inheritance diagram for Ark::internal::Parser:
[legend]
Collaboration diagram for Ark::internal::Parser:
[legend]

Public Member Functions

 Parser (unsigned debug, bool interpret=true)
 Constructs a new Parser object.
 
void process (const std::string &filename, const std::string &code)
 Parse the given code.
 
const Nodeast () const noexcept
 
const std::vector< Import > & imports () const
 
- Public Member Functions inherited from Ark::internal::BaseParser
 BaseParser ()=default
 

Private Member Functions

NodesetNodePosAndFilename (Node &node, const std::optional< FilePosition > &cursor=std::nullopt) const
 Update a node given a file position.
 
std::optional< Nodenode ()
 
std::optional< NodeletMutSet ()
 
std::optional< Nodedel ()
 
std::optional< Nodecondition ()
 
std::optional< Nodeloop ()
 
std::optional< Nodeimport_ ()
 
std::optional< Nodeblock ()
 
std::optional< NodefunctionArgs ()
 
std::optional< Nodefunction ()
 
std::optional< NodemacroCondition ()
 
std::optional< NodemacroArgs ()
 
std::optional< Nodemacro ()
 
std::optional< NodefunctionCall ()
 
std::optional< Nodelist ()
 
std::optional< Nodenumber ()
 
std::optional< Nodestring ()
 
std::optional< Nodefield ()
 
std::optional< Nodesymbol ()
 
std::optional< Nodespread ()
 
std::optional< Nodenil ()
 
std::optional< Nodeatom ()
 Try to parse an atom (number, string, spread, field, symbol, nil)
 
std::optional< NodeanyAtomOf (std::initializer_list< NodeType > types)
 Try to parse an atom, if any, match its type against the given list.
 
std::optional< NodenodeOrValue ()
 Try to parse an atom first, if it fails try to parse a node.
 
std::optional< Nodewrapped (std::optional< Node >(Parser::*parser)(), const std::string &name)
 Try to parse using a given parser, prefixing and suffixing it with (...), handling comments around the parsed node.
 

Private Attributes

bool m_interpret
 interpret escape codes in strings
 
Logger m_logger
 
Node m_ast
 
std::vector< Importm_imports
 
unsigned m_allow_macro_behavior
 Toggled on when inside a macro definition, off afterward.
 

Additional Inherited Members

- Protected Member Functions inherited from Ark::internal::BaseParser
void initParser (const std::string &filename, const std::string &code)
 
FilePosition getCursor () const
 
void error (const std::string &error, std::string exp)
 
void errorWithNextToken (const std::string &message)
 Fetch the next token (space and paren delimited) to generate an error.
 
void errorMissingSuffix (char suffix, const std::string &node_name)
 Generate an error for a given node when a suffix is missing.
 
long getCount ()
 
std::size_t getSize () const
 
bool isEOF () const
 
void backtrack (long n)
 
bool accept (const CharPred &t, std::string *s=nullptr)
 check if a Character Predicate was able to parse, call next() if matching
 
bool expect (const CharPred &t, std::string *s=nullptr)
 heck if a Character Predicate was able to parse, call next() if matching ; throw a CodeError if it doesn't match
 
bool space (std::string *s=nullptr)
 
bool inlineSpace (std::string *s=nullptr)
 
bool comment (std::string *s=nullptr)
 
bool spaceComment (std::string *s=nullptr)
 
bool newlineOrComment (std::string *s=nullptr)
 
bool prefix (char c)
 
bool suffix (char c)
 
bool number (std::string *s=nullptr)
 
bool signedNumber (std::string *s=nullptr)
 
bool hexNumber (unsigned length, std::string *s=nullptr)
 
bool name (std::string *s=nullptr)
 
bool sequence (const std::string &s)
 
bool packageName (std::string *s=nullptr)
 
bool anyUntil (const CharPred &delim, std::string *s=nullptr)
 Match any char that do not match the predicate.
 
bool oneOf (std::initializer_list< std::string > words, std::string *s=nullptr)
 Fetch a token and try to match one of the given words.
 
- Protected Attributes inherited from Ark::internal::BaseParser
std::string m_filename
 

Detailed Description

Definition at line 30 of file Parser.hpp.

Constructor & Destructor Documentation

◆ Parser()

Ark::internal::Parser::Parser ( unsigned debug,
bool interpret = true )
explicit

Constructs a new Parser object.

Parameters
debugdebug level
interpretinterpret escape codes in strings

Definition at line 7 of file Parser.cpp.

Member Function Documentation

◆ anyAtomOf()

std::optional< Node > Ark::internal::Parser::anyAtomOf ( std::initializer_list< NodeType > types)
private

Try to parse an atom, if any, match its type against the given list.

Parameters
typesautorized types
Returns
std::optional<Node> std::nullopt if the parsed atom didn't match the given types

Definition at line 859 of file Parser.cpp.

References atom(), Ark::internal::BaseParser::getCursor(), and setNodePosAndFilename().

Referenced by functionCall().

◆ ast()

const Node & Ark::internal::Parser::ast ( ) const
nodiscardnoexcept
Returns
const Node& resulting AST after processing the given code

Definition at line 48 of file Parser.cpp.

References m_ast.

Referenced by Ark::Welder::computeAST(), Ark::internal::ImportSolver::parseImport(), Formatter::run(), and Formatter::runWithString().

◆ atom()

std::optional< Node > Ark::internal::Parser::atom ( )
private

Try to parse an atom (number, string, spread, field, symbol, nil)

Returns
std::optional<Node> std::nullopt if no atom could be parsed

Definition at line 828 of file Parser.cpp.

References Ark::internal::BaseParser::backtrack(), field(), Ark::internal::BaseParser::getCount(), m_allow_macro_behavior, nil(), number(), spread(), string(), and symbol().

Referenced by anyAtomOf(), functionCall(), and nodeOrValue().

◆ block()

◆ condition()

◆ del()

◆ field()

std::optional< Node > Ark::internal::Parser::field ( )
inlineprivate

Definition at line 196 of file Parser.hpp.

Referenced by atom().

◆ function()

◆ functionArgs()

◆ functionCall()

◆ import_()

◆ imports()

const std::vector< Import > & Ark::internal::Parser::imports ( ) const
nodiscard
Returns
const std::vector<Import>& list of imports detected by the parser

Definition at line 53 of file Parser.cpp.

References m_imports.

Referenced by Ark::Welder::computeAST(), and Ark::internal::ImportSolver::parseImport().

◆ letMutSet()

◆ list()

◆ loop()

◆ macro()

◆ macroArgs()

◆ macroCondition()

◆ nil()

std::optional< Node > Ark::internal::Parser::nil ( )
inlineprivate

Definition at line 242 of file Parser.hpp.

References Ark::internal::Node::attachNearestCommentBefore().

Referenced by atom().

◆ node()

◆ nodeOrValue()

std::optional< Node > Ark::internal::Parser::nodeOrValue ( )
private

Try to parse an atom first, if it fails try to parse a node.

Returns
std::optional<Node> std::nullopt if no atom or node could be parsed

Definition at line 874 of file Parser.cpp.

References atom(), Ark::internal::BaseParser::getCursor(), node(), and setNodePosAndFilename().

Referenced by block(), condition(), function(), functionCall(), letMutSet(), list(), loop(), macro(), and macroCondition().

◆ number()

std::optional< Node > Ark::internal::Parser::number ( )
inlineprivate

Definition at line 89 of file Parser.hpp.

Referenced by atom().

◆ process()

◆ setNodePosAndFilename()

Node & Ark::internal::Parser::setNodePosAndFilename ( Node & node,
const std::optional< FilePosition > & cursor = std::nullopt ) const
private

Update a node given a file position.

Parameters
nodenode to update
cursorthe node position in file
Returns
Node& the modified node

Definition at line 58 of file Parser.cpp.

References Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::m_filename, node(), and Ark::internal::Node::setPos().

Referenced by anyAtomOf(), block(), condition(), del(), function(), functionArgs(), functionCall(), import_(), letMutSet(), list(), loop(), macro(), macroArgs(), macroCondition(), nodeOrValue(), and wrapped().

◆ spread()

std::optional< Node > Ark::internal::Parser::spread ( )
inlineprivate

Definition at line 230 of file Parser.hpp.

Referenced by atom().

◆ string()

std::optional< Node > Ark::internal::Parser::string ( )
inlineprivate

Definition at line 105 of file Parser.hpp.

References utf8::decode().

Referenced by atom().

◆ symbol()

std::optional< Node > Ark::internal::Parser::symbol ( )
inlineprivate

Definition at line 222 of file Parser.hpp.

Referenced by atom(), del(), functionArgs(), import_(), letMutSet(), and macro().

◆ wrapped()

std::optional< Node > Ark::internal::Parser::wrapped ( std::optional< Node >(Parser::* parser )(),
const std::string & name )
private

Try to parse using a given parser, prefixing and suffixing it with (...), handling comments around the parsed node.

Parameters
parserparser method returning a std::optional<Node>
nameconstruction name, eg "let", "condition"
Returns
std::optional<Node> std::nullopt if the parser didn't match

Definition at line 891 of file Parser.cpp.

References Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorMissingSuffix(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), Ark::internal::BaseParser::prefix(), setNodePosAndFilename(), Ark::internal::BaseParser::spaceComment(), and Ark::internal::BaseParser::suffix().

Referenced by node().

Member Data Documentation

◆ m_allow_macro_behavior

unsigned Ark::internal::Parser::m_allow_macro_behavior
private

Toggled on when inside a macro definition, off afterward.

Definition at line 64 of file Parser.hpp.

Referenced by atom(), function(), letMutSet(), and macro().

◆ m_ast

Node Ark::internal::Parser::m_ast
private

Definition at line 62 of file Parser.hpp.

Referenced by ast(), and process().

◆ m_imports

std::vector<Import> Ark::internal::Parser::m_imports
private

Definition at line 63 of file Parser.hpp.

Referenced by import_(), and imports().

◆ m_interpret

bool Ark::internal::Parser::m_interpret
private

interpret escape codes in strings

Definition at line 60 of file Parser.hpp.

◆ m_logger

Logger Ark::internal::Parser::m_logger
private

Definition at line 61 of file Parser.hpp.

Referenced by process().


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