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.
6 * @fileoverview Classes for custom functions for the speech rule engine.
10 goog.provide('cvox.SpeechRuleFunctions');
11 goog.provide('cvox.SpeechRuleFunctions.ContextFunctions');
12 goog.provide('cvox.SpeechRuleFunctions.CustomQueries');
13 goog.provide('cvox.SpeechRuleFunctions.CustomStrings');
20 cvox.SpeechRuleFunctions = function() { };
24 * Private superclass of all the custom function stores.
26 * @param {string} prefix A prefix string for the function names.
27 * @param {Object<Function>} store Storage object.
30 cvox.SpeechRuleFunctions.Store_ = function(prefix, store) {
32 this.prefix_ = prefix;
39 * Adds a new function for the function store.
40 * @param {string} name A name.
41 * @param {!Function} func A function.
43 cvox.SpeechRuleFunctions.Store_.prototype.add = function(name, func) {
44 if (this.checkCustomFunctionSyntax_(name)) {
45 this.store_[name] = func;
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.
55 cvox.SpeechRuleFunctions.Store_.prototype.lookup = function(name) {
56 return this.store_[name];
61 * Context function for use in speech rules.
62 * @typedef {function(!Node): Array<Node>}
64 cvox.SpeechRuleFunctions.CustomQuery;
69 * @extends {cvox.SpeechRuleFunctions.Store_}
71 cvox.SpeechRuleFunctions.CustomQueries = function() {
73 /** @type {Object<cvox.SpeechRuleFunctions.CustomQuery>} */ ({});
74 goog.base(this, 'CQF', store);
76 goog.inherits(cvox.SpeechRuleFunctions.CustomQueries,
77 cvox.SpeechRuleFunctions.Store_);
81 * Context function for use in speech rules.
82 * @typedef {function(!Node): string}
84 cvox.SpeechRuleFunctions.CustomString;
89 * @extends {cvox.SpeechRuleFunctions.Store_}
91 cvox.SpeechRuleFunctions.CustomStrings = function() {
93 /** @type {Object<cvox.SpeechRuleFunctions.CustomString>} */ ({});
94 goog.base(this, 'CSF', store);
96 goog.inherits(cvox.SpeechRuleFunctions.CustomStrings,
97 cvox.SpeechRuleFunctions.Store_);
101 * Context function for use in speech rules.
102 * @typedef {function(Array<Node>, ?string): (function(): string)}
104 cvox.SpeechRuleFunctions.ContextFunction;
109 * @extends {cvox.SpeechRuleFunctions.Store_}
111 cvox.SpeechRuleFunctions.ContextFunctions = function() {
113 /** @type {Object<cvox.SpeechRuleFunctions.ContextFunction>} */
115 goog.base(this, 'CTXF', store);
117 goog.inherits(cvox.SpeechRuleFunctions.ContextFunctions,
118 cvox.SpeechRuleFunctions.Store_);
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.
127 cvox.SpeechRuleFunctions.Store_.prototype.
128 checkCustomFunctionSyntax_ = function(name) {
129 var reg = new RegExp('^' + this.prefix_);
130 if (!name.match(reg)) {
132 'FunctionError: Invalid function name. Expected prefix' +