ArkScript
A small, fast, functional and scripting language for video games
Predicates.hpp
Go to the documentation of this file.
1#ifndef SRC_PREDICATES_HPP
2#define SRC_PREDICATES_HPP
3
4#include <string>
5#include <cctype>
6
8
9namespace Ark::internal
10{
11 struct CharPred
12 {
13 const std::string name;
14
15 explicit CharPred(std::string n) :
16 name(std::move(n)) {}
17 virtual ~CharPred() = default;
18
19 virtual bool operator()(utf8_char_t::codepoint_t c) const = 0;
20 };
21
22 inline struct IsSpace final : CharPred
23 {
25 CharPred("space") {}
26 bool operator()(const utf8_char_t::codepoint_t c) const override
27 {
28 return 0 <= c && c <= 255 && std::isspace(c) != 0;
29 }
31
32 inline struct IsInlineSpace final : CharPred
33 {
35 CharPred("inline space") {}
36 bool operator()(const utf8_char_t::codepoint_t c) const override
37 {
38 return 0 <= c && c <= 255 && (std::isspace(c) != 0) && (c != '\n') && (c != '\r');
39 }
41
42 inline struct IsDigit final : CharPred
43 {
45 CharPred("digit") {}
46 bool operator()(const utf8_char_t::codepoint_t c) const override
47 {
48 return 0 <= c && c <= 255 && std::isdigit(c) != 0;
49 }
51
52 inline struct IsHex final : CharPred
53 {
55 CharPred("hex") {}
56 bool operator()(const utf8_char_t::codepoint_t c) const override
57 {
58 return 0 <= c && c <= 255 && std::isxdigit(c) != 0;
59 }
61
62 inline struct IsAlpha final : CharPred
63 {
65 CharPred("alphabetic") {}
66 bool operator()(const utf8_char_t::codepoint_t c) const override
67 {
68 return 0 <= c && c <= 255 && std::isalpha(c) != 0;
69 }
71
72 inline struct IsAlnum final : CharPred
73 {
75 CharPred("alphanumeric") {}
76 bool operator()(const utf8_char_t::codepoint_t c) const override
77 {
78 return 0 <= c && c <= 255 && std::isalnum(c) != 0;
79 }
81
82 struct IsChar final : CharPred
83 {
84 explicit IsChar(const char c) :
85 CharPred("'" + std::string(1, c) + "'"), m_k(c)
86 {}
87 explicit IsChar(const utf8_char_t& c) :
88 CharPred(std::string(c.c_str())), m_k(c.codepoint())
89 {}
90 bool operator()(const utf8_char_t::codepoint_t c) const override
91 {
92 return m_k == c;
93 }
94
95 private:
97 };
98
99 struct IsEither final : CharPred
100 {
101 explicit IsEither(const CharPred& a, const CharPred& b) :
102 CharPred("(" + a.name + " | " + b.name + ")"), m_a(a), m_b(b)
103 {}
104 bool operator()(const utf8_char_t::codepoint_t c) const override
105 {
106 return m_a(c) || m_b(c);
107 }
108
109 private:
110 const CharPred& m_a;
111 const CharPred& m_b;
112 };
113
114 struct IsNot final : CharPred
115 {
116 explicit IsNot(const CharPred& a) :
117 CharPred("~" + a.name), m_a(a)
118 {}
119 bool operator()(const utf8_char_t::codepoint_t c) const override
120 {
121 return !m_a(c);
122 }
123
124 private:
125 const CharPred& m_a;
126 };
127
128 inline struct IsSymbol final : CharPred
129 {
131 CharPred("sym") {}
132 bool operator()(const utf8_char_t::codepoint_t c) const override
133 {
134 switch (c)
135 {
136 case ':':
137 case '!':
138 case '?':
139 case '@':
140 case '_':
141 case '-':
142 case '+':
143 case '*':
144 case '/':
145 case '|':
146 case '=':
147 case '<':
148 case '>':
149 case '%':
150 case '$':
151 return true;
152
153 default:
154 return false;
155 }
156 }
158
159 const IsChar IsMinus('-');
160}
161
162#endif
Ark::internal::IsHex IsHex
Ark::internal::IsAlnum IsAlnum
Ark::internal::IsAlpha IsAlpha
Ark::internal::IsDigit IsDigit
const IsChar IsMinus('-')
Ark::internal::IsInlineSpace IsInlineSpace
Ark::internal::IsSymbol IsSymbol
Ark::internal::IsSpace IsSpace
virtual ~CharPred()=default
CharPred(std::string n)
virtual bool operator()(utf8_char_t::codepoint_t c) const =0
const std::string name
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override
IsChar(const utf8_char_t &c)
IsChar(const char c)
const utf8_char_t::codepoint_t m_k
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override
const CharPred & m_b
const CharPred & m_a
IsEither(const CharPred &a, const CharPred &b)
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override
IsNot(const CharPred &a)
const CharPred & m_a
bool operator()(const utf8_char_t::codepoint_t c) const override
bool operator()(const utf8_char_t::codepoint_t c) const override