jalexiscv / base2

Automatically exported from code.google.com/p/base2
0 stars 0 forks source link

RegGrp: add match index and complete to replacement call #26

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
At the moment, replacement functions for RegGrp don't know at what index the 
match 
happened. This can be useful when for example a RegGrp implementation is used 
as parser. With 
that info, the position of a parse error can be used.

Below a diff of my solution. It basically calls the replacement function as it 
was directly called 
from String.replace. Initially I only added the lastIndex/offset argument, but 
to be complete, the 
whole string is added as well.

Index: RegGrp.js
===========================================================
========
--- RegGrp.js  (revision 104)
+++ RegGrp.js  (working copy)
@@ -26,7 +26,9 @@
             var replacement = match.replacement;
             switch (typeof replacement) {
               case "function":
-                return replacement.apply(self, slice(arguments, offset));
+                var matches = slice(arguments, offset, match.length + offset + 
1);
+                var matchIndex = arguments[arguments.length - 2];
+                return replacement.apply(self, matches.concat(matchIndex, 
string));
               case "number":
                 return arguments[offset + replacement];
               default:

Original issue reported on code.google.com by doek...@gmail.com on 17 Jul 2007 at 5:32

GoogleCodeExporter commented 9 years ago
Forgot the labels

Original comment by doek...@gmail.com on 17 Jul 2007 at 8:17

GoogleCodeExporter commented 9 years ago
I like it. I originally wrote the exec() method as if it were the replace() 
method of
a string. I changed it because I didn't want base2 to mess with 
String.prototype.
I'll manually add your changes to my local copy (I just changed RegGrp myself 
but
nothing so cool).

Good work.

Original comment by dean.edw...@gmail.com on 17 Jul 2007 at 9:15

GoogleCodeExporter commented 9 years ago
Thanks.

You also might want to remove a comment from RegGrp/Item.js:
        //  - add one because each pattern is itself a sub-expression
and this variable look dead to me:
                var i = this.length + 1;

Original comment by doek...@gmail.com on 17 Jul 2007 at 9:37

GoogleCodeExporter commented 9 years ago
I'm taking ownership of this bug. I'll try to merge in your changes soon.

Original comment by dean.edw...@gmail.com on 17 Jul 2007 at 9:38

GoogleCodeExporter commented 9 years ago
OK. I've checked in your changes.

Original comment by dean.edw...@gmail.com on 23 Jul 2007 at 9:06