forked from brando4526/programming-language-dragonfly-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_python_grammar.py
71 lines (50 loc) · 2.53 KB
/
_python_grammar.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
# Author:Brandon Lovrien
# This script is to be used for programming in the Python programming language
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule)
class PythonEnabler(CompoundRule):
spec = "Enable Python" # Spoken command to enable the Python grammar.
def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonBootstrap.disable()
pythonGrammar.enable()
print "Python grammar enabled"
class PythonDisabler(CompoundRule):
spec = "switch language" # spoken command to disable the Python grammar.
def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonGrammar.disable()
pythonBootstrap.enable()
print "Python grammar disabled"
# This is a test rule to see if the Python grammar is enabled
class PythonTestRule(CompoundRule):
spec = "test Python" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Python grammar tested"
# Handles Python commenting syntax
class PythonCommentsSyntax(MappingRule):
mapping = {
"comment": Text("# "),
}
# handles Python control structures
class PythonControlStructures(MappingRule):
mapping = {
"if": Text("if condition:") + Key("enter"),
"while loop": Text("while condition:") + Key("enter"),
"for loop": Text("for something in something:") + Key("enter"),
"function": Text("def functionName():") + Key("enter"),
"class": Text("class className(inheritance):") + Key("enter"),
}
# The main Python grammar rules are activated here
pythonBootstrap = Grammar("python bootstrap")
pythonBootstrap.add_rule(PythonEnabler())
pythonBootstrap.load()
pythonGrammar = Grammar("python grammar")
pythonGrammar.add_rule(PythonTestRule())
pythonGrammar.add_rule(PythonCommentsSyntax())
pythonGrammar.add_rule(PythonControlStructures())
pythonGrammar.add_rule(PythonDisabler())
pythonGrammar.load()
pythonGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global pythonGrammar
if pythonGrammar: pythonGrammar.unload()
pythonGrammar = None