-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-parser.js
161 lines (135 loc) · 3.89 KB
/
class-parser.js
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
/**
* Parses command scripts.
* @param {Object} config Options with which to initialize CommandParser.
*/
function CommandParser (config) {
var module = this;
module.scope = module;
module.regex = [
/'[^']*'/g,
/"[^"]*"/g,
/\[[^\]]*]/g
];
/**
* Initializes the parser. This is run on class instantiation.
* @param {Object} config The initialization object.
* @param {Object} config.scope The scope with which to run commands.
* @param {Object} config.commands The commands to bind to specific words.
*/
module.init = function (config) {
var prop;
for (prop in config) {
if (config.hasOwnProperty(prop)) {
module[prop] = config[prop];
}
}
};
module.init(config);
/**
* Begins the parsing process, then begins execution.
* @param {String} script The command script to parse.
*/
module.parse = function (script) {
var parsed,
words;
module.clean();
parsed = module.replaceArgs(script);
parsed = parsed.toLowerCase();
words = parsed.split(/\s+/g);
module.understand(words);
};
/**
* Used to move on to the next command. This should be called whenever needed
* in order to advance the command script. In this way, other code can await
* for an asyncronous action before proceeding to the next command.
*/
module.next = function () {
var set = module.queue.shift();
if (set) {
module.run.apply(module.scope, set);
}
};
/**
* @private
*
* Searches the script for quoted areas and replaces them with placeholders.
* @param {String} script The command script.
* @return {String} The command script with placeholders.
*/
module.replaceArgs = function (script) {
var i = module.regex.length;
while(i--) {
script = script.replace(module.regex[i], module.cacheMatch);
}
return script;
};
/**
* @private
*
* Caches the regex match passed to it and returns a placeholder.
* @param {String} match The regex match to cache.
* @return {String} The string placeholder.
*/
module.cacheMatch = function (match) {
return '{' + module.matches.push(match) + '}';
};
/**
* @private
*
* Goes through each word of the script and decides whether it needs to call
* a function or be an argument. Functions are expected to be before
* arguments, so calling the function is delayed until all of its arguments
* have been gathered. As soon as another function keyword is found, the
* waiting function gets called with the gathered arguments.
* @param {String[]} words An array of the words in the script.
*/
module.understand = function (words) {
var last = words.length,
i = 0,
args = [],
command,
atEnd,
word;
for (; i <= last; i++) {
atEnd = (i === last);
word = words[i];
if (module.commands[word] || atEnd) {
if (command) {
module.queue.push([command, args]);
args = [];
}
command = module.commands[word];
} else if (/\{\d+}/.test(word)) {
word = word.replace(/\{(\d+)}/,'$1');
word = module.matches[word - 1];
word = word.substring(1, word.length - 1);
args.push(word);
} else {
args.push(word);
}
}
module.next();
};
/**
* @private
*
* Runs the given command with the given arguments.
* @param {String} command The command to run.
* @param {Mixed} args The arguments to pass to the command.
*/
module.run = function (command, args) {
args.unshift(module);
command.apply(module.scope, args);
};
/**
* @private
*
* Cleans up by resetting the regex match list and function queue.
* @return {[type]} [description]
*/
module.clean = function () {
module.matches = [];
module.queue = [];
};
return module;
}