-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add web workers, parse automatically #272
Open
nolanlawson
wants to merge
1
commit into
zaach:master
Choose a base branch
from
nolanlawson:web-worker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−34
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
(function ($) { | ||
|
||
var worker = new Worker('../assets/js/worker.js'); | ||
|
||
var parser, | ||
parser2; | ||
|
||
|
@@ -13,8 +15,20 @@ print = function (){} | |
|
||
var printOut = function (str) { $("#out").html(str); }; | ||
|
||
function debounce(timeout, fn) { | ||
var timer; | ||
|
||
return function() { | ||
clearTimeout(timer); | ||
|
||
timer = setTimeout(function() { | ||
fn(); | ||
timer = null; | ||
}, timeout); | ||
}; | ||
} | ||
|
||
$(document).ready(function () { | ||
$("#process_btn").click(processGrammar); | ||
$("#parse_btn").click(runParser); | ||
|
||
$("#examples").change(function(ev) { | ||
|
@@ -23,52 +37,107 @@ $(document).ready(function () { | |
$.get("/jison/examples/"+file, function (data) { | ||
$("#grammar").val(data); | ||
$(document.body).removeClass("loading"); | ||
processGrammar(); | ||
}); | ||
}); | ||
|
||
|
||
// recompile the grammar using a web worker, | ||
// as the user types | ||
var onChange = debounce(700, processGrammar); | ||
$('#grammar').bind('input propertychange', onChange); | ||
processGrammar(); | ||
}); | ||
|
||
function processGrammar () { | ||
var type = "lalr"; | ||
|
||
var grammar = $("#grammar").val(); | ||
try { | ||
var cfg = JSON.parse(grammar); | ||
} catch(e) { | ||
|
||
function onError(e) { | ||
console.log(e); | ||
$("#gen_out").html("Oops. Make sure your grammar is " + | ||
"in the correct format."+ "\n" + e.stack) | ||
.removeClass('good') | ||
.removeClass('warning') | ||
.addClass('bad'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not strictly related to the web worker stuff, but I thought it would be nice if it printed out a full stack trace. More info is helpful. :) |
||
|
||
function onSuccess(result) { | ||
|
||
try { | ||
var cfg = bnf.parse(grammar); | ||
parser = Jison.Generator(result.cfg, {type: result.type}); | ||
} catch (e) { | ||
$("#gen_out").html("Oops. Make sure your grammar is in the correct format.\n"+e).addClass('bad'); | ||
return; | ||
return onError(e); | ||
} | ||
|
||
$("#out").removeClass("good").removeClass("bad").html(''); | ||
$("#gen_out").removeClass("good").removeClass("bad").removeClass('warning'); | ||
if (!parser.conflicts) { | ||
$("#gen_out").html('Generated successfully!').addClass('good'); | ||
} else { | ||
$("#gen_out").html('Conflicts encountered:<br/>').addClass('bad'); | ||
} | ||
} | ||
|
||
Jison.print = function () {}; | ||
parser = Jison.Generator(cfg, {type: type}); | ||
$("#download_btn").click(function () { | ||
window.location.href = "data:application/javascript;charset=utf-8;base64,"+Base64.encode(parser.generate()); | ||
}).removeAttr('disabled'); | ||
|
||
|
||
|
||
parser.resolutions.forEach(function (res) { | ||
var r = res[2]; | ||
if (!r.bydefault) return; | ||
$("#gen_out").append(r.msg+"\n"+"("+r.s+", "+r.r+") -> "+r.action); | ||
}); | ||
|
||
parser2 = parser.createParser(); | ||
} | ||
|
||
// for newer browsers | ||
function callWorker(grammar) { | ||
worker.addEventListener('error', onError); | ||
worker.addEventListener('message', function(e) { | ||
onSuccess(e.data.result); | ||
}); | ||
|
||
// ask the web worker to parse the grammar for us | ||
worker.postMessage(grammar); | ||
} | ||
|
||
// for older browsers (IE <=9, Android <=4.3) | ||
function callNonWorker(grammar) { | ||
Jison.print = function () {}; | ||
var cfg; | ||
|
||
$("#out").removeClass("good").removeClass("bad").html(''); | ||
$("#gen_out").removeClass("good").removeClass("bad"); | ||
if (!parser.conflicts) { | ||
$("#gen_out").html('Generated successfully!').addClass('good'); | ||
try { | ||
cfg = JSON.parse(grammar); | ||
} catch (e) { | ||
try { | ||
cfg = bnf.parse(grammar); | ||
} catch (e) { | ||
return onError(e); | ||
} | ||
} | ||
|
||
onSuccess({cfg: cfg, type: 'lalr'}); | ||
} | ||
|
||
$("#gen_out").html("Parsing...") | ||
.removeClass('good') | ||
.removeClass('bad') | ||
.addClass('warning'); | ||
$('#download_btn').attr('disabled', true); | ||
|
||
var grammar = $("#grammar").val(); | ||
|
||
if (typeof Worker !== 'undefined') { | ||
callWorker(grammar); | ||
} else { | ||
$("#gen_out").html('Conflicts encountered:<br/>').addClass('bad'); | ||
callNonWorker(grammar); | ||
} | ||
|
||
$("#download_btn").click(function () { | ||
window.location.href = "data:application/javascript;charset=utf-8;base64,"+Base64.encode(parser.generate()); | ||
}).removeAttr('disabled'); | ||
|
||
parser.resolutions.forEach(function (res) { | ||
var r = res[2]; | ||
if (!r.bydefault) return; | ||
$("#gen_out").append(r.msg+"\n"+"("+r.s+", "+r.r+") -> "+r.action); | ||
}); | ||
|
||
parser2 = parser.createParser(); | ||
} | ||
|
||
function runParser () { | ||
if (!parser) processGrammar(); | ||
if (!parser) { | ||
processGrammar(); | ||
} | ||
printOut("Parsing..."); | ||
var source = $("#source").val(); | ||
try { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
importScripts('jison.js'); | ||
Jison.print = function () {}; | ||
|
||
// request to parse a grammar | ||
self.addEventListener('message', function (e) { | ||
if (typeof e.data !== 'string') { | ||
return; | ||
} | ||
|
||
var grammar = e.data; | ||
|
||
var cfg; | ||
|
||
try { | ||
cfg = JSON.parse(grammar); | ||
} catch (e) { | ||
// intentionally throw an error here if it fails to parse | ||
cfg = bnf.parse(grammar); | ||
} | ||
|
||
self.postMessage({result: {cfg: cfg, type: "lalr"}}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nanoc
kept creating this file for some reason ^