speech_rule_functions.js revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @fileoverview Classes for custom functions for the speech rule engine. 7 * 8 */ 9 10goog.provide('cvox.SpeechRuleFunctions'); 11goog.provide('cvox.SpeechRuleFunctions.ContextFunctions'); 12goog.provide('cvox.SpeechRuleFunctions.CustomQueries'); 13goog.provide('cvox.SpeechRuleFunctions.CustomStrings'); 14 15 16 17/** 18 * @constructor 19 */ 20cvox.SpeechRuleFunctions = function() { }; 21 22 23/** 24 * Private superclass of all the custom function stores. 25 * @constructor 26 * @param {string} prefix A prefix string for the function names. 27 * @param {Object.<string, Function>} store Storage object. 28 * @private 29 */ 30cvox.SpeechRuleFunctions.Store_ = function(prefix, store) { 31 /** @private */ 32 this.prefix_ = prefix; 33 /** @private */ 34 this.store_ = store; 35}; 36 37 38/** 39 * Adds a new function for the function store. 40 * @param {string} name A name. 41 * @param {!Function} func A function. 42 */ 43cvox.SpeechRuleFunctions.Store_.prototype.add = function(name, func) { 44 if (this.checkCustomFunctionSyntax_(name)) { 45 this.store_[name] = func; 46 } 47}; 48 49 50/** 51 * Retrieves a function with the given name if one exists. 52 * @param {string} name A name. 53 * @return {Function} The function if it exists. 54 */ 55cvox.SpeechRuleFunctions.Store_.prototype.lookup = function(name) { 56 return this.store_[name]; 57}; 58 59 60/** 61 * Context function for use in speech rules. 62 * @typedef {function(!Node): Array.<Node>} 63 */ 64cvox.SpeechRuleFunctions.CustomQuery; 65 66 67/** 68 * @constructor 69 * @extends {cvox.SpeechRuleFunctions.Store_} 70 */ 71cvox.SpeechRuleFunctions.CustomQueries = function() { 72 var store = 73 /** @type {Object.<string, cvox.SpeechRuleFunctions.CustomQuery>} */ ({}); 74 goog.base(this, 'CQF', store); 75}; 76goog.inherits(cvox.SpeechRuleFunctions.CustomQueries, 77 cvox.SpeechRuleFunctions.Store_); 78 79 80/** 81 * Context function for use in speech rules. 82 * @typedef {function(!Node): string} 83 */ 84cvox.SpeechRuleFunctions.CustomString; 85 86 87/** 88 * @constructor 89 * @extends {cvox.SpeechRuleFunctions.Store_} 90 */ 91cvox.SpeechRuleFunctions.CustomStrings = function() { 92 var store = 93 /** @type {Object.<string, cvox.SpeechRuleFunctions.CustomString>} */ ({}); 94 goog.base(this, 'CSF', store); 95}; 96goog.inherits(cvox.SpeechRuleFunctions.CustomStrings, 97 cvox.SpeechRuleFunctions.Store_); 98 99 100/** 101 * Context function for use in speech rules. 102 * @typedef {function(Array.<Node>, ?string): (function(): string)} 103 */ 104cvox.SpeechRuleFunctions.ContextFunction; 105 106 107/** 108 * @constructor 109 * @extends {cvox.SpeechRuleFunctions.Store_} 110 */ 111cvox.SpeechRuleFunctions.ContextFunctions = function() { 112 var store = 113 /** @type {Object.<string, cvox.SpeechRuleFunctions.ContextFunction>} */ 114 ({}); 115 goog.base(this, 'CTXF', store); 116}; 117goog.inherits(cvox.SpeechRuleFunctions.ContextFunctions, 118 cvox.SpeechRuleFunctions.Store_); 119 120 121/** 122 * Checks validity for a custom function name. 123 * @param {string} name The name of the custom function. 124 * @return {!boolean} True if the name is valid. 125 * @private 126 */ 127cvox.SpeechRuleFunctions.Store_.prototype. 128 checkCustomFunctionSyntax_ = function(name) { 129 var reg = new RegExp('^' + this.prefix_); 130 if (!name.match(reg)) { 131 console.log( 132 'FunctionError: Invalid function name. Expected prefix' + 133 this.prefix_); 134 return false; 135 } 136 return true; 137 }; 138