1 /*
  2  * 	Copyright (C) 2012-2016 DFKI GmbH
  3  * 	Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4  * 	German Research Center for Artificial Intelligence
  5  * 	http://www.dfki.de
  6  * 
  7  * 	Permission is hereby granted, free of charge, to any person obtaining a 
  8  * 	copy of this software and associated documentation files (the 
  9  * 	"Software"), to deal in the Software without restriction, including 
 10  * 	without limitation the rights to use, copy, modify, merge, publish, 
 11  * 	distribute, sublicense, and/or sell copies of the Software, and to 
 12  * 	permit persons to whom the Software is furnished to do so, subject to 
 13  * 	the following conditions:
 14  * 
 15  * 	The above copyright notice and this permission notice shall be included 
 16  * 	in all copies or substantial portions of the Software.
 17  * 
 18  * 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 19  * 	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 20  * 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 21  * 	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 22  * 	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 23  * 	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 24  * 	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 25  */
 26 
 27 /**
 28  * @module workers/jison-compiler
 29  */
 30 
 31 importScripts('asyncCompileUtil.js');
 32 
 33 var jison;
 34 function _init(url){
 35 	
 36 	var libUrl = getPath(url) +'.js';
 37 	importScripts(libUrl);//'../vendor/libs/jison.js');
 38 
 39 	//set global var that holds jison
 40 	jison = Jison;
 41 }
 42 
 43 var defaultOptions = {
 44 	type: 'lalr'//'lr0' | 'slr' | 'lr' | 'll' | default: lalr
 45 };
 46 
 47 // setup jison compiler:
 48 
 49 function _preparePrintImpl(id){
 50 	
 51 	if(jison.print && jison.print.name === 'mmirPrint'){
 52 		return;
 53 	}
 54 	
 55 	jison.print = function mmirPrint(){
 56 		
 57 	//	var args = $.makeArray(arguments);
 58 	//	//prepend "location-information" to logger-call:
 59 	//	args.unshift('jison', 'compile');
 60 	//	
 61 	//	//output log-message:
 62 	//	logger.error.apply(logger, args);
 63 		
 64 		var args = _makeArray(arguments);
 65 		self.postMessage({error: args});
 66 	};
 67 }
 68 
 69 self.onmessage = function(e){
 70 	
 71   switch(e.data.cmd){
 72     case 'init':
 73       init(e.data);
 74       break;
 75     case 'parse':
 76       parse(e.data.text, e.data.config, e.data.id);
 77       break;
 78   }
 79   
 80 };
 81 
 82 function parse(grammarDefinition, config, id){
 83 	
 84 	if(!verifyInit(jison, 'jison', id)){
 85 		return;
 86 	}
 87 	
 88 	var options = _getOptions(config);
 89 	
 90 	_preparePrintImpl(id);
 91     
 92 	var hasError = false;
 93     var grammarParser;
 94 	try{
 95 		
 96     	var cfg = bnf.parse(grammarDefinition);
 97     	var parser = jison.Generator(cfg, options);
 98     	var grammarParser = parser.generate();
 99     	
100     } catch(error) {
101 //    	"{
102 //    	  "message": "Expected \"=\" or string but \"_\" found.",
103 //    	  "expected": [
104 //    	    {
105 //    	      "type": "literal",
106 //    	      "value": "=",
107 //    	      "description": "\"=\""
108 //    	    },
109 //    	    {
110 //    	      "type": "other",
111 //    	      "description": "string"
112 //    	    }
113 //    	  ],
114 //    	  "found": "_",
115 //    	  "offset": 4104,
116 //    	  "line": 40,
117 //    	  "column": 6,
118 //    	  "name": "SyntaxError"
119 //    	}"
120     	hasError = true;
121     	var msg = ' while compiling grammar "' + config.id + '": ';
122     	if(error.name === 'SyntaxError'){
123     		msg= 'SyntaxError' + msg + error.message;
124     	}
125     	else {
126     		msg = 'Error' + msg + (error && error.stack? error.stack : error);
127     	}
128     	
129     	if(typeof error.lineNumber !== 'undefined'){
130     		msg += ' at line '+error.lineNumber;
131     	}
132 
133     	if(typeof error.column !== 'undefined'){
134     		msg += ':'+error.column;
135     	}
136     	
137     	if(typeof error.index !== 'undefined'){
138     		msg += ' (offset '+error.index+')';
139     	}
140     	
141 //    	if(jison.printError){
142 //    		jison.printError(msg);
143 //    	}
144 //    	else {
145 //    		console.error(msg);
146 //    	}
147     	self.postMessage({error: msg, id: id});
148     	
149     	msg = '[INVALID GRAMMAR] ' + msg;
150     	grammarParser = 'var parser = { parse: function(){ var msg = '+JSON.stringify(msg)+'; console.error(msg); throw msg;} }';
151     }
152 	
153 	self.postMessage({def: grammarParser, isError: hasError, id: id, done: true});
154 }
155 
156