1 2 3 define(['constants', 'jisonGen', 'asyncGen', 'jquery'], 4 /** 5 * Asynchronous generator for executable language-grammars (i.e. converted JSON grammars). 6 * 7 * <p> 8 * This generator uses Jison for compiling the JSON grammar. 9 * 10 * <p> 11 * Usage of this compile is the same as for synchronously working JisonGenerator. 12 * 13 * @see JisonGenerator 14 * 15 * @class 16 * @constant 17 * @public 18 * @name JisonAsyncGenerator 19 * @memberOf mmir.env.grammar 20 * 21 * @requires JisonGenerator 22 * @requires jQuery.extend 23 */ 24 function(constants, jisonGen, asyncGen, $){ 25 26 /** 27 * Counter for generating IDs for compile-jobs that 28 * are sent to the WebWorker 29 * 30 * @private 31 * @memberOf JisonAsyncGenerator# 32 */ 33 var _taskId = 1; 34 35 /** 36 * WebWorker instance for compiling parser asynchronously 37 * 38 * @private 39 * @memberOf JisonAsyncGenerator# 40 */ 41 var asyncCompiler = asyncGen.createWorker(jisonGen.engineId); 42 43 /** 44 * printError function reference for compile-errors 45 * 46 * @private 47 * @memberOf JisonAsyncGenerator# 48 */ 49 var printError = jisonGen.printError; 50 51 52 ////////////////////init async compiler/thread ///////////////////////// 53 54 asyncCompiler._onerror = printError; 55 56 //setup async init-signaling: 57 var initDef = $.Deferred(); 58 var initMsg = asyncCompiler.prepareOnInit(jisonGen, initDef, require); 59 asyncCompiler.postMessage(initMsg); 60 61 /** 62 * Exported (public) functions for the jison grammar-engine. 63 * @public 64 * @type GrammarGenerator 65 * @memberOf JisonAsyncGenerator# 66 */ 67 var jisonAsyncGen = { 68 /** @scope JisonAsyncGenerator.prototype */ 69 70 /** 71 * @see JisonAsyncGenerator#init 72 * @memberOf JisonAsyncGenerator.prototype 73 */ 74 init: function(callback){ 75 //overwrite with own async "init signal" 76 if(callback){ 77 initDef.always(callback); 78 } 79 return initDef; 80 }, 81 82 /** 83 * @returns {Boolean} if this engine compilation works asynchronously. 84 * The current implementation works asynchronously (returns TRUE) 85 * 86 * @memberOf JisonAsyncGenerator.prototype 87 */ 88 isAsyncCompilation: function(){ return true; }, 89 /** 90 * @see JisonGenerator#_compileParser 91 * @protected 92 */ 93 _compileParser: function(grammarDefinition, options, onAfterCompileParserResult){ 94 95 //start compilation in web-worker: 96 asyncCompiler.postMessage({ 97 cmd: 'parse', 98 id: onAfterCompileParserResult, 99 config: options, 100 text: grammarDefinition 101 }); 102 103 }, 104 // /** 105 // * @see JisonGenerator#_preparePrintError 106 // * @protected 107 // */ 108 // _preparePrintError: function(){},//<- overwrite with NOOP 109 /** 110 * @see JisonGenerator#_afterCompileParser 111 * @protected 112 */ 113 _afterCompileParser: function(compileParserModuleFunc){ 114 115 //callback-ID: 116 var taskId = '' + _taskId++; 117 118 //register callback for messages from web-worker: 119 asyncCompiler.addCallback(taskId, function(evtData){ 120 121 if(evtData.error){ 122 123 //handle error message: 124 125 if(printError){ 126 printError(evtData.error); 127 } 128 else { 129 console.error(evtData.error); 130 } 131 132 //////////////////// EARLY EXIT ///////////////// 133 //NOTE this is not the final message from the compiler-thread... 134 return; 135 } 136 137 var hasError = evtData.isError; 138 var grammarParser = evtData.def; 139 140 compileParserModuleFunc(grammarParser, hasError); 141 }); 142 143 return taskId; 144 } 145 }; 146 147 //extend/overload sync-compiler with async-compiler: 148 return $.extend({}, jisonGen, jisonAsyncGen); 149 150 }); 151