Skip to content

Commit 157df79

Browse files
committed
fix regex matcher body and next func
1 parent b6cc981 commit 157df79

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/jsonata/jsonata.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,14 @@ def apply_inner(self, proc: Optional[Any], args: Optional[Any], input: Optional[
14721472
elif isinstance(proc, Jsonata.JLambda):
14731473
result = proc.call(input, validated_args)
14741474
elif isinstance(proc, re.Pattern):
1475-
result = [s for s in validated_args if proc.search(s) is not None]
1475+
_res = []
1476+
for s in validated_args:
1477+
if isinstance(s, str):
1478+
_res.append(Jsonata._regex_closure(proc.finditer(s)))
1479+
if len(_res) == 1:
1480+
result = _res[0]
1481+
else:
1482+
result = _res
14761483
else:
14771484
print("Proc not found " + str(proc))
14781485
raise jexception.JException("T1006", 0)
@@ -1486,6 +1493,19 @@ def apply_inner(self, proc: Optional[Any], args: Optional[Any], input: Optional[
14861493
raise err
14871494
return result
14881495

1496+
@staticmethod
1497+
def _regex_closure(iterator):
1498+
m = next(iterator, None)
1499+
if m is None:
1500+
return None
1501+
return {
1502+
"match": m.group(),
1503+
"start": m.start(),
1504+
"end": m.end(),
1505+
"groups": [m.group()],
1506+
"next": Jsonata.JLambda(lambda: Jsonata._regex_closure(iterator))
1507+
}
1508+
14891509
#
14901510
# Evaluate lambda against input data
14911511
# @param {Object} expr - JSONata expression

src/jsonata/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,8 @@ def process_ast(self, expr: Optional[Symbol]) -> Optional[Symbol]:
10471047
rest = self.process_ast(expr.rhs)
10481048
if (rest.type == "function" and rest.procedure.type == "path" and len(
10491049
rest.procedure.steps) == 1 and rest.procedure.steps[0].type == "name" and
1050-
result.steps[-1].type == "function"):
1050+
result.steps[-1].type == "function" and
1051+
isinstance(rest.procedure.steps[0].value, Parser.Symbol)):
10511052
# next function in chain of functions - will override a thenable
10521053
result.steps[-1].next_function = rest.procedure.steps[0].value
10531054
if rest.type == "path":

tests/string_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ def test_eval_regex(self):
5252
result = expr.evaluate(None)
5353
assert result.pattern == "^test.*$"
5454

55+
def test_eval_regex_check_answer_data(self):
56+
expr = jsonata.Jsonata("(\n $matcher := $eval('/l/');\n ('Hello World' ~> $matcher);\n)")
57+
result = expr.evaluate(None)
58+
assert result["match"] == "l"
59+
assert result["start"] == 2
60+
assert result["end"] == 3
61+
assert result["groups"] == ["l"]
62+
assert callable(result["next"].function)
63+
64+
def test_eval_regex_call_next_and_check_result(self):
65+
expr = jsonata.Jsonata("(\n $matcher := $eval('/l/');\n ('Hello World' ~> $matcher).next();\n)")
66+
result = expr.evaluate(None)
67+
assert result["match"] == "l"
68+
assert result["start"] == 3
69+
assert result["end"] == 4
70+
assert result["groups"] == ["l"]
71+
5572
#
5673
# Additional $split tests
5774
#

0 commit comments

Comments
 (0)