|
220 | 220 | (defn parse-rule* |
221 | 221 | "Creates a rule from the DSL syntax using the given environment map. *ns* |
222 | 222 | should be bound to the namespace the rule is meant to be defined in." |
223 | | - ([lhs rhs properties env] |
224 | | - (parse-rule* lhs rhs properties env {})) |
225 | | - ([lhs rhs properties env rule-meta] |
| 223 | + ([lhs rhs properties rule-env rule-meta] |
226 | 224 | (let [conditions (into [] (for [expr lhs] |
227 | 225 | (parse-expression expr rule-meta))) |
228 | 226 |
|
|
235 | 233 | assoc :file *file*))} |
236 | 234 |
|
237 | 235 | symbols (set (filter symbol? (com/flatten-expression (concat lhs rhs)))) |
238 | | - matching-env (into {} (for [sym (keys env) |
| 236 | + matching-env (into {} (for [sym (keys rule-env) |
239 | 237 | :when (symbols sym)] |
240 | 238 | [(keyword (name sym)) sym]))] |
241 | 239 |
|
|
245 | 243 | (seq properties) (assoc :props properties) |
246 | 244 |
|
247 | 245 | ;; Add the environment, if given. |
248 | | - (seq env) (assoc :env matching-env))))) |
| 246 | + (seq rule-env) (assoc :env matching-env))))) |
249 | 247 |
|
250 | 248 | (defn parse-rule-action* |
251 | 249 | "Creates a rule action from the DSL syntax using the given environment map. *ns* |
252 | 250 | should be bound to the namespace the rule is meant to be defined in." |
253 | | - ([lhs rhs properties env] |
254 | | - (parse-rule-action* lhs rhs properties env {})) |
255 | | - ([lhs rhs properties env rule-meta] |
| 251 | + ([lhs rhs properties rule-env rule-meta] |
256 | 252 | (let [conditions (into [] (for [expr lhs] |
257 | 253 | (parse-expression expr rule-meta))) |
258 | 254 |
|
|
262 | 258 | :rhs (vary-meta rhs assoc :file *file*)} |
263 | 259 |
|
264 | 260 | symbols (set (filter symbol? (com/flatten-expression (concat lhs rhs)))) |
265 | | - matching-env (into {} (for [sym (keys env) |
| 261 | + matching-env (into {} (for [sym (keys rule-env) |
266 | 262 | :when (symbols sym)] |
267 | 263 | [(keyword (name sym)) sym]))] |
268 | 264 |
|
|
272 | 268 | (seq properties) (assoc :props properties) |
273 | 269 |
|
274 | 270 | ;; Add the environment, if given. |
275 | | - (seq env) (assoc :env matching-env))))) |
| 271 | + (seq rule-env) (assoc :env matching-env))))) |
276 | 272 |
|
277 | 273 | (defn parse-query* |
278 | 274 | "Creates a query from the DSL syntax using the given environment map." |
279 | | - ([params lhs env] |
280 | | - (parse-query* params lhs env {})) |
281 | | - ([params lhs env query-meta] |
| 275 | + ([params lhs query-env query-meta] |
282 | 276 | (let [conditions (into [] (for [expr lhs] |
283 | 277 | (parse-expression expr query-meta))) |
284 | 278 |
|
|
288 | 282 |
|
289 | 283 | symbols (set (filter symbol? (com/flatten-expression lhs))) |
290 | 284 | matching-env (into {} |
291 | | - (for [sym (keys env) |
| 285 | + (for [sym (keys query-env) |
292 | 286 | :when (symbols sym)] |
293 | 287 | [(keyword (name sym)) sym]))] |
294 | 288 |
|
295 | 289 | (cond-> query |
296 | | - (seq env) (assoc :env matching-env))))) |
| 290 | + (seq query-env) (assoc :env matching-env))))) |
297 | 291 |
|
298 | 292 | (defmacro parse-rule |
299 | 293 | "Macro used to dynamically create a new rule using the DSL syntax." |
300 | 294 | ([lhs rhs] |
301 | | - (parse-rule* lhs rhs nil &env)) |
| 295 | + (parse-rule* lhs rhs nil &env (meta &form))) |
302 | 296 | ([lhs rhs properties] |
303 | | - (parse-rule* lhs rhs properties &env))) |
| 297 | + (parse-rule* lhs rhs properties &env (meta &form)))) |
304 | 298 |
|
305 | 299 | ;;; added to clojure.core in 1.9 |
306 | 300 | (defn- qualified-keyword? |
|
315 | 309 |
|
316 | 310 | (defn build-rule |
317 | 311 | "Function used to parse and build a rule using the DSL syntax." |
318 | | - ([name body] (build-rule name body {})) |
319 | | - ([name body form-meta] |
| 312 | + ([name body rule-env rule-meta] |
320 | 313 | (let [doc (if (string? (first body)) (first body) nil) |
321 | 314 | body (if doc (rest body) body) |
322 | 315 | properties (if (map? (first body)) (first body) nil) |
323 | 316 | definition (if properties (rest body) body) |
324 | 317 | {:keys [lhs rhs]} (split-lhs-rhs definition)] |
325 | | - (cond-> (parse-rule* lhs rhs properties {} form-meta) |
| 318 | + (cond-> (parse-rule* lhs rhs properties rule-env rule-meta) |
326 | 319 |
|
327 | 320 | name (assoc :name (production-name name)) |
328 | 321 | doc (assoc :doc doc))))) |
329 | 322 |
|
330 | 323 | (defn build-rule-action |
331 | 324 | "Function used to parse and build a rule action using the DSL syntax." |
332 | | - ([name body] (build-rule-action name body {})) |
333 | | - ([name body form-meta] |
| 325 | + ([name body rule-env rule-meta] |
334 | 326 | (let [doc (if (string? (first body)) (first body) nil) |
335 | 327 | body (if doc (rest body) body) |
336 | 328 | properties (if (map? (first body)) (first body) nil) |
337 | 329 | definition (if properties (rest body) body) |
338 | 330 | {:keys [lhs rhs]} (split-lhs-rhs definition)] |
339 | | - (cond-> (parse-rule-action* lhs rhs properties {} form-meta) |
| 331 | + (cond-> (parse-rule-action* lhs rhs properties rule-env rule-meta) |
340 | 332 |
|
341 | 333 | name (assoc :name (production-name name)) |
342 | 334 | doc (assoc :doc doc))))) |
343 | 335 |
|
344 | 336 | (defmacro parse-query |
345 | 337 | "Macro used to dynamically create a new rule using the DSL syntax." |
346 | 338 | [params lhs] |
347 | | - (parse-query* params lhs &env)) |
| 339 | + (parse-query* params lhs &env (meta &form))) |
348 | 340 |
|
349 | 341 | (defn build-query |
350 | 342 | "Function used to parse and build a query using the DSL syntax." |
351 | | - ([name body] (build-query name body {})) |
352 | | - ([name body form-meta] |
| 343 | + ([name body env form-meta] |
353 | 344 | (let [doc (if (string? (first body)) (first body) nil) |
354 | 345 | binding (if doc (second body) (first body)) |
355 | 346 | definition (if doc (drop 2 body) (rest body))] |
356 | | - (cond-> (parse-query* binding definition {} form-meta) |
| 347 | + (cond-> (parse-query* binding definition env form-meta) |
357 | 348 | name (assoc :name (production-name name)) |
358 | 349 | doc (assoc :doc doc))))) |
0 commit comments