[ Launch: codemirror AST explorer ] 5597237 by enjalot
[ Launch: codemirror log line widget ] 5564948 by ejfox
[ Launch: codemirror log line widget ] 5564930 by enjalot
[ Launch: codemirror log widget ] 5562666 by enjalot
[ Launch: codemirror widget test ] 5562642 by enjalot
-
-
Save enjalot/5597237 to your computer and use it in GitHub Desktop.
codemirror AST explorer
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
{"description":"codemirror AST explorer","endpoint":"","display":"div","public":true,"require":[{"name":"esprima","url":"http://esprima.org/esprima.js"},{"name":"escodegen","url":"http://esprima.org/test/3rdparty/escodegen.browser.js"},{"name":"jsoneditor","url":"http://enjalot.github.io/jsoneditor/jsoneditor.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"log.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"esprima.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/k3rpGLw.png"} |
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 is where we use esprima to interperet our code | |
//mainly taken from https://github.com/nornagon/live/blob/master/xform.coffee | |
tributary.esprima = function(code, prefix) { | |
if(!prefix) prefix = ''; | |
//TURN ON LOC to use logJacking etc; | |
var parsed = esprima.parse(code/*, {loc:true}*/); | |
//console.log(parsed) | |
__hasProp = {}.hasOwnProperty; | |
console.log("PARSED", parsed) | |
var transformed; | |
var $values = {}; | |
var id, nextId = 0; | |
function replace(e, depth) { | |
handleNumber(e); | |
//handleConsoleLog(e); | |
handleControls(e); | |
handleWhile(e,depth); | |
return transform(e, replace, depth); | |
} | |
function transform(object, f, depth) { | |
var i, key, newObject, v, value, _i, _len; | |
depth++; | |
if (object instanceof Array) { | |
newObject = []; | |
for (i = _i = 0, _len = object.length; _i < _len; i = ++_i) { | |
v = object[i]; | |
if (typeof v === 'object' && v !== null) { | |
newObject[i] = f(v, depth); | |
} else { | |
newObject[i] = v; | |
} | |
} | |
} else { | |
newObject = {}; | |
for (key in object) { | |
if (!__hasProp.call(object, key)) continue; | |
value = object[key]; | |
if (typeof value === 'object' && value !== null) { | |
newObject[key] = f(value, depth); | |
} else { | |
newObject[key] = value; | |
} | |
} | |
} | |
return newObject; | |
} | |
function handleNumber(e) { | |
if (e.type === 'Literal' && typeof e.value === 'number') { | |
id = nextId++; | |
$values[prefix + id] = { | |
value: e.value, | |
loc: e.loc | |
}; | |
/* | |
return { | |
type: "MemberExpression", | |
computed: true, | |
object: { | |
type: 'Identifier', | |
name: '$values' | |
}, | |
property: { | |
type: 'Literal', | |
value: prefix + '' + id | |
} | |
}; | |
*/ | |
} | |
} | |
function handleConsoleLog(e) { | |
if(e.type === 'ExpressionStatement' && e.expression && e.expression.type === 'CallExpression') { | |
var callee = e.expression.callee; | |
if(callee.object && callee.object.name === 'console' && callee.property && callee.property.name === 'log') { | |
callee.property.name = 'logJack'; | |
var pos = e.expression.loc.end; | |
pos.line -= 1; | |
var newArgs = [ | |
{ | |
"type": "ObjectExpression", | |
"properties": [ | |
{ | |
"type": "Property", | |
"key": { | |
"type": "Identifier", | |
"name": "line" | |
}, | |
"value": { | |
"type": "Literal", | |
"value": pos.line, | |
"raw": pos.line + "" | |
}, | |
"kind": "init" | |
}, | |
{ | |
"type": "Property", | |
"key": { | |
"type": "Identifier", | |
"name": "ch" | |
}, | |
"value": { | |
"type": "Literal", | |
"value": pos.column, | |
"raw": pos.column + "" | |
}, | |
"kind": "init" | |
} | |
] | |
}, | |
{ | |
"type": "ArrayExpression", | |
"elements": e['expression']['arguments'] | |
} | |
]; | |
e['expression']['arguments'] = newArgs; | |
//console.log("pos", e, callee.loc.end); | |
return transform(e,replace); | |
} | |
} | |
} | |
function handleControls(e) { | |
} | |
function contains(array, fn) { | |
var found = -1; | |
for (var i = array.length; i--;){ | |
if(fn(array[i], i)) { | |
found = i | |
break; | |
} | |
} | |
return found; | |
} | |
function handleWhile(e, depth) { | |
if(e.body) { | |
//console.log("TYPE", e.type, e) | |
var loopVars = "var $loopDepth" + depth + "=100,$loopCount" + depth + "=0;" | |
var loopAst = esprima.parse(loopVars) | |
var index = contains(e.body, function(d) { return d.type === 'WhileStatement' }); | |
if(index >= 0 && e.body[0] !== loopAst) { | |
e.body.unshift(loopAst) | |
} | |
} | |
/* | |
if(e.type === 'WhileStatement') { | |
//console.log("little ast",rent, grent) | |
//grent.body.splice(i,1, ast.body[0]) | |
grent.body.push(ast.body[0]) | |
//console.log(e) | |
transform(e,replace,rent) | |
} | |
*/ | |
} | |
transformed = replace(parsed, 0) | |
//transformed = transform(parsed, replace, 0) | |
return { | |
ast: transformed, | |
values: $values | |
} | |
} | |
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
var options = { | |
mode: 'javascript', | |
lineNumbers: true, | |
viewportMargin: Infinity, | |
theme: 'vibrant-ink' | |
} | |
var display = d3.select("#display") | |
var editor = display.append("div").classed("editor", true) | |
.classed("test-editor", true); | |
var cm = CodeMirror(editor.node(), options); | |
//cm.setValue("var x = 5\nconsole.log(\"x\",x);\nx = 1337;\nconsole.log(\"x\", x, x+42);\n$control(42, {name:'everything'})") | |
//cm.setValue("var $loopDepth=100,$loopCount=0;\nwhile(true);"); | |
//cm.setValue("function x() { while(true); }") | |
cm.setValue("while(true);"); | |
var display = d3.select("#display") | |
var editor2 = display.append("div") | |
.classed("editor", true) | |
.classed("test-editor", true); | |
var cm2 = CodeMirror(editor2.node(), options); | |
var container = display.append("div").attr("id", "jsoneditor").node() | |
var jsonEditor = new jsoneditor.JSONEditor(container); | |
var widgets = []; | |
editor.on("click", function() { | |
//console.log("editor click", arguments); | |
clear(); | |
}) | |
display.append("button") | |
.text("clear") | |
.on("click", clear); | |
function clear() { | |
for(var i = widgets.length; i--;) { | |
widgets.pop().clear(); //this remove's CodeMirror's handle to the widget; | |
} | |
// display.selectAll(".log-widget").remove() | |
} | |
console.log("hi"); | |
cm.on("change", execute) | |
function execute() { | |
clear(); | |
var js = cm.getValue(); | |
var transformed = tributary.esprima(js); | |
//console.log("ast", transformed.ast); | |
var values = 'var $values = ' + JSON.stringify(transformed.values) + '\n'; | |
var code = values + escodegen.generate(transformed.ast); | |
//console.log("code", code); | |
console.log("ast", transformed.ast) | |
cm2.setValue(code) | |
jsonEditor.set(transformed.ast) | |
jsonEditor.expandAll(); | |
try { | |
//eval(code); | |
} catch(e) { } | |
} | |
if (typeof console !== "undefined") { | |
console.logJack = function(pos, args) { | |
var text = JSON.stringify(args) | |
text = text.slice(1, text.length-1); | |
var widget = display.append("div") | |
.text(text) | |
.classed("log-widget", true) | |
var lwidget = cm.addLineWidget(pos.line, widget.node()); | |
widgets.push(lwidget); | |
console.log.apply(console, args); | |
} | |
} | |
execute() | |
//setup css for jsoneditor | |
var jscss = d3.select("head").selectAll("link.jsoneditor") | |
.data(["http://enjalot.github.io/jsoneditor/jsoneditor.css"]) | |
jscss.enter() | |
.append("link") | |
.classed("jsoneditor", true) | |
jscss | |
.attr({ | |
rel:"stylesheet", | |
type:"text/css", | |
href: function(d) { return d } | |
}); | |
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
#display { | |
overflow:scroll; | |
} | |
#jsoneditor { | |
position: absolute; | |
top: 100px; | |
left: 352px; | |
width: 400px; | |
height: 800px; | |
background: #FFFFFF; | |
} | |
#jsoneditor .field,#jsoneditor .value { | |
height: 25px; | |
line-height: 25px; | |
} | |
.test-editor { | |
width: 350px; | |
height: 400px; | |
} | |
.test-editor .CodeMirror { | |
width: 100%; | |
height: 100%; | |
overflow-x: scroll; | |
} | |
.log-widget { | |
/*border: 1px solid red;*/ | |
opacity: 0.5; | |
background: rgba(240,240,240,0.2); | |
padding-left: 1.2em; | |
} | |
.log-widget:before { | |
color: orange; | |
content: ">" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment