|
FE 0.13.1
Header-only C++ frontend library
|
The blueprint for a recursive descent/ ascent parser using a K lookahead of Tokens.
More...
#include <fe/parser.h>
Classes | |
| class | Tracker |
| class | Anchor |
RAII helper that anchors a Tag for its lifetime; use Parser::anchor to build one. More... | |
Protected Member Functions | |
Construction | |
| void | init () |
Tracker | |
Track Location in the 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. | |
| Tracker | tracker (Pos begin) |
As above but start tracking at begin. | |
| Tracker | tracker (Loc begin) |
Shift Token | |
| Tok | ahead (size_t i=0) const |
| 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. | |
| template<class... Args> | |
| Tok | expect (Tag tag, std::format_string< Args... > fmt, Args &&... args) |
| As above but builds the context via std::format. | |
| Tok | eat (Tag tag) |
Consume Parser::ahead which must be a tag; asserts otherwise. | |
Anchor | |
An anchor is a E.g., while parsing a parenthesized expression, ) is anchored: a nested parser must not swallow it but bail out, so the enclosing context can Parser::expect it. A ) that is not anchored, however, is simply bogus and Parser::recover discards it. | |
| Anchor | anchor (Tag tag) |
Factory method to build a Parser::Anchor; Parser::expect tag yourself at the end of the scope. | |
| bool | anchored (Tag tag) const |
Is tag anchored by an enclosing context? | |
| template<std::predicate< Tag > P> | |
| void | recover (P pred, std::string_view ctxt) |
Parser::lex all Tokens whose Tag satisfies pred and that are not Parser::anchored; report each one as S::unanchored_err. | |
| void | recover (Tag tag, std::string_view ctxt) |
As above but only recovers from tag. | |
Diagnostics | |
The defaults | |
| fe::Error & | error () |
| const fe::Error & | error () const |
| void | syntax_err (std::string_view what, Tok tok, std::string_view ctxt) |
Parser::expect did not find what while parsing ctxt. | |
| void | syntax_err (std::string_view what, std::string_view ctxt) |
As above but uses Parser::ahead as tok. | |
| void | syntax_err (Tag tag, std::string_view ctxt) |
As above but spells tag out via Parser::tag2str_. | |
| void | unanchored_err (Tok tok, std::string_view ctxt) |
Parser::recover discarded tok while parsing ctxt. | |
Static Protected Member Functions | |
| static auto | tag2str_ (Tag tag) |
Spells tag out via Tok::tag2str if there is one - a bare enumerator would render as its number. | |
Protected Attributes | |
| Ring< Tok, K > | ahead_ |
| Loc | curr_ |
| std::deque< Tag > | anchors_ |
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. Hence, Tok must be default-constructible and testable as a bool (to check for that failure):
S must provide the Lexer to pull from and the Driver to report to:
Parser::syntax_err and Parser::unanchored_err come with a default; declare either in S to word it differently. All Parser::syntax_err overloads funnel through the one taking a what/Tok/ctxt; override just that one.
S hides all of them, so add using Super::syntax_err;.
|
inlineprotected |
If Parser::ahead() is a tag, consume and return it, otherwise yield std::nullopt.
|
inlineprotected |
|
inlinenodiscardprotected |
Factory method to build a Parser::Anchor; Parser::expect tag yourself at the end of the scope.
Use like this:
|
inlineprotected |
|
inlineprotected |
Consume Parser::ahead which must be a tag; asserts otherwise.
|
inlineprotected |
Definition at line 198 of file parser.h.
References fe::Error::error().
|
inlineprotected |
Definition at line 199 of file parser.h.
References fe::Error::error().
|
inlineprotected |
|
inlineprotected |
Parser::lex Parser::ahead() which must be a tag.
Issue error with ctxt otherwise.
|
inlineprotected |
|
inlineprotected |
|
inlineprotected |
Parser::lex all Tokens whose Tag satisfies pred and that are not Parser::anchored; report each one as S::unanchored_err.
This turns an otherwise fatal Token into a mere error message and keeps the current parser going.
Definition at line 184 of file parser.h.
References ahead(), anchored(), and lex().
Referenced by recover().
|
inlineprotected |
|
inlineprotected |
As above but uses Parser::ahead as tok.
Definition at line 211 of file parser.h.
References ahead().
|
inlineprotected |
Parser::expect did not find what while parsing ctxt.
Backtick what yourself if it is a literal token rather than a phrase.
|
inlineprotected |
As above but spells tag out via Parser::tag2str_.
Definition at line 214 of file parser.h.
References ahead(), and tag2str_().
|
inlinestaticprotected |
Spells tag out via Tok::tag2str if there is one - a bare enumerator would render as its number.
Definition at line 226 of file parser.h.
Referenced by syntax_err().
|
inlineprotected |
Factory method to build a Parser::Tracker.
|
inlineprotected |
Definition at line 95 of file parser.h.
References fe::Loc::begin, and curr_.
|
inlineprotected |
|
inlineprotected |
Parser::recover discarded tok while parsing ctxt.
|
protected |
|
protected |
Definition at line 235 of file parser.h.
Referenced by anchored().
|
protected |