Line data Source code
1 : #ifndef TOKEN_STREAM_H
2 : #define TOKEN_STREAM_H
3 :
4 : #include "../errors.h"
5 : #include "token.h"
6 : #include "type.h"
7 : #include <cstddef>
8 : #include <expected>
9 : #include <limits>
10 : #include <variant>
11 : #include <vector>
12 :
13 : namespace nicole {
14 :
15 : // represents the tokens returned by the lexer
16 : class TokenStream final {
17 : private:
18 : std::vector<Token> tokens_;
19 : std::size_t currentPos_{0};
20 :
21 : public:
22 : explicit TokenStream(const std::vector<Token> &tokens) noexcept
23 198 : : tokens_{tokens} {}
24 :
25 90 : [[nodiscard]] size_t size() const noexcept { return tokens_.size(); };
26 :
27 : [[nodiscard]] const std::expected<std::monostate, Error> eat() noexcept;
28 :
29 27 : [[nodiscard]] size_t currentPos() const noexcept { return currentPos_; }
30 :
31 : [[nodiscard]] bool isEnd() const noexcept;
32 :
33 : [[nodiscard]] const std::expected<Token, Error> current() const noexcept;
34 :
35 : [[nodiscard]] const std::expected<Token, Error>
36 : lookAhead(const size_t pos) const noexcept;
37 :
38 : [[nodiscard]] const std::expected<Token, Error>
39 : lastRead() const noexcept;
40 :
41 : [[nodiscard]] const std::expected<bool, Error>
42 : isCurrentTokenType(const TokenType type) const noexcept;
43 :
44 : [[nodiscard]] bool
45 : isTokenAheadBeforeSemicolon(const TokenType type) const noexcept;
46 :
47 : // needed for whenever an import is found
48 : [[nodiscard]] const std::expected<std::monostate, Error>
49 : insertAfter(const TokenStream &tkStream,
50 : const size_t pos = std::numeric_limits<int>::infinity()) noexcept;
51 :
52 : void shiftToSemicolon() noexcept;
53 :
54 9 : [[nodiscard]] auto begin() const noexcept { return tokens_.begin(); }
55 :
56 9 : [[nodiscard]] auto end() const noexcept { return tokens_.end(); }
57 : };
58 :
59 : } // namespace nicole
60 :
61 : #endif
|