![]() |
ArkScript
A small, fast, functional and scripting language for video games
|
#include <Parser.hpp>
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 Node & | ast () const noexcept |
const std::vector< Import > & | imports () const |
![]() | |
BaseParser ()=default | |
Private Member Functions | |
Node & | setNodePosAndFilename (Node &node, const std::optional< FilePosition > &cursor=std::nullopt) const |
Update a node given a file position. | |
std::optional< Node > | node () |
std::optional< Node > | letMutSet () |
std::optional< Node > | del () |
std::optional< Node > | condition () |
std::optional< Node > | loop () |
std::optional< Node > | import_ () |
std::optional< Node > | block () |
std::optional< Node > | functionArgs () |
std::optional< Node > | function () |
std::optional< Node > | macroCondition () |
std::optional< Node > | macroArgs () |
std::optional< Node > | macro () |
std::optional< Node > | functionCall () |
std::optional< Node > | list () |
std::optional< Node > | number () |
std::optional< Node > | string () |
std::optional< Node > | field () |
std::optional< Node > | symbol () |
std::optional< Node > | spread () |
std::optional< Node > | nil () |
std::optional< Node > | atom () |
Try to parse an atom (number, string, spread, field, symbol, nil) | |
std::optional< Node > | anyAtomOf (std::initializer_list< NodeType > types) |
Try to parse an atom, if any, match its type against the given list. | |
std::optional< Node > | nodeOrValue () |
Try to parse an atom first, if it fails try to parse a node. | |
std::optional< Node > | wrapped (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< Import > | m_imports |
unsigned | m_allow_macro_behavior |
Toggled on when inside a macro definition, off afterward. | |
Additional Inherited Members | |
![]() | |
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. | |
![]() | |
std::string | m_filename |
Definition at line 30 of file Parser.hpp.
|
explicit |
Constructs a new Parser object.
debug | debug level |
interpret | interpret escape codes in strings |
Definition at line 7 of file Parser.cpp.
|
private |
Try to parse an atom, if any, match its type against the given list.
types | autorized types |
Definition at line 859 of file Parser.cpp.
References atom(), Ark::internal::BaseParser::getCursor(), and setNodePosAndFilename().
Referenced by functionCall().
|
nodiscardnoexcept |
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().
|
private |
Try to parse an atom (number, string, spread, field, symbol, nil)
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().
|
private |
Definition at line 408 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Begin, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and setNodePosAndFilename().
Referenced by node().
|
private |
Definition at line 199 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), condition(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::If, Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and setNodePosAndFilename().
Referenced by condition(), loop(), macroCondition(), and node().
|
private |
Definition at line 176 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), Ark::internal::Del, Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::List, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), Ark::internal::BaseParser::oneOf(), setNodePosAndFilename(), Ark::internal::Symbol, and symbol().
Referenced by node().
|
inlineprivate |
Definition at line 196 of file Parser.hpp.
Referenced by atom().
|
private |
Definition at line 501 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::Fun, functionArgs(), Ark::internal::BaseParser::getCount(), Ark::internal::List, m_allow_macro_behavior, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::Node::nodeType(), Ark::internal::BaseParser::oneOf(), setNodePosAndFilename(), Ark::internal::Node::string(), and Ark::internal::Symbol.
Referenced by node().
|
private |
Definition at line 450 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Node::attachNearestCommentBefore(), Ark::internal::BaseParser::backtrack(), Ark::internal::Capture, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), node(), setNodePosAndFilename(), Ark::internal::Symbol, and symbol().
Referenced by function().
|
private |
Definition at line 752 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), anyAtomOf(), atom(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expect(), Ark::internal::Field, Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), node(), nodeOrValue(), setNodePosAndFilename(), and Ark::internal::Symbol.
Referenced by node().
|
private |
Definition at line 272 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Node::attachNearestCommentBefore(), Ark::internal::BaseParser::backtrack(), Ark::internal::Import::col, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), Ark::internal::Import, Ark::internal::Import::is_glob, Ark::internal::BaseParser::isEOF(), Ark::internal::Import::line, Ark::internal::List, Ark::internal::Node::list(), m_imports, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), Ark::internal::BaseParser::oneOf(), Ark::internal::Import::package, Ark::internal::BaseParser::packageName(), Ark::internal::Import::prefix, Ark::internal::Node::push_back(), setNodePosAndFilename(), Ark::internal::BaseParser::space(), Ark::internal::Symbol, symbol(), Ark::internal::Import::symbols, Ark::internal::Import::toPackageString(), and Ark::internal::Import::with_prefix.
Referenced by node().
|
nodiscard |
Definition at line 53 of file Parser.cpp.
References m_imports.
Referenced by Ark::Welder::computeAST(), and Ark::internal::ImportSolver::parseImport().
|
private |
Definition at line 121 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::getCount(), Ark::internal::Let, Ark::internal::List, m_allow_macro_behavior, Ark::internal::Macro, Ark::internal::Mut, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::nodeTypes, Ark::internal::BaseParser::oneOf(), Ark::internal::Set, setNodePosAndFilename(), Ark::internal::Spread, Ark::internal::Symbol, and symbol().
Referenced by node().
|
private |
Definition at line 797 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), setNodePosAndFilename(), and Ark::internal::Symbol.
Referenced by node().
|
private |
Definition at line 242 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), condition(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), setNodePosAndFilename(), and Ark::internal::While.
Referenced by node().
|
private |
Definition at line 677 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::getCount(), m_allow_macro_behavior, Ark::internal::Macro, macroArgs(), Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), setNodePosAndFilename(), Ark::internal::Symbol, and symbol().
Referenced by node().
|
private |
Definition at line 612 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), Ark::internal::BaseParser::sequence(), setNodePosAndFilename(), Ark::internal::Spread, and Ark::internal::Symbol.
Referenced by macro().
|
private |
Definition at line 571 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), condition(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::If, Ark::internal::Macro, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and setNodePosAndFilename().
Referenced by node().
|
inlineprivate |
Definition at line 242 of file Parser.hpp.
References Ark::internal::Node::attachNearestCommentBefore().
Referenced by atom().
|
private |
Definition at line 69 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), block(), condition(), del(), function(), functionCall(), Ark::internal::BaseParser::getCount(), import_(), letMutSet(), list(), loop(), macro(), macroCondition(), and wrapped().
Referenced by functionArgs(), functionCall(), nodeOrValue(), process(), and setNodePosAndFilename().
|
private |
Try to parse an atom first, if it fails try to parse a node.
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().
|
inlineprivate |
Definition at line 89 of file Parser.hpp.
Referenced by atom().
void Ark::internal::Parser::process | ( | const std::string & | filename, |
const std::string & | code ) |
Parse the given code.
filename | can be left empty, used for error generation |
code | content of the file |
Definition at line 14 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::initParser(), Ark::internal::BaseParser::isEOF(), Ark::internal::Node::list(), m_ast, m_logger, Ark::internal::BaseParser::newlineOrComment(), node(), Ark::internal::Node::push_back(), Ark::internal::BaseParser::spaceComment(), Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().
Referenced by Ark::Welder::computeAST(), Ark::internal::ImportSolver::parseImport(), Formatter::run(), and Formatter::runWithString().
|
private |
Update a node given a file position.
node | node to update |
cursor | the node position in file |
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().
|
inlineprivate |
Definition at line 230 of file Parser.hpp.
Referenced by atom().
|
inlineprivate |
|
inlineprivate |
Definition at line 222 of file Parser.hpp.
Referenced by atom(), del(), functionArgs(), import_(), letMutSet(), and macro().
|
private |
Try to parse using a given parser, prefixing and suffixing it with (...), handling comments around the parsed node.
parser | parser method returning a std::optional<Node> |
name | construction name, eg "let", "condition" |
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().
|
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().
|
private |
Definition at line 62 of file Parser.hpp.
|
private |
Definition at line 63 of file Parser.hpp.
|
private |
interpret escape codes in strings
Definition at line 60 of file Parser.hpp.
|
private |
Definition at line 61 of file Parser.hpp.
Referenced by process().