Skip to content

Commit

Permalink
[eggex] Allow Int or Str arg for Match accessors
Browse files Browse the repository at this point in the history
Still need to calculate the index from the Str.
  • Loading branch information
Andy C committed Dec 17, 2023
1 parent 47be27c commit 0f6fc2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
38 changes: 30 additions & 8 deletions builtin/func_eggex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down
7 changes: 2 additions & 5 deletions spec/ysh-regex-api.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ if (x ~ /<capture d+> '-' <capture d+>/) {
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
Expand All @@ -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 ---

Expand Down Expand Up @@ -271,7 +268,7 @@ shopt -s ysh:all
var x = 'zz 2020-08-20'

if (x ~ /<capture d+ as year> '-' <capture d+ as month>/) {
argv.py $[_group('year')] $[_grou_group('month')]
argv.py $[_group('year')] $[_group('month')]
}
## STDOUT:
['2020', '08']
Expand Down
1 change: 0 additions & 1 deletion ysh/regex_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
re_e,
re_repeat,
re_repeat_e,
NameType,
EggexFlag,
)
from _devbuild.gen.id_kind_asdl import Id
Expand Down

0 comments on commit 0f6fc2a

Please sign in to comment.