xcomponent 0.12.0__tar.gz → 0.12.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/build-wheels.yml +3 -3
  2. {xcomponent-0.12.0 → xcomponent-0.12.2}/CHANGELOG.md +9 -0
  3. {xcomponent-0.12.0 → xcomponent-0.12.2}/Cargo.lock +9 -9
  4. {xcomponent-0.12.0 → xcomponent-0.12.2}/Cargo.toml +4 -4
  5. {xcomponent-0.12.0 → xcomponent-0.12.2}/PKG-INFO +1 -1
  6. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/source/user/getting_started.md +74 -16
  7. {xcomponent-0.12.0 → xcomponent-0.12.2}/pyproject.toml +1 -2
  8. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/context.rs +1 -1
  9. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/ast/eval.rs +85 -2
  10. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/ast/model.rs +17 -0
  11. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/ast/parse.rs +44 -0
  12. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/grammar.pest +52 -16
  13. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/i18n.rs +8 -1
  14. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/parser.rs +102 -11
  15. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/tokens.rs +86 -15
  16. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/markup/tokens.rs +1 -2
  17. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/adapters/test_babel.py +46 -0
  18. xcomponent-0.12.2/tests/unittests/expression/test_binary_in.py +94 -0
  19. xcomponent-0.12.2/tests/unittests/expression/test_for_expr.py +68 -0
  20. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_if_expr.py +21 -0
  21. xcomponent-0.12.2/tests/unittests/expression/test_match_expr.py +65 -0
  22. {xcomponent-0.12.0 → xcomponent-0.12.2}/uv.lock +1 -1
  23. xcomponent-0.12.0/tests/unittests/expression/test_for_expr.py +0 -24
  24. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/build-sdist.yml +0 -0
  25. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/publish-doc.yml +0 -0
  26. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/publish-pypi.yml +0 -0
  27. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/release.yml +0 -0
  28. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/tests-artifacts.yml +0 -0
  29. {xcomponent-0.12.0 → xcomponent-0.12.2}/.github/workflows/tests.yml +0 -0
  30. {xcomponent-0.12.0 → xcomponent-0.12.2}/.gitignore +0 -0
  31. {xcomponent-0.12.0 → xcomponent-0.12.2}/.python-version +0 -0
  32. {xcomponent-0.12.0 → xcomponent-0.12.2}/Justfile +0 -0
  33. {xcomponent-0.12.0 → xcomponent-0.12.2}/LICENSE +0 -0
  34. {xcomponent-0.12.0 → xcomponent-0.12.2}/README.md +0 -0
  35. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/Makefile +0 -0
  36. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/source/conf.py +0 -0
  37. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/source/develop/.gitkeep +0 -0
  38. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/source/index.md +0 -0
  39. {xcomponent-0.12.0 → xcomponent-0.12.2}/docs/source/user/changelog.md +0 -0
  40. {xcomponent-0.12.0 → xcomponent-0.12.2}/scripts/get_version.py +0 -0
  41. {xcomponent-0.12.0 → xcomponent-0.12.2}/scripts/write_changelog.py +0 -0
  42. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/__init__.py +0 -0
  43. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/adapters/__init__.py +0 -0
  44. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/adapters/babel.py +0 -0
  45. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/py.typed +0 -0
  46. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/service/__init__.py +0 -0
  47. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/service/catalog.py +0 -0
  48. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/python/xcomponent/xcore.pyi +0 -0
  49. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/catalog/catalog.rs +0 -0
  50. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/catalog/mod.rs +0 -0
  51. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/ast/mod.rs +0 -0
  52. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/expression/mod.rs +0 -0
  53. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/lib.rs +0 -0
  54. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/markup/grammar.pest +0 -0
  55. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/markup/mod.rs +0 -0
  56. {xcomponent-0.12.0 → xcomponent-0.12.2}/src/rust/markup/parser.rs +1 -1
  57. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/__init__.py +0 -0
  58. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/__init__.py +0 -0
  59. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/__init__.py +0 -0
  60. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/babel.ini +0 -0
  61. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/fr.mo +0 -0
  62. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/fr.po +0 -0
  63. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/mydomain.pot +0 -0
  64. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/functionals/i18n/test_l10n.py +0 -0
  65. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/__init__.py +0 -0
  66. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/adapters/__init__.py +0 -0
  67. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/conftest.py +0 -0
  68. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_binary_eq.py +0 -0
  69. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_binary_op.py +0 -0
  70. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_binary_op_bool.py +0 -0
  71. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_default_values.py +0 -0
  72. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_field_access.py +0 -0
  73. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_func.py +0 -0
  74. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_let.py +0 -0
  75. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_nested.py +0 -0
  76. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_render_comment.py +0 -0
  77. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_script.py +0 -0
  78. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_str_dedent.py +0 -0
  79. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_str_method.py +0 -0
  80. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_types.py +0 -0
  81. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/expression/test_unary.py +0 -0
  82. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_core.py +0 -0
  83. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_import_namespace.py +0 -0
  84. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_named_component.py +0 -0
  85. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_attributes.py +0 -0
  86. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_context.py +0 -0
  87. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_globals.py +0 -0
  88. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_globals_obj.py +0 -0
  89. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_globals_obj2.py +0 -0
  90. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_nested_dict.py +0 -0
  91. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_nested_node.py +0 -0
  92. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_node.py +0 -0
  93. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_properties.py +0 -0
  94. {xcomponent-0.12.0 → xcomponent-0.12.2}/tests/unittests/test_render_script.py +0 -0
@@ -38,9 +38,9 @@ jobs:
38
38
  target: [x86_64, aarch64]
39
39
  manylinux: [auto]
40
40
  include:
41
- - os: linux
42
- manylinux: auto
43
- target: i686
41
+ # - os: linux
42
+ # manylinux: auto
43
+ # target: i686
44
44
  - os: linux
45
45
  manylinux: auto
46
46
  target: aarch64
@@ -1,3 +1,12 @@
1
+ ## 0.12.2 - 2026-12-05
2
+
3
+ * Introduce a `match` keyword for pattern matching inside templates.
4
+ * Introduce new syntax using semicolon for `if` and `for` loop.
5
+
6
+ ## 0.12.1 - 2026-12-02
7
+
8
+ * Implement "in" and "not in" binary expressions.
9
+
1
10
  ## 0.12.0 - 2026-11-11
2
11
 
3
12
  * Upgrade pyo3
@@ -207,9 +207,9 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
207
207
 
208
208
  [[package]]
209
209
  name = "pest"
210
- version = "2.9.6"
210
+ version = "2.9.7"
211
211
  source = "registry+https://github.com/rust-lang/crates.io-index"
212
- checksum = "5ec53fd1d256900a9a771bf2720d42cef6f8bc91b4e25065fbd291e3e7222576"
212
+ checksum = "44cf434b793f79ab490e99ae3b2f9acce4ef02cdac3eee541a69de78c1aadd7c"
213
213
  dependencies = [
214
214
  "memchr",
215
215
  "pest_stack_guard",
@@ -218,9 +218,9 @@ dependencies = [
218
218
 
219
219
  [[package]]
220
220
  name = "pest_derive"
221
- version = "2.9.6"
221
+ version = "2.9.7"
222
222
  source = "registry+https://github.com/rust-lang/crates.io-index"
223
- checksum = "adcf794fd301ddc20818a6b4de85ed507f4b45300245cec948982e69e12e3bd7"
223
+ checksum = "082f5f21b49230a02b9ee78a8a39d5771fd0ed6dc4d88ecc9fd583cb781e5751"
224
224
  dependencies = [
225
225
  "pest",
226
226
  "pest_generator",
@@ -228,9 +228,9 @@ dependencies = [
228
228
 
229
229
  [[package]]
230
230
  name = "pest_generator"
231
- version = "2.9.6"
231
+ version = "2.9.7"
232
232
  source = "registry+https://github.com/rust-lang/crates.io-index"
233
- checksum = "c3073afae5db334c25dea9fc3e230a4f191e920c1df080cb9c0b37c9ce1dbb41"
233
+ checksum = "5e4725db179c5bc5ea719d3312ce518f4b70d368139d630f4c9309d9117a3ac1"
234
234
  dependencies = [
235
235
  "pest",
236
236
  "pest_meta",
@@ -241,9 +241,9 @@ dependencies = [
241
241
 
242
242
  [[package]]
243
243
  name = "pest_meta"
244
- version = "2.9.6"
244
+ version = "2.9.7"
245
245
  source = "registry+https://github.com/rust-lang/crates.io-index"
246
- checksum = "7bc7f614b3742541642af1685faaa910bf6333e3cd3729c2cccd118146a57327"
246
+ checksum = "8363b5d48f0960fc3db68b1c8aa0bdc1a370d465a8c74287172ed5093a4cf293"
247
247
  dependencies = [
248
248
  "pest",
249
249
  ]
@@ -568,7 +568,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
568
568
 
569
569
  [[package]]
570
570
  name = "xcore"
571
- version = "0.12.0"
571
+ version = "0.12.2"
572
572
  dependencies = [
573
573
  "env_logger",
574
574
  "html-escape",
@@ -1,16 +1,16 @@
1
1
  [package]
2
2
  name = "xcore"
3
- version = "0.12.0"
3
+ version = "0.12.2"
4
4
  authors = ["Guillaume Gauvrit <guillaume@gauvr.it>"]
5
- edition = "2021"
5
+ edition = "2024"
6
6
  description = "JSX-like for python using maturin/pyo3 and pest."
7
7
  license-file = "LICENSE"
8
8
  readme = "README.md"
9
9
 
10
10
  [dependencies]
11
11
  pyo3 = { version = "0.29", features = ["abi3-py311", "extension-module"] }
12
- pest = "2.9.6"
13
- pest_derive = "2.9.6"
12
+ pest = "2.9.7"
13
+ pest_derive = "2.9.7"
14
14
  lazy_static = "1.5.0"
15
15
  log = "0.4.27"
16
16
  env_logger = "0.11.8"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xcomponent
3
- Version: 0.12.0
3
+ Version: 0.12.2
4
4
  Classifier: Intended Audience :: Developers
5
5
  Classifier: License :: OSI Approved :: MIT License
6
6
  Classifier: Programming Language :: Python :: 3
@@ -127,7 +127,7 @@ Even if the expression is not Python, it raises a Python exception.
127
127
  }
128
128
  <Layout head={<HtmlHead title={title} />}>
129
129
  <h1>Hello, world!</h1>
130
- </Layout>
130
+ </Layout>;
131
131
  ```
132
132
 
133
133
  ```jsx
@@ -136,7 +136,7 @@ Even if the expression is not Python, it raises a Python exception.
136
136
  }
137
137
  <Layout head={<HtmlHead title={title} />} title={title}>
138
138
  <h1>Hello, world!</h1>
139
- </Layout>
139
+ </Layout>;
140
140
  ```
141
141
 
142
142
  In the React world, this is called "props drilling." Components are autonomous;
@@ -244,7 +244,6 @@ Says differently, it is not possible to generate a javascript function within
244
244
  a XComponent expression. **The content of the `<script>` and `<style>` markup
245
245
  tag is copied from the template at rendering, not interpreted**.
246
246
 
247
-
248
247
  ## XComponent Expression
249
248
 
250
249
  Everything in the Markup that has been enclosed by curly braces is an expression.
@@ -363,6 +362,19 @@ Also, Python functions can be registered to mitigate or implement complex binary
363
362
  | ------ | ---------- | --------------------- | ------------------ | ------------ | --------- |
364
363
  | equals | not equals | greater than or equal | less than or equal | greater than | less than |
365
364
 
365
+ #### Membership operators
366
+
367
+ | `in` | `not in` |
368
+ | --------------- | ----------------------- |
369
+ | membership test | negated membership test |
370
+
371
+ Supported containers should include:
372
+
373
+ - Lists: checks whether an element exists.
374
+ - Dictionaries: checks whether a key exists.
375
+ - Strings: checks whether a substring exists.
376
+ - Python objects: uses Python's membership protocol (`__contains__` method implemented).
377
+
366
378
  #### Priority
367
379
 
368
380
  The priority of operators follows the mathematical order.
@@ -374,25 +386,43 @@ The parenthesis, such as **( _condition_ )** can be used to override the priorit
374
386
 
375
387
  ### If Syntax
376
388
 
377
- The `if` syntax is the Rust syntax.
378
- It means that the binary expression is not enclosed by parentheses, and blocks
379
- are enclosed by curly braces.
389
+ Their are 2 construction for the if operator,
390
+
391
+ the first `if` syntax, like the Rust syntax, as a binary expression,
392
+ not enclosed by parentheses, a block enclosed by curly braces.
380
393
 
381
394
  Example:
382
395
 
383
396
  ```jsx
384
397
  {/* if statement without else */}
385
- { if elements > 0 { <Elements elements={elements} /> } }
398
+ { if "admin" in user.roles { <AdminPanel /> } }
386
399
 
387
400
  {/* if statement with else */}
388
- { if elements > 0 { <Elements elements={elements} /> } else { <Empty/>} }
401
+ { if "admin" in user.roles { <AdminPanel /> } else { <Forbidden />} }
389
402
  ```
390
403
 
391
- Blocks can contain other expressions or markup.
404
+ The alternate kind of block, inspired by python cannot contains other
405
+ expression, it can only include a markup element.
406
+
407
+ T
408
+ ```jsx
409
+ {/* if statement without else */}
410
+ { if "admin" in user.roles: <AdminPanel/> }
411
+
412
+ {/* if statement with else */}
413
+ {
414
+ if "admin" in user.roles:
415
+ <AdminPanel />
416
+ else:
417
+ <Forbidden />
418
+ }
419
+ ```
392
420
 
393
421
  ### For Syntax
394
422
 
395
- The `for` syntax is the Rust syntax.
423
+ The `for` syntax has too form, one like the Rust syntax,
424
+ that authorize to have a a full expression, using `{}`.
425
+ Or if there is only one markup statement, the `:` can be used.
396
426
 
397
427
  Example:
398
428
 
@@ -402,19 +432,27 @@ Example:
402
432
 
403
433
  {/* for statement applied to Python dict */}
404
434
  {for k in my_dict { <Item key={k} value={my_dict[k]}/>}}
435
+
436
+
437
+ {/* alternative syntax to for statement for markup body only. */}
438
+ {for x in my_list: <Item item={x}/>}
405
439
  ```
406
440
 
407
- Blocks can contain other expressions or markup.
441
+ ### Match Syntax
442
+
443
+ For pattern matching, the match keyword is used like this:
408
444
 
409
- ````{important}
410
- this is not python code, you can do
411
445
  ```jsx
412
446
  {
413
- /* ❌ this is wrong */
414
- if x in my_list { <hr/> }
447
+ match user.role {
448
+ "admin": <AdminPanel/>
449
+ "operator": <OperatorPanel/>
450
+ _: <Forbidden/>
451
+ }
415
452
  }
416
453
  ```
417
- ````
454
+
455
+ Note that match arms can be blocks too using curly braces.
418
456
 
419
457
  ### Let Syntax
420
458
 
@@ -442,3 +480,23 @@ Example:
442
480
  {/* display the value */}
443
481
  {y}
444
482
  ```
483
+
484
+
485
+ ### I18n support
486
+
487
+ XComponent supports i18n using babel, babel helpers has to be registered
488
+ in the `globals` special context to be available everywhere.
489
+
490
+ XComponent provide a babel extractor to generate a .pot files from the
491
+ templates.
492
+
493
+ The babel mapping file like `extraction.ini` here has to be created like this
494
+ and provide to the `extract` pybabel subcommand.
495
+
496
+ ```{ini}
497
+ [xcomponent: path/to/templates/**.py]
498
+ ```
499
+
500
+ ```
501
+ pybabel extract --output template.pot --mapping=extraction.ini
502
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "xcomponent"
3
- version = "0.12.0"
3
+ version = "0.12.2"
4
4
  description = "An embeded component base template engine - inspired by JSX"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Guillaume Gauvrit", email = "guillaume@gauvr.it" }]
@@ -59,7 +59,6 @@ docs = [
59
59
  xcomponent = "xcomponent.adapters.babel:extract_xcomponent"
60
60
 
61
61
  [tool.pyright]
62
- autoSearchPaths = false
63
62
  extraPaths = ["src/python"]
64
63
  include = ["src/python", "tests"]
65
64
  typeCheckingMode = "strict"
@@ -4,7 +4,7 @@ use std::fmt;
4
4
  use pyo3::exceptions::PyTypeError;
5
5
  use pyo3::marker::Python;
6
6
  use pyo3::types::{PyBool, PyDict, PyInt, PyList, PyNone, PyString};
7
- use pyo3::{prelude::*, BoundObject, IntoPyObjectExt};
7
+ use pyo3::{BoundObject, IntoPyObjectExt, prelude::*};
8
8
 
9
9
  use crate::catalog::XCatalog;
10
10
  use crate::markup::tokens::{ToHtml, XNode};
@@ -9,7 +9,7 @@ use pyo3::types::{PyDict, PyTuple};
9
9
 
10
10
  use crate::catalog::XCatalog;
11
11
  use crate::context::{Literal, LiteralKey, RenderContext, Truthy};
12
- use crate::expression::ast::model::AST;
12
+ use crate::expression::ast::model::{AST, ASTPattern};
13
13
  use crate::expression::ast::parse::parse;
14
14
  use crate::expression::tokens::{ExpressionToken, UnaryOperator};
15
15
  use crate::expression::{parser::tokenize, tokens::Operator};
@@ -116,6 +116,61 @@ fn eval_or(l: Literal, r: Literal) -> PyResult<Literal> {
116
116
  }
117
117
  }
118
118
 
119
+ fn literal_equals(a: &Literal, b: &Literal) -> bool {
120
+ match (a, b) {
121
+ (Literal::None(()), Literal::None(())) => true,
122
+ (Literal::Bool(a), Literal::Bool(b)) => a == b,
123
+ (Literal::Int(a), Literal::Int(b)) => a == b,
124
+ (Literal::Str(a), Literal::Str(b)) => a == b,
125
+ (Literal::Uuid(a), Literal::Uuid(b)) => a == b,
126
+ (Literal::List(a), Literal::List(b)) => {
127
+ a.len() == b.len() && a.iter().zip(b).all(|(a, b)| literal_equals(a, b))
128
+ }
129
+ _ => false,
130
+ }
131
+ }
132
+
133
+ fn raw_eval_in<'py>(py: Python<'py>, l: Literal, r: Literal) -> PyResult<bool> {
134
+ let result = match (l, r) {
135
+ // List membership
136
+ (value, Literal::List(items)) => items.iter().any(|item| literal_equals(item, &value)),
137
+
138
+ // Dict membership: check keys
139
+ (Literal::Str(key), Literal::Dict(dict)) => dict.contains_key(&LiteralKey::Str(key)),
140
+
141
+ // String membership
142
+ (Literal::Str(needle), Literal::Str(haystack)) => haystack.contains(&needle),
143
+
144
+ // PyObj
145
+ (value, Literal::Object(obj)) => {
146
+ if let Some(pyobj) = obj.clone().into_pyobject(py)?.get_item("obj")? {
147
+ let res = pyobj.call_method1("__contains__", (value.into_pyobject(py)?,))?;
148
+ res.extract::<bool>()?
149
+ } else {
150
+ false
151
+ }
152
+ }
153
+
154
+ // Unsupported combinations
155
+ (a, b) => {
156
+ return Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
157
+ "Cannot evaluate {:?} in {:?}, type mismatch",
158
+ a, b
159
+ )));
160
+ }
161
+ };
162
+
163
+ Ok(result)
164
+ }
165
+
166
+ fn eval_in<'py>(py: Python<'py>, l: Literal, r: Literal) -> PyResult<Literal> {
167
+ raw_eval_in(py, l, r).map(|b| Literal::Bool(b))
168
+ }
169
+
170
+ fn eval_not_in<'py>(py: Python<'py>, l: Literal, r: Literal) -> PyResult<Literal> {
171
+ raw_eval_in(py, l, r).map(|b| Literal::Bool(!b))
172
+ }
173
+
119
174
  fn eval_raw_eq(l: Literal, r: Literal, op: String) -> PyResult<bool> {
120
175
  match (l, r) {
121
176
  (Literal::Int(a), Literal::Int(b)) => Ok(a == b),
@@ -186,6 +241,18 @@ fn eval_lte(l: Literal, r: Literal) -> PyResult<Literal> {
186
241
  return eval_raw_gt(l, r).map(|b| Literal::Bool(!b));
187
242
  }
188
243
 
244
+ fn pattern_matches(pattern: &ASTPattern, value: &Literal) -> bool {
245
+ match pattern {
246
+ ASTPattern::Wildcard => true,
247
+
248
+ ASTPattern::Literal(expected) => literal_equals(expected, value),
249
+
250
+ ASTPattern::Or(patterns) => patterns
251
+ .iter()
252
+ .any(|pattern| pattern_matches(pattern, value)),
253
+ }
254
+ }
255
+
189
256
  pub fn eval_ast<'py>(
190
257
  py: Python<'py>,
191
258
  ast: &'py AST,
@@ -225,7 +292,6 @@ pub fn eval_ast<'py>(
225
292
  _ => (),
226
293
  }
227
294
  let r = eval_ast(py, right, catalog, context)?;
228
-
229
295
  match op {
230
296
  Operator::Add => eval_add(l, r),
231
297
  Operator::Sub => eval_sub(l, r),
@@ -233,6 +299,8 @@ pub fn eval_ast<'py>(
233
299
  Operator::Div => eval_div(l, r),
234
300
  Operator::And => eval_and(l, r),
235
301
  Operator::Or => eval_or(l, r),
302
+ Operator::In => eval_in(py, l, r),
303
+ Operator::NotIn => eval_not_in(py, l, r),
236
304
  Operator::Eq => eval_eq(l, r),
237
305
  Operator::Neq => eval_neq(l, r),
238
306
  Operator::Gt => eval_gt(l, r),
@@ -398,6 +466,21 @@ pub fn eval_ast<'py>(
398
466
  }
399
467
  }
400
468
  }
469
+
470
+ AST::MatchStatement { value, arms } => {
471
+ let value = eval_ast(py, value, catalog, context)?;
472
+
473
+ for arm in arms {
474
+ if pattern_matches(&arm.pattern, &value) {
475
+ return eval_ast(py, &arm.body, catalog, context);
476
+ }
477
+ }
478
+
479
+ Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
480
+ "No matching pattern in match statement",
481
+ ))
482
+ }
483
+
401
484
  AST::ForStatement {
402
485
  ident,
403
486
  iterable,
@@ -2,6 +2,19 @@ use crate::context::Literal;
2
2
  use crate::expression::tokens::{Operator, UnaryOperator};
3
3
  use std::collections::HashMap;
4
4
 
5
+ #[derive(Debug, Clone)]
6
+ pub struct ASTMatchArm {
7
+ pub pattern: ASTPattern,
8
+ pub body: Box<AST>,
9
+ }
10
+
11
+ #[derive(Debug, Clone)]
12
+ pub enum ASTPattern {
13
+ Wildcard,
14
+ Literal(Literal),
15
+ Or(Vec<ASTPattern>),
16
+ }
17
+
5
18
  #[derive(Debug, Clone)]
6
19
  pub enum AST {
7
20
  Variable(String),
@@ -27,6 +40,10 @@ pub enum AST {
27
40
  then_branch: Box<AST>,
28
41
  else_branch: Option<Box<AST>>,
29
42
  },
43
+ MatchStatement {
44
+ value: Box<AST>,
45
+ arms: Vec<ASTMatchArm>,
46
+ },
30
47
  ForStatement {
31
48
  ident: String,
32
49
  iterable: Box<AST>,
@@ -4,11 +4,42 @@ use pyo3::exceptions::PySyntaxError;
4
4
  use pyo3::prelude::*;
5
5
 
6
6
  use crate::context::Literal;
7
+ use crate::expression::ast::model::{ASTMatchArm, ASTPattern};
8
+ use crate::expression::tokens::{MatchArm, Pattern};
7
9
  use crate::expression::{
8
10
  ast::model::AST,
9
11
  tokens::{ExpressionToken, PostfixOp},
10
12
  };
11
13
 
14
+ fn token_pattern_to_ast(pattern: &Pattern) -> Result<ASTPattern, PyErr> {
15
+ match pattern {
16
+ Pattern::Wildcard => Ok(ASTPattern::Wildcard),
17
+
18
+ Pattern::Boolean(value) => Ok(ASTPattern::Literal(Literal::Bool(value.clone()))),
19
+ Pattern::Integer(value) => Ok(ASTPattern::Literal(Literal::Int(value.clone()))),
20
+ Pattern::String(value) => Ok(ASTPattern::Literal(Literal::Str(value.clone()))),
21
+
22
+ Pattern::Or(patterns) => {
23
+ let patterns = patterns
24
+ .iter()
25
+ .map(token_pattern_to_ast)
26
+ .collect::<Result<Vec<_>, _>>()?;
27
+
28
+ Ok(ASTPattern::Or(patterns))
29
+ }
30
+ }
31
+ }
32
+
33
+ fn token_match_arm_to_ast(arm: &MatchArm) -> Result<ASTMatchArm, PyErr> {
34
+ let pattern = token_pattern_to_ast(&arm.pattern)?;
35
+
36
+ let body = token_to_ast(&arm.body, 0)?;
37
+ Ok(ASTMatchArm {
38
+ pattern,
39
+ body: Box::new(body),
40
+ })
41
+ }
42
+
12
43
  pub fn token_to_ast(tok: &ExpressionToken, min_prec: u8) -> Result<AST, PyErr> {
13
44
  let ast = match tok {
14
45
  ExpressionToken::UnaryExpression { op, expr } => {
@@ -45,6 +76,19 @@ pub fn token_to_ast(tok: &ExpressionToken, min_prec: u8) -> Result<AST, PyErr> {
45
76
  None => None,
46
77
  },
47
78
  }),
79
+ ExpressionToken::MatchExpression { value, arms } => {
80
+ let value = token_to_ast(value, 0)?;
81
+
82
+ let arms = arms
83
+ .iter()
84
+ .map(token_match_arm_to_ast)
85
+ .collect::<Result<Vec<_>, _>>()?;
86
+
87
+ Ok(AST::MatchStatement {
88
+ value: Box::new(value),
89
+ arms,
90
+ })
91
+ }
48
92
  ExpressionToken::ForExpression {
49
93
  ident,
50
94
  iterable,
@@ -1,4 +1,4 @@
1
- expression = { for_expression | if_expression | let_expression | binary_expression | comment_expression }
1
+ expression = { for_expression | if_expression | match_expression | let_expression | binary_expression | comment_expression }
2
2
 
3
3
  binary_expression = { comment_expression* ~ whitespace* ~ term ~ (whitespace* ~ operator ~ whitespace* ~ term)* }
4
4
 
@@ -18,23 +18,31 @@ call_arg = _{ kw_arg | pos_arg }
18
18
  pos_arg = { expression }
19
19
  kw_arg = { ident ~ whitespace* ~ "=" ~ whitespace* ~ expression }
20
20
 
21
- primary = _{ "(" ~ expression ~ ")" | component | string | integer | boolean | ident }
21
+ // none = { "None" }
22
22
 
23
- operator = { "+" | "-" | "*" | "/" | "and" | "or" | "==" | "!=" | ">=" | "<=" | ">" | "<" }
23
+ literal = _{
24
+ string
25
+ | integer
26
+ | boolean // | none
27
+ }
28
+
29
+ primary = _{ "(" ~ expression ~ ")" | component | literal | ident }
24
30
 
25
- boolean = { "true" | "false" }
26
- integer = @{ (ASCII_DIGIT | "_")+ }
27
- dedent_string = @{
31
+ operator = { "not" ~ whitespace+ ~ "in" | "+" | "-" | "*" | "/" | "and" | "or" | "in" | "==" | "!=" | ">=" | "<=" | ">" | "<" }
32
+
33
+ boolean = { "true" | "false" }
34
+ integer = @{ (ASCII_DIGIT | "_")+ }
35
+ dedent_string = @{
28
36
  "\"\"\"" ~ (escape_sequence | !("\"\"\"" | "\\") ~ ANY)* ~ "\"\"\""
29
- | "'''" ~ (escape_sequence | !("'''" | "\\") ~ ANY)* ~ "'''"
37
+ | "'''" ~ (escape_sequence | !("'''" | "\\") ~ ANY)* ~ "'''"
30
38
  }
31
- normal_string = @{
39
+ normal_string = @{
32
40
  ("\"" ~ (escape_sequence | !("\"" | "\\") ~ ANY)* ~ "\"")
33
41
  | ("'" ~ (escape_sequence | !("'" | "\\") ~ ANY)* ~ "'")
34
42
  }
35
- string = _{ dedent_string | normal_string }
43
+ string = _{ dedent_string | normal_string }
36
44
 
37
- keyword = { "for" | "in" | "if" | "else" | "not" | "and" | "or" | "let" }
45
+ keyword = { "for" | "in" | "if" | "else" | "match" | "not" | "and" | "or" | "let" }
38
46
  keyword_spaced = { keyword ~ (whitespace+ | ".") }
39
47
 
40
48
  ident = { !keyword_spaced ~ ident_raw }
@@ -43,11 +51,39 @@ ident_raw = _{ (ASCII_ALPHA | "_" | "#") ~ (ASCII_ALPHANUMERIC | "_" | "#")* }
43
51
  whitespace = _{ " " | "\t" | "\n" | "\r" | comment_expression }
44
52
  escape_sequence = { "\\" ~ ("\"" | "'" | "\\" | "n" | "t" | "r") }
45
53
 
46
- if_expression = { "if " ~ whitespace* ~ binary_expression ~ whitespace* ~ block ~ (whitespace* ~ "else" ~ whitespace* ~ block)? }
47
- for_expression = { "for" ~ whitespace+ ~ ident ~ whitespace+ ~ "in" ~ whitespace+ ~ expression ~ whitespace* ~ block }
54
+ if_condition = _{"if " ~ whitespace* ~ binary_expression ~ whitespace*}
55
+ if_block = _{ if_condition ~ block ~ (whitespace* ~ "else" ~ whitespace* ~ block)? }
56
+ if_markup = _{ if_condition ~ ":" ~ markup_block ~ (whitespace* ~ "else:" ~ markup_block)? }
57
+ if_expression = { if_block | if_markup }
58
+
59
+ for_condition = _{"for" ~ whitespace+ ~ ident ~ whitespace+ ~ "in" ~ whitespace+ ~ expression ~ whitespace*}
60
+ for_block = _{ for_condition ~ block }
61
+ for_markup = _{ for_condition ~ ":" ~ markup_block }
62
+ for_expression = { for_block | for_markup }
63
+
48
64
  let_expression = { "let" ~ whitespace+ ~ ident ~ whitespace* ~ "=" ~ whitespace* ~ expression }
49
65
 
50
- block = { whitespace* ~ "{" ~ whitespace* ~ expression ~ whitespace* ~ "}" ~ whitespace* }
66
+ block = _{ whitespace* ~ "{" ~ whitespace* ~ expression ~ whitespace* ~ "}" ~ whitespace* }
67
+ markup_block = _{ whitespace* ~ component ~ whitespace* }
68
+ match_arm_body = _{ block | markup_block }
69
+
70
+ match_expression = {
71
+ "match" ~ whitespace+ ~ expression ~ whitespace* ~ "{" ~ whitespace* ~ match_arm* ~ "}"
72
+ }
73
+
74
+ match_arm = {
75
+ pattern ~ whitespace* ~ ":" ~ whitespace* ~ match_arm_body
76
+ }
77
+
78
+ pattern = {
79
+ primary_pattern ~ (whitespace* ~ "|" ~ whitespace* ~ primary_pattern)*
80
+ }
81
+
82
+ wildcard_pattern = { "_" }
83
+ primary_pattern = {
84
+ wildcard_pattern
85
+ | literal
86
+ }
51
87
 
52
88
  comment_expression = _{ "/*" ~ comment ~ "*/" }
53
89
  comment = { (!"*/" ~ ANY)* }
@@ -70,9 +106,9 @@ markup_attribute = _{ markup_attr_name ~ ("=" ~ markup_attr_value)? }
70
106
  markup_comment = _{ "<!--" ~ (!"-->" ~ ANY)* ~ "-->" }
71
107
  markup_expression = _{ "{" ~ whitespace* ~ expression ~ whitespace* ~ "}" }
72
108
 
73
- markup_ident = @{ ASCII_ALPHANUMERIC ~ (ASCII_ALPHANUMERIC | "_")* }
74
- markup_namespaced_tag = { markup_ident ~ ("." ~ markup_ident)+ }
75
- markup_tag_name = { markup_namespaced_tag | markup_ident }
109
+ markup_ident = @{ ASCII_ALPHANUMERIC ~ (ASCII_ALPHANUMERIC | "_")* }
110
+ markup_namespaced_tag = { markup_ident ~ ("." ~ markup_ident)+ }
111
+ markup_tag_name = { markup_namespaced_tag | markup_ident }
76
112
 
77
113
  markup_attr_name = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_" | "-" | ":" | ".")* }
78
114
  markup_attr_value = @{ markup_string_literal_quote | markup_string_literal_doublequote | markup_expression }
@@ -1,4 +1,4 @@
1
- use pyo3::{prelude::*, pyclass, pyfunction, pymethods, Bound, PyResult, Python};
1
+ use pyo3::{Bound, PyResult, Python, prelude::*, pyclass, pyfunction, pymethods};
2
2
 
3
3
  use crate::{
4
4
  context::Literal,
@@ -277,6 +277,13 @@ fn extract_from_ast(ast: AST) -> PyResult<Vec<ExtractedMessage>> {
277
277
  res.extend(extract_from_ast(*else_branch.unwrap().clone())?);
278
278
  }
279
279
  }
280
+
281
+ AST::MatchStatement { value: _, arms } => {
282
+ for arm in arms {
283
+ res.extend(extract_from_ast(*arm.body.clone())?);
284
+ }
285
+ }
286
+
280
287
  AST::ForStatement {
281
288
  ident: _,
282
289
  iterable: _,