make[2]: Entering directory `/home/stas/moz/code/l20n/js'
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | (function() { |
| 2 | 1 | 'use strict'; |
| 3 | ||
| 4 | 1 | var DEBUG = false; |
| 5 | ||
| 6 | 1 | var L20n; |
| 7 | ||
| 8 | 1 | if (typeof exports !== 'undefined') { |
| 9 | 1 | L20n = exports; |
| 10 | 1 | L20n.Parser = require('./parser.js'); |
| 11 | 1 | L20n.Compiler = require('./compiler.js'); |
| 12 | } else { | |
| 13 | 0 | L20n = this.L20n = {}; |
| 14 | } | |
| 15 | ||
| 16 | 1 | L20n.getContext = function L20n_getContext() { |
| 17 | 11 | return new Context(); |
| 18 | }; | |
| 19 | ||
| 20 | // clear the Resource cache which stores unprocessed LOL files and which is | |
| 21 | // shared across all contexts; additionally, each context also has its own | |
| 22 | // cache for ProcessedResources, which needs to be invalidated independently. | |
| 23 | 1 | L20n.invalidateCache = function L20n_invalidateCache() { |
| 24 | 11 | return resCache.invalidate(); |
| 25 | }; | |
| 26 | ||
| 27 | // define variables and functions whose implementation is | |
| 28 | // environment-dependent. Test suites can change the implementation to | |
| 29 | // support server-side XHR, or to add logic to how URLs are resolved. See | |
| 30 | // /tests/lib/context.js for an example. | |
| 31 | 1 | var env = { |
| 32 | DEBUG: DEBUG, | |
| 33 | getURL: function getURL(url) { | |
| 34 | // in DEBUG mode, bypass the server cache | |
| 35 | 0 | return DEBUG ? url + "?" + Date.now() : url; |
| 36 | }, | |
| 37 | // for now, we only use node to run tests, so it's safe to assume the test | |
| 38 | // suite will provide its own env anyways. in the future, however, when we | |
| 39 | // decide to support node, we should use fs.readFile and fs.readFileSync | |
| 40 | XMLHttpRequest: typeof exports === 'undefined' ? XMLHttpRequest : null, | |
| 41 | } | |
| 42 | ||
| 43 | 1 | Object.defineProperty(L20n, "env", { |
| 44 | 0 | get : function() { return env; }, |
| 45 | 1 | set : function(obj) { env = obj; }, |
| 46 | enumerable : false, | |
| 47 | configurable : false, | |
| 48 | }); | |
| 49 | ||
| 50 | 1 | function debug() { |
| 51 | 72 | if (env.DEBUG) { |
| 52 | 0 | console.log.apply(this, arguments); |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | 1 | var globals = { |
| 57 | get hour() { | |
| 58 | 0 | return new Date().getHours(); |
| 59 | }, | |
| 60 | get os() { | |
| 61 | 0 | if (/^MacIntel/.test(navigator.platform)) { |
| 62 | 0 | return 'mac'; |
| 63 | } | |
| 64 | 0 | if (/^Linux/.test(navigator.platform)) { |
| 65 | 0 | return 'linux'; |
| 66 | } | |
| 67 | 0 | if (/^Win/.test(navigatgor.platform)) { |
| 68 | 0 | return 'win'; |
| 69 | } | |
| 70 | 0 | return 'unknown'; |
| 71 | }, | |
| 72 | }; | |
| 73 | ||
| 74 | 1 | function EventEmitter() { |
| 75 | 13 | this._listeners = {}; |
| 76 | } | |
| 77 | ||
| 78 | 1 | EventEmitter.prototype.emit = function ee_emit() { |
| 79 | 12 | var args = Array.prototype.slice.call(arguments); |
| 80 | 12 | var type = args.shift(); |
| 81 | 12 | var typeListeners = this._listeners[type]; |
| 82 | 12 | if (!typeListeners || !typeListeners.length) { |
| 83 | 0 | return false; |
| 84 | } | |
| 85 | 12 | typeListeners.forEach(function(listener) { |
| 86 | 12 | listener.apply(this, args); |
| 87 | }, this); | |
| 88 | 12 | return true; |
| 89 | } | |
| 90 | ||
| 91 | 1 | EventEmitter.prototype.addEventListener = function ee_add(type, listener) { |
| 92 | 13 | if (!this._listeners[type]) { |
| 93 | 13 | this._listeners[type] = []; |
| 94 | } | |
| 95 | 13 | this._listeners[type].push(listener); |
| 96 | 13 | return this; |
| 97 | } | |
| 98 | ||
| 99 | 1 | EventEmitter.prototype.removeEventListener = function ee_remove(type, listener) { |
| 100 | 0 | var typeListeners = this._listeners[type]; |
| 101 | 0 | var pos = typeListeners.indexOf(listener); |
| 102 | 0 | if (pos === -1) { |
| 103 | 0 | return this; |
| 104 | } | |
| 105 | 0 | listeners.splice(pos, 1); |
| 106 | 0 | return this; |
| 107 | } | |
| 108 | ||
| 109 | 1 | function Cache(ctx, ctor) { |
| 110 | 14 | this.ctx = ctx; |
| 111 | 14 | this.ctor = ctor; |
| 112 | 14 | this.items = {}; |
| 113 | } | |
| 114 | ||
| 115 | 1 | Cache.prototype.get = function Cache_get(id) { |
| 116 | 34 | if (!this.items[id]) { |
| 117 | 34 | this.items[id] = new this.ctor(id, this.ctx, this); |
| 118 | } | |
| 119 | 34 | return this.items[id]; |
| 120 | }; | |
| 121 | ||
| 122 | 1 | Cache.prototype.invalidate = function Cache_invalidate() { |
| 123 | 11 | this.items = {}; |
| 124 | 11 | return true; |
| 125 | } | |
| 126 | ||
| 127 | // keep one cache of Resources for all created contexts | |
| 128 | 1 | var resCache = new Cache(null, Resource); |
| 129 | ||
| 130 | ||
| 131 | 1 | function _ifComplete(self, fn) { |
| 132 | 29 | return function ifComplete() { |
| 133 | 25 | if (self.isComplete()) { |
| 134 | 12 | fn.apply(self, arguments); |
| 135 | } | |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | 1 | function _fire(callbacks, args) { |
| 140 | 25 | callbacks.forEach(function(callback) { |
| 141 | 25 | callback.apply(this, args); |
| 142 | }); | |
| 143 | 25 | callbacks.length = 0; |
| 144 | } | |
| 145 | ||
| 146 | 1 | function Resource(id) { |
| 147 | 17 | var ast = null; |
| 148 | 17 | var imports = null; |
| 149 | 17 | var callbacks = []; |
| 150 | 17 | var xhr; |
| 151 | ||
| 152 | // when the resource is downloading, calling its get method will only add | |
| 153 | // the specified callback to callbacks, without initiating a new XHR | |
| 154 | 17 | var isDownloading = false; |
| 155 | // resource is corrupted when the XHR returned an error; the resource | |
| 156 | // must not be used. | |
| 157 | 17 | var isCorrupted = false; |
| 158 | ||
| 159 | 17 | function downloadAsync(url, callback, fallback) { |
| 160 | 17 | debug('Async GET ', url); |
| 161 | 17 | xhr = new env.XMLHttpRequest(); |
| 162 | 17 | xhr.overrideMimeType('text/plain'); |
| 163 | 17 | xhr.addEventListener('load', function() { |
| 164 | 16 | if (xhr.status == 200) { |
| 165 | 13 | debug(url, 'fetched'); |
| 166 | 13 | callback(xhr.responseText); |
| 167 | } else { | |
| 168 | 3 | debug(url, 'failed to fetch'); |
| 169 | 3 | isCorrupted = true; |
| 170 | 3 | fallback(); |
| 171 | } | |
| 172 | }); | |
| 173 | 17 | xhr.addEventListener('abort', function() { |
| 174 | 0 | debug('XHR aborted for ', url); |
| 175 | }); | |
| 176 | 17 | xhr.addEventListener('error', function() { |
| 177 | 0 | debug('XHR error for ', url); |
| 178 | 0 | fallback(); |
| 179 | }); | |
| 180 | 17 | xhr.open('GET', env.getURL(url), true); |
| 181 | 17 | xhr.send(''); |
| 182 | } | |
| 183 | ||
| 184 | 17 | function downloadSync(url, callback, fallback) { |
| 185 | 0 | debug('Sync GET ', url); |
| 186 | 0 | xhr = new env.XMLHttpRequest(); |
| 187 | 0 | xhr.overrideMimeType('text/plain'); |
| 188 | 0 | xhr.open('GET', env.getURL(url), false); |
| 189 | 0 | xhr.send(''); |
| 190 | 0 | if (xhr.status == 200) { |
| 191 | 0 | debug(url, 'fetched'); |
| 192 | 0 | callback(xhr.responseText); |
| 193 | } else { | |
| 194 | 0 | debug(url, 'failed to fetch'); |
| 195 | 0 | isCorrupted = true; |
| 196 | 0 | fallback(); |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | 17 | function parse(data) { |
| 201 | 13 | ast = L20n.Parser.parse(data).body; |
| 202 | 13 | imports = ast.filter(function(elem) { |
| 203 | 25 | return elem.type == 'ImportStatement'; |
| 204 | }); | |
| 205 | 13 | _fire(callbacks, [ast, imports]); |
| 206 | } | |
| 207 | ||
| 208 | 17 | this.abortXHR = function() { |
| 209 | 3 | debug('aborting XHR for ', id); |
| 210 | 3 | xhr.abort(); |
| 211 | }; | |
| 212 | ||
| 213 | 17 | this.get = function r_get(callback, fallback, async) { |
| 214 | 17 | if (ast) { |
| 215 | 0 | callback(ast, imports); |
| 216 | 17 | } else if (isCorrupted) { |
| 217 | 0 | fallback(); |
| 218 | } else { | |
| 219 | 17 | callbacks.push(callback); |
| 220 | 17 | if (!isDownloading) { |
| 221 | 17 | isDownloading = true; |
| 222 | 17 | if (async) { |
| 223 | 17 | downloadAsync(id, parse, fallback); |
| 224 | } else { | |
| 225 | 0 | downloadSync(id, parse, fallback); |
| 226 | } | |
| 227 | } | |
| 228 | } | |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | 1 | function ProcessedResource(id, ctx, cache) { |
| 233 | ||
| 234 | 30 | var rawast = []; |
| 235 | 30 | var totalImports = 0; |
| 236 | 30 | var callbacks = []; |
| 237 | ||
| 238 | 30 | this.id = id; |
| 239 | 30 | this.ctx = ctx; |
| 240 | 30 | this.ast = []; |
| 241 | 30 | this.importedResources = []; // imported resources in order |
| 242 | ||
| 243 | // a preprocessed resource is ready when all its child resources are ready | |
| 244 | // and it has been preprocessed to include their ASTs. | |
| 245 | 30 | this.isReady = false; |
| 246 | // a preprocessed resource is corrupted when its resource is corrupted, or | |
| 247 | // any of its child preprocessed resources is corrupted too; a corrupted | |
| 248 | // preprocessed resource must not be used. | |
| 249 | 30 | this.isCorrupted = false; |
| 250 | ||
| 251 | 30 | this.dirname = id ? id.split('/').slice(0, -1).join('/') : null; |
| 252 | 30 | this.resource = id ? resCache.get(id) : null; |
| 253 | ||
| 254 | 30 | var self = this; |
| 255 | ||
| 256 | 30 | function relativeToSelf(url) { |
| 257 | 7 | if (url[0] == '/') { |
| 258 | 0 | return url; |
| 259 | 7 | } else if (self.dirname) { |
| 260 | // strip the trailing slash if present | |
| 261 | 3 | if (self.dirname[self.dirname.length - 1] == '/') { |
| 262 | 0 | self.dirname = self.dirname.slice(0, self.dirname.length - 1); |
| 263 | } | |
| 264 | 3 | return self.dirname + '/' + url; |
| 265 | } else { | |
| 266 | 4 | return './' + url; |
| 267 | } | |
| 268 | } | |
| 269 | ||
| 270 | 30 | function normalizeURL(url) { |
| 271 | 17 | var normalized = []; |
| 272 | 17 | var parts = url.split('/'); |
| 273 | 17 | parts.forEach(function(part, i) { |
| 274 | 62 | if (part == '.') { |
| 275 | // don't do anything | |
| 276 | 58 | } else if (part == '..' && normalized[normalized.length - 1]) { |
| 277 | // remove the last element of `normalized` | |
| 278 | 0 | normalized.splice(normalized.length - 1, 1); |
| 279 | } else { | |
| 280 | 58 | normalized.push(part); |
| 281 | } | |
| 282 | }); | |
| 283 | 17 | return normalized.join('/'); |
| 284 | 30 | }; |
| 285 | ||
| 286 | 30 | function _expandUrn(urn, vars) { |
| 287 | 10 | return urn.replace(/(\{\{\s*(\w+)\s*\}\})/g, |
| 288 | function(match, p1, p2, offset, string) { | |
| 289 | 18 | if (vars.hasOwnProperty(p2)) { |
| 290 | 18 | return vars[p2]; |
| 291 | } | |
| 292 | 0 | return p1; |
| 293 | }); | |
| 294 | } | |
| 295 | ||
| 296 | 30 | function resolveURI(uri) { |
| 297 | 16 | if (!/^l10n:/.test(uri)) { |
| 298 | 7 | var url = normalizeURL(relativeToSelf(uri)); |
| 299 | 7 | return [url]; |
| 300 | } | |
| 301 | 9 | if (self.ctx.settings.locales === null) { |
| 302 | 0 | throw "Can't use schema uris without settings.locales"; |
| 303 | } | |
| 304 | 9 | var match = uri.match(/^l10n:(?:([^:]+):)?([^:]+)/); |
| 305 | 9 | if (match === null) { |
| 306 | 0 | throw "Malformed resource scheme: " + uri; |
| 307 | } | |
| 308 | 9 | var vars = { |
| 309 | 'locale': self.ctx.getLocale(), | |
| 310 | 'app': '', | |
| 311 | 'resource': match[2] | |
| 312 | }; | |
| 313 | 9 | if (self.ctx.settings.schemes === null) { |
| 314 | 2 | if (match[1] !== undefined) { |
| 315 | 0 | throw "Can't use schema uris without settings.schemes"; |
| 316 | } | |
| 317 | 2 | uri = [normalizeURL(_expandUrn(match[2], vars))]; |
| 318 | } else { | |
| 319 | 7 | vars['app'] = match[1]; |
| 320 | 7 | uri = []; |
| 321 | 7 | for (var i in self.ctx.settings.schemes) { |
| 322 | 8 | var expanded = _expandUrn(self.ctx.settings.schemes[i], vars); |
| 323 | 8 | uri.push(normalizeURL(expanded)); |
| 324 | } | |
| 325 | } | |
| 326 | 9 | return uri; |
| 327 | } | |
| 328 | ||
| 329 | 30 | function preprocess() { |
| 330 | 2 | if (this.isReady) { |
| 331 | 0 | return; |
| 332 | } | |
| 333 | 2 | rawast.forEach(function(node) { |
| 334 | 5 | if (node.type == 'ImportStatement') { |
| 335 | 2 | var importedAST = this.importedResources.shift().ast; |
| 336 | 2 | this.ast = self.ast.concat(importedAST); |
| 337 | } else { | |
| 338 | 3 | this.ast.push(node); |
| 339 | } | |
| 340 | }, this); | |
| 341 | 2 | this.isReady = true; |
| 342 | 2 | _fire(callbacks); |
| 343 | } | |
| 344 | ||
| 345 | 30 | this.abortImports = function() { |
| 346 | 5 | if (this.resource) { |
| 347 | 3 | this.resource.abortXHR(); |
| 348 | } | |
| 349 | 5 | this.importedResources.forEach(function(imported) { |
| 350 | 3 | imported.abortImports(); |
| 351 | }); | |
| 352 | }; | |
| 353 | ||
| 354 | 30 | this.load = function pr_load(callback, fallback, nesting, async) { |
| 355 | 17 | if (this.isReady) { |
| 356 | 0 | callback(); |
| 357 | 17 | } else if (this.isCorrupted) { |
| 358 | 0 | fallback(); |
| 359 | } else { | |
| 360 | 17 | callbacks.push(callback); |
| 361 | 17 | this.resource.get(function resolveImports(ast, imports) { |
| 362 | 13 | rawast = ast; |
| 363 | 13 | totalImports = imports.length; |
| 364 | 13 | if (totalImports) { |
| 365 | 3 | imports.forEach(function(node){ |
| 366 | 3 | debug('importing', node.uri.content) |
| 367 | 3 | self.importResource(node.uri.content, |
| 368 | _ifComplete(self, preprocess), | |
| 369 | fallback, nesting + 1, async); | |
| 370 | }); | |
| 371 | } else { | |
| 372 | 10 | self.ast = rawast; |
| 373 | 10 | self.isReady = true; |
| 374 | 10 | _fire(callbacks); |
| 375 | } | |
| 376 | }, fallback, async); | |
| 377 | } | |
| 378 | } | |
| 379 | ||
| 380 | 30 | function importFirstURL(urls, callback, fallback, nesting, async) { |
| 381 | 17 | var url = urls.shift(); |
| 382 | 17 | var imported = cache.get(url); |
| 383 | 17 | self.importedResources.push(imported); |
| 384 | 17 | imported.load( |
| 385 | function importSucceeded() { | |
| 386 | 12 | debug('calling callback,', url, ' loaded OK'); |
| 387 | 12 | callback(); |
| 388 | }, | |
| 389 | // `importFailed` is the fallback function which is called when the | |
| 390 | // imported resource couldn't be loaded. There are two ways in which | |
| 391 | // the load function can fail: | |
| 392 | // 1. the resource file could not be found at the specified URL and | |
| 393 | // there are more locations to look for it in; in this case, look for | |
| 394 | // the resource in a different location, | |
| 395 | // 2. the resource file could not be found and there are no more URLs | |
| 396 | // to try; in this case, the context cannot be integral and a subctx | |
| 397 | // must be created | |
| 398 | // The second type of failure can happen for the resource that is | |
| 399 | // currently being imported, as well as for any other nested import | |
| 400 | // inside of it. The `importFailed` fallback will be also called from | |
| 401 | // this resource's `importResource` method (or this resource's | |
| 402 | // children's `importResource` method) and the `integrityError` | |
| 403 | // argument is a flag which, when `true`, indicates that the second | |
| 404 | // type of failure has occurred somewhere deeper in the import tree. | |
| 405 | function importFailed(integrityError) { | |
| 406 | 4 | debug('calling fallback,', url, ' failed to load'); |
| 407 | 4 | if (integrityError || urls.length == 0) { |
| 408 | 3 | debug('no more scheme URLs to try, creating a subcontext'); |
| 409 | // fallback to the subctx; true bubbles the integrityError up to | |
| 410 | // the parent fallback | |
| 411 | 3 | fallback(true); |
| 412 | } else { | |
| 413 | 1 | imported.isCorrupted = true; |
| 414 | // remove the imported resource from imports, so that we don't | |
| 415 | // check if it's ready anymore | |
| 416 | 1 | var pos = self.importedResources.indexOf(imported); |
| 417 | 1 | if (pos > -1) { |
| 418 | 1 | self.importedResources.splice(pos, 1); |
| 419 | } | |
| 420 | 1 | importFirstURL(urls, callback, fallback, nesting, async); |
| 421 | } | |
| 422 | }, nesting, async); | |
| 423 | } | |
| 424 | ||
| 425 | 30 | this.importResource = function pr_importResource(uri, |
| 426 | callback, | |
| 427 | fallback, | |
| 428 | nesting, | |
| 429 | async) { | |
| 430 | 16 | nesting = nesting || 0; |
| 431 | 16 | if (nesting > 7) { |
| 432 | 0 | throw "Too many nested imports"; |
| 433 | } | |
| 434 | // in case the uri is defined in the l10n: scheme, resolve it | |
| 435 | 16 | var urls = resolveURI(uri); |
| 436 | 16 | importFirstURL(urls, callback, fallback, nesting, async); |
| 437 | } | |
| 438 | ||
| 439 | 30 | this.isComplete = function pr_isComplete() { |
| 440 | 2 | if (this.importedResources.length != totalImports) { |
| 441 | 0 | return false; |
| 442 | } | |
| 443 | 2 | for (var i in this.importedResources) { |
| 444 | 2 | if (!this.importedResources[i].isReady) { |
| 445 | 0 | return false; |
| 446 | } | |
| 447 | } | |
| 448 | 2 | return true; |
| 449 | } | |
| 450 | } | |
| 451 | ||
| 452 | 1 | function Context() { |
| 453 | ||
| 454 | 13 | var entries = {}; // resource entries |
| 455 | 13 | var ctxdata = {}; // context variables |
| 456 | 13 | var emitter = new EventEmitter(); |
| 457 | 13 | var settings = { |
| 458 | 'schemes': null, // path schemes | |
| 459 | 'locales': null, // list of locale codes in priority order | |
| 460 | 'timeout': 500, // timeout for asynchronous resource loading | |
| 461 | }; | |
| 462 | ||
| 463 | // when context is frozen, no more resources can be added to it | |
| 464 | 13 | var isFrozen = false; |
| 465 | // context is integral when all the resources have been downloaded | |
| 466 | 13 | var isIntegral = false; |
| 467 | // context is ready when it's integral and compiled | |
| 468 | 13 | var isReady = false; |
| 469 | ||
| 470 | // a subcontext can be created when 1) the context is not integral, or 2) | |
| 471 | // the context is ready, but an entity couldn't be found in it | |
| 472 | 13 | var subctx; |
| 473 | ||
| 474 | // contrary to the resCache, the cache for processed resources is | |
| 475 | // per-context, because ProcessedResources keep reference to their parent | |
| 476 | // context | |
| 477 | 13 | var cache = new Cache(this, ProcessedResource); |
| 478 | ||
| 479 | // Context is a top-level resource that imports other resources. The only | |
| 480 | // difference is that a regular resource is a file, and the EOF marks the | |
| 481 | // moment when the resource no longer accepts new imports. For the | |
| 482 | // context's meta resource we need the `freeze` method to simulate this | |
| 483 | // behavior. | |
| 484 | 13 | var meta = new ProcessedResource(null, this, cache); |
| 485 | // the URIs of all resources that have been addResource'd; used for | |
| 486 | // creating a subctx | |
| 487 | 13 | var uris = []; |
| 488 | // uris' length, basically; used in m_isComplete to make sure we're | |
| 489 | // checking all resources for readiness | |
| 490 | 13 | var totalResources = 0; |
| 491 | ||
| 492 | 13 | var self = this; |
| 493 | ||
| 494 | 13 | meta.isComplete = function m_isComplete() { |
| 495 | 23 | if (!isFrozen) { |
| 496 | 0 | return false; |
| 497 | } | |
| 498 | 23 | if (meta.importedResources.length != totalResources) { |
| 499 | 0 | return false; |
| 500 | } | |
| 501 | 23 | for (var i in this.importedResources) { |
| 502 | 23 | if (!this.importedResources[i].isReady) { |
| 503 | 13 | return false; |
| 504 | } | |
| 505 | } | |
| 506 | 10 | isIntegral = true; |
| 507 | 10 | return true; |
| 508 | } | |
| 509 | ||
| 510 | 13 | function compile() { |
| 511 | 10 | if (isReady) { |
| 512 | 0 | return; |
| 513 | } | |
| 514 | // flatten the AST | |
| 515 | 10 | meta.ast = meta.importedResources.reduce(function(prev, curr) { |
| 516 | 10 | return prev.concat(curr.ast); |
| 517 | }, []); | |
| 518 | 10 | L20n.Compiler.compile(meta.ast, entries, globals); |
| 519 | 10 | isReady = true; |
| 520 | 10 | debug('context ready, current locale is ', self.getLocale()); |
| 521 | 10 | emitter.emit('ready'); |
| 522 | } | |
| 523 | ||
| 524 | 13 | function getArgs(data) { |
| 525 | 10 | var args = Object.create(ctxdata); |
| 526 | 10 | if (data) { |
| 527 | 10 | for (var i in data) { |
| 528 | 0 | args[i] = data[i]; |
| 529 | } | |
| 530 | } | |
| 531 | 10 | return args; |
| 532 | } | |
| 533 | ||
| 534 | 13 | function createSubContext(async) { |
| 535 | 2 | var subctx = new Context(); |
| 536 | 2 | subctx.settings.schemes = self.settings.schemes; |
| 537 | 2 | subctx.settings.locales = self.settings.locales.slice(1); |
| 538 | 2 | if (!subctx.getLocale()) { |
| 539 | 0 | emitter.emit('error'); |
| 540 | 0 | throw "None of the requested locales was available."; |
| 541 | } | |
| 542 | 2 | debug('subcontext\'s locale is ', subctx.getLocale()); |
| 543 | 2 | uris.forEach(function(uri) { |
| 544 | 2 | subctx.addResource(uri, async); |
| 545 | }); | |
| 546 | 2 | subctx.freeze(); |
| 547 | 2 | return subctx; |
| 548 | } | |
| 549 | ||
| 550 | 13 | function invalidateOnIntegrityError() { |
| 551 | 2 | isIntegral = false; |
| 552 | 2 | meta.abortImports(); |
| 553 | 2 | debug('invalidated context\'s locale was ', self.getLocale()); |
| 554 | 2 | subctx = createSubContext(true); |
| 555 | 2 | subctx.addEventListener('ready', function() { |
| 556 | 2 | isReady = true; |
| 557 | 2 | emitter.emit('ready'); |
| 558 | }); | |
| 559 | } | |
| 560 | ||
| 561 | 13 | function getSync(id, data) { |
| 562 | 12 | if (!isReady) { |
| 563 | 0 | throw "Error: context not ready"; |
| 564 | } | |
| 565 | 12 | if (!isIntegral) { |
| 566 | 2 | return subctx.get(id, data); |
| 567 | } | |
| 568 | 10 | var entity = entries[id]; |
| 569 | 10 | if (!entity) { |
| 570 | 0 | debug('entity', id, 'not found, using subcontext'); |
| 571 | 0 | if (!subctx) { |
| 572 | 0 | debug('creating subcontext'); |
| 573 | 0 | subctx = createSubContext(false); |
| 574 | } | |
| 575 | 0 | return subctx.get(id, data); |
| 576 | } | |
| 577 | 10 | if (!entity || entity.local) { |
| 578 | 0 | throw "No such entity: " + id; |
| 579 | } | |
| 580 | 10 | var args = getArgs(data); |
| 581 | 10 | return entity.toString(args); |
| 582 | 13 | }; |
| 583 | ||
| 584 | 13 | function getAsync(id, data, callback, defaultValue) { |
| 585 | 11 | if (isReady) { |
| 586 | 0 | callback(getSync(id, data)); |
| 587 | 0 | return; |
| 588 | } | |
| 589 | 11 | var overdue = false; |
| 590 | 11 | var timeout = setTimeout(function() { |
| 591 | 1 | overdue = true; |
| 592 | // defaultValue is just a string, can't interpolate context data | |
| 593 | 1 | callback(defaultValue); |
| 594 | }, settings.timeout); | |
| 595 | ||
| 596 | 11 | self.addEventListener('ready', function(event) { |
| 597 | // if the event fired too late, the default value has already been used | |
| 598 | 10 | if (overdue) { |
| 599 | 0 | return; |
| 600 | } | |
| 601 | 10 | clearTimeout(timeout); |
| 602 | 10 | callback(getSync(id, data)); |
| 603 | }); | |
| 604 | 13 | }; |
| 605 | ||
| 606 | 13 | this.__defineSetter__('data', function(data) { |
| 607 | 0 | ctxdata = data; |
| 608 | }); | |
| 609 | ||
| 610 | 13 | this.__defineGetter__('data', function() { |
| 611 | 0 | return ctxdata; |
| 612 | }); | |
| 613 | ||
| 614 | 13 | Object.defineProperty(this, 'settings', { |
| 615 | value: Object.create(Object.prototype, { | |
| 616 | locales: { | |
| 617 | 11 | get: function() { return settings.locales }, |
| 618 | set: function(val) { | |
| 619 | 9 | if (!Array.isArray(val)) { |
| 620 | 0 | throw "Locales must be a list"; |
| 621 | } | |
| 622 | 9 | if (val.length == 0) { |
| 623 | 0 | throw "Locales list must not be empty"; |
| 624 | } | |
| 625 | 9 | if (settings.locales !== null) { |
| 626 | 0 | throw "Can't overwrite locales"; |
| 627 | } | |
| 628 | 9 | settings.locales = val; |
| 629 | 9 | Object.freeze(settings.locales); |
| 630 | }, | |
| 631 | configurable: false, | |
| 632 | enumerable: true, | |
| 633 | }, | |
| 634 | schemes: { | |
| 635 | 26 | get: function() { return settings.schemes }, |
| 636 | set: function(val) { | |
| 637 | 7 | if (!Array.isArray(val)) { |
| 638 | 0 | throw "Schemes must be a list"; |
| 639 | } | |
| 640 | 7 | if (val.length == 0) { |
| 641 | 0 | throw "Scheme list must not be empty"; |
| 642 | } | |
| 643 | 7 | if (settings.schemes !== null) { |
| 644 | 0 | throw "Can't overwrite schemes"; |
| 645 | } | |
| 646 | 7 | settings.schemes = val; |
| 647 | 7 | Object.freeze(settings.schemes); |
| 648 | }, | |
| 649 | configurable: false, | |
| 650 | enumerable: true, | |
| 651 | }, | |
| 652 | timeout: { | |
| 653 | 0 | get: function() { return settings.timeout }, |
| 654 | set: function(val) { | |
| 655 | 0 | if (typeof(val) !== 'number') { |
| 656 | 0 | throw "Timeout must be a number"; |
| 657 | } | |
| 658 | 0 | settings.timeout = val; |
| 659 | }, | |
| 660 | configurable: false, | |
| 661 | enumerable: true, | |
| 662 | }, | |
| 663 | }), | |
| 664 | writable: false, | |
| 665 | enumerable: false, | |
| 666 | configurable: false, | |
| 667 | }); | |
| 668 | ||
| 669 | 13 | this.getLocale = function ctx_getLocale() { |
| 670 | 25 | if (!settings.locales || settings.locales.length == 0) { |
| 671 | 3 | return null; |
| 672 | } | |
| 673 | 22 | return settings.locales[0]; |
| 674 | }; | |
| 675 | ||
| 676 | // clear this context's ProcessedResources cache | |
| 677 | 13 | this.invalidateCache = function ctx_invalidateCache() { |
| 678 | 0 | return cache.invalidate(); |
| 679 | }; | |
| 680 | ||
| 681 | 13 | this.addResource = function ctx_addResource(uri, async) { |
| 682 | 13 | if (isFrozen) { |
| 683 | 0 | throw "Context is frozen, can't add more resources"; |
| 684 | } | |
| 685 | 13 | if (async === undefined) { |
| 686 | 11 | async = true; |
| 687 | } | |
| 688 | 13 | uris.push(uri); |
| 689 | 13 | meta.importResource(uri, _ifComplete(meta, compile), |
| 690 | invalidateOnIntegrityError, 0, async); | |
| 691 | 13 | return true; |
| 692 | }; | |
| 693 | ||
| 694 | 13 | this.freeze = function ctx_freeze() { |
| 695 | 13 | isFrozen = true; |
| 696 | 13 | totalResources = uris.length; |
| 697 | 13 | _ifComplete(meta, compile)(); |
| 698 | 13 | return true; |
| 699 | }; | |
| 700 | ||
| 701 | 13 | this.get = function ctx_get(id, data, callback, fallback) { |
| 702 | 13 | if (callback === undefined) { |
| 703 | 2 | return getSync(id, data); |
| 704 | } | |
| 705 | 11 | getAsync(id, data, callback, fallback); |
| 706 | 11 | return null; |
| 707 | }; | |
| 708 | ||
| 709 | 13 | this.getAttribute = function ctx_getAttribute(id, attr, data) { |
| 710 | 0 | if (!isReady) { |
| 711 | 0 | throw "Error: context not ready"; |
| 712 | } | |
| 713 | 0 | var entity = entries[id]; |
| 714 | 0 | if (!entity || entity.local) { |
| 715 | 0 | throw "No such entity: " + id; |
| 716 | } | |
| 717 | 0 | var attribute = entity.attributes[attr] |
| 718 | 0 | if (!attribute || attribute.local) { |
| 719 | 0 | throw "No such attribute: " + attr; |
| 720 | } | |
| 721 | 0 | var args = getArgs(data); |
| 722 | 0 | return attribute.toString(args); |
| 723 | }; | |
| 724 | ||
| 725 | 13 | this.getAttributes = function ctx_getAttributes(id, data) { |
| 726 | 0 | if (!isReady) { |
| 727 | 0 | throw "Error: context not ready"; |
| 728 | } | |
| 729 | 0 | var entity = entries[id]; |
| 730 | 0 | if (!entity || entity.local) { |
| 731 | 0 | throw "No such entity: " + id; |
| 732 | } | |
| 733 | 0 | var args = getArgs(data); |
| 734 | 0 | var attributes = {}; |
| 735 | 0 | for (var attr in entity.attributes) { |
| 736 | 0 | var attribute = entity.attributes[attr]; |
| 737 | 0 | if (!attribute.local) { |
| 738 | 0 | attributes[attr] = attribute.toString(args); |
| 739 | } | |
| 740 | } | |
| 741 | 0 | return attributes; |
| 742 | } | |
| 743 | ||
| 744 | 13 | this.addEventListener = function ctx_addEventListener(type, listener) { |
| 745 | 13 | return emitter.addEventListener(type, listener); |
| 746 | } | |
| 747 | ||
| 748 | 13 | this.removeEventListener = function ctx_removeEventListener(type, listener) { |
| 749 | 0 | return emitter.removeEventListener(type, listener) |
| 750 | } | |
| 751 | } | |
| 752 | }).call(this); |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | (function() { |
| 2 | 1 | 'use strict'; |
| 3 | ||
| 4 | 1 | var Parser; |
| 5 | ||
| 6 | 1 | if (typeof exports !== 'undefined') { |
| 7 | 1 | Parser = exports; |
| 8 | } else { | |
| 9 | 0 | Parser = this.L20n.Parser = {}; |
| 10 | } | |
| 11 | ||
| 12 | 1 | Parser.parse = function parse(string) { |
| 13 | 13 | var lol = {type: 'LOL', |
| 14 | body: []} | |
| 15 | 13 | content = string |
| 16 | 13 | get_ws(); |
| 17 | 13 | while (content) { |
| 18 | 25 | lol.body.push(get_entry()) |
| 19 | 25 | get_ws(); |
| 20 | } | |
| 21 | 13 | return lol |
| 22 | } | |
| 23 | ||
| 24 | 1 | var content = null; |
| 25 | 1 | var patterns = { |
| 26 | id: /^([_a-zA-Z]\w*)/, | |
| 27 | value: /^(["'])([^'"]*)(["'])/, | |
| 28 | ws: /^\s+/ | |
| 29 | }; | |
| 30 | ||
| 31 | ||
| 32 | 1 | function get_ws() { |
| 33 | 91 | content = content.replace(patterns['ws'], '') |
| 34 | } | |
| 35 | ||
| 36 | 1 | function get_entry() { |
| 37 | 25 | var entry |
| 38 | 25 | if (content[0] == '<') { |
| 39 | 22 | content = content.substr(1) |
| 40 | 22 | var id = get_identifier() |
| 41 | 22 | if (content[0] == '(') { |
| 42 | 0 | entry = get_macro(id) |
| 43 | 22 | } else if (content[0] == '[') { |
| 44 | 0 | var index = get_index() |
| 45 | 0 | entry = get_entity(id, index) |
| 46 | } else { | |
| 47 | 22 | entry = get_entity(id); |
| 48 | } | |
| 49 | 3 | } else if (content.substr(0,2) == '/*') { |
| 50 | 0 | entry = get_comment(); |
| 51 | 3 | } else if (content.substr(0,6) == 'import') { |
| 52 | 3 | entry = get_importstatement() |
| 53 | } else { | |
| 54 | 0 | throw "ParserError at get_entry" |
| 55 | } | |
| 56 | 25 | return entry |
| 57 | } | |
| 58 | ||
| 59 | 1 | function get_importstatement() { |
| 60 | 3 | content = content.substr(6) |
| 61 | 3 | get_ws() |
| 62 | 3 | if (content[0] != '(') { |
| 63 | 0 | throw "ParserError" |
| 64 | } | |
| 65 | 3 | content = content.substr(1) |
| 66 | 3 | get_ws() |
| 67 | 3 | var uri = get_string() |
| 68 | 3 | get_ws() |
| 69 | 3 | if (content[0] != ')') { |
| 70 | 0 | throw "ParserError" |
| 71 | } | |
| 72 | 3 | content = content.substr(1) |
| 73 | 3 | var impStmt = { |
| 74 | type: 'ImportStatement', | |
| 75 | uri: uri | |
| 76 | } | |
| 77 | 3 | return impStmt |
| 78 | } | |
| 79 | ||
| 80 | 1 | function get_identifier() { |
| 81 | 22 | if (content[0] == '~') { |
| 82 | // this expression | |
| 83 | } | |
| 84 | 22 | var match = patterns['id'].exec(content) |
| 85 | 22 | if (!match) |
| 86 | 0 | throw "ParserError at get_identifier" |
| 87 | 22 | content = content.substr(match[0].length) |
| 88 | 22 | var identifier = {type: 'Identifier', |
| 89 | name: match[0]} | |
| 90 | 22 | return identifier |
| 91 | } | |
| 92 | ||
| 93 | 1 | function get_entity(id, index) { |
| 94 | 22 | var ch = content[0] |
| 95 | 22 | get_ws(); |
| 96 | 22 | if (content[0] == '>') { |
| 97 | // empty entity | |
| 98 | } | |
| 99 | 22 | if (!/\s/g.test(ch)) { |
| 100 | 0 | throw "ParserError at get_entity" |
| 101 | } | |
| 102 | 22 | var value = get_value(true) |
| 103 | 22 | get_ws() |
| 104 | 22 | var attrs = get_attributes() |
| 105 | 22 | var entity = { |
| 106 | type: 'Entity', | |
| 107 | id: id, | |
| 108 | value: value, | |
| 109 | index: index || [], | |
| 110 | attrs: attrs, | |
| 111 | local: (id.name[0] == '_') | |
| 112 | } | |
| 113 | 22 | return entity |
| 114 | } | |
| 115 | ||
| 116 | 1 | function get_macro(id) { |
| 117 | 0 | if (id.name[0] == '_') { |
| 118 | 0 | throw "ParserError at get_macro" |
| 119 | } | |
| 120 | 0 | var idlist = [] |
| 121 | 0 | content = content.substr(1) |
| 122 | 0 | get_ws() |
| 123 | 0 | if (content[0] == ')') { |
| 124 | 0 | content = content.substr(1) |
| 125 | } else { | |
| 126 | 0 | while (1) { |
| 127 | 0 | idlist.push(get_variable()) |
| 128 | 0 | get_ws() |
| 129 | 0 | if (content[0] == ',') { |
| 130 | 0 | content = content.substr(1) |
| 131 | 0 | get_ws() |
| 132 | 0 | } else if (content[0] == ')') { |
| 133 | 0 | content = content.substr(1) |
| 134 | 0 | break |
| 135 | } else { | |
| 136 | 0 | throw "ParserError at get_macro" |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | 0 | get_ws() |
| 141 | // we should check if the ws was empty and throw ParserError here | |
| 142 | 0 | if (content[0] != '{') { |
| 143 | 0 | throw "ParserError at get_macro" |
| 144 | } | |
| 145 | 0 | content = content.substr(1) |
| 146 | 0 | get_ws() |
| 147 | 0 | var exp = get_expression() |
| 148 | 0 | get_ws() |
| 149 | 0 | if (content[0] != '}') { |
| 150 | 0 | throw "ParserError at get_macro" |
| 151 | } | |
| 152 | 0 | content = content.substr(1) |
| 153 | 0 | get_ws() |
| 154 | 0 | var attrs = get_attributes() |
| 155 | 0 | var macro = { |
| 156 | 'type': 'Macro', | |
| 157 | 'id': id, | |
| 158 | 'args': idlist, | |
| 159 | 'expression': exp | |
| 160 | } | |
| 161 | // macro.attrs | |
| 162 | 0 | return macro |
| 163 | } | |
| 164 | ||
| 165 | 1 | function get_value(none) { |
| 166 | 22 | var c = content[0] |
| 167 | 22 | var value |
| 168 | 22 | if (c == '"' || c == "'") { |
| 169 | 22 | var ccc = content.substr(3) |
| 170 | 22 | var quote = (ccc == '"""' || ccc == "'''")?ccc:c |
| 171 | //var value = get_string() | |
| 172 | 22 | value = get_complex_string(quote) |
| 173 | 0 | } else if (c == '[') { |
| 174 | 0 | value = get_array() |
| 175 | 0 | } else if (c == '{') { |
| 176 | 0 | value = get_hash() |
| 177 | } | |
| 178 | 22 | return value |
| 179 | } | |
| 180 | ||
| 181 | 1 | function get_string() { |
| 182 | 3 | var match = patterns['value'].exec(content) |
| 183 | 3 | if (!match) { |
| 184 | 0 | throw "ParserError at get_string" |
| 185 | } | |
| 186 | 3 | content = content.substr(match[0].length) |
| 187 | 3 | return {type: 'String', content: match[2]} |
| 188 | } | |
| 189 | ||
| 190 | 1 | function get_complex_string(quote) { |
| 191 | 22 | var str_end = quote[0] |
| 192 | 22 | var literal = new RegExp("^([^\\\{"+str_end+"]+)") |
| 193 | 22 | var obj = [] |
| 194 | 22 | var buffer = '' |
| 195 | 22 | content = content.substr(quote.length) |
| 196 | 22 | var i = 0 |
| 197 | 22 | while (content.substr(0, quote.length) != quote) { |
| 198 | 22 | i++; |
| 199 | 22 | if (i>20) |
| 200 | 0 | break |
| 201 | 22 | if (content[0] == str_end) { |
| 202 | 0 | buffer += content[0] |
| 203 | 0 | content = content.substr(1) |
| 204 | } | |
| 205 | 22 | if (content[0] == '\\') { |
| 206 | 0 | var jump = content.substr(1, 3) == '{{' ? 3 : 2; |
| 207 | 0 | buffer += content.substr(1, jump) |
| 208 | 0 | content = content.substr(jump) |
| 209 | } | |
| 210 | 22 | if (content.substr(0, 2) == '{{') { |
| 211 | 0 | content = content.substr(2) |
| 212 | 0 | if (buffer) { |
| 213 | 0 | var string = {type: 'String', content: buffer} |
| 214 | 0 | obj.push(string) |
| 215 | 0 | buffer = '' |
| 216 | } | |
| 217 | 0 | get_ws() |
| 218 | 0 | var expr = get_expression() |
| 219 | 0 | obj.push(expr) |
| 220 | 0 | if (content.substr(0, 2) != '}}') { |
| 221 | 0 | throw "ParserError at get_complex_string" |
| 222 | } | |
| 223 | 0 | content = content.substr(2) |
| 224 | } | |
| 225 | 22 | var m = literal.exec(content) |
| 226 | 22 | if (m) { |
| 227 | 22 | buffer = m[1] |
| 228 | 22 | content = content.substr(m[0].length) |
| 229 | } | |
| 230 | } | |
| 231 | 22 | if (buffer) { |
| 232 | 22 | var string = {type: 'String', content: buffer} |
| 233 | 22 | obj.push(string) |
| 234 | } | |
| 235 | 22 | content = content.substr(quote.length) |
| 236 | 22 | if (obj.length == 1 && obj[0].type == 'String') { |
| 237 | 22 | return obj[0] |
| 238 | } | |
| 239 | 0 | var cs = {type: 'ComplexString', content: obj} |
| 240 | 0 | return cs |
| 241 | } | |
| 242 | ||
| 243 | 1 | function get_hash() { |
| 244 | 0 | content = content.substr(1) |
| 245 | 0 | get_ws() |
| 246 | 0 | if (content[0] == '}') { |
| 247 | 0 | var h = {type: 'Hash', content: []} |
| 248 | 0 | return h |
| 249 | } | |
| 250 | 0 | var hash = [] |
| 251 | 0 | while (1) { |
| 252 | 0 | var defitem = false |
| 253 | 0 | if (content[0] == '*') { |
| 254 | 0 | content = content.substr(1) |
| 255 | 0 | defitem = true |
| 256 | } | |
| 257 | 0 | var hi = get_kvp('HashItem') |
| 258 | 0 | hi['default'] = defitem |
| 259 | 0 | hash.push(hi) |
| 260 | 0 | get_ws() |
| 261 | 0 | if (content[0] == ',') { |
| 262 | 0 | content = content.substr(1) |
| 263 | 0 | get_ws() |
| 264 | 0 | } else if (content[0] == '}') { |
| 265 | 0 | break |
| 266 | } else { | |
| 267 | 0 | throw "ParserError in get_hash" |
| 268 | } | |
| 269 | } | |
| 270 | 0 | content = content.substr(1) |
| 271 | 0 | var h = {type: 'Hash', content: hash} |
| 272 | 0 | return h |
| 273 | } | |
| 274 | ||
| 275 | 1 | function get_kvp(cl) { |
| 276 | 0 | var key = get_identifier() |
| 277 | 0 | get_ws() |
| 278 | 0 | if (content[0] != ':') { |
| 279 | 0 | throw "ParserError" |
| 280 | } | |
| 281 | 0 | content = content.substr(1) |
| 282 | 0 | get_ws() |
| 283 | 0 | var val = get_value() |
| 284 | 0 | var kvp = {type: cl, key: key, value: val} |
| 285 | 0 | return kvp |
| 286 | } | |
| 287 | ||
| 288 | 1 | function get_attributes() { |
| 289 | 22 | if (content[0] == '>') { |
| 290 | 22 | content = content.substr(1) |
| 291 | 22 | return {} |
| 292 | } | |
| 293 | 0 | var attrs = {} |
| 294 | 0 | while (1) { |
| 295 | 0 | var attr = get_kvp('Attribute') |
| 296 | 0 | attr.local = attr.key.name[0] == '_' |
| 297 | 0 | attrs[attr.key.name] = attr |
| 298 | 0 | var ch = content[0] |
| 299 | 0 | get_ws() |
| 300 | 0 | if (content[0] == '>') { |
| 301 | 0 | content = content.substr(1) |
| 302 | 0 | break |
| 303 | 0 | } else if (!/^\s/.test(ch)) { |
| 304 | 0 | throw "ParserError" |
| 305 | } | |
| 306 | } | |
| 307 | 0 | return attrs |
| 308 | } | |
| 309 | ||
| 310 | 1 | function get_index() { |
| 311 | 0 | content = content.substr(1) |
| 312 | 0 | get_ws() |
| 313 | 0 | var index = [] |
| 314 | 0 | if (content[0] == ']') { |
| 315 | 0 | content = content.substr(1) |
| 316 | 0 | return index |
| 317 | } | |
| 318 | 0 | while (1) { |
| 319 | 0 | var expression = get_expression() |
| 320 | 0 | index.push(expression) |
| 321 | 0 | get_ws() |
| 322 | 0 | if (content[0] == ',') { |
| 323 | 0 | content = content.substr(1) |
| 324 | 0 | } else if (content[0] == ']') { |
| 325 | 0 | break |
| 326 | } else { | |
| 327 | 0 | throw "ParserError in get_index" |
| 328 | } | |
| 329 | } | |
| 330 | 0 | content = content.substr(1) |
| 331 | 0 | return index |
| 332 | } | |
| 333 | ||
| 334 | 1 | function get_expression() { |
| 335 | 0 | return get_conditional_expression() |
| 336 | 0 | var exp = get_primary_expression() |
| 337 | 0 | get_ws() |
| 338 | 0 | return exp |
| 339 | } | |
| 340 | ||
| 341 | 1 | function get_conditional_expression() { |
| 342 | 0 | var or_expression = get_or_expression() |
| 343 | 0 | get_ws() |
| 344 | 0 | if (content[0] != '?') { |
| 345 | 0 | return or_expression |
| 346 | } | |
| 347 | 0 | content = content.substr(1) |
| 348 | 0 | var consequent = get_expression() |
| 349 | 0 | get_ws() |
| 350 | 0 | if (content[0] != ':') { |
| 351 | 0 | throw "ParserError in get_conditional_expression" |
| 352 | } | |
| 353 | 0 | content = content.substr(1) |
| 354 | 0 | get_ws() |
| 355 | 0 | var alternate = get_expression() |
| 356 | 0 | var cons_exp = { |
| 357 | 'type': 'ConditionalExpression', | |
| 358 | 'test': or_expression, | |
| 359 | 'consequent': consequent, | |
| 360 | 'alternate': alternate | |
| 361 | } | |
| 362 | 0 | return cons_exp |
| 363 | } | |
| 364 | ||
| 365 | 1 | function get_prefix_expression(token, token_length, cl, op, nxt) { |
| 366 | 0 | var exp = nxt() |
| 367 | 0 | get_ws() |
| 368 | 0 | while (token.indexOf(content.substr(0, token_length)) !== -1) { |
| 369 | 0 | var t = content.substr(0, token_length) |
| 370 | 0 | content = content.substr(token_length) |
| 371 | 0 | get_ws() |
| 372 | 0 | op.token = t |
| 373 | 0 | cl.left = exp |
| 374 | 0 | cl.right = nxt() |
| 375 | 0 | get_ws() |
| 376 | } | |
| 377 | 0 | return exp |
| 378 | } | |
| 379 | ||
| 380 | 1 | function get_or_expression() { |
| 381 | 0 | var token=['||',] |
| 382 | 0 | var cl = { |
| 383 | 'type': 'LogicalExpression', | |
| 384 | } | |
| 385 | 0 | var op = { |
| 386 | 'type': 'LogicalOperator', | |
| 387 | } | |
| 388 | 0 | return get_prefix_expression(token, 2, cl, op, get_and_expression) |
| 389 | } | |
| 390 | ||
| 391 | 1 | function get_and_expression() { |
| 392 | 0 | var token=['&&',] |
| 393 | 0 | var cl = { |
| 394 | 'type': 'LogicalExpression', | |
| 395 | } | |
| 396 | 0 | var op = { |
| 397 | 'type': 'LogicalOperator', | |
| 398 | } | |
| 399 | 0 | return get_prefix_expression(token, 2, cl, op, get_equality_expression) |
| 400 | } | |
| 401 | ||
| 402 | 1 | function get_equality_expression() { |
| 403 | 0 | var token=['==',] |
| 404 | 0 | var cl = { |
| 405 | 'type': 'BinaryExpression', | |
| 406 | } | |
| 407 | 0 | var op = { |
| 408 | 'type': 'BinaryOperator', | |
| 409 | } | |
| 410 | 0 | return get_prefix_expression(token, 2, cl, op, get_member_expression) |
| 411 | } | |
| 412 | ||
| 413 | 1 | function get_member_expression() { |
| 414 | 0 | var exp = get_primary_expression() |
| 415 | 0 | var match = content.match(/^(\w*)/) |
| 416 | 0 | var ws_post_id = "" |
| 417 | ||
| 418 | 0 | if (match) { |
| 419 | 0 | ws_post_id = match[1] |
| 420 | 0 | content = content.substr(ws_post_id.length) |
| 421 | } | |
| 422 | 0 | get_ws() |
| 423 | 0 | var matched = false |
| 424 | 0 | while (1) { |
| 425 | 0 | if (['.[', '..'].indexOf(content.substr(2)) !== -1) { |
| 426 | 0 | exp = get_attr_expression(exp, ws_post_id) |
| 427 | 0 | matched = true |
| 428 | 0 | } else if (['[', '.'].indexOf(content[0]) !== -1) { |
| 429 | 0 | exp = get_property_expression(exp, ws_post_id) |
| 430 | 0 | matched = true |
| 431 | 0 | } else if (content[0] == '(') { |
| 432 | 0 | exp = get_call_expression(exp, ws_post_id) |
| 433 | 0 | matched = true |
| 434 | } else { | |
| 435 | 0 | break |
| 436 | } | |
| 437 | } | |
| 438 | 0 | if (!matched) { |
| 439 | 0 | content = ws_post_id + content |
| 440 | } | |
| 441 | 0 | return exp |
| 442 | } | |
| 443 | ||
| 444 | 1 | function get_primary_expression() { |
| 445 | // number | |
| 446 | 0 | var match = content.match(/^(\d+)/) |
| 447 | 0 | if (match) { |
| 448 | 0 | var d = parseInt(match[1]) |
| 449 | 0 | content = content.substr(match[1].length) |
| 450 | 0 | return { |
| 451 | 'type': 'Literal', | |
| 452 | 'value': d | |
| 453 | } | |
| 454 | } | |
| 455 | // value | |
| 456 | 0 | if (["'",'"','{','['].indexOf(content[0]) !== -1) { |
| 457 | 0 | return get_value() |
| 458 | } | |
| 459 | // variable | |
| 460 | 0 | if (content[0] == '$') { |
| 461 | 0 | return get_variable() |
| 462 | } | |
| 463 | // globals | |
| 464 | 0 | if (content[0] == '@') { |
| 465 | 0 | content = content.substr(1) |
| 466 | 0 | var id = get_identifier() |
| 467 | 0 | var ge = {type: 'GlobalsExpression', id: id} |
| 468 | 0 | return ge |
| 469 | } | |
| 470 | 0 | return get_identifier() |
| 471 | } | |
| 472 | ||
| 473 | 1 | function get_variable() { |
| 474 | 0 | content = content.substr(1) |
| 475 | 0 | var id = get_identifier() |
| 476 | 0 | var ve = {type: 'VariableExpression', id: id} |
| 477 | 0 | return ve |
| 478 | } | |
| 479 | ||
| 480 | 1 | function get_property_expression(idref, ws_post_id) { |
| 481 | 0 | var d= content[0] |
| 482 | 0 | if (d == '[') { |
| 483 | 0 | content = content.substr(1) |
| 484 | 0 | get_ws() |
| 485 | 0 | var exp = get_member_expression() |
| 486 | 0 | get_ws() |
| 487 | 0 | if (content[0] != ']') { |
| 488 | 0 | throw "ParserError in get_property_expression" |
| 489 | } | |
| 490 | 0 | content = content.substr(1) |
| 491 | 0 | var prop = { |
| 492 | 'type': 'PropertyExpression', | |
| 493 | 'expression': idref, | |
| 494 | 'property': exp, | |
| 495 | 'computed': true | |
| 496 | } | |
| 497 | 0 | return prop |
| 498 | 0 | } else if (d == '.') { |
| 499 | 0 | content = content.substr(1) |
| 500 | 0 | var prop = get_identifier() |
| 501 | 0 | var pe = { |
| 502 | 'type': 'PropertyExpression', | |
| 503 | 'expression': idref, | |
| 504 | 'property': prop, | |
| 505 | 'computed': false | |
| 506 | } | |
| 507 | 0 | return pe |
| 508 | } else { | |
| 509 | 0 | throw "ParserError in get_property_expression" |
| 510 | } | |
| 511 | } | |
| 512 | ||
| 513 | 1 | function get_call_expression(callee, ws_post_id) { |
| 514 | 0 | var mcall = { |
| 515 | 'type': 'CallExpression', | |
| 516 | 'callee': callee, | |
| 517 | 'arguments': [], | |
| 518 | } | |
| 519 | 0 | content = content.substr(1) |
| 520 | 0 | get_ws() |
| 521 | 0 | if (content[0] == ')') { |
| 522 | 0 | content = content.substr(1) |
| 523 | 0 | return mcall |
| 524 | } | |
| 525 | 0 | while (1) { |
| 526 | 0 | var exp = get_expression() |
| 527 | 0 | mcall.arguments.push(exp) |
| 528 | 0 | get_ws() |
| 529 | 0 | if (content[0] == ',') { |
| 530 | 0 | content = content.substr(1) |
| 531 | 0 | get_ws() |
| 532 | 0 | } else if (content[0] == ')') { |
| 533 | 0 | break |
| 534 | } else { | |
| 535 | 0 | throw "ParserError in get_call_expression" |
| 536 | } | |
| 537 | } | |
| 538 | 0 | content = content.substr(1) |
| 539 | 0 | return mcall |
| 540 | } | |
| 541 | ||
| 542 | 1 | function get_comment() { |
| 543 | 0 | var pos = content.indexOf('*/') |
| 544 | 0 | if (pos === -1) { |
| 545 | 0 | throw "ParserError in get_comment" |
| 546 | } | |
| 547 | 0 | var c = content.substr(2, pos-2) |
| 548 | 0 | content = content.substr(pos+2) |
| 549 | 0 | return { |
| 550 | 'type': 'Comment', | |
| 551 | 'content': c | |
| 552 | } | |
| 553 | } | |
| 554 | ||
| 555 | }).call(this); |
| Line | Hits | Source |
|---|---|---|
| 1 | // This is L20n's on-the-fly compiler. It takes the AST produced by the parser | |
| 2 | // and uses it to create a set of JavaScript objects and functions representing | |
| 3 | // entities and macros and other expressions. | |
| 4 | // | |
| 5 | // The module defines a `Compiler` singleton with a single method: `compile`. | |
| 6 | // The result of the compilation is stored on the `entries` object passed as | |
| 7 | // the second argument to the `compile` function. The third argument is | |
| 8 | // `globals`, an object whose properties provide information about the runtime | |
| 9 | // environment, e.g., the current hour, operating system etc. | |
| 10 | // | |
| 11 | // Main concepts | |
| 12 | // ------------- | |
| 13 | // | |
| 14 | // **Entities** and **attributes** are objects which are publicly available. | |
| 15 | // Their `toString` method is designed to be used by the L20n context to get | |
| 16 | // a string value of the entity, given the context data passed to the method. | |
| 17 | // | |
| 18 | // All other symbols defined by the grammar are implemented as expression | |
| 19 | // functions. The naming convention is: | |
| 20 | // | |
| 21 | // - capitalized first letters denote **expressions constructors**, e.g. | |
| 22 | // `PropertyExpression`. | |
| 23 | // - camel-case denotes **expression functions** returned by the | |
| 24 | // constructors, e.g. `propertyExpression`. | |
| 25 | // | |
| 26 | // ### Constructors | |
| 27 | // | |
| 28 | // The constructor is called for every node in the AST. It stores the | |
| 29 | // components of the expression which are constant and do not depend on the | |
| 30 | // calling context (an example of the latter would be the data passed by the | |
| 31 | // developer to the `toString` method). | |
| 32 | // | |
| 33 | // ### Expression functions | |
| 34 | // | |
| 35 | // The constructor, when called, returns an expression function, which, in | |
| 36 | // turn, is called every time the expression needs to be evaluated. The | |
| 37 | // evaluation call is context-dependend. Every expression function takes three | |
| 38 | // mandatory arguments and one optional one: | |
| 39 | // | |
| 40 | // - `locals`, which stores the information about the currently evaluated | |
| 41 | // entity (`locals.__this__`). It also stores the arguments passed to macros. | |
| 42 | // - `env`, which combines `entries` (all other entities and macros) and | |
| 43 | // `globals` passed to `Compiler.compile`. | |
| 44 | // - `data`, which is an object with data passed to the context by the | |
| 45 | // developer. The developer can define data on the context, or pass it on | |
| 46 | // a per-call basis. | |
| 47 | // - `key` (optional), which is a number or a string passed to an | |
| 48 | // `ArrayLiteral` or a `HashLiteral` expression denoting the member of the | |
| 49 | // array or the hash to return. The member will be another expression function | |
| 50 | // which can then be evaluated further. | |
| 51 | // | |
| 52 | // | |
| 53 | // Bubbling up the new _current_ entity | |
| 54 | // ------------------------------------ | |
| 55 | // | |
| 56 | // Every expression function returns an array [`newLocals`, `evaluatedValue`]. | |
| 57 | // The reason for this, and in particular for returning `newLocals`, is | |
| 58 | // important for understanding how the compiler works. | |
| 59 | // | |
| 60 | // In most of the cases. `newLocals` will be the same as the original `locals` | |
| 61 | // passed to the expression function during the evaluation call. In some | |
| 62 | // cases, however, `newLocals.__this__` will reference a different entity than | |
| 63 | // `locals.__this__` did. On runtime, as the compiler traverses the AST and | |
| 64 | // goes deeper into individual branches, when it hits an `identifier` and | |
| 65 | // evaluates it to an entity, it needs to **bubble up** this find back to the | |
| 66 | // top expressions in the chain. This is so that the evaluation of the | |
| 67 | // top-most expressions in the branch (root being at the very top of the tree) | |
| 68 | // takes into account the new value of `__this__`. | |
| 69 | // | |
| 70 | // To illustrate this point, consider the following example. | |
| 71 | // | |
| 72 | // Two entities, `brandName` and `about` are defined as such: | |
| 73 | // | |
| 74 | // <brandName { | |
| 75 | // short: "Firefox", | |
| 76 | // long: "Mozilla {{ ~ }}" | |
| 77 | // }> | |
| 78 | // <about "About {{ brandName.long }}"> | |
| 79 | // | |
| 80 | // Notice two `complexString`s: `about` references `brandName.long`, and | |
| 81 | // `brandName.long` references its own entity via `~`. This `~` (meaning, the | |
| 82 | // current entity) must always reference `brandName`, even when called from | |
| 83 | // `about`. | |
| 84 | // | |
| 85 | // The AST for the `about` entity looks like this: | |
| 86 | // | |
| 87 | // [Entity] | |
| 88 | // .id[Identifier] | |
| 89 | // .name[unicode "about"] | |
| 90 | // .index | |
| 91 | // .value[ComplexString] <1> | |
| 92 | // .content | |
| 93 | // [String] <2> | |
| 94 | // .content[unicode "About "] | |
| 95 | // [PropertyExpression] <3> | |
| 96 | // .expression[Identifier] <4> | |
| 97 | // .name[unicode "brandName"] | |
| 98 | // .property[Identifier] | |
| 99 | // .name[unicode "long"] | |
| 100 | // .computed[bool=False] | |
| 101 | // .attrs | |
| 102 | // .local[bool=False] | |
| 103 | // | |
| 104 | // During the compilation the compiler will walk the AST top-down to the | |
| 105 | // deepest terminal leaves and will use expression constructors to create | |
| 106 | // expression functions for the components. For instance, for `about`'s value, | |
| 107 | // the compiler will call `ComplexString()` to create an expression function | |
| 108 | // `complexString` <1> which will be assigned to the entity's value. The | |
| 109 | // `ComplexString` construtor, before it returns the `complexString` <1>, will | |
| 110 | // in turn call other expression constructors to create `content`: | |
| 111 | // a `stringLiteral` and a `propertyExpression`. The `PropertyExpression` | |
| 112 | // contructor will do the same, etc... | |
| 113 | // | |
| 114 | // When `entity.toString(ctxdata)` is called by a third-party code, we need to | |
| 115 | // resolve the whole `complexString` <1> to return a single string value. This | |
| 116 | // is what **resolving** means and it involves some recursion. On the other | |
| 117 | // hand, **evaluating** means _to call the expression once and use what it | |
| 118 | // returns_. | |
| 119 | // | |
| 120 | // `toString` sets `locals.__this__` to the current entity, `about` and tells | |
| 121 | // the `complexString` <1> to _resolve_ itself. | |
| 122 | // | |
| 123 | // In order to resolve the `complexString` <1>, we start by resolving its first | |
| 124 | // member <2> to a string. As we resolve deeper down, we bubble down `locals` | |
| 125 | // set by `toString`. The first member of `content` turns out to simply be | |
| 126 | // a string that reads `About `. | |
| 127 | // | |
| 128 | // On to the second member, the propertyExpression <3>. We bubble down | |
| 129 | // `locals` again and proceed to evaluate the `expression` field, which is an | |
| 130 | // `identifier`. Note that we don't _resolve_ it to a string; we _evaluate_ it | |
| 131 | // to something that can be further used in other expressions, in this case, an | |
| 132 | // **entity** called `brandName`. | |
| 133 | // | |
| 134 | // Had we _resolved_ the `propertyExpression`, it would have resolve to | |
| 135 | // a string, and it would have been impossible to access the `long` member. | |
| 136 | // This leads us to an important concept: the compiler _resolves_ expressions | |
| 137 | // when it expects a primitive value (a string, a number, a bool). On the | |
| 138 | // other hand, it _evaluates_ expressions (calls them only once) when it needs | |
| 139 | // to work with them further, e.g. in order to access a member of the hash. | |
| 140 | // | |
| 141 | // This also explains why in the above example, once the compiler hits the | |
| 142 | // `brandName` identifier and changes the value of `locals.__this__` to the | |
| 143 | // `brandName` entity, this value doesn't bubble up all the way up to the | |
| 144 | // `about` entity. All components of any `complexString` are _resolved_ by | |
| 145 | // the compiler until a primitive value is returned. This logic lives in the | |
| 146 | // `_resolve` function. | |
| 147 | ||
| 148 | // | |
| 149 | // Inline comments | |
| 150 | // --------------- | |
| 151 | // | |
| 152 | // Isolate the code by using an immediately-invoked function expression. | |
| 153 | // Invoke it via `(function(){ ... }).call(this)` so that inside of the IIFE, | |
| 154 | // `this` references the global object. | |
| 155 | 1 | (function() { |
| 156 | 1 | 'use strict'; |
| 157 | ||
| 158 | 1 | var Compiler; |
| 159 | ||
| 160 | // Depending on the environment the script is run in, define `Compiler` as | |
| 161 | // the exports object which can be `required` as a module, or as a member of | |
| 162 | // the L20n object defined on the global object in the browser, i.e. | |
| 163 | // `window`. | |
| 164 | 1 | if (typeof exports !== 'undefined') { |
| 165 | 1 | Compiler = exports; |
| 166 | } else { | |
| 167 | 0 | Compiler = this.L20n.Compiler = {}; |
| 168 | } | |
| 169 | ||
| 170 | // `Compiler.compile` is the only publicly visible method. It takes three | |
| 171 | // arguments: `ast`, the AST produced by the parser; `entries`, an object | |
| 172 | // which will be populted with compiled entities and macros (their `id`s will | |
| 173 | // be used asthe keys of the `entries` object; and `globals`, an object | |
| 174 | // whose properties can be accessed to get information about the runtime | |
| 175 | // environment. | |
| 176 | 1 | Compiler.compile = function compile(ast, entries, globals) { |
| 177 | // `entries` and `globals` are grouped into an `env` object throught the | |
| 178 | // file | |
| 179 | 10 | var env = { |
| 180 | entries: entries, | |
| 181 | globals: globals, | |
| 182 | }; | |
| 183 | 10 | for (var i = 0, entry; entry = ast[i]; i++) { |
| 184 | 21 | if (entry.type == 'Entity') { |
| 185 | 21 | env.entries[entry.id.name] = new Entity(entry, env); |
| 186 | 0 | } else if (entry.type == 'Macro') { |
| 187 | 0 | env.entries[entry.id.name] = new Macro(entry); |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | // The Entity object. | |
| 193 | 1 | function Entity(node, env) { |
| 194 | 21 | this.id = node.id; |
| 195 | 21 | this.value = Expression(node.value); |
| 196 | 21 | this.index = []; |
| 197 | 21 | node.index.forEach(function(ind) { |
| 198 | 0 | this.index.push(Expression(ind)); |
| 199 | }, this); | |
| 200 | 21 | this.attributes = {}; |
| 201 | 21 | for (var key in node.attrs) { |
| 202 | 0 | this.attributes[key] = new Attribute(node.attrs[key], this); |
| 203 | } | |
| 204 | 21 | this.local = node.local || false; |
| 205 | 21 | this.env = env; |
| 206 | } | |
| 207 | // Entities are wrappers around their value expression. _Yielding_ from the | |
| 208 | // entity is identical to _evaluating_ its value with the appropriate value | |
| 209 | // of `locals.__this__`. See `PropertyExpression` for an example usage. | |
| 210 | 1 | Entity.prototype._yield = function E_yield(data, key) { |
| 211 | 0 | var locals = { |
| 212 | __this__: this, | |
| 213 | }; | |
| 214 | 0 | return this.value(locals, this.env, data, key); |
| 215 | }; | |
| 216 | // Calling `entity._resolve` will _resolve_ its value to a primitive value. | |
| 217 | // See `ComplexString` for an example usage. | |
| 218 | 1 | Entity.prototype._resolve = function E_resolve(data, index) { |
| 219 | 10 | index = index || this.index; |
| 220 | 10 | var locals = { |
| 221 | __this__: this, | |
| 222 | }; | |
| 223 | 10 | return _resolve(this.value, locals, this.env, data, index); |
| 224 | }; | |
| 225 | // `toString` is the only method that is supposed to be used by the L20n's | |
| 226 | // context. | |
| 227 | 1 | Entity.prototype.toString = function toString(data) { |
| 228 | 10 | return this._resolve(data); |
| 229 | }; | |
| 230 | ||
| 231 | 1 | function Attribute(node, entity) { |
| 232 | 0 | this.key = node.key.name; |
| 233 | 0 | this.local = node.local || false; |
| 234 | 0 | this.value = Expression(node.value); |
| 235 | 0 | this.entity = entity; |
| 236 | } | |
| 237 | 1 | Attribute.prototype._yield = function A_yield(data, key) { |
| 238 | 0 | var locals = { |
| 239 | __this__: this.entity, | |
| 240 | }; | |
| 241 | 0 | return this.value(locals, this.entity.env, data, key); |
| 242 | }; | |
| 243 | 1 | Attribute.prototype._resolve = function A_resolve(data, index) { |
| 244 | 0 | index = index || this.entity.index; |
| 245 | 0 | var locals = { |
| 246 | __this__: this.entity, | |
| 247 | }; | |
| 248 | 0 | return _resolve(this.value, locals, this.entity.env, data, index); |
| 249 | }; | |
| 250 | 1 | Attribute.prototype.toString = function toString(data) { |
| 251 | 0 | return this._resolve(data); |
| 252 | }; | |
| 253 | ||
| 254 | 1 | function Macro(node) { |
| 255 | 0 | var expression = Expression(node.expression); |
| 256 | 0 | return function(locals, env, data, args) { |
| 257 | 0 | node.args.forEach(function(arg, i) { |
| 258 | 0 | locals[arg.id.name] = args[i]; |
| 259 | }); | |
| 260 | 0 | return expression(locals, env, data); |
| 261 | }; | |
| 262 | } | |
| 263 | ||
| 264 | // The 'dispatcher' expression constructor. Other expression constructors | |
| 265 | // call this to create expression functions for their components. For | |
| 266 | // instance, `ConditionalExpression` calls `Expression` to create expression | |
| 267 | // functions for its `test`, `consequent` and `alternate` symbols. | |
| 268 | 1 | function Expression(node) { |
| 269 | 21 | var EXPRESSION_TYPES = { |
| 270 | // Primary expressions. | |
| 271 | 'Identifier': Identifier, | |
| 272 | 'ThisExpression': ThisExpression, | |
| 273 | 'VariableExpression': VariableExpression, | |
| 274 | 'GlobalsExpression': GlobalsExpression, | |
| 275 | ||
| 276 | // Value expressions. | |
| 277 | 'Literal': NumberLiteral, | |
| 278 | 'String': StringLiteral, | |
| 279 | 'Array': ArrayLiteral, | |
| 280 | 'Hash': HashLiteral, | |
| 281 | 'HashItem': HashItem, | |
| 282 | 'ComplexString': ComplexString, | |
| 283 | ||
| 284 | // Logical expressions. | |
| 285 | 'UnaryExpression': UnaryExpression, | |
| 286 | 'BinaryExpression': BinaryExpression, | |
| 287 | 'LogicalExpression': LogicalExpression, | |
| 288 | 'ConditionalExpression': ConditionalExpression, | |
| 289 | ||
| 290 | // Member expressions. | |
| 291 | 'CallExpression': CallExpression, | |
| 292 | 'PropertyExpression': PropertyExpression, | |
| 293 | 'AttributeExpression': AttributeExpression, | |
| 294 | 'ParenthesisExpression': ParenthesisExpression, | |
| 295 | }; | |
| 296 | // An entity can have no value. It will be resolved to `null`. | |
| 297 | 21 | if (!node) { |
| 298 | 0 | return null; |
| 299 | } | |
| 300 | 21 | try { |
| 301 | 21 | var expr = EXPRESSION_TYPES[node.type](node); |
| 302 | } catch(e) { | |
| 303 | 0 | throw new Error('Unknown expression type'); |
| 304 | } | |
| 305 | 21 | return expr; |
| 306 | } | |
| 307 | ||
| 308 | 1 | function _resolve(expr, locals, env, data, index) { |
| 309 | // Bail out early if it's a primitive value or `null`. This is exactly | |
| 310 | // what we want. | |
| 311 | 20 | if (!expr || |
| 312 | typeof expr === 'string' || | |
| 313 | typeof expr === 'boolean' || | |
| 314 | typeof expr === 'number') { | |
| 315 | 10 | return expr; |
| 316 | } | |
| 317 | // Check if `expr` knows how to resolve itself (if it's an Entity or an | |
| 318 | // Attribute). | |
| 319 | 10 | if (expr._resolve) { |
| 320 | 0 | return expr._resolve(data, index); |
| 321 | } | |
| 322 | 10 | index = index || []; |
| 323 | 10 | var key = index.shift(); |
| 324 | // `var [locals, current] = expr(...)` is not ES5 (V8 doesn't support it) | |
| 325 | 10 | var current = expr(locals, env, data, key); |
| 326 | 10 | locals = current[0], current = current[1]; |
| 327 | 10 | return _resolve(current, locals, env, data, index); |
| 328 | } | |
| 329 | ||
| 330 | 1 | function Identifier(node) { |
| 331 | 0 | var name = node.name; |
| 332 | 0 | return function identifier(locals, env, data) { |
| 333 | 0 | var entity = env.entries[name] |
| 334 | 0 | return [{ __this__: entity }, entity] |
| 335 | }; | |
| 336 | } | |
| 337 | 1 | function ThisExpression(node) { |
| 338 | 0 | return function thisExpression(locals, env, data) { |
| 339 | 0 | return [locals, locals.__this__]; |
| 340 | }; | |
| 341 | } | |
| 342 | 1 | function VariableExpression(node) { |
| 343 | 0 | return function variableExpression(locals, env, data) { |
| 344 | 0 | var value = locals[node.id.name]; |
| 345 | 0 | if (value !== undefined) |
| 346 | 0 | return value; |
| 347 | 0 | return [locals, data[node.id.name]]; |
| 348 | }; | |
| 349 | } | |
| 350 | 1 | function GlobalsExpression(node) { |
| 351 | 0 | return function globalsExpression(locals, env, data) { |
| 352 | 0 | return [locals, env.globals[node.id.name]]; |
| 353 | }; | |
| 354 | } | |
| 355 | 1 | function NumberLiteral(node) { |
| 356 | 0 | return function numberLiteral(locals, env, data) { |
| 357 | 0 | return [locals, node.value]; |
| 358 | }; | |
| 359 | } | |
| 360 | 1 | function StringLiteral(node) { |
| 361 | 21 | return function stringLiteral(locals, env, data) { |
| 362 | 10 | return [locals, node.content]; |
| 363 | }; | |
| 364 | } | |
| 365 | 1 | function ArrayLiteral(node) { |
| 366 | 0 | var content = []; |
| 367 | 0 | node.content.forEach(function(elem, i) { |
| 368 | 0 | content.push(Expression(elem)); |
| 369 | }); | |
| 370 | 0 | return function arrayLiteral(locals, env, data, key) { |
| 371 | 0 | key = _resolve(key, locals, env, data); |
| 372 | 0 | if (key && content[key]) { |
| 373 | 0 | return [locals, content[key]]; |
| 374 | } else { | |
| 375 | // For Arrays, the default key is always 0. The syntax does not allow | |
| 376 | // specifying a different default with an asterisk, like in hashes. | |
| 377 | 0 | return [locals, content[0]]; |
| 378 | } | |
| 379 | }; | |
| 380 | } | |
| 381 | 1 | function HashLiteral(node) { |
| 382 | 0 | var content = []; |
| 383 | 0 | var defaultKey = null; |
| 384 | 0 | node.content.forEach(function(elem, i) { |
| 385 | 0 | content[elem.key.name] = HashItem(elem); |
| 386 | 0 | if (i == 0 || elem['default']) |
| 387 | 0 | defaultKey = elem.key.name; |
| 388 | }); | |
| 389 | 0 | return function hashLiteral(locals, env, data, key) { |
| 390 | 0 | key = _resolve(key, locals, env, data); |
| 391 | 0 | if (key && content[key]) { |
| 392 | 0 | return [locals, content[key]]; |
| 393 | } else { | |
| 394 | 0 | return [locals, content[defaultKey]]; |
| 395 | } | |
| 396 | }; | |
| 397 | } | |
| 398 | 1 | function HashItem(node) { |
| 399 | // return the value expression right away | |
| 400 | // the `key` and the `default` flag logic is done in `HashLiteral` | |
| 401 | 0 | return Expression(node.value) |
| 402 | } | |
| 403 | 1 | function ComplexString(node) { |
| 404 | 0 | var content = []; |
| 405 | 0 | node.content.forEach(function(elem) { |
| 406 | 0 | content.push(Expression(elem)); |
| 407 | }); | |
| 408 | // Every complexString needs to have its own `dirty` flag whose state | |
| 409 | // persists across multiple calls to the given complexString. On the other | |
| 410 | // hand, `dirty` must not be shared by all complexStrings. Hence the need | |
| 411 | // to define `dirty` as a variable available in the closure. Note that the | |
| 412 | // anonymous function is a self-invoked one and it returns the closure | |
| 413 | // immediately. | |
| 414 | 0 | return function() { |
| 415 | 0 | var dirty = false; |
| 416 | 0 | return function complexString(locals, env, data) { |
| 417 | 0 | if (dirty) { |
| 418 | 0 | throw new Error("Cyclic reference detected"); |
| 419 | } | |
| 420 | 0 | dirty = true; |
| 421 | 0 | var parts = []; |
| 422 | 0 | content.forEach(function resolveElemOfComplexString(elem) { |
| 423 | 0 | var part = _resolve(elem, locals, env, data); |
| 424 | 0 | parts.push(part); |
| 425 | }); | |
| 426 | 0 | dirty = false; |
| 427 | 0 | return [locals, parts.join('')]; |
| 428 | } | |
| 429 | }(); | |
| 430 | } | |
| 431 | ||
| 432 | 1 | function UnaryOperator(token) { |
| 433 | 0 | if (token == '-') return function negativeOperator(argument) { |
| 434 | 0 | return -argument; |
| 435 | }; | |
| 436 | 0 | if (token == '+') return function positiveOperator(argument) { |
| 437 | 0 | return +argument; |
| 438 | }; | |
| 439 | 0 | if (token == '!') return function notOperator(argument) { |
| 440 | 0 | return !argument; |
| 441 | }; | |
| 442 | 0 | throw new Error("Unknown token: " + token); |
| 443 | } | |
| 444 | 1 | function BinaryOperator(token) { |
| 445 | 0 | if (token == '==') return function equalOperator(left, right) { |
| 446 | 0 | return left == right; |
| 447 | }; | |
| 448 | 0 | if (token == '!=') return function notEqualOperator(left, right) { |
| 449 | 0 | return left != right; |
| 450 | }; | |
| 451 | 0 | if (token == '<') return function lessThanOperator(left, right) { |
| 452 | 0 | return left < right; |
| 453 | }; | |
| 454 | 0 | if (token == '<=') return function lessThanEqualOperator(left, right) { |
| 455 | 0 | return left <= right; |
| 456 | }; | |
| 457 | 0 | if (token == '>') return function greaterThanOperator(left, right) { |
| 458 | 0 | return left > right; |
| 459 | }; | |
| 460 | 0 | if (token == '>=') return function greaterThanEqualOperator(left, right) { |
| 461 | 0 | return left >= right; |
| 462 | }; | |
| 463 | 0 | if (token == '+') return function addOperator(left, right) { |
| 464 | 0 | return left + right; |
| 465 | }; | |
| 466 | 0 | if (token == '-') return function substractOperator(left, right) { |
| 467 | 0 | return left - right; |
| 468 | }; | |
| 469 | 0 | if (token == '*') return function multiplyOperator(left, right) { |
| 470 | 0 | return left * right; |
| 471 | }; | |
| 472 | 0 | if (token == '/') return function devideOperator(left, right) { |
| 473 | 0 | return left / right; |
| 474 | }; | |
| 475 | 0 | if (token == '%') return function moduloOperator(left, right) { |
| 476 | 0 | return left % right; |
| 477 | }; | |
| 478 | 0 | throw new Error("Unknown token: " + token); |
| 479 | } | |
| 480 | 1 | function LogicalOperator(token) { |
| 481 | 0 | if (token == '&&') return function andOperator(left, right) { |
| 482 | 0 | return left && right; |
| 483 | }; | |
| 484 | 0 | if (token == '||') return function orOperator(left, right) { |
| 485 | 0 | return left || right; |
| 486 | }; | |
| 487 | 0 | throw new Error("Unknown token: " + token); |
| 488 | } | |
| 489 | 1 | function UnaryExpression(node) { |
| 490 | 0 | var operator = UnaryOperator(node.operator.token); |
| 491 | 0 | var argument = Expression(node.argument); |
| 492 | 0 | return function unaryExpression(locals, env, data) { |
| 493 | 0 | return [locals, operator(_resolve(argument, locals, env, data))]; |
| 494 | }; | |
| 495 | } | |
| 496 | 1 | function BinaryExpression(node) { |
| 497 | 0 | var left = Expression(node.left); |
| 498 | 0 | var operator = BinaryOperator(node.operator.token); |
| 499 | 0 | var right = Expression(node.right); |
| 500 | 0 | return function binaryExpression(locals, env, data) { |
| 501 | 0 | return [locals, operator( |
| 502 | _resolve(left, locals, env, data), | |
| 503 | _resolve(right, locals, env, data) | |
| 504 | )]; | |
| 505 | }; | |
| 506 | } | |
| 507 | 1 | function LogicalExpression(node) { |
| 508 | 0 | var left = Expression(node.left); |
| 509 | 0 | var operator = LogicalOperator(node.operator.token); |
| 510 | 0 | var right = Expression(node.right); |
| 511 | 0 | return function logicalExpression(locals, env, data) { |
| 512 | 0 | return [locals, operator( |
| 513 | _resolve(left, locals, env, data), | |
| 514 | _resolve(right, locals, env, data) | |
| 515 | )]; | |
| 516 | } | |
| 517 | } | |
| 518 | 1 | function ConditionalExpression(node) { |
| 519 | 0 | var test = Expression(node.test); |
| 520 | 0 | var consequent = Expression(node.consequent); |
| 521 | 0 | var alternate = Expression(node.alternate); |
| 522 | 0 | return function conditionalExpression(locals, env, data) { |
| 523 | 0 | if (_resolve(test, locals, env, data)) { |
| 524 | 0 | return consequent(locals, env, data); |
| 525 | } | |
| 526 | 0 | return alternate(locals, env, data); |
| 527 | }; | |
| 528 | } | |
| 529 | ||
| 530 | 1 | function CallExpression(node) { |
| 531 | 0 | var callee = Expression(node.callee); |
| 532 | 0 | var args = []; |
| 533 | 0 | node.arguments.forEach(function(elem, i) { |
| 534 | 0 | args.push(Expression(elem)); |
| 535 | }); | |
| 536 | 0 | return function callExpression(locals, env, data) { |
| 537 | 0 | var evaluated_args = []; |
| 538 | 0 | args.forEach(function(arg, i) { |
| 539 | 0 | evaluated_args.push(arg(locals, env, data)); |
| 540 | }); | |
| 541 | // callee is an expression pointing to a macro, e.g. an identifier | |
| 542 | // XXX what if it doesn't point to a macro? | |
| 543 | 0 | var macro = callee(locals, env, data); |
| 544 | 0 | locals = macro[0], macro = macro[1]; |
| 545 | // rely entirely on the platform implementation to detect recursion | |
| 546 | 0 | return macro(locals, env, data, evaluated_args); |
| 547 | }; | |
| 548 | } | |
| 549 | 1 | function PropertyExpression(node) { |
| 550 | 0 | var expression = Expression(node.expression); |
| 551 | 0 | var property = node.computed ? |
| 552 | Expression(node.property) : | |
| 553 | node.property.name; | |
| 554 | 0 | return function propertyExpression(locals, env, data) { |
| 555 | 0 | var prop = _resolve(property, locals, env, data); |
| 556 | 0 | var parent = expression(locals, env, data); |
| 557 | 0 | locals = parent[0], parent = parent[1]; |
| 558 | // If `parent` is an Entity or an Attribute, evaluate its value via the | |
| 559 | // `_yield` method. This will ensure the correct value of | |
| 560 | // `locals.__this__`. | |
| 561 | 0 | if (parent._yield) { |
| 562 | 0 | return parent._yield(data, prop); |
| 563 | } | |
| 564 | // If `parent` is an object passed by the developer to the context (i.e., | |
| 565 | // `expression` was a `VariableExpression`), simply return the member of | |
| 566 | // the object corresponding to `prop`. We don't really care about | |
| 567 | // `locals` here. | |
| 568 | 0 | if (typeof parent !== 'function') { |
| 569 | 0 | return [locals, parent[prop]]; |
| 570 | } | |
| 571 | 0 | return parent(locals, env, data, prop); |
| 572 | } | |
| 573 | } | |
| 574 | 1 | function AttributeExpression(node) { |
| 575 | // XXX looks similar to PropertyExpression, but it's actually closer to | |
| 576 | // Identifier | |
| 577 | 0 | var expression = Expression(node.expression); |
| 578 | 0 | var attribute = node.computed ? |
| 579 | Expression(node.attribute) : | |
| 580 | node.attribute.name; | |
| 581 | 0 | return function attributeExpression(locals, env, data) { |
| 582 | 0 | var attr = _resolve(attribute, locals, env, data); |
| 583 | 0 | var entity = expression(locals, env, data); |
| 584 | 0 | locals = entity[0], entity = entity[1]; |
| 585 | // XXX what if it's not an entity? | |
| 586 | 0 | return [locals, entity.attributes[attr]]; |
| 587 | } | |
| 588 | } | |
| 589 | 1 | function ParenthesisExpression(node) { |
| 590 | 0 | return Expression(node.expression); |
| 591 | } | |
| 592 | ||
| 593 | }).call(this); |
| Line | Hits | Source |
|---|---|---|
| 1 | // This is L20n's on-the-fly compiler. It takes the AST produced by the parser | |
| 2 | // and uses it to create a set of JavaScript objects and functions representing | |
| 3 | // entities and macros and other expressions. | |
| 4 | // | |
| 5 | // The module defines a `Compiler` singleton with a single method: `compile`. | |
| 6 | // The result of the compilation is stored on the `entries` object passed as | |
| 7 | // the second argument to the `compile` function. The third argument is | |
| 8 | // `globals`, an object whose properties provide information about the runtime | |
| 9 | // environment, e.g., the current hour, operating system etc. | |
| 10 | // | |
| 11 | // Main concepts | |
| 12 | // ------------- | |
| 13 | // | |
| 14 | // **Entities** and **attributes** are objects which are publicly available. | |
| 15 | // Their `toString` method is designed to be used by the L20n context to get | |
| 16 | // a string value of the entity, given the context data passed to the method. | |
| 17 | // | |
| 18 | // All other symbols defined by the grammar are implemented as expression | |
| 19 | // functions. The naming convention is: | |
| 20 | // | |
| 21 | // - capitalized first letters denote **expressions constructors**, e.g. | |
| 22 | // `PropertyExpression`. | |
| 23 | // - camel-case denotes **expression functions** returned by the | |
| 24 | // constructors, e.g. `propertyExpression`. | |
| 25 | // | |
| 26 | // ### Constructors | |
| 27 | // | |
| 28 | // The constructor is called for every node in the AST. It stores the | |
| 29 | // components of the expression which are constant and do not depend on the | |
| 30 | // calling context (an example of the latter would be the data passed by the | |
| 31 | // developer to the `toString` method). | |
| 32 | // | |
| 33 | // ### Expression functions | |
| 34 | // | |
| 35 | // The constructor, when called, returns an expression function, which, in | |
| 36 | // turn, is called every time the expression needs to be evaluated. The | |
| 37 | // evaluation call is context-dependend. Every expression function takes three | |
| 38 | // mandatory arguments and one optional one: | |
| 39 | // | |
| 40 | // - `locals`, which stores the information about the currently evaluated | |
| 41 | // entity (`locals.__this__`). It also stores the arguments passed to macros. | |
| 42 | // - `env`, which combines `entries` (all other entities and macros) and | |
| 43 | // `globals` passed to `Compiler.compile`. | |
| 44 | // - `data`, which is an object with data passed to the context by the | |
| 45 | // developer. The developer can define data on the context, or pass it on | |
| 46 | // a per-call basis. | |
| 47 | // - `key` (optional), which is a number or a string passed to an | |
| 48 | // `ArrayLiteral` or a `HashLiteral` expression denoting the member of the | |
| 49 | // array or the hash to return. The member will be another expression function | |
| 50 | // which can then be evaluated further. | |
| 51 | // | |
| 52 | // | |
| 53 | // Bubbling up the new _current_ entity | |
| 54 | // ------------------------------------ | |
| 55 | // | |
| 56 | // Every expression function returns an array [`newLocals`, `evaluatedValue`]. | |
| 57 | // The reason for this, and in particular for returning `newLocals`, is | |
| 58 | // important for understanding how the compiler works. | |
| 59 | // | |
| 60 | // In most of the cases. `newLocals` will be the same as the original `locals` | |
| 61 | // passed to the expression function during the evaluation call. In some | |
| 62 | // cases, however, `newLocals.__this__` will reference a different entity than | |
| 63 | // `locals.__this__` did. On runtime, as the compiler traverses the AST and | |
| 64 | // goes deeper into individual branches, when it hits an `identifier` and | |
| 65 | // evaluates it to an entity, it needs to **bubble up** this find back to the | |
| 66 | // top expressions in the chain. This is so that the evaluation of the | |
| 67 | // top-most expressions in the branch (root being at the very top of the tree) | |
| 68 | // takes into account the new value of `__this__`. | |
| 69 | // | |
| 70 | // To illustrate this point, consider the following example. | |
| 71 | // | |
| 72 | // Two entities, `brandName` and `about` are defined as such: | |
| 73 | // | |
| 74 | // <brandName { | |
| 75 | // short: "Firefox", | |
| 76 | // long: "Mozilla {{ ~ }}" | |
| 77 | // }> | |
| 78 | // <about "About {{ brandName.long }}"> | |
| 79 | // | |
| 80 | // Notice two `complexString`s: `about` references `brandName.long`, and | |
| 81 | // `brandName.long` references its own entity via `~`. This `~` (meaning, the | |
| 82 | // current entity) must always reference `brandName`, even when called from | |
| 83 | // `about`. | |
| 84 | // | |
| 85 | // The AST for the `about` entity looks like this: | |
| 86 | // | |
| 87 | // [Entity] | |
| 88 | // .id[Identifier] | |
| 89 | // .name[unicode "about"] | |
| 90 | // .index | |
| 91 | // .value[ComplexString] <1> | |
| 92 | // .content | |
| 93 | // [String] <2> | |
| 94 | // .content[unicode "About "] | |
| 95 | // [PropertyExpression] <3> | |
| 96 | // .expression[Identifier] <4> | |
| 97 | // .name[unicode "brandName"] | |
| 98 | // .property[Identifier] | |
| 99 | // .name[unicode "long"] | |
| 100 | // .computed[bool=False] | |
| 101 | // .attrs | |
| 102 | // .local[bool=False] | |
| 103 | // | |
| 104 | // During the compilation the compiler will walk the AST top-down to the | |
| 105 | // deepest terminal leaves and will use expression constructors to create | |
| 106 | // expression functions for the components. For instance, for `about`'s value, | |
| 107 | // the compiler will call `ComplexString()` to create an expression function | |
| 108 | // `complexString` <1> which will be assigned to the entity's value. The | |
| 109 | // `ComplexString` construtor, before it returns the `complexString` <1>, will | |
| 110 | // in turn call other expression constructors to create `content`: | |
| 111 | // a `stringLiteral` and a `propertyExpression`. The `PropertyExpression` | |
| 112 | // contructor will do the same, etc... | |
| 113 | // | |
| 114 | // When `entity.toString(ctxdata)` is called by a third-party code, we need to | |
| 115 | // resolve the whole `complexString` <1> to return a single string value. This | |
| 116 | // is what **resolving** means and it involves some recursion. On the other | |
| 117 | // hand, **evaluating** means _to call the expression once and use what it | |
| 118 | // returns_. | |
| 119 | // | |
| 120 | // `toString` sets `locals.__this__` to the current entity, `about` and tells | |
| 121 | // the `complexString` <1> to _resolve_ itself. | |
| 122 | // | |
| 123 | // In order to resolve the `complexString` <1>, we start by resolving its first | |
| 124 | // member <2> to a string. As we resolve deeper down, we bubble down `locals` | |
| 125 | // set by `toString`. The first member of `content` turns out to simply be | |
| 126 | // a string that reads `About `. | |
| 127 | // | |
| 128 | // On to the second member, the propertyExpression <3>. We bubble down | |
| 129 | // `locals` again and proceed to evaluate the `expression` field, which is an | |
| 130 | // `identifier`. Note that we don't _resolve_ it to a string; we _evaluate_ it | |
| 131 | // to something that can be further used in other expressions, in this case, an | |
| 132 | // **entity** called `brandName`. | |
| 133 | // | |
| 134 | // Had we _resolved_ the `propertyExpression`, it would have resolve to | |
| 135 | // a string, and it would have been impossible to access the `long` member. | |
| 136 | // This leads us to an important concept: the compiler _resolves_ expressions | |
| 137 | // when it expects a primitive value (a string, a number, a bool). On the | |
| 138 | // other hand, it _evaluates_ expressions (calls them only once) when it needs | |
| 139 | // to work with them further, e.g. in order to access a member of the hash. | |
| 140 | // | |
| 141 | // This also explains why in the above example, once the compiler hits the | |
| 142 | // `brandName` identifier and changes the value of `locals.__this__` to the | |
| 143 | // `brandName` entity, this value doesn't bubble up all the way up to the | |
| 144 | // `about` entity. All components of any `complexString` are _resolved_ by | |
| 145 | // the compiler until a primitive value is returned. This logic lives in the | |
| 146 | // `_resolve` function. | |
| 147 | ||
| 148 | // | |
| 149 | // Inline comments | |
| 150 | // --------------- | |
| 151 | // | |
| 152 | // Isolate the code by using an immediately-invoked function expression. | |
| 153 | // Invoke it via `(function(){ ... }).call(this)` so that inside of the IIFE, | |
| 154 | // `this` references the global object. | |
| 155 | 1 | (function() { |
| 156 | 1 | 'use strict'; |
| 157 | ||
| 158 | 1 | var Compiler; |
| 159 | ||
| 160 | // Depending on the environment the script is run in, define `Compiler` as | |
| 161 | // the exports object which can be `required` as a module, or as a member of | |
| 162 | // the L20n object defined on the global object in the browser, i.e. | |
| 163 | // `window`. | |
| 164 | 1 | if (typeof exports !== 'undefined') { |
| 165 | 1 | Compiler = exports; |
| 166 | } else { | |
| 167 | 0 | Compiler = this.L20n.Compiler = {}; |
| 168 | } | |
| 169 | ||
| 170 | // `Compiler.compile` is the only publicly visible method. It takes three | |
| 171 | // arguments: `ast`, the AST produced by the parser; `entries`, an object | |
| 172 | // which will be populted with compiled entities and macros (their `id`s will | |
| 173 | // be used asthe keys of the `entries` object; and `globals`, an object | |
| 174 | // whose properties can be accessed to get information about the runtime | |
| 175 | // environment. | |
| 176 | 1 | Compiler.compile = function compile(ast, entries, globals) { |
| 177 | // `entries` and `globals` are grouped into an `env` object throught the | |
| 178 | // file | |
| 179 | 248 | var env = { |
| 180 | entries: entries, | |
| 181 | globals: globals, | |
| 182 | }; | |
| 183 | 248 | for (var i = 0, entry; entry = ast[i]; i++) { |
| 184 | 4152 | if (entry.type == 'Entity') { |
| 185 | 3306 | env.entries[entry.id.name] = new Entity(entry, env); |
| 186 | 846 | } else if (entry.type == 'Macro') { |
| 187 | 783 | env.entries[entry.id.name] = new Macro(entry); |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | // The Entity object. | |
| 193 | 1 | function Entity(node, env) { |
| 194 | 3306 | this.id = node.id; |
| 195 | 3306 | this.value = Expression(node.value); |
| 196 | 3306 | this.index = []; |
| 197 | 3306 | node.index.forEach(function(ind) { |
| 198 | 562 | this.index.push(Expression(ind)); |
| 199 | }, this); | |
| 200 | 3306 | this.attributes = {}; |
| 201 | 3306 | for (var key in node.attrs) { |
| 202 | 108 | this.attributes[key] = new Attribute(node.attrs[key], this); |
| 203 | } | |
| 204 | 3306 | this.local = node.local || false; |
| 205 | 3306 | this.env = env; |
| 206 | } | |
| 207 | // Entities are wrappers around their value expression. _Yielding_ from the | |
| 208 | // entity is identical to _evaluating_ its value with the appropriate value | |
| 209 | // of `locals.__this__`. See `PropertyExpression` for an example usage. | |
| 210 | 1 | Entity.prototype._yield = function E_yield(data, key) { |
| 211 | 43 | var locals = { |
| 212 | __this__: this, | |
| 213 | }; | |
| 214 | 43 | return this.value(locals, this.env, data, key); |
| 215 | }; | |
| 216 | // Calling `entity._resolve` will _resolve_ its value to a primitive value. | |
| 217 | // See `ComplexString` for an example usage. | |
| 218 | 1 | Entity.prototype._resolve = function E_resolve(data, index) { |
| 219 | 268 | index = index || this.index; |
| 220 | 268 | var locals = { |
| 221 | __this__: this, | |
| 222 | }; | |
| 223 | 268 | return _resolve(this.value, locals, this.env, data, index); |
| 224 | }; | |
| 225 | // `toString` is the only method that is supposed to be used by the L20n's | |
| 226 | // context. | |
| 227 | 1 | Entity.prototype.toString = function toString(data) { |
| 228 | 107 | return this._resolve(data); |
| 229 | }; | |
| 230 | ||
| 231 | 1 | function Attribute(node, entity) { |
| 232 | 108 | this.key = node.key.name; |
| 233 | 108 | this.local = node.local || false; |
| 234 | 108 | this.value = Expression(node.value); |
| 235 | 108 | this.entity = entity; |
| 236 | } | |
| 237 | 1 | Attribute.prototype._yield = function A_yield(data, key) { |
| 238 | 4 | var locals = { |
| 239 | __this__: this.entity, | |
| 240 | }; | |
| 241 | 4 | return this.value(locals, this.entity.env, data, key); |
| 242 | }; | |
| 243 | 1 | Attribute.prototype._resolve = function A_resolve(data, index) { |
| 244 | 6 | index = index || this.entity.index; |
| 245 | 6 | var locals = { |
| 246 | __this__: this.entity, | |
| 247 | }; | |
| 248 | 6 | return _resolve(this.value, locals, this.entity.env, data, index); |
| 249 | }; | |
| 250 | 1 | Attribute.prototype.toString = function toString(data) { |
| 251 | 0 | return this._resolve(data); |
| 252 | }; | |
| 253 | ||
| 254 | 1 | function Macro(node) { |
| 255 | 783 | var expression = Expression(node.expression); |
| 256 | 783 | return function(locals, env, data, args) { |
| 257 | 21970 | node.args.forEach(function(arg, i) { |
| 258 | 21974 | locals[arg.id.name] = args[i]; |
| 259 | }); | |
| 260 | 21970 | return expression(locals, env, data); |
| 261 | }; | |
| 262 | } | |
| 263 | ||
| 264 | // The 'dispatcher' expression constructor. Other expression constructors | |
| 265 | // call this to create expression functions for their components. For | |
| 266 | // instance, `ConditionalExpression` calls `Expression` to create expression | |
| 267 | // functions for its `test`, `consequent` and `alternate` symbols. | |
| 268 | 1 | function Expression(node) { |
| 269 | 23108 | var EXPRESSION_TYPES = { |
| 270 | // Primary expressions. | |
| 271 | 'Identifier': Identifier, | |
| 272 | 'ThisExpression': ThisExpression, | |
| 273 | 'VariableExpression': VariableExpression, | |
| 274 | 'GlobalsExpression': GlobalsExpression, | |
| 275 | ||
| 276 | // Value expressions. | |
| 277 | 'Literal': NumberLiteral, | |
| 278 | 'String': StringLiteral, | |
| 279 | 'Array': ArrayLiteral, | |
| 280 | 'Hash': HashLiteral, | |
| 281 | 'HashItem': HashItem, | |
| 282 | 'ComplexString': ComplexString, | |
| 283 | ||
| 284 | // Logical expressions. | |
| 285 | 'UnaryExpression': UnaryExpression, | |
| 286 | 'BinaryExpression': BinaryExpression, | |
| 287 | 'LogicalExpression': LogicalExpression, | |
| 288 | 'ConditionalExpression': ConditionalExpression, | |
| 289 | ||
| 290 | // Member expressions. | |
| 291 | 'CallExpression': CallExpression, | |
| 292 | 'PropertyExpression': PropertyExpression, | |
| 293 | 'AttributeExpression': AttributeExpression, | |
| 294 | 'ParenthesisExpression': ParenthesisExpression, | |
| 295 | }; | |
| 296 | // An entity can have no value. It will be resolved to `null`. | |
| 297 | 23108 | if (!node) { |
| 298 | 0 | return null; |
| 299 | } | |
| 300 | 23108 | try { |
| 301 | 23108 | var expr = EXPRESSION_TYPES[node.type](node); |
| 302 | } catch(e) { | |
| 303 | 0 | throw new Error('Unknown expression type'); |
| 304 | } | |
| 305 | 23108 | return expr; |
| 306 | } | |
| 307 | ||
| 308 | 1 | function _resolve(expr, locals, env, data, index) { |
| 309 | // Bail out early if it's a primitive value or `null`. This is exactly | |
| 310 | // what we want. | |
| 311 | 372178 | if (!expr || |
| 312 | typeof expr === 'string' || | |
| 313 | typeof expr === 'boolean' || | |
| 314 | typeof expr === 'number') { | |
| 315 | 186084 | return expr; |
| 316 | } | |
| 317 | // Check if `expr` knows how to resolve itself (if it's an Entity or an | |
| 318 | // Attribute). | |
| 319 | 186094 | if (expr._resolve) { |
| 320 | 26 | return expr._resolve(data, index); |
| 321 | } | |
| 322 | 186068 | index = index || []; |
| 323 | 186068 | var key = index.shift(); |
| 324 | // `var [locals, current] = expr(...)` is not ES5 (V8 doesn't support it) | |
| 325 | 186068 | var current = expr(locals, env, data, key); |
| 326 | 186055 | locals = current[0], current = current[1]; |
| 327 | 186055 | return _resolve(current, locals, env, data, index); |
| 328 | } | |
| 329 | ||
| 330 | 1 | function Identifier(node) { |
| 331 | 3254 | var name = node.name; |
| 332 | 3254 | return function identifier(locals, env, data) { |
| 333 | 22034 | var entity = env.entries[name] |
| 334 | 22034 | return [{ __this__: entity }, entity] |
| 335 | }; | |
| 336 | } | |
| 337 | 1 | function ThisExpression(node) { |
| 338 | 201 | return function thisExpression(locals, env, data) { |
| 339 | 9 | return [locals, locals.__this__]; |
| 340 | }; | |
| 341 | } | |
| 342 | 1 | function VariableExpression(node) { |
| 343 | 1436 | return function variableExpression(locals, env, data) { |
| 344 | 61717 | var value = locals[node.id.name]; |
| 345 | 61717 | if (value !== undefined) |
| 346 | 61690 | return value; |
| 347 | 27 | return [locals, data[node.id.name]]; |
| 348 | }; | |
| 349 | } | |
| 350 | 1 | function GlobalsExpression(node) { |
| 351 | 8 | return function globalsExpression(locals, env, data) { |
| 352 | 2 | return [locals, env.globals[node.id.name]]; |
| 353 | }; | |
| 354 | } | |
| 355 | 1 | function NumberLiteral(node) { |
| 356 | 2227 | return function numberLiteral(locals, env, data) { |
| 357 | 72700 | return [locals, node.value]; |
| 358 | }; | |
| 359 | } | |
| 360 | 1 | function StringLiteral(node) { |
| 361 | 5614 | return function stringLiteral(locals, env, data) { |
| 362 | 343 | return [locals, node.content]; |
| 363 | }; | |
| 364 | } | |
| 365 | 1 | function ArrayLiteral(node) { |
| 366 | 854 | var content = []; |
| 367 | 854 | node.content.forEach(function(elem, i) { |
| 368 | 1582 | content.push(Expression(elem)); |
| 369 | }); | |
| 370 | 854 | return function arrayLiteral(locals, env, data, key) { |
| 371 | 132 | key = _resolve(key, locals, env, data); |
| 372 | 132 | if (key && content[key]) { |
| 373 | 47 | return [locals, content[key]]; |
| 374 | } else { | |
| 375 | // For Arrays, the default key is always 0. The syntax does not allow | |
| 376 | // specifying a different default with an asterisk, like in hashes. | |
| 377 | 85 | return [locals, content[0]]; |
| 378 | } | |
| 379 | }; | |
| 380 | } | |
| 381 | 1 | function HashLiteral(node) { |
| 382 | 1696 | var content = []; |
| 383 | 1696 | var defaultKey = null; |
| 384 | 1696 | node.content.forEach(function(elem, i) { |
| 385 | 3486 | content[elem.key.name] = HashItem(elem); |
| 386 | 3486 | if (i == 0 || elem['default']) |
| 387 | 1810 | defaultKey = elem.key.name; |
| 388 | }); | |
| 389 | 1696 | return function hashLiteral(locals, env, data, key) { |
| 390 | 287 | key = _resolve(key, locals, env, data); |
| 391 | 287 | if (key && content[key]) { |
| 392 | 200 | return [locals, content[key]]; |
| 393 | } else { | |
| 394 | 87 | return [locals, content[defaultKey]]; |
| 395 | } | |
| 396 | }; | |
| 397 | } | |
| 398 | 1 | function HashItem(node) { |
| 399 | // return the value expression right away | |
| 400 | // the `key` and the `default` flag logic is done in `HashLiteral` | |
| 401 | 3486 | return Expression(node.value) |
| 402 | } | |
| 403 | 1 | function ComplexString(node) { |
| 404 | 2602 | var content = []; |
| 405 | 2602 | node.content.forEach(function(elem) { |
| 406 | 3364 | content.push(Expression(elem)); |
| 407 | }); | |
| 408 | // Every complexString needs to have its own `dirty` flag whose state | |
| 409 | // persists across multiple calls to the given complexString. On the other | |
| 410 | // hand, `dirty` must not be shared by all complexStrings. Hence the need | |
| 411 | // to define `dirty` as a variable available in the closure. Note that the | |
| 412 | // anonymous function is a self-invoked one and it returns the closure | |
| 413 | // immediately. | |
| 414 | 2602 | return function() { |
| 415 | 2602 | var dirty = false; |
| 416 | 2602 | return function complexString(locals, env, data) { |
| 417 | 106 | if (dirty) { |
| 418 | 4 | throw new Error("Cyclic reference detected"); |
| 419 | } | |
| 420 | 102 | dirty = true; |
| 421 | 102 | var parts = []; |
| 422 | 102 | content.forEach(function resolveElemOfComplexString(elem) { |
| 423 | 167 | var part = _resolve(elem, locals, env, data); |
| 424 | 159 | parts.push(part); |
| 425 | }); | |
| 426 | 94 | dirty = false; |
| 427 | 94 | return [locals, parts.join('')]; |
| 428 | } | |
| 429 | }(); | |
| 430 | } | |
| 431 | ||
| 432 | 1 | function UnaryOperator(token) { |
| 433 | 42 | if (token == '-') return function negativeOperator(argument) { |
| 434 | 0 | return -argument; |
| 435 | }; | |
| 436 | 42 | if (token == '+') return function positiveOperator(argument) { |
| 437 | 0 | return +argument; |
| 438 | }; | |
| 439 | 84 | if (token == '!') return function notOperator(argument) { |
| 440 | 8 | return !argument; |
| 441 | }; | |
| 442 | 0 | throw new Error("Unknown token: " + token); |
| 443 | } | |
| 444 | 1 | function BinaryOperator(token) { |
| 445 | 1148 | if (token == '==') return function equalOperator(left, right) { |
| 446 | 39655 | return left == right; |
| 447 | }; | |
| 448 | 744 | if (token == '!=') return function notEqualOperator(left, right) { |
| 449 | 0 | return left != right; |
| 450 | }; | |
| 451 | 810 | if (token == '<') return function lessThanOperator(left, right) { |
| 452 | 20 | return left < right; |
| 453 | }; | |
| 454 | 738 | if (token == '<=') return function lessThanEqualOperator(left, right) { |
| 455 | 20 | return left <= right; |
| 456 | }; | |
| 457 | 618 | if (token == '>') return function greaterThanOperator(left, right) { |
| 458 | 0 | return left > right; |
| 459 | }; | |
| 460 | 744 | if (token == '>=') return function greaterThanEqualOperator(left, right) { |
| 461 | 40 | return left >= right; |
| 462 | }; | |
| 463 | 576 | if (token == '+') return function addOperator(left, right) { |
| 464 | 10948 | return left + right; |
| 465 | }; | |
| 466 | 534 | if (token == '-') return function substractOperator(left, right) { |
| 467 | 21905 | return left - right; |
| 468 | }; | |
| 469 | 324 | if (token == '*') return function multiplyOperator(left, right) { |
| 470 | 15 | return left * right; |
| 471 | }; | |
| 472 | 240 | if (token == '/') return function devideOperator(left, right) { |
| 473 | 0 | return left / right; |
| 474 | }; | |
| 475 | 480 | if (token == '%') return function moduloOperator(left, right) { |
| 476 | 80 | return left % right; |
| 477 | }; | |
| 478 | 0 | throw new Error("Unknown token: " + token); |
| 479 | } | |
| 480 | 1 | function LogicalOperator(token) { |
| 481 | 354 | if (token == '&&') return function andOperator(left, right) { |
| 482 | 40 | return left && right; |
| 483 | }; | |
| 484 | 204 | if (token == '||') return function orOperator(left, right) { |
| 485 | 21 | return left || right; |
| 486 | }; | |
| 487 | 0 | throw new Error("Unknown token: " + token); |
| 488 | } | |
| 489 | 1 | function UnaryExpression(node) { |
| 490 | 42 | var operator = UnaryOperator(node.operator.token); |
| 491 | 42 | var argument = Expression(node.argument); |
| 492 | 42 | return function unaryExpression(locals, env, data) { |
| 493 | 8 | return [locals, operator(_resolve(argument, locals, env, data))]; |
| 494 | }; | |
| 495 | } | |
| 496 | 1 | function BinaryExpression(node) { |
| 497 | 946 | var left = Expression(node.left); |
| 498 | 946 | var operator = BinaryOperator(node.operator.token); |
| 499 | 946 | var right = Expression(node.right); |
| 500 | 946 | return function binaryExpression(locals, env, data) { |
| 501 | 72683 | return [locals, operator( |
| 502 | _resolve(left, locals, env, data), | |
| 503 | _resolve(right, locals, env, data) | |
| 504 | )]; | |
| 505 | }; | |
| 506 | } | |
| 507 | 1 | function LogicalExpression(node) { |
| 508 | 228 | var left = Expression(node.left); |
| 509 | 228 | var operator = LogicalOperator(node.operator.token); |
| 510 | 228 | var right = Expression(node.right); |
| 511 | 228 | return function logicalExpression(locals, env, data) { |
| 512 | 61 | return [locals, operator( |
| 513 | _resolve(left, locals, env, data), | |
| 514 | _resolve(right, locals, env, data) | |
| 515 | )]; | |
| 516 | } | |
| 517 | } | |
| 518 | 1 | function ConditionalExpression(node) { |
| 519 | 352 | var test = Expression(node.test); |
| 520 | 352 | var consequent = Expression(node.consequent); |
| 521 | 352 | var alternate = Expression(node.alternate); |
| 522 | 352 | return function conditionalExpression(locals, env, data) { |
| 523 | 39684 | if (_resolve(test, locals, env, data)) { |
| 524 | 10970 | return consequent(locals, env, data); |
| 525 | } | |
| 526 | 28714 | return alternate(locals, env, data); |
| 527 | }; | |
| 528 | } | |
| 529 | ||
| 530 | 1 | function CallExpression(node) { |
| 531 | 2148 | var callee = Expression(node.callee); |
| 532 | 2148 | var args = []; |
| 533 | 2148 | node.arguments.forEach(function(elem, i) { |
| 534 | 2274 | args.push(Expression(elem)); |
| 535 | }); | |
| 536 | 2148 | return function callExpression(locals, env, data) { |
| 537 | 21971 | var evaluated_args = []; |
| 538 | 21971 | args.forEach(function(arg, i) { |
| 539 | 21974 | evaluated_args.push(arg(locals, env, data)); |
| 540 | }); | |
| 541 | // callee is an expression pointing to a macro, e.g. an identifier | |
| 542 | // XXX what if it doesn't point to a macro? | |
| 543 | 21971 | var macro = callee(locals, env, data); |
| 544 | 21971 | locals = macro[0], macro = macro[1]; |
| 545 | // rely entirely on the platform implementation to detect recursion | |
| 546 | 21971 | return macro(locals, env, data, evaluated_args); |
| 547 | }; | |
| 548 | } | |
| 549 | 1 | function PropertyExpression(node) { |
| 550 | 1314 | var expression = Expression(node.expression); |
| 551 | 1314 | var property = node.computed ? |
| 552 | Expression(node.property) : | |
| 553 | node.property.name; | |
| 554 | 1314 | return function propertyExpression(locals, env, data) { |
| 555 | 73 | var prop = _resolve(property, locals, env, data); |
| 556 | 73 | var parent = expression(locals, env, data); |
| 557 | 73 | locals = parent[0], parent = parent[1]; |
| 558 | // If `parent` is an Entity or an Attribute, evaluate its value via the | |
| 559 | // `_yield` method. This will ensure the correct value of | |
| 560 | // `locals.__this__`. | |
| 561 | 73 | if (parent._yield) { |
| 562 | 47 | return parent._yield(data, prop); |
| 563 | } | |
| 564 | // If `parent` is an object passed by the developer to the context (i.e., | |
| 565 | // `expression` was a `VariableExpression`), simply return the member of | |
| 566 | // the object corresponding to `prop`. We don't really care about | |
| 567 | // `locals` here. | |
| 568 | 26 | if (typeof parent !== 'function') { |
| 569 | 3 | return [locals, parent[prop]]; |
| 570 | } | |
| 571 | 23 | return parent(locals, env, data, prop); |
| 572 | } | |
| 573 | } | |
| 574 | 1 | function AttributeExpression(node) { |
| 575 | // XXX looks similar to PropertyExpression, but it's actually closer to | |
| 576 | // Identifier | |
| 577 | 126 | var expression = Expression(node.expression); |
| 578 | 126 | var attribute = node.computed ? |
| 579 | Expression(node.attribute) : | |
| 580 | node.attribute.name; | |
| 581 | 126 | return function attributeExpression(locals, env, data) { |
| 582 | 10 | var attr = _resolve(attribute, locals, env, data); |
| 583 | 10 | var entity = expression(locals, env, data); |
| 584 | 10 | locals = entity[0], entity = entity[1]; |
| 585 | // XXX what if it's not an entity? | |
| 586 | 10 | return [locals, entity.attributes[attr]]; |
| 587 | } | |
| 588 | } | |
| 589 | 1 | function ParenthesisExpression(node) { |
| 590 | 60 | return Expression(node.expression); |
| 591 | } | |
| 592 | ||
| 593 | }).call(this); |