Skip to content

Instantly share code, notes, and snippets.

View pvdz's full-sized avatar

Peter van der Zee pvdz

View GitHub Profile
@pvdz
pvdz / gist:4528967
Created January 14, 2013 09:50
Minimal TypeScript compile case. Ouch..
function compile(source) { // derived from www.typescriptlang.org/Playground/
var result = '';
var outfile = {Write:function(s){ result += s; }, WriteLine:function(s){ result += s + '\n'; }, Close:function(){}};
var compiler = new TypeScript.TypeScriptCompiler();
compiler.setErrorCallback(function(start, len, message) { console.log('error',arguments); });
compiler.parser.errorRecovery = true;
compiler.addUnit(source, "input.ts");
compiler.emit(function createFile(fileName) { return outfile; });
return result;
@pvdz
pvdz / gist:3891556
Created October 15, 2012 08:56
Rewriting expressions, down the rabbit hole you go!
expr +150 -150
100 => 250 -50
-100 => 50 -250
x => x+150 x-150
x+100 => x+250 x-50
x-100 => x+50 x-250
x*100 => x*100+150 x*100-150
x*-100 => x*100+50 x*100-250
x++ => x++ +150 x++ -150
@pvdz
pvdz / gist:3878890
Created October 12, 2012 12:02
ace get linear caret position wot?
getCharPos1: function() {
// Get cursor position
var charPos = 0;
var cursorPos = this.editor.getCursorPosition();
for (var i=0; i<cursorPos.row; i++){
charPos += this.editor.session.$rowLengthCache[i]; // +1 for newline
}
charPos += cursorPos.column + cursorPos.row; // Add row count to compensate for newlines
return charPos;
var ordered = pieces.slice(i).sort(function(a,b){
var pa = this.distanceBetween(p,a);
var pb = this.distanceBetween(p.b);
if (pa < pb) return -1;
if (pa > pb) return 1;
return 0;
}.bind(this));
@pvdz
pvdz / gist:2830474
Created May 29, 2012 20:22
ZeParser tagliteral post to DOM syntax
// requires ZeParser (see repo)
// input is a string, your js input with tagliterals
function compile(input){
var tree = [];
var tokenizer = new Tokenizer(input);
tokenizer.tagLiterals = true; // enable tag literals in the engine
var parser = new ZeParser(input, tokenizer, tree);
parser.parse();
tokenizer.fixValues();
@pvdz
pvdz / nca.js
Created May 29, 2012 12:38
NCA fallback library
var NCA = function(type, version, compiler){
this.type = type;
this.version = version;
// compiler must have interface method "compile",
// which accepts a string as single argument
this.compiler = compiler;
};
NCA.prototype = {
queue: [],
fetching: false,
var parser = ZeParser.createParser(ta.value);
parser.tokenizer.fixValues();
var wtree = parser.tokenizer.wtree;
ta.className = '';
var ide = wtree.map(function(t){
if (t.name == 14) ta.className = 'error';
return '<span class="t'+t.name+'">'+(t.name==13?'\u29e6':(t.name==14?'\u292c':t.value)).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</span>';
});
@pvdz
pvdz / gist:1745582
Created February 5, 2012 13:27
ZeParser syntax highlighter
var parser = ZeParser.createParser(value);
parser.tokenizer.fixValues();
var ide = parser.tokenizer.wtree.map(function(t){
if (t.name == 14) ta.className = 'error';
return '<span class="t'+t.name+'">'+(t.name==13?'\u29e6':(t.name==14?'\u292c':t.value))+'</span>';
});
div.innerHTML = ide.join('');
// the case:
// runner.js
module.exports = {
arr: [],
fetch: function(str){ this.arr.push(str); }
};
// mod1.js
module.exports = function(){
// Not intended to be fast.
var WebVTTCueTextParser = (function(){ // this will return just the WebVTTCueTextParser function and not leak anything else
// local scope:
var
NEWLINE = /\r\n|\r|\n/,
SPACE = /[\u0020\t\f]/,
NOSPACE = /[^\u0020\t\f]/