From 0f6fc2a472aab6ac8d3d520453f75611a2f47dbe Mon Sep 17 00:00:00 2001 From: Andy C Date: Sat, 16 Dec 2023 20:55:45 -0500 Subject: [PATCH] [eggex] Allow Int or Str arg for Match accessors Still need to calculate the index from the Str. --- builtin/func_eggex.py | 38 ++++++++++++++++++++++++++++++-------- spec/ysh-regex-api.test.sh | 7 ++----- ysh/regex_translate.py | 1 - 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/builtin/func_eggex.py b/builtin/func_eggex.py index 1b5e0386f7..09bfedc948 100644 --- a/builtin/func_eggex.py +++ b/builtin/func_eggex.py @@ -5,14 +5,14 @@ from __future__ import print_function from _devbuild.gen.syntax_asdl import loc_t -from _devbuild.gen.value_asdl import value, value_t +from _devbuild.gen.value_asdl import value, value_e, value_t from core import error from core import state from core import vm from frontend import typed_args -from mycpp.mylib import log +from mycpp.mylib import log, tagswitch -from typing import List +from typing import List, cast _ = log @@ -64,8 +64,19 @@ def __init__(self, mem, to_return): def Call(self, rd): # type: (typed_args.Reader) -> value_t - # TODO: Support strings for named captures - i = rd.OptionalInt(default_=0) + group = rd.PosValue() + UP_group = group + with tagswitch(group) as case: + if case(value_e.Int): + group = cast(value.Int, UP_group) + i = group.i + elif case(value_e.Str): + group = cast(value.Str, UP_group) + # TODO: calculate from mem registers + i = 0 + else: + raise error.TypeErr(group, 'Expected Int or Str', + rd.LeftParenToken()) s, indices = self.mem.GetRegexIndices() @@ -88,9 +99,20 @@ def Call(self, rd): # This is guaranteed m = rd.PosMatch() - # TODO: Support strings for named captures - i = rd.OptionalInt(default_=0) - #val = rd.PosValue() + + group = rd.PosValue() + UP_group = group + with tagswitch(group) as case: + if case(value_e.Int): + group = cast(value.Int, UP_group) + i = group.i + elif case(value_e.Str): + group = cast(value.Str, UP_group) + # TODO: calculate from mem registers + i = 0 + else: + raise error.TypeErr(group, 'Expected Int or Str', + rd.LeftParenToken()) rd.Done() diff --git a/spec/ysh-regex-api.test.sh b/spec/ysh-regex-api.test.sh index 2e9025c7f1..da6120c959 100644 --- a/spec/ysh-regex-api.test.sh +++ b/spec/ysh-regex-api.test.sh @@ -101,15 +101,12 @@ if (x ~ / '-' /) { argv.py "${BASH_REMATCH[@]}" argv.py $[_group(0)] $[_group(1)] $[_group(2)] - argv.py $[_group()] # synonym for _group(0) - # TODO: Also test _start() and _end() } ## STDOUT: ['2020-08', '2020', '08'] ['2020-08', '2020', '08'] ['2020-08', '2020', '08'] -['2020-08'] ## END #### _group() returns null when group doesn't match @@ -128,7 +125,7 @@ shopt -s ysh:upgrade var s = 'foo123bar' if (s ~ /digit+/) { - echo start=$[_start()] end=$[_end()] + echo start=$[_start(0)] end=$[_end(0)] } echo --- @@ -271,7 +268,7 @@ shopt -s ysh:all var x = 'zz 2020-08-20' if (x ~ / '-' /) { - argv.py $[_group('year')] $[_grou_group('month')] + argv.py $[_group('year')] $[_group('month')] } ## STDOUT: ['2020', '08'] diff --git a/ysh/regex_translate.py b/ysh/regex_translate.py index b74d5d27a7..1f0d92f93d 100644 --- a/ysh/regex_translate.py +++ b/ysh/regex_translate.py @@ -13,7 +13,6 @@ re_e, re_repeat, re_repeat_e, - NameType, EggexFlag, ) from _devbuild.gen.id_kind_asdl import Id