forked from brando4526/programming-language-dragonfly-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_php_grammar.py
More file actions
203 lines (154 loc) · 8.19 KB
/
_php_grammar.py
File metadata and controls
203 lines (154 loc) · 8.19 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Author:Brandon Lovrien
# This script includes commands that are to be used for programming in the PHP programming language
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule)
class PHPEnabler(CompoundRule):
spec = "Enable PHP" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
phpBootstrap.disable()
phpGrammar.enable()
print "PHP grammar enabled"
class PHPDisabler(CompoundRule):
spec = "switch language" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
phpGrammar.disable()
phpBootstrap.enable()
print "PHP grammar disabled"
class PHPTestRule(CompoundRule):
spec = "test PHP" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "PHP grammar tested"
# Deals with printing PHP datatypes
class PHPDataTypes(MappingRule):
mapping = {
"integer": Text("int"),
"float": Text("float"),
"boolean": Text("boolean"),
"string": Text("string"),
"object": Text("object"),
"null": Text("NULL")
}
class PHPVariableDeclarations(MappingRule):
mapping = {
"variable": Text("$")
}
# Handles PHP commenting syntax
class PHPCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") + Key("enter") + Text("*/") + Key("up")
}
class PHPSuperGlobals(MappingRule):
mapping = {
"globals": Text("$GLOBALS['']"),
"post": Text("$_POST['']"),
"get": Text("$_GET['']"),
"files": Text("$_FILES['']"),
"cookie": Text("$_COOKIE['']"),
"session": Text("$_SESSION['']"),
"request": Text("$_REQUEST['']"),
"environment": Text("$_ENV['']")
}
class PHPControlStructures(MappingRule):
mapping = {
"code block": Text("{") + Key("enter")+ Key("enter") + Text("}"),
"if": Text("if() {") + Key("enter")+ Key("enter") + Text("}"),
"if else": Text("if() {") + Key("enter")+ Key("enter") + Text("}") + Key("enter") + Text("else {") + Key("enter")+ Key("enter") + Text("}"),
"else if": Text("elseif() {") + Key("enter")+ Key("enter") + Text("}"),
"for loop": Text("for(;;) {") + Key("enter")+ Key("enter") + Text("}"),
"for each loop": Text("for(<array> as [<value> |<key> => <value>]) {") + Key("enter")+ Key("enter") + Text("}"),
"while loop": Text("while() {") + Key("enter")+ Key("enter") + Text("}"),
"do while loop": Text("do {") + Key("enter") + Key("down") + Text("while()"),
"switch statement": Text("switch() {") + Key("enter")+ Key("enter") + Text("}"),
"try catch": Text("try {") + Key("enter")+ Key("enter") + Text("}") + Key("enter") + Text("catch(<ExceptionClass> e) {") + Key("enter")+ Key("enter") + Text("}"),
"function": Text("function functionName() {") + Key("enter")+ Key("enter") + Text("}"),
"class": Text("class ClassName {") + Key("enter")+ Key("enter") + Text("}")
}
class PHPAccessModifiers(MappingRule):
mapping = {
"public": Text("public "),
"private": Text("private "),
"static": Text("static ")
}
class PHPUsefulMethods(MappingRule):
mapping = {
"print statement": Text("echo \"\"") + Key("left")
}
class PHPLogicalOperators(MappingRule):
mapping = {
"logical and": Text("&&"),
"logical or": Text("||"),
"true": Text("true"),
"false": Text("false"),
"not": Text("!"),
"logical exclusive or": Text("xor")
}
class PHPAssignmentOperators(MappingRule):
mapping = {
"plus equals": Text("+="),
"minus equals": Text("-="),
"multiply equals": Text("*="),
"divide equals": Text("/="),
".equals": Text(".=")
}
class PHPArithmeticOperators(MappingRule):
mapping = {
"multiplied by": Text("*"),
"plus": Text("+"),
"minus": Text("-"),
"divided by": Text("/"),
"modulus": Text("%") #causes some weird problem in dragonfly so this doesn't work, use "percent sign" instead
}
class PHPEscapeSequences(MappingRule):
mapping = {
"escape quotes": Text("\ ")+ Key("left") + Text("\"") + Text("\ ")+ Key("left") + Text("\""),
"escape single quotes": Text("\ ")+ Key("left") + Text("\'") + Text("\ ")+ Key("left") + Text("\'"),
"escape line": Text("\ ")+ Key("left") + Text("n"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape carriage return": Text("\ ")+ Key("left") + Text("r"),
}
class PHPComparisonOperators(MappingRule):
mapping = {
"equal to": Text("=="),
"not equal to": Text("!="),
"equal to type comparison": Text("==="),
"not equal to type comparison": Text("!=="),
"greater than": Text(">"),
"less than": Text("<"),
"less than or equal to": Text("<="),
"greater than or equal to": Text(">="),
}
class PHPMiscellaneousStuff(MappingRule):
mapping = {
"PHP block": Text("<?php ?>"),
"require": Text("require ('');"),
"require once": Text("require_once ('');"),
"include": Text("include ('');"),
"include once": Text("require_once ('');"),
"equals": Text(" = "),
}
phpBootstrap = Grammar("php bootstrap") # Create a grammar to contain the command rule.
phpBootstrap.add_rule(PHPEnabler())
phpBootstrap.load()
phpGrammar = Grammar("php grammar")
phpGrammar.add_rule(PHPTestRule())
phpGrammar.add_rule(PHPDataTypes())
phpGrammar.add_rule(PHPVariableDeclarations())
phpGrammar.add_rule(PHPCommentsSyntax())
phpGrammar.add_rule(PHPSuperGlobals())
phpGrammar.add_rule(PHPControlStructures())
phpGrammar.add_rule(PHPAccessModifiers())
phpGrammar.add_rule(PHPUsefulMethods())
phpGrammar.add_rule(PHPLogicalOperators())
phpGrammar.add_rule(PHPAssignmentOperators())
phpGrammar.add_rule(PHPArithmeticOperators())
phpGrammar.add_rule(PHPEscapeSequences())
phpGrammar.add_rule(PHPComparisonOperators())
phpGrammar.add_rule(PHPMiscellaneousStuff())
phpGrammar.add_rule(PHPDisabler())
phpGrammar.load()
phpGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global phpGrammar
if phpGrammar: phpGrammar.unload()
phpGrammar = None