14template<
size_t K,
class S>
class Lexer {
16 S& self() {
return *
static_cast<S*
>(
this); }
17 const S& self()
const {
return *
static_cast<const S*
>(
this); }
20 Lexer(std::istream& istream,
const std::filesystem::path* path =
nullptr)
71 template<Append append = Append::On,
class Pred>
bool accept(Pred pred) {
73 auto c = self().next();
85 template<Append append = Append::On>
bool accept(
char32_t c) {
return accept<append>([c](
char32_t d) {
return c == d; }); }
The blueprint for a lexer with a buffer of K tokens to peek into the future (Lexer::ahead).
char32_t next()
Get next char32_t in Lexer::istream_ and increase Lexer::loc_.
void start()
Invoke before assembling the next token.
Lexer(std::istream &istream, const std::filesystem::path *path=nullptr)
Loc loc_
Location of the token we are currently constructing within Lexer::str_,.
Ring< char32_t, K > ahead_
char32_t ahead(size_t i=0) const
Pos peek_
Position of ahead_::first;.
@ Upper
Append accepted char via fe::utf8::toupper` to Lexer::str_.
@ On
Append accepted char as is to Lexer::str_.
@ Lower
Append accepted char via fe::utf8::tolower` to Lexer::str_.
@ Off
Do not append accepted char to Lexer::str_.
A ring buffer with N elements.
T put(T item)
Puts item into buffer.
static constexpr char32_t BOM
Byte Order Mark.
char32_t decode(std::istream &is)
Decodes the next sequence of bytes from is as UTF-32.
char32_t tolower(char32_t c)
char32_t toupper(char32_t c)
static constexpr char32_t EoF
End of File.
Pos finis
It's called finis because it refers to the last character within this Location.
Position in a source file; pass around as value.