FE 0.5.0
A header-only C++ library for writing frontends
Loading...
Searching...
No Matches
fe::Parser< Tok, Tag, K, S > Class Template Reference

The blueprint for a recursive descent/ ascent parser using a K lookahead of Tokens. More...

#include <fe/parser.h>

Classes

class  Tracker
 

Protected Member Functions

Construction
void init (const std::filesystem::path *path)
 
Track Location in Source File

Use like this:

auto track = tracker();
auto foo = parse_foo();
auto bar = parse_bar();
auto foobar = new FooBar(track, foo, bar);
Tracker tracker()
Factory method to build a Parser::Tracker.
Definition parser.h:71
Tracker tracker ()
 Factory method to build a Parser::Tracker.
 
Shift Token
Tok ahead (size_t i=0) const
 Get lookahead.
 
Tok lex ()
 Invoke Lexer to retrieve next Token.
 
Tok accept (Tag tag)
 If Parser::ahead() is a tag, consume and return it, otherwise yield std::nullopt.
 
Tok expect (Tag tag, std::string_view ctxt)
 Parser::lex Parser::ahead() which must be a tag.
 
Tok eat (Tag tag)
 Consume Parser::ahead which must be a tag; asserts otherwise.
 

Protected Attributes

Ring< Tok, K > ahead_
 
Loc prev_
 

Detailed Description

template<class Tok, class Tag, size_t K, class S>
requires (std::is_convertible_v<Tok, bool> || std::is_constructible_v<bool, Tok>) || std::is_default_constructible_v<Tok>
class fe::Parser< Tok, Tag, K, S >

The blueprint for a recursive descent/ ascent parser using a K lookahead of Tokens.

Parser::accept and Parser::expect indicate failure by constructing a Token with its default constructor. Provide a conversion operator to bool to check for an error:

class Tok {
public:
enum class Tag {
Nil,
// ...
};
// ...
explicit bool operator() const { return tag_ != Tag::Nil; }
// ...
};
// Your Parser:
if (auto tok = accept(Tok::Tag:My_Tag)) {
do_sth(tok);
}
Tok accept(Tag tag)
If Parser::ahead() is a tag, consume and return it, otherwise yield std::nullopt.
Definition parser.h:89

Definition at line 31 of file parser.h.

Member Function Documentation

◆ accept()

template<class Tok , class Tag , size_t K, class S >
Tok fe::Parser< Tok, Tag, K, S >::accept ( Tag  tag)
inlineprotected

If Parser::ahead() is a tag, consume and return it, otherwise yield std::nullopt.

Definition at line 89 of file parser.h.

◆ ahead()

template<class Tok , class Tag , size_t K, class S >
Tok fe::Parser< Tok, Tag, K, S >::ahead ( size_t  i = 0) const
inlineprotected

Get lookahead.

Definition at line 78 of file parser.h.

◆ eat()

template<class Tok , class Tag , size_t K, class S >
Tok fe::Parser< Tok, Tag, K, S >::eat ( Tag  tag)
inlineprotected

Consume Parser::ahead which must be a tag; asserts otherwise.

Definition at line 103 of file parser.h.

◆ expect()

template<class Tok , class Tag , size_t K, class S >
Tok fe::Parser< Tok, Tag, K, S >::expect ( Tag  tag,
std::string_view  ctxt 
)
inlineprotected

Parser::lex Parser::ahead() which must be a tag.

Issue error with ctxt otherwise.

Definition at line 96 of file parser.h.

◆ init()

template<class Tok , class Tag , size_t K, class S >
void fe::Parser< Tok, Tag, K, S >::init ( const std::filesystem::path *  path)
inlineprotected

Definition at line 39 of file parser.h.

◆ lex()

template<class Tok , class Tag , size_t K, class S >
Tok fe::Parser< Tok, Tag, K, S >::lex ( )
inlineprotected

Invoke Lexer to retrieve next Token.

Definition at line 81 of file parser.h.

◆ tracker()

template<class Tok , class Tag , size_t K, class S >
Tracker fe::Parser< Tok, Tag, K, S >::tracker ( )
inlineprotected

Factory method to build a Parser::Tracker.

Definition at line 71 of file parser.h.

References fe::Loc::begin, and fe::Parser< Tok, Tag, K, S >::Tracker::loc().

Member Data Documentation

◆ ahead_

template<class Tok , class Tag , size_t K, class S >
Ring<Tok, K> fe::Parser< Tok, Tag, K, S >::ahead_
protected

Definition at line 109 of file parser.h.

◆ prev_

template<class Tok , class Tag , size_t K, class S >
Loc fe::Parser< Tok, Tag, K, S >::prev_
protected

Definition at line 110 of file parser.h.


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