Skip to content

Commit

Permalink
New
Browse files Browse the repository at this point in the history
Add group and groups method to the match object.  Also add the re and string attributes.
  • Loading branch information
bnmnetp committed Jan 28, 2013
1 parent c73d264 commit 8fd6edd
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/builtin.js

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions src/lib/re/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ var $builtinmodule = function(name)
var mod = {};

var matchobj = function($gbl, $loc) {
$loc.__init__ = new Sk.builtin.func(function(self,thematch) {
$loc.__init__ = new Sk.builtin.func(function(self,thematch, pattern, string) {
self.thematch = thematch;
self.re = pattern;
self.string = string;
});

$loc.groups = new Sk.builtin.func(function(self) {
return new Sk.builtin.tuple(self.thematch.v.slice(1))
});

$loc.group = new Sk.builtin.func(function(self,grpnum) {
if(grpnum >= self.thematch.v.length) {
throw new Sk.builtin.IndexError("Index out of range: " + grpnum);
}
return self.thematch.v[grpnum]
});

}

mod.MatchObject = Sk.misceval.buildClass(mod, matchobj, 'MatchObject', []);
Expand Down Expand Up @@ -41,15 +55,15 @@ var $builtinmodule = function(name)
var res = "/"+pattern.v.replace("/","\\/")+"/";
lst = mod._findre(res,string);
if ( lst.v.length < 1 ) return null;
var mob = Sk.misceval.callsim(mod.MatchObject, lst);
var mob = Sk.misceval.callsim(mod.MatchObject, lst, pattern, string);
return mob;
});

mod.match = new Sk.builtin.func(function(pattern, string, flags) {
var res = "/^"+pattern.v.replace("/","\\/")+"/";
lst = mod._findre(res,string);
if ( lst.v.length < 1 ) return null;
var mob = Sk.misceval.callsim(mod.MatchObject, lst);
var mob = Sk.misceval.callsim(mod.MatchObject, lst, pattern, string);
return mob;
});

Expand Down
7 changes: 7 additions & 0 deletions test/run/t340.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import re

m = re.match('([0-9]+)([a-z]+)','345abu')
print m.groups()
print m.group(0)
print m.group(1)
print m.group(2)
4 changes: 4 additions & 0 deletions test/run/t340.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
('345', 'abu')
345abu
345
abu
30 changes: 30 additions & 0 deletions test/run/t340.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: m
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: re
is_referenced: True
is_imported: True
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
53 changes: 53 additions & 0 deletions test/run/t340.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Module(body=[Import(names=[alias(name='re',
asname=None)]),
Assign(targets=[Name(id='m',
ctx=Store())],
value=Call(func=Attribute(value=Name(id='re',
ctx=Load()),
attr='match',
ctx=Load()),
args=[Str(s='([0-9]+)([a-z]+)'),
Str(s='345abu')],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Call(func=Attribute(value=Name(id='m',
ctx=Load()),
attr='groups',
ctx=Load()),
args=[],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Attribute(value=Name(id='m',
ctx=Load()),
attr='group',
ctx=Load()),
args=[Num(n=0)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Attribute(value=Name(id='m',
ctx=Load()),
attr='group',
ctx=Load()),
args=[Num(n=1)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Attribute(value=Name(id='m',
ctx=Load()),
attr='group',
ctx=Load()),
args=[Num(n=2)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True)])

0 comments on commit 8fd6edd

Please sign in to comment.