codex
Loading...
Searching...
No Matches
lexer.hpp
Go to the documentation of this file.
1#ifndef CODEX_LUA_LEXER_H
2#define CODEX_LUA_LEXER_H
3
4#include "utils.hpp"
5
6constexpr fixed_string SPACE = " \t\n";
7
8constexpr fixed_string DIGITS = "0123456789";
9
11 "abcdefghijklmnopqrstuvwxyz"
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 "_";
14
16
17constexpr std::string_view ignore_interm(std::string_view s) {
18 s = s.substr(s.find_first_not_of(SPACE));
19 for(;;) {
20 if(slice(s, 0, 2) != "--")
21 return s;
22 auto i = std::ranges::find(s, '\n');
23 if(i != end(s))
24 ++i;
25 s = {i, end(s)};
26 }
27}
28
29constexpr std::string_view consume(std::string_view s, std::string_view c) {
30 if(!s.starts_with(c))
31 throw "invalid prefix";
32 return s.substr(c.size());
33}
34
35template<fixed_string s>
36constexpr bool set_contains(char c) {
37 return std::ranges::find(s, c) != s.end();
38}
39
40constexpr std::string_view consume_set_common(std::string_view s, auto i) {
41 if(i == begin(s))
42 throw "pattern not matched";
43 return {i, end(s)};
44}
45
46template<fixed_string p>
47constexpr std::string_view consume_set(std::string_view s) {
48 return consume_set_common(s, std::ranges::find_if_not(s, set_contains<p>));
49}
50
51template<fixed_string p>
52constexpr std::string_view consume_set_comp(std::string_view s) {
53 return consume_set_common(s, std::ranges::find_if(s, set_contains<p>));
54}
55
56#endif
constexpr std::string_view consume_set(std::string_view s)
Definition lexer.hpp:47
constexpr fixed_string DIGITS
Definition lexer.hpp:8
constexpr bool set_contains(char c)
Definition lexer.hpp:36
constexpr fixed_string SPACE
Definition lexer.hpp:6
constexpr fixed_string IDENT0
Definition lexer.hpp:10
constexpr std::string_view ignore_interm(std::string_view s)
Definition lexer.hpp:17
constexpr std::string_view consume(std::string_view s, std::string_view c)
Definition lexer.hpp:29
constexpr fixed_string IDENT1
Definition lexer.hpp:15
constexpr std::string_view consume_set_comp(std::string_view s)
Definition lexer.hpp:52
constexpr std::string_view consume_set_common(std::string_view s, auto i)
Definition lexer.hpp:40
Definition mult_inh.c:27
Definition utils.hpp:13
constexpr const char * end(void) const
Definition utils.hpp:20
constexpr fixed_string s
Definition test.cpp:6
constexpr auto slice(std::string_view s, std::size_t b, std::size_t e)
Definition utils.hpp:74