Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
452c84b2
Commit
452c84b2
authored
Jun 17, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More parsers.
parent
37ee4819
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
200 additions
and
0 deletions
+200
-0
parse-test.c++
c++/src/kj/parse-test.c++
+200
-0
parse.h
c++/src/kj/parse.h
+0
-0
No files found.
c++/src/kj/parse-test.c++
View file @
452c84b2
...
...
@@ -55,6 +55,61 @@ TEST(Parsers, ExactElementParser) {
EXPECT_TRUE
(
input
.
atEnd
());
}
TEST
(
Parsers
,
ExactlyConstParser
)
{
StringPtr
text
=
"foo"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
Tuple
<>>
result
=
exactlyConst
<
char
,
'f'
>
()(
input
);
EXPECT_TRUE
(
result
!=
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
result
=
exactlyConst
<
char
,
'o'
>
()(
input
);
EXPECT_TRUE
(
result
!=
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
result
=
exactlyConst
<
char
,
'x'
>
()(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
auto
parser
=
exactlyConst
<
char
,
'o'
>
();
ParserRef
<
Input
,
Tuple
<>>
wrapped
=
ref
<
Input
>
(
parser
);
result
=
wrapped
(
input
);
EXPECT_TRUE
(
result
!=
nullptr
);
EXPECT_TRUE
(
input
.
atEnd
());
}
TEST
(
Parsers
,
ExactChar
)
{
constexpr
auto
parser
=
exactChar
<
'a'
>
();
{
StringPtr
text
=
"a"
;
Input
input
(
text
.
begin
(),
text
.
end
());
EXPECT_TRUE
(
parser
(
input
)
!=
nullptr
);
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"b"
;
Input
input
(
text
.
begin
(),
text
.
end
());
EXPECT_TRUE
(
parser
(
input
)
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
}
TEST
(
Parsers
,
ConstResultParser
)
{
auto
parser
=
constResult
(
exactly
(
'o'
),
123
);
StringPtr
text
=
"o"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
int
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
i
,
result
)
{
EXPECT_EQ
(
123
,
*
i
);
}
else
{
ADD_FAILURE
()
<<
"Expected 123, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
TEST
(
Parsers
,
SequenceParser
)
{
StringPtr
text
=
"foo"
;
...
...
@@ -328,6 +383,151 @@ TEST(Parsers, AcceptIfParser) {
}
}
TEST
(
Parsers
,
CharRange
)
{
constexpr
auto
parser
=
charRange
(
'a'
,
'z'
);
{
StringPtr
text
=
"a"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'a'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"n"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'n'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"z"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'z'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"`"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"{"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"A"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
}
TEST
(
Parsers
,
AnyChar
)
{
constexpr
auto
parser
=
anyChar
(
"axn2B"
);
{
StringPtr
text
=
"a"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'a'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"n"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'n'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"B"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
'B'
,
*
value
);
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_TRUE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"b"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"j"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
{
StringPtr
text
=
"A"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
char
>
result
=
parser
(
input
);
EXPECT_TRUE
(
result
==
nullptr
);
EXPECT_FALSE
(
input
.
atEnd
());
}
}
TEST
(
Parsers
,
CharGroupCombo
)
{
constexpr
auto
parser
=
many
(
charRange
(
'0'
,
'9'
).
orRange
(
'a'
,
'z'
).
orRange
(
'A'
,
'Z'
).
orAny
(
"-_"
));
{
StringPtr
text
=
"foo1-bar2_baz3@qux"
;
Input
input
(
text
.
begin
(),
text
.
end
());
Maybe
<
Array
<
char
>>
result
=
parser
(
input
);
KJ_IF_MAYBE
(
value
,
result
)
{
EXPECT_EQ
(
"foo1-bar2_baz3"
,
str
(
*
value
));
}
else
{
ADD_FAILURE
()
<<
"Expected parse result, got null."
;
}
EXPECT_FALSE
(
input
.
atEnd
());
}
}
}
// namespace
}
// namespace parse
}
// namespace kj
c++/src/kj/parse.h
View file @
452c84b2
This diff is collapsed.
Click to expand it.
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