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

The parser is responsible of constructing the Abstract Syntax Tree from a token list. More...

#include <Parser.hpp>

+ Collaboration diagram for Ark::internal::Parser:

Public Member Functions

 Parser (unsigned debug, uint16_t options, const std::vector< std::string > &lib_env) noexcept
 Construct a new Parser object.
 
void feed (const std::string &code, const std::string &filename=ARK_NO_NAME_FILE)
 Give the code to parse.
 
const Nodeast () const noexcept
 Return the generated AST.
 
const std::vector< std::string > & getImports () const noexcept
 Return the list of files imported by the code given to the parser.
 

Private Member Functions

void sugar (std::vector< Token > &tokens) noexcept
 Applying syntactic sugar: {...} => (begin...), [...] => (list ...)
 
Node parse (std::list< Token > &tokens, bool authorize_capture=false, bool authorize_field_read=false, bool in_macro=false)
 Parse a list of tokens recursively.
 
void parseIf (Node &, std::list< Token > &, bool)
 
void parseLetMut (Node &, Token &, std::list< Token > &, bool)
 
void parseSet (Node &, Token &, std::list< Token > &, bool)
 
void parseFun (Node &, Token &, std::list< Token > &, bool)
 
void parseWhile (Node &, Token &, std::list< Token > &, bool)
 
void parseBegin (Node &, std::list< Token > &, bool)
 
void parseImport (Node &, std::list< Token > &)
 
void parseQuote (Node &, std::list< Token > &, bool)
 
void parseDel (Node &, std::list< Token > &)
 
Node parseShorthand (Token &, std::list< Token > &, bool)
 
void checkForInvalidTokens (Node &, Token &, bool, bool, bool)
 
Token nextToken (std::list< Token > &tokens)
 Get the next token if possible, from a list of tokens.
 
Node atom (const Token &token)
 Convert a token to a node.
 
bool checkForInclude (Node &n, Node &parent, std::size_t pos=0)
 Search for all the includes in a given node, in its sub-nodes and replace them by the code of the included file.
 
std::string seekFile (const std::string &file)
 Seek a file in the lib folder and everywhere.
 
void expect (bool pred, const std::string &message, Token token)
 Throw a parse exception is the given predicated is false.
 
void throwParseError (const std::string &message, Token token)
 Throw a parse error related to a token (seek it in the related file and highlight the error)
 

Private Attributes

unsigned m_debug
 
std::vector< std::string > m_libenv
 
uint16_t m_options
 
Lexer m_lexer
 
Node m_ast
 
Token m_last_token
 
std::string m_file
 
std::string m_code
 
std::vector< std::string > m_parent_include
 

Friends

std::ostream & operator<< (std::ostream &os, const Parser &P) noexcept
 

Detailed Description

The parser is responsible of constructing the Abstract Syntax Tree from a token list.

Definition at line 43 of file Parser.hpp.

Constructor & Destructor Documentation

◆ Parser()

Ark::internal::Parser::Parser ( unsigned  debug,
uint16_t  options,
const std::vector< std::string > &  lib_env 
)
noexcept

Construct a new Parser object.

Parameters
debugthe debug level
optionsthe parsing options
lib_envfallback library search path

Definition at line 13 of file Parser.cpp.

Member Function Documentation

◆ ast()

const Node & Ark::internal::Parser::ast ( ) const
noexcept

Return the generated AST.

Returns
const Node&

Definition at line 59 of file Parser.cpp.

References m_ast.

Referenced by checkForInclude(), Ark::Compiler::feed(), and Ark::JsonCompiler::feed().

◆ atom()

◆ checkForInclude()

bool Ark::internal::Parser::checkForInclude ( Node n,
Node parent,
std::size_t  pos = 0 
)
private

Search for all the includes in a given node, in its sub-nodes and replace them by the code of the included file.

Parameters
n
parentthe parent node of the current one
posthe position of the child node in the parent node list
Returns
true if we found an import and replaced it by the corresponding code

Definition at line 480 of file Parser.cpp.

References ast(), Ark::Utils::canonicalRelPath(), checkForInclude(), Ark::internal::Node::constList(), feed(), Ark::internal::Import, Ark::internal::Node::keyword(), Ark::internal::Keyword, Ark::internal::Node::list(), Ark::internal::List, m_debug, m_file, m_libenv, m_options, m_parent_include, Ark::internal::Node::nodeType(), Ark::Utils::readFile(), seekFile(), and Ark::internal::String.

Referenced by checkForInclude(), and feed().

◆ checkForInvalidTokens()

void Ark::internal::Parser::checkForInvalidTokens ( Node atomized,
Token token,
bool  previous_token_was_lparen,
bool  authorize_capture,
bool  authorize_field_read 
)
private

◆ expect()

void Ark::internal::Parser::expect ( bool  pred,
const std::string &  message,
internal::Token  token 
)
private

Throw a parse exception is the given predicated is false.

Parameters
pred
messageerror message to use
tokenconcerned token

Definition at line 579 of file Parser.cpp.

References throwParseError().

Referenced by nextToken(), parse(), parseBegin(), parseDel(), parseFun(), parseIf(), parseImport(), parseLetMut(), parseQuote(), parseSet(), and parseWhile().

◆ feed()

void Ark::internal::Parser::feed ( const std::string &  code,
const std::string &  filename = ARK_NO_NAME_FILE 
)

◆ getImports()

const std::vector< std::string > & Ark::internal::Parser::getImports ( ) const
noexcept

Return the list of files imported by the code given to the parser.

Each path of each imported file is relative to the filename given when feeding the parser.

Returns
const std::vector<std::string>&

Definition at line 64 of file Parser.cpp.

References m_parent_include.

◆ nextToken()

Token Ark::internal::Parser::nextToken ( std::list< Token > &  tokens)
private

Get the next token if possible, from a list of tokens.

The list of tokens is modified.

Parameters
tokenslist of tokens to get the next token from
Returns
Token

Definition at line 401 of file Parser.cpp.

References expect(), and m_last_token.

Referenced by parse(), parseDel(), parseIf(), parseImport(), parseLetMut(), parseSet(), and parseWhile().

◆ parse()

Node Ark::internal::Parser::parse ( std::list< Token > &  tokens,
bool  authorize_capture = false,
bool  authorize_field_read = false,
bool  in_macro = false 
)
private

◆ parseBegin()

void Ark::internal::Parser::parseBegin ( Node block,
std::list< Token > &  tokens,
bool  in_macro 
)
private

Definition at line 308 of file Parser.cpp.

References expect(), m_last_token, parse(), and Ark::internal::Node::push_back().

Referenced by parse().

◆ parseDel()

void Ark::internal::Parser::parseDel ( Node block,
std::list< Token > &  tokens 
)
private

◆ parseFun()

void Ark::internal::Parser::parseFun ( Node block,
Token token,
std::list< Token > &  tokens,
bool  in_macro 
)
private

◆ parseIf()

void Ark::internal::Parser::parseIf ( Node block,
std::list< Token > &  tokens,
bool  in_macro 
)
private

◆ parseImport()

void Ark::internal::Parser::parseImport ( Node block,
std::list< Token > &  tokens 
)
private

◆ parseLetMut()

void Ark::internal::Parser::parseLetMut ( Node block,
Token token,
std::list< Token > &  tokens,
bool  in_macro 
)
private

◆ parseQuote()

void Ark::internal::Parser::parseQuote ( Node block,
std::list< Token > &  tokens,
bool  in_macro 
)
private

Definition at line 330 of file Parser.cpp.

References expect(), parse(), and Ark::internal::Node::push_back().

Referenced by parse().

◆ parseSet()

void Ark::internal::Parser::parseSet ( Node block,
Token token,
std::list< Token > &  tokens,
bool  in_macro 
)
private

◆ parseShorthand()

◆ parseWhile()

void Ark::internal::Parser::parseWhile ( Node block,
Token token,
std::list< Token > &  tokens,
bool  in_macro 
)
private

◆ seekFile()

std::string Ark::internal::Parser::seekFile ( const std::string &  file)
private

Seek a file in the lib folder and everywhere.

Parameters
file
Returns
std::string

Definition at line 547 of file Parser.cpp.

References Ark::Utils::fileExists(), Ark::Utils::getDirectoryFromPath(), Ark::Utils::getFilenameFromPath(), m_debug, m_file, and m_libenv.

Referenced by checkForInclude().

◆ sugar()

void Ark::internal::Parser::sugar ( std::vector< Token > &  tokens)
privatenoexcept

Applying syntactic sugar: {...} => (begin...), [...] => (list ...)

Parameters
tokensa list of tokens

Definition at line 69 of file Parser.cpp.

References Ark::internal::Grouping, Ark::internal::Identifier, and Ark::internal::Keyword.

Referenced by feed().

◆ throwParseError()

void Ark::internal::Parser::throwParseError ( const std::string &  message,
internal::Token  token 
)
private

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const Parser P 
)
friend

Definition at line 597 of file Parser.cpp.

Member Data Documentation

◆ m_ast

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

Definition at line 86 of file Parser.hpp.

Referenced by ast(), and feed().

◆ m_code

std::string Ark::internal::Parser::m_code
private

Definition at line 92 of file Parser.hpp.

Referenced by feed(), and throwParseError().

◆ m_debug

unsigned Ark::internal::Parser::m_debug
private

Definition at line 82 of file Parser.hpp.

Referenced by checkForInclude(), feed(), parseShorthand(), and seekFile().

◆ m_file

std::string Ark::internal::Parser::m_file
private

Definition at line 90 of file Parser.hpp.

Referenced by atom(), checkForInclude(), feed(), parse(), parseShorthand(), seekFile(), and throwParseError().

◆ m_last_token

Token Ark::internal::Parser::m_last_token
private

Definition at line 87 of file Parser.hpp.

Referenced by feed(), nextToken(), parse(), parseBegin(), parseFun(), parseIf(), parseLetMut(), and parseSet().

◆ m_lexer

Lexer Ark::internal::Parser::m_lexer
private

Definition at line 85 of file Parser.hpp.

Referenced by feed().

◆ m_libenv

std::vector<std::string> Ark::internal::Parser::m_libenv
private

Definition at line 83 of file Parser.hpp.

Referenced by checkForInclude(), and seekFile().

◆ m_options

uint16_t Ark::internal::Parser::m_options
private

Definition at line 84 of file Parser.hpp.

Referenced by checkForInclude().

◆ m_parent_include

std::vector<std::string> Ark::internal::Parser::m_parent_include
private

Definition at line 94 of file Parser.hpp.

Referenced by checkForInclude(), feed(), and getImports().


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