![]() |
ArkScript
A small, lisp-inspired, functional scripting language
|
#include <Parser.hpp>
Public Member Functions | |
Parser (unsigned debug, ParserMode mode=ParserMode::Interpret) | |
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 | positioned (Node node, FilePosition cursor) const |
std::optional< Node > & | positioned (std::optional< Node > &node, FilePosition cursor) const |
std::optional< Node > | node () |
std::optional< Node > | letMutSet (FilePosition filepos) |
std::optional< Node > | del (FilePosition filepos) |
std::optional< Node > | condition (FilePosition filepos) |
std::optional< Node > | loop (FilePosition filepos) |
std::optional< Node > | import_ (FilePosition filepos) |
std::optional< Node > | block (FilePosition filepos) |
std::optional< Node > | functionArgs (FilePosition filepos) |
std::optional< Node > | function (FilePosition filepos) |
std::optional< Node > | macroCondition (FilePosition filepos) |
std::optional< Node > | macroArgs (FilePosition filepos) |
std::optional< Node > | macro (FilePosition filepos) |
std::optional< Node > | functionCall (FilePosition filepos) |
std::optional< Node > | list (FilePosition filepos) |
std::optional< Node > | number (FilePosition filepos) |
std::optional< Node > | string (FilePosition filepos) |
std::optional< Node > | field (FilePosition filepos) |
std::optional< Node > | symbol (FilePosition filepos) |
std::optional< Node > | spread (FilePosition filepos) |
std::optional< Node > | nil (FilePosition filepos) |
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)(FilePosition), const std::string &name) |
Try to parse using a given parser, prefixing and suffixing it with (...), handling comments around the parsed node. | |
Private Attributes | |
ParserMode | m_mode |
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. | |
std::size_t | m_nested_nodes |
Nested node counter. | |
std::vector< std::function< std::optional< Node >(FilePosition)> > | m_parsers |
Additional Inherited Members | |
![]() | |
void | initParser (const std::string &filename, const std::string &code) |
FilePosition | getCursor () const |
CodeErrorContext | generateErrorContextAtCurrentPosition () const |
void | error (const std::string &error, FilePosition start_at, const std::optional< CodeErrorContext > &additional_context=std::nullopt) const |
Create an error context and throw an error containing said context. | |
void | errorWithNextToken (const std::string &message, const std::optional< CodeErrorContext > &additional_context=std::nullopt) |
Fetch the next token (space and paren delimited) to generate an error. | |
void | expectSuffixOrError (char suffix, const std::string &context, const std::optional< CodeErrorContext > &additional_context=std::nullopt) |
Check for a closing char or generate an error. | |
long | getCount () |
std::size_t | getSize () const |
bool | isEOF () const |
void | backtrack (long n) |
Backtrack to a given position (this is NOT an offset!) | |
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 | |
std::string | peek () const |
bool | space (std::string *s=nullptr) |
bool | inlineSpace (std::string *s=nullptr) |
bool | comment (std::string *s=nullptr) |
std::string | spaceComment () |
std::string | newlineOrComment () |
bool | prefix (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 36 of file Parser.hpp.
|
explicit |
Constructs a new Parser object.
debug | debug level |
mode | how the parser should behave regarding certain nodes and errors |
Definition at line 7 of file Parser.cpp.
|
private |
Try to parse an atom, if any, match its type against the given list.
types | authorized types |
Definition at line 993 of file Parser.cpp.
References atom().
Referenced by functionCall().
|
nodiscardnoexcept |
Definition at line 92 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 961 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), field(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), m_allow_macro_behavior, nil(), number(), spread(), string(), and symbol().
Referenced by anyAtomOf(), and nodeOrValue().
|
private |
Definition at line 418 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Begin, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and positioned().
|
private |
Definition at line 230 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::If, Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and positioned().
|
private |
Definition at line 210 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(), positioned(), and Ark::internal::Symbol.
|
private |
Definition at line 900 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::Field, Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::name(), positioned(), and Ark::internal::Symbol.
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::BaseParser::getCursor(), Ark::internal::List, m_allow_macro_behavior, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::Node::nodeType(), Ark::internal::BaseParser::oneOf(), positioned(), Ark::internal::Node::push_back(), Ark::internal::Node::string(), and Ark::internal::Symbol.
|
private |
Definition at line 458 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Capture, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::expect(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), positioned(), and Ark::internal::Symbol.
Referenced by function().
|
private |
Definition at line 723 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), anyAtomOf(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::Field, Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), node(), nodeOrValue(), positioned(), and Ark::internal::Symbol.
|
private |
Definition at line 288 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::Node::attachNearestCommentBefore(), Ark::internal::BaseParser::backtrack(), Ark::internal::FilePosition::col, Ark::internal::Import::col, Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), 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(), positioned(), Ark::internal::Import::prefix, Ark::internal::Node::push_back(), Ark::internal::FilePosition::row, Ark::internal::BaseParser::space(), Ark::internal::Symbol, Ark::internal::Import::symbols, Ark::internal::Import::toPackageString(), and Ark::internal::Import::with_prefix.
|
nodiscard |
Definition at line 97 of file Parser.cpp.
References m_imports.
Referenced by Ark::Welder::computeAST(), and Ark::internal::ImportSolver::parseImport().
|
private |
Definition at line 157 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::BaseParser::getCursor(), 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::Node::nodeType(), Ark::internal::nodeTypes, Ark::internal::BaseParser::oneOf(), positioned(), Ark::internal::Node::push_back(), Ark::internal::Set, Ark::internal::Spread, and Ark::internal::Symbol.
|
private |
Definition at line 764 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), Ark::internal::BaseParser::isEOF(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), positioned(), and Ark::internal::Symbol.
|
private |
Definition at line 264 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::List, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), positioned(), and Ark::internal::While.
|
private |
Definition at line 655 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), m_allow_macro_behavior, Ark::internal::Macro, macroArgs(), Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), positioned(), and Ark::internal::Symbol.
|
private |
Definition at line 598 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(), positioned(), Ark::internal::BaseParser::sequence(), Ark::internal::Spread, and Ark::internal::Symbol.
Referenced by macro().
|
private |
Definition at line 565 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::If, Ark::internal::Macro, Ark::internal::BaseParser::newlineOrComment(), nodeOrValue(), Ark::internal::BaseParser::oneOf(), and positioned().
|
private |
Definition at line 947 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::comment(), Ark::internal::Interpret, Ark::internal::List, m_mode, Ark::internal::BaseParser::newlineOrComment(), positioned(), and Ark::internal::Symbol.
Referenced by atom().
|
private |
Definition at line 131 of file Parser.cpp.
References Ark::internal::BaseParser::backtrack(), Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), m_nested_nodes, m_parsers, and Ark::MaxNestedNodes.
Referenced by functionCall(), nodeOrValue(), positioned(), positioned(), and process().
|
private |
Try to parse an atom first, if it fails try to parse a node.
Definition at line 1006 of file Parser.cpp.
References atom(), and node().
Referenced by block(), condition(), function(), functionCall(), letMutSet(), list(), loop(), macro(), and macroCondition().
|
private |
Definition at line 793 of file Parser.cpp.
References Ark::internal::BaseParser::error(), Ark::Utils::isDouble(), positioned(), and Ark::internal::BaseParser::signedNumber().
Referenced by atom().
|
nodiscardprivate |
Definition at line 102 of file Parser.cpp.
References Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::m_filename, and node().
Referenced by block(), condition(), del(), field(), function(), functionArgs(), functionCall(), import_(), letMutSet(), list(), loop(), macro(), macroArgs(), macroCondition(), nil(), number(), spread(), string(), and symbol().
|
nodiscardprivate |
Definition at line 115 of file Parser.cpp.
References Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::m_filename, and node().
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 51 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::BaseParser::peek(), 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 |
Definition at line 935 of file Parser.cpp.
References Ark::internal::BaseParser::errorWithNextToken(), Ark::internal::BaseParser::name(), positioned(), Ark::internal::BaseParser::sequence(), and Ark::internal::Spread.
Referenced by atom().
|
private |
Definition at line 807 of file Parser.cpp.
References Ark::internal::BaseParser::accept(), Ark::internal::BaseParser::backtrack(), utf8::decode(), Ark::internal::BaseParser::error(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::getCount(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::hexNumber(), Ark::internal::Interpret, Ark::internal::BaseParser::isEOF(), m_mode, positioned(), and Ark::internal::String.
Referenced by atom().
|
private |
Definition at line 927 of file Parser.cpp.
References Ark::internal::BaseParser::name(), positioned(), and Ark::internal::Symbol.
Referenced by atom().
|
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 1016 of file Parser.cpp.
References Ark::internal::BaseParser::comment(), Ark::internal::BaseParser::expectSuffixOrError(), Ark::internal::BaseParser::generateErrorContextAtCurrentPosition(), Ark::internal::BaseParser::getCursor(), Ark::internal::BaseParser::name(), Ark::internal::BaseParser::newlineOrComment(), Ark::internal::BaseParser::prefix(), and Ark::internal::BaseParser::spaceComment().
|
private |
Toggled on when inside a macro definition, off afterward.
Definition at line 70 of file Parser.hpp.
Referenced by atom(), function(), letMutSet(), and macro().
|
private |
Definition at line 68 of file Parser.hpp.
|
private |
Definition at line 69 of file Parser.hpp.
|
private |
Definition at line 67 of file Parser.hpp.
Referenced by process().
|
private |
Definition at line 66 of file Parser.hpp.
|
private |
|
private |
Definition at line 72 of file Parser.hpp.
Referenced by node().