|
FE 0.16.1
Fast, Effecient FrontEnds
|
The blueprint for a recursive descent/ ascent parser using a K lookahead of Tokens.
More...
#include <fe/parser.h>
Classes | |
| class | Tracker |
| struct | Anchor |
| One anchor; the left end opened the context, the right one is what it is still waiting for. More... | |
| class | ScopedAnchor |
| RAII helper that pushes a Parser::Anchor for its lifetime; use Parser::anchor to build one. More... | |
| struct | Expected |
| What Parser::expect was after - a Tag, or free-form markup. 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 the Nil Tok(). | |
| Tok | expect (Tag tag, Cite ctxt) |
Parser::lex Parser::ahead() which must be a tag. | |
| template<class... Args> | |
| Tok | expect (Tag tag, cite_string< Args... > fmt, Args &&... args) |
| As above but builds the context via fe::format_cite. | |
| 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. | |
| ScopedAnchor | anchor (Tok l_tok, Tag r_tag) |
Factory method to build a Parser::ScopedAnchor; Parser::expect r_tag yourself at the end of the scope. | |
| const Anchor * | find_anchor (Tag r_tag) const |
The innermost Anchor waiting for r_tag - nullptr if no enclosing context is. | |
| bool | is_anchored (Tag r_tag) const |
Is r_tag anchored by an enclosing context? | |
| template<std::predicate< Tag > P> | |
| void | recover (P pred, Cite ctxt) |
Parser::lex all Tokens whose Tag satisfies pred and that are not Parser::is_anchored; report the whole run as a single S::unanchored_err. | |
| void | recover (Tag tag, Cite ctxt) |
As above but only recovers from tag. | |
Diagnostics | |
The defaults Each yields the Error it reported into, so a Note can be chained. | |
| fe::Error & | error () |
| const fe::Error & | error () const |
| fe::Error & | syntax_err (Expected what, Cite ctxt, Tok got={}) |
Parser::expect did not find what while parsing ctxt; got defaults to Parser::ahead. | |
| fe::Error & | unanchored_err (Tok tok, Loc loc, size_t n, Cite ctxt) |
Parser::recover discarded a run of n Tokens starting with tok and spanning loc 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_ |
| Vector< Anchor, 8 > | 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 an fe::Token - default-constructible, testable as a bool, and tagged Tag::Nil:
S must provide the Lexer to pull from and the Driver to report to - publicly, so that fe::Lexable and fe::Diagnosable hold outside this class too:
Parser::syntax_err and Parser::unanchored_err come with a default; declare either in S to word it differently. Each is one name with one signature, so a declaration in S replaces it outright - no using of the base needed.
| struct fe::Parser::Anchor |
|
inlineprotected |
If Parser::ahead() is a tag, consume and return it, otherwise yield the Nil Tok().
|
inlineprotected |
|
inlinenodiscardprotected |
Factory method to build a Parser::ScopedAnchor; Parser::expect r_tag yourself at the end of the scope.
l_tok is the Token that opened this context and Parser::syntax_err notes it on a missing r_tag, which is all it takes to point a ) that never came back at its (. Use like this:
|
inlineprotected |
Consume Parser::ahead which must be a tag; asserts otherwise.
|
inlineprotected |
Definition at line 269 of file parser.h.
Referenced by unanchored_err().
|
inlineprotected |
|
inlineprotected |
Parser::lex Parser::ahead() which must be a tag.
Issue error with ctxt otherwise.
|
inlineprotected |
As above but builds the context via fe::format_cite.
Definition at line 153 of file parser.h.
References ahead(), fe::format_cite(), and lex().
|
inlineprotected |
The innermost Anchor waiting for r_tag - nullptr if no enclosing context is.
Scans the innermost anchor first, but any enclosing context counts.
Definition at line 212 of file parser.h.
References anchors_.
Referenced by is_anchored().
|
inlineprotected |
|
inlineprotected |
Is r_tag anchored by an enclosing context?
Definition at line 220 of file parser.h.
References find_anchor().
Referenced by recover().
|
inlineprotected |
|
inlineprotected |
Parser::lex all Tokens whose Tag satisfies pred and that are not Parser::is_anchored; report the whole run as a single S::unanchored_err.
This turns an otherwise fatal Token into a mere error message and keeps the current parser going. One mistake discards one run, so one run is one diagnostic: a message per token buries the real error.
Definition at line 227 of file parser.h.
References ahead(), is_anchored(), and lex().
Referenced by recover().
|
inlineprotected |
|
inlineprotected |
Parser::expect did not find what while parsing ctxt; got defaults to Parser::ahead.
ctxt is markup, so backtick a literal token within it yourself. A what the innermost Anchor is waiting for gets a Note pointing back at the Token that opened it - the ( of a ) that never came.
|
inlinestaticprotected |
Spells tag out via Tok::tag2str if there is one - a bare enumerator would render as its number.
Definition at line 299 of file parser.h.
References fe::format_cite().
Referenced by fe::Parser< Tok, Tag, K, S >::Expected::Expected().
|
inlineprotected |
Factory method to build a Parser::Tracker.
|
inlineprotected |
Definition at line 121 of file parser.h.
References fe::Loc::begin, and curr_.
|
inlineprotected |
|
inlineprotected |
Parser::recover discarded a run of n Tokens starting with tok and spanning loc while parsing ctxt.
Definition at line 289 of file parser.h.
References fe::Error::e(), and error().
|
protected |
|
protected |
Definition at line 310 of file parser.h.
Referenced by find_anchor().
|
protected |