Yume
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
yume::ast::parser::Parser Struct Reference

#include <parser.hpp>

Collaboration diagram for yume::ast::parser::Parser:
Collaboration graph
[legend]

Classes

struct  FnArg
 

Public Member Functions

auto clamped_iterator (const TokenIterator &iter) const -> TokenIterator
 
auto emit_note (int offset=0, diagnostic::Severity severity=diagnostic::Severity::Note) const -> diagnostic::Note
 
auto emit_fatal_and_terminate (int offset=0) const noexcept(false) -> diagnostic::Note
 
auto ts (const VectorTokenIterator &entry) const -> span< Token >
 
template<typename T , typename... Args>
auto ast_ptr (const VectorTokenIterator &entry, Args &&... args)
 
template<typename T , typename... Args>
auto ast_ptr (TokenRange &&range, Args &&... args)
 
template<typename T , typename... Args>
auto make_ast (const VectorTokenIterator &entry, Args &&... args)
 
auto make_guard (const string &message) const -> ParserStackTrace
 
auto ignore_separator (source_location location=source_location::current()) -> bool
 Ignore any Separator tokens if any are present. More...
 
void expect (Token::Type token_type, source_location location=source_location::current()) const
 If the next token doesn't have the type, token_type, throw a runtime exception. More...
 
void require_separator (source_location location=source_location::current())
 Consume all subsequent Separator tokens. Throws if none were found. More...
 
void consume (TokenAtom token_atom, source_location location=source_location::current())
 Consume a token of the given type and payload. Throws if it wasn't encountered. More...
 
auto try_consume (TokenAtom token_atom, source_location location=source_location::current()) -> bool
 Attempt to consume a token of the given type and payload. Returns false if it wasn't encountered. More...
 
auto try_peek (int ahead, TokenAtom token_atom, source_location location=source_location::current()) const -> bool
 Check if the token ahead by ahead is of the given type and payload. More...
 
auto try_peek (int ahead, Token::Type token_type, source_location location=source_location::current()) const -> bool
 Check if the token ahead by ahead is of type token_type. More...
 
void consume_with_commas_until (TokenAtom token_atom, std::invocable auto action, const source_location location=source_location::current())
 Consume tokens until a token of the given type and payload is encountered. action (a no-arg function) is called every time. Between each call, a comma is expected. More...
 
template<typename T , std::convertible_to< T > U>
void collect_with_commas_until (TokenAtom token_atom, U(Parser::*action)(), vector< T > &vec, const source_location location=source_location::current())
 Consume tokens until a token of the given type and payload is encountered. action (a no-arg member function) is called every time and its result is appended to vec. Between each call, a comma is expected. More...
 
template<typename T , std::convertible_to< T > U>
auto collect_with_commas_until (TokenAtom token_atom, U(Parser::*action)(), const source_location location=source_location::current()) -> vector< T >
 Consume tokens until a token of the given type and payload is encountered. action (a no-arg member function) is called every time. Between each call, a comma is expected. Returns a vector of all the results of calling action. More...
 
auto next (source_location location=source_location::current()) -> Token
 Return the next token and increment the iterator. More...
 
auto assert_payload_next (source_location location=source_location::current()) -> Atom
 Returns the payload of the next token and increment the iterator. More...
 
auto consume_word (source_location location=source_location::current()) -> string
 Return the payload of the next token. Throws if the next token isn't a Word. More...
 
auto try_peek_uword (int ahead, source_location location=source_location::current()) const -> bool
 Check if the ahead by ahead is a capitalized word. More...
 
auto parse_stmt (bool require_sep=true) -> unique_ptr< Stmt >
 
auto parse_expr () -> unique_ptr< Expr >
 
auto parse_fn_arg () -> FnArg
 
auto parse_generic_type_params () -> vector< GenericParam >
 
auto try_parse_function_type () -> optional< unique_ptr< FunctionType > >
 
auto try_parse_type () -> optional< unique_ptr< Type > >
 
auto parse_type (bool implicit_self=false) -> unique_ptr< Type >
 
auto parse_type_name () -> unique_ptr< TypeName >
 
auto parse_fn_name () -> string
 
auto parse_struct_decl () -> unique_ptr< StructDecl >
 
auto parse_fn_or_ctor_decl () -> unique_ptr< Stmt >
 
auto parse_fn_decl () -> unique_ptr< FnDecl >
 
auto parse_ctor_decl () -> unique_ptr< CtorDecl >
 
auto parse_var_decl () -> unique_ptr< VarDecl >
 
auto parse_const_decl () -> unique_ptr< ConstDecl >
 
auto parse_while_stmt () -> unique_ptr< WhileStmt >
 
auto parse_return_stmt () -> unique_ptr< ReturnStmt >
 
auto parse_if_stmt () -> unique_ptr< IfStmt >
 
auto parse_number_expr () -> unique_ptr< NumberExpr >
 
auto parse_string_expr () -> unique_ptr< StringExpr >
 
auto parse_char_expr () -> unique_ptr< CharExpr >
 
auto parse_primary () -> unique_ptr< Expr >
 
auto parse_receiver (unique_ptr< Expr > receiver, VectorTokenIterator receiver_entry) -> unique_ptr< Expr >
 
auto parse_receiver () -> unique_ptr< Expr >
 
auto parse_unary () -> unique_ptr< Expr >
 
auto parse_lambda () -> unique_ptr< LambdaExpr >
 
auto parse_logical_or () -> unique_ptr< Expr >
 
auto parse_logical_and () -> unique_ptr< Expr >
 
template<size_t N = 0>
auto parse_operator () -> unique_ptr< Expr >
 

Static Public Member Functions

template<typename T , typename U >
static auto ts (T &&begin, U &&end) -> span< Token >
 
static auto operators ()
 
static auto unary_operators ()
 
static auto to_string (Token token) -> string
 
static auto is_uword (string_view word) -> bool
 Check if the string begins with a capital letter. Used for types, as all types must be capitalized. More...
 

Public Attributes

TokenIteratortokens
 
diagnostic::NotesHoldernotes
 

Static Public Attributes

static constexpr auto Symbol = Token::Type::Symbol
 
static constexpr auto Word = Token::Type::Word
 
static constexpr auto Separator = Token::Type::Separator
 
static constexpr auto Number = Token::Type::Number
 

Detailed Description

Definition at line 144 of file parser.hpp.

Member Function Documentation

◆ assert_payload_next()

auto yume::ast::parser::Parser::assert_payload_next ( source_location  location = source_location::current()) -> Atom

Returns the payload of the next token and increment the iterator.

Exceptions
ifthe next token has no payload

Definition at line 124 of file parser.cpp.

References yume::at().

◆ ast_ptr() [1/2]

template<typename T , typename... Args>
auto yume::ast::parser::Parser::ast_ptr ( const VectorTokenIterator entry,
Args &&...  args 
)
inline

Definition at line 171 of file parser.hpp.

References yume::ast::TokenIterator::begin(), and tokens.

◆ ast_ptr() [2/2]

template<typename T , typename... Args>
auto yume::ast::parser::Parser::ast_ptr ( TokenRange &&  range,
Args &&...  args 
)
inline

Definition at line 175 of file parser.hpp.

◆ clamped_iterator()

auto yume::ast::parser::Parser::clamped_iterator ( const TokenIterator iter) const -> TokenIterator
inline

Definition at line 148 of file parser.hpp.

References yume::ast::TokenIterator::end(), and tokens.

Referenced by emit_fatal_and_terminate(), and emit_note().

◆ collect_with_commas_until() [1/2]

template<typename T , std::convertible_to< T > U>
auto yume::ast::parser::Parser::collect_with_commas_until ( TokenAtom  token_atom,
U(Parser::*)()  action,
const source_location  location = source_location::current() 
) -> vector<T>
inline

Consume tokens until a token of the given type and payload is encountered. action (a no-arg member function) is called every time. Between each call, a comma is expected. Returns a vector of all the results of calling action.

Definition at line 270 of file parser.hpp.

References consume(), yume::ast::parser::SYM_COMMA, and try_consume().

◆ collect_with_commas_until() [2/2]

template<typename T , std::convertible_to< T > U>
void yume::ast::parser::Parser::collect_with_commas_until ( TokenAtom  token_atom,
U(Parser::*)()  action,
vector< T > &  vec,
const source_location  location = source_location::current() 
)
inline

Consume tokens until a token of the given type and payload is encountered. action (a no-arg member function) is called every time and its result is appended to vec. Between each call, a comma is expected.

Definition at line 256 of file parser.hpp.

References consume(), yume::ast::parser::SYM_COMMA, and try_consume().

◆ consume()

void yume::ast::parser::Parser::consume ( TokenAtom  token_atom,
source_location  location = source_location::current() 
)

Consume a token of the given type and payload. Throws if it wasn't encountered.

Definition at line 45 of file parser.cpp.

References yume::at(), yume::ast::TokenIterator::at_end(), emit_fatal_and_terminate(), ignore_separator(), to_string(), tokens, and yume::Token::type_name().

Referenced by collect_with_commas_until(), and consume_with_commas_until().

◆ consume_with_commas_until()

void yume::ast::parser::Parser::consume_with_commas_until ( TokenAtom  token_atom,
std::invocable auto  action,
const source_location  location = source_location::current() 
)
inline

Consume tokens until a token of the given type and payload is encountered. action (a no-arg function) is called every time. Between each call, a comma is expected.

Definition at line 242 of file parser.hpp.

References consume(), yume::ast::parser::SYM_COMMA, and try_consume().

◆ consume_word()

auto yume::ast::parser::Parser::consume_word ( source_location  location = source_location::current()) -> string

Return the payload of the next token. Throws if the next token isn't a Word.

Definition at line 136 of file parser.cpp.

References yume::at(), and yume::ast::parser::Word.

◆ emit_fatal_and_terminate()

auto yume::ast::parser::Parser::emit_fatal_and_terminate ( int  offset = 0) const -> diagnostic::Note
inlinenoexcept

◆ emit_note()

auto yume::ast::parser::Parser::emit_note ( int  offset = 0,
diagnostic::Severity  severity = diagnostic::Severity::Note 
) const -> diagnostic::Note
inline

Definition at line 154 of file parser.hpp.

References clamped_iterator(), yume::diagnostic::NotesHolder::emit(), notes, and tokens.

◆ expect()

void yume::ast::parser::Parser::expect ( Token::Type  token_type,
source_location  location = source_location::current() 
) const

If the next token doesn't have the type, token_type, throw a runtime exception.

Definition at line 23 of file parser.cpp.

References yume::at(), yume::ast::TokenIterator::at_end(), emit_fatal_and_terminate(), to_string(), tokens, and yume::Token::type_name().

Referenced by require_separator().

◆ ignore_separator()

auto yume::ast::parser::Parser::ignore_separator ( source_location  location = source_location::current()) -> bool

Ignore any Separator tokens if any are present.

Returns
true if a separator was encountered (and consumed)

Definition at line 11 of file parser.cpp.

References yume::at().

Referenced by consume(), yume::ast::Program::parse(), and require_separator().

◆ is_uword()

static auto yume::ast::parser::Parser::is_uword ( string_view  word) -> bool
inlinestatic

Check if the string begins with a capital letter. Used for types, as all types must be capitalized.

Definition at line 294 of file parser.hpp.

◆ make_ast()

template<typename T , typename... Args>
auto yume::ast::parser::Parser::make_ast ( const VectorTokenIterator entry,
Args &&...  args 
)
inline

Definition at line 179 of file parser.hpp.

References yume::ast::TokenIterator::begin(), and tokens.

◆ make_guard()

auto yume::ast::parser::Parser::make_guard ( const string &  message) const -> ParserStackTrace
inline

Definition at line 193 of file parser.hpp.

References tokens.

◆ next()

auto yume::ast::parser::Parser::next ( source_location  location = source_location::current()) -> Token

Return the next token and increment the iterator.

Definition at line 116 of file parser.cpp.

References yume::at().

◆ operators()

static auto yume::ast::parser::Parser::operators ( )
inlinestatic

◆ parse_char_expr()

auto yume::ast::parser::Parser::parse_char_expr ( ) -> unique_ptr<CharExpr>

Definition at line 713 of file parser.cpp.

References yume::Token::Char.

◆ parse_const_decl()

auto yume::ast::parser::Parser::parse_const_decl ( ) -> unique_ptr<ConstDecl>

Definition at line 592 of file parser.cpp.

References yume::ast::parser::KWD_CONST, and yume::ast::parser::SYM_EQ.

◆ parse_ctor_decl()

auto yume::ast::parser::Parser::parse_ctor_decl ( ) -> unique_ptr<CtorDecl>

◆ parse_expr()

auto yume::ast::parser::Parser::parse_expr ( ) -> unique_ptr<Expr>

Definition at line 352 of file parser.cpp.

Referenced by parse_primary(), and parse_receiver().

◆ parse_fn_arg()

auto yume::ast::parser::Parser::parse_fn_arg ( ) -> FnArg

Definition at line 427 of file parser.cpp.

References yume::ast::parser::SYM_COLON_COLON.

◆ parse_fn_decl()

auto yume::ast::parser::Parser::parse_fn_decl ( ) -> unique_ptr<FnDecl>

◆ parse_fn_name()

auto yume::ast::parser::Parser::parse_fn_name ( ) -> string

◆ parse_fn_or_ctor_decl()

auto yume::ast::parser::Parser::parse_fn_or_ctor_decl ( ) -> unique_ptr<Stmt>

Definition at line 464 of file parser.cpp.

References yume::ast::parser::SYM_COLON.

◆ parse_generic_type_params()

auto yume::ast::parser::Parser::parse_generic_type_params ( ) -> vector<GenericParam>

◆ parse_if_stmt()

auto yume::ast::parser::Parser::parse_if_stmt ( ) -> unique_ptr<IfStmt>

◆ parse_lambda()

auto yume::ast::parser::Parser::parse_lambda ( ) -> unique_ptr<LambdaExpr>

◆ parse_logical_and()

auto yume::ast::parser::Parser::parse_logical_and ( ) -> unique_ptr<Expr>

Definition at line 342 of file parser.cpp.

References parse_logical_and(), and yume::ast::parser::SYM_AND_AND.

Referenced by parse_logical_and().

◆ parse_logical_or()

auto yume::ast::parser::Parser::parse_logical_or ( ) -> unique_ptr<Expr>

Definition at line 332 of file parser.cpp.

References yume::ast::parser::SYM_OR_OR.

◆ parse_number_expr()

auto yume::ast::parser::Parser::parse_number_expr ( ) -> unique_ptr<NumberExpr>

Definition at line 692 of file parser.cpp.

◆ parse_operator()

template<size_t N = 0>
auto yume::ast::parser::Parser::parse_operator ( ) -> unique_ptr<Expr>
inline

◆ parse_primary()

auto yume::ast::parser::Parser::parse_primary ( ) -> unique_ptr<Expr>

◆ parse_receiver() [1/2]

auto yume::ast::parser::Parser::parse_receiver ( ) -> unique_ptr<Expr>

Definition at line 890 of file parser.cpp.

References yume::ast::parser::KWD_DEF.

◆ parse_receiver() [2/2]

auto yume::ast::parser::Parser::parse_receiver ( unique_ptr< Expr receiver,
VectorTokenIterator  receiver_entry 
) -> unique_ptr<Expr>

◆ parse_return_stmt()

auto yume::ast::parser::Parser::parse_return_stmt ( ) -> unique_ptr<ReturnStmt>

Definition at line 626 of file parser.cpp.

References yume::ast::parser::KWD_RETURN.

◆ parse_stmt()

auto yume::ast::parser::Parser::parse_stmt ( bool  require_sep = true) -> unique_ptr<Stmt>

◆ parse_string_expr()

auto yume::ast::parser::Parser::parse_string_expr ( ) -> unique_ptr<StringExpr>

Definition at line 704 of file parser.cpp.

References yume::Token::Literal.

◆ parse_struct_decl()

auto yume::ast::parser::Parser::parse_struct_decl ( ) -> unique_ptr<StructDecl>

◆ parse_type()

auto yume::ast::parser::Parser::parse_type ( bool  implicit_self = false) -> unique_ptr<Type>

◆ parse_type_name()

auto yume::ast::parser::Parser::parse_type_name ( ) -> unique_ptr<TypeName>

Definition at line 321 of file parser.cpp.

References yume::ast::parser::KWD_SELF_ITEM.

◆ parse_unary()

auto yume::ast::parser::Parser::parse_unary ( ) -> unique_ptr<Expr>

Definition at line 898 of file parser.cpp.

Referenced by parse_operator().

◆ parse_var_decl()

auto yume::ast::parser::Parser::parse_var_decl ( ) -> unique_ptr<VarDecl>

Definition at line 578 of file parser.cpp.

References yume::ast::parser::KWD_LET, and yume::ast::parser::SYM_EQ.

◆ parse_while_stmt()

auto yume::ast::parser::Parser::parse_while_stmt ( ) -> unique_ptr<WhileStmt>

Definition at line 606 of file parser.cpp.

References yume::ast::parser::KWD_END, and yume::ast::parser::KWD_WHILE.

◆ require_separator()

void yume::ast::parser::Parser::require_separator ( source_location  location = source_location::current())

Consume all subsequent Separator tokens. Throws if none were found.

Definition at line 33 of file parser.cpp.

References yume::ast::TokenIterator::at_end(), yume::Token::EndOfFile, expect(), ignore_separator(), Separator, and tokens.

◆ to_string()

auto yume::ast::parser::Parser::to_string ( Token  token) -> string
static

Definition at line 39 of file parser.cpp.

Referenced by consume(), and expect().

◆ try_consume()

auto yume::ast::parser::Parser::try_consume ( TokenAtom  token_atom,
source_location  location = source_location::current() 
) -> bool

Attempt to consume a token of the given type and payload. Returns false if it wasn't encountered.

Definition at line 69 of file parser.cpp.

References yume::at().

Referenced by collect_with_commas_until(), consume_with_commas_until(), and parse_operator().

◆ try_parse_function_type()

auto yume::ast::parser::Parser::try_parse_function_type ( ) -> optional<unique_ptr<FunctionType>>

◆ try_parse_type()

auto yume::ast::parser::Parser::try_parse_type ( ) -> optional<unique_ptr<Type>>

◆ try_peek() [1/2]

auto yume::ast::parser::Parser::try_peek ( int  ahead,
Token::Type  token_type,
source_location  location = source_location::current() 
) const -> bool

Check if the token ahead by ahead is of type token_type.

Definition at line 99 of file parser.cpp.

References yume::at(), and yume::Token::type_name().

◆ try_peek() [2/2]

auto yume::ast::parser::Parser::try_peek ( int  ahead,
TokenAtom  token_atom,
source_location  location = source_location::current() 
) const -> bool

Check if the token ahead by ahead is of the given type and payload.

Definition at line 82 of file parser.cpp.

References yume::at(), and yume::Token::type_name().

◆ try_peek_uword()

auto yume::ast::parser::Parser::try_peek_uword ( int  ahead,
source_location  location = source_location::current() 
) const -> bool

Check if the ahead by ahead is a capitalized word.

Definition at line 146 of file parser.cpp.

References yume::at(), and yume::ast::parser::Word.

◆ ts() [1/2]

auto yume::ast::parser::Parser::ts ( const VectorTokenIterator entry) const -> span<Token>
inline

Definition at line 167 of file parser.hpp.

References yume::ast::TokenIterator::begin(), and tokens.

◆ ts() [2/2]

template<typename T , typename U >
static auto yume::ast::parser::Parser::ts ( T &&  begin,
U &&  end 
) -> span<Token>
inlinestatic

Definition at line 163 of file parser.hpp.

References end.

◆ unary_operators()

static auto yume::ast::parser::Parser::unary_operators ( )
inlinestatic

Member Data Documentation

◆ notes

diagnostic::NotesHolder& yume::ast::parser::Parser::notes

Definition at line 146 of file parser.hpp.

Referenced by emit_fatal_and_terminate(), and emit_note().

◆ Number

constexpr auto yume::ast::parser::Parser::Number = Token::Type::Number
staticconstexpr

Definition at line 191 of file parser.hpp.

◆ Separator

constexpr auto yume::ast::parser::Parser::Separator = Token::Type::Separator
staticconstexpr

Definition at line 190 of file parser.hpp.

Referenced by require_separator().

◆ Symbol

constexpr auto yume::ast::parser::Parser::Symbol = Token::Type::Symbol
staticconstexpr

Definition at line 188 of file parser.hpp.

◆ tokens

TokenIterator& yume::ast::parser::Parser::tokens

◆ Word

constexpr auto yume::ast::parser::Parser::Word = Token::Type::Word
staticconstexpr

Definition at line 189 of file parser.hpp.


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