Skip to content

Commit

Permalink
Start implementing posix classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jul 7, 2013
1 parent 4108429 commit 61ba7f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
5 changes: 5 additions & 0 deletions tests/objects/test_regexpobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_compile_regexps(self, space):
/(.*|.+)/
/(?<=b)/
/\\A|\\z/
/[[:alnum:]]/
""")

def test_regexp_syntax_errors(self, space):
Expand Down Expand Up @@ -204,6 +205,10 @@ def test_quantify_set(self, space):
w_res = space.execute("return /([0-9]){3,5}?/ =~ 'ab12345'")
assert space.int_w(w_res) == 2

def test_posix_class(self, space):
w_res = space.execute("return /[[:digit:]]/ =~ 'abc2'")
assert space.int_w(w_res) == 3

def test_quantify(self, space):
w_res = space.execute("return /a{2,4}/.match('aaaaaa').to_a")
assert self.unwrap(space, w_res) == ["aaaa"]
Expand Down
37 changes: 23 additions & 14 deletions topaz/utils/regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ def compile(self, ctx):
}
PROPERTIES = {
"digit": CATEGORY_DIGIT,
"alnum": CATEGORY_WORD,
}


Expand Down Expand Up @@ -1247,31 +1248,39 @@ def _parse_property(source, info, positive, in_set):
here = source.pos
if source.match("{"):
negate = source.match("^")
b = StringBuilder(5)
found = False
while True:
ch = source.get()
if ch == "}":
found = True
break
elif not ch:
break
else:
b.append(ch)
if found:
name = b.build()
prop_name, name = _parse_property_name(source)
if source.match("}"):
if name in PROPERTIES:
return Property(PROPERTIES[name], positive != negate)
source.pos = here
return make_character(info, ord("p" if positive else "P"), in_set)


def _parse_property_name(source):
b = StringBuilder(5)
here = source.pos
while True:
pos2 = source.pos
ch = source.get()
if ch.isalnum():
b.append(ch)
else:
source.pos = pos2
break
name = b.build()
return name, name


def _parse_numeric_escape(source, info, ch, in_set):
raise NotImplementedError("_parse_numeric_escape")


def _parse_posix_class(source, info):
raise NotImplementedError("_parse_posix_class")
negate = source.match("^")
prop_name, name = _parse_property_name(source)
if not source.match(":]"):
raise ParseError
return Property(PROPERTIES[name], negate)


def _compile_no_cache(pattern, flags):
Expand Down

0 comments on commit 61ba7f8

Please sign in to comment.