Skip to content

Instantly share code, notes, and snippets.

@bopjesvla

bopjesvla/coffeetable.js

Last active Jan 8, 2017
Embed
What would you like to do?
"use strict"
var usage = "sjs -l ./coffeetable.js ./examplescript.js"
var indentToBraces = (chr, reader, prevTokens) => {
var punc = reader.readPunctuator();
// record the whitespace at the start of the current line
var indentFinder = /\s*/g;
var i = indentFinder.lastIndex = reader.lineStart;
reader.skipComment();
if (reader.lineStart === i) { // colon wasn't at end of line, abort
return null;
}
var outerIndent = indentFinder.exec(reader.source)[0];
indentFinder.lastIndex = reader.lineStart;
var innerIndent = indentFinder.exec(reader.source)[0];
if (outerIndent.startsWith(innerIndent)) { // no indentation, abort
return null;
}
if (!innerIndent.startsWith(outerIndent)) {
reader.throwSyntaxError("confusing-indent", "Mixed indentation after colon");
}
var prevStart = reader.lineStart, tokens = [], lastIndent, token, pushPuncBeforeFirstToken = false;
var out = []
do {
while(prevStart === reader.lineStart) {
token = reader.readToken();
if (token.type === reader.Token.Punctuator && ~"}])".indexOf(token.value)) {
return out.push(reader.makeDelimiter("{}", tokens), token)
}
tokens[tokens.length] = token;
reader.skipComment();
if (reader.index >= reader.length) {
out.push(reader.makeDelimiter("{}", tokens));
return out
}
}
indentFinder.lastIndex = prevStart = reader.lineStart;
lastIndent = indentFinder.exec(reader.source)[0];
} while (lastIndent.startsWith(innerIndent))
if (!outerIndent.startsWith(lastIndent)) {
reader.throwSyntaxError("confusing-indent", "Mixed indentation in ident block", tokens[tokens.length-1]);
}
var lastToken = tokens[tokens.length-1]
if (lastToken.type === reader.Token.Punctuator && ~",".indexOf(lastToken.value)) {
out.concat([reader.makeDelimiter("{}", tokens), lastToken])
}
else {
out.push(reader.makeDelimiter("{}", tokens))
}
return out;
}
module.exports = {
":": indentToBraces,
".": (chr, reader) => { // 2...5 should be interpreted as 2 ... 5, not as 2. . .5
var ellipsisLength = 0;
if (reader.source[reader.index-1] == ".") { // if the first dot was gobbled by an integer (2. in 2...5)
ellipsisLength++
}
while (reader.source[reader.index] == ".") {
reader.readPunctuator()
ellipsisLength++
}
if (ellipsisLength > 1) {
return reader.makePunctuator(".".repeat(ellipsisLength))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.