-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_parser_combinator.py
67 lines (52 loc) · 1.74 KB
/
test_parser_combinator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import parser_combinator as pc
def test_many() -> None:
f = pc.many(pc.digit())
maybeVal = pc.parse(f, "12345")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == [1, 2, 3, 4, 5]
assert s == ''
def test_integer() -> None:
f = pc.integer()
maybeVal = pc.parse(f, "12345")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == 12345
assert s == ''
maybeVal = pc.parse(f, "")
assert isinstance(maybeVal, pc.ParseError)
def test_char() -> None:
f = pc.char('a')
maybeVal = pc.parse(f, "abc")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == 'a'
assert s == "bc"
def test_between() -> None:
f = pc.between(pc.char('('), pc.string("hello"), pc.char(')'))
maybeVal = pc.parse(f, "(hello)")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == "hello"
assert s == ''
def test_array() -> None:
f = pc.array(pc.char('['), pc.integer(), pc.char(']'), pc.char(','))
maybeVal = pc.parse(f, "[1,2,3,4,5]")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == [1, 2, 3, 4, 5]
assert s == ''
f = pc.array(pc.char('('), pc.integer(), pc.char(')'), pc.many1(
pc.choice([pc.space(), pc.tab(), pc.newline(), pc.carriage_return()])))
maybeVal = pc.parse(f, "(1 2\t3\n4 \t 5\r6)")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == [1, 2, 3, 4, 5, 6]
assert s == ''
maybeVal = pc.parse(f, "()")
assert not isinstance(maybeVal, pc.ParseError)
(val, s) = maybeVal
assert val == []
assert s == ''
def test_regex() -> None:
pass