Skip to content

Commit

Permalink
fix: replace script
Browse files Browse the repository at this point in the history
  • Loading branch information
boyander committed Sep 19, 2023
1 parent 8464cab commit ae062b8
Showing 1 changed file with 1 addition and 73 deletions.
74 changes: 1 addition & 73 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1 @@
import * as ohm from "ohm-js";

const source = String.raw`
FaableQL {
Expr = FieldExpr+
FieldExpr = field operator value
operator = (eq | noteq)
eq = ":"
noteq = "!:"
field = letter+
value = letter+
}
`;

const faableQL = ohm.grammar(source);

console.log(faableQL);

const valid_fields = [{ name: "label", db: "labels" }];

const s = faableQL.createSemantics();
s.addAttribute("asMongo", {
FieldExpr(name, operator, value) {
const ops = {
":": "$eq",
"!:": "$ne",
};
const op = ops[operator.asMongo];
let val;
if (op == "$eq") {
val = value.asMongo;
} else {
val = { [op]: value.asMongo };
}

// Valid field
const token = name.asMongo;
const field_def = valid_fields.find((f) => f.name == token);
if (!field_def) {
throw new Error(`${token} is not a valid field to query`);
}
const fieldname = field_def.db;
return { [fieldname]: val };
},
field(_) {
return this.sourceString;
},
value(_) {
return this.sourceString;
},
_iter(...child) {
return { $and: child.map((e) => e.asMongo) };
},
_terminal() {
return this.sourceString;
},
} as any);

const query =
"label:babies label:vegetarian label:vegan label:freeze label!:egg label:glucose";
//const query = "label:babies";

const match = faableQL.match(query);
console.log(match);
if (match.succeeded()) {
console.log("✅ Valid match");

const ops_dict = s(match);
console.log(ops_dict.asMongo);
console.log(JSON.stringify(ops_dict.asMongo, null, 2));
} else {
console.log("❌ Invalid match");
}
export * from "./faableql";

0 comments on commit ae062b8

Please sign in to comment.