Skip to content

Commit c76edef

Browse files
committed
Added new buildTree function for general tree building from a given rule.
Rule.apply is now used in all cases, with the default case implementing the previously default behaviour.
1 parent 0908a26 commit c76edef

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

source/jquery.syntax.core.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Syntax.extractMatches = function() {
196196

197197
if (match[index].length > 0) {
198198
if (rule.brush) {
199-
matches.push(Syntax.brushes[rule.brush].buildTree(match[index], RegExp.indexOf(match, index)));
199+
matches.push(Syntax.Brush.buildTree(rule, match[index], RegExp.indexOf(match, index)));
200200
} else {
201201
var expression = jQuery.extend({owner: expr.owner}, rule);
202202

@@ -789,8 +789,8 @@ Syntax.Brush = function () {
789789
Syntax.Brush.prototype.derives = function (name) {
790790
this.parents.push(name);
791791
this.rules.push({
792-
apply: function(text, expr, offset) {
793-
return Syntax.brushes[name].getMatches(text, offset);
792+
apply: function(text, expr) {
793+
return Syntax.brushes[name].getMatches(text);
794794
}
795795
});
796796
}
@@ -828,6 +828,32 @@ Syntax.Brush.convertStringToTokenPattern = function (pattern, escape) {
828828
return prefix + pattern + postfix;
829829
}
830830

831+
Syntax.Brush.MatchPattern = function (text, rule) {
832+
if (!rule.pattern)
833+
return;
834+
835+
// Duplicate the pattern so that the function is reentrant.
836+
var matches = [], pattern = new RegExp;
837+
pattern.compile(rule.pattern);
838+
839+
while((match = pattern.exec(text)) !== null) {
840+
if (rule.matches) {
841+
matches = matches.concat(rule.matches(match, rule));
842+
} else if (rule.brush) {
843+
matches.push(Syntax.Brush.buildTree(rule, match[0], match.index));
844+
} else {
845+
matches.push(new Syntax.Match(match.index, match[0].length, rule, match[0]));
846+
}
847+
848+
if (rule.incremental) {
849+
// Don't start scanning from the end of the match..
850+
pattern.lastIndex = match.index + 1;
851+
}
852+
}
853+
854+
return matches;
855+
}
856+
831857
Syntax.Brush.prototype.push = function () {
832858
if (jQuery.isArray(arguments[0])) {
833859
var patterns = arguments[0], rule = arguments[1];
@@ -862,6 +888,9 @@ Syntax.Brush.prototype.push = function () {
862888
if (typeof(XRegExp) !== 'undefined') {
863889
rule.pattern = new XRegExp(rule.pattern);
864890
}
891+
892+
// Default pattern extraction algorithm
893+
rule.apply = rule.apply || Syntax.Brush.MatchPattern;
865894

866895
if (rule.pattern && rule.pattern.global || typeof(rule.pattern) == 'undefined') {
867896
this.rules.push(jQuery.extend({owner: this}, rule));
@@ -871,45 +900,16 @@ Syntax.Brush.prototype.push = function () {
871900
}
872901
};
873902

874-
Syntax.Brush.prototype.getMatchesForRule = function (text, rule, offset) {
903+
Syntax.Brush.prototype.getMatchesForRule = function (text, rule) {
875904
var matches = [], match = null;
876905

877906
// Short circuit (user defined) function:
878907
if (typeof(rule.apply) != 'undefined') {
879-
return rule.apply(text, rule, offset);
880-
}
881-
882-
if (typeof(rule.pattern) == 'undefined') {
883-
return matches;
884-
}
885-
886-
// Duplicate the pattern so that the function is reentrant.
887-
var pattern = new RegExp;
888-
pattern.compile(rule.pattern);
889-
890-
while((match = pattern.exec(text)) !== null) {
891-
if (rule.matches) {
892-
matches = matches.concat(rule.matches(match, rule));
893-
} else if (rule.brush) {
894-
matches.push(Syntax.brushes[rule.brush].buildTree(match[0], match.index));
895-
} else {
896-
matches.push(new Syntax.Match(match.index, match[0].length, rule, match[0]));
897-
}
898-
899-
if (rule.incremental) {
900-
// Don't start scanning from the end of the match..
901-
pattern.lastIndex = match.index + 1;
902-
}
903-
}
904-
905-
if (offset && offset > 0) {
906-
for (var i = 0; i < matches.length; i += 1) {
907-
matches[i].shift(offset);
908-
}
908+
matches = rule.apply(text, rule);
909909
}
910910

911911
if (rule.debug) {
912-
Syntax.log("matches", matches);
912+
Syntax.log("Syntax matches:", rule, text, matches);
913913
}
914914

915915
return matches;
@@ -935,13 +935,27 @@ Syntax.Brush.prototype.getMatches = function(text, offset) {
935935
return matches;
936936
};
937937

938+
Syntax.Brush.buildTree = function(rule, text, offset, additionalMatches) {
939+
var match = Syntax.brushes[rule.brush].buildTree(text, offset, additionalMatches);
940+
941+
jQuery.extend(match.expression, rule);
942+
943+
return match;
944+
}
945+
938946
Syntax.Brush.prototype.buildTree = function(text, offset, additionalMatches) {
939947
offset = offset || 0;
940948

941949
// Fixes code that uses \r\n for line endings. /$/ matches both \r\n, which is a problem..
942950
text = text.replace(/\r/g, '');
943951

944-
var matches = this.getMatches(text, offset);
952+
var matches = this.getMatches(text);
953+
954+
if (offset && offset > 0) {
955+
for (var i = 0; i < matches.length; i += 1) {
956+
matches[i].shift(offset);
957+
}
958+
}
945959

946960
var top = new Syntax.Match(offset, text.length, {klass: this.allKlasses().join(" "), allow: '*', owner: this}, text);
947961

0 commit comments

Comments
 (0)