Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
d5e439c5
Commit
d5e439c5
authored
May 14, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add an FSM-based one-pass string parser
parent
350b1941
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
0 deletions
+192
-0
libcaf_core/caf/detail/parser/fsm.hpp
libcaf_core/caf/detail/parser/fsm.hpp
+15
-0
libcaf_core/caf/detail/parser/read_string.hpp
libcaf_core/caf/detail/parser/read_string.hpp
+74
-0
libcaf_core/test/read_string.cpp
libcaf_core/test/read_string.cpp
+103
-0
No files found.
libcaf_core/caf/detail/parser/fsm.hpp
View file @
d5e439c5
...
...
@@ -70,6 +70,11 @@
goto s_##target; \
}
#define default_action(target, statement) \
statement; \
ch = ps.next(); \
goto s_##target;
#define checked_action(predicate, target, statement, error_code) \
if (predicate(ch)) { \
if (statement) { \
...
...
@@ -81,5 +86,15 @@
} \
}
#define invalid_input(predicate, error_code) \
if (predicate(ch)) { \
ps.code = error_code; \
return; \
}
#define default_failure(error_code) \
ps.code = error_code; \
return;
#define epsilon(target) goto e_##target;
libcaf_core/caf/detail/parser/read_string.hpp
0 → 100644
View file @
d5e439c5
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstdint>
#include <string>
#include "caf/detail/scope_guard.hpp"
#include "caf/detail/parser/ec.hpp"
#include "caf/detail/parser/fsm.hpp"
#include "caf/detail/parser/is_char.hpp"
#include "caf/detail/parser/state.hpp"
namespace
caf
{
namespace
detail
{
namespace
parser
{
/// Reads a number, i.e., on success produces either an `int64_t` or a
/// `double`.
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
>
void
read_string
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
consumer
)
{
std
::
string
res
;
auto
g
=
caf
::
detail
::
make_scope_guard
([
&
]
{
if
(
ps
.
code
<=
ec
::
trailing_character
)
consumer
.
value
(
std
::
move
(
res
));
});
start
();
state
(
init
)
{
input
(
is_char
<
' '
>
,
init
)
input
(
is_char
<
'\t'
>
,
init
)
input
(
is_char
<
'"'
>
,
read_chars
)
}
state
(
read_chars
)
{
input
(
is_char
<
'\\'
>
,
escape
)
input
(
is_char
<
'"'
>
,
done
)
invalid_input
(
is_char
<
'\n'
>
,
ec
::
unexpected_newline
)
default_action
(
read_chars
,
res
+=
ch
)
}
state
(
escape
)
{
action
(
is_char
<
'n'
>
,
read_chars
,
res
+=
'\n'
)
action
(
is_char
<
'r'
>
,
read_chars
,
res
+=
'\r'
)
action
(
is_char
<
't'
>
,
read_chars
,
res
+=
'\t'
)
action
(
is_char
<
'\\'
>
,
read_chars
,
res
+=
'\\'
)
action
(
is_char
<
'"'
>
,
read_chars
,
res
+=
'"'
)
default_failure
(
ec
::
illegal_escape_sequence
)
}
term_state
(
done
)
{
input
(
is_char
<
' '
>
,
done
)
input
(
is_char
<
'\t'
>
,
done
)
}
fin
();
}
}
// namespace parser
}
// namespace detail
}
// namespace caf
libcaf_core/test/read_string.cpp
0 → 100644
View file @
d5e439c5
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE read_string
#include <string>
#include "caf/test/unit_test.hpp"
#include "caf/variant.hpp"
#include "caf/detail/parser/read_string.hpp"
using
namespace
caf
;
using
detail
::
parser
::
ec
;
namespace
{
struct
string_parser_consumer
{
std
::
string
x
;
inline
void
value
(
std
::
string
y
)
{
x
=
std
::
move
(
y
);
}
};
using
res_t
=
variant
<
ec
,
std
::
string
>
;
struct
string_parser
{
res_t
operator
()(
std
::
string
str
)
{
detail
::
parser
::
state
<
std
::
string
::
iterator
>
res
;
string_parser_consumer
f
;
res
.
i
=
str
.
begin
();
res
.
e
=
str
.
end
();
detail
::
parser
::
read_string
(
res
,
f
);
if
(
res
.
code
==
ec
::
success
)
return
f
.
x
;
return
res
.
code
;
}
};
struct
fixture
{
string_parser
p
;
};
// TODO: remove and use "..."s from the STL when switching to C++14
std
::
string
operator
""
_s
(
const
char
*
str
,
size_t
str_len
)
{
std
::
string
result
;
result
.
assign
(
str
,
str_len
);
return
result
;
}
}
// namespace <anonymous>
CAF_TEST_FIXTURE_SCOPE
(
read_string_tests
,
fixture
)
CAF_TEST
(
empty
string
)
{
CAF_CHECK_EQUAL
(
p
(
R"("")"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
R"( "")"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
R"( "")"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("" )"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("" )"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
R"( "" )"
),
""
_s
);
CAF_CHECK_EQUAL
(
p
(
"
\t
\"\"
\t\t\t
"
),
""
_s
);
}
CAF_TEST
(
non
-
empty
string
)
{
CAF_CHECK_EQUAL
(
p
(
R"("abc")"
),
"abc"
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("a b c")"
),
"a b c"
_s
);
CAF_CHECK_EQUAL
(
p
(
R"( "abcdefABCDEF" )"
),
"abcdefABCDEF"
_s
);
}
CAF_TEST
(
string
with
escaped
characters
)
{
CAF_CHECK_EQUAL
(
p
(
R"("a\tb\tc")"
),
"a
\t
b
\t
c"
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("a\nb\r\nc")"
),
"a
\n
b
\r\n
c"
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("a\\b")"
),
"a
\\
b"
_s
);
CAF_CHECK_EQUAL
(
p
(
R"("foo = \"bar\"")"
),
"foo =
\"
bar
\"
"
_s
);
}
CAF_TEST
(
invalid
strings
)
{
CAF_CHECK_EQUAL
(
p
(
R"("abc)"
),
ec
::
unexpected_eof
);
CAF_CHECK_EQUAL
(
p
(
"
\"
ab
\n
c
\"
"
),
ec
::
unexpected_newline
);
CAF_CHECK_EQUAL
(
p
(
R"("foo \i bar")"
),
ec
::
illegal_escape_sequence
);
CAF_CHECK_EQUAL
(
p
(
R"(foo)"
),
ec
::
unexpected_character
);
CAF_CHECK_EQUAL
(
p
(
R"("abc" def)"
),
ec
::
trailing_character
);
}
CAF_TEST_FIXTURE_SCOPE_END
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment