Permalink
Browse files

Implement BASH_REMATCH with the libc.regex_match() function.

Fixes #133.
  • Loading branch information...
Andy Chu
Andy Chu committed Jun 7, 2018
1 parent 440356a commit 17cf4c901ecc690e6c0c5ec11a98a48daf90f816
Showing with 22 additions and 15 deletions.
  1. +12 −15 core/expr_eval.py
  2. +10 −0 spec/regex.test.sh
View
@@ -17,10 +17,10 @@
except ImportError:
from benchmarks import fake_libc as libc
from osh.meta import BOOL_ARG_TYPES, Id, types
from core import util
from core import state
from osh.meta import BOOL_ARG_TYPES, Id, types
from osh.meta import runtime
from osh.meta import ast
log = util.log
@@ -444,7 +444,7 @@ class BoolEvaluator(_ExprEvaluator):
def _SetRegexMatches(self, matches):
"""For ~= to set the BASH_REMATCH array."""
self.mem
state.SetGlobalArray(self.mem, 'BASH_REMATCH', matches)
def _EvalCompoundWord(self, word, do_fnmatch=False):
"""
@@ -606,22 +606,19 @@ def Eval(self, node):
return s1 != s2
if op_id == Id.BoolBinary_EqualTilde:
# NOTE: regex matching can't fail if compilation succeeds.
match = libc.regex_match(s2, s1)
# TODO: BASH_REMATCH or REGEX_MATCH
if match == 1:
self._SetRegexMatches('TODO')
is_match = True
elif match == 0:
is_match = False
elif match == -1:
matches = libc.regex_match(s2, s1)
# NOTE: regex matching shouldn't fail if compilation succeeds.
if matches == -1:
raise AssertionError(
"Invalid regex %r: should have been caught at compile time" %
s2)
else:
raise AssertionError
return is_match
if matches is None:
return False
self._SetRegexMatches(matches)
return True
if op_id == Id.Redir_Less: # pun
return s1 < s2
View
@@ -34,6 +34,16 @@
# From bash code: ( | ) are treated special. Normally they must be quoted, but
# they can be UNQUOTED in BASH_REGEX state. In fact they can't be quoted!
### BASH_REMATCH
[[ foo123 =~ ([a-z]+)([0-9]+) ]]
argv.py "${BASH_REMATCH[@]}"
## STDOUT:
['foo123', 'foo', '123']
## END
## N-I zsh STDOUT:
['']
## END
### Match is unanchored at both ends
[[ 'bar' =~ a ]] && echo true
# stdout: true

0 comments on commit 17cf4c9

Please sign in to comment.