-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_test_dragonfly.py
131 lines (95 loc) · 3.95 KB
/
_test_dragonfly.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from dragonfly.all import Grammar, Sequence, Rule, Literal, Alternative
from VocolaUtils import *
class CommandSequence(Sequence):
def __init__(self, terms, actions, name=None):
Sequence.__init__(self, children=terms, name=name)
self.actions = actions
def value(self, node):
values = super(CommandSequence, self).value(node)
def thunk(list_buffer, functional):
return self.actions(values, list_buffer, functional)
return thunk
class VocolaRule(Rule):
def __init__(self, name=None, element=None, context=None):
Rule.__init__(self, name, element, context, False, True)
def process_recognition(self, node):
thunk = self.value(node)
top_buffer = thunk("", False)
#top_buffer = thunk("", True)
do_flush(False, top_buffer)
grammar = Grammar("example grammar")
def actions_1(values, list_buffer, functional):
try:
list_buffer += 'fun'
return list_buffer
except Exception, e:
handle_error('input.vcl', 7, '\'this is a test\'', e)
command_1 = CommandSequence([Literal('this is a test')], actions_1)
grammar.add_rule(VocolaRule("test1", command_1, None))
def actions_2(values, list_buffer, functional):
try:
list_buffer = do_flush(functional, list_buffer);
call_Dragon('Beep', '', [])
return list_buffer
except Exception, e:
handle_error('input.vcl', 9, '\'I want you to beep\'', e)
command_2 = CommandSequence([Literal('I want you to beep')], actions_2)
grammar.add_rule(VocolaRule("test2", command_2, None))
# 'red'
def actions_unnamed_1(values, list_buffer, functional):
try:
list_buffer += 'red'
return list_buffer
except Exception, e:
handle_error('input.vcl', 3, '\'red\'', e)
command_unnamed_1 = CommandSequence([Literal('red')], actions_unnamed_1)
# 'blue'
def actions_unnamed_2(values, list_buffer, functional):
try:
list_buffer += '2'
return list_buffer
except Exception, e:
handle_error('input.vcl', 3, '\'blue\'', e)
command_unnamed_2 = CommandSequence([Literal('blue')], actions_unnamed_2)
# 'green'
def actions_unnamed_3(values, list_buffer, functional):
try:
list_buffer += 'green'
return list_buffer
except Exception, e:
handle_error('input.vcl', 3, '\'green\'', e)
command_unnamed_3 = CommandSequence([Literal('green')], actions_unnamed_3)
menu_list = Alternative([command_unnamed_1, command_unnamed_2, command_unnamed_3])
# 'try color' <list>
def actions_3(values, list_buffer, functional):
print repr(values)
try:
list_buffer += '<'
list_buffer = values[1](list_buffer,functional)
list_buffer += '>'
return list_buffer
except Exception, e:
handle_error('input.vcl', 10, '\'try color\' <list>', e)
command_3 = CommandSequence([Literal('try color'), menu_list], actions_3)
grammar.add_rule(VocolaRule("test3", command_3, None))
#command_foo = CommandSequence([Literal('extended'), Alternative(menu_list, command_3)], actions_3)
possibilities = Alternative([command_unnamed_1, command_unnamed_2, command_unnamed_3, command_3])
command_foo = CommandSequence([Literal('extended'), possibilities], actions_3)
grammar.add_rule(VocolaRule("testf", command_foo, None))
def e1_actions(values, list_buffer, functional):
return "Hello: " + repr(values)
header = Literal("Superman")
e1 = CommandSequence([header, Literal("17")], e1_actions)
e2 = CommandSequence([header, Literal("water")], e1_actions)
them = Alternative([e1,e2])
def e3_actions(values, list_buffer, functional):
return "Bye: " + repr(values[1]())
e3 = CommandSequence([header, them], e3_actions)
#rule = VocolaRule("test", e1, None)
rule = VocolaRule("test", Alternative([e1,e2,e3]), None)
grammar.add_rule(rule)
grammar.load() # Load the grammar.
def unload():
global grammar
if grammar: grammar.unload()
grammar = None