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