dragonfly-grammars/_terminal_grammar.py

128 lines
5.2 KiB
Python
Raw Normal View History

# Author: Jeremy Hayes
# This script includes commands that are to be used for Terminal programming
from dragonfly import (Grammar, CompoundRule, Dictation, RuleRef, DictList, DictListRef, Text, Key, AppContext, MappingRule, Function, Sequence, Mimic)
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
def doSomethingToCommand(command):
newCommand = Sequence(command)
newCommand.execute()
class TerminalEnabler(CompoundRule):
spec = "Activate Terminal" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
TerminalBootstrap.disable()
TerminalGrammar.enable()
s = "Terminal grammar activated"
print s
speaker.Speak(s)
class TerminalDisabler(CompoundRule):
spec = "switch language" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
TerminalGrammar.disable()
TerminalBootstrap.enable()
s = "Terminal grammar deactivated"
print s
speaker.Speak(s)
class TerminalTestRule(CompoundRule):
spec = "test Terminal" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Terminal grammar tested"
class TerminalControlStructures(MappingRule):
mapping = {
"variable": Text("var "),
"console": Text("console.log();"),
"function": Text("function functionName() {") + Key("enter")+ Key("enter"), #+ Text("}"),
"variable function": Text("functionName = function () {") + Key("enter")+ Key("enter"), #+ Text("}"),
"self function": Text("(function() {") + Key("enter")+ Key("enter"), #+ Text("}())"),
"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("else if() {") + 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()"),
"for loop": Text("for( ; ; ) {") + Key("enter")+ Key("enter"), #+ Text("}"),
"switch statement": Text("switch() {") + Key("enter")+ Key("enter"), #+ Text("}"),
}
class TerminalCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") #+ Key("enter") + Text("*/") + Key("up")
}
class TerminalMiscellaneousStuff(MappingRule):
mapping = {
"equals": Text(" = "),
"new": Text("new "),
}
class TerminalComparisonOperators(MappingRule):
mapping = {
"equal to": Text("=="),
"exactly equal to": Text("==="),
"not equal to": Text("!="),
"greater than": Text(">"),
"less than": Text("<"),
"greater than or equal to": Text(">="),
"less than or equal to": Text("<="),
}
class TerminalArithmeticOperators(MappingRule):
mapping = {
"plus plus": Text("++"),
"minus minus": Text("--"),
}
class TerminalAssignmentOperators(MappingRule):
mapping = {
"plus equals": Text("+="),
"minus equals": Text("-="),
"multiply equals": Text("*="),
"divide equals": Text("/="),
"modulus equals": Text("%="),
}
TerminalBootstrap = Grammar("Terminal bootstrap") # Create a grammar to contain the command rule.
TerminalBootstrap.add_rule(TerminalEnabler())
TerminalBootstrap.load()
TerminalGrammar = Grammar("Terminal grammar")
TerminalGrammar.add_rule(TerminalTestRule())
TerminalGrammar.add_rule(TerminalControlStructures())
TerminalGrammar.add_rule(TerminalCommentsSyntax())
TerminalGrammar.add_rule(TerminalMiscellaneousStuff())
TerminalGrammar.add_rule(TerminalComparisonOperators())
TerminalGrammar.add_rule(TerminalArithmeticOperators())
TerminalGrammar.add_rule(TerminalAssignmentOperators())
TerminalGrammar.add_rule(TerminalDisabler())
TerminalGrammar.load()
TerminalGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global TerminalGrammar
if TerminalGrammar: TerminalGrammar.unload()
TerminalGrammar = None