Commit feae89a3 authored by Dominik Charousset's avatar Dominik Charousset

Add parse function for atoms

parent 2cfa6ee6
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <cstdint> #include <cstdint>
#include "caf/detail/parser/state.hpp" #include "caf/detail/parser/state.hpp"
#include "caf/fwd.hpp"
#include "caf/string_view.hpp" #include "caf/string_view.hpp"
namespace caf { namespace caf {
...@@ -54,5 +55,9 @@ void parse(parse_state& ps, float& x); ...@@ -54,5 +55,9 @@ void parse(parse_state& ps, float& x);
void parse(parse_state& ps, double& x); void parse(parse_state& ps, double& x);
// -- CAF types ----------------------------------------------------------------
void parse(parse_state& ps, atom_value& x);
} // namespace detail } // namespace detail
} // namespace caf } // namespace caf
...@@ -41,7 +41,8 @@ namespace parser { ...@@ -41,7 +41,8 @@ namespace parser {
/// Reads a number, i.e., on success produces either an `int64_t` or a /// Reads a number, i.e., on success produces either an `int64_t` or a
/// `double`. /// `double`.
template <class Iterator, class Sentinel, class Consumer> template <class Iterator, class Sentinel, class Consumer>
void read_atom(state<Iterator, Sentinel>& ps, Consumer& consumer) { void read_atom(state<Iterator, Sentinel>& ps, Consumer&& consumer,
bool accept_unquoted = false) {
size_t pos = 0; size_t pos = 0;
char buf[11]; char buf[11];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
...@@ -58,10 +59,12 @@ void read_atom(state<Iterator, Sentinel>& ps, Consumer& consumer) { ...@@ -58,10 +59,12 @@ void read_atom(state<Iterator, Sentinel>& ps, Consumer& consumer) {
if (ps.code <= pec::trailing_character) if (ps.code <= pec::trailing_character)
consumer.value(atom(buf)); consumer.value(atom(buf));
}); });
// clang-format off
start(); start();
state(init) { state(init) {
transition(init, " \t") transition(init, " \t")
transition(read_chars, '\'') transition(read_chars, '\'')
epsilon_if(accept_unquoted, read_unquoted_chars, is_legal)
} }
state(read_chars) { state(read_chars) {
transition(done, '\'') transition(done, '\'')
...@@ -70,7 +73,11 @@ void read_atom(state<Iterator, Sentinel>& ps, Consumer& consumer) { ...@@ -70,7 +73,11 @@ void read_atom(state<Iterator, Sentinel>& ps, Consumer& consumer) {
term_state(done) { term_state(done) {
transition(done, " \t") transition(done, " \t")
} }
term_state(read_unquoted_chars) {
transition(read_unquoted_chars, is_legal, append(ch), pec::too_many_characters)
}
fin(); fin();
// clang-format on
} }
} // namespace parser } // namespace parser
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "caf/detail/parse.hpp" #include "caf/detail/parse.hpp"
#include "caf/detail/consumer.hpp" #include "caf/detail/consumer.hpp"
#include "caf/detail/parser/read_atom.hpp"
#include "caf/detail/parser/read_floating_point.hpp" #include "caf/detail/parser/read_floating_point.hpp"
#include "caf/detail/parser/read_signed_integer.hpp" #include "caf/detail/parser/read_signed_integer.hpp"
#include "caf/detail/parser/read_unsigned_integer.hpp" #include "caf/detail/parser/read_unsigned_integer.hpp"
...@@ -51,5 +52,9 @@ PARSE_IMPL(float, floating_point) ...@@ -51,5 +52,9 @@ PARSE_IMPL(float, floating_point)
PARSE_IMPL(double, floating_point) PARSE_IMPL(double, floating_point)
void parse(parse_state& ps, atom_value& x) {
parser::read_atom(ps, make_consumer(x), true);
}
} // namespace detail } // namespace detail
} // namespace caf } // namespace caf
...@@ -120,3 +120,11 @@ CAF_TEST(invalid floating point numbers) { ...@@ -120,3 +120,11 @@ CAF_TEST(invalid floating point numbers) {
CHECK_INVALID(double, "--0.01e10", pec::unexpected_character); CHECK_INVALID(double, "--0.01e10", pec::unexpected_character);
CHECK_INVALID(double, "++10e-10", pec::unexpected_character); CHECK_INVALID(double, "++10e-10", pec::unexpected_character);
} }
CAF_TEST(valid atom value) {
CAF_CHECK_EQUAL(read<atom_value>("foo"), atom("foo"));
CAF_CHECK_EQUAL(read<atom_value>("'foo'"), atom("foo"));
CAF_CHECK_EQUAL(read<atom_value>("foooooooooo"), pec::too_many_characters);
CAF_CHECK_EQUAL(read<atom_value>("foo,bar"), pec::trailing_character);
CAF_CHECK_EQUAL(read<atom_value>("$"), pec::unexpected_character);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment