diff options
author | Benjamin Barenblat <bbaren@mit.edu> | 2015-08-26 17:29:32 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@mit.edu> | 2015-08-26 17:29:32 -0400 |
commit | bc6b0bee8fe4120642029daaa8ce6c069ef667b8 (patch) | |
tree | fab27db4556318e4f6a4b7c60e6b47c7ab58e59a /src/regex__FFI.js | |
parent | 75ff1a7a1979466a77dcc3acbbb88e897213027f (diff) |
Rework replacement API to rely on transformation
Redesign library API around highly general regex-based transformations.
Instead of specifying a string to substitute for each match, you
now execute an entire function over the match (and over nonmatching
regions as well). The resulting C++ code is much simpler, with more
functionality pushed into Ur, and the engine now supports certain
types of regex transformations needed to mimic Perl.
Diffstat (limited to 'src/regex__FFI.js')
-rw-r--r-- | src/regex__FFI.js | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/src/regex__FFI.js b/src/regex__FFI.js index d69369b..1062520 100644 --- a/src/regex__FFI.js +++ b/src/regex__FFI.js @@ -14,37 +14,42 @@ var UrWeb = { Regex: { -_compile: function(needle_string) { - var needle; +Substring: { + start: function(substring) { + return substring.start; + }, + + length: function(substring) { + return substring.length; + }, + + List: { + length: function(list) { + return list.length; + }, + + get: function(list, n) { + return list[n]; + }, + }, +}, + +doMatch: function(needle_string, haystack) { try { - needle = new RegExp(needle_string, "g"); + var needle = new RegExp(needle_string); } catch (e) { er("regex: compilation failed"); } - return needle; -}, - -succeeded: function(match) { - return !!match; -}, - -nSubexpressionMatches: function(match) { - return match.length - 1; -}, - -subexpressionMatch: function(match, n) { - if (match.length - 1 <= n) { - er("regex: match does not exist"); + var result = needle.exec(haystack); + if (result) { + for (var i = 0; i < result.length; i++) { + result[i] = {start: haystack.indexOf(result[i]), + length: result[i].length}; + } + } else { + result = [] } - return match[n + 1]; -}, - -doMatch: function(needle, haystack) { - return haystack.match(UrWeb.Regex._compile(needle)); -}, - -replace: function(needle, replacement, haystack) { - return haystack.replace(UrWeb.Regex._compile(needle), replacement); + return result; }, }}; // UrWeb.Regex |