Implement OnigRegExp.getCaptureTree natively

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-08-07 16:20:02 -07:00
parent 088b1a1398
commit 827b3e29d3
5 changed files with 46 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
(function() {
native function buildOnigRegExp(source);
native function search(string, index);
native function getCaptureTree(source, index);
native function getCaptureCount();
function OnigRegExp(source) {
@@ -12,6 +13,7 @@
}
OnigRegExp.prototype.search = search;
OnigRegExp.prototype.getCaptureTree = getCaptureTree;
OnigRegExp.prototype.getCaptureCount = getCaptureCount;
this.OnigRegExp = OnigRegExp;

View File

@@ -1,19 +0,0 @@
OnigRegExp.prototype.getCaptureTree = (string, startPosition) ->
buildCaptureTree = (captures, startPositions, totalCaptures=captures.length) ->
index = totalCaptures - captures.length
text = captures.shift()
startPosition = startPositions.shift()
endPosition = startPosition + text.length
tree = { index, text, position: startPosition }
childCaptures = []
while startPositions[0] < endPosition
subtree = buildCaptureTree(captures, startPositions, totalCaptures)
childCaptures.push(subtree) if subtree.text.length
tree.captures = childCaptures if childCaptures.length
tree
if match = @search(string, startPosition)
buildCaptureTree(match, match.indices)