dotted-notation 0.55.3__tar.gz → 0.55.5__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 (51) hide show
  1. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/CHANGELOG.md +38 -0
  2. {dotted_notation-0.55.3/dotted_notation.egg-info → dotted_notation-0.55.5}/PKG-INFO +60 -2
  3. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/README.md +59 -1
  4. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/access.py +26 -0
  5. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/api.py +86 -70
  6. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/base.py +63 -16
  7. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/grammar.py +7 -1
  8. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/groups.py +37 -34
  9. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/recursive.py +29 -12
  10. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/wrappers.py +31 -6
  11. {dotted_notation-0.55.3 → dotted_notation-0.55.5/dotted_notation.egg-info}/PKG-INFO +60 -2
  12. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/pyproject.toml +1 -1
  13. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/LICENSE +0 -0
  14. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/MANIFEST.in +0 -0
  15. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/__init__.py +0 -0
  16. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/__main__.py +0 -0
  17. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/access.pxd +0 -0
  18. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/base.pxd +0 -0
  19. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/cli/__init__.py +0 -0
  20. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/cli/_compat.py +0 -0
  21. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/cli/formats.py +0 -0
  22. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/cli/main.py +0 -0
  23. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/containers.py +0 -0
  24. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/engine.pxd +0 -0
  25. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/engine.py +0 -0
  26. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/filters.pxd +0 -0
  27. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/filters.py +0 -0
  28. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/groups.pxd +0 -0
  29. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/matchers.pxd +0 -0
  30. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/matchers.py +0 -0
  31. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/native.py +0 -0
  32. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/predicates.py +0 -0
  33. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/recursive.pxd +0 -0
  34. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/results.py +0 -0
  35. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/transforms.py +0 -0
  36. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/__init__.py +0 -0
  37. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/core.py +0 -0
  38. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/spark.py +0 -0
  39. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/spark_pyspark.py +0 -0
  40. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/sql.py +0 -0
  41. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/translators/sql_pg.py +0 -0
  42. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/utils.py +0 -0
  43. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/utypes.py +0 -0
  44. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted/wrappers.pxd +0 -0
  45. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted_notation.egg-info/SOURCES.txt +0 -0
  46. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted_notation.egg-info/dependency_links.txt +0 -0
  47. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted_notation.egg-info/entry_points.txt +0 -0
  48. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted_notation.egg-info/requires.txt +0 -0
  49. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/dotted_notation.egg-info/top_level.txt +0 -0
  50. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/setup.cfg +0 -0
  51. {dotted_notation-0.55.3 → dotted_notation-0.55.5}/setup.py +0 -0
@@ -3,6 +3,44 @@
3
3
  All notable changes to `dotted` are recorded here. Versions prior to
4
4
  the ones listed are omitted — browse git history for earlier entries.
5
5
 
6
+ ## [0.55.5]
7
+
8
+ ### Fixed
9
+ - `match` and `match_multi` honor `strict=True`, which they took and
10
+ ignored: a key pattern matches only a key, a slot pattern only a
11
+ slot, an attr pattern only an attr, as strict mode does for `get`.
12
+ `match('[*]', 'table', strict=True)` and `match('*', '[0]',
13
+ strict=True)` are `None`. `**` reads keys, so under strict it
14
+ consumes only key segments; `*(.*,[*])` reads both.
15
+
16
+ ## [0.55.4]
17
+
18
+ ### Changed
19
+ - A `match(..., groups=...)` capture is the path's segment as the path
20
+ writes it, accessor, filters, value guard and transforms included:
21
+ `match('*[*].*', 'lots[*&a=1].d', groups=True)` captures
22
+ `('lots', '[*&a=1]', 'd')`, and `[3]` captures `'[3]'`, not `3`.
23
+ A filtered pattern segment, `*&id=*`, captures once, the whole
24
+ segment, and a negation, `(!a.b)`, captures the segments it consumed
25
+ as one.
26
+ - `translate`: a `$N` that is a whole segment, `.$0` or `[$0]`, becomes
27
+ the captured segment, its accessor included:
28
+ `translate('bye[3]', {'bye[*]': 'gone.$0'})` is `'gone[3]'`.
29
+
30
+ ### Fixed
31
+ - Under partial matching, a pattern with `**` (or a group on either
32
+ side) drops no part of the path: the last capture runs to its end,
33
+ `match('**.b', 'a.b.c', groups=True)` captures `('a', 'b.c')`.
34
+ - A value-guarded path segment, `a=1` or `a.b!=1`, matches an
35
+ unguarded pattern, as a filtered one does.
36
+ - A whole-list slice, `a[]` or `a[:]`, matches a slice filter,
37
+ `a[x=1]`: the filter only narrows the list. A partial slice, `a[1:]`,
38
+ still does not.
39
+ - `a&x=1.b` parses as `(a&x=1, b)`: a dot in a number needs a digit
40
+ or an exponent after it (`1.5`, `1.e5`), so the dot starts the next
41
+ segment instead of ending the float `1.`. `a&x=1.` and `[1.]` no
42
+ longer parse.
43
+
6
44
  ## [0.55.3]
7
45
 
8
46
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotted_notation
3
- Version: 0.55.3
3
+ Version: 0.55.5
4
4
  Summary: Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms
5
5
  Author-email: Frey Waid <logophage1@gmail.com>
6
6
  License-Expression: MIT
@@ -619,6 +619,19 @@ With the `groups=True` parameter, you'll see how it was matched:
619
619
  In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
620
620
  matching is enabled by default).
621
621
 
622
+ A capture is the path's segment as the path writes it: its accessor, filters, value
623
+ guard and transforms come with it. A filter, guard or transform on the path only
624
+ narrows a segment, so an undecorated pattern still matches it:
625
+
626
+ >>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
627
+ ('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
628
+ >>> dotted.match('*.*', 'a.b|int', groups=True)
629
+ ('a.b|int', ('a', 'b|int'))
630
+ >>> dotted.match('*', 'a=1', groups=True)
631
+ ('a=1', ('a=1',))
632
+ >>> dotted.match('**.b', 'a.b.c', groups=True)
633
+ ('a.b.c', ('a', 'b.c'))
634
+
622
635
  Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
623
636
  excluding literals:
624
637
 
@@ -680,6 +693,26 @@ only a recursive pattern that subsumes it will match:
680
693
  '*b'
681
694
  >>> dotted.match('*', '**')
682
695
 
696
+ A slice filter reads the list narrowed to the items that pass it, so the
697
+ slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
698
+ depends on the data, so no part of the list covers it, and neither does
699
+ `[*]`, which steps into each item rather than reading the list:
700
+
701
+ >>> dotted.match('a[]', 'a[x=1]')
702
+ 'a[x=1]'
703
+ >>> dotted.match('a[:]', 'a[x=1]', groups=True)
704
+ ('a[x=1]', ('a', '[x=1]'))
705
+ >>> dotted.match('a[1:]', 'a[x=1]')
706
+ >>> dotted.match('a[*]', 'a[x=1]')
707
+
708
+ A segment matches however it is read: `[*]` matches the key `table`, and `*` the slot
709
+ `[0]`. Pass `strict=True` to keep keys, slots and attrs apart, as
710
+ [strict mode](#strict-mode) does for `get`:
711
+
712
+ >>> dotted.match('[*]', 'table')
713
+ 'table'
714
+ >>> dotted.match('[*]', 'table', strict=True)
715
+
683
716
  <a id="replace"></a>
684
717
  ### Replace
685
718
 
@@ -743,6 +776,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
743
776
  >>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
744
777
  'Y.X'
745
778
 
779
+ A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
780
+ path writes it, its accessor included:
781
+
782
+ >>> translate('bye[3]', {'bye[*]': 'gone.$0'})
783
+ 'gone[3]'
784
+ >>> translate('bye.3', {'bye[*]': 'gone.$0'})
785
+ 'gone.3'
786
+ >>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
787
+ 'gone[*&a=1]'
788
+
746
789
  The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
747
790
  Returns `None` if no pattern matches:
748
791
 
@@ -1147,7 +1190,7 @@ Pass `strict=True` to enforce this separation:
1147
1190
  True
1148
1191
 
1149
1192
  Strict mode applies to all operations — `get`, `update`, `remove`, `has`, `expand`,
1150
- `pluck`, and their `_multi` variants. Mismatches silently return empty results, consistent
1193
+ `pluck`, `match`, and their `_multi` variants. Mismatches silently return empty results, consistent
1151
1194
  with dotted's safe traversal model:
1152
1195
 
1153
1196
  >>> dotted.has(d, '[0]', strict=True)
@@ -1162,6 +1205,21 @@ with dotted's safe traversal model:
1162
1205
  Use strict mode when your data mixes integer dict keys and list indices, and you need
1163
1206
  to distinguish between them.
1164
1207
 
1208
+ `match` and `match_multi` take `strict` too. By default a segment matches however it is
1209
+ read, so `[*]` matches a key and `*` a slot. Strict keeps them apart, as `get` does: a key
1210
+ pattern matches only a key, a slot pattern only a slot, an attr pattern only an attr.
1211
+ `**` reads keys, so it consumes only key segments; `*(.*,[*])` reads both:
1212
+
1213
+ >>> dotted.match('[*]', 'table')
1214
+ 'table'
1215
+ >>> dotted.match('[*]', 'table', strict=True)
1216
+ >>> dotted.match('*', '[0]', strict=True)
1217
+ >>> dotted.match('[*]', '[0]', strict=True)
1218
+ '[0]'
1219
+ >>> dotted.match('**.b', 'a[0].b', strict=True)
1220
+ >>> dotted.match('*(.*,[*]).b', 'a[0].b', strict=True)
1221
+ 'a[0].b'
1222
+
1165
1223
  <a id="paths"></a>
1166
1224
  ## Paths
1167
1225
 
@@ -569,6 +569,19 @@ With the `groups=True` parameter, you'll see how it was matched:
569
569
  In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
570
570
  matching is enabled by default).
571
571
 
572
+ A capture is the path's segment as the path writes it: its accessor, filters, value
573
+ guard and transforms come with it. A filter, guard or transform on the path only
574
+ narrows a segment, so an undecorated pattern still matches it:
575
+
576
+ >>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
577
+ ('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
578
+ >>> dotted.match('*.*', 'a.b|int', groups=True)
579
+ ('a.b|int', ('a', 'b|int'))
580
+ >>> dotted.match('*', 'a=1', groups=True)
581
+ ('a=1', ('a=1',))
582
+ >>> dotted.match('**.b', 'a.b.c', groups=True)
583
+ ('a.b.c', ('a', 'b.c'))
584
+
572
585
  Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
573
586
  excluding literals:
574
587
 
@@ -630,6 +643,26 @@ only a recursive pattern that subsumes it will match:
630
643
  '*b'
631
644
  >>> dotted.match('*', '**')
632
645
 
646
+ A slice filter reads the list narrowed to the items that pass it, so the
647
+ slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
648
+ depends on the data, so no part of the list covers it, and neither does
649
+ `[*]`, which steps into each item rather than reading the list:
650
+
651
+ >>> dotted.match('a[]', 'a[x=1]')
652
+ 'a[x=1]'
653
+ >>> dotted.match('a[:]', 'a[x=1]', groups=True)
654
+ ('a[x=1]', ('a', '[x=1]'))
655
+ >>> dotted.match('a[1:]', 'a[x=1]')
656
+ >>> dotted.match('a[*]', 'a[x=1]')
657
+
658
+ A segment matches however it is read: `[*]` matches the key `table`, and `*` the slot
659
+ `[0]`. Pass `strict=True` to keep keys, slots and attrs apart, as
660
+ [strict mode](#strict-mode) does for `get`:
661
+
662
+ >>> dotted.match('[*]', 'table')
663
+ 'table'
664
+ >>> dotted.match('[*]', 'table', strict=True)
665
+
633
666
  <a id="replace"></a>
634
667
  ### Replace
635
668
 
@@ -693,6 +726,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
693
726
  >>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
694
727
  'Y.X'
695
728
 
729
+ A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
730
+ path writes it, its accessor included:
731
+
732
+ >>> translate('bye[3]', {'bye[*]': 'gone.$0'})
733
+ 'gone[3]'
734
+ >>> translate('bye.3', {'bye[*]': 'gone.$0'})
735
+ 'gone.3'
736
+ >>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
737
+ 'gone[*&a=1]'
738
+
696
739
  The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
697
740
  Returns `None` if no pattern matches:
698
741
 
@@ -1097,7 +1140,7 @@ Pass `strict=True` to enforce this separation:
1097
1140
  True
1098
1141
 
1099
1142
  Strict mode applies to all operations — `get`, `update`, `remove`, `has`, `expand`,
1100
- `pluck`, and their `_multi` variants. Mismatches silently return empty results, consistent
1143
+ `pluck`, `match`, and their `_multi` variants. Mismatches silently return empty results, consistent
1101
1144
  with dotted's safe traversal model:
1102
1145
 
1103
1146
  >>> dotted.has(d, '[0]', strict=True)
@@ -1112,6 +1155,21 @@ with dotted's safe traversal model:
1112
1155
  Use strict mode when your data mixes integer dict keys and list indices, and you need
1113
1156
  to distinguish between them.
1114
1157
 
1158
+ `match` and `match_multi` take `strict` too. By default a segment matches however it is
1159
+ read, so `[*]` matches a key and `*` a slot. Strict keeps them apart, as `get` does: a key
1160
+ pattern matches only a key, a slot pattern only a slot, an attr pattern only an attr.
1161
+ `**` reads keys, so it consumes only key segments; `*(.*,[*])` reads both:
1162
+
1163
+ >>> dotted.match('[*]', 'table')
1164
+ 'table'
1165
+ >>> dotted.match('[*]', 'table', strict=True)
1166
+ >>> dotted.match('*', '[0]', strict=True)
1167
+ >>> dotted.match('[*]', '[0]', strict=True)
1168
+ '[0]'
1169
+ >>> dotted.match('**.b', 'a[0].b', strict=True)
1170
+ >>> dotted.match('*(.*,[*]).b', 'a[0].b', strict=True)
1171
+ 'a[0].b'
1172
+
1115
1173
  <a id="paths"></a>
1116
1174
  ## Paths
1117
1175
 
@@ -443,6 +443,9 @@ class Key(AccessOp):
443
443
  except (IndexError, TypeError):
444
444
  return ()
445
445
 
446
+ def access_kind(self):
447
+ return 'key'
448
+
446
449
  def default(self):
447
450
  if self.is_pattern():
448
451
  return {}
@@ -609,6 +612,9 @@ class Attr(Key):
609
612
  result = self._concrete_fallback(node, result, filtered, settings)
610
613
  return result
611
614
 
615
+ def access_kind(self):
616
+ return 'attr'
617
+
612
618
  def default(self):
613
619
  o = types.SimpleNamespace()
614
620
  if self.is_pattern():
@@ -707,6 +713,9 @@ class Slot(Key):
707
713
 
708
714
  return self._items(node, keys, filtered, settings)
709
715
 
716
+ def access_kind(self):
717
+ return 'slot'
718
+
710
719
  def default(self):
711
720
  if isinstance(self.op, matchers.Numeric) and self.op.is_int():
712
721
  return []
@@ -900,6 +909,9 @@ class SliceFilter(BaseOp):
900
909
  def concrete(cls, val):
901
910
  return Slot.concrete(val)
902
911
 
912
+ def access_kind(self):
913
+ return 'slot'
914
+
903
915
  def is_pattern(self):
904
916
  return False
905
917
 
@@ -1013,6 +1025,8 @@ class Slice(SimpleOp):
1013
1025
  if step is not None:
1014
1026
  m += [s(step)]
1015
1027
  return '[' + ':'.join(m) + ']'
1028
+ def access_kind(self):
1029
+ return 'slot'
1016
1030
  def is_pattern(self):
1017
1031
  return False
1018
1032
  def is_slice(self):
@@ -1103,7 +1117,19 @@ class Slice(SimpleOp):
1103
1117
  def matchable(self, op):
1104
1118
  return isinstance(op, Slice)
1105
1119
 
1120
+ def is_whole(self):
1121
+ """
1122
+ True if this slice reads the whole list: [], [:], [0:], [::1].
1123
+ """
1124
+ start, stop, step = self.args
1125
+ return start in (None, 0) and stop is None and step in (None, 1)
1126
+
1106
1127
  def match(self, op, specials=False):
1128
+ # a slice filter reads the list narrowed to the items that pass
1129
+ # it, which ones depending on the data: only the whole list
1130
+ # covers it
1131
+ if isinstance(op, SliceFilter):
1132
+ return base.MatchResult(slice(None)) if self.is_whole() else None
1107
1133
  if not isinstance(op, Slice):
1108
1134
  return None
1109
1135
  if self.cardinality() < op.cardinality():
@@ -880,6 +880,16 @@ def match(pattern, path, groups=False, partial=True, strict=False):
880
880
  >>> match('hello.*', 'hello.there.bye', groups='patterns')
881
881
  ('hello.there.bye', ('there.bye',))
882
882
 
883
+ A segment matches however it is read, unless strict=True keeps a key
884
+ to keys, a slot to slots and an attr to attrs, as strict mode does
885
+ for get:
886
+ >>> match('[*]', 'table')
887
+ 'table'
888
+ >>> match('[*]', 'table', strict=True)
889
+ >>> match('*', '[0]', strict=True)
890
+ >>> match('[*]', '[0]', strict=True)
891
+ '[0]'
892
+
883
893
  Group patterns match if any branch matches:
884
894
  >>> match('(a,b)', 'a')
885
895
  'a'
@@ -937,83 +947,58 @@ def match(pattern, path, groups=False, partial=True, strict=False):
937
947
  '*b'
938
948
  >>> match('*', '**')
939
949
  """
940
- return _match_parsed(parse(pattern), parse(path), path, groups, partial)
950
+ return _match_parsed(parse(pattern), parse(path), path, groups, partial, strict)
941
951
 
942
952
 
943
- def _match_parsed(pats, path_ops, path, groups, partial):
953
+ def _match_parsed(pats, path_ops, path, groups, partial, strict=False):
944
954
  """
945
955
  match() on parsed arguments. `path` is what a successful match returns.
946
- """
947
- # matching is structural: a path op's transforms, a.b|int, play no part
948
- if any(isinstance(op, wrappers.TransformWrap) for op in path_ops):
949
- path_ops = results.Dotted({'ops': [op.inner if isinstance(op, wrappers.TransformWrap) else op for op in path_ops]})
956
+ Each capture is the path segments one pattern segment matched, as the
957
+ path writes them.
958
+ """
959
+ caps = _captures(pats, path_ops, partial, bool(groups), strict)
960
+ if not groups:
961
+ return None if caps is None else path
962
+ if caps is None:
963
+ return (None, ())
950
964
  # groups can be a bool, a GroupMode member, or its string value.
951
- _patterns_only = (groups == GroupMode.patterns
952
- or groups == GroupMode.patterns.value)
965
+ if groups == GroupMode.patterns or groups == GroupMode.patterns.value:
966
+ caps = [c for c in caps if c[1]]
967
+ return (path, tuple(results.assemble(ops) for ops, _ in caps))
953
968
 
954
- def returns(r, matches, is_pat=None):
955
- if not groups:
956
- return r
957
- if _patterns_only and is_pat is not None:
958
- matches = [m for m, ip in zip(matches, is_pat) if ip]
959
- return (r, tuple(matches))
960
969
 
970
+ def _captures(pats, path_ops, partial, collect=True, strict=False):
971
+ """
972
+ The captures of the path that pats match, as base.match_ops gives
973
+ them: (ops, is_pattern) pairs, each ops the path ops one pattern
974
+ segment matched. None when pats don't match. Without collect, a
975
+ match may give no captures. Under strict, a segment matches only
976
+ one read the same way: a key a key, a slot a slot, an attr an attr.
977
+ """
961
978
  # Variadic ops (recursive, groups) consume variable-length path
962
979
  # segments — use the recursive matcher. On the path side they make
963
980
  # the path denote multiple expansions, which likewise needs it.
964
981
  if pats.variadic or path_ops.variadic:
965
- result = base.match_ops(list(pats), list(path_ops), partial)
966
- if result is None:
967
- return returns(None, [])
968
- return returns(path, [v for v, _ in result], [p for _, p in result])
969
-
970
- # Original non-recursive match logic
971
- collect = bool(groups)
972
- _matches = []
973
- _is_pat = []
974
- for idx,(pop,kop) in enumerate(zip(pats, path_ops)):
975
- # this means we have more pattern constraints than path segments
976
- if kop is None:
977
- return returns(None, [], [])
978
- if pop is None:
979
- break
980
- m = pop.match(kop, specials=True)
981
- if not m:
982
- return returns(None, [], [])
983
- if not collect:
984
- # captures are only returned with groups
985
- continue
986
- is_pat = pop.is_pattern()
987
- if isinstance(m, (tuple, list)):
988
- for _m in m:
989
- _matches.append(_m.val)
990
- _is_pat.append(is_pat)
991
- else:
992
- _matches.append(m.val)
993
- _is_pat.append(is_pat)
994
-
995
- # we've completed matching but the last item in match groups is treated 'greedily'
996
- assert kop is not None # sanity
997
-
998
- # exact match
999
- if len(pats) == len(path_ops):
1000
- return returns(path, _matches, _is_pat)
1001
-
1002
- # if we're not doing partial matches or we haven't consumed all pats, fail
1003
- if not partial or idx < len(pats) - 1:
1004
- return returns(None, [], [])
1005
-
1006
- # otherwise inexact (partial) match
1007
- if not collect:
1008
- return path
1009
- # assemble remaining segments
1010
- rpath = path_ops.assemble(start=idx)
1011
- if pop is None:
1012
- _matches.append(rpath)
1013
- _is_pat.append(False)
1014
- else:
1015
- _matches[-1] = rpath
1016
- return returns(path, _matches, _is_pat)
982
+ return base.match_ops(list(pats), list(path_ops), partial, strict)
983
+
984
+ # one path segment per pattern segment; a partial match's last
985
+ # capture runs to the end of the path
986
+ if len(pats) > len(path_ops):
987
+ return None
988
+ if len(pats) < len(path_ops) and not partial:
989
+ return None
990
+ caps = []
991
+ for pop, kop in zip(pats, path_ops):
992
+ target = kop.match_target()
993
+ if strict and not base.same_access(pop, target):
994
+ return None
995
+ if not pop.match(target, specials=True):
996
+ return None
997
+ if collect:
998
+ caps.append(((kop,), pop.is_pattern()))
999
+ if collect and len(pats) < len(path_ops):
1000
+ caps[-1] = (tuple(path_ops.ops[len(pats) - 1:]), caps[-1][1])
1001
+ return caps
1017
1002
 
1018
1003
 
1019
1004
  def match_multi(pattern, iterable, groups=False, partial=True, strict=False):
@@ -1027,7 +1012,7 @@ def match_multi(pattern, iterable, groups=False, partial=True, strict=False):
1027
1012
  for p in iterable:
1028
1013
  if pats is None:
1029
1014
  pats = parse(pattern)
1030
- yield _match_parsed(pats, parse(p), p, groups, partial)
1015
+ yield _match_parsed(pats, parse(p), p, groups, partial, strict)
1031
1016
 
1032
1017
  matches = _matches()
1033
1018
  if groups:
@@ -1068,17 +1053,48 @@ def translate(path, pattern_map):
1068
1053
  pats = parse(pattern)
1069
1054
  if path_ops is None:
1070
1055
  path_ops = parse(path)
1071
- (r, groups) = _match_parsed(pats, path_ops, path, GroupMode.patterns, False)
1072
- if not r:
1056
+ caps = _captures(pats, path_ops, False)
1057
+ if caps is None:
1073
1058
  continue
1074
1059
  try:
1075
- return replace(template, groups)
1060
+ return _fill(template, [ops for ops, is_pat in caps if is_pat])
1076
1061
  except IndexError:
1077
1062
  continue
1078
1063
 
1079
1064
  return None
1080
1065
 
1081
1066
 
1067
+ def _fill(template, captures):
1068
+ """
1069
+ `template` with each $N filled from captures[N], the path ops of one
1070
+ capture. A $N that is a whole segment, gone.$0 or gone[$0], becomes
1071
+ the captured segment as the path writes it, its accessor included:
1072
+ gone.$0 on a capture of [3] is gone[3]. A $N anywhere else takes the
1073
+ capture's text, as replace() does.
1074
+ """
1075
+ texts = [results.assemble(ops) for ops in captures]
1076
+ ops = []
1077
+ for op in parse(template):
1078
+ n = _segment_subst(op)
1079
+ if n is None:
1080
+ ops.append(op.resolve(texts, False))
1081
+ continue
1082
+ ops.extend(captures[n])
1083
+ return results.assemble(ops)
1084
+
1085
+
1086
+ def _segment_subst(op):
1087
+ """
1088
+ N when template op `op` is a whole $N segment, .$0 or [$0], else None.
1089
+ """
1090
+ if type(op) is not access.Key and type(op) is not access.Slot:
1091
+ return None
1092
+ sub = op.op
1093
+ if type(sub) is not matchers.Subst or sub.transforms or not isinstance(sub.value, int):
1094
+ return None
1095
+ return sub.value
1096
+
1097
+
1082
1098
  def translate_multi(paths, pattern_map):
1083
1099
  """
1084
1100
  Translate multiple paths via pattern_map.
@@ -42,26 +42,60 @@ def has_any(gen):
42
42
  return any(True for _ in gen)
43
43
 
44
44
 
45
- def match_ops(pats, path_ops, partial):
45
+ def match_ops(pats, path_ops, partial, strict=False):
46
46
  """
47
47
  Match a list of pattern ops against a list of path ops.
48
- Returns a list of (value, is_pattern) capture pairs on success, None
49
- on failure. Dispatch is per-op on both sides: a variadic op at the
50
- head of the path (group, recursive) decides how the pattern must
48
+ Returns a list of (ops, is_pattern) capture pairs on success, None
49
+ on failure: ops are the path ops one pattern segment matched, as the
50
+ path writes them. Dispatch is per-op on both sides: a variadic op at
51
+ the head of the path (group, recursive) decides how the pattern must
51
52
  cover it via do_match_path; otherwise the head pattern op decides
52
- how many path segments it consumes via do_match.
53
+ how many path segments it consumes via do_match. Under strict, a
54
+ segment matches only one read the same way (see same_access).
53
55
  """
54
56
  if path_ops and path_ops[0].is_variadic():
55
- return path_ops[0].do_match_path(list(pats), list(path_ops[1:]), partial)
57
+ return path_ops[0].do_match_path(list(pats), list(path_ops[1:]), partial, strict)
56
58
  if pats:
57
- return pats[0].do_match(pats[1:], path_ops, partial)
59
+ return pats[0].do_match(pats[1:], path_ops, partial, strict)
58
60
  if not path_ops:
59
61
  return []
60
62
  if partial:
61
- return []
63
+ return leftover(path_ops)
62
64
  return None
63
65
 
64
66
 
67
+ def leftover(path_ops):
68
+ """
69
+ The captures of what a partial match leaves of the path: one capture
70
+ with no pattern segment of its own, which joins the capture before it
71
+ (see capture).
72
+ """
73
+ return [(tuple(path_ops), None)]
74
+
75
+
76
+ def capture(path_ops, is_pat, rest):
77
+ """
78
+ The captures `rest` with a capture of `path_ops` first: the path ops
79
+ one pattern segment matched. A partial match's leftover at the head
80
+ of `rest` joins it, so the last capture runs to the end of the path.
81
+ """
82
+ ops = tuple(path_ops)
83
+ if rest and rest[0][1] is None:
84
+ return [(ops + rest[0][0], is_pat)] + rest[1:]
85
+ return [(ops, is_pat)] + rest
86
+
87
+
88
+ def same_access(pat, op):
89
+ """
90
+ True if pattern op `pat` and path op `op` read a node the same way,
91
+ as strict mode asks: a key only a key, a slot only a slot, an attr
92
+ only an attr. An op with no access of its own (a group) passes.
93
+ """
94
+ a = pat.access_kind()
95
+ b = op.access_kind()
96
+ return a is None or b is None or a == b
97
+
98
+
65
99
  class MatchResult:
66
100
  def __init__(self, val):
67
101
  self.val = val
@@ -110,6 +144,20 @@ class Op:
110
144
  return self
111
145
  def scrub(self, node):
112
146
  return node
147
+ def access_kind(self):
148
+ """
149
+ How this op reads a node: 'key', 'slot' or 'attr', or None when it
150
+ has no one way (a group, a recursive op). Strict mode keeps the
151
+ three apart (see same_access).
152
+ """
153
+ return None
154
+ def match_target(self):
155
+ """
156
+ What a pattern matches this path op as: the op itself. A
157
+ decoration that narrows only the values a segment reaches, a
158
+ value guard or a transform, plays no part (see wrappers).
159
+ """
160
+ return self
113
161
  def match_keys(self, node):
114
162
  """
115
163
  Keys of dict-like `node` that this op matches.
@@ -504,7 +552,7 @@ class TraversalOp(Op):
504
552
  val = getattr(getattr(self, 'op', self), 'value', self)
505
553
  return has_any(matcher.matches((val,)))
506
554
 
507
- def do_match(self, rest_pats, path_ops, partial):
555
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
508
556
  """
509
557
  Match this op against exactly one path segment, then continue with
510
558
  the remaining pattern ops. Variadic ops (recursive, groups)
@@ -512,16 +560,15 @@ class TraversalOp(Op):
512
560
  """
513
561
  if not path_ops:
514
562
  return None
515
- m = self.match(path_ops[0], specials=True)
516
- if not m:
563
+ target = path_ops[0].match_target()
564
+ if strict and not same_access(self, target):
565
+ return None
566
+ if not self.match(target, specials=True):
517
567
  return None
518
- rest = match_ops(rest_pats, path_ops[1:], partial)
568
+ rest = match_ops(rest_pats, path_ops[1:], partial, strict)
519
569
  if rest is None:
520
570
  return None
521
- is_pat = self.is_pattern()
522
- if isinstance(m, (tuple, list)):
523
- return [(_m.val, is_pat) for _m in m] + rest
524
- return [(m.val, is_pat)] + rest
571
+ return capture(path_ops[:1], self.is_pattern(), rest)
525
572
 
526
573
 
527
574
  class MatchOp(Op):
@@ -242,7 +242,13 @@ _numeric_quoted = S('#') + ((S("'") + ppc.number + S("'")) | (S('"') + ppc.numbe
242
242
  numeric_quoted = _numeric_quoted.set_parse_action(matchers.NumericQuoted)
243
243
 
244
244
  numeric_key = integer.copy().set_parse_action(matchers.Numeric)
245
- numeric_slot = ppc.number.copy().set_parse_action(matchers.Numeric)
245
+
246
+ # A number as pyparsing reads one, but a dot in it needs a digit or an
247
+ # exponent after it: in a&x=1.b the dot starts the next segment, it does
248
+ # not end 1. 1.e5 is still the float 100000.0.
249
+ _number = pp.Regex(r'[+-]?(?:[0-9]+(?:\.[0-9]+|\.(?=[eE][+-]?[0-9]))?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?')
250
+ _number.set_parse_action(lambda t: int(t[0]) if t[0].lstrip('+-').isdigit() else float(t[0]))
251
+ numeric_slot = _number.copy().add_parse_action(matchers.Numeric)
246
252
 
247
253
  # Extended numeric literals: scientific (1e10), underscore (1_000), hex (0x1F), octal (0o17), binary (0b1010)
248
254
  _numeric_extended_re = pp.Regex(
@@ -4,7 +4,6 @@ from . import base
4
4
  from . import matchers
5
5
  from . import wrappers
6
6
  from . import engine
7
- from . import results
8
7
  from .access import AccessOp, Key, Attr, Slot, SlotSpecial
9
8
 
10
9
 
@@ -191,7 +190,7 @@ class OpGroup(base.TraversalOp):
191
190
  def operator(self, top=False):
192
191
  return self._render(top)
193
192
 
194
- def do_match(self, rest_pats, path_ops, partial):
193
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
195
194
  """
196
195
  Match a concrete path against this group: the path matches if it
197
196
  matches any branch followed by the remaining pattern ops. A single
@@ -206,17 +205,16 @@ class OpGroup(base.TraversalOp):
206
205
  for branch in base.branches_only(self.branches):
207
206
  branch_ops = list(branch)
208
207
  for n in range(len(path_ops) + 1):
209
- consumed = base.match_ops(branch_ops, path_ops[:n], False)
208
+ consumed = base.match_ops(branch_ops, path_ops[:n], False, strict)
210
209
  if consumed is None:
211
210
  continue
212
- rest = base.match_ops(list(rest_pats), path_ops[n:], partial)
211
+ rest = base.match_ops(list(rest_pats), path_ops[n:], partial, strict)
213
212
  if rest is None:
214
213
  continue
215
- combined = results.assemble(path_ops[:n])
216
- return [(combined, True)] + rest
214
+ return base.capture(path_ops[:n], True, rest)
217
215
  return None
218
216
 
219
- def do_match_path(self, pats, rest_path, partial):
217
+ def do_match_path(self, pats, rest_path, partial, strict=False):
220
218
  """
221
219
  Match when this group appears on the *path* side. The path
222
220
  denotes every expansion of the group's branches, so the pattern
@@ -231,19 +229,21 @@ class OpGroup(base.TraversalOp):
231
229
  decomposition exists do captures fall back to the first
232
230
  branch's expansion.
233
231
  """
234
- fallback = self._verify_covered(pats, rest_path, partial)
232
+ if not pats and partial:
233
+ return base.leftover([self] + list(rest_path))
234
+ fallback = self._verify_covered(pats, rest_path, partial, strict)
235
235
  if fallback is None:
236
236
  return None
237
- r = self._aligned_captures(pats, rest_path, partial)
237
+ r = self._aligned_captures(pats, rest_path, partial, strict)
238
238
  if r is not None:
239
239
  return r
240
240
  if pats and pats[0].is_variadic():
241
- r = pats[0].do_match(pats[1:], [self] + list(rest_path), partial)
241
+ r = pats[0].do_match(pats[1:], [self] + list(rest_path), partial, strict)
242
242
  if r is not None:
243
243
  return r
244
244
  return fallback
245
245
 
246
- def _verify_covered(self, pats, rest_path, partial):
246
+ def _verify_covered(self, pats, rest_path, partial, strict=False):
247
247
  """
248
248
  Subsumption check over expansions: splice each branch into the
249
249
  path and match. Returns the first branch's result (the capture
@@ -251,16 +251,16 @@ class OpGroup(base.TraversalOp):
251
251
  """
252
252
  first = base.marker
253
253
  for branch in base.branches_only(self.branches):
254
- r = base.match_ops(pats, list(branch) + list(rest_path), partial)
254
+ r = base.match_ops(pats, list(branch) + list(rest_path), partial, strict)
255
255
  if r is None:
256
256
  return None
257
257
  if first is base.marker:
258
258
  first = r
259
259
  if first is base.marker:
260
- return base.match_ops(pats, list(rest_path), partial)
260
+ return base.match_ops(pats, list(rest_path), partial, strict)
261
261
  return first
262
262
 
263
- def _aligned_captures(self, pats, rest_path, partial):
263
+ def _aligned_captures(self, pats, rest_path, partial, strict=False):
264
264
  """
265
265
  Group-as-itself captures: find a pattern prefix that covers the
266
266
  branch set exactly (or up to a partial tail when the pattern
@@ -270,20 +270,20 @@ class OpGroup(base.TraversalOp):
270
270
  for k in range(1, len(pats) + 1):
271
271
  head = list(pats[:k])
272
272
  tail_partial = partial and k == len(pats) and not rest_path
273
- if not self._covers_branches(head, tail_partial):
273
+ if not self._covers_branches(head, tail_partial, strict):
274
274
  continue
275
- rest = base.match_ops(list(pats[k:]), list(rest_path), partial)
275
+ rest = base.match_ops(list(pats[k:]), list(rest_path), partial, strict)
276
276
  if rest is None:
277
277
  continue
278
278
  is_pat = any(p.is_pattern() for p in head)
279
- return [(results.assemble([self]), is_pat)] + rest
279
+ return base.capture([self], is_pat, rest)
280
280
  return None
281
281
 
282
- def _covers_branches(self, head_pats, tail_partial=False):
282
+ def _covers_branches(self, head_pats, tail_partial=False, strict=False):
283
283
  """
284
284
  True if head_pats covers every branch of this group.
285
285
  """
286
- return all(base.match_ops(head_pats, list(b), tail_partial) is not None
286
+ return all(base.match_ops(head_pats, list(b), tail_partial, strict) is not None
287
287
  for b in base.branches_only(self.branches))
288
288
 
289
289
  def covered_by(self, matcher):
@@ -658,23 +658,23 @@ class OpGroupAnd(OpGroup):
658
658
  def __repr__(self):
659
659
  return self._render(top=True)
660
660
 
661
- def _verify_covered(self, pats, rest_path, partial):
661
+ def _verify_covered(self, pats, rest_path, partial, strict=False):
662
662
  """
663
663
  A conjunction's expansions are the intersection of its
664
664
  branches', so a pattern covering any single branch covers the
665
665
  whole (sufficient, conservatively incomplete).
666
666
  """
667
667
  for branch in base.branches_only(self.branches):
668
- r = base.match_ops(pats, list(branch) + list(rest_path), partial)
668
+ r = base.match_ops(pats, list(branch) + list(rest_path), partial, strict)
669
669
  if r is not None:
670
670
  return r
671
671
  return None
672
672
 
673
- def _covers_branches(self, head_pats, tail_partial=False):
673
+ def _covers_branches(self, head_pats, tail_partial=False, strict=False):
674
674
  """
675
675
  Any single covered branch suffices for a conjunction.
676
676
  """
677
- return any(base.match_ops(head_pats, list(b), tail_partial) is not None
677
+ return any(base.match_ops(head_pats, list(b), tail_partial, strict) is not None
678
678
  for b in base.branches_only(self.branches))
679
679
 
680
680
  def push_children(self, stack, frame, paths):
@@ -890,24 +890,27 @@ class OpGroupNot(OpGroup):
890
890
  def default(self):
891
891
  return self._leaf().default()
892
892
 
893
- def do_match(self, rest_pats, path_ops, partial):
893
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
894
894
  """
895
895
  Match the segments of one subtree of the complement: they follow
896
896
  the inner branch op by op until one does not match, and that one
897
897
  ends the subtree; then continue with the remaining ops. A path
898
898
  the whole branch matches is in the negated pattern, not out of it.
899
+ Under strict, each segment is read as the inner op it meets is.
899
900
  """
900
901
  inner = self.inner
901
902
  for i, kop in enumerate(path_ops[:len(inner)]):
902
- if base.match_ops([inner[i]], [kop], False) is not None:
903
+ if strict and not base.same_access(inner[i], kop.match_target()):
904
+ return None
905
+ if base.match_ops([inner[i]], [kop], False, strict) is not None:
903
906
  continue
904
- rest = base.match_ops(rest_pats, path_ops[i + 1:], partial)
907
+ rest = base.match_ops(rest_pats, path_ops[i + 1:], partial, strict)
905
908
  if rest is None:
906
909
  return None
907
- return [(getattr(getattr(seg, 'op', seg), 'value', seg), True) for seg in path_ops[:i + 1]] + rest
910
+ return base.capture(path_ops[:i + 1], True, rest)
908
911
  return None
909
912
 
910
- def do_match_path(self, pats, rest_path, partial):
913
+ def do_match_path(self, pats, rest_path, partial, strict=False):
911
914
  """
912
915
  Match when this negation appears on the *path* side. It denotes
913
916
  every segment its inner pattern excludes — an open set — so it
@@ -915,14 +918,14 @@ class OpGroupNot(OpGroup):
915
918
  segment.
916
919
  """
917
920
  if not pats:
918
- return [] if partial else None
921
+ return base.leftover([self] + list(rest_path)) if partial else None
919
922
  head = pats[0]
920
923
  if head != self and not self._wildcard_covers(head):
921
924
  return None
922
- rest = base.match_ops(pats[1:], rest_path, partial)
925
+ rest = base.match_ops(pats[1:], rest_path, partial, strict)
923
926
  if rest is None:
924
927
  return None
925
- return [(results.assemble([self]), True)] + rest
928
+ return base.capture([self], True, rest)
926
929
 
927
930
  def _wildcard_covers(self, head):
928
931
  """
@@ -1018,16 +1021,16 @@ class Pack(base.TraversalOp):
1018
1021
  return None
1019
1022
  return base.MatchResult(self)
1020
1023
 
1021
- def do_match(self, rest_pats, path_ops, partial):
1024
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
1022
1025
  """
1023
1026
  One segment, matched by equality (see match).
1024
1027
  """
1025
1028
  if not path_ops or not self.match(path_ops[0]):
1026
1029
  return None
1027
- rest = base.match_ops(rest_pats, path_ops[1:], partial)
1030
+ rest = base.match_ops(rest_pats, path_ops[1:], partial, strict)
1028
1031
  if rest is None:
1029
1032
  return None
1030
- return [(self, False)] + rest
1033
+ return base.capture(path_ops[:1], False, rest)
1031
1034
 
1032
1035
  def operator(self, top=False):
1033
1036
  """
@@ -3,7 +3,6 @@
3
3
  from . import base
4
4
  from . import engine
5
5
  from . import matchers
6
- from . import results
7
6
  from . import utils
8
7
  from .access import BaseOp, Key, Attr
9
8
 
@@ -149,23 +148,41 @@ class Recursive(BaseOp):
149
148
  def match(self, op, specials=False):
150
149
  return self.inner.matchable(op, specials=specials)
151
150
 
152
- def do_match(self, rest_pats, path_ops, partial):
151
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
153
152
  """
154
153
  Match by consuming 1..N path segments via backtracking. Each
155
154
  consumed segment must match the inner pattern (chain-following);
156
- stop extending once a segment fails.
155
+ stop extending once a segment fails. Under strict, each must
156
+ also be read the way one of this op's accessors reads a node.
157
157
  """
158
158
  for n in range(1, len(path_ops) + 1):
159
- if not path_ops[n - 1].covered_by(self.inner):
159
+ seg = path_ops[n - 1]
160
+ if strict and not self._reads(seg):
160
161
  return None
161
- rest = base.match_ops(rest_pats, path_ops[n:], partial)
162
+ if not seg.covered_by(self.inner):
163
+ return None
164
+ rest = base.match_ops(rest_pats, path_ops[n:], partial, strict)
162
165
  if rest is None:
163
166
  continue
164
- combined = results.assemble(path_ops[:n])
165
- return [(combined, True)] + rest
167
+ return base.capture(path_ops[:n], True, rest)
166
168
  return None
167
169
 
168
- def do_match_path(self, pats, rest_path, partial):
170
+ def _reads(self, op):
171
+ """
172
+ True if one of this op's accessors reads a node the way path op
173
+ `op` does: ** reads keys, *(.*,[*]) keys and slots.
174
+ """
175
+ kind = op.match_target().access_kind()
176
+ return kind is None or kind in self._access_kinds
177
+
178
+ @utils.lazyprop
179
+ def _access_kinds(self):
180
+ """
181
+ How this op's accessors read a node (see base.Op.access_kind).
182
+ """
183
+ return {op.access_kind() for branch in base.branches_only(self._effective_branches()) for op in branch}
184
+
185
+ def do_match_path(self, pats, rest_path, partial, strict=False):
169
186
  """
170
187
  Match when this recursive op appears on the *path* side. It
171
188
  denotes unboundedly many expansions, so only a pattern segment
@@ -175,18 +192,18 @@ class Recursive(BaseOp):
175
192
  a recursive pattern may keep covering segments after the group.
176
193
  """
177
194
  if not pats:
178
- return [] if partial else None
195
+ return base.leftover([self] + list(rest_path)) if partial else None
179
196
  head = pats[0]
180
197
  if not isinstance(head, Recursive):
181
198
  return None
182
199
  if not self._subsumed_by(head):
183
200
  return None
184
- rest = base.match_ops(pats[1:], rest_path, partial)
201
+ rest = base.match_ops(pats[1:], rest_path, partial, strict)
185
202
  if rest is None:
186
- rest = base.match_ops(pats, rest_path, partial)
203
+ rest = base.match_ops(pats, rest_path, partial, strict)
187
204
  if rest is None:
188
205
  return None
189
- return [(results.assemble([self]), True)] + rest
206
+ return base.capture([self], True, rest)
190
207
 
191
208
  def _subsumed_by(self, pat):
192
209
  """
@@ -85,21 +85,32 @@ class Wrap(base.TraversalOp):
85
85
  """
86
86
  return self.inner.leaf_op()
87
87
 
88
- def do_match(self, rest_pats, path_ops, partial):
88
+ def access_kind(self):
89
+ """
90
+ A wrap decorates; it reads a node as the wrapped op does.
91
+ """
92
+ return self.inner.access_kind()
93
+
94
+ def do_match(self, rest_pats, path_ops, partial, strict=False):
89
95
  """
90
96
  Variadic inners (recursive ops, groups) decide their own segment
91
97
  consumption; otherwise match one segment via this wrap's match.
92
98
  """
93
99
  if self.is_variadic():
94
- return self.inner.do_match(rest_pats, path_ops, partial)
95
- return super().do_match(rest_pats, path_ops, partial)
100
+ return self.inner.do_match(rest_pats, path_ops, partial, strict)
101
+ return super().do_match(rest_pats, path_ops, partial, strict)
96
102
 
97
- def do_match_path(self, pats, rest_path, partial):
103
+ def do_match_path(self, pats, rest_path, partial, strict=False):
98
104
  """
99
105
  Delegate path-side matching to the wrapped op (only reached for
100
- variadic inners, via is_variadic).
106
+ variadic inners, via is_variadic). A capture of the wrapped op
107
+ is a capture of this one: the segment as the path writes it.
101
108
  """
102
- return self.inner.do_match_path(pats, rest_path, partial)
109
+ r = self.inner.do_match_path(pats, rest_path, partial, strict)
110
+ if r is None:
111
+ return None
112
+ inner = self.inner
113
+ return [(tuple(self if op is inner else op for op in ops), is_pat) for ops, is_pat in r]
103
114
 
104
115
  def covered_by(self, matcher):
105
116
  """
@@ -213,6 +224,13 @@ class TransformWrap(Wrap):
213
224
  """
214
225
  return TransformWrap(inner, self.transforms)
215
226
 
227
+ def match_target(self):
228
+ """
229
+ A transform changes what the segment yields, not which segment it
230
+ is: a pattern matches the wrapped op.
231
+ """
232
+ return self.inner.match_target()
233
+
216
234
  def __repr__(self):
217
235
  return self.operator(top=True)
218
236
 
@@ -398,6 +416,13 @@ class ValueGuard(Wrap):
398
416
  """
399
417
  return self.pred_op is predicates.NE
400
418
 
419
+ def match_target(self):
420
+ """
421
+ A guard narrows the values a segment reaches, as a filter does:
422
+ a pattern matches the wrapped op.
423
+ """
424
+ return self.inner.match_target()
425
+
401
426
  def __repr__(self):
402
427
  return self.operator(top=True)
403
428
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotted_notation
3
- Version: 0.55.3
3
+ Version: 0.55.5
4
4
  Summary: Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms
5
5
  Author-email: Frey Waid <logophage1@gmail.com>
6
6
  License-Expression: MIT
@@ -619,6 +619,19 @@ With the `groups=True` parameter, you'll see how it was matched:
619
619
  In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
620
620
  matching is enabled by default).
621
621
 
622
+ A capture is the path's segment as the path writes it: its accessor, filters, value
623
+ guard and transforms come with it. A filter, guard or transform on the path only
624
+ narrows a segment, so an undecorated pattern still matches it:
625
+
626
+ >>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
627
+ ('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
628
+ >>> dotted.match('*.*', 'a.b|int', groups=True)
629
+ ('a.b|int', ('a', 'b|int'))
630
+ >>> dotted.match('*', 'a=1', groups=True)
631
+ ('a=1', ('a=1',))
632
+ >>> dotted.match('**.b', 'a.b.c', groups=True)
633
+ ('a.b.c', ('a', 'b.c'))
634
+
622
635
  Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
623
636
  excluding literals:
624
637
 
@@ -680,6 +693,26 @@ only a recursive pattern that subsumes it will match:
680
693
  '*b'
681
694
  >>> dotted.match('*', '**')
682
695
 
696
+ A slice filter reads the list narrowed to the items that pass it, so the
697
+ slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
698
+ depends on the data, so no part of the list covers it, and neither does
699
+ `[*]`, which steps into each item rather than reading the list:
700
+
701
+ >>> dotted.match('a[]', 'a[x=1]')
702
+ 'a[x=1]'
703
+ >>> dotted.match('a[:]', 'a[x=1]', groups=True)
704
+ ('a[x=1]', ('a', '[x=1]'))
705
+ >>> dotted.match('a[1:]', 'a[x=1]')
706
+ >>> dotted.match('a[*]', 'a[x=1]')
707
+
708
+ A segment matches however it is read: `[*]` matches the key `table`, and `*` the slot
709
+ `[0]`. Pass `strict=True` to keep keys, slots and attrs apart, as
710
+ [strict mode](#strict-mode) does for `get`:
711
+
712
+ >>> dotted.match('[*]', 'table')
713
+ 'table'
714
+ >>> dotted.match('[*]', 'table', strict=True)
715
+
683
716
  <a id="replace"></a>
684
717
  ### Replace
685
718
 
@@ -743,6 +776,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
743
776
  >>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
744
777
  'Y.X'
745
778
 
779
+ A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
780
+ path writes it, its accessor included:
781
+
782
+ >>> translate('bye[3]', {'bye[*]': 'gone.$0'})
783
+ 'gone[3]'
784
+ >>> translate('bye.3', {'bye[*]': 'gone.$0'})
785
+ 'gone.3'
786
+ >>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
787
+ 'gone[*&a=1]'
788
+
746
789
  The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
747
790
  Returns `None` if no pattern matches:
748
791
 
@@ -1147,7 +1190,7 @@ Pass `strict=True` to enforce this separation:
1147
1190
  True
1148
1191
 
1149
1192
  Strict mode applies to all operations — `get`, `update`, `remove`, `has`, `expand`,
1150
- `pluck`, and their `_multi` variants. Mismatches silently return empty results, consistent
1193
+ `pluck`, `match`, and their `_multi` variants. Mismatches silently return empty results, consistent
1151
1194
  with dotted's safe traversal model:
1152
1195
 
1153
1196
  >>> dotted.has(d, '[0]', strict=True)
@@ -1162,6 +1205,21 @@ with dotted's safe traversal model:
1162
1205
  Use strict mode when your data mixes integer dict keys and list indices, and you need
1163
1206
  to distinguish between them.
1164
1207
 
1208
+ `match` and `match_multi` take `strict` too. By default a segment matches however it is
1209
+ read, so `[*]` matches a key and `*` a slot. Strict keeps them apart, as `get` does: a key
1210
+ pattern matches only a key, a slot pattern only a slot, an attr pattern only an attr.
1211
+ `**` reads keys, so it consumes only key segments; `*(.*,[*])` reads both:
1212
+
1213
+ >>> dotted.match('[*]', 'table')
1214
+ 'table'
1215
+ >>> dotted.match('[*]', 'table', strict=True)
1216
+ >>> dotted.match('*', '[0]', strict=True)
1217
+ >>> dotted.match('[*]', '[0]', strict=True)
1218
+ '[0]'
1219
+ >>> dotted.match('**.b', 'a[0].b', strict=True)
1220
+ >>> dotted.match('*(.*,[*]).b', 'a[0].b', strict=True)
1221
+ 'a[0].b'
1222
+
1165
1223
  <a id="paths"></a>
1166
1224
  ## Paths
1167
1225
 
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
6
6
 
7
7
  [project]
8
8
  name = "dotted_notation"
9
- version = "0.55.3"
9
+ version = "0.55.5"
10
10
  description = "Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms"
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.6"