Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
lexer.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4
5#include <absl/container/flat_hash_map.h>
6#include <fe/lexer.h>
7
8#include "thorin/fe/tok.h"
9
10namespace thorin {
11class World;
12
13class Lexer : public fe::Lexer<3, Lexer> {
14 using Super = fe::Lexer<3, Lexer>;
15
16public:
17 /// Creates a lexer to read Thorin files (see [Lexical Structure](@ref lex)).
18 /// If @p md is not `nullptr`, a Markdown output will be generated.
19 Lexer(World& world, std::istream& istream, const fs::path* path = nullptr, std::ostream* md = nullptr);
20
21 World& world() { return world_; }
22 const fs::path* path() const { return loc_.path; }
23 Loc loc() const { return loc_; }
24 Tok lex();
25
26private:
27 char32_t next() {
28 auto res = Super::next();
29 if (md_ && out_) {
30 if (res == fe::utf8::EoF) {
31 *md_ << "\n```\n";
32 out_ = false;
33 } else if (res) {
34 bool success = fe::utf8::encode(*md_, res);
35 assert_unused(success);
36 }
37 }
38 return res;
39 }
40
41 Tok tok(Tok::Tag tag) { return {loc(), tag}; }
42 Sym sym();
43 Loc cache_trailing_dot();
44 bool lex_id();
45 char8_t lex_char();
46 std::optional<Tok> parse_lit();
47 void parse_digits(int base = 10);
48 bool parse_exp(int base = 10);
49 void eat_comments();
50 bool start_md() const { return ahead(0) == '/' && ahead(1) == '/' && ahead(2) == '/'; }
51 void emit_md(bool start_of_file = false);
52 void md_fence() {
53 if (md_) *md_ << "```\n";
54 }
55
56 World& world_;
57 std::ostream* md_;
58 bool out_ = true;
59 fe::SymMap<Tok::Tag> keywords_;
60 std::optional<Tok> cache_ = std::nullopt;
61
62 friend class fe::Lexer<3, Lexer>;
63};
64
65} // namespace thorin
const fs::path * path() const
Definition lexer.h:22
Loc loc() const
Definition lexer.h:23
Tok lex()
Definition lexer.cpp:31
World & world()
Definition lexer.h:21
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Definition cfg.h:11