| 1 |
/** |
| 2 |
* @preserve jed.js https://github.com/SlexAxton/Jed |
| 3 |
*/ |
| 4 |
/* |
| 5 |
----------- |
| 6 |
A gettext compatible i18n library for modern JavaScript Applications |
| 7 |
by Alex Sexton - AlexSexton [at] gmail - @SlexAxton |
| 8 |
MIT License |
| 9 |
A jQuery Foundation project - requires CLA to contribute - |
| 10 |
https://contribute.jquery.org/CLA/ |
| 11 |
Jed offers the entire applicable GNU gettext spec'd set of |
| 12 |
functions, but also offers some nicer wrappers around them. |
| 13 |
The api for gettext was written for a language with no function |
| 14 |
overloading, so Jed allows a little more of that. |
| 15 |
Many thanks to Joshua I. Miller - [email protected] - who wrote |
| 16 |
gettext.js back in 2008. I was able to vet a lot of my ideas |
| 17 |
against his. I also made sure Jed passed against his tests |
| 18 |
in order to offer easy upgrades -- jsgettext.berlios.de |
| 19 |
*/ |
| 20 |
(function (root, undef) { |
| 21 |
|
| 22 |
// Set up some underscore-style functions, if you already have |
| 23 |
// underscore, feel free to delete this section, and use it |
| 24 |
// directly, however, the amount of functions used doesn't |
| 25 |
// warrant having underscore as a full dependency. |
| 26 |
// Underscore 1.3.0 was used to port and is licensed |
| 27 |
// under the MIT License by Jeremy Ashkenas. |
| 28 |
var ArrayProto = Array.prototype, |
| 29 |
ObjProto = Object.prototype, |
| 30 |
slice = ArrayProto.slice, |
| 31 |
hasOwnProp = ObjProto.hasOwnProperty, |
| 32 |
nativeForEach = ArrayProto.forEach, |
| 33 |
breaker = {}; |
| 34 |
|
| 35 |
// We're not using the OOP style _ so we don't need the |
| 36 |
// extra level of indirection. This still means that you |
| 37 |
// sub out for real `_` though. |
| 38 |
var _ = { |
| 39 |
forEach : function( obj, iterator, context ) { |
| 40 |
var i, l, key; |
| 41 |
if ( obj === null ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if ( nativeForEach && obj.forEach === nativeForEach ) { |
| 46 |
obj.forEach( iterator, context ); |
| 47 |
} |
| 48 |
else if ( obj.length === +obj.length ) { |
| 49 |
for ( i = 0, l = obj.length; i < l; i++ ) { |
| 50 |
if ( i in obj && iterator.call( context, obj[i], i, obj ) === breaker ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
else { |
| 56 |
for ( key in obj) { |
| 57 |
if ( hasOwnProp.call( obj, key ) ) { |
| 58 |
if ( iterator.call (context, obj[key], key, obj ) === breaker ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
}, |
| 65 |
extend : function( obj ) { |
| 66 |
this.forEach( slice.call( arguments, 1 ), function ( source ) { |
| 67 |
for ( var prop in source ) { |
| 68 |
obj[prop] = source[prop]; |
| 69 |
} |
| 70 |
}); |
| 71 |
return obj; |
| 72 |
} |
| 73 |
}; |
| 74 |
// END Miniature underscore impl |
| 75 |
|
| 76 |
// Jed is a constructor function |
| 77 |
var Jed = function ( options ) { |
| 78 |
// Some minimal defaults |
| 79 |
this.defaults = { |
| 80 |
"locale_data" : { |
| 81 |
"messages" : { |
| 82 |
"" : { |
| 83 |
"domain" : "messages", |
| 84 |
"lang" : "en", |
| 85 |
"plural_forms" : "nplurals=2; plural=(n != 1);" |
| 86 |
} |
| 87 |
// There are no default keys, though |
| 88 |
} |
| 89 |
}, |
| 90 |
// The default domain if one is missing |
| 91 |
"domain" : "messages", |
| 92 |
// enable debug mode to log untranslated strings to the console |
| 93 |
"debug" : false |
| 94 |
}; |
| 95 |
|
| 96 |
// Mix in the sent options with the default options |
| 97 |
this.options = _.extend( {}, this.defaults, options ); |
| 98 |
this.textdomain( this.options.domain ); |
| 99 |
|
| 100 |
if ( options.domain && ! this.options.locale_data[ this.options.domain ] ) { |
| 101 |
throw new Error('Text domain set to non-existent domain: `' + options.domain + '`'); |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
// The gettext spec sets this character as the default |
| 106 |
// delimiter for context lookups. |
| 107 |
// e.g.: context\u0004key |
| 108 |
// If your translation company uses something different, |
| 109 |
// just change this at any time and it will use that instead. |
| 110 |
Jed.context_delimiter = String.fromCharCode( 4 ); |
| 111 |
|
| 112 |
function getPluralFormFunc ( plural_form_string ) { |
| 113 |
return Jed.PF.compile( plural_form_string || "nplurals=2; plural=(n != 1);"); |
| 114 |
} |
| 115 |
|
| 116 |
function Chain( key, i18n ){ |
| 117 |
this._key = key; |
| 118 |
this._i18n = i18n; |
| 119 |
} |
| 120 |
|
| 121 |
// Create a chainable api for adding args prettily |
| 122 |
_.extend( Chain.prototype, { |
| 123 |
onDomain : function ( domain ) { |
| 124 |
this._domain = domain; |
| 125 |
return this; |
| 126 |
}, |
| 127 |
withContext : function ( context ) { |
| 128 |
this._context = context; |
| 129 |
return this; |
| 130 |
}, |
| 131 |
ifPlural : function ( num, pkey ) { |
| 132 |
this._val = num; |
| 133 |
this._pkey = pkey; |
| 134 |
return this; |
| 135 |
}, |
| 136 |
fetch : function ( sArr ) { |
| 137 |
if ( {}.toString.call( sArr ) != '[object Array]' ) { |
| 138 |
sArr = [].slice.call(arguments, 0); |
| 139 |
} |
| 140 |
return ( sArr && sArr.length ? Jed.sprintf : function(x){ return x; } )( |
| 141 |
this._i18n.dcnpgettext(this._domain, this._context, this._key, this._pkey, this._val), |
| 142 |
sArr |
| 143 |
); |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
// Add functions to the Jed prototype. |
| 148 |
// These will be the functions on the object that's returned |
| 149 |
// from creating a `new Jed()` |
| 150 |
// These seem redundant, but they gzip pretty well. |
| 151 |
_.extend( Jed.prototype, { |
| 152 |
// The sexier api start point |
| 153 |
translate : function ( key ) { |
| 154 |
return new Chain( key, this ); |
| 155 |
}, |
| 156 |
|
| 157 |
textdomain : function ( domain ) { |
| 158 |
if ( ! domain ) { |
| 159 |
return this._textdomain; |
| 160 |
} |
| 161 |
this._textdomain = domain; |
| 162 |
}, |
| 163 |
|
| 164 |
gettext : function ( key ) { |
| 165 |
return this.dcnpgettext.call( this, undef, undef, key ); |
| 166 |
}, |
| 167 |
|
| 168 |
dgettext : function ( domain, key ) { |
| 169 |
return this.dcnpgettext.call( this, domain, undef, key ); |
| 170 |
}, |
| 171 |
|
| 172 |
dcgettext : function ( domain , key /*, category */ ) { |
| 173 |
// Ignores the category anyways |
| 174 |
return this.dcnpgettext.call( this, domain, undef, key ); |
| 175 |
}, |
| 176 |
|
| 177 |
ngettext : function ( skey, pkey, val ) { |
| 178 |
return this.dcnpgettext.call( this, undef, undef, skey, pkey, val ); |
| 179 |
}, |
| 180 |
|
| 181 |
dngettext : function ( domain, skey, pkey, val ) { |
| 182 |
return this.dcnpgettext.call( this, domain, undef, skey, pkey, val ); |
| 183 |
}, |
| 184 |
|
| 185 |
dcngettext : function ( domain, skey, pkey, val/*, category */) { |
| 186 |
return this.dcnpgettext.call( this, domain, undef, skey, pkey, val ); |
| 187 |
}, |
| 188 |
|
| 189 |
pgettext : function ( context, key ) { |
| 190 |
return this.dcnpgettext.call( this, undef, context, key ); |
| 191 |
}, |
| 192 |
|
| 193 |
dpgettext : function ( domain, context, key ) { |
| 194 |
return this.dcnpgettext.call( this, domain, context, key ); |
| 195 |
}, |
| 196 |
|
| 197 |
dcpgettext : function ( domain, context, key/*, category */) { |
| 198 |
return this.dcnpgettext.call( this, domain, context, key ); |
| 199 |
}, |
| 200 |
|
| 201 |
npgettext : function ( context, skey, pkey, val ) { |
| 202 |
return this.dcnpgettext.call( this, undef, context, skey, pkey, val ); |
| 203 |
}, |
| 204 |
|
| 205 |
dnpgettext : function ( domain, context, skey, pkey, val ) { |
| 206 |
return this.dcnpgettext.call( this, domain, context, skey, pkey, val ); |
| 207 |
}, |
| 208 |
|
| 209 |
// The most fully qualified gettext function. It has every option. |
| 210 |
// Since it has every option, we can use it from every other method. |
| 211 |
// This is the bread and butter. |
| 212 |
// Technically there should be one more argument in this function for 'Category', |
| 213 |
// but since we never use it, we might as well not waste the bytes to define it. |
| 214 |
dcnpgettext : function ( domain, context, singular_key, plural_key, val ) { |
| 215 |
// Set some defaults |
| 216 |
|
| 217 |
plural_key = plural_key || singular_key; |
| 218 |
|
| 219 |
// Use the global domain default if one |
| 220 |
// isn't explicitly passed in |
| 221 |
domain = domain || this._textdomain; |
| 222 |
|
| 223 |
var fallback; |
| 224 |
|
| 225 |
// Handle special cases |
| 226 |
|
| 227 |
// No options found |
| 228 |
if ( ! this.options ) { |
| 229 |
// There's likely something wrong, but we'll return the correct key for english |
| 230 |
// We do this by instantiating a brand new Jed instance with the default set |
| 231 |
// for everything that could be broken. |
| 232 |
fallback = new Jed(); |
| 233 |
return fallback.dcnpgettext.call( fallback, undefined, undefined, singular_key, plural_key, val ); |
| 234 |
} |
| 235 |
|
| 236 |
// No translation data provided |
| 237 |
if ( ! this.options.locale_data ) { |
| 238 |
throw new Error('No locale data provided.'); |
| 239 |
} |
| 240 |
|
| 241 |
if ( ! this.options.locale_data[ domain ] ) { |
| 242 |
throw new Error('Domain `' + domain + '` was not found.'); |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! this.options.locale_data[ domain ][ "" ] ) { |
| 246 |
throw new Error('No locale meta information provided.'); |
| 247 |
} |
| 248 |
|
| 249 |
// Make sure we have a truthy key. Otherwise we might start looking |
| 250 |
// into the empty string key, which is the options for the locale |
| 251 |
// data. |
| 252 |
if ( ! singular_key ) { |
| 253 |
throw new Error('No translation key found.'); |
| 254 |
} |
| 255 |
|
| 256 |
var key = context ? context + Jed.context_delimiter + singular_key : singular_key, |
| 257 |
locale_data = this.options.locale_data, |
| 258 |
dict = locale_data[ domain ], |
| 259 |
defaultConf = (locale_data.messages || this.defaults.locale_data.messages)[""], |
| 260 |
pluralForms = dict[""].plural_forms || dict[""]["Plural-Forms"] || dict[""]["plural-forms"] || defaultConf.plural_forms || defaultConf["Plural-Forms"] || defaultConf["plural-forms"], |
| 261 |
val_list, |
| 262 |
res; |
| 263 |
|
| 264 |
var val_idx; |
| 265 |
if (val === undefined) { |
| 266 |
// No value passed in; assume singular key lookup. |
| 267 |
val_idx = 0; |
| 268 |
|
| 269 |
} else { |
| 270 |
// Value has been passed in; use plural-forms calculations. |
| 271 |
|
| 272 |
// Handle invalid numbers, but try casting strings for good measure |
| 273 |
if ( typeof val != 'number' ) { |
| 274 |
val = parseInt( val, 10 ); |
| 275 |
|
| 276 |
if ( isNaN( val ) ) { |
| 277 |
throw new Error('The number that was passed in is not a number.'); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
val_idx = getPluralFormFunc(pluralForms)(val); |
| 282 |
} |
| 283 |
|
| 284 |
// Throw an error if a domain isn't found |
| 285 |
if ( ! dict ) { |
| 286 |
throw new Error('No domain named `' + domain + '` could be found.'); |
| 287 |
} |
| 288 |
|
| 289 |
val_list = dict[ key ]; |
| 290 |
|
| 291 |
// If there is no match, then revert back to |
| 292 |
// english style singular/plural with the keys passed in. |
| 293 |
if ( ! val_list || val_idx > val_list.length ) { |
| 294 |
if (this.options.missing_key_callback) { |
| 295 |
this.options.missing_key_callback(key, domain); |
| 296 |
} |
| 297 |
res = [ singular_key, plural_key ]; |
| 298 |
|
| 299 |
// collect untranslated strings |
| 300 |
if (this.options.debug===true) { |
| 301 |
console.log(res[ getPluralFormFunc(pluralForms)( val ) ]); |
| 302 |
} |
| 303 |
return res[ getPluralFormFunc()( val ) ]; |
| 304 |
} |
| 305 |
|
| 306 |
res = val_list[ val_idx ]; |
| 307 |
|
| 308 |
// This includes empty strings on purpose |
| 309 |
if ( ! res ) { |
| 310 |
res = [ singular_key, plural_key ]; |
| 311 |
return res[ getPluralFormFunc()( val ) ]; |
| 312 |
} |
| 313 |
return res; |
| 314 |
} |
| 315 |
}); |
| 316 |
|
| 317 |
|
| 318 |
// We add in sprintf capabilities for post translation value interolation |
| 319 |
// This is not internally used, so you can remove it if you have this |
| 320 |
// available somewhere else, or want to use a different system. |
| 321 |
|
| 322 |
// We _slightly_ modify the normal sprintf behavior to more gracefully handle |
| 323 |
// undefined values. |
| 324 |
|
| 325 |
/** |
| 326 |
sprintf() for JavaScript 0.7-beta1 |
| 327 |
http://www.diveintojavascript.com/projects/javascript-sprintf |
| 328 |
Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com> |
| 329 |
All rights reserved. |
| 330 |
Redistribution and use in source and binary forms, with or without |
| 331 |
modification, are permitted provided that the following conditions are met: |
| 332 |
* Redistributions of source code must retain the above copyright |
| 333 |
notice, this list of conditions and the following disclaimer. |
| 334 |
* Redistributions in binary form must reproduce the above copyright |
| 335 |
notice, this list of conditions and the following disclaimer in the |
| 336 |
documentation and/or other materials provided with the distribution. |
| 337 |
* Neither the name of sprintf() for JavaScript nor the |
| 338 |
names of its contributors may be used to endorse or promote products |
| 339 |
derived from this software without specific prior written permission. |
| 340 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 341 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 342 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 343 |
DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY |
| 344 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 345 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 346 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 347 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 348 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 349 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 350 |
*/ |
| 351 |
var sprintf = (function() { |
| 352 |
function get_type(variable) { |
| 353 |
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase(); |
| 354 |
} |
| 355 |
function str_repeat(input, multiplier) { |
| 356 |
for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */} |
| 357 |
return output.join(''); |
| 358 |
} |
| 359 |
|
| 360 |
var str_format = function() { |
| 361 |
if (!str_format.cache.hasOwnProperty(arguments[0])) { |
| 362 |
str_format.cache[arguments[0]] = str_format.parse(arguments[0]); |
| 363 |
} |
| 364 |
return str_format.format.call(null, str_format.cache[arguments[0]], arguments); |
| 365 |
}; |
| 366 |
|
| 367 |
str_format.format = function(parse_tree, argv) { |
| 368 |
var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length; |
| 369 |
for (i = 0; i < tree_length; i++) { |
| 370 |
node_type = get_type(parse_tree[i]); |
| 371 |
if (node_type === 'string') { |
| 372 |
output.push(parse_tree[i]); |
| 373 |
} |
| 374 |
else if (node_type === 'array') { |
| 375 |
match = parse_tree[i]; // convenience purposes only |
| 376 |
if (match[2]) { // keyword argument |
| 377 |
arg = argv[cursor]; |
| 378 |
for (k = 0; k < match[2].length; k++) { |
| 379 |
if (!arg.hasOwnProperty(match[2][k])) { |
| 380 |
throw(sprintf('[sprintf] property "%s" does not exist', match[2][k])); |
| 381 |
} |
| 382 |
arg = arg[match[2][k]]; |
| 383 |
} |
| 384 |
} |
| 385 |
else if (match[1]) { // positional argument (explicit) |
| 386 |
arg = argv[match[1]]; |
| 387 |
} |
| 388 |
else { // positional argument (implicit) |
| 389 |
arg = argv[cursor++]; |
| 390 |
} |
| 391 |
|
| 392 |
if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) { |
| 393 |
throw(sprintf('[sprintf] expecting number but found %s', get_type(arg))); |
| 394 |
} |
| 395 |
|
| 396 |
// Jed EDIT |
| 397 |
if ( typeof arg == 'undefined' || arg === null ) { |
| 398 |
arg = ''; |
| 399 |
} |
| 400 |
// Jed EDIT |
| 401 |
|
| 402 |
switch (match[8]) { |
| 403 |
case 'b': arg = arg.toString(2); break; |
| 404 |
case 'c': arg = String.fromCharCode(arg); break; |
| 405 |
case 'd': arg = parseInt(arg, 10); break; |
| 406 |
case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break; |
| 407 |
case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break; |
| 408 |
case 'o': arg = arg.toString(8); break; |
| 409 |
case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break; |
| 410 |
case 'u': arg = Math.abs(arg); break; |
| 411 |
case 'x': arg = arg.toString(16); break; |
| 412 |
case 'X': arg = arg.toString(16).toUpperCase(); break; |
| 413 |
} |
| 414 |
arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg); |
| 415 |
pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' '; |
| 416 |
pad_length = match[6] - String(arg).length; |
| 417 |
pad = match[6] ? str_repeat(pad_character, pad_length) : ''; |
| 418 |
output.push(match[5] ? arg + pad : pad + arg); |
| 419 |
} |
| 420 |
} |
| 421 |
return output.join(''); |
| 422 |
}; |
| 423 |
|
| 424 |
str_format.cache = {}; |
| 425 |
|
| 426 |
str_format.parse = function(fmt) { |
| 427 |
var _fmt = fmt, match = [], parse_tree = [], arg_names = 0; |
| 428 |
while (_fmt) { |
| 429 |
if ((match = /^[^\x25]+/.exec(_fmt)) !== null) { |
| 430 |
parse_tree.push(match[0]); |
| 431 |
} |
| 432 |
else if ((match = /^\x25{2}/.exec(_fmt)) !== null) { |
| 433 |
parse_tree.push('%'); |
| 434 |
} |
| 435 |
else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) { |
| 436 |
if (match[2]) { |
| 437 |
arg_names |= 1; |
| 438 |
var field_list = [], replacement_field = match[2], field_match = []; |
| 439 |
if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { |
| 440 |
field_list.push(field_match[1]); |
| 441 |
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { |
| 442 |
if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { |
| 443 |
field_list.push(field_match[1]); |
| 444 |
} |
| 445 |
else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) { |
| 446 |
field_list.push(field_match[1]); |
| 447 |
} |
| 448 |
else { |
| 449 |
throw('[sprintf] huh?'); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
else { |
| 454 |
throw('[sprintf] huh?'); |
| 455 |
} |
| 456 |
match[2] = field_list; |
| 457 |
} |
| 458 |
else { |
| 459 |
arg_names |= 2; |
| 460 |
} |
| 461 |
if (arg_names === 3) { |
| 462 |
throw('[sprintf] mixing positional and named placeholders is not (yet) supported'); |
| 463 |
} |
| 464 |
parse_tree.push(match); |
| 465 |
} |
| 466 |
else { |
| 467 |
throw('[sprintf] huh?'); |
| 468 |
} |
| 469 |
_fmt = _fmt.substring(match[0].length); |
| 470 |
} |
| 471 |
return parse_tree; |
| 472 |
}; |
| 473 |
|
| 474 |
return str_format; |
| 475 |
})(); |
| 476 |
|
| 477 |
var vsprintf = function(fmt, argv) { |
| 478 |
argv.unshift(fmt); |
| 479 |
return sprintf.apply(null, argv); |
| 480 |
}; |
| 481 |
|
| 482 |
Jed.parse_plural = function ( plural_forms, n ) { |
| 483 |
plural_forms = plural_forms.replace(/n/g, n); |
| 484 |
return Jed.parse_expression(plural_forms); |
| 485 |
}; |
| 486 |
|
| 487 |
Jed.sprintf = function ( fmt, args ) { |
| 488 |
if ( {}.toString.call( args ) == '[object Array]' ) { |
| 489 |
return vsprintf( fmt, [].slice.call(args) ); |
| 490 |
} |
| 491 |
return sprintf.apply(this, [].slice.call(arguments) ); |
| 492 |
}; |
| 493 |
|
| 494 |
Jed.prototype.sprintf = function () { |
| 495 |
return Jed.sprintf.apply(this, arguments); |
| 496 |
}; |
| 497 |
// END sprintf Implementation |
| 498 |
|
| 499 |
// Start the Plural forms section |
| 500 |
// This is a full plural form expression parser. It is used to avoid |
| 501 |
// running 'eval' or 'new Function' directly against the plural |
| 502 |
// forms. |
| 503 |
// |
| 504 |
// This can be important if you get translations done through a 3rd |
| 505 |
// party vendor. I encourage you to use this instead, however, I |
| 506 |
// also will provide a 'precompiler' that you can use at build time |
| 507 |
// to output valid/safe function representations of the plural form |
| 508 |
// expressions. This means you can build this code out for the most |
| 509 |
// part. |
| 510 |
Jed.PF = {}; |
| 511 |
|
| 512 |
Jed.PF.parse = function ( p ) { |
| 513 |
var plural_str = Jed.PF.extractPluralExpr( p ); |
| 514 |
return Jed.PF.parser.parse.call(Jed.PF.parser, plural_str); |
| 515 |
}; |
| 516 |
|
| 517 |
Jed.PF.compile = function ( p ) { |
| 518 |
// Handle trues and falses as 0 and 1 |
| 519 |
function imply( val ) { |
| 520 |
return (val === true ? 1 : val ? val : 0); |
| 521 |
} |
| 522 |
|
| 523 |
var ast = Jed.PF.parse( p ); |
| 524 |
return function ( n ) { |
| 525 |
return imply( Jed.PF.interpreter( ast )( n ) ); |
| 526 |
}; |
| 527 |
}; |
| 528 |
|
| 529 |
Jed.PF.interpreter = function ( ast ) { |
| 530 |
return function ( n ) { |
| 531 |
var res; |
| 532 |
switch ( ast.type ) { |
| 533 |
case 'GROUP': |
| 534 |
return Jed.PF.interpreter( ast.expr )( n ); |
| 535 |
case 'TERNARY': |
| 536 |
if ( Jed.PF.interpreter( ast.expr )( n ) ) { |
| 537 |
return Jed.PF.interpreter( ast.truthy )( n ); |
| 538 |
} |
| 539 |
return Jed.PF.interpreter( ast.falsey )( n ); |
| 540 |
case 'OR': |
| 541 |
return Jed.PF.interpreter( ast.left )( n ) || Jed.PF.interpreter( ast.right )( n ); |
| 542 |
case 'AND': |
| 543 |
return Jed.PF.interpreter( ast.left )( n ) && Jed.PF.interpreter( ast.right )( n ); |
| 544 |
case 'LT': |
| 545 |
return Jed.PF.interpreter( ast.left )( n ) < Jed.PF.interpreter( ast.right )( n ); |
| 546 |
case 'GT': |
| 547 |
return Jed.PF.interpreter( ast.left )( n ) > Jed.PF.interpreter( ast.right )( n ); |
| 548 |
case 'LTE': |
| 549 |
return Jed.PF.interpreter( ast.left )( n ) <= Jed.PF.interpreter( ast.right )( n ); |
| 550 |
case 'GTE': |
| 551 |
return Jed.PF.interpreter( ast.left )( n ) >= Jed.PF.interpreter( ast.right )( n ); |
| 552 |
case 'EQ': |
| 553 |
return Jed.PF.interpreter( ast.left )( n ) == Jed.PF.interpreter( ast.right )( n ); |
| 554 |
case 'NEQ': |
| 555 |
return Jed.PF.interpreter( ast.left )( n ) != Jed.PF.interpreter( ast.right )( n ); |
| 556 |
case 'MOD': |
| 557 |
return Jed.PF.interpreter( ast.left )( n ) % Jed.PF.interpreter( ast.right )( n ); |
| 558 |
case 'VAR': |
| 559 |
return n; |
| 560 |
case 'NUM': |
| 561 |
return ast.val; |
| 562 |
default: |
| 563 |
throw new Error("Invalid Token found."); |
| 564 |
} |
| 565 |
}; |
| 566 |
}; |
| 567 |
|
| 568 |
Jed.PF.regexps = { |
| 569 |
TRIM_BEG: /^\s\s*/, |
| 570 |
TRIM_END: /\s\s*$/, |
| 571 |
HAS_SEMICOLON: /;\s*$/, |
| 572 |
NPLURALS: /nplurals\=(\d+);/, |
| 573 |
PLURAL: /plural\=(.*);/ |
| 574 |
}; |
| 575 |
|
| 576 |
Jed.PF.extractPluralExpr = function ( p ) { |
| 577 |
// trim first |
| 578 |
p = p.replace(Jed.PF.regexps.TRIM_BEG, '').replace(Jed.PF.regexps.TRIM_END, ''); |
| 579 |
|
| 580 |
if (! Jed.PF.regexps.HAS_SEMICOLON.test(p)) { |
| 581 |
p = p.concat(';'); |
| 582 |
} |
| 583 |
|
| 584 |
var nplurals_matches = p.match( Jed.PF.regexps.NPLURALS ), |
| 585 |
res = {}, |
| 586 |
plural_matches; |
| 587 |
|
| 588 |
// Find the nplurals number |
| 589 |
if ( nplurals_matches.length > 1 ) { |
| 590 |
res.nplurals = nplurals_matches[1]; |
| 591 |
} |
| 592 |
else { |
| 593 |
throw new Error('nplurals not found in plural_forms string: ' + p ); |
| 594 |
} |
| 595 |
|
| 596 |
// remove that data to get to the formula |
| 597 |
p = p.replace( Jed.PF.regexps.NPLURALS, "" ); |
| 598 |
plural_matches = p.match( Jed.PF.regexps.PLURAL ); |
| 599 |
|
| 600 |
if (!( plural_matches && plural_matches.length > 1 ) ) { |
| 601 |
throw new Error('`plural` expression not found: ' + p); |
| 602 |
} |
| 603 |
return plural_matches[ 1 ]; |
| 604 |
}; |
| 605 |
|
| 606 |
/* Jison generated parser */ |
| 607 |
Jed.PF.parser = (function(){ |
| 608 |
|
| 609 |
var parser = {trace: function trace() { }, |
| 610 |
yy: {}, |
| 611 |
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}, |
| 612 |
terminals_: {2:"error",5:"EOF",6:"?",7:":",8:"||",9:"&&",10:"<",11:"<=",12:">",13:">=",14:"!=",15:"==",16:"%",17:"(",18:")",19:"n",20:"NUMBER"}, |
| 613 |
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]], |
| 614 |
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) { |
| 615 |
|
| 616 |
var $0 = $$.length - 1; |
| 617 |
switch (yystate) { |
| 618 |
case 1: return { type : 'GROUP', expr: $$[$0-1] }; |
| 619 |
break; |
| 620 |
case 2:this.$ = { type: 'TERNARY', expr: $$[$0-4], truthy : $$[$0-2], falsey: $$[$0] }; |
| 621 |
break; |
| 622 |
case 3:this.$ = { type: "OR", left: $$[$0-2], right: $$[$0] }; |
| 623 |
break; |
| 624 |
case 4:this.$ = { type: "AND", left: $$[$0-2], right: $$[$0] }; |
| 625 |
break; |
| 626 |
case 5:this.$ = { type: 'LT', left: $$[$0-2], right: $$[$0] }; |
| 627 |
break; |
| 628 |
case 6:this.$ = { type: 'LTE', left: $$[$0-2], right: $$[$0] }; |
| 629 |
break; |
| 630 |
case 7:this.$ = { type: 'GT', left: $$[$0-2], right: $$[$0] }; |
| 631 |
break; |
| 632 |
case 8:this.$ = { type: 'GTE', left: $$[$0-2], right: $$[$0] }; |
| 633 |
break; |
| 634 |
case 9:this.$ = { type: 'NEQ', left: $$[$0-2], right: $$[$0] }; |
| 635 |
break; |
| 636 |
case 10:this.$ = { type: 'EQ', left: $$[$0-2], right: $$[$0] }; |
| 637 |
break; |
| 638 |
case 11:this.$ = { type: 'MOD', left: $$[$0-2], right: $$[$0] }; |
| 639 |
break; |
| 640 |
case 12:this.$ = { type: 'GROUP', expr: $$[$0-1] }; |
| 641 |
break; |
| 642 |
case 13:this.$ = { type: 'VAR' }; |
| 643 |
break; |
| 644 |
case 14:this.$ = { type: 'NUM', val: Number(yytext) }; |
| 645 |
break; |
| 646 |
} |
| 647 |
}, |
| 648 |
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]}], |
| 649 |
defaultActions: {6:[2,1]}, |
| 650 |
parseError: function parseError(str, hash) { |
| 651 |
throw new Error(str); |
| 652 |
}, |
| 653 |
parse: function parse(input) { |
| 654 |
var self = this, |
| 655 |
stack = [0], |
| 656 |
vstack = [null], // semantic value stack |
| 657 |
lstack = [], // location stack |
| 658 |
table = this.table, |
| 659 |
yytext = '', |
| 660 |
yylineno = 0, |
| 661 |
yyleng = 0, |
| 662 |
recovering = 0, |
| 663 |
TERROR = 2, |
| 664 |
EOF = 1; |
| 665 |
|
| 666 |
//this.reductionCount = this.shiftCount = 0; |
| 667 |
|
| 668 |
this.lexer.setInput(input); |
| 669 |
this.lexer.yy = this.yy; |
| 670 |
this.yy.lexer = this.lexer; |
| 671 |
if (typeof this.lexer.yylloc == 'undefined') |
| 672 |
this.lexer.yylloc = {}; |
| 673 |
var yyloc = this.lexer.yylloc; |
| 674 |
lstack.push(yyloc); |
| 675 |
|
| 676 |
if (typeof this.yy.parseError === 'function') |
| 677 |
this.parseError = this.yy.parseError; |
| 678 |
|
| 679 |
function popStack (n) { |
| 680 |
stack.length = stack.length - 2*n; |
| 681 |
vstack.length = vstack.length - n; |
| 682 |
lstack.length = lstack.length - n; |
| 683 |
} |
| 684 |
|
| 685 |
function lex() { |
| 686 |
var token; |
| 687 |
token = self.lexer.lex() || 1; // $end = 1 |
| 688 |
// if token isn't its numeric value, convert |
| 689 |
if (typeof token !== 'number') { |
| 690 |
token = self.symbols_[token] || token; |
| 691 |
} |
| 692 |
return token; |
| 693 |
} |
| 694 |
|
| 695 |
var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected; |
| 696 |
while (true) { |
| 697 |
// retreive state number from top of stack |
| 698 |
state = stack[stack.length-1]; |
| 699 |
|
| 700 |
// use default actions if available |
| 701 |
if (this.defaultActions[state]) { |
| 702 |
action = this.defaultActions[state]; |
| 703 |
} else { |
| 704 |
if (symbol == null) |
| 705 |
symbol = lex(); |
| 706 |
// read action for current state and first input |
| 707 |
action = table[state] && table[state][symbol]; |
| 708 |
} |
| 709 |
|
| 710 |
// handle parse error |
| 711 |
_handle_error: |
| 712 |
if (typeof action === 'undefined' || !action.length || !action[0]) { |
| 713 |
|
| 714 |
if (!recovering) { |
| 715 |
// Report error |
| 716 |
expected = []; |
| 717 |
for (p in table[state]) if (this.terminals_[p] && p > 2) { |
| 718 |
expected.push("'"+this.terminals_[p]+"'"); |
| 719 |
} |
| 720 |
var errStr = ''; |
| 721 |
if (this.lexer.showPosition) { |
| 722 |
errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + this.terminals_[symbol]+ "'"; |
| 723 |
} else { |
| 724 |
errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " + |
| 725 |
(symbol == 1 /*EOF*/ ? "end of input" : |
| 726 |
("'"+(this.terminals_[symbol] || symbol)+"'")); |
| 727 |
} |
| 728 |
this.parseError(errStr, |
| 729 |
{text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected}); |
| 730 |
} |
| 731 |
|
| 732 |
// just recovered from another error |
| 733 |
if (recovering == 3) { |
| 734 |
if (symbol == EOF) { |
| 735 |
throw new Error(errStr || 'Parsing halted.'); |
| 736 |
} |
| 737 |
|
| 738 |
// discard current lookahead and grab another |
| 739 |
yyleng = this.lexer.yyleng; |
| 740 |
yytext = this.lexer.yytext; |
| 741 |
yylineno = this.lexer.yylineno; |
| 742 |
yyloc = this.lexer.yylloc; |
| 743 |
symbol = lex(); |
| 744 |
} |
| 745 |
|
| 746 |
// try to recover from error |
| 747 |
while (1) { |
| 748 |
// check for error recovery rule in this state |
| 749 |
if ((TERROR.toString()) in table[state]) { |
| 750 |
break; |
| 751 |
} |
| 752 |
if (state == 0) { |
| 753 |
throw new Error(errStr || 'Parsing halted.'); |
| 754 |
} |
| 755 |
popStack(1); |
| 756 |
state = stack[stack.length-1]; |
| 757 |
} |
| 758 |
|
| 759 |
preErrorSymbol = symbol; // save the lookahead token |
| 760 |
symbol = TERROR; // insert generic error symbol as new lookahead |
| 761 |
state = stack[stack.length-1]; |
| 762 |
action = table[state] && table[state][TERROR]; |
| 763 |
recovering = 3; // allow 3 real symbols to be shifted before reporting a new error |
| 764 |
} |
| 765 |
|
| 766 |
// this shouldn't happen, unless resolve defaults are off |
| 767 |
if (action[0] instanceof Array && action.length > 1) { |
| 768 |
throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol); |
| 769 |
} |
| 770 |
|
| 771 |
switch (action[0]) { |
| 772 |
|
| 773 |
case 1: // shift |
| 774 |
//this.shiftCount++; |
| 775 |
|
| 776 |
stack.push(symbol); |
| 777 |
vstack.push(this.lexer.yytext); |
| 778 |
lstack.push(this.lexer.yylloc); |
| 779 |
stack.push(action[1]); // push state |
| 780 |
symbol = null; |
| 781 |
if (!preErrorSymbol) { // normal execution/no error |
| 782 |
yyleng = this.lexer.yyleng; |
| 783 |
yytext = this.lexer.yytext; |
| 784 |
yylineno = this.lexer.yylineno; |
| 785 |
yyloc = this.lexer.yylloc; |
| 786 |
if (recovering > 0) |
| 787 |
recovering--; |
| 788 |
} else { // error just occurred, resume old lookahead f/ before error |
| 789 |
symbol = preErrorSymbol; |
| 790 |
preErrorSymbol = null; |
| 791 |
} |
| 792 |
break; |
| 793 |
|
| 794 |
case 2: // reduce |
| 795 |
//this.reductionCount++; |
| 796 |
|
| 797 |
len = this.productions_[action[1]][1]; |
| 798 |
|
| 799 |
// perform semantic action |
| 800 |
yyval.$ = vstack[vstack.length-len]; // default to $$ = $1 |
| 801 |
// default location, uses first token for firsts, last for lasts |
| 802 |
yyval._$ = { |
| 803 |
first_line: lstack[lstack.length-(len||1)].first_line, |
| 804 |
last_line: lstack[lstack.length-1].last_line, |
| 805 |
first_column: lstack[lstack.length-(len||1)].first_column, |
| 806 |
last_column: lstack[lstack.length-1].last_column |
| 807 |
}; |
| 808 |
r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack); |
| 809 |
|
| 810 |
if (typeof r !== 'undefined') { |
| 811 |
return r; |
| 812 |
} |
| 813 |
|
| 814 |
// pop off stack |
| 815 |
if (len) { |
| 816 |
stack = stack.slice(0,-1*len*2); |
| 817 |
vstack = vstack.slice(0, -1*len); |
| 818 |
lstack = lstack.slice(0, -1*len); |
| 819 |
} |
| 820 |
|
| 821 |
stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce) |
| 822 |
vstack.push(yyval.$); |
| 823 |
lstack.push(yyval._$); |
| 824 |
// goto new state = table[STATE][NONTERMINAL] |
| 825 |
newState = table[stack[stack.length-2]][stack[stack.length-1]]; |
| 826 |
stack.push(newState); |
| 827 |
break; |
| 828 |
|
| 829 |
case 3: // accept |
| 830 |
return true; |
| 831 |
} |
| 832 |
|
| 833 |
} |
| 834 |
|
| 835 |
return true; |
| 836 |
}};/* Jison generated lexer */ |
| 837 |
var lexer = (function(){ |
| 838 |
|
| 839 |
var lexer = ({EOF:1, |
| 840 |
parseError:function parseError(str, hash) { |
| 841 |
if (this.yy.parseError) { |
| 842 |
this.yy.parseError(str, hash); |
| 843 |
} else { |
| 844 |
throw new Error(str); |
| 845 |
} |
| 846 |
}, |
| 847 |
setInput:function (input) { |
| 848 |
this._input = input; |
| 849 |
this._more = this._less = this.done = false; |
| 850 |
this.yylineno = this.yyleng = 0; |
| 851 |
this.yytext = this.matched = this.match = ''; |
| 852 |
this.conditionStack = ['INITIAL']; |
| 853 |
this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0}; |
| 854 |
return this; |
| 855 |
}, |
| 856 |
input:function () { |
| 857 |
var ch = this._input[0]; |
| 858 |
this.yytext+=ch; |
| 859 |
this.yyleng++; |
| 860 |
this.match+=ch; |
| 861 |
this.matched+=ch; |
| 862 |
var lines = ch.match(/\n/); |
| 863 |
if (lines) this.yylineno++; |
| 864 |
this._input = this._input.slice(1); |
| 865 |
return ch; |
| 866 |
}, |
| 867 |
unput:function (ch) { |
| 868 |
this._input = ch + this._input; |
| 869 |
return this; |
| 870 |
}, |
| 871 |
more:function () { |
| 872 |
this._more = true; |
| 873 |
return this; |
| 874 |
}, |
| 875 |
pastInput:function () { |
| 876 |
var past = this.matched.substr(0, this.matched.length - this.match.length); |
| 877 |
return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, ""); |
| 878 |
}, |
| 879 |
upcomingInput:function () { |
| 880 |
var next = this.match; |
| 881 |
if (next.length < 20) { |
| 882 |
next += this._input.substr(0, 20-next.length); |
| 883 |
} |
| 884 |
return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, ""); |
| 885 |
}, |
| 886 |
showPosition:function () { |
| 887 |
var pre = this.pastInput(); |
| 888 |
var c = new Array(pre.length + 1).join("-"); |
| 889 |
return pre + this.upcomingInput() + "\n" + c+"^"; |
| 890 |
}, |
| 891 |
next:function () { |
| 892 |
if (this.done) { |
| 893 |
return this.EOF; |
| 894 |
} |
| 895 |
if (!this._input) this.done = true; |
| 896 |
|
| 897 |
var token, |
| 898 |
match, |
| 899 |
col, |
| 900 |
lines; |
| 901 |
if (!this._more) { |
| 902 |
this.yytext = ''; |
| 903 |
this.match = ''; |
| 904 |
} |
| 905 |
var rules = this._currentRules(); |
| 906 |
for (var i=0;i < rules.length; i++) { |
| 907 |
match = this._input.match(this.rules[rules[i]]); |
| 908 |
if (match) { |
| 909 |
lines = match[0].match(/\n.*/g); |
| 910 |
if (lines) this.yylineno += lines.length; |
| 911 |
this.yylloc = {first_line: this.yylloc.last_line, |
| 912 |
last_line: this.yylineno+1, |
| 913 |
first_column: this.yylloc.last_column, |
| 914 |
last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length} |
| 915 |
this.yytext += match[0]; |
| 916 |
this.match += match[0]; |
| 917 |
this.matches = match; |
| 918 |
this.yyleng = this.yytext.length; |
| 919 |
this._more = false; |
| 920 |
this._input = this._input.slice(match[0].length); |
| 921 |
this.matched += match[0]; |
| 922 |
token = this.performAction.call(this, this.yy, this, rules[i],this.conditionStack[this.conditionStack.length-1]); |
| 923 |
if (token) return token; |
| 924 |
else return; |
| 925 |
} |
| 926 |
} |
| 927 |
if (this._input === "") { |
| 928 |
return this.EOF; |
| 929 |
} else { |
| 930 |
this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(), |
| 931 |
{text: "", token: null, line: this.yylineno}); |
| 932 |
} |
| 933 |
}, |
| 934 |
lex:function lex() { |
| 935 |
var r = this.next(); |
| 936 |
if (typeof r !== 'undefined') { |
| 937 |
return r; |
| 938 |
} else { |
| 939 |
return this.lex(); |
| 940 |
} |
| 941 |
}, |
| 942 |
begin:function begin(condition) { |
| 943 |
this.conditionStack.push(condition); |
| 944 |
}, |
| 945 |
popState:function popState() { |
| 946 |
return this.conditionStack.pop(); |
| 947 |
}, |
| 948 |
_currentRules:function _currentRules() { |
| 949 |
return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules; |
| 950 |
}, |
| 951 |
topState:function () { |
| 952 |
return this.conditionStack[this.conditionStack.length-2]; |
| 953 |
}, |
| 954 |
pushState:function begin(condition) { |
| 955 |
this.begin(condition); |
| 956 |
}}); |
| 957 |
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { |
| 958 |
|
| 959 |
var YYSTATE=YY_START; |
| 960 |
switch($avoiding_name_collisions) { |
| 961 |
case 0:/* skip whitespace */ |
| 962 |
break; |
| 963 |
case 1:return 20 |
| 964 |
break; |
| 965 |
case 2:return 19 |
| 966 |
break; |
| 967 |
case 3:return 8 |
| 968 |
break; |
| 969 |
case 4:return 9 |
| 970 |
break; |
| 971 |
case 5:return 6 |
| 972 |
break; |
| 973 |
case 6:return 7 |
| 974 |
break; |
| 975 |
case 7:return 11 |
| 976 |
break; |
| 977 |
case 8:return 13 |
| 978 |
break; |
| 979 |
case 9:return 10 |
| 980 |
break; |
| 981 |
case 10:return 12 |
| 982 |
break; |
| 983 |
case 11:return 14 |
| 984 |
break; |
| 985 |
case 12:return 15 |
| 986 |
break; |
| 987 |
case 13:return 16 |
| 988 |
break; |
| 989 |
case 14:return 17 |
| 990 |
break; |
| 991 |
case 15:return 18 |
| 992 |
break; |
| 993 |
case 16:return 5 |
| 994 |
break; |
| 995 |
case 17:return 'INVALID' |
| 996 |
break; |
| 997 |
} |
| 998 |
}; |
| 999 |
lexer.rules = [/^\s+/,/^[0-9]+(\.[0-9]+)?\b/,/^n\b/,/^\|\|/,/^&&/,/^\?/,/^:/,/^<=/,/^>=/,/^</,/^>/,/^!=/,/^==/,/^%/,/^\(/,/^\)/,/^$/,/^./]; |
| 1000 |
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;})() |
| 1001 |
parser.lexer = lexer; |
| 1002 |
return parser; |
| 1003 |
})(); |
| 1004 |
// End parser |
| 1005 |
|
| 1006 |
// Handle node, amd, and global systems |
| 1007 |
if (typeof exports !== 'undefined') { |
| 1008 |
if (typeof module !== 'undefined' && module.exports) { |
| 1009 |
exports = module.exports = Jed; |
| 1010 |
} |
| 1011 |
exports.Jed = Jed; |
| 1012 |
} |
| 1013 |
else { |
| 1014 |
if (typeof define === 'function' && define.amd) { |
| 1015 |
define(function() { |
| 1016 |
return Jed; |
| 1017 |
}); |
| 1018 |
} |
| 1019 |
// Leak a global regardless of module system |
| 1020 |
root['Jed'] = Jed; |
| 1021 |
} |
| 1022 |
|
| 1023 |
})(this); |