├── .circleci └── config.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── jexl-eval ├── Cargo.toml └── src │ ├── error.rs │ └── lib.rs ├── jexl-parser ├── Cargo.toml └── src │ ├── ast.rs │ ├── lib.rs │ ├── parser.lalrpop │ └── parser.rs └── parser-gen ├── Cargo.toml ├── README.md ├── ensure_parser_up_to_date.sh └── src └── main.rs /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | jobs: 3 | Ensure parser up-to-date: 4 | docker: 5 | - image: circleci/rust:latest 6 | resource_class: small 7 | steps: 8 | - checkout 9 | - run: ./parser-gen/ensure_parser_up_to_date.sh 10 | orbs: 11 | rust: glotrade/rust@0.1.3 12 | workflows: 13 | build: 14 | jobs: 15 | - rust/check 16 | - rust/format 17 | - rust/test 18 | - Ensure parser up-to-date 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | .vscode 4 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Community Participation Guidelines 2 | 3 | This repository is governed by Mozilla's code of conduct and etiquette guidelines. 4 | For more details, please read the 5 | [Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/). 6 | 7 | ## How to Report 8 | For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page. 9 | 10 | ## Project Specific Etiquette 11 | 12 | ### Our Responsibilities 13 | 14 | Project maintainers are responsible for clarifying the standards of acceptable 15 | behavior and are expected to take appropriate and fair corrective action in 16 | response to any instances of unacceptable behavior. 17 | 18 | Project maintainers have the right and responsibility to remove, edit, or 19 | reject comments, commits, code, wiki edits, issues, and other contributions 20 | that are not aligned to this Code of Conduct, or to ban temporarily or 21 | permanently any contributor for other behaviors that they deem inappropriate, 22 | threatening, offensive, or harmful. 23 | 24 | Project maintainers who do not follow or enforce Mozilla's Participation Guidelines in good 25 | faith may face temporary or permanent repercussions. 26 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.13" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "anyhow" 16 | version = "1.0.32" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" 19 | 20 | [[package]] 21 | name = "arrayref" 22 | version = "0.3.6" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" 25 | 26 | [[package]] 27 | name = "arrayvec" 28 | version = "0.5.1" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" 31 | 32 | [[package]] 33 | name = "ascii-canvas" 34 | version = "2.0.0" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "ff8eb72df928aafb99fe5d37b383f2fe25bd2a765e3e5f7c365916b6f2463a29" 37 | dependencies = [ 38 | "term", 39 | ] 40 | 41 | [[package]] 42 | name = "atty" 43 | version = "0.2.14" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 46 | dependencies = [ 47 | "hermit-abi", 48 | "libc", 49 | "winapi", 50 | ] 51 | 52 | [[package]] 53 | name = "autocfg" 54 | version = "1.0.1" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 57 | 58 | [[package]] 59 | name = "base64" 60 | version = "0.12.3" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" 63 | 64 | [[package]] 65 | name = "bit-set" 66 | version = "0.5.2" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "6e11e16035ea35e4e5997b393eacbf6f63983188f7a2ad25bfb13465f5ad59de" 69 | dependencies = [ 70 | "bit-vec", 71 | ] 72 | 73 | [[package]] 74 | name = "bit-vec" 75 | version = "0.6.2" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "5f0dc55f2d8a1a85650ac47858bb001b4c0dd73d79e3c455a842925e68d29cd3" 78 | 79 | [[package]] 80 | name = "blake2b_simd" 81 | version = "0.5.10" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" 84 | dependencies = [ 85 | "arrayref", 86 | "arrayvec", 87 | "constant_time_eq", 88 | ] 89 | 90 | [[package]] 91 | name = "block-buffer" 92 | version = "0.7.3" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" 95 | dependencies = [ 96 | "block-padding", 97 | "byte-tools", 98 | "byteorder", 99 | "generic-array", 100 | ] 101 | 102 | [[package]] 103 | name = "block-padding" 104 | version = "0.1.5" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" 107 | dependencies = [ 108 | "byte-tools", 109 | ] 110 | 111 | [[package]] 112 | name = "byte-tools" 113 | version = "0.3.1" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" 116 | 117 | [[package]] 118 | name = "byteorder" 119 | version = "1.3.4" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 122 | 123 | [[package]] 124 | name = "cfg-if" 125 | version = "0.1.10" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 128 | 129 | [[package]] 130 | name = "constant_time_eq" 131 | version = "0.1.5" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" 134 | 135 | [[package]] 136 | name = "crossbeam-utils" 137 | version = "0.7.2" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" 140 | dependencies = [ 141 | "autocfg", 142 | "cfg-if", 143 | "lazy_static", 144 | ] 145 | 146 | [[package]] 147 | name = "diff" 148 | version = "0.1.12" 149 | source = "registry+https://github.com/rust-lang/crates.io-index" 150 | checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" 151 | 152 | [[package]] 153 | name = "digest" 154 | version = "0.8.1" 155 | source = "registry+https://github.com/rust-lang/crates.io-index" 156 | checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" 157 | dependencies = [ 158 | "generic-array", 159 | ] 160 | 161 | [[package]] 162 | name = "dirs" 163 | version = "1.0.5" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" 166 | dependencies = [ 167 | "libc", 168 | "redox_users", 169 | "winapi", 170 | ] 171 | 172 | [[package]] 173 | name = "docopt" 174 | version = "1.1.0" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "7f525a586d310c87df72ebcd98009e57f1cc030c8c268305287a476beb653969" 177 | dependencies = [ 178 | "lazy_static", 179 | "regex", 180 | "serde", 181 | "strsim", 182 | ] 183 | 184 | [[package]] 185 | name = "either" 186 | version = "1.6.1" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 189 | 190 | [[package]] 191 | name = "ena" 192 | version = "0.14.0" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3" 195 | dependencies = [ 196 | "log", 197 | ] 198 | 199 | [[package]] 200 | name = "fake-simd" 201 | version = "0.1.2" 202 | source = "registry+https://github.com/rust-lang/crates.io-index" 203 | checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" 204 | 205 | [[package]] 206 | name = "fixedbitset" 207 | version = "0.2.0" 208 | source = "registry+https://github.com/rust-lang/crates.io-index" 209 | checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" 210 | 211 | [[package]] 212 | name = "generic-array" 213 | version = "0.12.3" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" 216 | dependencies = [ 217 | "typenum", 218 | ] 219 | 220 | [[package]] 221 | name = "getrandom" 222 | version = "0.1.15" 223 | source = "registry+https://github.com/rust-lang/crates.io-index" 224 | checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" 225 | dependencies = [ 226 | "cfg-if", 227 | "libc", 228 | "wasi", 229 | ] 230 | 231 | [[package]] 232 | name = "hashbrown" 233 | version = "0.9.1" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" 236 | 237 | [[package]] 238 | name = "hermit-abi" 239 | version = "0.1.17" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" 242 | dependencies = [ 243 | "libc", 244 | ] 245 | 246 | [[package]] 247 | name = "indexmap" 248 | version = "1.6.0" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" 251 | dependencies = [ 252 | "autocfg", 253 | "hashbrown", 254 | ] 255 | 256 | [[package]] 257 | name = "itertools" 258 | version = "0.9.0" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" 261 | dependencies = [ 262 | "either", 263 | ] 264 | 265 | [[package]] 266 | name = "itoa" 267 | version = "0.4.3" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | checksum = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" 270 | 271 | [[package]] 272 | name = "jexl-eval" 273 | version = "0.3.1-alpha.0" 274 | dependencies = [ 275 | "anyhow", 276 | "jexl-parser", 277 | "serde", 278 | "serde_json", 279 | "thiserror", 280 | ] 281 | 282 | [[package]] 283 | name = "jexl-parser" 284 | version = "0.3.1-alpha.0" 285 | dependencies = [ 286 | "lalrpop-util", 287 | "regex", 288 | ] 289 | 290 | [[package]] 291 | name = "lalrpop" 292 | version = "0.19.0" 293 | source = "registry+https://github.com/rust-lang/crates.io-index" 294 | checksum = "d6f55673d283313791404be21209bb433f128f7e5c451986df107eb5fdbd68d2" 295 | dependencies = [ 296 | "ascii-canvas", 297 | "atty", 298 | "bit-set", 299 | "diff", 300 | "docopt", 301 | "ena", 302 | "itertools", 303 | "lalrpop-util", 304 | "petgraph", 305 | "regex", 306 | "regex-syntax", 307 | "serde", 308 | "serde_derive", 309 | "sha2", 310 | "string_cache", 311 | "term", 312 | "unicode-xid", 313 | ] 314 | 315 | [[package]] 316 | name = "lalrpop-util" 317 | version = "0.19.0" 318 | source = "registry+https://github.com/rust-lang/crates.io-index" 319 | checksum = "f7e88f15a7d31dfa8fb607986819039127f0161058a3b248a146142d276cbd28" 320 | dependencies = [ 321 | "regex", 322 | ] 323 | 324 | [[package]] 325 | name = "lazy_static" 326 | version = "1.4.0" 327 | source = "registry+https://github.com/rust-lang/crates.io-index" 328 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 329 | 330 | [[package]] 331 | name = "libc" 332 | version = "0.2.79" 333 | source = "registry+https://github.com/rust-lang/crates.io-index" 334 | checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743" 335 | 336 | [[package]] 337 | name = "log" 338 | version = "0.4.11" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" 341 | dependencies = [ 342 | "cfg-if", 343 | ] 344 | 345 | [[package]] 346 | name = "memchr" 347 | version = "2.3.3" 348 | source = "registry+https://github.com/rust-lang/crates.io-index" 349 | checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" 350 | 351 | [[package]] 352 | name = "new_debug_unreachable" 353 | version = "1.0.4" 354 | source = "registry+https://github.com/rust-lang/crates.io-index" 355 | checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" 356 | 357 | [[package]] 358 | name = "opaque-debug" 359 | version = "0.2.3" 360 | source = "registry+https://github.com/rust-lang/crates.io-index" 361 | checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" 362 | 363 | [[package]] 364 | name = "parser-gen" 365 | version = "0.3.1-alpha.0" 366 | dependencies = [ 367 | "lalrpop", 368 | ] 369 | 370 | [[package]] 371 | name = "petgraph" 372 | version = "0.5.1" 373 | source = "registry+https://github.com/rust-lang/crates.io-index" 374 | checksum = "467d164a6de56270bd7c4d070df81d07beace25012d5103ced4e9ff08d6afdb7" 375 | dependencies = [ 376 | "fixedbitset", 377 | "indexmap", 378 | ] 379 | 380 | [[package]] 381 | name = "phf_shared" 382 | version = "0.8.0" 383 | source = "registry+https://github.com/rust-lang/crates.io-index" 384 | checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" 385 | dependencies = [ 386 | "siphasher", 387 | ] 388 | 389 | [[package]] 390 | name = "precomputed-hash" 391 | version = "0.1.1" 392 | source = "registry+https://github.com/rust-lang/crates.io-index" 393 | checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" 394 | 395 | [[package]] 396 | name = "proc-macro2" 397 | version = "1.0.19" 398 | source = "registry+https://github.com/rust-lang/crates.io-index" 399 | checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" 400 | dependencies = [ 401 | "unicode-xid", 402 | ] 403 | 404 | [[package]] 405 | name = "quote" 406 | version = "1.0.7" 407 | source = "registry+https://github.com/rust-lang/crates.io-index" 408 | checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" 409 | dependencies = [ 410 | "proc-macro2", 411 | ] 412 | 413 | [[package]] 414 | name = "redox_syscall" 415 | version = "0.1.57" 416 | source = "registry+https://github.com/rust-lang/crates.io-index" 417 | checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" 418 | 419 | [[package]] 420 | name = "redox_users" 421 | version = "0.3.5" 422 | source = "registry+https://github.com/rust-lang/crates.io-index" 423 | checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" 424 | dependencies = [ 425 | "getrandom", 426 | "redox_syscall", 427 | "rust-argon2", 428 | ] 429 | 430 | [[package]] 431 | name = "regex" 432 | version = "1.3.9" 433 | source = "registry+https://github.com/rust-lang/crates.io-index" 434 | checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" 435 | dependencies = [ 436 | "aho-corasick", 437 | "memchr", 438 | "regex-syntax", 439 | "thread_local", 440 | ] 441 | 442 | [[package]] 443 | name = "regex-syntax" 444 | version = "0.6.18" 445 | source = "registry+https://github.com/rust-lang/crates.io-index" 446 | checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" 447 | 448 | [[package]] 449 | name = "rust-argon2" 450 | version = "0.8.2" 451 | source = "registry+https://github.com/rust-lang/crates.io-index" 452 | checksum = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19" 453 | dependencies = [ 454 | "base64", 455 | "blake2b_simd", 456 | "constant_time_eq", 457 | "crossbeam-utils", 458 | ] 459 | 460 | [[package]] 461 | name = "ryu" 462 | version = "0.2.6" 463 | source = "registry+https://github.com/rust-lang/crates.io-index" 464 | checksum = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7" 465 | 466 | [[package]] 467 | name = "serde" 468 | version = "1.0.70" 469 | source = "registry+https://github.com/rust-lang/crates.io-index" 470 | checksum = "0c3adf19c07af6d186d91dae8927b83b0553d07ca56cbf7f2f32560455c91920" 471 | dependencies = [ 472 | "serde_derive", 473 | ] 474 | 475 | [[package]] 476 | name = "serde_derive" 477 | version = "1.0.116" 478 | source = "registry+https://github.com/rust-lang/crates.io-index" 479 | checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8" 480 | dependencies = [ 481 | "proc-macro2", 482 | "quote", 483 | "syn", 484 | ] 485 | 486 | [[package]] 487 | name = "serde_json" 488 | version = "1.0.30" 489 | source = "registry+https://github.com/rust-lang/crates.io-index" 490 | checksum = "7f60a296fed15c3edbbe9aa83b646531459e565c525b0ab628deb1a4b28e4180" 491 | dependencies = [ 492 | "itoa", 493 | "ryu", 494 | "serde", 495 | ] 496 | 497 | [[package]] 498 | name = "sha2" 499 | version = "0.8.2" 500 | source = "registry+https://github.com/rust-lang/crates.io-index" 501 | checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" 502 | dependencies = [ 503 | "block-buffer", 504 | "digest", 505 | "fake-simd", 506 | "opaque-debug", 507 | ] 508 | 509 | [[package]] 510 | name = "siphasher" 511 | version = "0.3.3" 512 | source = "registry+https://github.com/rust-lang/crates.io-index" 513 | checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7" 514 | 515 | [[package]] 516 | name = "string_cache" 517 | version = "0.8.0" 518 | source = "registry+https://github.com/rust-lang/crates.io-index" 519 | checksum = "2940c75beb4e3bf3a494cef919a747a2cb81e52571e212bfbd185074add7208a" 520 | dependencies = [ 521 | "lazy_static", 522 | "new_debug_unreachable", 523 | "phf_shared", 524 | "precomputed-hash", 525 | "serde", 526 | ] 527 | 528 | [[package]] 529 | name = "strsim" 530 | version = "0.9.3" 531 | source = "registry+https://github.com/rust-lang/crates.io-index" 532 | checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" 533 | 534 | [[package]] 535 | name = "syn" 536 | version = "1.0.38" 537 | source = "registry+https://github.com/rust-lang/crates.io-index" 538 | checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4" 539 | dependencies = [ 540 | "proc-macro2", 541 | "quote", 542 | "unicode-xid", 543 | ] 544 | 545 | [[package]] 546 | name = "term" 547 | version = "0.5.2" 548 | source = "registry+https://github.com/rust-lang/crates.io-index" 549 | checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" 550 | dependencies = [ 551 | "byteorder", 552 | "dirs", 553 | "winapi", 554 | ] 555 | 556 | [[package]] 557 | name = "thiserror" 558 | version = "1.0.20" 559 | source = "registry+https://github.com/rust-lang/crates.io-index" 560 | checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" 561 | dependencies = [ 562 | "thiserror-impl", 563 | ] 564 | 565 | [[package]] 566 | name = "thiserror-impl" 567 | version = "1.0.20" 568 | source = "registry+https://github.com/rust-lang/crates.io-index" 569 | checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" 570 | dependencies = [ 571 | "proc-macro2", 572 | "quote", 573 | "syn", 574 | ] 575 | 576 | [[package]] 577 | name = "thread_local" 578 | version = "1.0.1" 579 | source = "registry+https://github.com/rust-lang/crates.io-index" 580 | checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" 581 | dependencies = [ 582 | "lazy_static", 583 | ] 584 | 585 | [[package]] 586 | name = "typenum" 587 | version = "1.12.0" 588 | source = "registry+https://github.com/rust-lang/crates.io-index" 589 | checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" 590 | 591 | [[package]] 592 | name = "unicode-xid" 593 | version = "0.2.1" 594 | source = "registry+https://github.com/rust-lang/crates.io-index" 595 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 596 | 597 | [[package]] 598 | name = "wasi" 599 | version = "0.9.0+wasi-snapshot-preview1" 600 | source = "registry+https://github.com/rust-lang/crates.io-index" 601 | checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" 602 | 603 | [[package]] 604 | name = "winapi" 605 | version = "0.3.9" 606 | source = "registry+https://github.com/rust-lang/crates.io-index" 607 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 608 | dependencies = [ 609 | "winapi-i686-pc-windows-gnu", 610 | "winapi-x86_64-pc-windows-gnu", 611 | ] 612 | 613 | [[package]] 614 | name = "winapi-i686-pc-windows-gnu" 615 | version = "0.4.0" 616 | source = "registry+https://github.com/rust-lang/crates.io-index" 617 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 618 | 619 | [[package]] 620 | name = "winapi-x86_64-pc-windows-gnu" 621 | version = "0.4.0" 622 | source = "registry+https://github.com/rust-lang/crates.io-index" 623 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 624 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "jexl-parser", 4 | "jexl-eval", 5 | "parser-gen", 6 | ] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jexl-rs [![CircleCI](https://circleci.com/gh/mozilla/jexl-rs/tree/main.svg?style=svg)](https://circleci.com/gh/mozilla/jexl-rs/tree/main) 2 | 3 | > [JEXL](https://www.npmjs.com/package/jexl) in Rust 4 | 5 | 6 | * jexl-eval: [![](https://img.shields.io/crates/v/jexl-eval.svg)](https://crates.io/crates/jexl-eval) [![](https://docs.rs/jexl-eval/badge.svg?v=2)](https://docs.rs/jexl-eval) 7 | * jexl-parser: [![](https://img.shields.io/crates/v/jexl-parser.svg)](https://crates.io/crates/jexl-parser) [![](https://docs.rs/jexl-parser/badge.svg?v=2)](https://docs.rs/jexl-parser) 8 | 9 | ## Releases 10 | 11 | Releases are done using `cargo release`. Check [cargo-release](https://github.com/sunng87/cargo-release) for more information 12 | 13 | ## License 14 | 15 | This project is licensed under the [Mozilla Public License 2.0](https://github.com/tarikeshaq/update-notifier/blob/master/LICENSE) 16 | -------------------------------------------------------------------------------- /jexl-eval/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | edition = '2018' 3 | name = "jexl-eval" 4 | version = "0.3.1-alpha.0" 5 | authors = ["Mike Cooper ", "The Sync Team ", "The Glean Team "] 6 | description = "A JEXL evaluator written in Rust" 7 | license = "MPL-2.0" 8 | repository = "https://github.com/mozilla/jexl-rs" 9 | 10 | [dependencies] 11 | jexl-parser = { version = "^0.3.1-alpha.0", path = "../jexl-parser" } 12 | serde_json = "1" 13 | serde = "1" 14 | thiserror = "1" 15 | anyhow = "1" 16 | -------------------------------------------------------------------------------- /jexl-eval/src/error.rs: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | use jexl_parser::{ast::OpCode, ParseError, Token}; 6 | 7 | use serde_json::Value; 8 | 9 | pub type Result<'a, T, E = EvaluationError<'a>> = std::result::Result; 10 | #[derive(Debug, thiserror::Error)] 11 | pub enum EvaluationError<'a> { 12 | #[error("Parsing error: {0}")] 13 | ParseError(Box, &'a str>>), 14 | #[error("Invalid binary operation, left: {left}, right: {right}, operation: {operation}")] 15 | InvalidBinaryOp { 16 | left: Value, 17 | right: Value, 18 | operation: OpCode, 19 | }, 20 | #[error("Unknown transform: {0}")] 21 | UnknownTransform(String), 22 | #[error("Duplicate object key: {0}")] 23 | DuplicateObjectKey(String), 24 | #[error("Identifier '{0}' is undefined")] 25 | UndefinedIdentifier(String), 26 | #[error("Invalid context provided")] 27 | InvalidContext, 28 | #[error("Invalid index type")] 29 | InvalidIndexType, 30 | #[error("Invalid json: {0}")] 31 | JSONError(#[from] serde_json::Error), 32 | #[error("Custom error: {0}")] 33 | CustomError(#[from] anyhow::Error), 34 | #[error("Invalid filter")] 35 | InvalidFilter, 36 | } 37 | 38 | impl<'a> From, &'a str>> for EvaluationError<'a> { 39 | fn from(cause: ParseError, &'a str>) -> Self { 40 | EvaluationError::ParseError(Box::new(cause)) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /jexl-eval/src/lib.rs: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | //! A JEXL evaluator written in Rust 6 | //! This crate depends on a JEXL parser crate that handles all the parsing 7 | //! and is a part of the same workspace. 8 | //! JEXL is an expression language used by Mozilla, you can find more information here: https://github.com/mozilla/mozjexl 9 | //! 10 | //! # How to use 11 | //! The access point for this crate is the `eval` functions of the Evaluator Struct 12 | //! You can use the `eval` function directly to evaluate standalone statements 13 | //! 14 | //! For example: 15 | //! ```rust 16 | //! use jexl_eval::Evaluator; 17 | //! use serde_json::json as value; 18 | //! let evaluator = Evaluator::new(); 19 | //! assert_eq!(evaluator.eval("'Hello ' + 'World'").unwrap(), value!("Hello World")); 20 | //! ``` 21 | //! 22 | //! You can also run the statements against a context using the `eval_in_context` function 23 | //! The context can be any type that implements the `serde::Serializable` trait 24 | //! and the function will return errors if the statement doesn't match the context 25 | //! 26 | //! For example: 27 | //! ```rust 28 | //! use jexl_eval::Evaluator; 29 | //! use serde_json::json as value; 30 | //! let context = value!({"a": {"b": 2.0}}); 31 | //! let evaluator = Evaluator::new(); 32 | //! assert_eq!(evaluator.eval_in_context("a.b", context).unwrap(), value!(2.0)); 33 | //! ``` 34 | //! 35 | 36 | use jexl_parser::{ 37 | ast::{Expression, OpCode}, 38 | Parser, 39 | }; 40 | use serde_json::{json as value, Value}; 41 | 42 | pub mod error; 43 | use error::*; 44 | use std::collections::HashMap; 45 | 46 | const EPSILON: f64 = 0.000001f64; 47 | 48 | trait Truthy { 49 | fn is_truthy(&self) -> bool; 50 | } 51 | 52 | impl Truthy for Value { 53 | fn is_truthy(&self) -> bool { 54 | match self { 55 | Value::Bool(b) => *b, 56 | Value::Null => false, 57 | Value::Number(f) => f.as_f64().unwrap() != 0.0, 58 | Value::String(s) => !s.is_empty(), 59 | // It would be better if these depended on the contents of the 60 | // object (empty array/object is falsey, non-empty is truthy, like 61 | // in Python) but this matches JS semantics. Is it worth changing? 62 | Value::Array(_) => true, 63 | Value::Object(_) => true, 64 | } 65 | } 66 | } 67 | 68 | impl<'b> Truthy for Result<'b, Value> { 69 | fn is_truthy(&self) -> bool { 70 | match self { 71 | Ok(v) => v.is_truthy(), 72 | _ => false, 73 | } 74 | } 75 | } 76 | 77 | type Context = Value; 78 | 79 | /// TransformFn represents an arbitrary transform function 80 | /// Transform functions take an arbitrary number of `serde_json::Value`to represent their arguments 81 | /// and return a `serde_json::Value`. 82 | /// the transform function itself is responsible for checking if the format and number of 83 | /// the arguments is correct 84 | /// 85 | /// Returns a Result with an `anyhow::Error`. This allows consumers to return their own custom errors 86 | /// in the closure, and use `.into` to convert it into an `anyhow::Error`. The error message will be perserved 87 | pub type TransformFn<'a> = Box Result + Send + Sync + 'a>; 88 | 89 | #[derive(Default)] 90 | pub struct Evaluator<'a> { 91 | transforms: HashMap>, 92 | } 93 | 94 | impl<'a> Evaluator<'a> { 95 | pub fn new() -> Self { 96 | Self::default() 97 | } 98 | 99 | /// Adds a custom transform function 100 | /// This is meant as a way to allow consumers to add their own custom functionality 101 | /// to the expression language. 102 | /// Note that the name added here has to match with 103 | /// the name that the transform will have when it's a part of the expression statement 104 | /// 105 | /// # Arguments: 106 | /// - `name`: The name of the transfrom 107 | /// - `transform`: The actual function. A closure the implements Fn(&[serde_json::Value]) -> Result 108 | /// 109 | /// # Example: 110 | /// 111 | /// ```rust 112 | /// use jexl_eval::Evaluator; 113 | /// use serde_json::{json as value, Value}; 114 | /// 115 | /// let mut evaluator = Evaluator::new().with_transform("lower", |v: &[Value]| { 116 | /// let s = v 117 | /// .first() 118 | /// .expect("Should have 1 argument!") 119 | /// .as_str() 120 | /// .expect("Should be a string!"); 121 | /// Ok(value!(s.to_lowercase())) 122 | /// }); 123 | /// 124 | /// assert_eq!(evaluator.eval("'JOHN DOe'|lower").unwrap(), value!("john doe")) 125 | /// ``` 126 | pub fn with_transform(mut self, name: &str, transform: F) -> Self 127 | where 128 | F: Fn(&[Value]) -> Result + Send + Sync + 'a, 129 | { 130 | self.transforms 131 | .insert(name.to_string(), Box::new(transform)); 132 | self 133 | } 134 | 135 | pub fn eval<'b>(&self, input: &'b str) -> Result<'b, Value> { 136 | let context = value!({}); 137 | self.eval_in_context(input, &context) 138 | } 139 | 140 | pub fn eval_in_context<'b, T: serde::Serialize>( 141 | &self, 142 | input: &'b str, 143 | context: T, 144 | ) -> Result<'b, Value> { 145 | let tree = Parser::parse(input)?; 146 | let context = serde_json::to_value(context)?; 147 | if !context.is_object() { 148 | return Err(EvaluationError::InvalidContext); 149 | } 150 | self.eval_ast(tree, &context) 151 | } 152 | 153 | fn eval_ast<'b>(&self, ast: Expression, context: &Context) -> Result<'b, Value> { 154 | match ast { 155 | Expression::Number(n) => Ok(value!(n)), 156 | Expression::Boolean(b) => Ok(value!(b)), 157 | Expression::String(s) => Ok(value!(s)), 158 | Expression::Array(xs) => xs.into_iter().map(|x| self.eval_ast(*x, context)).collect(), 159 | Expression::Null => Ok(Value::Null), 160 | 161 | Expression::Object(items) => { 162 | let mut map = serde_json::Map::with_capacity(items.len()); 163 | for (key, expr) in items.into_iter() { 164 | if map.contains_key(&key) { 165 | return Err(EvaluationError::DuplicateObjectKey(key)); 166 | } 167 | let value = self.eval_ast(*expr, context)?; 168 | map.insert(key, value); 169 | } 170 | Ok(Value::Object(map)) 171 | } 172 | 173 | Expression::Identifier(inner) => match context.get(&inner) { 174 | Some(v) => Ok(v.clone()), 175 | _ => Err(EvaluationError::UndefinedIdentifier(inner.clone())), 176 | }, 177 | 178 | Expression::DotOperation { subject, ident } => { 179 | let subject = self.eval_ast(*subject, context)?; 180 | Ok(subject.get(&ident).unwrap_or(&value!(null)).clone()) 181 | } 182 | 183 | Expression::IndexOperation { subject, index } => { 184 | let subject = self.eval_ast(*subject, context)?; 185 | if let Expression::Filter { ident, op, right } = *index { 186 | let subject_arr = subject.as_array().ok_or(EvaluationError::InvalidFilter)?; 187 | let right = self.eval_ast(*right, context)?; 188 | let filtered = subject_arr 189 | .iter() 190 | .filter(|e| { 191 | let left = e.get(&ident).unwrap_or(&value!(null)); 192 | // returns false if any members fail the op, could happen if array members are missing the identifier 193 | Self::apply_op(op, left.clone(), right.clone()) 194 | .unwrap_or(value!(false)) 195 | .is_truthy() 196 | }) 197 | .collect::>(); 198 | return Ok(value!(filtered)); 199 | } 200 | 201 | let index = self.eval_ast(*index, context)?; 202 | match index { 203 | Value::String(inner) => { 204 | Ok(subject.get(&inner).unwrap_or(&value!(null)).clone()) 205 | } 206 | Value::Number(inner) => Ok(subject 207 | .get(inner.as_f64().unwrap().floor() as usize) 208 | .unwrap_or(&value!(null)) 209 | .clone()), 210 | _ => Err(EvaluationError::InvalidIndexType), 211 | } 212 | } 213 | 214 | Expression::BinaryOperation { 215 | left, 216 | right, 217 | operation, 218 | } => self.eval_op(operation, left, right, context), 219 | Expression::Transform { 220 | name, 221 | subject, 222 | args, 223 | } => { 224 | let subject = self.eval_ast(*subject, context)?; 225 | let mut args_arr = Vec::new(); 226 | args_arr.push(subject); 227 | if let Some(args) = args { 228 | for arg in args { 229 | args_arr.push(self.eval_ast(*arg, context)?); 230 | } 231 | } 232 | let f = self 233 | .transforms 234 | .get(&name) 235 | .ok_or(EvaluationError::UnknownTransform(name))?; 236 | f(&args_arr).map_err(|e| e.into()) 237 | } 238 | 239 | Expression::Conditional { 240 | left, 241 | truthy, 242 | falsy, 243 | } => { 244 | if self.eval_ast(*left, context).is_truthy() { 245 | self.eval_ast(*truthy, context) 246 | } else { 247 | self.eval_ast(*falsy, context) 248 | } 249 | } 250 | 251 | Expression::Filter { 252 | ident: _, 253 | op: _, 254 | right: _, 255 | } => { 256 | // Filters shouldn't be evaluated individually 257 | // instead, they are evaluated as a part of an IndexOperation 258 | return Err(EvaluationError::InvalidFilter); 259 | } 260 | } 261 | } 262 | 263 | fn eval_op<'b>( 264 | &self, 265 | operation: OpCode, 266 | left: Box, 267 | right: Box, 268 | context: &Context, 269 | ) -> Result<'b, Value> { 270 | let left = self.eval_ast(*left, context); 271 | 272 | // We want to delay evaluating the right hand side in the cases of AND and OR. 273 | let eval_right = || self.eval_ast(*right, context); 274 | Ok(match operation { 275 | OpCode::Or => { 276 | if left.is_truthy() { 277 | left? 278 | } else { 279 | eval_right()? 280 | } 281 | } 282 | OpCode::And => { 283 | if left.is_truthy() { 284 | eval_right()? 285 | } else { 286 | left? 287 | } 288 | } 289 | _ => Self::apply_op(operation, left?, eval_right()?)?, 290 | }) 291 | } 292 | 293 | fn apply_op<'b>(operation: OpCode, left: Value, right: Value) -> Result<'b, Value> { 294 | match (operation, left, right) { 295 | (OpCode::NotEqual, a, b) => { 296 | // Implement NotEquals as the inverse of Equals. 297 | let value = Self::apply_op(OpCode::Equal, a, b)?; 298 | let equality = value 299 | .as_bool() 300 | .unwrap_or_else(|| unreachable!("Equality always returns a bool")); 301 | Ok(value!(!equality)) 302 | } 303 | 304 | (OpCode::And, a, b) => Ok(if a.is_truthy() { b } else { a }), 305 | (OpCode::Or, a, b) => Ok(if a.is_truthy() { a } else { b }), 306 | 307 | (op, Value::Number(a), Value::Number(b)) => { 308 | let left = a.as_f64().unwrap(); 309 | let right = b.as_f64().unwrap(); 310 | Ok(match op { 311 | OpCode::Add => value!(left + right), 312 | OpCode::Subtract => value!(left - right), 313 | OpCode::Multiply => value!(left * right), 314 | OpCode::Divide => value!(left / right), 315 | OpCode::FloorDivide => value!((left / right).floor()), 316 | OpCode::Modulus => value!(left % right), 317 | OpCode::Exponent => value!(left.powf(right)), 318 | OpCode::Less => value!(left < right), 319 | OpCode::Greater => value!(left > right), 320 | OpCode::LessEqual => value!(left <= right), 321 | OpCode::GreaterEqual => value!(left >= right), 322 | OpCode::Equal => value!((left - right).abs() < EPSILON), 323 | OpCode::NotEqual => value!((left - right).abs() >= EPSILON), 324 | OpCode::In => value!(false), 325 | OpCode::And | OpCode::Or => { 326 | unreachable!("Covered by previous case in parent match") 327 | } 328 | }) 329 | } 330 | 331 | (op, Value::String(a), Value::String(b)) => match op { 332 | OpCode::Equal => Ok(value!(a == b)), 333 | 334 | OpCode::Add => Ok(value!(format!("{}{}", a, b))), 335 | OpCode::In => Ok(value!(b.contains(&a))), 336 | 337 | OpCode::Less => Ok(value!(a < b)), 338 | OpCode::Greater => Ok(value!(a > b)), 339 | OpCode::LessEqual => Ok(value!(a <= b)), 340 | OpCode::GreaterEqual => Ok(value!(a >= b)), 341 | 342 | _ => Err(EvaluationError::InvalidBinaryOp { 343 | operation, 344 | left: value!(a), 345 | right: value!(b), 346 | }), 347 | }, 348 | 349 | (OpCode::In, left, Value::Array(v)) => Ok(value!(v.contains(&left))), 350 | (OpCode::In, Value::String(left), Value::Object(v)) => { 351 | Ok(value!(v.contains_key(&left))) 352 | } 353 | 354 | (OpCode::Equal, a, b) => match (a, b) { 355 | // Number == Number is handled above 356 | // String == String is handled above 357 | (Value::Bool(a), Value::Bool(b)) => Ok(value!(a == b)), 358 | (Value::Null, Value::Null) => Ok(value!(true)), 359 | (Value::Array(a), Value::Array(b)) => Ok(value!(a == b)), 360 | (Value::Object(a), Value::Object(b)) => Ok(value!(a == b)), 361 | // If the types don't match, it's always false 362 | _ => Ok(value!(false)), 363 | }, 364 | 365 | (operation, left, right) => Err(EvaluationError::InvalidBinaryOp { 366 | operation, 367 | left, 368 | right, 369 | }), 370 | } 371 | } 372 | } 373 | 374 | #[cfg(test)] 375 | mod tests { 376 | use super::*; 377 | use serde_json::json as value; 378 | 379 | #[test] 380 | fn test_literal() { 381 | assert_eq!(Evaluator::new().eval("1").unwrap(), value!(1.0)); 382 | } 383 | 384 | #[test] 385 | fn test_binary_expression_addition() { 386 | assert_eq!(Evaluator::new().eval("1 + 2").unwrap(), value!(3.0)); 387 | } 388 | 389 | #[test] 390 | fn test_binary_expression_multiplication() { 391 | assert_eq!(Evaluator::new().eval("2 * 3").unwrap(), value!(6.0)); 392 | } 393 | 394 | #[test] 395 | fn test_precedence() { 396 | assert_eq!(Evaluator::new().eval("2 + 3 * 4").unwrap(), value!(14.0)); 397 | } 398 | 399 | #[test] 400 | fn test_parenthesis() { 401 | assert_eq!(Evaluator::new().eval("(2 + 3) * 4").unwrap(), value!(20.0)); 402 | } 403 | 404 | #[test] 405 | fn test_string_concat() { 406 | assert_eq!( 407 | Evaluator::new().eval("'Hello ' + 'World'").unwrap(), 408 | value!("Hello World") 409 | ); 410 | } 411 | 412 | #[test] 413 | fn test_true_comparison() { 414 | assert_eq!(Evaluator::new().eval("2 > 1").unwrap(), value!(true)); 415 | } 416 | 417 | #[test] 418 | fn test_false_comparison() { 419 | assert_eq!(Evaluator::new().eval("2 <= 1").unwrap(), value!(false)); 420 | } 421 | 422 | #[test] 423 | fn test_boolean_logic() { 424 | assert_eq!( 425 | Evaluator::new() 426 | .eval("'foo' && 6 >= 6 && 0 + 1 && true") 427 | .unwrap(), 428 | value!(true) 429 | ); 430 | } 431 | 432 | #[test] 433 | fn test_identifier() { 434 | let context = value!({"a": 1.0}); 435 | assert_eq!( 436 | Evaluator::new().eval_in_context("a", context).unwrap(), 437 | value!(1.0) 438 | ); 439 | } 440 | 441 | #[test] 442 | fn test_identifier_chain() { 443 | let context = value!({"a": {"b": 2.0}}); 444 | assert_eq!( 445 | Evaluator::new().eval_in_context("a.b", context).unwrap(), 446 | value!(2.0) 447 | ); 448 | } 449 | 450 | #[test] 451 | fn test_context_filter_arrays() { 452 | let context = value!({ 453 | "foo": { 454 | "bar": [ 455 | {"tek": "hello"}, 456 | {"tek": "baz"}, 457 | {"tok": "baz"}, 458 | ] 459 | } 460 | }); 461 | assert_eq!( 462 | Evaluator::new() 463 | .eval_in_context("foo.bar[.tek == 'baz']", &context) 464 | .unwrap(), 465 | value!([{"tek": "baz"}]) 466 | ); 467 | } 468 | 469 | #[test] 470 | fn test_context_array_index() { 471 | let context = value!({ 472 | "foo": { 473 | "bar": [ 474 | {"tek": "hello"}, 475 | {"tek": "baz"}, 476 | {"tok": "baz"}, 477 | ] 478 | } 479 | }); 480 | assert_eq!( 481 | Evaluator::new() 482 | .eval_in_context("foo.bar[1].tek", context) 483 | .unwrap(), 484 | value!("baz") 485 | ); 486 | } 487 | 488 | #[test] 489 | fn test_object_expression_properties() { 490 | let context = value!({"foo": {"baz": {"bar": "tek"}}}); 491 | assert_eq!( 492 | Evaluator::new() 493 | .eval_in_context("foo['ba' + 'z'].bar", &context) 494 | .unwrap(), 495 | value!("tek") 496 | ); 497 | } 498 | 499 | #[test] 500 | fn test_divfloor() { 501 | assert_eq!(Evaluator::new().eval("7 // 2").unwrap(), value!(3.0)); 502 | } 503 | 504 | #[test] 505 | fn test_empty_object_literal() { 506 | assert_eq!(Evaluator::new().eval("{}").unwrap(), value!({})); 507 | } 508 | 509 | #[test] 510 | fn test_object_literal_strings() { 511 | assert_eq!( 512 | Evaluator::new().eval("{'foo': {'bar': 'tek'}}").unwrap(), 513 | value!({"foo": {"bar": "tek"}}) 514 | ); 515 | } 516 | 517 | #[test] 518 | fn test_object_literal_identifiers() { 519 | assert_eq!( 520 | Evaluator::new().eval("{foo: {bar: 'tek'}}").unwrap(), 521 | value!({"foo": {"bar": "tek"}}) 522 | ); 523 | } 524 | 525 | #[test] 526 | fn test_object_literal_properties() { 527 | assert_eq!( 528 | Evaluator::new().eval("{foo: 'bar'}.foo").unwrap(), 529 | value!("bar") 530 | ); 531 | } 532 | 533 | #[test] 534 | fn test_array_literal() { 535 | assert_eq!( 536 | Evaluator::new().eval("['foo', 1+2]").unwrap(), 537 | value!(["foo", 3.0]) 538 | ); 539 | } 540 | 541 | #[test] 542 | fn test_array_literal_indexing() { 543 | assert_eq!(Evaluator::new().eval("[1, 2, 3][1]").unwrap(), value!(2.0)); 544 | } 545 | 546 | #[test] 547 | fn test_in_operator_string() { 548 | assert_eq!( 549 | Evaluator::new().eval("'bar' in 'foobartek'").unwrap(), 550 | value!(true) 551 | ); 552 | assert_eq!( 553 | Evaluator::new().eval("'baz' in 'foobartek'").unwrap(), 554 | value!(false) 555 | ); 556 | } 557 | 558 | #[test] 559 | fn test_in_operator_array() { 560 | assert_eq!( 561 | Evaluator::new() 562 | .eval("'bar' in ['foo', 'bar', 'tek']") 563 | .unwrap(), 564 | value!(true) 565 | ); 566 | assert_eq!( 567 | Evaluator::new() 568 | .eval("'baz' in ['foo', 'bar', 'tek']") 569 | .unwrap(), 570 | value!(false) 571 | ); 572 | } 573 | 574 | #[test] 575 | fn test_in_operator_object() { 576 | assert_eq!( 577 | Evaluator::new() 578 | .eval("'bar' in {foo: 1, bar: 2, tek: 3}") 579 | .unwrap(), 580 | value!(true) 581 | ); 582 | assert_eq!( 583 | Evaluator::new() 584 | .eval("'baz' in {foo: 1, bar: 2, tek: 3}") 585 | .unwrap(), 586 | value!(false) 587 | ); 588 | } 589 | 590 | #[test] 591 | fn test_conditional_expression() { 592 | assert_eq!( 593 | Evaluator::new().eval("'foo' ? 1 : 2").unwrap(), 594 | value!(1f64) 595 | ); 596 | assert_eq!(Evaluator::new().eval("'' ? 1 : 2").unwrap(), value!(2f64)); 597 | } 598 | 599 | #[test] 600 | fn test_arbitrary_whitespace() { 601 | assert_eq!( 602 | Evaluator::new().eval("(\t2\n+\n3) *\n4\n\r\n").unwrap(), 603 | value!(20.0) 604 | ); 605 | } 606 | 607 | #[test] 608 | fn test_non_integer() { 609 | assert_eq!(Evaluator::new().eval("1.5 * 3.0").unwrap(), value!(4.5)); 610 | } 611 | 612 | #[test] 613 | fn test_string_literal() { 614 | assert_eq!( 615 | Evaluator::new().eval("'hello world'").unwrap(), 616 | value!("hello world") 617 | ); 618 | assert_eq!( 619 | Evaluator::new().eval("\"hello world\"").unwrap(), 620 | value!("hello world") 621 | ); 622 | } 623 | 624 | #[test] 625 | fn test_string_escapes() { 626 | assert_eq!(Evaluator::new().eval("'a\\'b'").unwrap(), value!("a'b")); 627 | assert_eq!(Evaluator::new().eval("\"a\\\"b\"").unwrap(), value!("a\"b")); 628 | } 629 | 630 | #[test] 631 | // Test a very simple transform that applies to_lowercase to a string 632 | fn test_simple_transform() { 633 | let evaluator = Evaluator::new().with_transform("lower", |v: &[Value]| { 634 | let s = v 635 | .get(0) 636 | .expect("There should be one argument!") 637 | .as_str() 638 | .expect("Should be a string!"); 639 | Ok(value!(s.to_lowercase())) 640 | }); 641 | assert_eq!(evaluator.eval("'T_T'|lower").unwrap(), value!("t_t")); 642 | } 643 | 644 | #[test] 645 | // Test returning an UnknownTransform error if a transform is unknown 646 | fn test_missing_transform() { 647 | let err = Evaluator::new().eval("'hello'|world").unwrap_err(); 648 | if let EvaluationError::UnknownTransform(transform) = err { 649 | assert_eq!(transform, "world") 650 | } else { 651 | panic!("Should have thrown an unknown transform error") 652 | } 653 | } 654 | 655 | #[test] 656 | // Test returning an UndefinedIdentifier error if an identifier is unknown 657 | fn test_undefined_identifier() { 658 | let err = Evaluator::new().eval("not_defined").unwrap_err(); 659 | if let EvaluationError::UndefinedIdentifier(id) = err { 660 | assert_eq!(id, "not_defined") 661 | } else { 662 | panic!("Should have thrown an undefined identifier error") 663 | } 664 | } 665 | 666 | #[test] 667 | // Test returning an UndefinedIdentifier error if an identifier is unknown 668 | fn test_undefined_identifier_truthy_ops() { 669 | let err = Evaluator::new().eval("not_defined").unwrap_err(); 670 | if let EvaluationError::UndefinedIdentifier(id) = err { 671 | assert_eq!(id, "not_defined") 672 | } else { 673 | panic!("Should have thrown an undefined identifier error") 674 | } 675 | 676 | let evaluator = Evaluator::new(); 677 | let context = value!({ 678 | "NULL": null, 679 | "DEFINED": "string", 680 | }); 681 | 682 | let test = |expr: &str, is_ok: bool, exp: Value| { 683 | let obs = evaluator.eval_in_context(&expr, context.clone()); 684 | if !is_ok { 685 | assert!(obs.is_err()); 686 | assert!(matches!( 687 | obs.unwrap_err(), 688 | EvaluationError::UndefinedIdentifier(_) 689 | )); 690 | } else { 691 | assert_eq!(obs.unwrap(), exp,); 692 | } 693 | }; 694 | 695 | test("UNDEFINED", false, value!(null)); 696 | test("UNDEFINED == 'string'", false, value!(null)); 697 | test("'string' == UNDEFINED", false, value!(null)); 698 | 699 | test("UNDEFINED ? 'WRONG' : 'RIGHT'", true, value!("RIGHT")); 700 | test("DEFINED ? UNDEFINED : 'WRONG'", false, value!(null)); 701 | 702 | test("UNDEFINED || 'RIGHT'", true, value!("RIGHT")); 703 | test("'RIGHT' || UNDEFINED", true, value!("RIGHT")); 704 | 705 | test("'WRONG' && UNDEFINED", false, value!(null)); 706 | test("UNDEFINED && 'WRONG'", false, value!(null)); 707 | 708 | test("UNDEFINED && 'WRONG'", false, value!(null)); 709 | 710 | test( 711 | "(UNDEFINED && UNDEFINED == 'string') || (DEFINED && DEFINED == 'string')", 712 | true, 713 | value!(true), 714 | ); 715 | } 716 | 717 | #[test] 718 | fn test_add_multiple_transforms() { 719 | let evaluator = Evaluator::new() 720 | .with_transform("sqrt", |v: &[Value]| { 721 | let num = v 722 | .first() 723 | .expect("There should be one argument!") 724 | .as_f64() 725 | .expect("Should be a valid number!"); 726 | Ok(value!(num.sqrt() as u64)) 727 | }) 728 | .with_transform("square", |v: &[Value]| { 729 | let num = v 730 | .first() 731 | .expect("There should be one argument!") 732 | .as_f64() 733 | .expect("Should be a valid number!"); 734 | Ok(value!((num as u64).pow(2))) 735 | }); 736 | 737 | assert_eq!(evaluator.eval("4|square").unwrap(), value!(16)); 738 | assert_eq!(evaluator.eval("4|sqrt").unwrap(), value!(2)); 739 | assert_eq!(evaluator.eval("4|square|sqrt").unwrap(), value!(4)); 740 | } 741 | 742 | #[test] 743 | fn test_transform_with_argument() { 744 | let evaluator = Evaluator::new().with_transform("split", |args: &[Value]| { 745 | let s = args 746 | .first() 747 | .expect("Should be a first argument!") 748 | .as_str() 749 | .expect("Should be a string!"); 750 | let c = args 751 | .get(1) 752 | .expect("There should be a second argument!") 753 | .as_str() 754 | .expect("Should be a string"); 755 | let res: Vec<&str> = s.split_terminator(c).collect(); 756 | Ok(value!(res)) 757 | }); 758 | 759 | assert_eq!( 760 | evaluator.eval("'John Doe'|split(' ')").unwrap(), 761 | value!(vec!["John", "Doe"]) 762 | ); 763 | } 764 | 765 | #[derive(Debug, thiserror::Error)] 766 | enum CustomError { 767 | #[error("Invalid argument in transform!")] 768 | InvalidArgument, 769 | } 770 | 771 | #[test] 772 | fn test_custom_error_message() { 773 | let evaluator = Evaluator::new().with_transform("error", |_: &[Value]| { 774 | Err(CustomError::InvalidArgument.into()) 775 | }); 776 | let res = evaluator.eval("1234|error"); 777 | assert!(res.is_err()); 778 | if let EvaluationError::CustomError(e) = res.unwrap_err() { 779 | assert_eq!(e.to_string(), "Invalid argument in transform!") 780 | } else { 781 | panic!("Should have returned a Custom error!") 782 | } 783 | } 784 | 785 | #[test] 786 | fn test_filter_collections_many_returned() { 787 | let evaluator = Evaluator::new(); 788 | let context = value!({ 789 | "foo": [ 790 | {"bobo": 50, "fofo": 100}, 791 | {"bobo": 60, "baz": 90}, 792 | {"bobo": 10, "bar": 83}, 793 | {"bobo": 20, "yam": 12}, 794 | ] 795 | }); 796 | let exp = "foo[.bobo >= 50]"; 797 | assert_eq!( 798 | evaluator.eval_in_context(exp, context).unwrap(), 799 | value!([{"bobo": 50, "fofo": 100}, {"bobo": 60, "baz": 90}]) 800 | ); 801 | } 802 | 803 | #[test] 804 | fn test_binary_op_eq_ne() { 805 | let evaluator = Evaluator::new(); 806 | let context = value!({ 807 | "NULL": null, 808 | "STRING": "string", 809 | "BOOLEAN": true, 810 | "NUMBER": 42, 811 | "OBJECT": { "x": 1, "y": 2 }, 812 | "ARRAY": [ "string" ] 813 | }); 814 | 815 | let test = |l: &str, r: &str, exp: bool| { 816 | let expr = format!("{} == {}", l, r); 817 | assert_eq!( 818 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 819 | value!(exp) 820 | ); 821 | 822 | let expr = format!("{} != {}", l, r); 823 | assert_eq!( 824 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 825 | value!(!exp) 826 | ); 827 | }; 828 | 829 | test("STRING", "'string'", true); 830 | test("NUMBER", "42", true); 831 | test("BOOLEAN", "true", true); 832 | test("OBJECT", "OBJECT", true); 833 | test("ARRAY", "[ 'string' ]", true); 834 | test("NULL", "null", true); 835 | 836 | test("OBJECT", "{ 'x': 1, 'y': 2 }", false); 837 | 838 | test("STRING", "NULL", false); 839 | test("NUMBER", "NULL", false); 840 | test("BOOLEAN", "NULL", false); 841 | // test("NULL", "NULL", false); 842 | test("OBJECT", "NULL", false); 843 | test("ARRAY", "NULL", false); 844 | 845 | // test("STRING", "STRING", false); 846 | test("NUMBER", "STRING", false); 847 | test("BOOLEAN", "STRING", false); 848 | test("NULL", "STRING", false); 849 | test("OBJECT", "STRING", false); 850 | test("ARRAY", "STRING", false); 851 | 852 | test("STRING", "NUMBER", false); 853 | // test("NUMBER", "NUMBER", false); 854 | test("BOOLEAN", "NUMBER", false); 855 | test("NULL", "NUMBER", false); 856 | test("OBJECT", "NUMBER", false); 857 | test("ARRAY", "NUMBER", false); 858 | 859 | test("STRING", "BOOLEAN", false); 860 | test("NUMBER", "BOOLEAN", false); 861 | // test("BOOLEAN", "BOOLEAN", false); 862 | test("NULL", "BOOLEAN", false); 863 | test("OBJECT", "BOOLEAN", false); 864 | test("ARRAY", "BOOLEAN", false); 865 | 866 | test("STRING", "OBJECT", false); 867 | test("NUMBER", "OBJECT", false); 868 | test("BOOLEAN", "OBJECT", false); 869 | test("NULL", "OBJECT", false); 870 | // test("OBJECT", "OBJECT", false); 871 | test("ARRAY", "OBJECT", false); 872 | 873 | test("STRING", "ARRAY", false); 874 | test("NUMBER", "ARRAY", false); 875 | test("BOOLEAN", "ARRAY", false); 876 | test("NULL", "ARRAY", false); 877 | test("OBJECT", "ARRAY", false); 878 | // test("ARRAY", "ARRAY", false); 879 | } 880 | 881 | #[test] 882 | fn test_binary_op_string_gt_lt_gte_lte() { 883 | let evaluator = Evaluator::new(); 884 | let context = value!({ 885 | "A": "A string", 886 | "B": "B string", 887 | }); 888 | 889 | let test = |l: &str, r: &str, is_gt: bool| { 890 | let expr = format!("{} > {}", l, r); 891 | assert_eq!( 892 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 893 | value!(is_gt) 894 | ); 895 | 896 | let expr = format!("{} <= {}", l, r); 897 | assert_eq!( 898 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 899 | value!(!is_gt) 900 | ); 901 | 902 | // we test equality in another test 903 | let expr = format!("{} == {}", l, r); 904 | let is_eq = evaluator 905 | .eval_in_context(&expr, context.clone()) 906 | .unwrap() 907 | .as_bool() 908 | .unwrap(); 909 | 910 | if is_eq { 911 | let expr = format!("{} >= {}", l, r); 912 | assert_eq!( 913 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 914 | value!(true) 915 | ); 916 | } else { 917 | let expr = format!("{} < {}", l, r); 918 | assert_eq!( 919 | evaluator.eval_in_context(&expr, context.clone()).unwrap(), 920 | value!(!is_gt) 921 | ); 922 | } 923 | }; 924 | 925 | test("A", "B", false); 926 | test("B", "A", true); 927 | test("A", "A", false); 928 | } 929 | 930 | #[test] 931 | fn test_lazy_eval_binary_op_and_or() { 932 | let evaluator = Evaluator::new(); 933 | // error is a missing transform 934 | let res = evaluator.eval("42 || 0|error"); 935 | assert!(res.is_ok()); 936 | assert_eq!(res.unwrap(), value!(42.0)); 937 | 938 | let res = evaluator.eval("false || 0|error"); 939 | assert!(res.is_err()); 940 | 941 | let res = evaluator.eval("42 && 0|error"); 942 | assert!(res.is_err()); 943 | 944 | let res = evaluator.eval("false && 0|error"); 945 | assert!(res.is_ok()); 946 | assert_eq!(res.unwrap(), value!(false)); 947 | } 948 | 949 | #[test] 950 | fn test_lazy_eval_trinary_op() { 951 | let evaluator = Evaluator::new(); 952 | // error is a missing transform 953 | let res = evaluator.eval("true ? 42 : 0|error"); 954 | assert!(res.is_ok()); 955 | assert_eq!(res.unwrap(), value!(42.0)); 956 | 957 | let res = evaluator.eval("true ? 0|error : 42"); 958 | assert!(res.is_err()); 959 | 960 | let res = evaluator.eval("true ? 0|error : 42"); 961 | assert!(res.is_err()); 962 | 963 | let res = evaluator.eval("false ? 0|error : 42"); 964 | assert!(res.is_ok()); 965 | assert_eq!(res.unwrap(), value!(42.0)); 966 | } 967 | } 968 | -------------------------------------------------------------------------------- /jexl-parser/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | edition = '2018' 3 | name = "jexl-parser" 4 | version = "0.3.1-alpha.0" 5 | authors = ["Mike Cooper ", "The Sync Team ", "The Glean Team "] 6 | description = "A JEXL parser written in Rust" 7 | license = "MPL-2.0" 8 | repository = "https://github.com/mozilla/jexl-rs" 9 | 10 | [dependencies] 11 | lalrpop-util = { version = "0.19", features = ["lexer"] } 12 | regex = "1.3" 13 | -------------------------------------------------------------------------------- /jexl-parser/src/ast.rs: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | #[derive(Debug, PartialEq)] 6 | pub enum Expression { 7 | Number(f64), 8 | String(String), 9 | Boolean(bool), 10 | Array(Vec>), 11 | Object(Vec<(String, Box)>), 12 | Identifier(String), 13 | Null, 14 | 15 | BinaryOperation { 16 | operation: OpCode, 17 | left: Box, 18 | right: Box, 19 | }, 20 | Transform { 21 | name: String, 22 | subject: Box, 23 | args: Option>>, 24 | }, 25 | DotOperation { 26 | subject: Box, 27 | ident: String, 28 | }, 29 | IndexOperation { 30 | subject: Box, 31 | index: Box, 32 | }, 33 | 34 | Conditional { 35 | left: Box, 36 | truthy: Box, 37 | falsy: Box, 38 | }, 39 | 40 | Filter { 41 | ident: String, 42 | op: OpCode, 43 | right: Box, 44 | }, 45 | } 46 | 47 | #[derive(Debug, PartialEq, Eq, Copy, Clone)] 48 | pub enum OpCode { 49 | Add, 50 | Subtract, 51 | Multiply, 52 | Divide, 53 | FloorDivide, 54 | Less, 55 | LessEqual, 56 | Greater, 57 | GreaterEqual, 58 | Equal, 59 | NotEqual, 60 | And, 61 | Or, 62 | Modulus, 63 | Exponent, 64 | In, 65 | } 66 | 67 | impl std::fmt::Display for OpCode { 68 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 69 | write!( 70 | f, 71 | "{}", 72 | match self { 73 | OpCode::Add => "Add", 74 | OpCode::Subtract => "Subtract", 75 | OpCode::Multiply => "Multiply", 76 | OpCode::Divide => "Divide", 77 | OpCode::FloorDivide => "Floor division", 78 | OpCode::Less => "Less than", 79 | OpCode::LessEqual => "Less than or equal to", 80 | OpCode::Greater => "Greater than", 81 | OpCode::GreaterEqual => "Greater than or equal to", 82 | OpCode::Equal => "Equal", 83 | OpCode::NotEqual => "Not equal", 84 | OpCode::And => "Bitwise And", 85 | OpCode::Or => "Bitwise Or", 86 | OpCode::Modulus => "Modulus", 87 | OpCode::Exponent => "Exponent", 88 | OpCode::In => "In", 89 | } 90 | ) 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /jexl-parser/src/lib.rs: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | pub mod ast; 6 | #[rustfmt::skip] 7 | mod parser; 8 | 9 | pub use lalrpop_util::ParseError; 10 | 11 | pub use crate::parser::Token; 12 | 13 | pub struct Parser {} 14 | 15 | impl Parser { 16 | pub fn parse(input: &str) -> Result> { 17 | Ok(*parser::ExpressionParser::new().parse(input)?) 18 | } 19 | } 20 | 21 | #[cfg(test)] 22 | mod tests { 23 | use super::*; 24 | use crate::ast::{Expression, OpCode}; 25 | 26 | #[test] 27 | fn literal() { 28 | assert_eq!(Parser::parse("1"), Ok(Expression::Number(1.0))); 29 | } 30 | 31 | #[test] 32 | fn binary_expression() { 33 | assert_eq!( 34 | Parser::parse("1+2"), 35 | Ok(Expression::BinaryOperation { 36 | operation: OpCode::Add, 37 | left: Box::new(Expression::Number(1.0)), 38 | right: Box::new(Expression::Number(2.0)), 39 | }), 40 | ); 41 | } 42 | 43 | #[test] 44 | fn binary_expression_whitespace() { 45 | assert_eq!(Parser::parse("1 + 2 "), Parser::parse("1+2"),); 46 | } 47 | 48 | #[test] 49 | fn transform_simple_no_args() { 50 | let exp = "'T_T'|lower"; 51 | let parsed = Parser::parse(exp).unwrap(); 52 | assert_eq!( 53 | parsed, 54 | Expression::Transform { 55 | name: "lower".to_string(), 56 | subject: Box::new(Expression::String("T_T".to_string())), 57 | args: None 58 | } 59 | ); 60 | } 61 | 62 | #[test] 63 | fn transform_multiple_args() { 64 | let exp = "'John Doe'|split(' ')"; 65 | let parsed = Parser::parse(exp).unwrap(); 66 | assert_eq!( 67 | parsed, 68 | Expression::Transform { 69 | name: "split".to_string(), 70 | subject: Box::new(Expression::String("John Doe".to_string())), 71 | args: Some(vec![Box::new(Expression::String(" ".to_string()))]) 72 | } 73 | ); 74 | } 75 | 76 | #[test] 77 | fn trasform_way_too_many_args() { 78 | let exp = "123456|math(12, 35, 100, 31, 90)"; 79 | let parsed = Parser::parse(exp).unwrap(); 80 | assert_eq!( 81 | parsed, 82 | Expression::Transform { 83 | name: "math".to_string(), 84 | subject: Box::new(Expression::Number(123_456f64)), 85 | args: Some(vec![ 86 | Box::new(Expression::Number(12f64)), 87 | Box::new(Expression::Number(35f64)), 88 | Box::new(Expression::Number(100f64)), 89 | Box::new(Expression::Number(31f64)), 90 | Box::new(Expression::Number(90f64)), 91 | ]) 92 | } 93 | ); 94 | } 95 | 96 | #[test] 97 | fn test_index_op_ident() { 98 | let exp = "foo[0]"; 99 | let parsed = Parser::parse(exp).unwrap(); 100 | assert_eq!( 101 | parsed, 102 | Expression::IndexOperation { 103 | subject: Box::new(Expression::Identifier("foo".to_string())), 104 | index: Box::new(Expression::Number(0f64)) 105 | } 106 | ); 107 | } 108 | 109 | #[test] 110 | fn test_index_op_array_literal() { 111 | let exp = "[1, 2, 3][0]"; 112 | let parsed = Parser::parse(exp).unwrap(); 113 | assert_eq!( 114 | parsed, 115 | Expression::IndexOperation { 116 | subject: Box::new(Expression::Array(vec![ 117 | Box::new(Expression::Number(1f64)), 118 | Box::new(Expression::Number(2f64)), 119 | Box::new(Expression::Number(3f64)), 120 | ])), 121 | index: Box::new(Expression::Number(0f64)) 122 | } 123 | ); 124 | } 125 | 126 | #[test] 127 | fn test_dot_op_ident() { 128 | let exp = "foo.bar"; 129 | let parsed = Parser::parse(exp).unwrap(); 130 | assert_eq!( 131 | parsed, 132 | Expression::DotOperation { 133 | subject: Box::new(Expression::Identifier("foo".to_string())), 134 | ident: "bar".to_string() 135 | } 136 | ); 137 | } 138 | 139 | #[test] 140 | fn test_dot_op_equality_with_null() { 141 | let exp = "foo.bar == null"; 142 | let parsed = Parser::parse(exp).unwrap(); 143 | assert_eq!( 144 | parsed, 145 | Expression::BinaryOperation { 146 | operation: OpCode::Equal, 147 | left: Box::new(Expression::DotOperation { 148 | subject: Box::new(Expression::Identifier("foo".to_string())), 149 | ident: "bar".to_string() 150 | }), 151 | right: Box::new(Expression::Null), 152 | } 153 | ); 154 | } 155 | 156 | #[test] 157 | fn test_dot_op_object_literal() { 158 | let exp = "{'foo': 1}.foo"; 159 | let parsed = Parser::parse(exp).unwrap(); 160 | assert_eq!( 161 | parsed, 162 | Expression::DotOperation { 163 | subject: Box::new(Expression::Object(vec![( 164 | "foo".to_string(), 165 | Box::new(Expression::Number(1f64)) 166 | )])), 167 | ident: "foo".to_string() 168 | } 169 | ); 170 | } 171 | 172 | #[test] 173 | fn test_parsing_null() { 174 | assert_eq!(Parser::parse("null"), Ok(Expression::Null)); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /jexl-parser/src/parser.lalrpop: -------------------------------------------------------------------------------- 1 | use std::str::FromStr; 2 | 3 | use crate::ast::{Expression, OpCode}; 4 | 5 | grammar; 6 | 7 | pub Expression: Box = Expr00; 8 | 9 | Expr00: Box = { 10 | => Box::new(Expression::BinaryOperation { left, right, operation }), 11 | Expr10, 12 | }; 13 | 14 | Expr10: Box = { 15 | => Box::new(Expression::BinaryOperation { left, right, operation }), 16 | Expr20, 17 | }; 18 | 19 | Expr20: Box = { 20 | => Box::new(Expression::BinaryOperation { left, right, operation }), 21 | Expr30, 22 | }; 23 | 24 | Expr30: Box = { 25 | => Box::new(Expression::BinaryOperation { left, right, operation }), 26 | Expr40, 27 | }; 28 | 29 | Expr40: Box = { 30 | => Box::new(Expression::BinaryOperation { left, right, operation }), 31 | Expr50, 32 | }; 33 | 34 | Expr50: Box = { 35 | "?" ":" => Box::new(Expression::Conditional {left, truthy, falsy}), 36 | Expr60, 37 | } 38 | 39 | Expr60: Box = { 40 | "|" => Box::new(Expression::Transform{name, subject, args}), 41 | Expr70 42 | }; 43 | 44 | 45 | /// Expression for dereferencing. 46 | /// Used for dereferencing object literals, array literals, and the context 47 | /// There are two types of operations here: 48 | /// - Either a `dot` operation, taking an expression on the left hand side, and an identifier on the right hand side (a string without the quotations) 49 | /// - Or an `index` operation, taking an expression on the left hand side, and another expression inside square ("[]") brackets. 50 | /// 51 | /// # Examples: 52 | /// 53 | /// Assume our context is the following 54 | /// ``` 55 | ///{ 56 | /// "foo": 57 | /// { 58 | /// "bar": [{"baz": 1}, {"bobo": [13, 12]}] 59 | // } 60 | // } 61 | /// ``` 62 | /// 63 | /// `foo.bar == [{"baz": 1}, {"bobo": [13, 12]]` 64 | /// `foo.bar[0] == {"baz": 1}` 65 | /// `foo.bar[1].bobo[0] == 13` 66 | /// `[1, 2, 3][1] == 2` 67 | Expr70: Box = { 68 | => Box::new(Expression::IndexOperation{subject, index}), 69 | "." => Box::new(Expression::DotOperation{subject, ident}), 70 | Expr80 71 | }; 72 | 73 | Expr80: Box = { 74 | Number => Box::new(Expression::Number(<>)), 75 | Boolean => Box::new(Expression::Boolean(<>)), 76 | String => Box::new(Expression::String(<>)), 77 | Array => Box::new(Expression::Array(<>)), 78 | Object => Box::new(Expression::Object(<>)), 79 | Null => Box::new(Expression::Null), 80 | Identifier => Box::new(Expression::Identifier(<>)), 81 | "(" ")", 82 | }; 83 | 84 | Args: Vec> = { 85 | "(" > ")" 86 | }; 87 | 88 | Op10: OpCode = { 89 | "&&" => OpCode::And, 90 | "||" => OpCode::Or, 91 | }; 92 | 93 | Op20: OpCode = { 94 | "==" => OpCode::Equal, 95 | "!=" => OpCode::NotEqual, 96 | ">=" => OpCode::GreaterEqual, 97 | "<=" => OpCode::LessEqual, 98 | ">" => OpCode::Greater, 99 | "<" => OpCode::Less, 100 | "in" => OpCode::In, 101 | }; 102 | 103 | Op30: OpCode = { 104 | "+" => OpCode::Add, 105 | "-" => OpCode::Subtract, 106 | }; 107 | 108 | Op40: OpCode = { 109 | "*" => OpCode::Multiply, 110 | "//" => OpCode::FloorDivide, 111 | "/" => OpCode::Divide, 112 | }; 113 | 114 | Op50: OpCode = { 115 | "%" => OpCode::Modulus, 116 | "^" => OpCode::Exponent, 117 | }; 118 | 119 | Number: f64 = { 120 | r"[0-9]+" => f64::from_str(<>).unwrap(), 121 | r"[0-9]+\.[0-9]*" => f64::from_str(<>).unwrap(), 122 | r"\.[0-9]+" => f64::from_str(<>).unwrap(), 123 | }; 124 | 125 | String: String = { 126 | => s[1..s.len() - 1].to_string().replace("\\\"", "\""), 127 | => s[1..s.len() - 1].to_string().replace("\\'", "'"), 128 | }; 129 | 130 | Null: Option> = { 131 | "null" => None, 132 | } 133 | 134 | Identifier: String = { 135 | r#"[a-zA-Z_][a-zA-Z0-9_]*"# => <>.to_string() 136 | } 137 | 138 | Index: Box = { 139 | "[" "." "]" => Box::new(Expression::Filter {ident, op, right}), 140 | "[" "]", 141 | } 142 | 143 | 144 | Boolean: bool = { 145 | "true" => true, 146 | "false" => false, 147 | } 148 | 149 | Comma: Vec = { 150 | ",")*> => match e { 151 | None => v, 152 | Some(e) => { 153 | let mut v = v; 154 | v.push(e); 155 | v 156 | } 157 | } 158 | }; 159 | 160 | Array: Vec> = { 161 | "[" > "]" 162 | } 163 | 164 | Object: Vec<(String, Box)> = { 165 | "{" ":" )>> "}", 166 | "{}" => vec![], 167 | } 168 | 169 | ObjectIdentifier: String = { 170 | String, 171 | Identifier 172 | } 173 | -------------------------------------------------------------------------------- /parser-gen/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "parser-gen" 3 | version = "0.3.1-alpha.0" 4 | authors = ["Edouard Oger "] 5 | edition = "2018" 6 | license = "MPL-2.0" 7 | publish = false 8 | 9 | [dependencies] 10 | lalrpop = { version = "0.19", features = ["lexer"] } 11 | -------------------------------------------------------------------------------- /parser-gen/README.md: -------------------------------------------------------------------------------- 1 | # jexl-parser generator 2 | 3 | We do not use `lalrpop` to generate the parser during the `jexl-parser` build phase. 4 | Why? because doing so introduces a lot of dependencies in Firefox's build. So instead we check-in the generated parser file, and run a CI job to ensure it's always fresh. 5 | -------------------------------------------------------------------------------- /parser-gen/ensure_parser_up_to_date.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | cargo run --bin parser-gen 4 | 5 | CHANGES=$(git status --porcelain) 6 | 7 | if [ -n "$CHANGES" ]; then 8 | git status 9 | echo -e "\033[0;31mThe parser file looks outdated (git status is dirty). You can fix this yourself by running cargo run --bin parser-gen and then committing the result" 10 | fi 11 | -------------------------------------------------------------------------------- /parser-gen/src/main.rs: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | fn main() { 6 | lalrpop::Configuration::new() 7 | .generate_in_source_tree() 8 | .process() 9 | .unwrap(); 10 | } 11 | --------------------------------------------------------------------------------