ot-jed 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/jed.js +868 -0
- package/package.json +28 -0
package/lib/jed.js
ADDED
@@ -0,0 +1,868 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
/**
|
7
|
+
* @preserve jed.js https://github.com/SlexAxton/Jed
|
8
|
+
*/
|
9
|
+
/*
|
10
|
+
-----------
|
11
|
+
A gettext compatible i18n library for modern JavaScript Applications
|
12
|
+
|
13
|
+
by Alex Sexton - AlexSexton [at] gmail - @SlexAxton
|
14
|
+
|
15
|
+
MIT License
|
16
|
+
|
17
|
+
A jQuery Foundation project - requires CLA to contribute -
|
18
|
+
https://contribute.jquery.org/CLA/
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Jed offers the entire applicable GNU gettext spec'd set of
|
23
|
+
functions, but also offers some nicer wrappers around them.
|
24
|
+
The api for gettext was written for a language with no function
|
25
|
+
overloading, so Jed allows a little more of that.
|
26
|
+
|
27
|
+
Many thanks to Joshua I. Miller - unrtst@cpan.org - who wrote
|
28
|
+
gettext.js back in 2008. I was able to vet a lot of my ideas
|
29
|
+
against his. I also made sure Jed passed against his tests
|
30
|
+
in order to offer easy upgrades -- jsgettext.berlios.de
|
31
|
+
*/
|
32
|
+
|
33
|
+
// Set up some underscore-style functions, if you already have
|
34
|
+
// underscore, feel free to delete this section, and use it
|
35
|
+
// directly, however, the amount of functions used doesn't
|
36
|
+
// warrant having underscore as a full dependency.
|
37
|
+
// Underscore 1.3.0 was used to port and is licensed
|
38
|
+
// under the MIT License by Jeremy Ashkenas.
|
39
|
+
var ArrayProto = Array.prototype,
|
40
|
+
ObjProto = Object.prototype,
|
41
|
+
slice = ArrayProto.slice,
|
42
|
+
hasOwnProp = ObjProto.hasOwnProperty,
|
43
|
+
nativeForEach = ArrayProto.forEach,
|
44
|
+
breaker = {};
|
45
|
+
|
46
|
+
// We're not using the OOP style _ so we don't need the
|
47
|
+
// extra level of indirection. This still means that you
|
48
|
+
// sub out for real `_` though.
|
49
|
+
var _ = {
|
50
|
+
forEach: function forEach(obj, iterator, context) {
|
51
|
+
var i, l, key;
|
52
|
+
if (obj === null) {
|
53
|
+
return;
|
54
|
+
}
|
55
|
+
|
56
|
+
if (nativeForEach && obj.forEach === nativeForEach) {
|
57
|
+
obj.forEach(iterator, context);
|
58
|
+
} else if (obj.length === +obj.length) {
|
59
|
+
for (i = 0, l = obj.length; i < l; i++) {
|
60
|
+
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) {
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
} else {
|
65
|
+
for (key in obj) {
|
66
|
+
if (hasOwnProp.call(obj, key)) {
|
67
|
+
if (iterator.call(context, obj[key], key, obj) === breaker) {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
},
|
74
|
+
extend: function extend(obj) {
|
75
|
+
this.forEach(slice.call(arguments, 1), function (source) {
|
76
|
+
for (var prop in source) {
|
77
|
+
obj[prop] = source[prop];
|
78
|
+
}
|
79
|
+
});
|
80
|
+
return obj;
|
81
|
+
},
|
82
|
+
// memoize function taken from lodash@4.17.21
|
83
|
+
// original function included
|
84
|
+
// memoized.cache = new (memoize.Cache || Map)
|
85
|
+
// changed to memoized.cache = new Map()
|
86
|
+
// added semicolons
|
87
|
+
memoize: function memoize(func, resolver) {
|
88
|
+
if (typeof func !== 'function' || resolver != null && typeof resolver !== 'function') {
|
89
|
+
throw new TypeError('Expected a function');
|
90
|
+
}
|
91
|
+
var memoized = function memoized() {
|
92
|
+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
93
|
+
args[_key] = arguments[_key];
|
94
|
+
}
|
95
|
+
|
96
|
+
var key = resolver ? resolver.apply(this, args) : args[0];
|
97
|
+
var cache = memoized.cache;
|
98
|
+
|
99
|
+
if (cache.has(key)) {
|
100
|
+
return cache.get(key);
|
101
|
+
}
|
102
|
+
var result = func.apply(this, args);
|
103
|
+
memoized.cache = cache.set(key, result) || cache;
|
104
|
+
return result;
|
105
|
+
};
|
106
|
+
memoized.cache = new Map();
|
107
|
+
return memoized;
|
108
|
+
}
|
109
|
+
};
|
110
|
+
// END Miniature underscore impl
|
111
|
+
|
112
|
+
// Jed is a constructor function
|
113
|
+
var Jed = function Jed(options) {
|
114
|
+
// Some minimal defaults
|
115
|
+
this.defaults = {
|
116
|
+
"locale_data": {
|
117
|
+
"messages": {
|
118
|
+
"": {
|
119
|
+
"domain": "messages",
|
120
|
+
"lang": "en",
|
121
|
+
"plural_forms": "nplurals=2; plural=(n != 1);"
|
122
|
+
// There are no default keys, though
|
123
|
+
} }
|
124
|
+
},
|
125
|
+
// The default domain if one is missing
|
126
|
+
"domain": "messages",
|
127
|
+
// enable debug mode to log untranslated strings to the console
|
128
|
+
"debug": false
|
129
|
+
};
|
130
|
+
|
131
|
+
// Mix in the sent options with the default options
|
132
|
+
this.options = _.extend({}, this.defaults, options);
|
133
|
+
this.textdomain(this.options.domain);
|
134
|
+
|
135
|
+
if (options.domain && !this.options.locale_data[this.options.domain]) {
|
136
|
+
throw new Error('Text domain set to non-existent domain: `' + options.domain + '`');
|
137
|
+
}
|
138
|
+
};
|
139
|
+
|
140
|
+
// The gettext spec sets this character as the default
|
141
|
+
// delimiter for context lookups.
|
142
|
+
// e.g.: context\u0004key
|
143
|
+
// If your translation company uses something different,
|
144
|
+
// just change this at any time and it will use that instead.
|
145
|
+
Jed.context_delimiter = String.fromCharCode(4);
|
146
|
+
|
147
|
+
function _getPluralFormFunc(plural_form_string) {
|
148
|
+
return Jed.PF.compile(plural_form_string || "nplurals=2; plural=(n != 1);");
|
149
|
+
}
|
150
|
+
|
151
|
+
// Memoizing two parts here
|
152
|
+
// 1st the compiler created from plural_form_string
|
153
|
+
// 2nd the compiled fn that text values are called on
|
154
|
+
|
155
|
+
var getPluralFormFunc = _.memoize(function (plural_form_string) {
|
156
|
+
var compiler = _getPluralFormFunc(plural_form_string);
|
157
|
+
return _.memoize(compiler);
|
158
|
+
});
|
159
|
+
|
160
|
+
// Add functions to the Jed prototype.
|
161
|
+
// These will be the functions on the object that's returned
|
162
|
+
// from creating a `new Jed()`
|
163
|
+
// These seem redundant, but they gzip pretty well.
|
164
|
+
_.extend(Jed.prototype, {
|
165
|
+
|
166
|
+
textdomain: function textdomain(domain) {
|
167
|
+
if (!domain) {
|
168
|
+
return this._textdomain;
|
169
|
+
}
|
170
|
+
this._textdomain = domain;
|
171
|
+
},
|
172
|
+
|
173
|
+
gettext: function gettext(key) {
|
174
|
+
return this.dcnpgettext.call(this, undefined, undefined, key);
|
175
|
+
},
|
176
|
+
|
177
|
+
dgettext: function dgettext(domain, key) {
|
178
|
+
return this.dcnpgettext.call(this, domain, undefined, key);
|
179
|
+
},
|
180
|
+
|
181
|
+
dcgettext: function dcgettext(domain, key /*, category */) {
|
182
|
+
// Ignores the category anyways
|
183
|
+
return this.dcnpgettext.call(this, domain, undefined, key);
|
184
|
+
},
|
185
|
+
|
186
|
+
ngettext: function ngettext(skey, pkey, val) {
|
187
|
+
return this.dcnpgettext.call(this, undefined, undefined, skey, pkey, val);
|
188
|
+
},
|
189
|
+
|
190
|
+
dngettext: function dngettext(domain, skey, pkey, val) {
|
191
|
+
return this.dcnpgettext.call(this, domain, undefined, skey, pkey, val);
|
192
|
+
},
|
193
|
+
|
194
|
+
dcngettext: function dcngettext(domain, skey, pkey, val /*, category */) {
|
195
|
+
return this.dcnpgettext.call(this, domain, undefined, skey, pkey, val);
|
196
|
+
},
|
197
|
+
|
198
|
+
pgettext: function pgettext(context, key) {
|
199
|
+
return this.dcnpgettext.call(this, undefined, context, key);
|
200
|
+
},
|
201
|
+
|
202
|
+
dpgettext: function dpgettext(domain, context, key) {
|
203
|
+
return this.dcnpgettext.call(this, domain, context, key);
|
204
|
+
},
|
205
|
+
|
206
|
+
dcpgettext: function dcpgettext(domain, context, key /*, category */) {
|
207
|
+
return this.dcnpgettext.call(this, domain, context, key);
|
208
|
+
},
|
209
|
+
|
210
|
+
npgettext: function npgettext(context, skey, pkey, val) {
|
211
|
+
return this.dcnpgettext.call(this, undefined, context, skey, pkey, val);
|
212
|
+
},
|
213
|
+
|
214
|
+
dnpgettext: function dnpgettext(domain, context, skey, pkey, val) {
|
215
|
+
return this.dcnpgettext.call(this, domain, context, skey, pkey, val);
|
216
|
+
},
|
217
|
+
|
218
|
+
// The most fully qualified gettext function. It has every option.
|
219
|
+
// Since it has every option, we can use it from every other method.
|
220
|
+
// This is the bread and butter.
|
221
|
+
// Technically there should be one more argument in this function for 'Category',
|
222
|
+
// but since we never use it, we might as well not waste the bytes to define it.
|
223
|
+
dcnpgettext: function dcnpgettext(domain, context, singular_key, plural_key, val) {
|
224
|
+
// Set some defaults
|
225
|
+
|
226
|
+
plural_key = plural_key || singular_key;
|
227
|
+
|
228
|
+
// Use the global domain default if one
|
229
|
+
// isn't explicitly passed in
|
230
|
+
domain = domain || this._textdomain;
|
231
|
+
|
232
|
+
var fallback;
|
233
|
+
|
234
|
+
// Handle special cases
|
235
|
+
|
236
|
+
// No options found
|
237
|
+
if (!this.options) {
|
238
|
+
// There's likely something wrong, but we'll return the correct key for english
|
239
|
+
// We do this by instantiating a brand new Jed instance with the default set
|
240
|
+
// for everything that could be broken.
|
241
|
+
fallback = new Jed();
|
242
|
+
return fallback.dcnpgettext.call(fallback, undefined, undefined, singular_key, plural_key, val);
|
243
|
+
}
|
244
|
+
|
245
|
+
// No translation data provided
|
246
|
+
if (!this.options.locale_data) {
|
247
|
+
throw new Error('No locale data provided.');
|
248
|
+
}
|
249
|
+
|
250
|
+
if (!this.options.locale_data[domain]) {
|
251
|
+
throw new Error('Domain `' + domain + '` was not found.');
|
252
|
+
}
|
253
|
+
|
254
|
+
if (!this.options.locale_data[domain][""]) {
|
255
|
+
throw new Error('No locale meta information provided.');
|
256
|
+
}
|
257
|
+
|
258
|
+
// Make sure we have a truthy key. Otherwise we might start looking
|
259
|
+
// into the empty string key, which is the options for the locale
|
260
|
+
// data.
|
261
|
+
if (!singular_key) {
|
262
|
+
throw new Error('No translation key found.');
|
263
|
+
}
|
264
|
+
|
265
|
+
var key = context ? context + Jed.context_delimiter + singular_key : singular_key,
|
266
|
+
locale_data = this.options.locale_data,
|
267
|
+
dict = locale_data[domain],
|
268
|
+
defaultConf = (locale_data.messages || this.defaults.locale_data.messages)[""],
|
269
|
+
pluralForms = dict[""].plural_forms || dict[""]["Plural-Forms"] || dict[""]["plural-forms"] || defaultConf.plural_forms || defaultConf["Plural-Forms"] || defaultConf["plural-forms"],
|
270
|
+
val_list,
|
271
|
+
res;
|
272
|
+
|
273
|
+
var val_idx;
|
274
|
+
if (val === undefined) {
|
275
|
+
// No value passed in; assume singular key lookup.
|
276
|
+
val_idx = 0;
|
277
|
+
} else {
|
278
|
+
// Value has been passed in; use plural-forms calculations.
|
279
|
+
|
280
|
+
// Handle invalid numbers, but try casting strings for good measure
|
281
|
+
if (typeof val != 'number') {
|
282
|
+
val = parseInt(val, 10);
|
283
|
+
|
284
|
+
if (isNaN(val)) {
|
285
|
+
throw new Error('The number that was passed in is not a number.');
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
val_idx = getPluralFormFunc(pluralForms)(val);
|
290
|
+
}
|
291
|
+
|
292
|
+
// Throw an error if a domain isn't found
|
293
|
+
if (!dict) {
|
294
|
+
throw new Error('No domain named `' + domain + '` could be found.');
|
295
|
+
}
|
296
|
+
|
297
|
+
val_list = dict[key];
|
298
|
+
|
299
|
+
// If there is no match, then revert back to
|
300
|
+
// english style singular/plural with the keys passed in.
|
301
|
+
if (!val_list || val_idx > val_list.length) {
|
302
|
+
if (this.options.missing_key_callback) {
|
303
|
+
this.options.missing_key_callback(key, domain);
|
304
|
+
}
|
305
|
+
res = [singular_key, plural_key];
|
306
|
+
|
307
|
+
// collect untranslated strings
|
308
|
+
if (this.options.debug === true) {
|
309
|
+
console.log(res[getPluralFormFunc(pluralForms)(val)]);
|
310
|
+
}
|
311
|
+
return res[getPluralFormFunc()(val)];
|
312
|
+
}
|
313
|
+
|
314
|
+
res = val_list[val_idx];
|
315
|
+
|
316
|
+
// This includes empty strings on purpose
|
317
|
+
if (!res) {
|
318
|
+
res = [singular_key, plural_key];
|
319
|
+
return res[getPluralFormFunc()(val)];
|
320
|
+
}
|
321
|
+
return res;
|
322
|
+
}
|
323
|
+
});
|
324
|
+
|
325
|
+
// Start the Plural forms section
|
326
|
+
// This is a full plural form expression parser. It is used to avoid
|
327
|
+
// running 'eval' or 'new Function' directly against the plural
|
328
|
+
// forms.
|
329
|
+
//
|
330
|
+
// This can be important if you get translations done through a 3rd
|
331
|
+
// party vendor. I encourage you to use this instead, however, I
|
332
|
+
// also will provide a 'precompiler' that you can use at build time
|
333
|
+
// to output valid/safe function representations of the plural form
|
334
|
+
// expressions. This means you can build this code out for the most
|
335
|
+
// part.
|
336
|
+
Jed.PF = {};
|
337
|
+
|
338
|
+
Jed.PF.parse = function (p) {
|
339
|
+
var plural_str = Jed.PF.extractPluralExpr(p);
|
340
|
+
return Jed.PF.parser.parse.call(Jed.PF.parser, plural_str);
|
341
|
+
};
|
342
|
+
|
343
|
+
Jed.PF.compile = function (p) {
|
344
|
+
// Handle trues and falses as 0 and 1
|
345
|
+
function imply(val) {
|
346
|
+
return val === true ? 1 : val ? val : 0;
|
347
|
+
}
|
348
|
+
|
349
|
+
var ast = Jed.PF.parse(p);
|
350
|
+
return function (n) {
|
351
|
+
return imply(Jed.PF.interpreter(ast)(n));
|
352
|
+
};
|
353
|
+
};
|
354
|
+
|
355
|
+
Jed.PF.interpreter = function (ast) {
|
356
|
+
return function (n) {
|
357
|
+
var res;
|
358
|
+
switch (ast.type) {
|
359
|
+
case 'GROUP':
|
360
|
+
return Jed.PF.interpreter(ast.expr)(n);
|
361
|
+
case 'TERNARY':
|
362
|
+
if (Jed.PF.interpreter(ast.expr)(n)) {
|
363
|
+
return Jed.PF.interpreter(ast.truthy)(n);
|
364
|
+
}
|
365
|
+
return Jed.PF.interpreter(ast.falsey)(n);
|
366
|
+
case 'OR':
|
367
|
+
return Jed.PF.interpreter(ast.left)(n) || Jed.PF.interpreter(ast.right)(n);
|
368
|
+
case 'AND':
|
369
|
+
return Jed.PF.interpreter(ast.left)(n) && Jed.PF.interpreter(ast.right)(n);
|
370
|
+
case 'LT':
|
371
|
+
return Jed.PF.interpreter(ast.left)(n) < Jed.PF.interpreter(ast.right)(n);
|
372
|
+
case 'GT':
|
373
|
+
return Jed.PF.interpreter(ast.left)(n) > Jed.PF.interpreter(ast.right)(n);
|
374
|
+
case 'LTE':
|
375
|
+
return Jed.PF.interpreter(ast.left)(n) <= Jed.PF.interpreter(ast.right)(n);
|
376
|
+
case 'GTE':
|
377
|
+
return Jed.PF.interpreter(ast.left)(n) >= Jed.PF.interpreter(ast.right)(n);
|
378
|
+
case 'EQ':
|
379
|
+
return Jed.PF.interpreter(ast.left)(n) == Jed.PF.interpreter(ast.right)(n);
|
380
|
+
case 'NEQ':
|
381
|
+
return Jed.PF.interpreter(ast.left)(n) != Jed.PF.interpreter(ast.right)(n);
|
382
|
+
case 'MOD':
|
383
|
+
return Jed.PF.interpreter(ast.left)(n) % Jed.PF.interpreter(ast.right)(n);
|
384
|
+
case 'VAR':
|
385
|
+
return n;
|
386
|
+
case 'NUM':
|
387
|
+
return ast.val;
|
388
|
+
default:
|
389
|
+
throw new Error("Invalid Token found.");
|
390
|
+
}
|
391
|
+
};
|
392
|
+
};
|
393
|
+
|
394
|
+
Jed.PF.regexps = {
|
395
|
+
TRIM_BEG: /^\s\s*/,
|
396
|
+
TRIM_END: /\s\s*$/,
|
397
|
+
HAS_SEMICOLON: /;\s*$/,
|
398
|
+
NPLURALS: /nplurals\=(\d+);/,
|
399
|
+
PLURAL: /plural\=(.*);/
|
400
|
+
};
|
401
|
+
|
402
|
+
Jed.PF.extractPluralExpr = function (p) {
|
403
|
+
// trim first
|
404
|
+
p = p.replace(Jed.PF.regexps.TRIM_BEG, '').replace(Jed.PF.regexps.TRIM_END, '');
|
405
|
+
|
406
|
+
if (!Jed.PF.regexps.HAS_SEMICOLON.test(p)) {
|
407
|
+
p = p.concat(';');
|
408
|
+
}
|
409
|
+
|
410
|
+
var nplurals_matches = p.match(Jed.PF.regexps.NPLURALS),
|
411
|
+
res = {},
|
412
|
+
plural_matches;
|
413
|
+
|
414
|
+
// Find the nplurals number
|
415
|
+
if (nplurals_matches.length > 1) {
|
416
|
+
res.nplurals = nplurals_matches[1];
|
417
|
+
} else {
|
418
|
+
throw new Error('nplurals not found in plural_forms string: ' + p);
|
419
|
+
}
|
420
|
+
|
421
|
+
// remove that data to get to the formula
|
422
|
+
p = p.replace(Jed.PF.regexps.NPLURALS, "");
|
423
|
+
plural_matches = p.match(Jed.PF.regexps.PLURAL);
|
424
|
+
|
425
|
+
if (!(plural_matches && plural_matches.length > 1)) {
|
426
|
+
throw new Error('`plural` expression not found: ' + p);
|
427
|
+
}
|
428
|
+
return plural_matches[1];
|
429
|
+
};
|
430
|
+
|
431
|
+
/* Jison generated parser */
|
432
|
+
Jed.PF.parser = function () {
|
433
|
+
|
434
|
+
var parser = { trace: function trace() {},
|
435
|
+
yy: {},
|
436
|
+
symbols_: { "error": 2, "expressions": 3, "e": 4, "EOF": 5, "?": 6, ":": 7, "||": 8, "&&": 9, "<": 10, "<=": 11, ">": 12, ">=": 13, "!=": 14, "==": 15, "%": 16, "(": 17, ")": 18, "n": 19, "NUMBER": 20, "$accept": 0, "$end": 1 },
|
437
|
+
terminals_: { 2: "error", 5: "EOF", 6: "?", 7: ":", 8: "||", 9: "&&", 10: "<", 11: "<=", 12: ">", 13: ">=", 14: "!=", 15: "==", 16: "%", 17: "(", 18: ")", 19: "n", 20: "NUMBER" },
|
438
|
+
productions_: [0, [3, 2], [4, 5], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 1], [4, 1]],
|
439
|
+
performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate, $$, _$) {
|
440
|
+
|
441
|
+
var $0 = $$.length - 1;
|
442
|
+
switch (yystate) {
|
443
|
+
case 1:
|
444
|
+
return { type: 'GROUP', expr: $$[$0 - 1] };
|
445
|
+
break;
|
446
|
+
case 2:
|
447
|
+
this.$ = { type: 'TERNARY', expr: $$[$0 - 4], truthy: $$[$0 - 2], falsey: $$[$0] };
|
448
|
+
break;
|
449
|
+
case 3:
|
450
|
+
this.$ = { type: "OR", left: $$[$0 - 2], right: $$[$0] };
|
451
|
+
break;
|
452
|
+
case 4:
|
453
|
+
this.$ = { type: "AND", left: $$[$0 - 2], right: $$[$0] };
|
454
|
+
break;
|
455
|
+
case 5:
|
456
|
+
this.$ = { type: 'LT', left: $$[$0 - 2], right: $$[$0] };
|
457
|
+
break;
|
458
|
+
case 6:
|
459
|
+
this.$ = { type: 'LTE', left: $$[$0 - 2], right: $$[$0] };
|
460
|
+
break;
|
461
|
+
case 7:
|
462
|
+
this.$ = { type: 'GT', left: $$[$0 - 2], right: $$[$0] };
|
463
|
+
break;
|
464
|
+
case 8:
|
465
|
+
this.$ = { type: 'GTE', left: $$[$0 - 2], right: $$[$0] };
|
466
|
+
break;
|
467
|
+
case 9:
|
468
|
+
this.$ = { type: 'NEQ', left: $$[$0 - 2], right: $$[$0] };
|
469
|
+
break;
|
470
|
+
case 10:
|
471
|
+
this.$ = { type: 'EQ', left: $$[$0 - 2], right: $$[$0] };
|
472
|
+
break;
|
473
|
+
case 11:
|
474
|
+
this.$ = { type: 'MOD', left: $$[$0 - 2], right: $$[$0] };
|
475
|
+
break;
|
476
|
+
case 12:
|
477
|
+
this.$ = { type: 'GROUP', expr: $$[$0 - 1] };
|
478
|
+
break;
|
479
|
+
case 13:
|
480
|
+
this.$ = { type: 'VAR' };
|
481
|
+
break;
|
482
|
+
case 14:
|
483
|
+
this.$ = { type: 'NUM', val: Number(yytext) };
|
484
|
+
break;
|
485
|
+
}
|
486
|
+
},
|
487
|
+
table: [{ 3: 1, 4: 2, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 1: [3] }, { 5: [1, 6], 6: [1, 7], 8: [1, 8], 9: [1, 9], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16] }, { 4: 17, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 5: [2, 13], 6: [2, 13], 7: [2, 13], 8: [2, 13], 9: [2, 13], 10: [2, 13], 11: [2, 13], 12: [2, 13], 13: [2, 13], 14: [2, 13], 15: [2, 13], 16: [2, 13], 18: [2, 13] }, { 5: [2, 14], 6: [2, 14], 7: [2, 14], 8: [2, 14], 9: [2, 14], 10: [2, 14], 11: [2, 14], 12: [2, 14], 13: [2, 14], 14: [2, 14], 15: [2, 14], 16: [2, 14], 18: [2, 14] }, { 1: [2, 1] }, { 4: 18, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 19, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 20, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 21, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 22, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 23, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 24, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 25, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 26, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 4: 27, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 6: [1, 7], 8: [1, 8], 9: [1, 9], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16], 18: [1, 28] }, { 6: [1, 7], 7: [1, 29], 8: [1, 8], 9: [1, 9], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16] }, { 5: [2, 3], 6: [2, 3], 7: [2, 3], 8: [2, 3], 9: [1, 9], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16], 18: [2, 3] }, { 5: [2, 4], 6: [2, 4], 7: [2, 4], 8: [2, 4], 9: [2, 4], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16], 18: [2, 4] }, { 5: [2, 5], 6: [2, 5], 7: [2, 5], 8: [2, 5], 9: [2, 5], 10: [2, 5], 11: [2, 5], 12: [2, 5], 13: [2, 5], 14: [2, 5], 15: [2, 5], 16: [1, 16], 18: [2, 5] }, { 5: [2, 6], 6: [2, 6], 7: [2, 6], 8: [2, 6], 9: [2, 6], 10: [2, 6], 11: [2, 6], 12: [2, 6], 13: [2, 6], 14: [2, 6], 15: [2, 6], 16: [1, 16], 18: [2, 6] }, { 5: [2, 7], 6: [2, 7], 7: [2, 7], 8: [2, 7], 9: [2, 7], 10: [2, 7], 11: [2, 7], 12: [2, 7], 13: [2, 7], 14: [2, 7], 15: [2, 7], 16: [1, 16], 18: [2, 7] }, { 5: [2, 8], 6: [2, 8], 7: [2, 8], 8: [2, 8], 9: [2, 8], 10: [2, 8], 11: [2, 8], 12: [2, 8], 13: [2, 8], 14: [2, 8], 15: [2, 8], 16: [1, 16], 18: [2, 8] }, { 5: [2, 9], 6: [2, 9], 7: [2, 9], 8: [2, 9], 9: [2, 9], 10: [2, 9], 11: [2, 9], 12: [2, 9], 13: [2, 9], 14: [2, 9], 15: [2, 9], 16: [1, 16], 18: [2, 9] }, { 5: [2, 10], 6: [2, 10], 7: [2, 10], 8: [2, 10], 9: [2, 10], 10: [2, 10], 11: [2, 10], 12: [2, 10], 13: [2, 10], 14: [2, 10], 15: [2, 10], 16: [1, 16], 18: [2, 10] }, { 5: [2, 11], 6: [2, 11], 7: [2, 11], 8: [2, 11], 9: [2, 11], 10: [2, 11], 11: [2, 11], 12: [2, 11], 13: [2, 11], 14: [2, 11], 15: [2, 11], 16: [2, 11], 18: [2, 11] }, { 5: [2, 12], 6: [2, 12], 7: [2, 12], 8: [2, 12], 9: [2, 12], 10: [2, 12], 11: [2, 12], 12: [2, 12], 13: [2, 12], 14: [2, 12], 15: [2, 12], 16: [2, 12], 18: [2, 12] }, { 4: 30, 17: [1, 3], 19: [1, 4], 20: [1, 5] }, { 5: [2, 2], 6: [1, 7], 7: [2, 2], 8: [1, 8], 9: [1, 9], 10: [1, 10], 11: [1, 11], 12: [1, 12], 13: [1, 13], 14: [1, 14], 15: [1, 15], 16: [1, 16], 18: [2, 2] }],
|
488
|
+
defaultActions: { 6: [2, 1] },
|
489
|
+
parseError: function parseError(str, hash) {
|
490
|
+
throw new Error(str);
|
491
|
+
},
|
492
|
+
parse: function parse(input) {
|
493
|
+
var self = this,
|
494
|
+
stack = [0],
|
495
|
+
vstack = [null],
|
496
|
+
// semantic value stack
|
497
|
+
lstack = [],
|
498
|
+
// location stack
|
499
|
+
table = this.table,
|
500
|
+
yytext = '',
|
501
|
+
yylineno = 0,
|
502
|
+
yyleng = 0,
|
503
|
+
recovering = 0,
|
504
|
+
TERROR = 2,
|
505
|
+
EOF = 1;
|
506
|
+
|
507
|
+
//this.reductionCount = this.shiftCount = 0;
|
508
|
+
|
509
|
+
this.lexer.setInput(input);
|
510
|
+
this.lexer.yy = this.yy;
|
511
|
+
this.yy.lexer = this.lexer;
|
512
|
+
if (typeof this.lexer.yylloc == 'undefined') this.lexer.yylloc = {};
|
513
|
+
var yyloc = this.lexer.yylloc;
|
514
|
+
lstack.push(yyloc);
|
515
|
+
|
516
|
+
if (typeof this.yy.parseError === 'function') this.parseError = this.yy.parseError;
|
517
|
+
|
518
|
+
function popStack(n) {
|
519
|
+
stack.length = stack.length - 2 * n;
|
520
|
+
vstack.length = vstack.length - n;
|
521
|
+
lstack.length = lstack.length - n;
|
522
|
+
}
|
523
|
+
|
524
|
+
function lex() {
|
525
|
+
var token;
|
526
|
+
token = self.lexer.lex() || 1; // $end = 1
|
527
|
+
// if token isn't its numeric value, convert
|
528
|
+
if (typeof token !== 'number') {
|
529
|
+
token = self.symbols_[token] || token;
|
530
|
+
}
|
531
|
+
return token;
|
532
|
+
}
|
533
|
+
|
534
|
+
var symbol,
|
535
|
+
preErrorSymbol,
|
536
|
+
state,
|
537
|
+
action,
|
538
|
+
a,
|
539
|
+
r,
|
540
|
+
yyval = {},
|
541
|
+
p,
|
542
|
+
len,
|
543
|
+
newState,
|
544
|
+
expected;
|
545
|
+
while (true) {
|
546
|
+
// retreive state number from top of stack
|
547
|
+
state = stack[stack.length - 1];
|
548
|
+
|
549
|
+
// use default actions if available
|
550
|
+
if (this.defaultActions[state]) {
|
551
|
+
action = this.defaultActions[state];
|
552
|
+
} else {
|
553
|
+
if (symbol == null) symbol = lex();
|
554
|
+
// read action for current state and first input
|
555
|
+
action = table[state] && table[state][symbol];
|
556
|
+
}
|
557
|
+
|
558
|
+
// handle parse error
|
559
|
+
_handle_error: if (typeof action === 'undefined' || !action.length || !action[0]) {
|
560
|
+
|
561
|
+
if (!recovering) {
|
562
|
+
// Report error
|
563
|
+
expected = [];
|
564
|
+
for (p in table[state]) {
|
565
|
+
if (this.terminals_[p] && p > 2) {
|
566
|
+
expected.push("'" + this.terminals_[p] + "'");
|
567
|
+
}
|
568
|
+
}var errStr = '';
|
569
|
+
if (this.lexer.showPosition) {
|
570
|
+
errStr = 'Parse error on line ' + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(', ') + ", got '" + this.terminals_[symbol] + "'";
|
571
|
+
} else {
|
572
|
+
errStr = 'Parse error on line ' + (yylineno + 1) + ": Unexpected " + (symbol == 1 /*EOF*/ ? "end of input" : "'" + (this.terminals_[symbol] || symbol) + "'");
|
573
|
+
}
|
574
|
+
this.parseError(errStr, { text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected });
|
575
|
+
}
|
576
|
+
|
577
|
+
// just recovered from another error
|
578
|
+
if (recovering == 3) {
|
579
|
+
if (symbol == EOF) {
|
580
|
+
throw new Error(errStr || 'Parsing halted.');
|
581
|
+
}
|
582
|
+
|
583
|
+
// discard current lookahead and grab another
|
584
|
+
yyleng = this.lexer.yyleng;
|
585
|
+
yytext = this.lexer.yytext;
|
586
|
+
yylineno = this.lexer.yylineno;
|
587
|
+
yyloc = this.lexer.yylloc;
|
588
|
+
symbol = lex();
|
589
|
+
}
|
590
|
+
|
591
|
+
// try to recover from error
|
592
|
+
while (1) {
|
593
|
+
// check for error recovery rule in this state
|
594
|
+
if (TERROR.toString() in table[state]) {
|
595
|
+
break;
|
596
|
+
}
|
597
|
+
if (state == 0) {
|
598
|
+
throw new Error(errStr || 'Parsing halted.');
|
599
|
+
}
|
600
|
+
popStack(1);
|
601
|
+
state = stack[stack.length - 1];
|
602
|
+
}
|
603
|
+
|
604
|
+
preErrorSymbol = symbol; // save the lookahead token
|
605
|
+
symbol = TERROR; // insert generic error symbol as new lookahead
|
606
|
+
state = stack[stack.length - 1];
|
607
|
+
action = table[state] && table[state][TERROR];
|
608
|
+
recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
|
609
|
+
}
|
610
|
+
|
611
|
+
// this shouldn't happen, unless resolve defaults are off
|
612
|
+
if (action[0] instanceof Array && action.length > 1) {
|
613
|
+
throw new Error('Parse Error: multiple actions possible at state: ' + state + ', token: ' + symbol);
|
614
|
+
}
|
615
|
+
|
616
|
+
switch (action[0]) {
|
617
|
+
|
618
|
+
case 1:
|
619
|
+
// shift
|
620
|
+
//this.shiftCount++;
|
621
|
+
|
622
|
+
stack.push(symbol);
|
623
|
+
vstack.push(this.lexer.yytext);
|
624
|
+
lstack.push(this.lexer.yylloc);
|
625
|
+
stack.push(action[1]); // push state
|
626
|
+
symbol = null;
|
627
|
+
if (!preErrorSymbol) {
|
628
|
+
// normal execution/no error
|
629
|
+
yyleng = this.lexer.yyleng;
|
630
|
+
yytext = this.lexer.yytext;
|
631
|
+
yylineno = this.lexer.yylineno;
|
632
|
+
yyloc = this.lexer.yylloc;
|
633
|
+
if (recovering > 0) recovering--;
|
634
|
+
} else {
|
635
|
+
// error just occurred, resume old lookahead f/ before error
|
636
|
+
symbol = preErrorSymbol;
|
637
|
+
preErrorSymbol = null;
|
638
|
+
}
|
639
|
+
break;
|
640
|
+
|
641
|
+
case 2:
|
642
|
+
// reduce
|
643
|
+
//this.reductionCount++;
|
644
|
+
|
645
|
+
len = this.productions_[action[1]][1];
|
646
|
+
|
647
|
+
// perform semantic action
|
648
|
+
yyval.$ = vstack[vstack.length - len]; // default to $$ = $1
|
649
|
+
// default location, uses first token for firsts, last for lasts
|
650
|
+
yyval._$ = {
|
651
|
+
first_line: lstack[lstack.length - (len || 1)].first_line,
|
652
|
+
last_line: lstack[lstack.length - 1].last_line,
|
653
|
+
first_column: lstack[lstack.length - (len || 1)].first_column,
|
654
|
+
last_column: lstack[lstack.length - 1].last_column
|
655
|
+
};
|
656
|
+
r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
|
657
|
+
|
658
|
+
if (typeof r !== 'undefined') {
|
659
|
+
return r;
|
660
|
+
}
|
661
|
+
|
662
|
+
// pop off stack
|
663
|
+
if (len) {
|
664
|
+
stack = stack.slice(0, -1 * len * 2);
|
665
|
+
vstack = vstack.slice(0, -1 * len);
|
666
|
+
lstack = lstack.slice(0, -1 * len);
|
667
|
+
}
|
668
|
+
|
669
|
+
stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce)
|
670
|
+
vstack.push(yyval.$);
|
671
|
+
lstack.push(yyval._$);
|
672
|
+
// goto new state = table[STATE][NONTERMINAL]
|
673
|
+
newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
|
674
|
+
stack.push(newState);
|
675
|
+
break;
|
676
|
+
|
677
|
+
case 3:
|
678
|
+
// accept
|
679
|
+
return true;
|
680
|
+
}
|
681
|
+
}
|
682
|
+
|
683
|
+
return true;
|
684
|
+
} }; /* Jison generated lexer */
|
685
|
+
var lexer = function () {
|
686
|
+
|
687
|
+
var lexer = { EOF: 1,
|
688
|
+
parseError: function parseError(str, hash) {
|
689
|
+
if (this.yy.parseError) {
|
690
|
+
this.yy.parseError(str, hash);
|
691
|
+
} else {
|
692
|
+
throw new Error(str);
|
693
|
+
}
|
694
|
+
},
|
695
|
+
setInput: function setInput(input) {
|
696
|
+
this._input = input;
|
697
|
+
this._more = this._less = this.done = false;
|
698
|
+
this.yylineno = this.yyleng = 0;
|
699
|
+
this.yytext = this.matched = this.match = '';
|
700
|
+
this.conditionStack = ['INITIAL'];
|
701
|
+
this.yylloc = { first_line: 1, first_column: 0, last_line: 1, last_column: 0 };
|
702
|
+
return this;
|
703
|
+
},
|
704
|
+
input: function input() {
|
705
|
+
var ch = this._input[0];
|
706
|
+
this.yytext += ch;
|
707
|
+
this.yyleng++;
|
708
|
+
this.match += ch;
|
709
|
+
this.matched += ch;
|
710
|
+
var lines = ch.match(/\n/);
|
711
|
+
if (lines) this.yylineno++;
|
712
|
+
this._input = this._input.slice(1);
|
713
|
+
return ch;
|
714
|
+
},
|
715
|
+
unput: function unput(ch) {
|
716
|
+
this._input = ch + this._input;
|
717
|
+
return this;
|
718
|
+
},
|
719
|
+
more: function more() {
|
720
|
+
this._more = true;
|
721
|
+
return this;
|
722
|
+
},
|
723
|
+
pastInput: function pastInput() {
|
724
|
+
var past = this.matched.substr(0, this.matched.length - this.match.length);
|
725
|
+
return (past.length > 20 ? '...' : '') + past.substr(-20).replace(/\n/g, "");
|
726
|
+
},
|
727
|
+
upcomingInput: function upcomingInput() {
|
728
|
+
var next = this.match;
|
729
|
+
if (next.length < 20) {
|
730
|
+
next += this._input.substr(0, 20 - next.length);
|
731
|
+
}
|
732
|
+
return (next.substr(0, 20) + (next.length > 20 ? '...' : '')).replace(/\n/g, "");
|
733
|
+
},
|
734
|
+
showPosition: function showPosition() {
|
735
|
+
var pre = this.pastInput();
|
736
|
+
var c = new Array(pre.length + 1).join("-");
|
737
|
+
return pre + this.upcomingInput() + "\n" + c + "^";
|
738
|
+
},
|
739
|
+
next: function next() {
|
740
|
+
if (this.done) {
|
741
|
+
return this.EOF;
|
742
|
+
}
|
743
|
+
if (!this._input) this.done = true;
|
744
|
+
|
745
|
+
var token, match, col, lines;
|
746
|
+
if (!this._more) {
|
747
|
+
this.yytext = '';
|
748
|
+
this.match = '';
|
749
|
+
}
|
750
|
+
var rules = this._currentRules();
|
751
|
+
for (var i = 0; i < rules.length; i++) {
|
752
|
+
match = this._input.match(this.rules[rules[i]]);
|
753
|
+
if (match) {
|
754
|
+
lines = match[0].match(/\n.*/g);
|
755
|
+
if (lines) this.yylineno += lines.length;
|
756
|
+
this.yylloc = { first_line: this.yylloc.last_line,
|
757
|
+
last_line: this.yylineno + 1,
|
758
|
+
first_column: this.yylloc.last_column,
|
759
|
+
last_column: lines ? lines[lines.length - 1].length - 1 : this.yylloc.last_column + match[0].length };
|
760
|
+
this.yytext += match[0];
|
761
|
+
this.match += match[0];
|
762
|
+
this.matches = match;
|
763
|
+
this.yyleng = this.yytext.length;
|
764
|
+
this._more = false;
|
765
|
+
this._input = this._input.slice(match[0].length);
|
766
|
+
this.matched += match[0];
|
767
|
+
token = this.performAction.call(this, this.yy, this, rules[i], this.conditionStack[this.conditionStack.length - 1]);
|
768
|
+
if (token) return token;else return;
|
769
|
+
}
|
770
|
+
}
|
771
|
+
if (this._input === "") {
|
772
|
+
return this.EOF;
|
773
|
+
} else {
|
774
|
+
this.parseError('Lexical error on line ' + (this.yylineno + 1) + '. Unrecognized text.\n' + this.showPosition(), { text: "", token: null, line: this.yylineno });
|
775
|
+
}
|
776
|
+
},
|
777
|
+
lex: function lex() {
|
778
|
+
var r = this.next();
|
779
|
+
if (typeof r !== 'undefined') {
|
780
|
+
return r;
|
781
|
+
} else {
|
782
|
+
return this.lex();
|
783
|
+
}
|
784
|
+
},
|
785
|
+
begin: function begin(condition) {
|
786
|
+
this.conditionStack.push(condition);
|
787
|
+
},
|
788
|
+
popState: function popState() {
|
789
|
+
return this.conditionStack.pop();
|
790
|
+
},
|
791
|
+
_currentRules: function _currentRules() {
|
792
|
+
return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules;
|
793
|
+
},
|
794
|
+
topState: function topState() {
|
795
|
+
return this.conditionStack[this.conditionStack.length - 2];
|
796
|
+
},
|
797
|
+
pushState: function begin(condition) {
|
798
|
+
this.begin(condition);
|
799
|
+
} };
|
800
|
+
lexer.performAction = function anonymous(yy, yy_, $avoiding_name_collisions, YY_START) {
|
801
|
+
|
802
|
+
var YYSTATE = YY_START;
|
803
|
+
switch ($avoiding_name_collisions) {
|
804
|
+
case 0:
|
805
|
+
/* skip whitespace */
|
806
|
+
break;
|
807
|
+
case 1:
|
808
|
+
return 20;
|
809
|
+
break;
|
810
|
+
case 2:
|
811
|
+
return 19;
|
812
|
+
break;
|
813
|
+
case 3:
|
814
|
+
return 8;
|
815
|
+
break;
|
816
|
+
case 4:
|
817
|
+
return 9;
|
818
|
+
break;
|
819
|
+
case 5:
|
820
|
+
return 6;
|
821
|
+
break;
|
822
|
+
case 6:
|
823
|
+
return 7;
|
824
|
+
break;
|
825
|
+
case 7:
|
826
|
+
return 11;
|
827
|
+
break;
|
828
|
+
case 8:
|
829
|
+
return 13;
|
830
|
+
break;
|
831
|
+
case 9:
|
832
|
+
return 10;
|
833
|
+
break;
|
834
|
+
case 10:
|
835
|
+
return 12;
|
836
|
+
break;
|
837
|
+
case 11:
|
838
|
+
return 14;
|
839
|
+
break;
|
840
|
+
case 12:
|
841
|
+
return 15;
|
842
|
+
break;
|
843
|
+
case 13:
|
844
|
+
return 16;
|
845
|
+
break;
|
846
|
+
case 14:
|
847
|
+
return 17;
|
848
|
+
break;
|
849
|
+
case 15:
|
850
|
+
return 18;
|
851
|
+
break;
|
852
|
+
case 16:
|
853
|
+
return 5;
|
854
|
+
break;
|
855
|
+
case 17:
|
856
|
+
return 'INVALID';
|
857
|
+
break;
|
858
|
+
}
|
859
|
+
};
|
860
|
+
lexer.rules = [/^\s+/, /^[0-9]+(\.[0-9]+)?\b/, /^n\b/, /^\|\|/, /^&&/, /^\?/, /^:/, /^<=/, /^>=/, /^</, /^>/, /^!=/, /^==/, /^%/, /^\(/, /^\)/, /^$/, /^./];
|
861
|
+
lexer.conditions = { "INITIAL": { "rules": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "inclusive": true } };return lexer;
|
862
|
+
}();
|
863
|
+
parser.lexer = lexer;
|
864
|
+
return parser;
|
865
|
+
}();
|
866
|
+
// End parser
|
867
|
+
|
868
|
+
exports.default = Jed;
|
package/package.json
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"name": "ot-jed",
|
3
|
+
"version": "0.0.11",
|
4
|
+
"private": false,
|
5
|
+
"publishConfig": {
|
6
|
+
"access": "public"
|
7
|
+
},
|
8
|
+
"description": "",
|
9
|
+
"license": "MIT",
|
10
|
+
"author": "hopntbl-jed",
|
11
|
+
"main": "lib/jed.js",
|
12
|
+
"repository": {
|
13
|
+
"type": "git",
|
14
|
+
"url": "https://SlexAxton@github.com/SlexAxton/Jed.git"
|
15
|
+
},
|
16
|
+
"scripts": {
|
17
|
+
"build": "npm run mkdir && node build.js",
|
18
|
+
"mkdir": "node build.js",
|
19
|
+
"prepublishOnly": "npm run build",
|
20
|
+
"test": "make test"
|
21
|
+
},
|
22
|
+
"dependencies" : {},
|
23
|
+
"devDependencies" : {
|
24
|
+
"mocha" : "*",
|
25
|
+
"expect.js" : "*",
|
26
|
+
"serve": "*"
|
27
|
+
}
|
28
|
+
}
|