-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_cat.jl
101 lines (86 loc) · 2.13 KB
/
parse_cat.jl
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
include("juliaparsec.jl")
## silly test example
abstract Token123
type Dog <: Token123 end
type Cat <: Token123 end
function parse_cat(xs,pos)
r = parse_char(xs,'c',pos)
if r == nothing return nothing end
x,p2 = r
r = parse_char(xs,'a',p2)
if r == nothing return nothing end
x,p2 = r
r = parse_char(xs,'t',p2)
if r == nothing
return nothing
end
x,p2 = r
return (Cat(),p2)
end
function parse_dog(xs,pos)
r = parse_char(xs,'d',pos)
if r == nothing return nothing end
x,p2 = r
r = parse_char(xs,'o',p2)
if r == nothing return nothing end
x,p2 = r
r = parse_char(xs,'g',p2)
if r == nothing
return nothing
end
x,p2 = r
return (Dog(),p2)
end
#end #@iprofile begin
if(false)
myparser = branch([parse_cat,parse_dog])
@show myparser("dog")
@show myparser("cat")
@show myparser("dogcat")
@show myparser("catdog")
@show myparser("god")
myparser2 = branch2([parse_cat,parse_dog])
@show myparser2("dog")
@show myparser2("cat")
@show myparser2("dogcat")
@show myparser2("catdog")
@show myparser2("god")
myseqparser = sequence([parse_cat,parse_dog])
@show myseqparser("dog")
@show myseqparser("cat")
@show myseqparser("dogcat")
@show myseqparser("catdog")
@show myseqparser("god")
myseqparser2 = sequence2([parse_cat,parse_dog])
@show myseqparser2("dog")
@show myseqparser2("cat")
@show myseqparser2("dogcat")
@show myseqparser2("catdog")
@show myseqparser2("god")
myzeroparser = zeroormore(parse_cat)
@show myzeroparser("dog")
@show myzeroparser("cat")
@show myzeroparser("catcatcat")
@show myzeroparser("dogcat")
@show myzeroparser("catdog")
@show myzeroparser("god")
end
numseq = 100
cats = fill!(Array(Function,numseq),parse_cat)
catstext = "cat"^numseq
seq1parser = sequence(cats)
seq2parser = sequence2(cats)
print("seq / seq2 JIT\n")
@time seq1parser(catstext,1)
@time seq2parser(catstext,1)
print("seq / seq2 test\n")
@time seq1parser(catstext,1)
@time seq2parser(catstext,1)
branch1parser = branch(cats)
branch2parser = branch2(cats)
print("branch / branch2 JIT\n")
@time branch1parser(catstext,1)
@time branch2parser(catstext,1)
print("branch / branch2 test\n")
@time branch1parser(catstext,1)
@time branch2parser(catstext,1)