Skip to content

Instantly share code, notes, and snippets.

@DarrenN
Forked from jlongster/where-macro.js
Created August 17, 2016 19:34
Show Gist options
  • Save DarrenN/45ee2821b48f6f78632b8b363bccb1c5 to your computer and use it in GitHub Desktop.
Save DarrenN/45ee2821b48f6f78632b8b363bccb1c5 to your computer and use it in GitHub Desktop.
syntax where = function(ctx) {
const binaryOps = ["or", "and", "==", ">", "<", "-", "+", "*", "/", "."];
const dummy = #`dummy`.get(0);
function matchNext(names) {
const marker = ctx.mark();
const next = ctx.next();
ctx.reset(marker);
return !next.done && names.includes(next.value.val());
}
function readBinaryOp(op) {
const precIdx = binaryOps.indexOf(op);
const isLast = precIdx === binaryOps.length - 1;
const readNext = () => {
if(isLast) {
return dummy.fromString("" + ctx.next().value.val());
}
return readBinaryOp(binaryOps[precIdx + 1]);
}
const left = readNext();
while(matchNext([op])) {
ctx.next();
const right = readNext();
left = #`({ op: ${dummy.fromString(op)}, left: ${left}, right: ${right} })`;
}
return left;
}
function read() {
return readBinaryOp(binaryOps[0]);
}
return read();
}
where t.x == 3 / 4 * 2 and z > 10;
// Output:
//
// ({
// op: "and",
// left: {
// op: "==",
// left: {
// op: ".",
// left: "t",
// right: "x"
// },
// right: {
// op: "*",
// left: {
// op: "/",
// left: "3",
// right: "4"
// },
// right: "2"
// }
// },
// right: {
// op: ">",
// left: "z",
// right: "10"
// }
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment