ArkScript
A small, lisp-inspired, functional scripting language
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 IsOct final : CharPred
63 {
65 CharPred("oct") {}
66 bool operator()(const utf8_char_t::codepoint_t c) const override
67 {
68 return '0' <= c && c <= '7';
69 }
71
72 inline struct IsAlpha final : CharPred
73 {
75 CharPred("alphabetic") {}
76 bool operator()(const utf8_char_t::codepoint_t c) const override
77 {
78 return 0 <= c && c <= 255 && std::isalpha(c) != 0;
79 }
81
82 inline struct IsAlnum final : CharPred
83 {
85 CharPred("alphanumeric") {}
86 bool operator()(const utf8_char_t::codepoint_t c) const override
87 {
88 return 0 <= c && c <= 255 && std::isalnum(c) != 0;
89 }
91
92 struct IsChar final : CharPred
93 {
94 explicit IsChar(const char c) :
95 CharPred("'" + std::string(1, c) + "'"), m_k(c)
96 {}
97 explicit IsChar(const utf8_char_t& c) :
98 CharPred(std::string(c.c_str())), m_k(c.codepoint())
99 {}
100 bool operator()(const utf8_char_t::codepoint_t c) const override
101 {
102 return m_k == c;
103 }
104
105 private:
107 };
108
109 struct IsEither final : CharPred
110 {
111 explicit IsEither(const CharPred& a, const CharPred& b) :
112 CharPred("(" + a.name + " | " + b.name + ")"), m_a(a), m_b(b)
113 {}
114 bool operator()(const utf8_char_t::codepoint_t c) const override
115 {
116 return m_a(c) || m_b(c);
117 }
118
119 private:
120 const CharPred& m_a;
121 const CharPred& m_b;
122 };
123
124 struct IsNot final : CharPred
125 {
126 explicit IsNot(const CharPred& a) :
127 CharPred("~" + a.name), m_a(a)
128 {}
129 bool operator()(const utf8_char_t::codepoint_t c) const override
130 {
131 return !m_a(c);
132 }
133
134 private:
135 const CharPred& m_a;
136 };
137
138 inline struct IsSymbol final : CharPred
139 {
141 CharPred("sym") {}
142 bool operator()(const utf8_char_t::codepoint_t c) const override
143 {
144 switch (c)
145 {
146 case ':':
147 case '!':
148 case '?':
149 case '@':
150 case '_':
151 case '-':
152 case '+':
153 case '*':
154 case '/':
155 case '|':
156 case '=':
157 case '<':
158 case '>':
159 case '%':
160 case '$':
161 return true;
162
163 default:
164 return false;
165 }
166 }
168
169 const IsChar IsMinus('-');
170}
171
172#endif
Ark::internal::IsHex IsHex
Ark::internal::IsAlnum IsAlnum
Ark::internal::IsAlpha IsAlpha
Ark::internal::IsDigit IsDigit
Ark::internal::IsOct IsOct
const IsChar IsMinus('-')
Ark::internal::IsInlineSpace IsInlineSpace
Ark::internal::IsSymbol IsSymbol
Ark::internal::IsSpace IsSpace
STL namespace.
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
bool operator()(const utf8_char_t::codepoint_t c) const override