tcalc 0.2.0
 
Loading...
Searching...
No Matches
tokenizer.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <functional>
15#include <map>
16#include <string>
17#include <string_view>
18
19#include "tcalc/common.hpp"
20#include "tcalc/error.hpp"
21#include "tcalc/token.hpp"
22
23namespace tcalc::token {
24
29class TCALC_PUBLIC Tokenizer
30{
31public:
33 std::greater<std::string_view>;
35 inline static const std::map<std::string_view, TokenType, KeywordComp>
36 KEYWORDS = {
37 { "def", TokenType::DEF }, { "let", TokenType::LET },
38 { "if", TokenType::IF }, { "then", TokenType::THEN },
39 { "else", TokenType::ELSE }, { "import", TokenType::IMPORT },
40 { "==", TokenType::EQUAL }, { "!=", TokenType::NOTEQUAL },
41 { ">=", TokenType::GREATEREQUAL }, { "<=", TokenType::LESSEQUAL },
42 { "&&", TokenType::AND }, { "||", TokenType::OR },
43 { "+", TokenType::PLUS }, { "-", TokenType::MINUS },
44 { "*", TokenType::MULTIPLY }, { "/", TokenType::DIVIDE },
45 { "(", TokenType::LPAREN }, { ")", TokenType::RPAREN },
46 { ",", TokenType::COMMA }, { ";", TokenType::SEMICOLON },
47 { "=", TokenType::ASSIGN }, { ">", TokenType::GREATER },
48 { "<", TokenType::LESS }, { "!", TokenType::NOT },
49 };
51 constexpr static char QUOTE = '\'';
53private:
54 std::string_view _input;
55 std::string_view::const_iterator _pos;
56
57public:
63 explicit Tokenizer(std::string_view input)
64 : _input{ input }
65 , _pos{ _input.begin() }
66 {
67 }
68
75
81 [[nodiscard]] TCALC_INLINE auto pos() const noexcept { return _pos; }
82
88 [[nodiscard]] TCALC_INLINE auto spos() const noexcept
89 {
90 return std::distance(_input.begin(), _pos);
91 }
92
93private:
101 template<typename Pred>
102 auto _next_with(TokenType type, const Pred& pred)
103 {
104 const auto* start = _pos;
105 while (_pos != _input.end() && pred(*_pos)) {
106 ++_pos;
107 }
108 return Token{ type, std::string{ start, _pos } };
109 }
110
116 template<typename Pred>
117 void _skip_with(const Pred& pred)
118 {
119 while (_pos != _input.end() && pred(*_pos)) {
120 ++_pos;
121 }
122 }
123
128 error::Result<Token> _parse_number();
129
135 error::Result<Token> _parse_quoted_identifier();
136
144 bool _is_keyword(std::string_view keyword);
145
153 static bool _is_identifier_char(char c);
154
162 static bool _is_first_identifier_char(char c);
163
171 static bool _is_skippable_char(char c);
172};
173
174}
Tokenize the input string into tokens.
Definition tokenizer.hpp:30
TCALC_INLINE auto spos() const noexcept
Get the current position of the tokenizer as an index.
Definition tokenizer.hpp:88
TCALC_INLINE auto pos() const noexcept
Get the current position of the tokenizer.
Definition tokenizer.hpp:81
Tokenizer(std::string_view input)
Construct a new Tokenizer object.
Definition tokenizer.hpp:63
std::greater< std::string_view > KeywordComp
Definition tokenizer.hpp:33
tcalc common header.
tcalc error definition.
_TCALC_EXPECTED_NS::expected< T, Error > Result
Result type.
Definition error.hpp:153
Input token definition.
TokenType
Token type, some values are ASCII code of the corresponding character.
Definition token.hpp:25