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/pegjs-compiler 29 */ 30 31 importScripts('asyncCompileUtil.js'); 32 33 var pegjs; 34 function _init(url){ 35 36 var libUrl = getPath(url) +'.js'; 37 importScripts(libUrl);//'../vendor/libs/peg-0.8.0.js'); 38 39 //set global var that holds jison 40 pegjs = PEG; 41 } 42 43 var defaultOptions = { 44 cache: false, 45 optimize: "speed", 46 output: "source", 47 allowedStartRules: void(0) 48 }; 49 50 // setup PEG.js compiler: 51 52 function _preparePrintImpl(id){ 53 54 if(pegjs.print && pegjs.print.name === 'mmirPrint'){ 55 return; 56 } 57 58 pegjs.print = function mmirPrint(){ 59 60 // var args = $.makeArray(arguments); 61 // //prepend "location-information" to logger-call: 62 // args.unshift('jison', 'compile'); 63 // 64 // //output log-message: 65 // logger.error.apply(logger, args); 66 67 var args = _makeArray(arguments); 68 self.postMessage({error: args}); 69 }; 70 } 71 72 self.onmessage = function(e){ 73 74 switch(e.data.cmd){ 75 case 'init': 76 init(e.data); 77 break; 78 case 'parse': 79 parse(e.data.text, e.data.config, e.data.id); 80 break; 81 } 82 83 }; 84 85 function parse(grammarDefinition, config, id){ 86 87 if(!verifyInit(pegjs, 'pegjs', id)){ 88 return; 89 } 90 91 _preparePrintImpl(id); 92 93 var options = _getOptions(config); 94 95 var hasError = false; 96 var grammarParser; 97 try{ 98 grammarParser = pegjs.buildParser(grammarDefinition, options); 99 } catch(error) { 100 // "{ 101 // "message": "Expected \"=\" or string but \"_\" found.", 102 // "expected": [ 103 // { 104 // "type": "literal", 105 // "value": "=", 106 // "description": "\"=\"" 107 // }, 108 // { 109 // "type": "other", 110 // "description": "string" 111 // } 112 // ], 113 // "found": "_", 114 // "offset": 4104, 115 // "line": 40, 116 // "column": 6, 117 // "name": "SyntaxError" 118 // }" 119 hasError = true; 120 var msg = ' while compiling grammar "' + config.id + '": '; 121 if(error.name === 'SyntaxError'){ 122 msg= 'SyntaxError' + msg + error.message; 123 } 124 else { 125 msg = 'Error' + msg + (error && error.stack? error.stack : error); 126 } 127 128 if(typeof error.line !== 'undefined'){ 129 msg += ' at line '+error.line; 130 } 131 132 if(typeof error.column !== 'undefined'){ 133 msg += ':'+error.column; 134 } 135 136 if(typeof error.offset !== 'undefined'){ 137 msg += ' (offset '+error.offset+')'; 138 } 139 140 // if(pegjs.printError){ 141 // pegjs.printError(msg); 142 // } 143 // else { 144 // console.error(msg); 145 // } 146 147 self.postMessage({error: msg, id: id}); 148 149 msg = '[INVALID GRAMMAR] ' + msg; 150 grammarParser = '{ 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